Zum Hauptinhalt springen

Die Tasten auslesen

Was dieses Beispiel zeigt​

Alle 16 Pads werden alle 50 ms ausgelesen und als 4x4-Karte im Serial Monitor mit 115200 Baud ausgegeben: X für ein gedrücktes Pad, . für ein losgelassenes. Die LEDs bleiben aus.

Der Beispielcode​

Dies ist examples/ReadButtons/ReadButtons.ino aus der fest verwendeten Bibliotheksversion, unverändert.

Select Board
ReadButtons.ino
/**
**************************************************
*
* @file ReadButtons.ino
* @brief Example showing how to read all 16 buttons on the Inputronic Grid.
*
* Reads all 16 button states every 50 ms and prints a 4×4 grid
* map to the Serial monitor. Pressed buttons show 'X'; released
* buttons show '.'.
*
* Hardware wiring:
* - Connect Inputronic Grid to host via Qwiic / I2C
* (SDA, SCL, 3V3, GND).
*
* @authors Josip Šimun Kuči @ soldered.com
***************************************************/

#include <InputronicGrid.h>

InputronicGrid grid;

void setup()
{
Serial.begin(115200);
while (!Serial)
;

if (!grid.begin())
{
Serial.println("Inputronic GRID not found. Check wiring.");
while (1)
;
}

Serial.println("Inputronic GRID ready.\n");
grid.clearLEDs();
}

void loop()
{
Serial.println("+---------+");
for (uint8_t row = 0; row < 4; row++)
{
Serial.print("| ");
for (uint8_t col = 0; col < 4; col++)
{
Serial.print(grid.readPad(row, col) ? 'X' : '.');
Serial.print(' ');
}
Serial.println("|");
}
Serial.println("+---------+\n");

delay(50);
}

Erwartetes Ergebnis, wenn nichts gedrückt ist:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
Inputronic GRID ready.

+---------+
| . . . . |
| . . . . |
| . . . . |
| . . . . |
+---------+

Alle 50 ms folgt eine neue Karte. Solange Sie ein Pad gedrückt halten, steht an der Stelle seines . ein X: Die oberste Zeile der Karte ist Zeile 0, und das erste Zeichen in einer Zeile ist Spalte 0. Antwortet das Board nicht, gibt der Sketch Inputronic GRID not found. Check wiring. aus und hält an.

Verwendete Funktionen​

InputronicGrid(uint8_t address = INPUTRONIC_GRID_DEFAULT_ADDR)returns InputronicGrid

Erstellt das Driver-Objekt für ein GRID. Das Beispiel deklariert es als InputronicGrid grid; ohne Adresse, daher verwendet es die Standardadresse 0x30.

ReturnsDas Driver-Objekt.

Parameter

TypNameBeschreibung
uint8_taddressI2C-Adresse des Boards. Optional, standardmäßig 0x30 (INPUTRONIC_GRID_DEFAULT_ADDR).
grid.begin(TwoWire &wire = Wire)returns bool

Startet den I2C-Bus mit wire.begin() und prüft, ob das Board unter seiner Adresse antwortet.

Returnstrue, wenn das Board am Bus bestätigt hat, false, wenn es nicht gefunden wurde.

Parameter

TypNameBeschreibung
TwoWire &wireDer zu verwendende I2C-Bus. Optional, standardmäßig Wire.
grid.clearLEDs()returns void

Schaltet alle 16 LEDs aus. Das ist dasselbe wie setAllLEDs(0, 0, 0).

ReturnsNichts.

grid.readPad(uint8_t row, uint8_t col)returns bool

Fragt das Board, ob ein Pad gerade gedrückt ist, anhand seiner Position im Raster. Jeder Aufruf sendet dem Board eine kurze I2C-Anfrage und liest seine Antwort von einem Byte.

Returnstrue, solange das Pad gedrückt ist, false, wenn nicht.

Parameter

TypNameBeschreibung
uint8_trowZeile, 0 bis 3, von oben nach unten.
uint8_tcolSpalte, 0 bis 3, von links nach rechts.

Alles zusammengesetzt​

setup() wartet auf den Serial Monitor, prüft das Board mit begin() und schaltet die LEDs aus. Bei jedem Durchlauf ruft loop() für jede Zeile und Spalte einmal readPad() auf und gibt das Ergebnis als eine Zeile pro Reihe aus.

Um alle 16 Pads mit einer einzigen Anfrage und Antwort statt mit sechzehn auszulesen, hat die Bibliothek außerdem readAllPads(). Die Funktion gibt eine 16-Bit-Maske zurück, in der das Bit row * 4 + col für jedes gedrückte Pad gesetzt ist. In diesem Commit verwendet kein Beispiel sie.