From d6ed30ca78d436cf7f5f3a43c0d5fbcbf4efb211 Mon Sep 17 00:00:00 2001 From: Alexandertp Date: Wed, 20 Dec 2023 21:32:14 +0100 Subject: [PATCH] Allow typing touch code on arduino --- arduino/arduino.ino | 4 +-- arduino/motor.ino | 2 +- arduino/state.ino | 68 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/arduino/arduino.ino b/arduino/arduino.ino index 295fa01..24b2f34 100644 --- a/arduino/arduino.ino +++ b/arduino/arduino.ino @@ -9,7 +9,7 @@ MKRIoTCarrier carrier; WiFiSSLClient wifi_client; MqttClient mqtt_client(wifi_client); -//motor control variables +// Motor control variables int motor_count = 0; int motor_count_max = 64; @@ -83,6 +83,4 @@ void loop() } loop_state(); - - delay(50); } diff --git a/arduino/motor.ino b/arduino/motor.ino index c2a90da..9e5f29a 100644 --- a/arduino/motor.ino +++ b/arduino/motor.ino @@ -1,6 +1,6 @@ -//motorgøgl, pls dont touch int motor_speed = 1200; int motor_lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001}; + // Motor pins // Pins 8 and 9 conflict with the display int motor_pin1 = 10; // Blue - 28BYJ48 pin 1 diff --git a/arduino/state.ino b/arduino/state.ino index fcbc7ba..70bcf26 100644 --- a/arduino/state.ino +++ b/arduino/state.ino @@ -1,10 +1,37 @@ +#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 (new_state) { + switch (current_state) { case STATE_CONNECTING: carrier.display.setCursor(20, 100); carrier.display.setTextSize(3); @@ -20,9 +47,25 @@ void change_state(enum state new_state) 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; } - current_state = new_state; } void loop_state() @@ -32,6 +75,27 @@ void loop_state() 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; }; } \ No newline at end of file