MacroBoard-rs/macroboard.ino
2019-11-24 15:54:36 +01:00

133 lines
2.6 KiB
C++

/// ----------------------------------------------------------------
/// ----------------------- Type Definitions -----------------------
/// ----------------------------------------------------------------
enum State {
WaitingForInput,
WaitingForAck,
};
struct Button {
const byte pin_id;
uint8_t last_state;
};
/// ----------------------------------------------------------------
/// ----------------------- Board Specifics ------------------------
/// ----------------------------------------------------------------
const int button_count = 8;
const Button buttons[button_count] = {
{
.pin_id = 6,
.last_state = LOW
},
{
.pin_id = 7,
.last_state = LOW
},
{
.pin_id = 8,
.last_state = LOW
},
{
.pin_id = 9,
.last_state = LOW
},
{
.pin_id = 10,
.last_state = LOW
},
{
.pin_id = 11,
.last_state = LOW
},
{
.pin_id = 12,
.last_state = LOW
},
{
.pin_id = 13,
.last_state = LOW
}
};
const char message_start = '<';
const char message_end = '>';
/// ----------------------------------------------------------------
/// -------------------------- Variables ---------------------------
/// ----------------------------------------------------------------
State current_state;
/// ----------------------------------------------------------------
/// --------------------------- Startup ----------------------------
/// ----------------------------------------------------------------
void setup() {
// enable serial port, with 9600 Baud
Serial.begin(9600);
// setup variables
current_state = WaitingForInput;
// setup pins
for (byte i = 0; i < button_count; i++) {
pinMode(&buttons[i], INPUT);
}
}
void loop() {
// check for possible input
if (current_state == WaitingForInput) {
for (byte i = 0; i < button_count; i++) {
check_pin(&buttons[i], i);
}
}
}
void check_pin(Button* button, byte index) {
if (digitalRead(button->pin_id) == HIGH) {
if (button->last_state == LOW) {
button->last_state = HIGH;
/*
// assemble message
Serial.print(message_start);
Serial.print(index + 1);
Serial.print(message_end);
// force new line
Serial.print('\n');
*/
debug_message(button->pin_id, "HIGH");
}
} else {
if (button->last_state == HIGH) {
button->last_state = LOW;
debug_message(button->pin_id, "LOW");
}
}
}
void debug_message(byte pin_id, char c[]) {
// assemble message
Serial.print(message_start);
Serial.print(pin_id);
Serial.print(' ');
Serial.print(c);
Serial.print(message_end);
// force new line
Serial.print('\n');
}