From 3c0e703a3189c342f3127916d89ae8f3b08e5c84 Mon Sep 17 00:00:00 2001 From: Malte Schmitz <malte@schmitz-sh.de> Date: Sun, 29 May 2022 15:51:25 +0200 Subject: [PATCH] Add first simple game logic --- src/main.cpp | 82 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 23 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 48c5700..a222598 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,43 +45,79 @@ void loop() { scheduler.execute(); } -uint32_t color = 0xFFFFFF; int position = 0; +int last_internal_button_state = HIGH; +int last_external_button_state = HIGH; +unsigned long last_internal_button_pressed = 0; +unsigned long last_external_button_pressed = 0; + +#define STATE_PAUSE 0 +#define STATE_FORWARD 1 +#define STATE_BACKWARD 2 +int state = STATE_PAUSE; void show() { for (int i = 0; i < NEOPIXEL_LEN; i++) { pixels.setPixelColor(i, 0); } - for (int i = position; i < position + 10; i++) { - pixels.setPixelColor(i, color); + if (state > STATE_PAUSE) { + pixels.setPixelColor(position, 0xFFFFFF); } pixels.show(); } void handleButton() { - if (digitalRead(INTERNAL_BUTTON_PIN_B) == LOW) { - color = 0x0000FF; - } else if (digitalRead(EXTERNAL_BUTTON_PIN_NO) == LOW) { - color = 0xFF0000; - } else if (digitalRead(EXTERNAL_BUTTON_PIN_B) == LOW) { - color = 0x00FF00; + int current_internal_button_state = digitalRead(INTERNAL_BUTTON_PIN_B); + int current_external_button_state = digitalRead(EXTERNAL_BUTTON_PIN_B); + + if (last_internal_button_state == HIGH && current_internal_button_state == LOW) { + last_internal_button_pressed = millis(); } - show(); + + if (last_external_button_state == HIGH && current_external_button_state == LOW) { + last_external_button_pressed = millis(); + } + + last_internal_button_state = current_internal_button_state; + last_external_button_state = current_external_button_state; } -boolean forward = true; void handleStep() { - if (forward) { - position = position + 1; - if (position > NEOPIXEL_LEN - 11) { - position = position - 2; - forward = false; - } - } else { - position = position - 1; - if (position < 0) { - position = 1; - forward = true; - } + unsigned long now = millis(); + switch (state) { + case STATE_PAUSE: + if (now - last_internal_button_pressed < 100) { + position = 0; + state = STATE_FORWARD; + } + if (now - last_external_button_pressed < 100) { + position = NEOPIXEL_LEN - 1; + state = STATE_BACKWARD; + } + break; + case STATE_FORWARD: + position = position + 1; + if (position > NEOPIXEL_LEN - 1) { + if (now - last_external_button_pressed < 100) { + position = position - 2; + state = STATE_BACKWARD; + } else { + state = STATE_PAUSE; + } + } + break; + case STATE_BACKWARD: + position = position - 1; + if (position < 0) { + if (now - last_internal_button_pressed < 100) { + position = 1; + state = STATE_FORWARD; + } else { + state = STATE_PAUSE; + } + } + break; } + + show(); } -- GitLab