Skip to content

Commit c2997ea

Browse files
authored
Revert "[#108] Update watchdog_pet()" (#209)
1 parent b31b47d commit c2997ea

File tree

2 files changed

+79
-108
lines changed

2 files changed

+79
-108
lines changed

lib/pysquared/pysquared.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"""
99

1010
# Common CircuitPython Libs
11-
import asyncio # Add asyncio import
1211
import sys
1312
import time
1413
from collections import OrderedDict
@@ -578,38 +577,11 @@ def date(self, ymdw: tuple[int, int, int, int]) -> None:
578577
"""
579578

580579
def watchdog_pet(self) -> None:
581-
"""Pet the watchdog timer"""
582-
self.logger.debug("Petting watchdog")
583580
self.watchdog_pin.value = True
584581
time.sleep(0.01)
585582
self.watchdog_pin.value = False
586583

587-
async def _watchdog_pet_task(self) -> None:
588-
"""Async task to continuously pet the watchdog"""
589-
self.logger.info("Starting watchdog petting background task")
590-
while self.hardware.get("WDT", False):
591-
self.watchdog_pet()
592-
await asyncio.sleep(1.0) # Pet watchdog every second
593-
self.logger.info("Watchdog petting task stopped")
594-
595-
def start_watchdog_background_task(self) -> None:
596-
"""Start the watchdog petting as a true background task using asyncio"""
597-
self._last_watchdog_pet = time.monotonic()
598-
self.hardware["WDT"] = True
599-
# Create and schedule the task but don't wait for it
600-
asyncio.create_task(self._watchdog_pet_task())
601-
self.logger.info("Watchdog background task created")
602-
603-
def check_watchdog(self) -> None:
604-
"""
605-
Legacy method maintained for compatibility
606-
The actual petting is now handled by the background task
607-
"""
608-
pass # No-op as this is now handled by the async task
609-
610584
def check_reboot(self) -> None:
611-
"""Check if system needs to be rebooted"""
612-
# No need to check watchdog anymore as it's handled by the background task
613585
self.UPTIME: int = self.get_system_uptime
614586
self.logger.debug("Current up time stat:", uptime=self.UPTIME)
615587
if self.UPTIME > self.reboot_time:

main.py

Lines changed: 79 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
Published: Nov 19, 2024
99
"""
1010

11-
import asyncio
11+
import gc
1212
import time
1313

1414
import digitalio
@@ -53,10 +53,7 @@
5353
config: Config = Config("config.json")
5454

5555
c = pysquared.Satellite(config, logger, __version__)
56-
57-
# Start the watchdog background task
58-
c.start_watchdog_background_task()
59-
56+
c.watchdog_pet()
6057
sleep_helper = SleepHelper(c, logger)
6158

6259
radio_manager = RFM9xManager(
@@ -73,107 +70,109 @@
7370

7471
f = functions.functions(c, logger, config, sleep_helper, radio_manager)
7572

76-
async def initial_boot():
73+
def initial_boot():
74+
c.watchdog_pet()
7775
f.beacon()
76+
c.watchdog_pet()
7877
f.listen()
78+
c.watchdog_pet()
7979

80-
async def critical_power_operations():
81-
await initial_boot()
82-
# Convert blocking operation to a non-blocking one using run_in_executor
83-
# Since this is a blocking function that will likely put the device to sleep,
84-
# we accept that this will block the event loop as intended
85-
logger.info("Entering long hibernation (this will block the event loop)")
86-
sleep_helper.long_hibernate()
87-
logger.info("Woke from long hibernation")
80+
try:
81+
c.boot_count.increment()
8882

89-
async def minimum_power_operations():
90-
await initial_boot()
91-
# Same as above - this is a blocking operation by design
92-
logger.info("Entering short hibernation (this will block the event loop)")
93-
sleep_helper.short_hibernate()
94-
logger.info("Woke from short hibernation")
83+
logger.info(
84+
"FC Board Stats",
85+
bytes_remaining=gc.mem_free(),
86+
boot_number=c.boot_count.get(),
87+
)
88+
89+
initial_boot()
90+
91+
except Exception as e:
92+
logger.error("Error in Boot Sequence", e)
9593

96-
async def send_imu_data():
94+
finally:
95+
pass
96+
97+
def send_imu_data():
9798
logger.info("Looking to get imu data...")
9899
IMUData = []
100+
c.watchdog_pet()
101+
logger.info("IMU has baton")
99102
IMUData = f.get_imu_data()
103+
c.watchdog_pet()
100104
f.send(IMUData)
101105

102-
async def main():
106+
def main():
103107
f.beacon()
108+
104109
f.listen_loiter()
110+
105111
f.state_of_health()
112+
106113
f.listen_loiter()
114+
107115
f.all_face_data()
116+
c.watchdog_pet()
108117
f.send_face()
118+
109119
f.listen_loiter()
110-
await send_imu_data()
120+
121+
send_imu_data()
122+
111123
f.listen_loiter()
124+
112125
f.joke()
126+
113127
f.listen_loiter()
114128

129+
def critical_power_operations():
130+
initial_boot()
131+
c.watchdog_pet()
132+
133+
sleep_helper.long_hibernate()
134+
135+
def minimum_power_operations():
136+
initial_boot()
137+
c.watchdog_pet()
138+
139+
sleep_helper.short_hibernate()
140+
115141
######################### MAIN LOOP ##############################
116-
async def main_loop():
117-
"""Async main loop that runs alongside the watchdog task"""
118-
try:
119-
while True:
120-
# L0 automatic tasks no matter the battery level
121-
c.check_reboot() # Now this only checks for reboot conditions
122-
123-
if c.power_mode == "critical":
124-
c.rgb = (0, 0, 0)
125-
await critical_power_operations()
126-
127-
elif c.power_mode == "minimum":
128-
c.rgb = (255, 0, 0)
129-
await minimum_power_operations()
130-
131-
elif c.power_mode == "normal":
132-
c.rgb = (255, 255, 0)
133-
await main()
134-
135-
elif c.power_mode == "maximum":
136-
c.rgb = (0, 255, 0)
137-
await main()
138-
139-
else:
140-
f.listen()
141-
142-
# Small yield to allow other tasks to run
143-
await asyncio.sleep(0.1)
144-
145-
except Exception as e:
146-
logger.critical("Critical in Main Loop", e)
147-
time.sleep(10)
148-
microcontroller.on_next_reset(microcontroller.RunMode.NORMAL)
149-
microcontroller.reset()
150-
finally:
151-
logger.info("Going Neutral!")
152-
c.rgb = (0, 0, 0)
153-
c.hardware["WDT"] = False
154-
155-
# Set up the asyncio event loop
156-
async def run_tasks():
157-
# The watchdog task is already started by c.start_watchdog_background_task()
158-
# Just run the main loop as a task
159-
main_task = asyncio.create_task(main_loop())
160-
161-
try:
162-
# Wait for the main task to complete (it should run forever)
163-
await main_task
164-
except asyncio.CancelledError:
165-
logger.info("Main task was cancelled")
166-
except Exception as e:
167-
logger.critical("Error in run_tasks", e)
168-
raise
169-
170-
# Run the asyncio event loop
171142
try:
172-
asyncio.run(run_tasks())
143+
while True:
144+
# L0 automatic tasks no matter the battery level
145+
c.check_reboot()
146+
147+
if c.power_mode == "critical":
148+
c.rgb = (0, 0, 0)
149+
critical_power_operations()
150+
151+
elif c.power_mode == "minimum":
152+
c.rgb = (255, 0, 0)
153+
minimum_power_operations()
154+
155+
elif c.power_mode == "normal":
156+
c.rgb = (255, 255, 0)
157+
main()
158+
159+
elif c.power_mode == "maximum":
160+
c.rgb = (0, 255, 0)
161+
main()
162+
163+
else:
164+
f.listen()
165+
173166
except Exception as e:
174-
logger.critical("Error in asyncio.run", e)
167+
logger.critical("Critical in Main Loop", e)
168+
time.sleep(10)
175169
microcontroller.on_next_reset(microcontroller.RunMode.NORMAL)
176170
microcontroller.reset()
171+
finally:
172+
logger.info("Going Neutral!")
173+
174+
c.rgb = (0, 0, 0)
175+
c.hardware["WDT"] = False
177176

178177
except Exception as e:
179178
logger.critical("An exception occured within main.py", e)

0 commit comments

Comments
 (0)