Move exmaples to separate directory

This commit is contained in:
2026-01-22 20:05:53 +01:00
parent 654303b9ec
commit d6e0fb978e
3 changed files with 148 additions and 0 deletions

49
examples/i2c-schedule.py Normal file
View File

@@ -0,0 +1,49 @@
from time import sleep
from random import binomialvariate
DISPLAY = "display"
SENSOR = "sensor"
ZIGBEE = "zigbee"
LABELS = {DISPLAY, SENSOR, ZIGBEE}
time = 0
transactions = []
active_transaction = None
slice_labels = {l: "" for l in LABELS}
def display():
if time % 5 == 1:
transactions.append((DISPLAY, 4 if binomialvariate(p=0.15) else 2, 50))
def sensor():
if time % 5 == 0:
transactions.append((SENSOR, 1, 10))
def zigbee():
if time % 15 == 14:
transactions.append((ZIGBEE, 10, 100))
print(' '*5, *(l[0] for l in LABELS))
while True:
display()
sensor()
zigbee()
if active_transaction is None and transactions:
active_transaction = min(transactions, key=lambda t: t[2])
transactions.remove(active_transaction)
print(f"t={time:02}: ", end="")
if active_transaction is not None:
label, duration, priority = active_transaction
print(*('x' if l == label else '#' if any(t[0]==l for t in transactions) else ' ' for l in LABELS))
active_transaction = (label, duration-1, priority) if duration > 1 else None
transactions = [(l, d, p*0.8) for (l, d, p) in transactions]
else:
print(" ")
time += 1
sleep(1)