Extract utilities functions and add a test loop

This commit is contained in:
Gabriel Augendre 2021-04-29 17:15:49 +02:00
parent de305307d5
commit ac6fcae9b5
4 changed files with 106 additions and 29 deletions

View file

@ -1,29 +1,8 @@
#include <Arduino.h>
#include <LiquidCrystal.h>
#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() {
}
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();
}
}

38
super_simon/src/main.h Normal file
View file

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

33
super_simon/src/utils.cpp Normal file
View file

@ -0,0 +1,33 @@
//
// Created by Gabriel Augendre on 29/04/2021.
//
#include <Arduino.h>
#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);
}
}

13
super_simon/src/utils.h Normal file
View file

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