added asynchronous timer

This commit is contained in:
Sean 2022-04-06 21:35:35 -04:00
parent 53879d6546
commit 62666ebb8c
6 changed files with 608 additions and 505 deletions

View file

@ -1,20 +1,16 @@
// WIP. 3/28/2022: added test state machine. Need to re-write moving to next state // WIP. 4/4/2022: started to implement delay timer asyncronously using existing timer2 interrupt.
#include "config.h" #include "config.h"
#include "cubeplex.h" #include "cubeplex.h"
#include <timer.h> #include <timer.h>
Timer animationDelay;
bool animationDelayTiming = false;
int color = red; int color = red;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial.println("start program..."); Serial.println("start program...");
initCube(); initCube();
timerInit(); //timerInit();
currentState = CHASETHEDOT; currentState = CHASETHEDOT;
lastState = -1; lastState = -1;
@ -29,13 +25,14 @@ void loop() {
case CHASETHEDOT: case CHASETHEDOT:
chaseTheDot(); chaseTheDot();
//currentState = PLANARFLOP3D; break;
case RANDOMRAINBOW:
rainbow_random();
break; break;
case PLANARFLOP3D: case PLANARFLOP3D:
planarFlop3D(); planarFlop3D();
//currentState = CHASETHEDOT;
break; break;
} }
animationDelay.update();
} }

View file

