def loop(self): colored = False try: with display.open() as disp: while True: localtime = utime.localtime() self.updateClock(disp, localtime) if self.run_once: break # check for button presses v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) button_pressed = v != 0 if button_pressed and v & buttons.BOTTOM_LEFT != 0: self.setTheme(self.theme - 1) self.writeConfig() elif button_pressed and v & buttons.BOTTOM_RIGHT != 0: self.setTheme(self.theme + 1) self.writeConfig() elif button_pressed and v & buttons.TOP_RIGHT != 0: self.setTime(disp, localtime) utime.sleep_ms(23) except KeyboardInterrupt: for i in range(11): leds.set(i, (0, 0, 0)) return
def loop(self): try: with display.open() as disp: button_pressed = False while True: localtime = utime.localtime() disp.clear() disp = self.clock.updateClock(disp, localtime[3], localtime[4], localtime[5]) disp.update() # check for button presses v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT) if v == 0: button_pressed = False if not button_pressed and v & buttons.BOTTOM_LEFT != 0: button_pressed = True self.clock.cycleTheme(-1) elif not button_pressed and v & buttons.BOTTOM_RIGHT != 0: button_pressed = True self.clock.cycleTheme(1) sleep(self.timeout) except KeyboardInterrupt: for i in range(11): leds.set(i, (0, 0, 0)) return
def loop(self): colored = False try: with display.open() as disp: button_pressed = False while True: self.updateClock(disp) if self.run_once: break # check for button presses v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT) if v == 0: button_pressed = False if not button_pressed and v & buttons.BOTTOM_LEFT != 0: button_pressed = True self.setTheme(self.theme - 1) self.writeConfig() elif not button_pressed and v & buttons.BOTTOM_RIGHT != 0: button_pressed = True self.setTheme(self.theme + 1) self.writeConfig() except KeyboardInterrupt: for i in range(11): leds.set(i, (0, 0, 0)) return
def blink_led(led): """ Turns off leds, blinks given led for 100ms can be used as an indicator :param led: led to blink """ leds.clear() utime.sleep(0.1) leds.set(led, [255, 0, 0]) utime.sleep(0.1) leds.clear()
def loop(self): colored = False try: with display.open() as disp: while True: self.updateClock(disp) if self.run_once: break except KeyboardInterrupt: for i in range(11): leds.set(i, (0, 0, 0)) return
def execute(cmd_str, LEDData = None, source = None): global BackStatus, FrontStatus if cmd_str == "set": BackStatus = Custom FrontStatus = Custom leds.set(LEDData) if cmd_str == "off": if BackStatus == Off and FrontStatus == Off: return allOff() if cmd_str == "brake": FrontStatus = Off front() tmp = BackStatus BackStatus = Full back() time.sleep(1) if tmp == Full: tmp = Off BackStatus = tmp back() if cmd_str == "dimmed": if BackStatus == Dimmed or FrontStatus == Dimmed: return BackStatus = Dimmed FrontStatus = Dimmed front() back() if cmd_str == "flash": tmp = FrontStatus FrontStatus = Full front() time.sleep(1) FrontStatus = tmp front() if cmd_str == "forward": FrontStatus = Full front()
def AirQualityLED(gas_quality, numLEDs): if (gas_quality < 50): QualityColor = htmlcolor.GREEN elif (gas_quality < 100): QualityColor = htmlcolor.YELLOW elif (gas_quality < 150): QualityColor = htmlcolor.ORANGE elif (gas_quality < 200): QualityColor = htmlcolor.RED elif (gas_quality < 250): QualityColor = htmlcolor.CRIMSON else: QualityColor = htmlcolor.PURPLE LEDcount = round(gas_quality / 200 * numLEDs) leds.clear() for i in range(0, 10 - LEDcount): leds.set(i, QualityColor) leds.update()
def updateClock(self, disp, hour, minute, second): disp = draw_number(disp, hour // 10, offsetx=3, offsety=5) disp = draw_number(disp, hour % 10, offsetx=(32 + 6) + 3) if second % 2: disp = draw_colon(disp, offsetx=(32 + 6) * 2 + 2 + 3) if self.blinking: self.led1 = toggle_led1(self.led1) fill_leds(second // 6) if second % 60 == 0: for led in range(10): leds.set(led + 1, (0, 0, 0)) v = buttons.read(buttons.TOP_RIGHT) if v: if self.blinking: leds.set(0, (0, 0, 0)) self.blinking = not self.blinking disp = draw_number(disp, minute // 10, offsetx=(32 + 6) * 2 + 8 + 3) disp = draw_number(disp, minute % 10, offsetx=(32 + 6) * 3 + 8 + 3) return disp
def draw_event(event, disp, offset, daysCount): disp.clear() disp.rect(0, 0, 159, 20, col=color.CHAOSBLUE) disp.rect(0, 20, 159, 80, col=color.CHAOSBLUE) for i in range(daysCount): if i == int(event["day"]) - 1: leds.set(10-i, [0, 255, 0]) else: leds.set(10-i, [255, 0, 0]) title = event['title'] + " -- " linelen = len(title) offset %= linelen post_title = "" if offset + MAXCHARS > linelen: post_title = title[:(offset + MAXCHARS - linelen)] disp.print(title[offset: offset + MAXCHARS] + post_title, posy=0, fg=color.CAMPGREEN, bg=color.CHAOSBLUE) disp.print("D " + event["day"] + " - " + event['start'], posy=20, bg=color.CHAOSBLUE) disp.print(event['room'], posy=40, bg=color.CHAOSBLUE) e_duration = event["duration"] duration = "" if e_duration[0] != '0': duration += e_duration[0:2] + "h" elif e_duration[1] != '0': duration += e_duration[1] + "h" if e_duration[3] != '0': duration += e_duration[3:5] + "m" elif e_duration[4] != '0': duration += e_duration[4] + "m" disp.print(duration + " long", posy=60, bg=color.CHAOSBLUE) disp.update()
def loop(): global beep_off_if_no_location_timestamp location = get_location() if location: timestamp = get_millis() lines = [] lines.append("%s:%d" % (get_status(location), location.hacc)) lines.append("TIME: %d" % (location.time)) lines.append("LAT: %f" % (location.lat)) lines.append("LON: %f" % (location.lon)) if (location.hacc <= 100): beep_off_if_no_location_timestamp = get_millis() nearest_station = get_nearest_station(location, stations) if nearest_station: distance = get_distance(location, nearest_station) lines.append("%.1fkm in %s" % (distance, nearest_station.name)) lines.append("%.1fkm/h" % (location.speed)) if (distance <= 1.0 and not leds.is_on()): leds.set(True) if (distance > 1.0 and leds.is_on()): leds.set(False) if (distance <= 0.3 and not pwm.is_beep()): pwm.set_beep(True) leds.set_sleep_timeout(400) if (distance > 0.3 and pwm.is_beep()): pwm.set_beep(False) leds.set_sleep_timeout() else: print("ERROR: no nearest station?") print("duration %d" % (millis_passed(timestamp))) else: if pwm.is_beep(): pwm.set_beep(False) if leds.is_on(): leds.set(False) oled.display_lines(lines) if beep_off_if_no_location_timestamp != 0 and millis_passed( beep_off_if_no_location_timestamp) > 10000: beep_off_if_no_location_timestamp = 0 print("No location in 10 seconds, turning off") if pwm.is_beep(): pwm.set_beep(False) if leds.is_on(): leds.set(False)
def update_charging_indicator(): chargeVoltage = power.read_chargein_voltage() if chargeVoltage < 1: leds.set(10, [0, 0, 0]) else: batteryVoltage = power.read_battery_voltage() if batteryVoltage < 4.15: leds.set(10, [42, 0, 0]) else: leds.set(10, [0, 42, 0]) utime.sleep_ms(250)
def toggle_led1(value): if value: leds.set(0, (0, 0, 0)) return False leds.set(0, (255, 255, 255)) return True
except: pass while True: pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT) if pressed & buttons.BOTTOM_LEFT != 0: with display.open() as d: fg = colors[rand() % len(colors)] nx = 80 - round(len(nick) / 2 * 14) d.clear() d.print(nick, fg=fg, bg=[0xff - c for c in fg], posx=(nx - 8) + rand() % 16, posy=22 + rand() % 16) d.update() d.close() utime.sleep_ms(500) elif pressed & buttons.BOTTOM_RIGHT != 0: with display.open() as d: d.clear() d.update() d.close() else: leds.set(rand() % 11, colors[rand() % len(colors)]) leds.set_rocket(rand() % 3, rand() % 32) utime.sleep_ms(100) utime.sleep_us(1)
def matrix_leds(): for l in range(15): leds.set(l, urandom.choice(COLORS))
def main(): power_saving = False disp = display.open() disp.backlight(25) disp.clear().update() leds.clear() leds.set_powersave() leds.dim_top(1) leds.dim_bottom(1) with bme680.Bme680() as environment: while True: data = environment.get_data() disp.clear() # button toggle screen if buttons.read(buttons.TOP_RIGHT): power_saving = not power_saving if (data.iaq_accuracy >= 2): leds.set_all([iaq_color(data.iaq)] * 11) # Set green rocket if under 600 ppm: no mask required if (data.eco2 < 600): co2_text = "No masks required" co2_color = colors['green'] leds.set_rocket(2, 31) leds.set_rocket(1, 0) leds.set_rocket(0, 0) # Set blue rocket if over 600 ppm: masks required elif (data.eco2 >= 600 and data.eco2 < 900): co2_text = "Wear masks inside" co2_color = colors['blue'] leds.set_rocket(0, 31) leds.set_rocket(1, 0) leds.set_rocket(2, 0) # Set yellow rocket if over 900 ppm: dangerous even with masks on else: co2_text = "Masks won't save you!" co2_color = colors['orange'] leds.flash_rocket(1, 31, 500) leds.set_rocket(2, 0) leds.set_rocket(0, 0) # vibra.vibrate(500) # time.sleep(1) # vibra.vibrate(500) # time.sleep(1) # vibra.vibrate(500) leds_set_bottom(co2_color) if (not power_saving): disp.backlight(25) disp.print("IAQ: " + str(data.iaq)) disp.print("CO2: " + str(int(data.eco2)) + "ppm", posy=40) if (data.iaq_accuracy == 3): disp.print(iaq_string(data.iaq), posy=20, fg=iaq_color(data.iaq)) disp.print(co2_text, posy=60, fg=co2_color, font=display.FONT12) else: disp.print("(still calibrating...)", posy=60, font=display.FONT12) else: disp.clear() disp.backlight(0) disp.update() time.sleep(1) else: disp.print("Calibrating...", posy=0, font=display.FONT16) disp.print( "Place the badge both in open-air and a closed box with exhaled air for around 10min each.", posy=40, font=display.FONT8) disp.update() if (not power_saving): oldest_time = time.time() latest_time = oldest_time while (latest_time - oldest_time < 1): leds.set(rand() % 11, all_colors[rand() % len(all_colors)]) time.sleep_ms(1) # Feed watch doge latest_time = time.time() else: leds.set_all([[0, 0, 0]] * 15) time.sleep(1)
def leds_set_bottom(co2_color): leds.set(11, co2_color) leds.set(12, co2_color) leds.set(13, co2_color) leds.set(14, co2_color)
WIDTH = 8 HEIGHT = 8 AMPLITUDE = 50 DELAY = 1 channel, board = leds.initialize(WIDTH, HEIGHT, AMPLITUDE) try: colors = [[0 for i in range(HEIGHT)] for j in range(WIDTH)] for i in range(WIDTH): for j in range(HEIGHT): colors[i][j] = ((i << 4) << 8) + (j << 4) leds.set(channel, board, colors) time.sleep(DELAY) for i in range(WIDTH): for j in range(HEIGHT): colors[i][j] = ((i << 4) << 16) + (j << 4) leds.set(channel, board, colors) time.sleep(DELAY) for i in range(WIDTH): for j in range(HEIGHT): colors[i][j] = ((i << 4) << 8) + ((j << 4) << 16)
def run_loop(): bat = [1, [0, 230, 00], [255, 215, 0], [255, 0, 0]] anim = 0 write_timer = 0 with display.open() as disp: disp.clear() disp.backlight(5) disp.print("@derfnull", posy=0) disp.update() disp.close() while True: pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT) if pressed & buttons.BOTTOM_LEFT: anim += 1 if pressed & buttons.BOTTOM_RIGHT: anim += 2 if pressed: if anim > 4: anim = 0 if anim == 0: leds.clear() leds.set_rocket(0, 0) leds.set_rocket(1, 0) leds.set_rocket(2, 0) if anim == 1: leds.set_rocket(0, 0) leds.set_rocket(1, 0) leds.set_rocket(2, 0) leds.gay(0.2) if anim == 2: leds.clear() leds.set_rocket(0, 2) leds.set_rocket(1, 15) leds.set_rocket(2, 15) if anim == 3: leds.clear() leds.set_rocket(0, 15) leds.set_rocket(1, 15) leds.set_rocket(2, 15) if anim == 4: leds.clear() leds.set_rocket(0, 0) leds.set_rocket(1, 0) leds.set_rocket(2, 0) leds.set(11, [127, 127, 127]) leds.set(12, [127, 127, 127]) leds.set(13, [127, 127, 127]) leds.set(14, [127, 127, 127]) sensor_data = bme680.get_data() ambient_light = light_sensor.get_reading() battery_voltage = os.read_battery() with display.open() as disp: disp.clear() render_battery(disp, bat, battery_voltage) disp.print("@derfnull", posy=0) disp.print("{:2.1f} C {:2.0f} %".format(sensor_data[0], sensor_data[1]), posy=20) disp.print("{:5.1f} hPa ".format(sensor_data[2]), posy=40) disp.print("{:4.1f} kOhm ".format(sensor_data[3] / 1000), posy=60) disp.update() disp.close() write_timer += 1 if write_timer == 5: with open('sensorlog.txt', 'a') as f: f.write('{} {} {} {} {} {} {}\n'.format( utime.time_ms(), sensor_data[0], sensor_data[1], sensor_data[2], sensor_data[3], ambient_light, battery_voltage)) write_timer = 0 utime.sleep(2)
with open('/nickname.txt', 'r') as f: nick = str(f.read()) except: pass string = [] for c in nick.upper(): if not c in charset: c = '_' string = string + charset[c] + [0] while True: sign = lambda v: 1 if v>=0 else -1 accel_hist = [] direction = 0 while direction == 0: samples = accel.read() accel_hist.extend([ 0 if abs(s.y) < 0.2 else s.y for s in samples ]) accel_hist = accel_hist[max(len(accel_hist)-20, 0):] if len(accel_hist) > 2: direction = sign(accel_hist[-1]) - sign(accel_hist[-2]) colors = [(0, 0, 0), (0xff, 0xff, 0xff)] string_iter = string if direction > 0: string_iter = reversed(string_iter) for column in string: for l in range(11): leds.set(10-l, colors[column>>l & 1]) leds.clear() utime.sleep(0.001)
FONT = '/usr/share/fonts/truetype/droid/DroidSans.ttf' channel, board = leds.initialize(WIDTH, HEIGHT, AMPLITUDE) imageWidth = WIDTH * 20 image = Image.new("RGB", (imageWidth, HEIGHT)) draw = ImageDraw.Draw(image) font = ImageFont.truetype(FONT, 8) draw.text((0, 0), MESSAGE, (50, 50, 50), font=font) raw = image.tobytes() packed = [[ struct.unpack( '>I', b'\x00' + raw[(i + j * imageWidth) * 3:(i + j * imageWidth + 1) * 3])[0] for i in range(imageWidth) ] for j in range(HEIGHT)] try: for k in range(imageWidth - WIDTH + 1): buffer = [[packed[i][j + k] for j in range(WIDTH)] for i in range(HEIGHT)] leds.set(channel, board, buffer) time.sleep(0.1) finally: leds.shutdown(board)
nick = 'sample text' try: with open('/nickname.txt') as f: nick = f.read() except: pass while True: with display.open() as d: for k in range(4): (x1, y1) = (randrange(159), randrange(79)) (x2, y2) = (min(x1 + randrange(40), 159), min(y1 + randrange(40), 79)) try: d.rect(x1, y1, x2, y2, col=choice(disp_colors), filled=True) except: pass fg = choice(disp_colors) nx = 80 - round(len(nick) / 2 * 14) d.print(nick, fg=fg, bg=[0xff - c for c in fg], posx=(nx - 8) + randrange(16), posy=22 + randrange(16)) d.update() d.close() leds.set(randrange(11), choice(led_colors)) leds.set_rocket(randrange(3), randrange(32)) utime.sleep(0.001)