|
| 1 | +from machine import Pin |
| 2 | +import machine |
| 3 | +from config import config |
| 4 | +import time |
| 5 | +import gc |
| 6 | + |
| 7 | +gc.collect() |
| 8 | + |
| 9 | +import network |
| 10 | + |
| 11 | +print("Running boot.py") |
| 12 | + |
| 13 | +# just to be safe |
| 14 | +sta = network.WLAN(network.STA_IF) |
| 15 | +ap = network.WLAN(network.AP_IF) |
| 16 | +sta.active(True) |
| 17 | +ap.active(True) |
| 18 | +sta.active(False) |
| 19 | +ap.active(False) |
| 20 | + |
| 21 | +from ssp_networking import SSP_Networking |
| 22 | + |
| 23 | +# Network |
| 24 | +infmsg = True |
| 25 | +dbgmsg = False |
| 26 | +errmsg = True |
| 27 | +global timer |
| 28 | +peer_mac = b'\xff\xff\xff\xff\xff\xff' |
| 29 | +configuration = config["configuration"] |
| 30 | +hive = config["hive"] |
| 31 | +if configuration == "AM1": |
| 32 | + infmsg = True |
| 33 | + |
| 34 | +networking = SSP_Networking(infmsg, dbgmsg, errmsg) |
| 35 | + |
| 36 | +print("{:.3f} Name: {}, ID: {}, Configuration: {}, Sta mac: {}, Ap mac: {}, Version: {}".format( |
| 37 | + (time.ticks_ms() - networking.inittime) / 1000, |
| 38 | + networking.config["name"], |
| 39 | + networking.config["id"], |
| 40 | + networking.config["configuration"], |
| 41 | + networking.config["ap_mac"], |
| 42 | + networking.config["sta_mac"], |
| 43 | + networking.config["version"] |
| 44 | +)) |
| 45 | + |
| 46 | +def idle(): |
| 47 | + lastPressed = 0 |
| 48 | + message = "Boop!" |
| 49 | + |
| 50 | + def boop(pin): |
| 51 | + global lastPressed |
| 52 | + if (time.ticks_ms() - lastPressed > 1000): |
| 53 | + lastPressed = time.ticks_ms() |
| 54 | + networking.ping(peer_mac) |
| 55 | + networking.echo(peer_mac, message) |
| 56 | + networking.send(peer_mac, message) |
| 57 | + print(f"{(time.ticks_ms() - networking.inittime) / 1000:.3f} Networking Tool: Sent {message} to {peer_mac}") |
| 58 | + print( |
| 59 | + f"{(time.ticks_ms() - networking.inittime) / 1000:.3f} Networking Tool: RSSI table: {networking.rssi()}") |
| 60 | + |
| 61 | + # Buttons |
| 62 | + switch_select = Pin(9, Pin.IN, Pin.PULL_UP) |
| 63 | + switch_select.irq(trigger=Pin.IRQ_FALLING, handler=boop) |
| 64 | + |
| 65 | + def heartbeat(timer): |
| 66 | + print("") |
| 67 | + print(f"{(time.ticks_ms() - networking.inittime) / 1000:.3f} Networking Tool Heartbeat: {gc.mem_free()} bytes") |
| 68 | + print("") |
| 69 | + gc.collect() |
| 70 | + |
| 71 | + timer = machine.Timer(0) |
| 72 | + timer.init(period=5000, mode=machine.Timer.PERIODIC, callback=heartbeat) |
| 73 | + |
| 74 | +def run_config_module(module_name): |
| 75 | + try: |
| 76 | + with open(module_name + ".py") as f: |
| 77 | + code = f.read() |
| 78 | + exec(code) |
| 79 | + except Exception as e: |
| 80 | + print(f"Error running {module_name}: {e}") |
| 81 | + |
| 82 | +# cases for different configurations |
| 83 | +if not hive: |
| 84 | + if configuration == "AM1": |
| 85 | + print("am1") |
| 86 | + idle() |
| 87 | + elif configuration == "SM3": |
| 88 | + print("sm3") |
| 89 | + run_config_module("sm3") |
| 90 | + elif configuration == "SL1": |
| 91 | + print("sl1") |
| 92 | + run_config_module("sl1") |
| 93 | + else: |
| 94 | + print("idle") |
| 95 | + idle() |
| 96 | +else: |
| 97 | + run_config_module("hm1") |
| 98 | + # insert code here to run in case of hive motor! |
| 99 | + |
0 commit comments