Ein Pad beim Drücken beleuchten
Was dieses Beispiel zeigt
Jedes Pad schaltet seine eigene LED. Beim ersten Drücken leuchtet sie in der Farbe ihrer Reihe (Reihe 0 rot, Reihe 1 grün, Reihe 2 blau, Reihe 3 orange), und beim nächsten Drücken geht sie wieder aus. Jeder Tastendruck wird in der Shell ausgegeben.
Der Beispielcode
Dies ist Examples/ButtonLEDControl.py aus dem angegebenen Modul, unverändert. Speichern Sie es
auf Ihrem Board und führen Sie es aus.
# FILE: ButtonLEDControl.py
# AUTHOR: Josip Šimun Kuči @ Soldered
# BRIEF: Example showing button-driven LED toggle on the Inputronic GRID.
# Each button toggles its corresponding LED on or off.
# First press turns the LED on in a row-specific color;
# second press turns it off.
# Demonstrates reading individual pads by row/col coordinate
# and reacting with per-LED color control using the same coordinates.
# WORKS WITH: Inputronic GRID: www.soldered.com
# LAST UPDATED: 2026-05-12
from machine import I2C, Pin
from InputronicGrid import InputronicGrid
import time
# If you aren't using the Qwiic connector, manually enter your I2C pins:
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# grid = InputronicGrid(i2c=i2c)
grid = InputronicGrid()
grid.clearLEDs()
print("Press any button to toggle its LED.")
# Toggle state per button, indexed by [row][col]
ledOn = [[False] * 4 for _ in range(4)]
# Colors per row (R, G, B)
rowColor = [
(255, 0, 0), # row 0 — red
( 0, 255, 0), # row 1 — green
( 0, 0, 255), # row 2 — blue
(255, 128, 0), # row 3 — orange
]
# Previous pad state per [row][col] for edge detection
prevState = [[False] * 4 for _ in range(4)]
while True:
for row in range(4):
for col in range(4):
pressed = grid.readPad(row, col)
if pressed and not prevState[row][col]:
ledOn[row][col] = not ledOn[row][col]
if ledOn[row][col]:
r, g, b = rowColor[row]
grid.setLED(row, col, r, g, b, 255)
else:
grid.setLED(row, col, 0, 0, 0, 255)
print("Button (row {}, col {}) → LED {}".format(
row, col, "ON" if ledOn[row][col] else "OFF"))
prevState[row][col] = pressed
time.sleep_ms(20)
🔗Vollständiges Beispiel öffnen
Examples/ButtonLEDControl.py im Commit 36d3def
Erwartetes Ergebnis, nachdem das Pad in Reihe 0, Spalte 0 zweimal gedrückt wurde:
Press any button to toggle its LED.
Button (row 0, col 0) → LED ON
Button (row 0, col 0) → LED OFF
Die LED des Pads leuchtet beim ersten Drücken rot und geht beim zweiten wieder aus. Antwortet das
Board nicht, löst InputronicGrid() eine Ausnahme mit der Meldung
Inputronic GRID not found. Check wiring. aus.
Verwendete Funktionen
InputronicGrid(i2c=None, address=INPUTRONIC_GRID_DEFAULT_ADDR)returns InputronicGridErstellt das Driver-Objekt und prüft, ob das Board antwortet. Bleibt i2c auf None, erstellt es I2C(0, scl=Pin(22), sda=Pin(21)) auf den Ports, die das Modul erkennt (ESP32, ESP8266 und ein Soldered-Board), und löst bei einem nicht erkannten Port eine Ausnahme aus.
ReturnsDas Driver-Objekt. Löst eine Ausnahme mit 'Inputronic GRID not found. Check wiring.' aus, wenn das Board nicht antwortet.
Parameter
| Typ | Name | Beschreibung |
|---|---|---|
I2C | i2c | Ein fertiges machine.I2C-Objekt. Optional. |
int | address | I2C-Adresse des Boards. Optional, standardmäßig 0x30. |
grid.clearLEDs()returns NoneSchaltet alle 16 LEDs aus. Das entspricht setAllLEDs(0, 0, 0).
ReturnsNichts.
grid.readPad(row, col)returns boolFragt das Board anhand der Position im Raster, ob ein Pad gerade gedrückt ist.
ReturnsTrue, solange das Pad gedrückt ist. False, wenn es nicht gedrückt ist oder die I2C-Kommunikation fehlschlägt.
Parameter
| Typ | Name | Beschreibung |
|---|---|---|
int | row | Reihe, 0 bis 3, von oben nach unten. |
int | col | Spalte, 0 bis 3, von links nach rechts. |
grid.setLED(row, col, r, g, b, intensity=128)returns NoneLegt die Farbe der LED unter einem Pad anhand seiner Position im Raster fest. Jeder Farbwert wird vor dem Senden mit intensity / 255 skaliert. Ein I2C-Fehler wird ignoriert.
ReturnsNichts.
Parameter
| Typ | Name | Beschreibung |
|---|---|---|
int | row | Reihe, 0 bis 3. |
int | col | Spalte, 0 bis 3. |
int | r | Rot, 0 bis 255. |
int | g | Grün, 0 bis 255. |
int | b | Blau, 0 bis 255. |
int | intensity | Helligkeit, 0 (aus) bis 255 (volle Helligkeit). Optional, standardmäßig 128. |
Alles zusammengesetzt
InputronicGrid() richtet I2C ein und prüft das Board, und clearLEDs() startet
mit ausgeschalteten LEDs. Die Schleife liest alle 16 Pads alle 20 ms mit readPad() und
vergleicht jedes mit seinem Zustand aus dem letzten Durchlauf, sodass ein gedrückt gehaltenes Pad
als ein einziger Tastendruck zählt. Bei einem neuen Tastendruck kehrt das Beispiel den Eintrag dieses Pads in ledOn um
und ruft setLED() mit derselben Reihe und Spalte auf, mit voller Helligkeit in der Farbe
der Reihe oder mit 0, 0, 0, um die LED auszuschalten.