use callback example
This commit is contained in:
parent
c8f0c34c77
commit
161acf624c
1 changed files with 32 additions and 33 deletions
65
src/main.cpp
65
src/main.cpp
|
@ -5,6 +5,31 @@
|
|||
|
||||
Adafruit_NeoKey_1x4 neokey; // Create the NeoKey object
|
||||
|
||||
uint32_t Wheel(byte WheelPos);
|
||||
NeoKey1x4Callback blink(keyEvent evt);
|
||||
|
||||
//define a callback for key presses
|
||||
NeoKey1x4Callback blink(keyEvent evt) {
|
||||
uint8_t key = evt.bit.NUM;
|
||||
|
||||
if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_RISING) {
|
||||
Serial.print("Key press ");
|
||||
Serial.println(key);
|
||||
neokey.pixels.setPixelColor(key, Wheel(map(key, 0, neokey.pixels.numPixels(), 0, 255)));
|
||||
|
||||
} else if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_FALLING) {
|
||||
Serial.print("Key release ");
|
||||
Serial.println(key);
|
||||
|
||||
neokey.pixels.setPixelColor(key, 0);
|
||||
}
|
||||
|
||||
// Turn on/off the neopixels!
|
||||
neokey.pixels.show();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (! Serial) delay(10);
|
||||
|
@ -27,43 +52,17 @@ void setup() {
|
|||
neokey.pixels.show();
|
||||
delay(50);
|
||||
}
|
||||
|
||||
// set callbacks
|
||||
for(int i=0; i<NEOKEY_1X4_KEYS; i++){
|
||||
neokey.registerCallback(i, blink);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
uint8_t buttons = neokey.read();
|
||||
// we handle all key events with the callbacks
|
||||
neokey.read();
|
||||
|
||||
// Check each button, if pressed, light the matching neopixel
|
||||
|
||||
if (buttons & (1<<0)) {
|
||||
Serial.println("Button A");
|
||||
neokey.pixels.setPixelColor(0, 0xFF0000); // red
|
||||
} else {
|
||||
neokey.pixels.setPixelColor(0, 0);
|
||||
}
|
||||
|
||||
if (buttons & (1<<1)) {
|
||||
Serial.println("Button B");
|
||||
neokey.pixels.setPixelColor(1, 0xFFFF00); // yellow
|
||||
} else {
|
||||
neokey.pixels.setPixelColor(1, 0);
|
||||
}
|
||||
|
||||
if (buttons & (1<<2)) {
|
||||
Serial.println("Button C");
|
||||
neokey.pixels.setPixelColor(2, 0x00FF00); // green
|
||||
} else {
|
||||
neokey.pixels.setPixelColor(2, 0);
|
||||
}
|
||||
|
||||
if (buttons & (1<<3)) {
|
||||
Serial.println("Button D");
|
||||
neokey.pixels.setPixelColor(3, 0x00FFFF); // blue
|
||||
} else {
|
||||
neokey.pixels.setPixelColor(3, 0);
|
||||
}
|
||||
|
||||
neokey.pixels.show();
|
||||
|
||||
delay(10); // don't print too fast
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue