import light_sensor import math WIDTH = 160 HEIGHT = 80 disp = display.open() light_sensor.start() history = [] while True: disp.clear() value = light_sensor.get_reading() history.insert(0, value) if len(history) > WIDTH: history.pop() blueColor = (int)(255 * (value / max(history))) redColor = (int)(255 * (1 - value / max(history))) disp.print("%i" % value, fg=[redColor, 0, blueColor]) isFirstMax = True for i in range(0, len(history)): currentVal = history[i] historyMin = min(history) y = math.floor(HEIGHT * ((currentVal - historyMin) /
def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat): """ Main function to render the nickname on screen. Pretty ugly but not time for cleanup right now (and some APIs missing) :param title: first row of text :param sub: second row of text :param fg: tuple of (day, night) rgb for title text color :param bg: tuple of (day, night) rgb for title background color :param fg_sub: tuple of (day, night) rgb for subtitle text color :param bg_sub: tuple of (day, night) rgb for subtitle background color :param main_bg: tuple of (day, night) rgb for general background color :param mode: default animation to start in (index of ANIM_TYPES array) :param bat: battery config tuple (boolean: indicator on/off, array: good rgb, array: ok rgb, array: bad rgb) """ anim = mode posy = 30 if sub != "": posy = 18 r = 255 g = 0 b = 0 rainbow_led_pos = 0 r_sub = sub last_btn_poll = utime.time() - 2 while True: sleep = 0.5 if sub == "#time": r_sub = get_time() 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] # Button handling pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT) if utime.time() - last_btn_poll >= 1: last_btn_poll = utime.time() if pressed & buttons.BOTTOM_RIGHT != 0: anim = anim + 1 if anim >= len(ANIM_TYPES): anim = 0 blink_led(0) if pressed & buttons.BOTTOM_LEFT != 0: anim = anim - 1 if anim < 0: anim = len(ANIM_TYPES) - 1 blink_led(0) # Animations if ANIM_TYPES[anim] == "fade": sleep = 0.1 leds.clear() toggle_rockets(False) if r > 0 and b == 0: r -= 1 g += 1 if g > 0 and r == 0: g -= 1 b += 1 if b > 0 and g == 0: r += 1 b -= 1 r_bg = [r, g, b] r_bg_color = r_bg r_bg_sub_color = r_bg if ANIM_TYPES[anim] == "led": if dark == 1: for i in range(0, 11): leds.prep(i, r_bg) leds.update() leds.dim_top(4) toggle_rockets(True) else: leds.clear() toggle_rockets(False) if ANIM_TYPES[anim] == "rnd_led": if dark == 1: for i in range(0, 11): leds.prep(i, random_rgb()) leds.update() leds.dim_top(4) toggle_rockets(True) else: leds.clear() toggle_rockets(False) if ANIM_TYPES[anim] == "gay": toggle_rockets(False) leds.gay(0.4) if ANIM_TYPES[anim] == "rainbow": for i in range(0, 11): lr, lg, lb = wheel(rainbow_led_pos + i * 3) leds.prep(i, [lr, lg, lb]) rainbow_led_pos += 1 if rainbow_led_pos > 255: rainbow_led_pos = 0 leds.update() leds.dim_top(3) toggle_rockets(True) if ANIM_TYPES[anim] == "none": leds.clear() toggle_rockets(False) with display.open() as disp: disp.rect(0, 0, 160, 80, col=r_bg, filled=True) if bat[0]: render_battery(disp, bat) disp.print( title, fg=r_fg_color, bg=r_bg_color, posx=80 - round(len(title) / 2 * 14), posy=posy, ) if r_sub != "": disp.print( r_sub, fg=r_fg_sub_color, bg=r_bg_sub_color, posx=80 - round(len(r_sub) / 2 * 14), posy=42, ) disp.update() disp.close() utime.sleep(sleep)
frame[frame_y][x] = int(row[x]) frame_y = frame_y + 1 if frame_y == size_y: #render_error('render', 'screen') draw_blinken_screen() utime.sleep_ms(frame_duration * 0.7) clear_frame() frame_y = 0 f.close() def play_all_files(): files = os.listdir('.') for file in files: if (file[-4:] == '.blm'): render_error('file:', file[:-4]) play_blinken_blm_file(file) def play_all_files_random_order(): files = os.listdir('.') # print(files) file = choice(files) if (file[-4:] == '.blm'): #vibra.vibrate(60) render_error('file:', file[:-4]) play_blinken_blm_file(file) #render_error('hello', 'world') seed(light_sensor.get_reading()) while True: play_all_files_random_order() # play_blinken_blm_file("ask_for_bombs.blm")
def brightness(): light = light_sensor.get_reading() display_brightness = int(light // 4) if light >= 4 else 1 display_brightness = 100 if light > 300 else display_brightness return display_brightness
def ctrl_backlight(d): Y_ANGLE = -30 Y_SPAN = 20 #+/- around angle Z_ANGLE = 0 Z_SPAN = 10 #+/- around angle RELIABLE_THRESHOLD = 2 MOVEMENT_THRESHOLD = 2 VIEW_TIMEOUT = 10 global viewing_state #can be viewing, not_viewing, timeout global viewing_event_start_time #start of viewing event global last_y_angle #last angle watch was held at in timeout state samples = bhi.read() if samples: sample = samples[-1] if viewing_state == 'viewing': #adjust brightness light = light_sensor.get_reading() display_brightness = int(light // 4) if light >= 4 else 1 display_brightness = 100 if light > 300 else display_brightness d.backlight(display_brightness) #check for state transistion if (utime.time() - viewing_event_start_time ) > VIEW_TIMEOUT: #display was on for too long viewing_state = 'timeout' #go to timeout last_y_angle = sample.y #remember angle we are currently held at elif (sample.status >= RELIABLE_THRESHOLD): #if orientation data is reliable if (sample.z < (Z_ANGLE - Z_SPAN)) or (sample.z > (Z_ANGLE + Z_SPAN)) or ( sample.y < (Y_ANGLE - Y_SPAN)) or ( sample.y > (Y_ANGLE + Y_SPAN)): #and we are outside of viewing angle viewing_state = 'not_viewing' #leave viewing state, as display is not held at correct angle elif viewing_state == 'not_viewing': #switch display off display_brightness = 0 d.backlight(display_brightness) #check for state transition if (sample.status < RELIABLE_THRESHOLD): #data is unreliable viewing_state = 'viewing' #switch display on to be safe viewing_event_start_time = utime.time() else: #data is reliable if (sample.z > (Z_ANGLE - Z_SPAN)) and ( sample.z < (Z_ANGLE + Z_SPAN)) and ( sample.y > (Y_ANGLE - Y_SPAN)) and ( sample.y < (Y_ANGLE + Y_SPAN)): # we are inside viewing angle viewing_state = 'viewing' #switch display on viewing_event_start_time = utime.time() elif viewing_state == 'timeout': #switch display off display_brightness = 0 d.backlight(display_brightness) #state transistions if ( abs(sample.y - last_y_angle) > MOVEMENT_THRESHOLD ): # there has been movement #removed(data is unreliable) because it leads to loop if unreliable all the time:(sample.status < RELIABLE_THRESHOLD) viewing_state = 'viewing' #switch display on viewing_event_start_time = utime.time() last_y_angle = sample.y #remember angle we are currently held at
disp.clear() disp.print("Frankonian", fg=[255, 0, 0]) disp.print("Village", fg=[255, 255, 255], posy=60, posx=60) disp.update() utime.sleep_ms(750) brightness = 10 minVoltage = 3.42 # minVoltage = 3.9 maxVoltage = 4.1 disp = display.open() mode = 0 # 0 = Voltage & Percentage | 1 = waitAfterUpdate = False light_sensor.start() mode = math.floor(4 * min(8, max(1, light_sensor.get_reading() / 10))) light_sensor.stop() display_logo() while True: disp.clear() pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) if pressed & buttons.BOTTOM_LEFT != 0: brightness = max(0, brightness - round(1 + brightness * 0.1)) utime.sleep_ms(20) if pressed & buttons.BOTTOM_RIGHT != 0:
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 ctrl_backlight(d): light = light_sensor.get_reading() display_brightness = int(light // 4) if light >= 4 else 1 display_brightness = 100 if light > 300 else display_brightness d.backlight(display_brightness)
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)