def randomLedColor(): from gfxhat import backlight from random import randint for x in range(6): backlight.set_pixel(x, randint(0, 255), randint(0, 255), randint(0, 255)) backlight.show()
def off(): for x in range(6): backlight.set_pixel(x, 0, 0, 0) touch.set_led(x, 0) backlight.show() lcd.clear() lcd.show()
def handler(ch, event): if event == 'press': led_states[ch] = not led_states[ch] touch.set_led(ch, led_states[ch]) if led_states[ch]: backlight.set_pixel(ch, 0, 255, 255) else: backlight.set_pixel(ch, 0, 255, 0) backlight.show()
def start(self): for x in range(6): touch.set_led(x, 1) time.sleep(0.1) touch.set_led(x, 0) for x in range(6): backlight.set_pixel(x, 255, 79, 193) backlight.show()
def reboot(self): for x in range(6): backlight.set_pixel(x, 0, 0, 0) touch.set_led(x, 0) backlight.show() lcd.clear() drive = clsDrive.Drive() drive.stop() lcd.show() call("sudo reboot", shell=True)
def test_backlight_set_pixel(sn3218): """Test that set_pixel sets the right buffer elements.""" from gfxhat import backlight with pytest.raises(ValueError): backlight.set_pixel(9, 255, 255, 255) backlight.set_pixel(0, 255, 255, 255) # TODO we probably shouldn't use implementation details to test the API # but with the absence of a corresponding `get_pixel`... offset = backlight.LED_MAP[0] * 3 assert backlight._buf[offset:offset + 3] == [255, 255, 255]
def Menu(): global menuOptions, cur menuOptions = addOptions() ShowOption(menuOptions[cur]) for x in range(6): touch.on(x, handler) try: signal.pause() except KeyboardInterrupt: for x in range(6): backlight.set_pixel(x, 0, 0, 0) touch.set_led(x, 0) backlight.show() lcd.clear() lcd.show()
def backlight_screen(self, channel, event): time.sleep(0.5) if self.backlight == True: for x in range(6): backlight.set_pixel(x, 0, 0, 0) backlight.show() self.backlight = False touch.set_led(2, 0) print("Backlight status: ", self.backlight) return self.backlight else: for x in range(6): backlight.set_pixel(x, 255, 79, 193) backlight.show() self.backlight = True touch.set_led(2, 1) print("Backlight status: ", self.backlight) return self.backlight
def backlight_clear_reset(): from gfxhat import touch, lcd, backlight from time import sleep lcd.clear() lcd.show() i = '0' if i == '0': backlight.set_all(220, 130, 110) backlight.show() sleep(1) backlight.set_all(120, 230, 110) backlight.show() sleep(1) backlight.set_all(0, 0, 110) backlight.show() sleep(3) for x in range(6): backlight.set_pixel(x, 0, 0, 0) touch.set_led(x, 0) backlight.show() lcd.clear()
def etchaske(x, y): backlight.set_pixel(0, 0, 255, 0) backlight.show() while True: key = getchar() print(key) lcd.set_pixel(x, y, 1) lcd.show() if key == 's': clearScreen(lcd) elif key == 'w': y = y - 1 if y == 0: y = 63 lcd.set_pixel(x, y, 1) lcd.show() elif key == 'x': y = y + 1 if y == 63: y = 0 lcd.set_pixel(x, y, 1) lcd.show() elif key == 'a': x = x - 1 if x == 0: x = 127 lcd.set_pixel(x, y, 1) lcd.show() elif key == 'd': x = x + 1 if x == 127: x = 0 lcd.set_pixel(x, y, 1) lcd.show() elif key == 'q': lcd.clear() lcd.show() exit() else: print("Not a valid input")
def __init__(self, config, display): # self.playing = False self.myConfig = config self.myDisplay = display for x in range(6): backlight.set_pixel(x, 0, 128, 0) backlight.show() def handlerVolumeUp(ch, event): if event == 'press': logging.info("Volume Up") volume = self.myConfig.getVolume() volume = volume + 5 os.system("mpc volume %d" % (volume)) self.myDisplay.drawVolume(volume) self.myConfig.setVolume(volume) def handlerVolumeDown(ch, event): if event == 'press': logging.info("Volume Down") volume = self.myConfig.getVolume() volume = volume - 5 os.system("mpc volume %d" % (volume)) self.myDisplay.drawVolume(volume) self.myConfig.setVolume(volume) def handler2(ch, event): if event == 'press': logging.info("Press on button 2") def handler3(ch, event): if event == 'press': logging.info("Press on button 3") def handlerOnOff(ch, event): if event == 'press': if not self.myConfig.getPlayState(): # if logging.info("On") #os.system("mpc play") self.myConfig.setPlayState(True) self.setPlaying(self.myConfig.getPlayState()) else: logging.info("Off") #os.system("mpc stop") self.myConfig.setPlayState(False) self.setPlaying(self.myConfig.getPlayState()) def handlerOff(ch, event): if event == 'press': logging.info("Off") os.system("mpc stop") logging.info("Registering button handlers....") touch.on(0, handlerVolumeUp) touch.on(1, handlerVolumeDown) touch.on(2, handler2) touch.on(3, handler3) touch.on(4, handlerOnOff) touch.on(5, handlerOff) self.setPlaying(True)
def init_screen(self): for x in range(6): touch.set_led(x, 0) backlight.set_pixel(x, 255, 255, 255) touch.on(x, self.handler)
def GFXbacklight(r, g, b): for x in range(6): backlight.set_pixel(x, r, g, b) backlight.show()
for y in range(56,8,-1): lcd.set_pixel(x, y, 0) for x in range(2,6): for y in range(56,int(56-(8*Volume)),-1): lcd.set_pixel(x+(y % 2), y, x % 2) backlight.show() lcd.show() def LedOnOff(): for x in range(6): touch.set_led(x, 1) time.sleep(0.1) touch.set_led(x, 0) for x in range(6): backlight.set_pixel(x, 0, 0, 0) touch.on(x, handler) for x in range(128): for y in range(64): pixel = image.getpixel((x, y)) lcd.set_pixel(x, y, pixel) lcd.show() backlight.show() # VERSION 1.1: added volume control for screen # VERSION 1.0: added audio mode + percent of random vocal to be sent instead of text
import socket # For Pinging from urllib2 import urlopen, URLError, HTTPError # For pinging from gfxhat import touch, lcd, backlight, fonts from PIL import Image, ImageFont, ImageDraw #Inital Variables text = ('Booting..') router = 0 dnspi = 0 internet = '1' backlightr = "0" #Inital Backlight set backlight.set_pixel( 2, 0, 255, 0) #Backlight colour (I think first is brightness, 0-5, then RGB) backlight.show() #Write pixels function def write(text): led_states = [False for _ in range(6)] width, height = lcd.dimensions() image = Image.new('P', (width, height)) draw = ImageDraw.Draw(image) font = ImageFont.truetype(fonts.AmaticSCBold, 25) #Make sure to set the text variable w, h = font.getsize(text) x = (width - w) // 2 y = (height - h) // 2
def main(): print("""display-temp.py""") print('Connecting to {}'.format(SENSORTAG_ADDRESS)) tag = SensorTag(SENSORTAG_ADDRESS) print("Connection successfull!") print("Initializing GFX-HAT...") width, height = lcd.dimensions() image = Image.new('P', (width, height)) draw = ImageDraw.Draw(image) for x in range(6): touch.set_led(x, 0) backlight.set_pixel(x, 255, 255, 255) # touch.on(x, handler) backlight.set_all(155, 155, 155) backlight.show() atexit.register(cleanup) print("Reporting started every {0} seconds".format(FREQUENCY_SECONDS)) try: while True: image.paste(0, (0, 0, width, height)) readings = get_readings(tag) if not readings: print("SensorTag disconnected. Reconnecting.") reconnect(tag) continue # print readings print("Time:\t{}".format(datetime.datetime.now())) print("IR reading:\t\t{}, temperature:\t{}".format( readings["ir"], readings["ir_temp"])) print("Humidity reading:\t{}, temperature:\t{}".format( readings["humidity"], readings["humidity_temp"])) print("Barometer reading:\t{}, temperature:\t{}".format( readings["pressure"], readings["baro_temp"])) print("Luxmeter reading:\t{}".format(readings["light"])) fields = [ datetime.datetime.now(), readings['ir'], readings['ir_temp'], readings['humidity'], readings['humidity_temp'], readings['pressure'], readings['baro_temp'], readings['light'] ] with open(r'data.csv', 'a') as f: writer = csv.writer(f) writer.writerow(fields) # Drawing IP and Hostname # draw.text((2,1), "IP: " + get_ip(), 1, font) # draw.text((2,10), "Hostname: " + get_hostname(), 1, font) now = datetime.datetime.now() current_time = now.strftime("%H:%M:%S") draw.text((2, 1), "Time : " + str(current_time), 1, font) draw.text((2, 10), "IR (temp): " + str(readings["ir_temp"]) + "C", 1, font) draw.text((2, 20), "Hum (temp): " + str(readings["humidity_temp"]) + "C", 1, font) draw.text((2, 30), "Bar (temp): " + str(readings["baro_temp"]) + "C", 1, font) draw.text((2, 40), "Luxometer : " + str(readings["light"]) + "lux", 1, font) draw.text((2, 50), "IP: " + get_ip(), 1, font) mean_temp = float(readings['ir_temp']) + float( readings['humidity_temp']) + float(readings['baro_temp']) mean_temp /= 3.0 if mean_temp > 28.0: backlight.set_all(155, 0, 0) elif mean_temp > 27.0: backlight.set_all(155, 75, 75) elif mean_temp < 24.0: backlight.set_all(100, 100, 155) elif mean_temp < 23.0: backlight.set_all(50, 50, 155) elif mean_temp < 22.0: backlight.set_all(0, 0, 155) else: backlight.set_all(155, 155, 155) backlight.show() for x in range(width): for y in range(height): pixel = image.getpixel((x, y)) lcd.set_pixel(x, y, pixel) lcd.show() # time.sleep(1.0 / 30) time.sleep(FREQUENCY_SECONDS) except KeyboardInterrupt: cleanup()
def handler(ch, event): global current_menu_option, trigger_action if event != 'press': return if ch == 1: current_menu_option += 1 if ch == 0: current_menu_option -= 1 if ch == 4: trigger_action = True current_menu_option %= len(menu_options) for x in range(6): touch.set_led(x, 0) backlight.set_pixel(x, 255, 255, 255) touch.on(x, handler) backlight.show() def cleanup(): backlight.set_all(0, 0, 0) backlight.show() lcd.clear() lcd.show() atexit.register(cleanup) def main_loop():
def enable_backlight(self): for x in range(6): backlight.set_pixel(x, 50, 100, 100) backlight.show()