| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- #include <NeoPixelBus.h>
- #include <NeoPixelAnimator.h>
- #define PIN_LEDS 19 //灯球Din接核心板的19引脚
- #define NUMPIXELS 194
- #define PIN_CHRG 14 //将tp4054的CHRG引脚接到esp32的14引脚
- #define LIGHTNESS 0.05f
- #define BRIGHTNESS 60
- RgbColor BLACK(0, 0, 0);
- RgbColor RED( 0,BRIGHTNESS, 0);
- RgbColor ORANGE( BRIGHTNESS/5,BRIGHTNESS, 0);
- RgbColor YELLOW(BRIGHTNESS/2, BRIGHTNESS/2, 0);
- RgbColor GREEN( BRIGHTNESS,0, 0);
- RgbColor BLUENESS(BRIGHTNESS/2,0, BRIGHTNESS);
- RgbColor BLUE(0, 0, BRIGHTNESS);
- RgbColor PURPLE( 0,BRIGHTNESS/2, BRIGHTNESS);
- RgbColor WHITE(BRIGHTNESS/5, BRIGHTNESS/5, BRIGHTNESS/5);
- NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> leds(NUMPIXELS, PIN_LEDS);
- NeoPixelAnimator animations(10); // NeoPixel animation management object
- #define NUM_RINGS 11
- #define RING_MAX_PIXELS 26
- #define RING_MIN_PIXELS 8
- // all rings starts at 0 ray
- byte RINGS[NUM_RINGS][RING_MAX_PIXELS] = {
- {194,193,192,191,190,189,188,187}, // 8
- {186,185,184,183,182,181,180,179,178,177,176,175,174,173}, // 14
- {172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155}, // 18
- {154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135}, // 20
- {134,133,132,131,130,129,128,127,126,125,124,123,122,121,120,119,118,117,116,115,114,113,112,111}, // 24
- {110,109,108,107,106,105,104,103,102,101,100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85}, // 26
- { 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84}, // 24
- { 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, // 20
- { 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, // 18
- { 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}, // 14
- { 1, 2, 3, 4, 5, 6, 7, 8} // 8
- };
- byte RING_SIZES[NUM_RINGS] = {8, 14, 18, 20, 24, 26, 24, 20, 18, 14, 8};
- unsigned long startMillis = 0;
- short animation = 0;
- void setup() {
- pinMode(PIN_CHRG,INPUT_PULLUP);//设置模式为输入,上拉
- Serial.begin(115200);
- initRandom();
- leds.Begin();
- leds.ClearTo(BLACK);
- leds.Show();
- //animations.StartAnimation(0, 6000, rainbowAnimation);
- //animations.StartAnimation(0, 2000, raysRainbow);
- //animations.StartAnimation(0, 2000, ringsRainbow);
- //animations.StartAnimation(0, 500, xmasOrbAnimation);
- //animations.StartAnimation(0, 1000, ringAnimation);
- //animations.StartAnimation(0, 500, rayAnimation);
- //animations.StartAnimation(0, 100, randomAnimation);
- //animations.StartAnimation(0, 1500, ringAnimation2);
- }
- void loop() {
- animations.UpdateAnimations();
- leds.Show();
- delay(10);
-
- if(digitalRead(PIN_CHRG) == LOW){//检测CHRG是否被拉低
- leds.ClearTo(RED);
- leds.Show();
- delay(1000);
- leds.ClearTo(BLACK);
- leds.Show();
- while(true);//开始充电模式 不继续运行后面程序了
- }
- if (true && (startMillis == 0 || startMillis + 11400 < millis())) {
- startMillis = millis();
- switch (animation) {
- case 0:
- animations.StartAnimation(0, 500, oneByOne);
- break;
- /*
- case 1:
- animations.StartAnimation(0, 600, rayAnimation);
- break;
- case 2:
- animations.StartAnimation(0, 1200, ringAnimation);
- break;
-
- case 1:
- animations.StartAnimation(0, 1200, ringAnimation2);
- break; */
- case 1:
- animations.StartAnimation(0, 600, rayAnimation2);
- break;
- case 2:
- animations.StartAnimation(0, 3000, raysRainbow);
- break;
- case 3:
- animations.StartAnimation(0, 2000, ringsRainbow);
- break;
- case 4:
- animations.StartAnimation(0, 8000, rainbowAnimation);
- break;
- case 5:
- animations.StartAnimation(0, 100, randomAnimation);
- animation = -1;
- break;
- }
- animation ++;
- //leds.ClearTo(BLACK);
- }
-
- }
- void randomAnimation(const AnimationParam& param) {
- float hue;
- HslColor color;
- if (param.state == AnimationState_Completed) {
- for (byte i = 0; i < 194; i ++) {
- hue = random(0, 1000) / 1000.0f;
- color = HslColor(hue, 1.0f, LIGHTNESS);
- leds.SetPixelColor(i, color);
- }
- animations.RestartAnimation(0);
- }
- }
- void rainbowAnimation(const AnimationParam& param) {
- HslColor color = HslColor(param.progress, 1.0f, LIGHTNESS);
- leds.ClearTo(color);
- if (param.state == AnimationState_Completed) {
- animations.RestartAnimation(0);
- }
- }
- void raysRainbow(const AnimationParam& param) {
- HslColor color;
- float hue;
- for (int i = 0; i < RING_MAX_PIXELS; i++) {
- hue = param.progress + (float) i / (float) RING_MAX_PIXELS;
- if (hue > 1.0f) {
- hue -= 1.0f;
- }
- color = HslColor(hue, 1.0f, LIGHTNESS);
- rayColor(i, RgbColor(color));
- }
- if (param.state == AnimationState_Completed) {
- animations.RestartAnimation(0);
- }
- }
- void ringsRainbow(const AnimationParam& param) {
- HslColor color;
- float hue;
- for (int i = 0; i < NUM_RINGS; i++) {
- hue = param.progress + (float) i / (float) NUM_RINGS;
- if (hue > 1.0f) {
- hue -= 1.0f;
- }
- color = HslColor(hue, 1.0f, LIGHTNESS);
- ringColor(i, RgbColor(color));
- }
- if (param.state == AnimationState_Completed) {
- animations.RestartAnimation(0);
- }
- }
- void oneByOne(const AnimationParam& param) {
- static int static_cnt = 0;
- static int static_colorCnt = 0;
- int cnt = param.progress * 100;
- int index = 0;
- RgbColor color;
- leds.ClearTo(BLACK);
- if(static_cnt > NUMPIXELS){
- static_cnt = 0;
- static_colorCnt += 1;
- if(static_colorCnt>6){
- static_colorCnt = 0;
- }
- //leds.ClearTo(BLACK);
- }
- switch(static_colorCnt){
- case 0:color = RED;break;
- case 1:color = BLUE;break;
- case 2:color = GREEN;break;
- case 3:color = YELLOW;break;
- case 4:color = BLUENESS;break;
- case 5:color = ORANGE;break;
- case 6:color = PURPLE;break;
- default:color = RED;break;
- }
- for(int i=0;i<6;i++){
- index = static_cnt + i;
- if(index>NUMPIXELS){
- break;
- }
- if(index<85){
- leds.SetPixelColor(index, color);
- }
- else if(index<=110){
- leds.SetPixelColor(110 - index + 84, color);
- }
- else if(index<=134){
- leds.SetPixelColor(134 - index + 110, color);
- }
- else if(index<=154){
- leds.SetPixelColor(154 - index + 134, color);
- }
- else if(index<=172){
- leds.SetPixelColor(172 - index + 154, color);
- }
- else if(index<=186){
- leds.SetPixelColor(186 - index + 172, color);
- }
- else{
- leds.SetPixelColor(194 - index + 186, color);
- }
- }
-
- //static_cnt += 1;
-
- if(cnt%3 == 0){
- static_cnt += 1;
- }
- if (param.state == AnimationState_Completed) {
- animations.RestartAnimation(0);
- }
- }
- void xmasOrbAnimation(const AnimationParam& param) {
- ringColor(0, WHITE);
- ringColor(1, RED);
- ringColor(2, RED);
- ringColor(3, RED);
- ringColor(4, RED);
- ringColor(5, WHITE);
- ringColor(6, RED);
- ringColor(7, RED);
- ringColor(8, RED);
- ringColor(9, RED);
- ringColor(10, WHITE);
- byte offset = round(param.progress);
- for (byte i = offset; i < RING_SIZES[3]; i+=2) {
- leds.SetPixelColor(RINGS[3][i] - 1, WHITE);
- }
- for (byte i = offset; i < RING_SIZES[7]; i+=2) {
- leds.SetPixelColor(RINGS[7][i] - 1, WHITE);
- }
- if (param.state == AnimationState_Completed) {
- animations.RestartAnimation(0);
- }
- }
- void ringAnimation(const AnimationParam& param) {
- int index = param.progress * (NUM_RINGS * 2 - 2);
- static int static_colorCnt = 0;
- RgbColor color;
- if(index>=20){
- static_colorCnt += 1;
- if(static_colorCnt>6){
- static_colorCnt = 0;
- }
- }
- switch(static_colorCnt){
- case 0:color = RED;break;
- case 1:color = ORANGE;break;
- case 2:color = YELLOW;break;
- case 3:color = GREEN;break;
- case 4:color = BLUENESS;break;
- case 5:color = BLUE;break;
- case 6:color = PURPLE;break;
- default:color = RED;break;
- }
- leds.ClearTo(BLACK);
- if (index < NUM_RINGS) {
- ringColor(index, color);
- }
- else {
- ringColor(NUM_RINGS - (index - NUM_RINGS) - 2, color);
- }
- if (param.state == AnimationState_Completed) {
- animations.RestartAnimation(0);
- }
- }
- void ringAnimation2(const AnimationParam& param) {
- static int static_cnt = 0;
- int index = param.progress * (NUM_RINGS * 2 - 2);
- if (index>=20) {
- static_cnt += 1;
- if(static_cnt > 3){
- static_cnt = 0;
- }
- }
- switch(static_cnt){
- case 0:
- if (index < NUM_RINGS) {
- ringColor(index, WHITE);
- }
- else {
- ringColor(NUM_RINGS - (index - NUM_RINGS-1) - 2, RED);
- }
- break;
- case 1:
- if (index < NUM_RINGS) {
- ringColor(index, ORANGE);
- }
- else {
- ringColor(NUM_RINGS - (index - NUM_RINGS-1) - 2, YELLOW);
- }
- break;
- case 2:
- if (index < NUM_RINGS) {
- ringColor(index, GREEN);
- }
- else {
- ringColor(NUM_RINGS - (index - NUM_RINGS-1) - 2, BLUENESS);
- }
- break;
- case 3:
- if (index < NUM_RINGS) {
- ringColor(index, BLUE);
- }
- else {
- ringColor(NUM_RINGS - (index - NUM_RINGS-1) - 2, PURPLE);
- }
- break;
- }
- if (param.state == AnimationState_Completed) {
- animations.RestartAnimation(0);
- }
- }
- void rayAnimation(const AnimationParam& param) {
- int index = param.progress * (RING_MAX_PIXELS / 2);
- static int static_colorCnt = 0;
- RgbColor color;
- switch(static_colorCnt){
- case 0:color = RED;break;
- case 1:color = ORANGE;break;
- case 2:color = YELLOW;break;
- case 3:color = GREEN;break;
- case 4:color = BLUENESS;break;
- case 5:color = BLUE;break;
- case 6:color = PURPLE;break;
- default:color = RED;break;
- }
- if (index > 12) {
- index = 12;
- static_colorCnt += 1;
- if(static_colorCnt>6){
- static_colorCnt = 0;
- }
- }
-
- leds.ClearTo(BLACK);
- rayColor(index, color);
- rayColor(index + (RING_MAX_PIXELS / 2), color);
- if (param.state == AnimationState_Completed) {
- animations.RestartAnimation(0);
- }
- }
- void rayAnimation2(const AnimationParam& param) {
- static int static_cnt = 0;
- int index = param.progress * (RING_MAX_PIXELS+1);
- if (index>=26) {
- static_cnt += 1;
- if(static_cnt > 6){
- static_cnt = 0;
- }
- }
- switch(static_cnt){
- case 0:
- rayColor(index, RED);
- break;
- case 1:
- rayColor(index, ORANGE);
- break;
- case 2:
- rayColor(index, YELLOW);
- break;
- case 3:
- rayColor(index, GREEN);
- break;
- case 4:
- rayColor(index, BLUENESS);
- break;
- case 5:
- rayColor(index, BLUE);
- break;
- case 6:
- rayColor(index, PURPLE);
- break;
- }
- if (param.state == AnimationState_Completed) {
- animations.RestartAnimation(0);
- }
- }
- void rayColor(byte rayIndex, RgbColor color) {
- int pixelIndex;
- byte pixel;
- if (rayIndex >= RING_MAX_PIXELS) {
- return; // prevents out of bounds
- }
- for (byte i = 0; i < NUM_RINGS; i ++) {
- pixelIndex = round((float) RING_SIZES[i] / (float) RING_MAX_PIXELS * rayIndex);
- pixel = RINGS[i][pixelIndex];
- if (pixel == 0) {
- continue; // skip condition
- }
- leds.SetPixelColor(pixel - 1, color); // index starts from 1 (0 is stop condition)
- }
- }
- void ringColor(byte ringIndex, RgbColor color) {
- byte pixel;
- if (ringIndex >= NUM_RINGS) {
- return; // prevents out of bounds
- }
- for (byte i = 0; i < RING_MAX_PIXELS; i ++) {
- pixel = RINGS[ringIndex][i];
- if (pixel == 0) {
- return; // end condition
- }
- leds.SetPixelColor(pixel - 1, color); // index starts from 1 (0 is stop condition)
- }
- }
- void initRandom() {
- // random works best with a seed that can use 31 bits
- // analogRead on a unconnected pin tends toward less than four bits
- uint32_t seed = analogRead(0);
- delay(1);
- for (int shifts = 3; shifts < 31; shifts += 3) {
- seed ^= analogRead(0) << shifts;
- delay(1);
- }
- // Serial.println(seed);
- randomSeed(seed);
- }
- RgbColor colorWheel(byte wheelPos) {
- // Input a value 0 to 255 to get a color value.
- // The colours are a transition r - g - b - back to r.
- wheelPos = 255 - wheelPos;
- if (wheelPos < 85) {
- return RgbColor(255 - wheelPos * 3, 0, wheelPos * 3);
- }
- if (wheelPos < 170) {
- wheelPos -= 85;
- return RgbColor(0, wheelPos * 3, 255 - wheelPos * 3);
- }
- wheelPos -= 170;
- return RgbColor(wheelPos * 3, 255 - wheelPos * 3, 0);
- }
|