Skip to content

Commit

Permalink
Merge pull request #47 from jellewie/ANI_PHYSICS
Browse files Browse the repository at this point in the history
Ani physics
  • Loading branch information
jellewie authored May 21, 2021
2 parents 347f330 + c603673 commit 73bb048
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
43 changes: 41 additions & 2 deletions Arduino/Animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Arduino.ino: Add the caller to 'switch (Mode) {' as a new case 'case ###: if (LastMode != Mode) StartAnimation(xxx, -2); break;'. where ### is the enum name and xxx ths ID in the 'switch (CurrentAnimation)'
*/
byte CurrentAnimation; //Which AnimationCounter Animation is selected
byte TotalAnimations = 12;
byte TotalAnimations = 13;
CRGB AnimationRGB = {0, 0, 0};

//==================================================
Expand Down Expand Up @@ -91,6 +91,7 @@ void ShowAnimation(bool Start) { //This would be
UpdateLEDs = true;
} break;
case 2: { //CONFETTI
if (Start) ClockClear();
#define ANIMATION_TIME_CONFETTI 1000 / 60
EVERY_N_MILLISECONDS(ANIMATION_TIME_CONFETTI) { //Limit to x FPS
fadeToBlackBy(LEDs, TotalLEDsClock, 1); //Dim a color by (X/256ths)
Expand All @@ -102,12 +103,14 @@ void ShowAnimation(bool Start) { //This would be
}
} break;
case 3: { //FLASH
if (Start) ClockClear();
EVERY_N_MILLISECONDS(500) {
LED_Flash(0, TotalLEDsClock, AnimationRGB);
UpdateLEDs = true;
}
} break;
case 4: { //GLITTER
if (Start) ClockClear();
#define ANIMATION_TIME_GLITTER 1000 / 60
EVERY_N_MILLISECONDS(ANIMATION_TIME_GLITTER) { //Limit to x FPS
fadeToBlackBy(LEDs, TotalLEDsClock, 1); //Dim a color by (X/256ths)
Expand All @@ -117,6 +120,7 @@ void ShowAnimation(bool Start) { //This would be
}
} break;
case 5: { //JUGGLE
if (Start) ClockClear();
#define ANIMATION_TIME_JUGGLE 1000 / 120
EVERY_N_MILLISECONDS(ANIMATION_TIME_JUGGLE) { //Limit to x FPS
fadeToBlackBy(LEDs, TotalLEDsClock, 10);
Expand Down Expand Up @@ -149,13 +153,15 @@ void ShowAnimation(bool Start) { //This would be
}
} break;
case 8: { //SINELON
if (Start) ClockClear();
#define ANIMATION_TIME_SINELON 1000 / 120
EVERY_N_MILLISECONDS(ANIMATION_TIME_SINELON) { //Limit to x FPS
AnimationSinelon(AnimationRGB, 5, Start, 13);
UpdateLEDs = true;
}
} break;
case 9: { //SINELON2
if (Start) ClockClear();
#define ANIMATION_TIME_SINELON2 1000 / 120
EVERY_N_MILLISECONDS(ANIMATION_TIME_SINELON2) { //Limit to x FPS
if (AnimationSinelon(AnimationRGB, 1, Start, 13))
Expand All @@ -168,6 +174,7 @@ void ShowAnimation(bool Start) { //This would be
static byte BlinkCounter;
static byte BlinkEachxLoops = 20;
if (Start) {
ClockClear();
BlinkLeft = random8(0, 2);
BlinkCounter = 0;
BlinkEachxLoops = random8(20, 50);
Expand Down Expand Up @@ -248,6 +255,39 @@ void ShowAnimation(bool Start) { //This would be
UpdateLEDs = true;
}
} break;
case 13: { //PHYSICS
#define Speed -0.004 //Lower = slower
#define Drag 0.995 //Lower = more slowdown per step
#define FallToLED TotalLEDsClock / 2
static CRGB Saved_Color[TotalLEDsClock];
static float Position[TotalLEDsClock];
static float Velocity[TotalLEDsClock];
EVERY_N_SECONDS(30) { //Just repeat is sometimes, to keep the standalone Mode intresting
for (int i = 0; i < TotalLEDsClock; i++) {
Position[i] = i;
Velocity[i] = 0;
}
}
if (Start) {
for (int i = 0; i < TotalLEDsClock; i++) {
Position[i] = i;
Velocity[i] = 0;
Saved_Color[i] = LEDs[LEDtoPosition(i)]; //Dont use memcpy since its about the same speed, but doesnt allow the offset
}
}
#define ANIMATION_TIME_PHYSICS 1000/60
EVERY_N_MILLISECONDS(ANIMATION_TIME_PHYSICS) {
ClockClear(); //Clear the LEDs so we start from a blank slate
for (int i = 0; i < TotalLEDsClock; i++) {
float Acceleration = Speed * (Position[i] - FallToLED);//Calculate howmuch we wish to move (just linear) https://www.desmos.com/calculator/ljo4mllyzq y=-\frac{1}{2}\left(x-b\right)
Velocity[i] = Velocity[i] * Drag + Acceleration; //Set the Velocity to be (the speed we shere add) * (Drag) + (howmuch we wish to move)
Position[i] = Position[i] + Velocity[i]; //Calculate new position
signed int _Pos = round(Position[i]);
LED_Add(LEDtoPosition(_Pos), 1, Saved_Color[i], TotalLEDsClock); //Draw the LED
}
UpdateLEDs = true;
}
} break;

default:
AnimationCounter = 0; //Stop animation
Expand All @@ -267,6 +307,5 @@ void StartAnimation(byte ID, int Time) {
#ifdef SerialEnabled
Serial.println("AN: Selected special mode " + String(CurrentAnimation));
#endif //SerialEnabled
FastLED.clear();
ShowAnimation(true);
}
2 changes: 2 additions & 0 deletions Arduino/Arduino.ino
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ void loopLEDS() {
case SMILEY: if (LastMode != Mode) StartAnimation(10, -2); break;
case FLASH2: if (LastMode != Mode) StartAnimation(11, -2); break;
case PACMAN: if (LastMode != Mode) StartAnimation(12, -2); break;
case PHYSICS: if (LastMode != Mode) StartAnimation(13, -2); break;

default:
#ifdef SerialEnabled
Serial.println("mode with ID " + String(Mode) + " not found");
Expand Down
4 changes: 2 additions & 2 deletions Arduino/Structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ struct TimeS {
unsigned long Ticks;
};
enum Modes {OFF, ON, WIFI, RESET, CLOCK,
BLINK, BPM, CONFETTI, FLASH, GLITTER, JUGGLE, MOVE, RAINBOW, SINELON, SINELON2, SMILEY, FLASH2, PACMAN
BLINK, BPM, CONFETTI, FLASH, GLITTER, JUGGLE, MOVE, RAINBOW, SINELON, SINELON2, SMILEY, FLASH2, PACMAN, PHYSICS
}; //Just to make the code more clear to read, OFF=0 and ON=1 etc
String ModesString[] = {"OFF", "ON", "WIFI", "RESET", "CLOCK",
"BLINK", "BPM", "CONFETTI", "FLASH", "GLITTER", "JUGGLE", "MOVE", "RAINBOW", "SINELON", "SINELON2", "SMILEY", "FLASH2", "PACMAN"
"BLINK", "BPM", "CONFETTI", "FLASH", "GLITTER", "JUGGLE", "MOVE", "RAINBOW", "SINELON", "SINELON2", "SMILEY", "FLASH2", "PACMAN", "PHYSICS"
}; //ALL CAPS!
const byte Modes_Amount = sizeof(ModesString) / sizeof(ModesString[0]);//Why filling this in if we can automate that? :)

Expand Down
12 changes: 9 additions & 3 deletions Arduino/handler.ino
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,14 @@ void handle_Set() {
#ifdef Server_SerialEnabled
Serial.println();
#endif //Server_SerialEnabled
if (LastMode != Mode and Section == 0) //If mode has updated, and we are talking about the whole LEDstrip, clear the current state
FastLED.clear();


//Do not clear the clock/LED, we could need it for PHYSICS, but unsure how this would impact Section support, will fix later when I know more

//if (LastMode != Mode and Section == 0) //If mode has updated, and we are talking about the whole LEDstrip, clear the current state
// FastLED.clear();


bool ColorUpdated = false;
if (Mode == WIFI) AnimationCounter = 0;
if (AnimationCounter != 0) { //Animation needs to be shown
Expand Down Expand Up @@ -230,7 +236,7 @@ void handle_OnConnect() {
"let Sg=new Slider('Green');"
"let Sb=new Slider('Blue');"

"let Dm=new DropDown({name:'Mode',setParamName:'m',possibleValues:['OFF','ON','WIFI','CLOCK','BLINK','BPM','CONFETTI','FLASH','FLASH2','GLITTER','JUGGLE','MOVE','PACMAN','RAINBOW','SINELON','SINELON2','SMILEY'],modifySendParams:(oldParams)=>{if(Dm.value=='WIFI'){let extraData=this.getServerStateMessageData();return{...oldParams,...extraData};}},});"
"let Dm=new DropDown({name:'Mode',setParamName:'m',possibleValues:['OFF','ON','WIFI','CLOCK','BLINK','BPM','CONFETTI','FLASH','FLASH2','GLITTER','JUGGLE','MOVE','PACMAN','PHYSICS','RAINBOW','SINELON','SINELON2','SMILEY'],modifySendParams:(oldParams)=>{if(Dm.value=='WIFI'){let extraData=this.getServerStateMessageData();return{...oldParams,...extraData};}},});"
"let Dbm=new DropDown({name:'Bootmode',setParamName:'bm',possibleValues:['OFF','ON','WIFI','CLOCK']});"
"let Ddm=new DropDown({name:'Doublepress mode',setParamName:'dm',possibleValues:['WIFI','CLOCK','BLINK','RAINBOW']});"

Expand Down

0 comments on commit 73bb048

Please sign in to comment.