Commit 6d98a963 authored by Ondřej Kučera's avatar Ondřej Kučera
Browse files

add oktkas FW

parent 4b2d0b5c
// oktkasův stromeček
// @oktkas
#include "timer.h"
#define l1 8
#define l2 7
#define l3 6
#define l4 5
#define l5 4
#define l6 15
#define l7 14
#define l8 13
#define l9 12
#define l0 16
#define btn1 2
#define btn2 3
#define R 9
#define G 10
#define B 11
int l[10] = {8, 7, 6, 5, 4, 15, 14, 13, 12, 16};
bool randomValues[10][4] = {
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
};
//kdo driv
bool kdEN = false;
bool kdLED = 0;
bool kdp1 = 0;
bool kdp2 = 0;
bool kdGO = 1; //game over
int player1 = 0; //score
int player2 = 0; //score
Timer timer;
Timer timerGL;
unsigned long fcPlayer1last = 0;
unsigned long fcPlayer2last = 0;
unsigned long fcPlayer1 = 0;
unsigned long fcPlayer2 = 0;
bool fcEN = false;
int fcSLOW = 1000;
int fcFAST = 150;
int fcWINtime = 1000;
int timeoutRB = 900;
int timeoutRC = timeoutRB*3;
int counterRB = 0;
int counterRC = 0;
int loadingIndex = 0;
bool loadingDir = true;
void setup() {
pinMode(R, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
// set-up outputs, turn everything off
int len = sizeof(l)/sizeof(l[0]);
for (int i=0; i < len; i++) {
pinMode(l[i], OUTPUT);
digitalWrite(l[i], HIGH);
}
// setup random numbers generator
pinMode(A7, INPUT);
randomSeed(analogRead(20));
// tie games' start functions to button presses
attachInterrupt(digitalPinToInterrupt(btn1), kdStart, FALLING);
attachInterrupt(digitalPinToInterrupt(btn2), fcStart, FALLING);
startup(); // play the startup animation
}
//------LOOP----------------------------------------------------------------
void loop() {
// random change
timer.setTimeout(random(50, 90));
timer.setCallback(change);
timer.start();
counterRC = 0;
while(true) {
counterRC++;
randomChange();
if (counterRC > timeoutRC | kdEN | fcEN){
break;
}
}
//falling
for (int i = 0; i < 20; i++) {
if (kdEN | fcEN){
break;
}
int _r = 0;
int _g = 0;
int _b = 0;
while (!(_r | _g | _b)) { //get at least one "1"
_r = random(0, 2);
_g = random(0, 2);
_b = random(0, 2);
}
falling(_r, _g, _b);
}
// random blink
counterRB = 0;
while(true) {
counterRB++;
randomBlink();
if (counterRB > timeoutRB | kdEN | fcEN){
break;
}
}
// KDO DRIV GAME
if (kdEN) {
detachInterrupt(digitalPinToInterrupt(btn1));
detachInterrupt(digitalPinToInterrupt(btn2));
// all LEDS off
int len = sizeof(l)/sizeof(l[0]);
for (int i=0; i < len; i++) {
digitalWrite(l[i], HIGH);
}
delay(2000);
kdoDriv();
kdEN = false;
attachInterrupt(digitalPinToInterrupt(btn1), kdStart, FALLING);
attachInterrupt(digitalPinToInterrupt(btn2), fcStart, FALLING);
}
// FAST CLICKER GAME
if (fcEN) {
detachInterrupt(digitalPinToInterrupt(btn1));
detachInterrupt(digitalPinToInterrupt(btn2));
// all LEDS off
int len = sizeof(l)/sizeof(l[0]);
for (int i=0; i < len; i++) {
digitalWrite(l[i], HIGH);
}
delay(2000);
fastClicker();
fcEN = false;
attachInterrupt(digitalPinToInterrupt(btn1), kdStart, FALLING);
attachInterrupt(digitalPinToInterrupt(btn2), fcStart, FALLING);
}
}
//-------RANDOM-BLINK-------------------------------------------------------
void randomBlink() {
int _led = random(0, sizeof(l)/sizeof(l[0]));
setLED(l[_led], random(0, 2), random(0, 2), random(0, 2), random(0, 2));
delay(random(50, 90));
}
//-------RANDOM-CHANGE-------------------------------------------------------
void randomChange() {
int len = sizeof(l)/sizeof(l[0]);
for (int i = 0; i < len; i++) {
setLED(l[i], randomValues[i][0], randomValues[i][1], randomValues[i][2], randomValues[i][3]);
}
timer.update();
}
void change() {
// set a random LED to a random state
int _led = random(0, sizeof(l)/sizeof(l[0]));
randomValues[_led][0] = random(0, 2);
randomValues[_led][1] = random(0, 2);
randomValues[_led][2] = random(0, 2);
randomValues[_led][3] = random(0, 2);
timer.setTimeout(random(10, 30));
timer.setCallback(change);
timer.start();
}
//-------FAST-CLICKER-------------------------------------------------------
void fcStart() {
fcEN = true;
}
// on button interrupt
void fcplayer1() {
fcPlayer1 = millis() - fcPlayer1last; //get time between clicks
fcPlayer1last = millis(); // save time of current click
}
// on button interrupt
void fcplayer2() {
fcPlayer2 = millis() - fcPlayer2last; //get time between clicks
fcPlayer2last = millis(); // save time of current click
}
void fastClicker() {
attachInterrupt(digitalPinToInterrupt(btn1), fcplayer1, FALLING);
attachInterrupt(digitalPinToInterrupt(btn2), fcplayer2, FALLING);
int winner = 0;
fcPlayer1 = fcSLOW;
fcPlayer2 = fcSLOW;
unsigned long winnerChange = 0;
while (true) {
//get time between clicks
if (fcPlayer1 < millis() - fcPlayer1last) {
fcPlayer1 = millis() - fcPlayer1last;
}
if (fcPlayer2 < millis() - fcPlayer2last) {
fcPlayer2 = millis() - fcPlayer2last;
}
// map time to score
if (fcPlayer1 > fcSLOW) { fcPlayer1 = fcSLOW;}
if (fcPlayer2 > fcSLOW) { fcPlayer2 = fcSLOW;}
int _player1 = map(fcPlayer1, fcSLOW, fcFAST, -1, 5);
int _player2 = map(fcPlayer2, fcSLOW, fcFAST, -1, 5);
// set the current winner if they surpass the fsFAST limit
if (fcPlayer2 < fcPlayer1 & fcPlayer2 < fcFAST) {
_player2 = 5;
if (winner == 0 | winner == 1) {
winner = 2;
winnerChange = millis();
}
}
if (fcPlayer1 < fcPlayer2 & fcPlayer1 < fcFAST) {
_player1 = 5;
if (winner == 0 | winner == 2) {
winner = 1;
winnerChange = millis();
}
}
// set the current winner to 0 if neither of the players are faster than fcFAST
if (fcPlayer1 > fcFAST & fcPlayer2 > fcFAST) {
winner = 0;
winnerChange = millis();
}
/*
if (winner == 2) {
setLED(l0, 1, 0, 0, 1);
} else if (winner == 1) {
setLED(l0, 1, 0, 1, 0);
} else {
setLED(l0, 0, 0, 0, 0);
}
*/
if (winner != 0 & (millis() - winnerChange > fcWINtime)) {
if (winner == 2) {
blinkScore(1, _player2); // blink player's score
blinkScore(1, _player2); // blink player's score
blinkScore(1, _player2); // blink player's score
blinkScore(1, _player2); // blink player's score
} else {
blinkScore(0, _player1); // blink player's score
blinkScore(0, _player1); // blink player's score
blinkScore(0, _player1); // blink player's score
blinkScore(0, _player1); // blink player's score
}
break;
}
displayScore(_player1, _player2);
}
detachInterrupt(digitalPinToInterrupt(btn1));
detachInterrupt(digitalPinToInterrupt(btn2));
}
//-------KDO-DRIV-----------------------------------------------------------
void kdStart() {
kdEN = true;
}
void kdoDriv() {
attachInterrupt(digitalPinToInterrupt(btn1), kdplayer1, FALLING);
attachInterrupt(digitalPinToInterrupt(btn2), kdplayer2, FALLING);
player1 = 0;
player2 = 0;
while (player1 < 5 & player2 < 5) {
displayScore(player1, player2);
timer.update();
//start the game
if (kdGO) {
kdGO = 0;
kdp1 = 0;
kdp2 = 0;
setLED(l0, 0, 0, 0, 0);
kdLED = 0;
timer.setTimeout(random(300, 3000));
timer.setCallback(kdl);
timer.start();
}
}
if (player1 >= 5) {
setLED(l0, 0, 0, 0, 0);
kdGO = 1;
kdp1 = 0;
kdp2 = 0;
blinkScore(0, player1); // blink player's score
blinkScore(0, player1); // blink player's score
blinkScore(0, player1); // blink player's score
blinkScore(0, player1); // blink player's score
}
if (player2 >= 5) {
setLED(l0, 0, 0, 0, 0);
kdGO = 1;
kdp1 = 0;
kdp2 = 0;
blinkScore(1, player2); // blink player's score
blinkScore(1, player2); // blink player's score
blinkScore(1, player2); // blink player's score
blinkScore(1, player2); // blink player's score
}
detachInterrupt(digitalPinToInterrupt(btn1));
detachInterrupt(digitalPinToInterrupt(btn2));
}
// on timer ring
void kdl() {
setLED(l0, 1, 1, 1, 1); // turn on main LED
kdLED = 1;
}
// on button interrupt
void kdplayer1() {
if (kdLED & !kdp2) {
kdp1 = 1;
blinkScore(0, player1); // blink player's score
player1++;
kdGO = 1;
}
}
// on button interrupt
void kdplayer2() {
if (kdLED & !kdp1) {
kdp2 = 1;
blinkScore(1, player2); // blink player's score
player2++;
kdGO = 1;
}
}
void displayScore(int _player1, int _player2) {
if (_player1 > 5) {_player1 = 5;}
if (_player2 > 5) {_player2 = 5;}
// all LEDs off (except "LED11")
for (int i = 0; i<9; i++) {
setLED(l[i], 0, 0, 0, 0);
}
for (int i = 0; i < _player1 ; i++) {
setLED(l[i], 1, 0, 1, 0);
}
for (int i = 8; i >= 9-_player2; i--) {
setLED(l[i], 1, 0, 0, 1);
}
if (kdLED == 1) {
setLED(l0, 1, 1, 1, 1); // turn on main LED
} else {
setLED(l0, 0, 0, 0, 0);
}
}
void blinkScore(bool _player, int _score) {
if (_score > 5) {_score = 5;}
int len = sizeof(l)/sizeof(l[0]);
if (_player) {
digitalWrite(R, LOW);
digitalWrite(G, LOW);
digitalWrite(B, HIGH);
// all LEDS off
len = sizeof(l)/sizeof(l[0]);
for (int i=0; i < len; i++) {
digitalWrite(l[i], HIGH);
}
for (int i = 8; i >= 9-_score; i--) {
digitalWrite(l[i], LOW);
}
myDelay(200);
for (int i = 8; i >= 9-_score; i--) {
digitalWrite(l[i], HIGH);
}
myDelay(200);
for (int i = 8; i >= 9-_score; i--) {
digitalWrite(l[i], LOW);
}
myDelay(200);
digitalWrite(R, LOW);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
// all LEDS off
int len = sizeof(l)/sizeof(l[0]);
for (int i=0; i < len; i++) {
digitalWrite(l[i], HIGH);
}
} else {
digitalWrite(R, LOW);
digitalWrite(G, HIGH);
digitalWrite(B, LOW);
// all LEDS off
int len = sizeof(l)/sizeof(l[0]);
for (int i=0; i < len; i++) {
digitalWrite(l[i], HIGH);
}
for (int i = 0; i < _score; i++) {
digitalWrite(l[i], LOW);
}
myDelay(200);
for (int i = 0; i < _score; i++) {
digitalWrite(l[i], HIGH);
}
myDelay(200);
for (int i = 0; i < _score; i++) {
digitalWrite(l[i], LOW);
}
myDelay(200);
digitalWrite(R, LOW);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
// all LEDS off
for (int i=0; i < len; i++) {
digitalWrite(l[i], HIGH);
}
}
}
//-------COMMON-------------------------------------------------------------
void startup() {
for (int i = 0; i < 9; i++) {
setLED(l[i], 1, random(0,2), random(0,2), random(0,2));
delay(50);
}
for (int i = 8; i >= 0; i--) {
setLED(l[i], 1, random(0,2), random(0,2), random(0,2));
delay(50);
}
for (int i = 0; i < 9; i++) {
setLED(l[i], 1, random(0,2), random(0,2), random(0,2));
delay(50);
}
for (int i = 8; i >= 0; i--) {
setLED(l[i], 1, random(0,2), random(0,2), random(0,2));
delay(50);
}
/*
for (int i = 0; i < 9; i++) {
digitalWrite(R, HIGH);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
digitalWrite(l[i], LOW);
}
delay(100);
for (int i = 0; i < 9; i++) {
digitalWrite(l[i], HIGH);
}
delay(100);
for (int i = 0; i < 9; i++) {
digitalWrite(R, LOW);
digitalWrite(G, HIGH);
digitalWrite(B, LOW);
digitalWrite(l[i], LOW);
}
delay(100);
for (int i = 0; i < 9; i++) {
digitalWrite(l[i], HIGH);
}
delay(100);
for (int i = 0; i < 9; i++) {
digitalWrite(R, LOW);
digitalWrite(G, LOW);
digitalWrite(B, HIGH);
digitalWrite(l[i], LOW);
}
delay(100);
for (int i = 0; i < 9; i++) {
digitalWrite(l[i], HIGH);
}
delay(100);
*/
}
void falling(bool _r, bool _g, bool _b) {
int _delay = 50;
digitalWrite(R, _r);
digitalWrite(G, _g);
digitalWrite(B, _b);
digitalWrite(l[4], LOW);
delay(_delay);
for (int i = 5; i >= 0; i--) {
digitalWrite(l[i], LOW);
digitalWrite(l[8-i], LOW);
delay(_delay);
}
digitalWrite(l[4], HIGH);
delay(_delay);
for (int i = 5; i >= 0; i--) {
digitalWrite(l[i], HIGH);
digitalWrite(l[8-i], HIGH);
delay(_delay);
}
digitalWrite(R, 0);
digitalWrite(G, 0);
digitalWrite(B, 0);
}
// ( <LEDnum> , <ON/OFF> , R , G , B )
void setLED(int _led, bool _state, bool _r, bool _g, bool _b) {
// all LEDS off
int len = sizeof(l)/sizeof(l[0]);
for (int i=0; i < len; i++) {
digitalWrite(l[i], HIGH);
}
// set color
digitalWrite(R, _r);
digitalWrite(G, _g);
digitalWrite(B, _b);
// turn the one LED
if (_state) {
digitalWrite(_led, LOW);
} else {
digitalWrite(_led, HIGH);
}
//wait a little
delay(1);
}
void myDelay(int _milliseconds) {
for (int i = 0; i<_milliseconds; i++) {
delayMicroseconds(1000);
}
}
#include "timer.h"
#include "timerManager.h"
Timer::Timer() {
initial_time = 0;
current_time = 0;
interval = 0;
last_interval_time = 0;
repeat_count = -1;
is_running = false;
is_paused = false;
interval_is_setted = false;
function_callback = NULL;
TimerManager::instance().add(this);
}
Timer::~Timer() {
TimerManager::instance().remove(this);
}
void Timer::start() {
if(isPaused()) {
int paused_time = millis() - current_time;
current_time = millis();
initial_time += paused_time;
last_interval_time += paused_time;
} else {
reset();
repeat_count = total_repeat_count;
}
is_running = true;
is_paused = false;
}