Allow typing touch code on arduino

This commit is contained in:
Alexandertp 2023-12-20 21:32:14 +01:00
parent 228ccc7d04
commit d6ed30ca78
3 changed files with 68 additions and 6 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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;
};
}