def main(): try: run() except Exception as e: try: io.oled.println(str(e)) except: pass sleep_ms(5000) io.off() deepsleep()
def main(): if machine.reset_cause() == machine.HARD_RESET: io.off() elif machine.reset_cause() == machine.SOFT_RESET: pass # impossible in this version else: if not io.powered(): # bike not powered on startup; was motion wakeup io.off() # nothing should be on, but ok... deepsleep() loop.create_task(task_ecu()) loop.create_task(task_gear_indicator()) loop.run_until_complete( await_pwroff()) # wait for poweroff (bike shutdown) # -> bike powered off io.off() deepsleep()
def main(): if machine.reset_cause() == machine.HARD_RESET: ctrl.off() elif machine.reset_cause() == machine.SOFT_RESET: # this must be a webapp reset, so start it again start_net() else: tmr = tms() while ctrl.switch_pressed(): # wait for switch to be released. NC = remains on until timeout if tdiff(tms(), tmr) > _SW_PWRUP_TIMEOUT: # break light flash switch NC or hold down long for special fun start_net() break else: # not pressed long enough if not ctrl.powered(): # bike not powered on startup; was motion wakeup ctrl.off() # nothing should be on, but ok... deepsleep() while True: if ctrl.powered(): # bike (and maybe network) running loop.create_task(task_ecu()) loop.create_task(task_ctrl()) loop.run_until_complete(await_pwroff()) # wait for poweroff (bike shutdown) # -> bike powered off # only required if net started afterwards, so no wrong data is displayed: reset_loop() # clear ecu and ctrl task as these are not required now ctrl.clear() ecu.reset() if ctrl.switch_pressed(): # switch held down during shutdown start_net(ctrl.mode * 60 if ctrl.mode > 0 else _NET_DEFAULT_ACTIVE_TIME) # will only set tmr if alr active elif ctrl.mode == 10: ctrl.off() return # to console else: if not net.stay_on(): ctrl.off() net.stop() # stop running network (explicitly kick clients), we want to deepsleep deepsleep() else: # stay_on time not over -> reschedule task loop.create_task(task_net()) # -> only network running, should stay on (and check for powerup meanwhile) loop.run_until_complete(await_pwron())
def run(): if machine.reset_cause() == machine.HARD_RESET: io.off() show_logo() elif machine.reset_cause() == machine.SOFT_RESET: # this must be a webapp reset, so start it again start_net() show_logo() else: tmr = tms() while io.switch_pressed(): # wait for switch to be released. NC = remains on until timeout if tdiff(tms(), tmr) > _SW_PWRUP_TIMEOUT: # brakelight flash switch NC or hold down long for special fun start_net() break else: # BLF switch not pressed (or long enough) if not io.powered(): # bike not powered on startup; was motion wakeup io.off() # nothing should be on, but ok... deepsleep() else: show_logo() while True: if io.powered(): # bike (and maybe network) running loop.create_task(task_ctrl.run()) # first start display loop.create_task(task_ecu()) loop.run_until_complete(await_pwroff()) # wait for poweroff (bike shutdown) # -> bike powered off # only required if net started afterwards, so no wrong data is displayed: reset_loop() # clear ecu and ctrl task as these are not required now io.clear() ecu.reset() if io.switch_pressed(): # switch held down during shutdown start_net() elif not net.stay_on(): io.off() net.stop() # stop running network (explicitly kick clients), we want to deepsleep deepsleep() else: # stay_on time not over -> reschedule task loop.create_task(task_net()) # -> only network running, should stay on (and check for powerup meanwhile) loop.run_until_complete(await_pwron())
async def await_pwron(): while not ctrl.powered(): if not net.stay_on(): ctrl.off() deepsleep() await d(1000)
def execute(self, msg): # execute a msg object (must be dict unpacked from json) global _stay_on_for, _stay_on_tmr try: if 'PING' in msg: # send stay-on-time (secs) as ACK self.obj['io'].oled.println("do ACK") self.send(ACK=max((_stay_on_for - tdiff(tms(), _stay_on_tmr)) // 1000, 0)) self.obj['io'].oled.println("acknowledged PING " + str(msg["PING"])) self.conn_tmr = tms() # reset timer elif 'SET' in msg and 'TO' in msg: # client wants to set local variable self._set_var(msg['SET'], msg['TO']) elif 'CMD' in msg: # ESP command without args cmd = msg['CMD'] if cmd == "reboot": reset() # soft reset elif cmd == "deepsleep": deepsleep() elif cmd == "console": raise Exception("net return") elif cmd == "ifconfig": # returns AP and STA IP and Port; 0.0.0.0 if not connected aw = "" ap = network.WLAN(network.AP_IF) if ap.active(): aw += "AP:\n{}\n\n".format(ap.ifconfig()) sta = network.WLAN(network.STA_IF) if sta.active(): aw += "Station:\n{}\n\n".format(sta.ifconfig()) aw += "Port: " + str(read_cfg("port")) self.send(ALERT=aw) elif cmd == "netls": self.send(ALERT='\n\n'.join(["ID: %s\nPW: %s" % (kid, kpw) for (kid, kpw) in read_cfg("knets")])) elif cmd == "netadd": cfg = read_cfg() knets = cfg["knets"] for i in range(len(knets)): if knets[i][0] == msg['ID']: knets[i][1] = msg['PW'] break else: # not found cfg["knets"].append((msg['ID'], msg['PW'])) write_cfg(cfg) elif cmd == "netrm": cfg = read_cfg() knets = cfg["knets"] for i in range(len(knets)): if knets[i][0] == msg['ID']: knets.pop(i) break else: return # not found -> nothing to do write_cfg(cfg) elif cmd == "nettime": _stay_on_for = int(msg['VAL']) * 1000 # ms _stay_on_tmr = tms() elif cmd == "text": self.obj['io'].oled.clear() self.obj['io'].oled.text(msg["MSG"]) self.obj['io'].oled.show() elif cmd == "println": self.obj['io'].oled.println(msg["MSG"]) # elif 'GET' in msg: # client wants to get local variable(s) # if not msg['GET']: # empty string or None -> enquiring all cached data # self.send(UPDATE=self.data) # else: # self._get_var(msg['GET']) except ValueError: # not in JSON format pass