Skip to content

Commit 5559f3e

Browse files
committed
small code changes
1 parent 0c2747d commit 5559f3e

File tree

4 files changed

+128
-35
lines changed

4 files changed

+128
-35
lines changed

software/main/am1.py

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from machine import Pin
22
import machine
33
from config import config
4-
import time
4+
55
import gc
66

77
gc.collect()
@@ -24,15 +24,18 @@
2424
infmsg = True
2525
dbgmsg = False
2626
errmsg = True
27-
global timer
28-
peer_mac = b'\xff\xff\xff\xff\xff\xff'
2927
configuration = config["configuration"]
30-
hive = config["hive"]
3128
if configuration == "AM1":
3229
infmsg = True
3330

3431
networking = SSP_Networking(infmsg, dbgmsg, errmsg)
3532

33+
peer_mac = b'\xff\xff\xff\xff\xff\xff'
34+
35+
import time
36+
37+
global timer
38+
3639
print("{:.3f} Name: {}, ID: {}, Configuration: {}, Sta mac: {}, Ap mac: {}, Version: {}".format(
3740
(time.ticks_ms() - networking.inittime) / 1000,
3841
networking.config["name"],
@@ -43,57 +46,41 @@
4346
networking.config["version"]
4447
))
4548

46-
def idle():
49+
if configuration == "AM1":
4750
lastPressed = 0
51+
4852
message = "Boop!"
4953

5054
def boop(pin):
5155
global lastPressed
5256
if (time.ticks_ms() - lastPressed > 1000):
5357
lastPressed = time.ticks_ms()
5458
networking.ping(peer_mac)
55-
networking.echo(peer_mac, message)
56-
networking.send(peer_mac, message)
59+
#networking.echo(peer_mac, message)
60+
#networking.send(peer_mac, message)
5761
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()}")
62+
#print(f"{(time.ticks_ms() - networking.inittime) / 1000:.3f} Networking Tool: RSSI table: {networking.rssi()}")
6063

6164
# Buttons
6265
switch_select = Pin(9, Pin.IN, Pin.PULL_UP)
6366
switch_select.irq(trigger=Pin.IRQ_FALLING, handler=boop)
6467

68+
6569
def heartbeat(timer):
6670
print("")
6771
print(f"{(time.ticks_ms() - networking.inittime) / 1000:.3f} Networking Tool Heartbeat: {gc.mem_free()} bytes")
6872
print("")
6973
gc.collect()
7074

75+
7176
timer = machine.Timer(0)
7277
timer.init(period=5000, mode=machine.Timer.PERIODIC, callback=heartbeat)
7378

74-
def run_config_module(module_name):
79+
80+
def deinit():
81+
networking.cleanup()
7582
try:
76-
with open(module_name + ".py") as f:
77-
code = f.read()
78-
exec(code)
83+
timer.deinit()
7984
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-
85+
print(e)
86+
machine.reset()

software/main/archive/am1.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+

software/main/hm3.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,9 @@ def handle_data():
227227
deinit()
228228
print("Hello End")
229229

230+
try:
231+
with open("sm3.py") as f:
232+
code = f.read()
233+
exec(code)
234+
except Exception as e:
235+
print(f"Error running sm3.py: {e}")

software/release/README.qmd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ A short description of the directories can be found below.
3737
|--------------|-----------------------------------------|--------------|
3838
| release/config.py | Smart Module Configuration File | Nick |
3939
| main/boot.py | Smart Module boot.py | Nick |
40-
| main/hm3.py | Hive motor module main program | Milan |
40+
| main/am1.py | Admin module main program | Nick |
41+
| main/hm3.py | Hive motor module main program | Nick |
4142
| main/sl1.py | Smart light module main program | Milan |
4243
| main/sm3.py | Smart motor module main program | Milan |
43-
| main/sp1.py | Smart splat module main program | Milan |
44+
| main/sp1.py | Smart splat module main program | Nick |
4445
| networking/networking.py | This is the main networking code that builds on ESP-NOW. There are many prepared functionalities (and some more that I am working on), such as long message support, sending of various variable types (bytes, bytearray, dicts, lists, int, float, char, string), as well as different types of messages such as ping, echo and more. There are also various features in place to make the networking more robust. | Nick |
4546
| networking/ssp_networking.py | This is the ssp networking module. It needs config.py to function. | Nick |
4647
| libraries/adxl345.py | Support for the built in accelerometer https://github.com/DFRobot/micropython-dflib/blob/master/ADXL345/user_lib/ADXL345.py | Milan |

0 commit comments

Comments
 (0)