def toggle_rockets(state): brightness = 15 if not state: brightness = 0 leds.set_rocket(0, brightness) leds.set_rocket(1, brightness) leds.set_rocket(2, brightness)
def toggle_rockets(state): """ Turns all rocked LEDs on or off. :param state: True=on, False=off """ brightness = 15 if not state: brightness = 0 leds.set_rocket(0, brightness) leds.set_rocket(1, brightness) leds.set_rocket(2, brightness)
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 update_rockets(batteryLevel): if batteryLevel == 1: leds.set_rocket(0, 31) leds.set_rocket(1, 31) leds.set_rocket(2, 31) else: leds.set_rocket(0, 0) leds.set_rocket(1, 0) leds.set_rocket(2, 0)
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)
import leds import os import utime try: accel = bhi160.BHI160Accelerometer() except: os.reset() leds.clear() with display.open() as d: d.clear() d.update() for i in range(3): leds.set_rocket(i, 0) # Characters are encoded in columns per 11 bits in an integer. # LSB = top, MSB = bottom. charset = { 'A': [0x780, 0x178, 0x107, 0x178, 0x780], 'B': [0x7ff, 0x663, 0x663, 0x19c], 'C': [0x1fc, 0x306, 0x603, 0x603], 'D': [0x7ff, 0x603, 0x603, 0x30e, 0x1fc], 'E': [0x7ff, 0x663, 0x663, 0x663], 'F': [0x7ff, 0x063, 0x063, 0x063], 'G': [0x7ff, 0x603, 0x633, 0x1e6], 'H': [0x7ff, 0x060, 0x060, 0x7ff], 'I': [0x603, 0x7ff, 0x603], 'J': [0x780, 0x603, 0x603, 0x7ff], 'K': [0x7ff, 0x070, 0x1dc, 0x707],
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)
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)
def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg): anim = 'led' posy = 30 if sub != '': posy = 18 r = 255 g = 0 b = 0 while True: dark = 0 if light_sensor.get_reading() < 30: dark = 1 r_fg_color = fg[dark] r_bg_color = bg[dark] r_fg_sub_color = fg_sub[dark] r_bg_sub_color = bg_sub[dark] r_bg = main_bg[dark] if anim == 'fade': if r > 0 and b == 0: r = r - 1 g = g + 1 if g > 0 and r == 0: g = g - 1 b = b + 1 if b > 0 and g == 0: r = r + 1 b = b - 1 r_bg = [r, g, b] if anim == 'led': for i in range(0, 11): leds.prep(i, r_bg) leds.update() leds.dim_top(3) leds.set_rocket(0, 15) leds.set_rocket(1, 15) leds.set_rocket(2, 15) if anim == 'none': leds.clear() leds.set_rocket(0, 0) leds.set_rocket(1, 0) leds.set_rocket(2, 0) with display.open() as disp: disp.rect(0, 0, 160, 80, col=r_bg, filled=True) disp.print(title, fg=r_fg_color, bg=r_bg_color, posx=80 - round(len(title) / 2 * 14), posy=posy) if sub != '': disp.print(sub, fg=r_fg_sub_color, bg=r_bg_sub_color, posx=80 - round(len(sub) / 2 * 14), posy=42) disp.update() disp.close() pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT) if pressed & buttons.BOTTOM_LEFT != 0: anim = ANIM_TYPES[1] if pressed & buttons.BOTTOM_RIGHT != 0: anim = ANIM_TYPES[0] utime.sleep(0.3)
def display_seconds(sec, intensity): leds.set_rocket(1, intensity) if sec % 2 == 0 else leds.set_rocket(1, 0)