def feed(): ''' Start / resets the power management task ''' global timeout, enabled if timeout < 1: #Disabled by setting return if not virtualtimers.update(timeout, pm_task): enabled = True virtualtimers.new(timeout, pm_task, True)
def start(): global state, tasks_loop, tasks_draw if not state: state = True for i in tasks_loop: virtualtimers.new(1, i) for i in tasks_draw: virtualtimers.new(1, i, True)
def feed(start=True): ''' Resets the timer ''' global timeout, enabled if start: enabled = True if enabled: if timeout < 1: #Disabled by setting return if not virtualtimers.update(timeout, pm_task): virtualtimers.new(timeout, pm_task, True)
def focus(in_focus): global task_active, seconds print("Focus changed to", in_focus) if in_focus: if not task_active: virtualtimers.new(0, counterTask, False) task_active = True else: if task_active: virtualtimers.delete(counterTask) task_active = False seconds = 0
def on_key(key_index, pressed): global presses if pressed: press = {'is_long': False, 'timer': lambda: on_long_press(key_index)} presses[key_index] = press virtualtimers.new(LONG_PRESS_MS, press['timer'], hfpm=True) print('press %d' % key_index) else: if key_index in presses: press = presses[key_index] if not press['is_long']: start_app(key_index) virtualtimers.delete(press['timer']) del presses[key_index] print('release %d' % key_index)
def on_key(key_index, is_pressed): global pattern if is_pressed: x, y = keypad.index_to_coords(key_index) pattern[x + y * 4] = (pattern[x + y * 4] + 1) % 4 if pattern[x + y * 4] == 0: display.drawPixel(x, y, 0x400000) tone = 396 elif pattern[x + y * 4] == 1: display.drawPixel(x, y, 0x004000) tone = 440 elif pattern[x + y * 4] == 2: display.drawPixel(x, y, 0x004040) tone = 495 elif pattern[x + y * 4] == 3: display.drawPixel(x, y, 0x404000) tone = 528 display.flush() sndmixer.freq(synth, tone) sndmixer.play(synth) sndmixer.volume(synth, 64) virtualtimers.new(100, sound_off, False)
if counter % 2 == 0: color = settings['color_off'] else: color = settings['color_on'] elif pattern[x + y * 4] == 3: if counter % 2 == 0: color = settings['color_on'] else: color = settings['color_off'] display.drawPixel(x, y, color) display.flush() return interval sndmixer.begin(1) synth = sndmixer.synth() sndmixer.waveform(synth, 0) sndmixer.volume(synth, 0) # Adding callbacks virtualtimers.begin(50) virtualtimers.new(0, draw, False) keypad.add_handler(on_key) touchpads.on(touchpads.RIGHT, interval_up) touchpads.on(touchpads.LEFT, interval_down) touchpads.on(touchpads.CANCEL, stop_blinking) touchpads.on(touchpads.OK, continue_blinking)
def feed(): ''' Start / resets the power management task ''' global userResponseTime if not virtualtimers.update(userResponseTime, pm_task): virtualtimers.new(userResponseTime, pm_task, True)
import rgb, time rgb.clear() time.sleep(0.1) orig_start(app, status) system.start = hijacked_start ## Make badge sleep in undervoltage conditions virtualtimers.activate(1000) # low resolution needed def _vcc_callback(): try: vcc = system.get_vcc_bat() if vcc != None: if vcc < 3300: __import__('deepsleep') deepsleep.vcc_low() finally: # Return 10000 to start again in 10 seconds gc.collect() return 10000 virtualtimers.new(10000, _vcc_callback, hfpm=True) del folders, uos gc.collect() gc.mem_free()
apps = valuestore.load('slider', 'apps') or ['clock', 'nickname'] current_index = valuestore.load('slider', 'current') or 0 time = valuestore.load('slider', 'time') or 20 next_index = None if current_index is None else ((current_index + 1) % len(apps)) def _next(): import valuestore ## Start next app by restarting slider print('rebooting into next app: ', next_index) valuestore.save('slider', 'current', next_index) system.start('slider') pass ## Set timer for starting next app virtualtimers.new(time * 1000, _next, hfpm=True) if len(apps) == 0: print('No apps set, slider will start normal launcher instead') system.reboot() if current_index is None or current_index < 0 or current_index >= len(apps): print('Current app index %d is out of the bounds of the set slider apps, setting index to 0' % current_index) try: current_index = 0 valuestore.save('slider', 'current', current_index) except BaseException as error: print('Additional error whilst saving index: %s' % error) system.start('launcher') app = apps[current_index] del apps, current_index, valuestore
def setup(drawCb=None): global services global drawCallbacks if drawCb: print("[SERVICES] Draw callback registered") global drawCallback drawCallback = drawCb #This might need a better name... # Status of wifi wifiFailed = False #Check if lib folder exists and get application list, else stop try: apps = uos.listdir('lib') except OSError: return [False, False] #For each app... for app in apps: print("APP: " + app) try: #Try to open and read the json description fd = open('/lib/' + app + '/service.json') description = ujson.loads(fd.read()) fd.close() except: print("[SERVICES] No description found for " + app) continue #Or skip the app try: #Try to open the service itself fd = open('/lib/' + app + '/service.py') fd.close() except: print("[SERVICES] No script found for " + app) continue #Or skip the app rtcRequired = False # True if RTC should be set before starting service loopEnabled = False # True if loop callback is requested drawEnabled = False # True if draw callback is requested wifiInSetup = False # True if wifi needed in setup wifiInLoop = False # True if wifi needed in loop try: if description['apiVersion'] != 2: print("[SERVICES] Service for " + app + " is not compatible with current firmware") continue #Skip the app wifiInSetup = description['wifi']['setup'] wifiInLoop = description['wifi']['setup'] rtcRequired = description['rtc'] loopEnabled = description['loop'] drawEnabled = description['draw'] except: print("[SERVICES] Could not parse description of app " + app) continue #Skip the app print("[SERVICES] Found service for " + app) # Import the service.py script try: srv = __import__('lib/' + app + '/service') except BaseException as e: print("[SERVICES] Could not import service of app " + app + ": ") sys.print_exception(e) continue #Skip the app if wifiInSetup or wifiInLoop: if wifiFailed: print( "[SERVICES] Service of app " + app + " requires wifi and wifi failed so the service has been disabled." ) continue if not easywifi.status(): if not easywifi.enable(): wifiFailed = True print("[SERVICES] Could not connect to wifi!") continue # Skip the app if rtcRequired and time.time() < 1482192000: if not wifiFailed: print("[SERVICES] RTC required, configuring...") easyrtc.configure() else: print( "[SERVICES] RTC required but not available. Skipping service." ) continue # Skip the app (because wifi failed and rtc not available) try: srv.setup() except BaseException as e: print("[SERVICES] Exception in service setup " + app + ":") sys.print_exception(e) continue if loopEnabled: try: virtualtimers.new(1, srv.loop) except: print("[SERVICES] Loop requested but not defined in service " + app) if drawEnabled and drawCb: drawCallbacks.append(srv) # Add the script to the global service list services.append(srv) handleDraw = False if len(drawCallbacks) > 0 and drawCb: print("[SERVICES] The service subsystem now handles screen redraws") handleDraw = True virtualtimers.new(1, draw_task, True) return handleDraw
start() def goToSleep(unused_variable=None): system.home() # Read configuration from NVS or apply default values cfg_term_menu = machine.nvs_get_u8( 'splash', 'term_menu') # Show a menu on the serial port instead of a prompt if cfg_term_menu == None: cfg_term_menu = True # If not set the menu is shown # Scheduler virtualtimers.activate(100) # Start scheduler with 100ms ticks virtualtimers.new(100, updateEink) # Terminal menu if cfg_term_menu: init_power_management() umenu = term_menu.UartMenu(system.home, pm, False, "< Back") umenu.main() #(Note: power management is disabled when the menu is disabled, to keep the python prompt clean and usefull) term.header(True, "Python shell") print("Type \"import menu\" to access the menu.")
def enable(): if badge.nvs_get_u8('badge','evrt',0)==0: virtualtimers.new(1, ber_task)
def wifiTask(): global wifi_status_prev, wifi_status_curr, gui_redraw, ota_available wifi_status_prev = wifi_status_curr wifi_status_curr = wifi.status() if wifi_status_curr: wifi.ntp(True) if wifi_status_curr != wifi_status_prev: #pm.feed() wifi_status_prev = wifi_status_curr gui_redraw = True if wifi_status_curr: ota_available = otacheck.available(True) return 1000 virtualtimers.new(50, wifiTask, True) # Services gui_apps = [] gui_app_names = [] gui_app_current = -1 def next_app(pressed): global gui_apps, gui_app_current, gui_redraw pm.feed() if pressed: if gui_app_current < len(gui_apps) - 1: if gui_app_current >= 0: try: gui_apps[gui_app_current].focus(False) except BaseException as e:
# # if not easywifi.failure(): # # otac.available(True) # RTC === #if time.time() < 1482192000: # easyrtc.configure() # ======= if badge.safe_mode(): draw(False) services.force_draw() draw(True) else: have_services = services.setup(draw) # Start services if not have_services: draw(False) services.force_draw() draw(True) easywifi.disable() gc.collect() virtualtimers.activate(25) pm.callback(onSleep) pm.feed() virtualtimers.new(10, ledAnimationTask) umenu = term_menu.UartMenu(goToSleep, pm, badge.safe_mode()) umenu.main()
else: display.drawFill() drawEyeClosed(0, 0xFFFFFF) drawEyeClosed(7, 0xFFFFFF) display.flush() animation_step = 0 elif animation_type == 3: for x in range(14): for y in range(5): color = x + (y * 14) display.drawPixel(x, y, color | (color << 8) | (color << 16)) return 100 virtualtimers.activate(100) virtualtimers.new(1, animation_task) def btnA(pressed): global animation_type if pressed: animation_type = 1 elif animation_type == 1: animation_type = 0 def btnB(pressed): global animation_type if pressed: animation_type = 2 elif animation_type == 2:
def wifiTask(): global wifi_status_prev, wifi_status_curr, gui_redraw, ota_available wifi_status_prev = wifi_status_curr wifi_status_curr = wifi.status() if wifi_status_curr: wifi.ntp(True) if wifi_status_curr != wifi_status_prev: #pm.feed() wifi_status_prev = wifi_status_curr gui_redraw = True if wifi_status_curr: ota_available = otacheck.available(True) return 1000 virtualtimers.new(50, wifiTask, True) # Services gui_apps = [] gui_app_names = [] gui_app_current = -1 def next_app(pressed): global gui_apps, gui_app_current, gui_redraw pm.feed() if pressed: if gui_app_current < len(gui_apps) - 1: if gui_app_current >= 0: try: gui_apps[gui_app_current].focus(False)
"abcdefghijklmnopqrstuvwxzy.0123456789") valuestore.save('mqttclient', 'settings', settings) client_id = settings['client_id'] try: mqttc = MQTTClient(client_id, settings["mqtt_server"]) mqttc.set_callback(mqtt_cb) mqttc.connect() except: # Maybe wrong hostname del settings["mqtt_server"] valuestore.save('mqttclient', 'settings', settings) # Might be something different, rethrow to restart... raise #status = client_id + "/status" topic = client_id + "/text" mqttc.subscribe(topic) print('Connected to MQTT server!') def check_mqtt_messages(): mqttc.check_msg() return 200 import virtualtimers virtualtimers.begin(200) virtualtimers.new(0, check_mqtt_messages) uinterface.skippabletext("Publish messages to topic " + topic)
drawApps() print('page %d' % page) def on_right(pressed): global page if pressed: page += 1 drawApps() print('page %d' % page) touchpads.on(touchpads.LEFT, on_left) touchpads.on(touchpads.RIGHT, on_right) # if not machine.nvs_getint('system', 'welcome_done'): # machine.nvs_setint('system', 'welcome_done', 1) # audio.play('/cache/system/welcome.mp3') # time.sleep(3) # audio.play('/cache/system/press.mp3') # time.sleep(3) virtualtimers.activate(500) keypad.add_handler(on_key) update() virtualtimers.new(500, update) # Refresh app list twice per second virtualtimers.new(2000, update_mp3_cache) # Refresh app names every 2 seconds ## Launch terminal menu # menu = term_menu.UartMenu() # menu.main()
def wifiTask(): global wifi_status_prev, wifi_status_curr, gui_redraw, ota_available wifi_status_prev = wifi_status_curr wifi_status_curr = wifi.status() if wifi_status_curr: wifi.ntp(True) if wifi_status_curr != wifi_status_prev: pm.feed() wifi_status_prev = wifi_status_curr gui_redraw = True if wifi_status_curr: ota_available = otacheck.available(True) return 1000 virtualtimers.new(50, wifiTask, True) # Services gui_apps = [] gui_app_names = [] gui_app_current = -1 def next_app(pressed): global gui_apps, gui_app_current, gui_redraw pm.feed() if pressed: if gui_app_current < len(gui_apps) - 1: if gui_app_current >= 0: try: gui_apps[gui_app_current].focus(False)
if (gyros[0] < 0): gu = (-gyros[0])>>8 else: gd = (gyros[0])>>8 if (gyros[1] < 0): gl = (-gyros[1])>>8 else: gr = (gyros[1])>>8 if (gyros[2] < 0): gz1 = (-gyros[2])>>8 else: gz2 = (gyros[2])>>8 if mode > 0: setLed(l,r,u,d,z1,z2,gl,gr,gu,gd,gz1,gz2) else: r = 0 g = 0 if (accel[1] < 0): g = (-accel[1])>>8 else: r = (accel[1])>>8 setSmiley(r,g) return 20 virtualtimers.activate(20) virtualtimers.new(20, task) import menu