Skip to content

Instantly share code, notes, and snippets.

@costyn
Last active May 25, 2019 11:46
Show Gist options
  • Save costyn/29eecd578a905764d7321bab29d29850 to your computer and use it in GitHub Desktop.
Save costyn/29eecd578a905764d7321bab29d29850 to your computer and use it in GitHub Desktop.
My attempt at creating a circular loader
void circularLoader2() {
fill_solid(leds, NUM_LEDS, CRGB::Black);
uint8_t uneased_startP = lerp8by8( 0, NUM_LEDS, beat8( 30, 5000 ) ); // start position, runs behind endP
uint8_t uneased_endP = lerp8by8( 0, NUM_LEDS, beat8( 30 ) ); // start position
uint8_t startP = QuadraticEaseIn8( uneased_startP );
uint8_t endP = CubicEaseIn8( uneased_endP );
DEBUG_PRINT(F("startP: ")) ;
DEBUG_PRINT(startP) ;
DEBUG_PRINT(F("\t")) ;
DEBUG_PRINT(F("endP: ")) ;
DEBUG_PRINT(endP) ;
// DEBUG_PRINT(F("\t")) ;
// DEBUG_PRINT(F("eased: ")) ;
// DEBUG_PRINT(eased) ;
DEBUG_PRINTLN() ;
fillSolidRing(startP, endP, CHSV(90, 255, 255));
FastLED.show();
}
// Uses functions from https://github.com/warrenm/AHEasing
uint8_t QuadraticEaseIn8( uint8_t p ) {
int i_100 = map(p, 0, NUM_LEDS, 0, 100); // Map current led p to percentage between 0 - 100
AHFloat eased_float = QuadraticEaseInOut( (float)i_100 / (float)100); // Convert to value between 0 - 1
int eased_100 = (int)(eased_float * 100); // convert back to percentage
return map(eased_100, 0, 100, 0, NUM_LEDS); // convert back to LED position
}
uint8_t CubicEaseIn8( uint8_t p ) {
int i_100 = map(p, 0, NUM_LEDS, 0, 100); // Map current led p to percentage between 0 - 100
AHFloat eased_float = CubicEaseInOut( (float)i_100 / (float)100); // Convert to value between 0 - 1
int eased_100 = (int)(eased_float * 100); // convert back to percentage
return map(eased_100, 0, 100, 0, NUM_LEDS); // convert back to LED position
}
// This is a little convoluted and could probably be written better :)
void fillSolidRing( int startLed, int endLed, CHSV color ) {
// Determine actual start and actual end (normalize using custom modulo):
int actualStart = mod(startLed + NUM_LEDS, NUM_LEDS) ;
int actualEnd = mod(endLed + NUM_LEDS, NUM_LEDS) ;
// If beginning is at say 50, and end at 10, then we split the fill in 2:
// * one from 50-59
// * one from 0-10
if ( actualStart > actualEnd ) {
fill_solid(leds + actualStart, NUM_LEDS - actualStart, color);
fill_solid(leds, actualEnd, color);
} else {
fill_solid(leds + actualStart, actualEnd - actualStart, color);
}
} // end fillSolidRing()
// Custom modulo which always returns a positive number
int mod(int x, int m) {
return (x % m + m) % m;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment