UCUq sans interface (headless)

UCUq sans interface (headless) #

Cliquez sur l’encart pour lancer le programme. Pour réduire l’encart, cliquez à nouveau sur son en-tête.

Vous pouvez modifier le programme directement dans l’éditeur et la relancer avec le bouton Run.

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)

SSD1306 (OLED) #

PRENOM = """ """ import ucuq, datetime oled = ucuq.SSD1306_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)

Buzzer #

FREQ = 440 import ucuq buzzer = ucuq.PWM(5, freq=FREQ, u16 = 32000) ucuq.sleep(1) buzzer.setU16(0)

WS2812 (LEDs RGB) #

R = 0 G = 30 B = 0 import ucuq ucuq.WS2812(20, 8).fill([R,G,B]).write()

HD44780 (LCD) #

import datetime LINE_1 = "Hello, World!" LINE_2 = f"{datetime.datetime.now().strftime("%d/%m/%Y %H:%M")}" import ucuq lcd = ucuq.HD44780_I2C(16, 2,ucuq.SoftI2C(6, 7)).backlightOn() lcd\ .putString(LINE_1)\ .moveTo(0,1)\ .putString(LINE_2)\

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)

Autre #

Avec Blockly.