slik-dispenser/arduino/state.ino

101 lines
2.1 KiB
C++

#define INPUT_SIZE 4
enum state current_state;
char input_code[INPUT_SIZE];
bool is_input_empty()
{
for (int i = 0; i < INPUT_SIZE; i++) {
if (input_code[i]) return false;
}
return true;
}
void change_state(enum state new_state)
{
// Initialize state
switch (new_state) {
case STATE_INPUT_PASSCODE:
input_code[0] = '\0';
input_code[1] = '\0';
input_code[2] = '\0';
input_code[3] = '\0';
break;
}
current_state = new_state;
draw_state();
}
void draw_state()
{
carrier.display.fillScreen(0x000);
switch (current_state) {
case STATE_CONNECTING:
carrier.display.setCursor(20, 100);
carrier.display.setTextSize(3);
carrier.display.print("Connecting..");
break;
case STATE_LOGGED_OUT:
carrier.display.setCursor(15, 100);
carrier.display.setTextSize(2);
carrier.display.print("Press Green Button");
carrier.leds.fill(green_led_color, 2, 1);
carrier.leds.setBrightness(5);
carrier.leds.show();
break;
case STATE_INPUT_PASSCODE:
if (is_input_empty()) {
carrier.display.setCursor(50, 100);
carrier.display.setTextSize(2);
carrier.display.print("Please enter");
carrier.display.setCursor(80, 120);
carrier.display.print("passcode");
} else {
carrier.display.setCursor(50, 100);
carrier.display.setTextSize(3);
for (int i = 0; i < INPUT_SIZE; i++) {
char ch = input_code[i];
if (!ch) ch = '_';
carrier.display.print(ch);
carrier.display.print(' ');
}
}
break;
}
}
void loop_state()
{
switch (current_state) {
case STATE_LOGGED_OUT:
if (carrier.Buttons.onTouchDown(TOUCH2)) {
change_state(STATE_INPUT_PASSCODE);
}
break;
case STATE_INPUT_PASSCODE:
touchButtons buttons[] = { TOUCH0, TOUCH1, TOUCH3, TOUCH4 };
char input_characters[] = "1234";
for (int i = 0; i < sizeof(buttons) / sizeof(buttons[0]); i++) {
if (carrier.Buttons.onTouchDown(buttons[i])) {
for (int j = 0; j < sizeof(input_code); j++) {
if (!input_code[j]) {
input_code[j] = input_characters[i];
break;
}
}
draw_state();
break;
}
}
break;
};
}