-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS31_Emergency_Buffer.py
More file actions
63 lines (53 loc) · 1.98 KB
/
S31_Emergency_Buffer.py
File metadata and controls
63 lines (53 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File: main.py
# Author: Armstrong Subero
# Platform: Raspberry Pi Pico (RP2040) with MicroPython
# Program: S31_Emergency_Exception_Buffer
# Interpreter: MicroPython (latest version)
# Program Version: 1.0
#
# Program Description: This program allows the Raspberry Pi Pico to
# demonstrate superloop style round robin scheduling
# and the use of emergency exception buffer to
# handle exceptions without dynamic memory allocation
# problems
#
# Hardware Description: LEDs are connected via 1k resistors to GP16 and GP17
#
# Created: August 23rd, 2024, 11:05 AM
# Last Updated: August 23rd, 2024, 11:05 AM
import time
import micropython
from machine import Pin
# Define the GPIO pins for the LEDs
LED_ONE = 16
LED_TWO = 17
# Set up the LEDs as output pins
led1 = Pin(LED_ONE, Pin.OUT)
led2 = Pin(LED_TWO, Pin.OUT)
# Allocate emergency exception buffer to handle interrupts
micropython.alloc_emergency_exception_buf(100)
def task1():
try:
led1.toggle()
# Intentionally cause an exception to demonstrate emergency exception buffer
if time.ticks_ms() % 5 == 0:
raise ValueError("Simulated error in task1")
except Exception as e:
print("Emergency Exception in task1:", e)
def task2():
try:
led2.toggle()
# Intentionally cause an exception to demonstrate emergency exception buffer
if time.ticks_ms() % 7 == 0:
raise ValueError("Simulated error in task2")
except Exception as e:
print("Emergency Exception in task2:", e)
# Superloop for round-robin scheduling
time_start = time.ticks_ms()
while True:
task1() # Execute task1 (toggle LED1)
time.sleep_ms(200) # Delay for 200 ms
task2() # Execute task2 (toggle LED2)
time.sleep_ms(200) # Delay for 200 ms
seconds_live = time.ticks_diff(time.ticks_ms(), time_start) / 1000
print("Executing for", seconds_live, "seconds")