"""
from machine import Pin, SPI, RTC, Timer
import max7219
import time
# =========================
# CONFIG MATRIX
# =========================
spi = SPI(1, baudrate=10000000, polarity=0, phase=0,
sck=Pin(10), mosi=Pin(11))
cs = Pin(13, Pin.OUT)
display = max7219.Matrix8x8(spi, cs, 4) # 4 modules = 32x8
display.brightness(5)
display.fill(0)
display.show()
# =========================
# POLICE 3x5
# =========================
FONT = {
"0": [0b111,0b101,0b101,0b101,0b111],
"1": [0b010,0b110,0b010,0b010,0b111],
"2": [0b111,0b001,0b111,0b100,0b111],
"3": [0b111,0b001,0b111,0b001,0b111],
"4": [0b101,0b101,0b111,0b001,0b001],
"5": [0b111,0b100,0b111,0b001,0b111],
"6": [0b111,0b100,0b111,0b101,0b111],
"7": [0b111,0b001,0b010,0b010,0b010],
"8": [0b111,0b101,0b111,0b101,0b111],
"9": [0b111,0b101,0b111,0b001,0b111],
":": [0b000,0b010,0b000,0b010,0b000]
}
def draw_char_3x5(char, x, y):
pattern = FONT.get(char)
if not pattern:
return
for row in range(5):
line = pattern[row]
for col in range(3):
if line & (1 << (2-col)):
display.pixel(x+col, y+row, 1)
# =========================
# HORLOGE AUTONOME
# =========================
class ClockRTC:
def __init__(self):
dt=[0,0,0,0,12,45,22]
self.rtc = RTC()
def draw_time(self, t=None):
global dt
if t is None:
t = self.rtc.datetime()
h = "{:02}".format(t[4])
m = "{:02}".format(t[5])
s = "{:02}".format(t[6])
display.fill(0)
x = 0
for char in h + ":" + m + ":" + s:
draw_char_3x5(char, x, 1)
x += 4 # 3 pixels + 1 pixel d'espace
display.show()
clock = ClockRTC()
# =========================
# TIMER HARDWARE NON BLOQUANT
# =========================
def tick(timer):
clock.draw_time() # mise à jour automatique chaque seconde
tmr = Timer()
tmr.init(period=1000, mode=Timer.PERIODIC, callback=tick)
# =========================
# BOUCLE PRINCIPALE
# =========================
# Tu n’as plus besoin d’appeler update()
while True:
time.sleep(1)
# programme principal continue librement
# lecture boutons, effets LED, etc
pass
"""
from machine import Pin, SPI, RTC, Timer
import max7219
import time
# =========================
# CONFIG MATRIX
# =========================
spi = SPI(1, baudrate=10000000, polarity=0, phase=0,
sck=Pin(10), mosi=Pin(11))
cs = Pin(13, Pin.OUT)
display = max7219.Matrix8x8(spi, cs, 4) # 4 modules = 32x8
display.brightness(5)
display.fill(0)
display.show()
# =========================
# POLICE 3x5
# =========================
FONT = {
"0": [0b111,0b101,0b101,0b101,0b111],
"1": [0b010,0b110,0b010,0b010,0b111],
"2": [0b111,0b001,0b111,0b100,0b111],
"3": [0b111,0b001,0b111,0b001,0b111],
"4": [0b101,0b101,0b111,0b001,0b001],
"5": [0b111,0b100,0b111,0b001,0b111],
"6": [0b111,0b100,0b111,0b101,0b111],
"7": [0b111,0b001,0b010,0b010,0b010],
"8": [0b111,0b101,0b111,0b101,0b111],
"9": [0b111,0b101,0b111,0b001,0b111],
":": [0b000,0b010,0b000,0b010,0b000]
}
def draw_char_3x5(char, x, y):
pattern = FONT.get(char)
if not pattern:
return
for row in range(5):
line = pattern[row]
for col in range(3):
if line & (1 << (2-col)):
display.pixel(x+col, y+row, 1)
# =========================
# HORLOGE AUTONOME
# =========================
class ClockRTC:
def __init__(self):
self.rtc = RTC()
self.colon_visible = True # état du ":"
def draw_time(self, t=None):
print(self.colon_visible)
self.colon_visible = not self.colon_visible
if t is None:
t = self.rtc.datetime()
h = "{:02}".format(t[4])
m = "{:02}".format(t[5])
display.fill(0)
x = 0
for char in h:
draw_char_3x5(char, x, 1)
x += 4
# ":" clignotant
if self.colon_visible:
draw_char_3x5(":", x, 1)
x += 4
for char in m:
draw_char_3x5(char, x, 1)
x += 4
display.show()
clock = ClockRTC()
# =========================
# TIMERS
# =========================
# Timer 1000 ms pour mise à jour de l'heure
def update_time(timer):
clock.draw_time()
tmr_time = Timer()
tmr_time.init(period=1000, mode=Timer.PERIODIC, callback=update_time)
# Timer 500 ms pour clignotement du ":"
def blink_colon(timer):
clock.colon_visible = not clock.colon_visible
tmr_blink2 = Timer()
tmr_blink2.init(period=500, mode=Timer.PERIODIC, callback=blink_colon)
# =========================
# BOUCLE PRINCIPALE
# =========================
while True:
time.sleep(0.020)
pass # boucle principale libre pour autres tâches