#include "FastLED.h"
#include "Target.h"
#include "LedDisplay.h"
//Target Led definitions
#define LED_PIN 5
#define LED_COUNT 40
#define LED_TYPE WS2812
#define LED_GLOBAL_BRIGHTNESS 255
#define LED_CORRECTION TypicalLEDStrip
CRGB leds[LED_COUNT];
//Result Led definitions
#define DLED_PIN 12
#define DLED_COUNT 174 //4 digits = 4*42 = 168 + 6 in the middle = 174
#define DLED_TYPE WS2812
#define DLED_GLOBAL_BRIGHTNESS 255
#define DLED_CORRECTION TypicalLEDStrip
CRGB dleds[DLED_COUNT];
bool gameOver = false;
//States declaration
enum class targetStateOLD : uint8_t {
INACTIVE,
ACTIVE,
SHOT
};
//Target setup # pin minLed maxLed
Target t_1( 1, 34, 0, 4 );
Target t_2( 2, 35, 5, 9 );
Target t_3( 3, 32, 10, 14 );
Target t_4( 4, 33, 15, 19 );
Target t_5( 5, 25, 20, 24 );
Target t_6( 6, 26, 25, 29 );
Target t_7( 7, 27, 30, 34 );
Target t_8( 8, 14, 35, 39 );
//Create the 4digits led display
LedDisplay LD(DLED_COUNT);
//
void setup() {
Serial.begin(9600);
//Strip initialization
FastLED.addLeds<LED_TYPE, LED_PIN>(leds, LED_COUNT);
FastLED.addLeds<DLED_TYPE, DLED_PIN>(dleds, DLED_COUNT);
FastLED.setBrightness(LED_GLOBAL_BRIGHTNESS);
FastLED.setCorrection(LED_CORRECTION);
//initialize all leds to off
leds_off();
//initialize all leds to off
dleds_off();
//Target init
t_1.hue = 0;
t_2.hue = 30;
t_3.hue = 60;
t_4.hue = 90;
t_5.hue = 120;
t_6.hue = 150;
t_7.hue = 180;
t_8.hue = 210;
//Display
FastLED.show();
//Initial random time to wait before the timer static_assert
Serial.println(F("Standby......."));
delay(random(1500,5000));
Serial.println(F("Biiiiiipp !"));
}
void loop() {
//rainbow code
//for(int i=0; i < LED_COUNT; i++){
// leds[i] = CHSV(hue+10*i, saturation, value);
//}
//EVERY_N_MILLISECONDS(7) {
// hue++;
// }
static unsigned long timer = millis();
static uint8_t ld_hue = 0;
//Process the target states
t_1.process();
t_2.process();
t_3.process();
t_4.process();
t_5.process();
t_6.process();
t_7.process();
t_8.process();
//Update led values for each target
leds_update();
//Update led values for the led display
dleds_update();
//display
FastLED.show();
if (countActive() == 0 && !gameOver) {
//stop the timer
double result = millis() - timer;
result = round(result/10);
result = result/100;
//Display result = amount of time necessary to shot all targets
Serial.println(F(""));
Serial.println(F("*************************************"));
Serial.println(F(" Game over ! "));
Serial.println(F("*************************************"));
Serial.println(F(""));
Serial.print (F("Score : "));
Serial.print (result);
Serial.println(F("s"));
LD.setTimer(result,ld_hue,255,255); //set the result on the display
LD.rainbow(); //reset the hue of each line to rainbow
gameOver = true;
}
else if (!gameOver) {
//uint16_t resultx = ((millis() - timer)/1U)%10;
//ld_hue = ld_hue + 10;
//test
//LD.setCounter(1234, ld_hue, 255, 255);
}
else if (gameOver) {
//if game is over, slide the rainbow of the result displayed
EVERY_N_MILLISECONDS(5) {
//ld_hue++;
//LD.hue(ld_hue);
LD.huePlus(1); //slide each hue value from 1 points
}
}
}
uint8_t countActive() {
uint8_t currCount = 0;
if (t_1.currState == targetState::ACTIVE) { currCount++;}
if (t_2.currState == targetState::ACTIVE) { currCount++;}
if (t_3.currState == targetState::ACTIVE) { currCount++;}
if (t_4.currState == targetState::ACTIVE) { currCount++;}
if (t_5.currState == targetState::ACTIVE) { currCount++;}
if (t_6.currState == targetState::ACTIVE) { currCount++;}
if (t_7.currState == targetState::ACTIVE) { currCount++;}
if (t_8.currState == targetState::ACTIVE) { currCount++;}
return currCount;
}
//Sets the whole leds array values to HSV = 0,0,0
void leds_off(){
//update the FastLED array to off
for (uint16_t i = 0; i < LED_COUNT; i++) {
leds[i] = CHSV(0, 0, 0);
}
//update the target values to off
t_1.value = 0;
t_2.value = 0;
t_3.value = 0;
t_4.value = 0;
t_5.value = 0;
t_6.value = 0;
t_7.value = 0;
t_8.value = 0;
}
//Sets the whole dleds values to HSV = 0,0,0
void dleds_off(){
//update the FastLED values to off
for (uint16_t i = 0; i < DLED_COUNT; i++) {
dleds[i] = CHSV(0, 0, 0);
}
//update the led display array values to off
LD.off();
}
//Updates values of the target's leds from the led array
void leds_update(){
{
for (int i = t_1.minLed ; i < t_1.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_1.hue, t_1.saturation, t_1.value);
}
for (int i = t_2.minLed ; i < t_2.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_2.hue, t_2.saturation, t_2.value);
}
for (int i = t_3.minLed ; i < t_3.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_3.hue, t_3.saturation, t_3.value);
}
for (int i = t_4.minLed ; i < t_4.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_4.hue, t_4.saturation, t_4.value);
}
for (int i = t_5.minLed ; i < t_5.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_5.hue, t_5.saturation, t_5.value);
}
for (int i = t_6.minLed ; i < t_6.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_6.hue, t_6.saturation, t_6.value);
}
for (int i = t_7.minLed ; i < t_7.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_7.hue, t_7.saturation, t_7.value);
}
for (int i = t_8.minLed ; i < t_8.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_8.hue, t_8.saturation, t_8.value);
}
}
}
//Updates values of the led display from the led array
void dleds_update(){
for (uint16_t i = 0; i<DLED_COUNT ; i++){
dleds[i] = CHSV(LD.dled[i][0], LD.dled[i][1], LD.dled[i][2]);
}
}