-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buttons.cpp
69 lines (58 loc) · 2.07 KB
/
Buttons.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "Buttons.h"
#include "pitches.h"
#include "vibrations.h"
#define COLOR_RGB(r, g, b) (((uint32_t)r << 16) | ((uint32_t)g << 8) | b)
Button::Button(uint8_t sense, uint32_t note, uint8_t ledIndex, uint32_t color) :
sense(sense), note(note), ledIndex(ledIndex), color(color), pressed(false) {
}
void Buttons::play(uint8_t number) {
for (uint8_t i = 0; i < NUMBER_OF_BUTTONS; ++i) {
Button *button = &buttons[i];
pixels->setPixelColor(button->ledIndex, COLOR_RGB(0, 0, 0));
}
Button *button = &buttons[number];
pixels->setPixelColor(button->ledIndex, button->color);
pixels->show();
speaker->play(button->note);
areAnyOn = true;
}
void Buttons::fail() {
for (uint8_t i = 0; i < NUMBER_OF_BUTTONS; ++i) {
Button *button = &buttons[i];
pixels->setPixelColor(button->ledIndex, COLOR_RGB(128, 128, 128));
}
pixels->show();
speaker->fail();
}
Buttons::Buttons(Speaker *speaker, Adafruit_NeoPixel *pixels) :
speaker(speaker), pixels(pixels),
buttons({
Button(PIN_BUTTON_SENSE_1, NOTE_C4, 0, COLOR_RGB(255, 0, 0)),
Button(PIN_BUTTON_SENSE_3, NOTE_A3, 1, COLOR_RGB(9, 0, 255)),
Button(PIN_BUTTON_SENSE_4, NOTE_B3, 2, COLOR_RGB(255, 255, 0)),
Button(PIN_BUTTON_SENSE_2, NOTE_G3, 3, COLOR_RGB(0, 255, 0)),
}) {
}
void Buttons::off() {
for (uint8_t i = 0; i < NUMBER_OF_BUTTONS; ++i) {
Button *button = &buttons[i];
pixels->setPixelColor(button->ledIndex, COLOR_RGB(0, 0, 0));
}
pixels->show();
areAnyOn = false;
}
void Buttons::setup() {
vibration_sensors_setup(vibrationSensors);
}
void Buttons::tick() {
vibration_sensor_t *pressed = vibration_sensors_detect_press(vibrationSensors);
if (pressed != nullptr) {
for (uint8_t i = 0; i < NUMBER_OF_BUTTONS; ++i) {
if (buttons[i].sense == pressed->pin) {
buttons[i].pressed = true;
}
}
}
}
void Buttons::irq() {
}