def start(self): # change these as desired - they're the pins connected from the # SPI port on the ADC to the Cobbler SPICLK = RaspberryPi.SPI_CE1 #CLK SPIMISO = RaspberryPi.SPI_CE0 #DOUT SPIMOSI = RaspberryPi.SPI_SCLK #DIN SPICS = RaspberryPi.SPI_MISO #CS # set up the SPI interface pins GPIO.setup(SPIMOSI, GPIO.OUT) GPIO.setup(SPIMISO, GPIO.IN) GPIO.setup(SPICLK, GPIO.OUT) GPIO.setup(SPICS, GPIO.OUT) # 10k trim pot connected to adc #0 potentiometer_adc = 0; last_read = 0 # this keeps track of the last potentiometer value tolerance = 5 # to keep from being jittery we'll only change # volume when the pot has moved more than 5 'counts' buzzer = Buzzer() lcd = LCD() lcd.Blink() printed = False try: while True: # we'll assume that the pot didn't move trim_pot_changed = False # read the analog pin trim_pot = self.readadc(potentiometer_adc, SPICLK, SPIMOSI, SPIMISO, SPICS) # how much has it changed since the last read? pot_adjust = abs(trim_pot - last_read) if DEBUG: print "trim_pot:", trim_pot print "pot_adjust:", pot_adjust print "last_read", last_read if ( pot_adjust > tolerance ): trim_pot_changed = True if DEBUG: print "trim_pot_changed", trim_pot_changed if ( trim_pot_changed ): value = trim_pot / 10.24 # convert 10bit adc0 (0-1024) trim pot read into 0-100 volume level value = round(value) # round out decimal value value = int(value) # cast volume as integer number = ''; printedNumber = '' released = False if ( value != 60): if (value >= 75 and value < 80): number ='1' elif (value >= 69 and value < 74): number ='2' elif (value >= 64 and value <= 70): number ='3' elif (value >= 0 and value <= 12): number ='4' elif (value >= 32 and value <= 37): number ='5' elif (value >= 38 and value <= 44): number ='6' elif (value >= 21 and value <= 26): number ='7' elif (value >= 83 and value <= 90): number ='8' elif (value >= 45 and value <= 51): number ='9' elif (value >= 54 and value <= 59): number ='*' elif (value >= 94 and value <= 100): number ='0' elif (value >= 50 and value <= 53): number ='#' else: print "Dunno: ", value if (value == 60): released = True printed = False if(number != printedNumber and not printed): printedNumber = number print number, value lcd.message(number) buzzer.beep(659, 125) printed = True time.sleep(0.1) if(number == '*'): self.typePassword = 1 self.password = '' elif(number == '#' and self.typePassword == 1): lcd.noBlink() self.typePassword = 0 user = self.database.check_login(self.password) print user sleep_time = 1 if(user != 'Invalid Code'): sleep_time = 5 self.led.show_status_light = 1 system_status = self.database.system_status() if(system_status == '1'): self.sp.stop_timer() self.sp.soundingAlarm = 0 self.database.system_disarm() message = 'User '+user['first_name']+' '+user['last_name']+' disarmed the system' print message self.database.log(message) print user['first_name']+' '+user['last_name'] user = '******'+user['first_name']+' '+user['last_name'] else: self.database.system_arm() user = user['first_name']+' '+user['last_name']+'\nArmed The System' self.database.log(user) time.sleep(0.5) lcd.clear() lcd.message(user) self.message = '' time.sleep(sleep_time) lcd.clear() lcd.Blink() if(self.password == '1111'): print 'play mario' mario_thread = threading.Thread(target=self.play_song(),) print 'die mario' mario_thread.start() elif(self.typePassword == 1): if(number != '*'): self.password += number time.sleep(0.01) except KeyboardInterrupt: GPIO.cleanup() print "\nKill"
#!/usr/bin/python # https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_CharLCD from LCD import LCD from subprocess import * from time import sleep, strftime from datetime import datetime lcd = LCD() cmd = "ip addr show wlan0 | grep inet | awk '{print $2}' | cut -d/ -f1" lcd.begin(16, 1) def run_cmd(cmd): p = Popen(cmd, shell=True, stdout=PIPE) output = p.communicate()[0] return output.split('\n')[0] while 1: lcd.clear() ipaddr = run_cmd(cmd) lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n')) lcd.message('IP %s' % (ipaddr)) sleep(1)
class Keypad: def __init__(self, database): self.pins = [RaspberryPi.GPIO0, RaspberryPi.GPIO1, RaspberryPi.GPIO2, RaspberryPi.GPIO3, RaspberryPi.GPIO4, RaspberryPi.GPIO5, RaspberryPi.GPIO6] self.active = 0 pin0 = {RaspberryPi.GPIO1: '2', RaspberryPi.GPIO6: '5', RaspberryPi.GPIO5: '8', RaspberryPi.GPIO3: '0'} pin2 = {RaspberryPi.GPIO1: '1', RaspberryPi.GPIO6: '4', RaspberryPi.GPIO5: '7', RaspberryPi.GPIO3: '*'} pin4 = {RaspberryPi.GPIO1: '3', RaspberryPi.GPIO6: '6', RaspberryPi.GPIO5: '9', RaspberryPi.GPIO3: '#'} self.lookup = {RaspberryPi.GPIO0: pin0, RaspberryPi.GPIO2: pin2, RaspberryPi.GPIO4: pin4} self.message = '' self.password = '' self.typePassword = 0 for i in range(3): GPIO.setup(self.pins[::2][i], GPIO.OUT, initial=GPIO.LOW) GPIO.setup(self.pins[1::2][i], GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(self.pins[6], GPIO.IN, pull_up_down=GPIO.PUD_DOWN) self.buzzer = Buzzer() self.lcd = LCD() self.database = database self.setup_callbacks() self.loop() def setup_callbacks(self): for i in range(3): GPIO.add_event_detect(self.pins[1::2][i], GPIO.RISING, callback=self.callback, bouncetime=200) GPIO.add_event_detect(self.pins[6], GPIO.RISING, callback=self.callback, bouncetime=200) def callback(self, channel): if (GPIO.input(channel)): char = self.lookup[self.active][channel] #self.buzzer.beep(659, 125) print 'PRESS: ', char self.message += char self.lcd.clear() self.lcd.message(self.message) if(char == '*'): self.typePassword = 1 self.password = '' elif(char == '#' and self.typePassword == 1): self.typePassword = 0 user = self.database.check_login(self.password) sleep(0.5) self.lcd.clear() self.lcd.message(user) self.message = '' sleep(3) self.lcd.clear() elif(self.typePassword == 1): self.password += char def loop(self): while True: for i in range(3): self.active = self.pins[::2][i] GPIO.output(self.active, True) sleep(0.01) GPIO.output(self.active, False)
#!/usr/bin/python # https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_CharLCD from LCD import LCD from subprocess import * from time import sleep, strftime from datetime import datetime lcd = LCD() cmd = "ip addr show wlan0 | grep inet | awk '{print $2}' | cut -d/ -f1" lcd.begin(16,1) def run_cmd(cmd): p = Popen(cmd, shell=True, stdout=PIPE) output = p.communicate()[0] return output.split('\n')[0] while 1: lcd.clear() ipaddr = run_cmd(cmd) lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n')) lcd.message('IP %s' % ( ipaddr ) ) sleep(1)
cur_song = "" cur_art = "" cur_song_idx = 0 cur_art_idx = 0 while True: time.sleep(0.2) st = client.status() current = client.currentsong() if st["state"] == "play": try: line = "{0} v {1}% {2}".format( st["state"], st["volume"], parse_audio(st['audio'])) lcd.message(line, 1) lcd.message("{0}".format(show_time(st)), 2) if cur_art != current_song(current)[1]: cur_art_idx = 0 cur_song_idx = 0 cur_song = current_song(current)[0] cur_art = current_song(current)[1] disp_song, song_new_idx = display_text(cur_song, cur_song_idx) disp_art, art_new_idx = display_text(cur_art, cur_art_idx) cur_song_idx = song_new_idx cur_art_idx = art_new_idx lcd.message(disp_song, 3) lcd.message(disp_art, 4) except KeyError: pass else:
import time import requests from LCD import LCD URL = 'http://localhost:8000' LCD_ADDRESS = 0x27 lcd = LCD(2, LCD_ADDRESS, True) lcd.clear() counter = 0 while True: try: data = requests.get(URL).json() temp_text = '{:.1f}C'.format(data['temperature']) hum_text = '{:.1f}%'.format(data['humidity']) counter = (counter + 1) % 11 temp_c = 0 while temp_c < counter: temp_text = ' '+temp_text hum_text = ' '+hum_text temp_c += 1 lcd.clear() lcd.message(temp_text, 1) lcd.message(hum_text, 2) except: lcd.clear() lcd.message("Service", 1) lcd.message("Unavailable", 2) time.sleep(3)