Skip to content

Commit

Permalink
Skynet averted.
Browse files Browse the repository at this point in the history
ctrl-c x2 or ctrl-c and ctrl-d will now actually interrupt the code
  • Loading branch information
NicK4rT committed Nov 18, 2024
1 parent 161a66d commit 310e1aa
Showing 1 changed file with 42 additions and 16 deletions.
58 changes: 42 additions & 16 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 @@ -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 310e1aa

Please sign in to comment.