UCUq #
The UCUq library purpose is is intended to help prototyping devices build around Wi-Fi equipped microcontrolers (Raspberry Pi Pico (2) W, ESP32 series, ESP8266…).
This section demonstrates the use of the UCUq library using some specifically made physical devices, so it will probably of no use for you, but you can find some online demonstrations of the UCUq library which relies on a ESP32 simulator ont this page.
Golf (SSD1306) #
Remise à zéro globale #
import ucuq
ucuq.HD44780_I2C(16, 2, ucuq.SoftI2C(6, 7))
ucuq.SSD1306_I2C(128, 64, ucuq.I2C(8,9))
ucuq.WS2812(20, 8).fill([30,30,30])
ucuq.PWM(5, freq=500).setNS(0)
HD44780 (LCD) #
NAME = """
"""
import datetime, ucuq, unicodedata
LINE_1 = 'Ravi de vous rencontrer, '
LINE_2 = 'Comment va ?'
DELAY = 0.25
LCD = ucuq.HD44780_I2C(16, 2, ucuq.SoftI2C(6, 7)).backlightOn()
PADDING = ' ' * 15
def display(name):
if name := name.strip():
name = ''.join(c for c in unicodedata.normalize('NFD', name) if unicodedata.category(c) != 'Mn')
Message = ''.join([str(x) for x in [PADDING, LINE_1, name, ',', PADDING[: int(15 - len(name))]]])
LCD.clear()
for j in range(len(Message) - 15):
LCD.moveTo(0,0)
LCD.putString((Message[int((j + 1) - 1) : int(j + 16)]))
ucuq.sleep(DELAY)
LCD.moveTo(0,1)
LCD.putString(LINE_2)
else:
LCD\
.putString("Hello, World!")\
.moveTo(0,1)\
.putString(f"{datetime.datetime.now().strftime("%d/%m/%Y %H:%M")}")\
.backlightOn()
display(NAME)
WS2812 (RGB LEDs strip) #
Hangman game #
Simon’s game #
Countdown #
Hotel (SH1106) #
SH1106 (OLED) #
PRENOM = """
"""
import ucuq, datetime
oled = ucuq.SH1106_I2C(128, 64, ucuq.I2C(8, 9))
def display(prenom, y):
if prenom:
oled\
.text("Bien le bonjour,", 0, y)\
.text(f"{prenom} !", 0, y+10)\
.text("Merci pour votre", 0, y+45)\
.text("participation !", 0, y+55)\
.show()
else:
oled\
.text("Test accompli le", 0, y+20)\
.text(f"{datetime.datetime.now().strftime("%d/%m/%Y %H:%M")}", 0, y+40)\
.show()
for i in range(64):
oled.fill(0)
display(ucuq.toASCII(PRENOM.strip()), 64-i)
Frère Jacques (buzzers) #
import ucuq, re, math
NOTE_MAP = {
'C': -9, 'C#': -8, 'Db': -8, 'D': -7, 'D#': -6, 'Eb': -6,
'E': -5, 'F': -4, 'F#': -3, 'Gb': -3, 'G': -2, 'G#': -1, 'Ab': -1,
'A': 0, 'A#': 1, 'Bb': 1, 'B': 2
}
b = []
b.append(ucuq.PWM(13, freq=440, u16=32000))
ucuq.sleep(0.5)
b[0].setU16(0)
b.append(ucuq.PWM(2, freq=550, u16=32000))
ucuq.sleep(0.5)
b[1].setU16(0)
b.append(ucuq.PWM(7, freq=660, u16=32000))
ucuq.sleep(0.5)
b[2].setU16(0)
ucuq.sleep(1)
def note_to_freq(note_str, octave):
if note_str == 'R':
return 0 # silence
if len(note_str) == 2 and note_str[1] in('b','#'):
note_key = note_str
else:
note_key = note_str[0]
if note_key not in NOTE_MAP:
return 0
note = 12 * (int(octave) + 1) + NOTE_MAP[note_key]
return 440.0 * (2 ** ((note - 57) / 12.0))
def duration_to_seconds(duration, base, dots=0):
value = 1 / (2 ** (4 - duration))
total = value
for _ in range(dots):
value /= 2
total += value
return base * total
def parse_note_string(note_str, base):
match = re.match(r'([A-Z][b#]?)(\d)(\d)(\.*)', note_str)
if not match:
match = re.match(r'(R)(\d)(\.*)', note_str)
if not match:
return None
octave = 0
note, duration, dots = match.groups()
else:
note, octave, duration, dots = match.groups()
return note_to_freq(note, int(octave)), duration_to_seconds(int(duration), base, len(dots)),
def extract_notes(voice_str):
return re.findall(r'([A-Z][b#]?\d\d\.*|R\d\.*)', voice_str)
def generate_polyphonic_events_one_voice_per_event(voices, tempo, callback):
voice_notes = [extract_notes(v) for v in voices]
raws = []
for a in voice_notes:
raw = []
for b in a:
raw.append(parse_note_string(b, 60.0 / tempo))
raw.append((0, 0))
raws.append(raw)
indexes = [0 for _ in raws]
freqs = [0 for _ in raws]
delays = [0 for _ in raws]
while any(i != None for i in indexes):
events = []
delay = 100000
for i in range(len(indexes)):
if indexes[i] != None:
if delays[i] == 0:
freqs[i], delays[i] = raws[i][indexes[i]]
indexes[i] += 1
events.append((i, freqs[i]))
delay = min(delay, delays[i])
callback(events, delay)
for i in range(len(indexes)):
if indexes[i] != None and indexes[i] >= len(raws[i]):
indexes[i] = None
else:
delays[i] -= delay
def callback(events, duration):
if duration:
id = ucuq.sleepStart()
for event in events:
buzzer = b[event[0]]
buzzer.setU16(0)
if event[1] != 0:
buzzer.setFreq(int(event[1])).setU16(10000 + 25000 * event[0])
if duration:
ucuq.sleepWait(id, duration)
VOICES = (
"C44D44E44C44C44D44E44C44E44F44G45E44F44G45G43.A42G43F43E44C44G43.A42G43F43E44C44C44G34C45C44G34C45C44D44E44C44C44D44E44C44E44F44G45E44F44G45G43.A42G43F43E44C44G43.A42G43F43E44C44C44G34C45C44G34C45",
"R6R6C44D44E44C44C44D44E44C44E44F44G45E44F44G45G43.A42G43F43E44C44G43.A42G43F43E44C44C44G34C45C44G34C45C44D44E44C44C44D44E44C44E44F44G45E44F44G45G43.A42G43F43E44C44G43.A42G43F43E44C44C44G34C45C44G34C45",
"R6R6R6R6C44D44E44C44C44D44E44C44E44F44G45E44F44G45G43.A42G43F43E44C44G43.A42G43F43E44C44C44G34C45C44G34C45C44D44E44C44C44D44E44C44E44F44G45E44F44G45G43.A42G43F43E44C44G43.A42G43F43E44C44C44G34C45C44G34C45",
)
generate_polyphonic_events_one_voice_per_event(VOICES, 120, callback)
Felix (Echo) #
Piano (buzzer, loudspeaker) #
HT16K33 (LED matrix) #
Servos #
Divers #
Headless | avec Blockly | avec GUI |
---|
Articles journaux :