From ac6fcae9b52b08ca718e6fd3b6540ef5c8613928 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Thu, 29 Apr 2021 17:15:49 +0200 Subject: [PATCH] Extract utilities functions and add a test loop --- super_simon/src/main.cpp | 51 +++++++++++++++++---------------------- super_simon/src/main.h | 38 +++++++++++++++++++++++++++++ super_simon/src/utils.cpp | 33 +++++++++++++++++++++++++ super_simon/src/utils.h | 13 ++++++++++ 4 files changed, 106 insertions(+), 29 deletions(-) create mode 100644 super_simon/src/main.h create mode 100644 super_simon/src/utils.cpp create mode 100644 super_simon/src/utils.h diff --git a/super_simon/src/main.cpp b/super_simon/src/main.cpp index 1bcb558..0c29931 100644 --- a/super_simon/src/main.cpp +++ b/super_simon/src/main.cpp @@ -1,29 +1,8 @@ #include #include +#include "main.h" +#include "utils.h" -#define LCD_COLS 16 -#define LCD_ROWS 2 - -#define BUTTON_GREEN 2 -#define BUTTON_YELLOW 10 -#define BUTTON_BLUE 12 -#define BUTTON_RED 3 - -#define LED_GREEN A5 -#define LED_YELLOW A4 -#define LED_BLUE A3 -#define LED_RED A2 - -#define TONE_BLUE 329 -#define TONE_RED 440 -#define TONE_GREEN 165 -#define TONE_YELLOW 277 - -#define BUZZER 11 - -const uint8_t LEDS[] = {LED_GREEN, LED_YELLOW, LED_BLUE, LED_RED}; -const uint8_t BUTTONS[] = {BUTTON_GREEN, BUTTON_YELLOW, BUTTON_BLUE, BUTTON_RED}; -const uint16_t TONES[] = {TONE_GREEN, TONE_YELLOW, TONE_BLUE, TONE_RED}; LiquidCrystal lcd(9, 8, 4, 5, 6, 7); void setup() { @@ -31,17 +10,31 @@ void setup() { digitalWrite(LED_BUILTIN, HIGH); lcd.begin(LCD_COLS, LCD_ROWS); pinMode(BUZZER, OUTPUT); - for (int i = 0; i < 4; i++) { - const uint8_t led = LEDS[i]; + for (byte i = 0; i < 4; i++) { + const byte led = LEDS[i]; pinMode(led, OUTPUT); pinMode(BUTTONS[i], INPUT_PULLUP); - digitalWrite(led, HIGH); - tone(BUZZER, TONES[i]); + activate(i); delay(300); } - noTone(BUZZER); digitalWrite(LED_BUILTIN, LOW); + deactivateAll(); } void loop() { -} \ No newline at end of file + if (buttonIsPressed(GREEN)) { + activate(GREEN); + } + else if (buttonIsPressed(YELLOW)) { + activate(YELLOW); + } + else if (buttonIsPressed(BLUE)) { + activate(BLUE); + } + else if (buttonIsPressed(RED)) { + activate(RED); + } + else { + deactivateAll(); + } +} diff --git a/super_simon/src/main.h b/super_simon/src/main.h new file mode 100644 index 0000000..c4fbc1c --- /dev/null +++ b/super_simon/src/main.h @@ -0,0 +1,38 @@ +// +// Created by Gabriel Augendre on 29/04/2021. +// + +#ifndef SUPER_SIMON_MAIN_H +#define SUPER_SIMON_MAIN_H + +#define LCD_COLS 16 +#define LCD_ROWS 2 + +#define BUZZER 11 + +#define BUTTON_GREEN 2 +#define BUTTON_YELLOW 10 +#define BUTTON_BLUE 12 +#define BUTTON_RED 3 + +#define LED_GREEN A5 +#define LED_YELLOW A4 +#define LED_BLUE A3 +#define LED_RED A2 + +#define TONE_GREEN 165 +#define TONE_YELLOW 277 +#define TONE_BLUE 329 +#define TONE_RED 440 + +// Indices in the constants tables +#define GREEN 0 +#define YELLOW 1 +#define BLUE 2 +#define RED 3 + +const uint8_t LEDS[] = {LED_GREEN, LED_YELLOW, LED_BLUE, LED_RED}; +const uint8_t BUTTONS[] = {BUTTON_GREEN, BUTTON_YELLOW, BUTTON_BLUE, BUTTON_RED}; +const uint16_t TONES[] = {TONE_GREEN, TONE_YELLOW, TONE_BLUE, TONE_RED}; + +#endif //SUPER_SIMON_MAIN_H diff --git a/super_simon/src/utils.cpp b/super_simon/src/utils.cpp new file mode 100644 index 0000000..13e4951 --- /dev/null +++ b/super_simon/src/utils.cpp @@ -0,0 +1,33 @@ +// +// Created by Gabriel Augendre on 29/04/2021. +// + +#include +#include "main.h" +#include "utils.h" + + +void activate(byte index) { + for (const byte led : LEDS) { + if (led != index) { + digitalWrite(led, LOW); + } + } + digitalWrite(LEDS[index], HIGH); + buzz(index); +} + +void buzz(byte index, unsigned long duration) { + tone(BUZZER, TONES[index], duration); +} + +bool buttonIsPressed(byte index) { + return digitalRead(BUTTONS[index]) == LOW; +} + +void deactivateAll() { + noTone(BUZZER); + for (const byte led : LEDS) { + digitalWrite(led, LOW); + } +} diff --git a/super_simon/src/utils.h b/super_simon/src/utils.h new file mode 100644 index 0000000..b6db2cf --- /dev/null +++ b/super_simon/src/utils.h @@ -0,0 +1,13 @@ +// +// Created by Gabriel Augendre on 29/04/2021. +// + +#ifndef SUPER_SIMON_UTILS_H +#define SUPER_SIMON_UTILS_H + +void activate(byte index); +void buzz(byte index, unsigned long duration = 0); +bool buttonIsPressed(byte index); +void deactivateAll(); + +#endif //SUPER_SIMON_UTILS_H