-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.py
More file actions
executable file
·30 lines (22 loc) · 959 Bytes
/
Copy pathtimer.py
File metadata and controls
executable file
·30 lines (22 loc) · 959 Bytes
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
# Generador de interrupciones, timer de tiempo real
import time
class Timer:
def __init__(self, freq_hz):
self.timer_enabled = freq_hz > 0 # Si la frecuencia es 0, el timer se desactiva
if self.timer_enabled:
self.period = 1.0 / freq_hz
self.next_fire = time.perf_counter() + self.period
else:
self.period = float('inf') # Período infinito, no manda interrupciones
self.next_fire = float('inf')
self.state = False
def update(self):
if not self.timer_enabled: # Si está desactivado, sale sin calcular nada
return
now = time.perf_counter()
fired = False
while now >= self.next_fire: # Verifica si pasó el tiempo del período
self.next_fire += self.period
fired = True
if fired:
self.state = True # Envia la señal, que puede ser a una linea IRQ o NMI por ejemplo.