@ -4,6 +4,8 @@
| | | |
| Inspired By: Jonah Glick | | Inspired By: Jonah Glick |
| Written By: Asher Glick | | Written By: Asher Glick |
| |
| Modified By: S. Dugre to use asynchronous timer |
\******************************************************************************/ \******************************************************************************/
int xpos = 0; int xpos = 0;
@ -13,21 +15,23 @@ int zpos = 0;
int animationSpeed = 100; int animationSpeed = 100;
void chaseTheDot() { void chaseTheDot() {
//continuePattern = true;
if (currentState != lastState) { if (currentState != lastState) {
lastState = currentState; lastState = currentState;
Serial.print("last State = "); Serial.print("New State = ");
Serial.println(lastState); Serial.println(currentState);
xpos = 0; xpos = 0;
ypos = 0; ypos = 0;
zpos = 0; zpos = 0;
currentTimer = 0;
maxTimer = 6250; // 100 ms
} }
//while (continuePattern) { //while (continuePattern) {
if ( not animationDelayTiming) { if ( timerReset) {
switch (random(0, 6)) { switch (random(0, 6)) {
case 0: case 0:
if (xpos > 0) { if (xpos > 0) {
@ -75,8 +79,12 @@ void chaseTheDot() {
flushBuffer(); flushBuffer();
clearBuffer(); clearBuffer();
//delay(animationSpeed); //delay(animationSpeed);
animationDelay.start();
animationDelayTiming = true; //animationDelay.start();
Serial.println("start timer"); //animationDelayTiming = true;
//Serial.println("start timer");
currentTimer = 0;
timerReset = false;
} }
} }

View file

@ -3,7 +3,10 @@
typedef enum STATE { typedef enum STATE {
CHASETHEDOT, CHASETHEDOT,
RANDOMRAINBOW,
PLANARFLOP3D PLANARFLOP3D
}; };
STATE currentState, lastState; STATE currentState, lastState;
bool timerReset = true;

View file

@ -113,9 +113,15 @@ void clearBuffer () {
| the secondary colors purple, teal, yellow (every pairing of two leds) | | the secondary colors purple, teal, yellow (every pairing of two leds) |
| the ALL THE COLORS (red,green,blue,purple,teal,yellow,white) | | the ALL THE COLORS (red,green,blue,purple,teal,yellow,white) |
\******************************************************************************/ \******************************************************************************/
int nextPrimaryColor(int color) { return (color+1)%3; } int nextPrimaryColor(int color) {
int nextSecondaryColor(int color) { return ((color+1)%3)+3; } return (color + 1) % 3;
int nextColor(int color) { return (color+1)%7; } }
int nextSecondaryColor(int color) {
return ((color + 1) % 3) + 3;
}
int nextColor(int color) {
return (color + 1) % 7;
}
/********************************** SWAP INT **********************************\ /********************************** SWAP INT **********************************\
| This function uses the fast xor swap to change the values of the two | | This function uses the fast xor swap to change the values of the two |
@ -288,9 +294,18 @@ void drawLine(int color, int brightness, int startx, int starty, int startz, int
bool reverseX = false; bool reverseX = false;
bool reverseY = false; bool reverseY = false;
bool reverseZ = false; bool reverseZ = false;
if (startx > endx) {swapint(startx,endx);reverseX=true;} if (startx > endx) {
if (starty > endy) {swapint(starty,endy);reverseY=true;} swapint(startx, endx);
if (startz > endz) {swapint(startz,endz);reverseZ=true;} reverseX = true;
}
if (starty > endy) {
swapint(starty, endy);
reverseY = true;
}
if (startz > endz) {
swapint(startz, endz);
reverseZ = true;
}
int delx = endx - startx; int delx = endx - startx;
int dely = endy - starty; int dely = endy - starty;
@ -582,14 +597,25 @@ uint8_t DebouncePin(uint8_t pin) {
return debounced_state; return debounced_state;
} }
int currentTimer = 0;
int maxTimer = 6250; // 100ms delay / 16uS interrupt time
//int nextState(int state) { return (state+1)%6; } int nextState(int state) { return (state+1)%2; }
// the interrupt function to display the leds // the interrupt function to display the leds
ISR(TIMER2_OVF_vect) { ISR(TIMER2_OVF_vect) {
if(DebouncePin(18)) { if (not timerReset){
//nextState(currentState); currentTimer++;
if (currentTimer >= maxTimer) {
timerReset = true;
currentTimer = 0;
} }
}
if (DebouncePin(18)) {
currentState = nextState(currentState);
}
int pin1 = _cube_current_frame->pin1; int pin1 = _cube_current_frame->pin1;
int pin2 = _cube_current_frame->pin2; int pin2 = _cube_current_frame->pin2;
int count = (pin1 & 0xF0) | ((pin2 & 0xF0) >> 4); int count = (pin1 & 0xF0) | ((pin2 & 0xF0) >> 4);
@ -634,8 +660,8 @@ int animationMax = 0;
ISR(TIMER1_OVF_vect) { ISR(TIMER1_OVF_vect) {
animationTimer++; animationTimer++;
if (animationTimer >= animationMax) { if (animationTimer >= animationMax) {
continuePattern = false; //continuePattern = false;
//nextState(currentState); currentState = nextState(currentState);
animationTimer = 0; animationTimer = 0;
} }
} }

View file

@ -1,91 +1,159 @@
/******************************* PLANAR FLOP 3D *******************************\ /******************************* PLANAR FLOP 3D *******************************\
| Version 2 of the planar flop, doing more complicated maths and 3D vectors | | Version 2 of the planar flop, doing more complicated maths and 3D vectors |
| 'n stuff. Making this function found the bug of reversed z axis line drawing | | 'n stuff. Making this function found the bug of reversed z axis line drawing |
| |
| Modified By: S. Dugre to use asynchronous timer |
\******************************************************************************/ \******************************************************************************/
void planarFlop3D() { int planarCase;
continuePattern = true;
int animationSpeed = 50;
while (continuePattern) {
void planarFlop3D() {
if (currentState != lastState) {
lastState = currentState;
Serial.print("New State = ");
Serial.println(currentState);
currentTimer = 0;
maxTimer = 3125; // 50 ms
planarCase = 0;
}
if (timerReset) {
switch (planarCase) {
case 0:
for (int y = 3; y > 0; y--) { for (int y = 3; y > 0; y--) {
for (int z = 0; z < 4; z++) drawLine(color, 0, 3, z, 3, y, z); for (int z = 0; z < 4; z++) drawLine(color, 0, 3, z, 3, y, z);
flushBuffer(); flushBuffer();
clearBuffer(); clearBuffer();
delay(animationSpeed); //delay(animationSpeed);
planarCase++;
currentTimer = 0;
timerReset = false;
} }
case 1:
for (int x = 3; x > 0; x--) { for (int x = 3; x > 0; x--) {
for (int z = 0; z < 4; z++) drawLine(color, 0, 3, z, x, 0, z); for (int z = 0; z < 4; z++) drawLine(color, 0, 3, z, x, 0, z);
flushBuffer(); flushBuffer();
clearBuffer(); clearBuffer();
delay(animationSpeed); //delay(animationSpeed);
planarCase++;
currentTimer = 0;
timerReset = false;
} }
case 2:
for (int x = 0; x < 3; x++) { for (int x = 0; x < 3; x++) {
for (int y = 0; y < 4; y++) drawLine(color, 0, y, 0, x, y, 3); for (int y = 0; y < 4; y++) drawLine(color, 0, y, 0, x, y, 3);
flushBuffer(); flushBuffer();
clearBuffer(); clearBuffer();
delay(animationSpeed); //delay(animationSpeed);
planarCase++;
currentTimer = 0;
timerReset = false;
} }
case 3:
for (int z = 3; z > 0; z--) { for (int z = 3; z > 0; z--) {
for (int y = 0; y < 4; y++) drawLine(color, 0, y, 0, 3, y, z); for (int y = 0; y < 4; y++) drawLine(color, 0, y, 0, 3, y, z);
flushBuffer(); flushBuffer();
clearBuffer(); clearBuffer();
delay(animationSpeed); //delay(animationSpeed);
planarCase++;
currentTimer = 0;
timerReset = false;
} }
case 4:
for (int z = 0; z < 3; z++) { for (int z = 0; z < 3; z++) {
for (int x = 0; x < 4; x++) drawLine(color, x, 0, 0, x, 3, z); for (int x = 0; x < 4; x++) drawLine(color, x, 0, 0, x, 3, z);
flushBuffer(); flushBuffer();
clearBuffer(); clearBuffer();
delay(animationSpeed); //delay(animationSpeed);
planarCase++;
currentTimer = 0;
timerReset = false;
} }
case 5:
for (int y = 3; y > 0; y--) { for (int y = 3; y > 0; y--) {
for (int x = 0; x < 4; x++) drawLine(color, x, 0, 0, x, y, 3); for (int x = 0; x < 4; x++) drawLine(color, x, 0, 0, x, y, 3);
flushBuffer(); flushBuffer();
clearBuffer(); clearBuffer();
delay(animationSpeed); //delay(animationSpeed);
planarCase++;
currentTimer = 0;
timerReset = false;
} }
case 6:
for (int y = 0; y < 3; y++) { for (int y = 0; y < 3; y++) {
for (int z = 0; z < 4; z++) drawLine(color, 3, 0, z, 0, y, z); for (int z = 0; z < 4; z++) drawLine(color, 3, 0, z, 0, y, z);
flushBuffer(); flushBuffer();
clearBuffer(); clearBuffer();
delay(animationSpeed); //delay(animationSpeed);
planarCase++;
currentTimer = 0;
timerReset = false;
} }
case 7:
for (int x = 0; x < 3; x++) { for (int x = 0; x < 3; x++) {
for (int z = 0; z < 4; z++) drawLine(color, 3, 0, z, x, 3, z); for (int z = 0; z < 4; z++) drawLine(color, 3, 0, z, x, 3, z);
flushBuffer(); flushBuffer();
clearBuffer(); clearBuffer();
delay(animationSpeed); //delay(animationSpeed);
planarCase++;
currentTimer = 0;
timerReset = false;
} }
case 8:
for (int x = 3; x > 0; x--) { for (int x = 3; x > 0; x--) {
for (int y = 0; y < 4; y++) drawLine(color, 3, y, 3, x, y, 0); for (int y = 0; y < 4; y++) drawLine(color, 3, y, 3, x, y, 0);
flushBuffer(); flushBuffer();
clearBuffer(); clearBuffer();
delay(animationSpeed); //delay(animationSpeed);
planarCase++;
currentTimer = 0;
timerReset = false;
} }
case 9:
for (int z = 0; z < 3; z++) { for (int z = 0; z < 3; z++) {
for (int y = 0; y < 4; y++) drawLine(color, 3, y, 3, 0, y, z); for (int y = 0; y < 4; y++) drawLine(color, 3, y, 3, 0, y, z);
flushBuffer(); flushBuffer();
clearBuffer(); clearBuffer();
delay(animationSpeed); //delay(animationSpeed);
planarCase++;
currentTimer = 0;
timerReset = false;
} }
case 10:
for (int z = 3; z > 0; z--) { for (int z = 3; z > 0; z--) {
for (int x = 0; x < 4; x++) drawLine(color, x, 3, 3, x, 0, z); for (int x = 0; x < 4; x++) drawLine(color, x, 3, 3, x, 0, z);
flushBuffer(); flushBuffer();
clearBuffer(); clearBuffer();
delay(animationSpeed); //delay(animationSpeed);
planarCase++;
currentTimer = 0;
timerReset = false;
} }
case 11:
for (int y = 0; y < 3; y++) { for (int y = 0; y < 3; y++) {
for (int x = 0; x < 4; x++) drawLine(color, x, 3, 3, x, y, 0); for (int x = 0; x < 4; x++) drawLine(color, x, 3, 3, x, y, 0);
flushBuffer(); flushBuffer();
clearBuffer(); clearBuffer();
delay(animationSpeed); //delay(animationSpeed);
planarCase = 0;
currentTimer = 0;
timerReset = false;
} }
color = nextColor(color); color = nextColor(color);
} }
} }
}

View file

@ -1,5 +1,5 @@
/*
void timerInit() { void timerInit() {
animationDelay.setTimeout(100); animationDelay.setTimeout(100);
animationDelay.setCallback(animationDelayCallback); animationDelay.setCallback(animationDelayCallback);
@ -9,3 +9,4 @@ void animationDelayCallback() {
animationDelayTiming = false; animationDelayTiming = false;
//Serial.println("timed out"); //Serial.println("timed out");
} }
*/