Skip to content

Commit

Permalink
Merge pull request #15 from tuftsceeo/dev/nick
Browse files Browse the repository at this point in the history
Dev/nick
  • Loading branch information
NicK4rT authored Nov 18, 2024
2 parents 554dda8 + 310e1aa commit d876e00
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
Binary file modified software/.DS_Store
Binary file not shown.
Binary file not shown.
3 changes: 2 additions & 1 deletion software/networking/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mysecrets = {"SSID": "Tufts_Robot", "key" : ""}
codes = {"0": b'PairingCodePhrase', "1": b'PairingResponsePhrase', "2": b'PairingConfirmationPhrase'}
configname = "Nickname"
configname = "Nickname"
config = ""
68 changes: 47 additions & 21 deletions software/networking/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import json
import random
import sys
import uos
import os

boottime = time.ticks_ms()

class Networking:
def __init__(self, infmsg=True, dbgmsg=False, admin=False):
def __init__(self, infmsg=True, dbgmsg=False, admin=True):
self.master = self
self.infmsg = infmsg
self.dbgmsg = dbgmsg
Expand All @@ -32,6 +34,13 @@ def __init__(self, infmsg=True, dbgmsg=False, admin=False):
self.name = configname
if self.name == "" or self.name == None:
self.name = str(self.id)

def _cleanup(self):
self._dprint("._cleanup")
self.aen.irq(None)
self.aen._aen.active(False)
self._staif.active(False)
self._apif.active(False)

def _iprint(self, message):
if self.infmsg:
Expand Down Expand Up @@ -189,14 +198,7 @@ def __init__(self, master):
self._pairing = True
self._running = True

if self.master._admin:
try:
self._aen.irq(self._irq)
except KeyboardInterrupt:#Trigger should be disabled when ctrl. C-ing
self._aen.irq(trigger=0)
self._aen.irq(handler=None)
else:
self._aen.irq(self._irq)#Processes the messages asap after receiving them, this should not be interrupted by doing ctrl c
self._aen.irq(self._irq)

self.master._iprint("ESP-NOW initialized and ready")

Expand Down Expand Up @@ -321,11 +323,35 @@ def return_messages(self):

def _irq(self, espnow):
self.master._dprint("aen._irq")
self._receive()
if self._irq_function and self.check_messages() and self._isrunning:
self._irq_function()
gc.collect()
return
if self.master._admin:
try:
self._receive()
if self._irq_function and self.check_messages() and self._isrunning:
self._irq_function()
gc.collect()
return
except KeyboardInterrupt:
#machine.disable_irq() #throws errors
self.master._iprint("aen._irq except KeyboardInterrupt")
#self._aen.irq(None) #does not work
self._aen.active(False)
#self.master._cleanup()
raise SystemExit("Stopping networking execution. ctrl-c or ctrl-d again to stop main code") #in thonny stops library code but main code keeps running, same in terminal
#self._isrunning = False
#raise KeyboardInterrupt #error in thonny but then stops running, just keeps running in terminal
#sys.exit(0) #breaks thonny, keeps running and recv (although ctl-d-able and keps running main loop in terminal
#machine.reset() #nogo keeps raising errors and running in terminal
#uos.sysexit() #raises an error in thonny but keeps running in terminal (althouzgh ctrl-d able)
#raise SystemExit #stops current library script, but main script keeps running, but now it just keeps the main code running in terminal...
#os.execv(sys.argv[0], sys.argv) #error in thonny, keeps running recv in terminal
#raise Exception("An error occurred!") #error in thonny, and then stops running due to keyboard interrupt, keeps running recv and irq in terminal
#raise KeyboardInterrupt("User interrupt simulated.") #interrupts library code, but main code keeps running, recv just keeps running in terminal
else:
self._receive()
if self._irq_function and self.check_messages() and self._isrunning:
self._irq_function()
gc.collect()
return

def irq(self, func):
self.master._dprint("aen.irq")
Expand Down Expand Up @@ -792,16 +818,16 @@ def __handle_ack(sender_mac, subtype, stimestamp, rtimestamp, payload_type, payl
elif subtype == b'\x15': #Echo
self.master._iprint(f"Echo received from {sender_mac} ({self.peer_name(sender_mac)}), {__decode_payload(payload_type, payload)}")
elif subtype == b'\x11': #Success
payload = __decode_payload(payload_type, payload) # should return a list with a cmd type and payload
payload = __decode_payload(payload_type, payload) # should return a list with a cmd type and payload
self.master._iprint(f"Success received from {sender_mac} ({self.peer_name(sender_mac)}) for type {payload[0]} with payload {payload[1]}")
#add to ack buffer
elif subtype == b'\x12': #Fail
payload = __decode_payload(payload_type, payload) # should return a list with a cmd type, error and payload
self.master._iprint(f"Command fail received from {sender_mac} ({self.peer_name(sender_mac)}) for type {payload[0]} with error {payload[1]} and payload {payload[2]}")")
payload = __decode_payload(payload_type, payload) # should return a list with a cmd type, error and payload
self.master._iprint(f"Command fail received from {sender_mac} ({self.peer_name(sender_mac)}) for type {payload[0]} with error {payload[1]} and payload {payload[2]}")
#add to ack buffer
elif subtype == b'\x13': #Confirm
payload = __decode_payload(payload_type,payload) # should return a list with message type and payload
self.master._iprint(f"Receive confirmation received from {sender_mac} ({self.peer_name(sender_mac)}) for type {payload[0]} with payload {payload[1]}")")
payload = __decode_payload(payload_type,payload) # should return a list with message type and payload
self.master._iprint(f"Receive confirmation received from {sender_mac} ({self.peer_name(sender_mac)}) for type {payload[0]} with payload {payload[1]}")
#add to ack buffer
else:
self.master._iprint(f"Unknown ack subtype from {sender_mac} ({self.peer_name(sender_mac)}): {subtype}, Payload: {payload}")
Expand All @@ -819,7 +845,7 @@ def __handle_ack(sender_mac, subtype, stimestamp, rtimestamp, payload_type, payl
__process_message(sender_mac, data, rtimestamp)
if not self._aen.any():#this is necessary as the for loop gets stuck and does not exit properly.
break


#message structure (what kind of message types do I need?: Command which requires me to do something (ping, pair, change state(update, code, mesh mode, run a certain file), Informational Message (Sharing Sensor Data and RSSI Data)
#| Header (1 byte) | Type (1 byte) | Subtype (1 byte) | Timestamp(ms ticks) (4 bytes) | Payload type (1) | Payload (variable) | Checksum (1 byte) |

0 comments on commit d876e00

Please sign in to comment.