-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
94 lines (74 loc) · 2.4 KB
/
main.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
Interruption of four internal Timers.
ESP32 has four 64-bit hardware timers (Timer0, Timer1, Timer2,
and Timer3) based on 16-bit pre-scalers. Init all timers and
wait for interrupt signals.
Inspired by:
* https://www.upesy.com/blogs/tutorials/timer-esp32-with-micro-python-scripts#
* https://microcontrollerslab.com/micropython-timers-esp32-esp8266-generate-delay/
Author: Wokwi, Tomas Fryza
Creation Date: 2023-10-17
Last Modified: 2024-10-08
"""
from machine import Timer
from machine import Pin
import sys
def toggleLed(t):
led.value(not led.value())
def handleInterrupt(t):
global counter
counter += 1
print(f"Timer1 interrupted {counter} time(s)")
led = Pin(2, Pin.OUT)
counter = 0 # Global variable
# Create a timer object
tim0 = Timer(0)
# Periodically invert LED value 5 times per second
tim0.init(mode=Timer.PERIODIC,
freq=5,
callback=toggleLed)
# Init function has three parameters:
# -- mode: PERIODIC (periodically) or ONE_SHOT (once)
# -- period or freq: Period in ms of frequency in Hz
# -- callback: Interrupt routine when timer is triggered
# Periodically call function every two seconds
tim1 = Timer(1)
tim1.init(mode=Timer.PERIODIC,
freq=.5,
callback=handleInterrupt)
# Print info just ones after 5_000 millisecs
tim2 = Timer(2)
tim2.init(mode=Timer.ONE_SHOT,
period=5000,
callback=lambda t:print(f"{t} callback"))
# A lambda function is defined without a name and evaluates and
# returns only one expression.
# Syntax
# lambda param(s): expression
# -- lambda: analog of `def` in normal functions
# -- param(s): argument(s) just like normal function
# -- expression: code being executed, ideally a single-line
# Stop and disable Timer1 peripheral after 10 secs
tim3 = Timer(3)
tim3.init(mode=Timer.ONE_SHOT,
period=10000,
callback=lambda t:tim1.deinit())
print("Timer 0 set to `PERIODIC, freq=5 Hz`")
print("Timer 1 set to `PERIODIC, freq=1/2 Hz`")
print("Timer 2 set to `ONE_SHOT, period=5_000 ms`")
print("Timer 3 set to `ONE_SHOT, period=10_000 ms`")
print(f"Press `Ctrl+C` to stop")
try:
while True:
pass
except KeyboardInterrupt:
# This part runs when Ctrl+C is pressed
print("Program stopped. Exiting...")
# Optional cleanup code
led.off()
tim0.deinit()
tim1.deinit()
tim2.deinit()
tim3.deinit()
# Stop program execution
sys.exit(0)