neo-sphere.ino 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #include <NeoPixelBus.h>
  2. #include <NeoPixelAnimator.h>
  3. #define PIN_LEDS 19 //灯球Din接核心板的19引脚
  4. #define NUMPIXELS 194
  5. #define PIN_CHRG 14 //将tp4054的CHRG引脚接到esp32的14引脚
  6. #define LIGHTNESS 0.05f
  7. #define BRIGHTNESS 60
  8. RgbColor BLACK(0, 0, 0);
  9. RgbColor RED( 0,BRIGHTNESS, 0);
  10. RgbColor ORANGE( BRIGHTNESS/5,BRIGHTNESS, 0);
  11. RgbColor YELLOW(BRIGHTNESS/2, BRIGHTNESS/2, 0);
  12. RgbColor GREEN( BRIGHTNESS,0, 0);
  13. RgbColor BLUENESS(BRIGHTNESS/2,0, BRIGHTNESS);
  14. RgbColor BLUE(0, 0, BRIGHTNESS);
  15. RgbColor PURPLE( 0,BRIGHTNESS/2, BRIGHTNESS);
  16. RgbColor WHITE(BRIGHTNESS/5, BRIGHTNESS/5, BRIGHTNESS/5);
  17. NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> leds(NUMPIXELS, PIN_LEDS);
  18. NeoPixelAnimator animations(10); // NeoPixel animation management object
  19. #define NUM_RINGS 11
  20. #define RING_MAX_PIXELS 26
  21. #define RING_MIN_PIXELS 8
  22. // all rings starts at 0 ray
  23. byte RINGS[NUM_RINGS][RING_MAX_PIXELS] = {
  24. {194,193,192,191,190,189,188,187}, // 8
  25. {186,185,184,183,182,181,180,179,178,177,176,175,174,173}, // 14
  26. {172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155}, // 18
  27. {154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135}, // 20
  28. {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
  29. {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
  30. { 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
  31. { 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, // 20
  32. { 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, // 18
  33. { 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}, // 14
  34. { 1, 2, 3, 4, 5, 6, 7, 8} // 8
  35. };
  36. byte RING_SIZES[NUM_RINGS] = {8, 14, 18, 20, 24, 26, 24, 20, 18, 14, 8};
  37. unsigned long startMillis = 0;
  38. short animation = 0;
  39. void setup() {
  40. pinMode(PIN_CHRG,INPUT_PULLUP);//设置模式为输入,上拉
  41. Serial.begin(115200);
  42. initRandom();
  43. leds.Begin();
  44. leds.ClearTo(BLACK);
  45. leds.Show();
  46. //animations.StartAnimation(0, 6000, rainbowAnimation);
  47. //animations.StartAnimation(0, 2000, raysRainbow);
  48. //animations.StartAnimation(0, 2000, ringsRainbow);
  49. //animations.StartAnimation(0, 500, xmasOrbAnimation);
  50. //animations.StartAnimation(0, 1000, ringAnimation);
  51. //animations.StartAnimation(0, 500, rayAnimation);
  52. //animations.StartAnimation(0, 100, randomAnimation);
  53. //animations.StartAnimation(0, 1500, ringAnimation2);
  54. }
  55. void loop() {
  56. animations.UpdateAnimations();
  57. leds.Show();
  58. delay(10);
  59. if(digitalRead(PIN_CHRG) == LOW){//检测CHRG是否被拉低
  60. leds.ClearTo(RED);
  61. leds.Show();
  62. delay(1000);
  63. leds.ClearTo(BLACK);
  64. leds.Show();
  65. while(true);//开始充电模式 不继续运行后面程序了
  66. }
  67. if (true && (startMillis == 0 || startMillis + 11400 < millis())) {
  68. startMillis = millis();
  69. switch (animation) {
  70. case 0:
  71. animations.StartAnimation(0, 500, oneByOne);
  72. break;
  73. /*
  74. case 1:
  75. animations.StartAnimation(0, 600, rayAnimation);
  76. break;
  77. case 2:
  78. animations.StartAnimation(0, 1200, ringAnimation);
  79. break;
  80. case 1:
  81. animations.StartAnimation(0, 1200, ringAnimation2);
  82. break; */
  83. case 1:
  84. animations.StartAnimation(0, 600, rayAnimation2);
  85. break;
  86. case 2:
  87. animations.StartAnimation(0, 3000, raysRainbow);
  88. break;
  89. case 3:
  90. animations.StartAnimation(0, 2000, ringsRainbow);
  91. break;
  92. case 4:
  93. animations.StartAnimation(0, 8000, rainbowAnimation);
  94. break;
  95. case 5:
  96. animations.StartAnimation(0, 100, randomAnimation);
  97. animation = -1;
  98. break;
  99. }
  100. animation ++;
  101. //leds.ClearTo(BLACK);
  102. }
  103. }
  104. void randomAnimation(const AnimationParam& param) {
  105. float hue;
  106. HslColor color;
  107. if (param.state == AnimationState_Completed) {
  108. for (byte i = 0; i < 194; i ++) {
  109. hue = random(0, 1000) / 1000.0f;
  110. color = HslColor(hue, 1.0f, LIGHTNESS);
  111. leds.SetPixelColor(i, color);
  112. }
  113. animations.RestartAnimation(0);
  114. }
  115. }
  116. void rainbowAnimation(const AnimationParam& param) {
  117. HslColor color = HslColor(param.progress, 1.0f, LIGHTNESS);
  118. leds.ClearTo(color);
  119. if (param.state == AnimationState_Completed) {
  120. animations.RestartAnimation(0);
  121. }
  122. }
  123. void raysRainbow(const AnimationParam& param) {
  124. HslColor color;
  125. float hue;
  126. for (int i = 0; i < RING_MAX_PIXELS; i++) {
  127. hue = param.progress + (float) i / (float) RING_MAX_PIXELS;
  128. if (hue > 1.0f) {
  129. hue -= 1.0f;
  130. }
  131. color = HslColor(hue, 1.0f, LIGHTNESS);
  132. rayColor(i, RgbColor(color));
  133. }
  134. if (param.state == AnimationState_Completed) {
  135. animations.RestartAnimation(0);
  136. }
  137. }
  138. void ringsRainbow(const AnimationParam& param) {
  139. HslColor color;
  140. float hue;
  141. for (int i = 0; i < NUM_RINGS; i++) {
  142. hue = param.progress + (float) i / (float) NUM_RINGS;
  143. if (hue > 1.0f) {
  144. hue -= 1.0f;
  145. }
  146. color = HslColor(hue, 1.0f, LIGHTNESS);
  147. ringColor(i, RgbColor(color));
  148. }
  149. if (param.state == AnimationState_Completed) {
  150. animations.RestartAnimation(0);
  151. }
  152. }
  153. void oneByOne(const AnimationParam& param) {
  154. static int static_cnt = 0;
  155. static int static_colorCnt = 0;
  156. int cnt = param.progress * 100;
  157. int index = 0;
  158. RgbColor color;
  159. leds.ClearTo(BLACK);
  160. if(static_cnt > NUMPIXELS){
  161. static_cnt = 0;
  162. static_colorCnt += 1;
  163. if(static_colorCnt>6){
  164. static_colorCnt = 0;
  165. }
  166. //leds.ClearTo(BLACK);
  167. }
  168. switch(static_colorCnt){
  169. case 0:color = RED;break;
  170. case 1:color = BLUE;break;
  171. case 2:color = GREEN;break;
  172. case 3:color = YELLOW;break;
  173. case 4:color = BLUENESS;break;
  174. case 5:color = ORANGE;break;
  175. case 6:color = PURPLE;break;
  176. default:color = RED;break;
  177. }
  178. for(int i=0;i<6;i++){
  179. index = static_cnt + i;
  180. if(index>NUMPIXELS){
  181. break;
  182. }
  183. if(index<85){
  184. leds.SetPixelColor(index, color);
  185. }
  186. else if(index<=110){
  187. leds.SetPixelColor(110 - index + 84, color);
  188. }
  189. else if(index<=134){
  190. leds.SetPixelColor(134 - index + 110, color);
  191. }
  192. else if(index<=154){
  193. leds.SetPixelColor(154 - index + 134, color);
  194. }
  195. else if(index<=172){
  196. leds.SetPixelColor(172 - index + 154, color);
  197. }
  198. else if(index<=186){
  199. leds.SetPixelColor(186 - index + 172, color);
  200. }
  201. else{
  202. leds.SetPixelColor(194 - index + 186, color);
  203. }
  204. }
  205. //static_cnt += 1;
  206. if(cnt%3 == 0){
  207. static_cnt += 1;
  208. }
  209. if (param.state == AnimationState_Completed) {
  210. animations.RestartAnimation(0);
  211. }
  212. }
  213. void xmasOrbAnimation(const AnimationParam& param) {
  214. ringColor(0, WHITE);
  215. ringColor(1, RED);
  216. ringColor(2, RED);
  217. ringColor(3, RED);
  218. ringColor(4, RED);
  219. ringColor(5, WHITE);
  220. ringColor(6, RED);
  221. ringColor(7, RED);
  222. ringColor(8, RED);
  223. ringColor(9, RED);
  224. ringColor(10, WHITE);
  225. byte offset = round(param.progress);
  226. for (byte i = offset; i < RING_SIZES[3]; i+=2) {
  227. leds.SetPixelColor(RINGS[3][i] - 1, WHITE);
  228. }
  229. for (byte i = offset; i < RING_SIZES[7]; i+=2) {
  230. leds.SetPixelColor(RINGS[7][i] - 1, WHITE);
  231. }
  232. if (param.state == AnimationState_Completed) {
  233. animations.RestartAnimation(0);
  234. }
  235. }
  236. void ringAnimation(const AnimationParam& param) {
  237. int index = param.progress * (NUM_RINGS * 2 - 2);
  238. static int static_colorCnt = 0;
  239. RgbColor color;
  240. if(index>=20){
  241. static_colorCnt += 1;
  242. if(static_colorCnt>6){
  243. static_colorCnt = 0;
  244. }
  245. }
  246. switch(static_colorCnt){
  247. case 0:color = RED;break;
  248. case 1:color = ORANGE;break;
  249. case 2:color = YELLOW;break;
  250. case 3:color = GREEN;break;
  251. case 4:color = BLUENESS;break;
  252. case 5:color = BLUE;break;
  253. case 6:color = PURPLE;break;
  254. default:color = RED;break;
  255. }
  256. leds.ClearTo(BLACK);
  257. if (index < NUM_RINGS) {
  258. ringColor(index, color);
  259. }
  260. else {
  261. ringColor(NUM_RINGS - (index - NUM_RINGS) - 2, color);
  262. }
  263. if (param.state == AnimationState_Completed) {
  264. animations.RestartAnimation(0);
  265. }
  266. }
  267. void ringAnimation2(const AnimationParam& param) {
  268. static int static_cnt = 0;
  269. int index = param.progress * (NUM_RINGS * 2 - 2);
  270. if (index>=20) {
  271. static_cnt += 1;
  272. if(static_cnt > 3){
  273. static_cnt = 0;
  274. }
  275. }
  276. switch(static_cnt){
  277. case 0:
  278. if (index < NUM_RINGS) {
  279. ringColor(index, WHITE);
  280. }
  281. else {
  282. ringColor(NUM_RINGS - (index - NUM_RINGS-1) - 2, RED);
  283. }
  284. break;
  285. case 1:
  286. if (index < NUM_RINGS) {
  287. ringColor(index, ORANGE);
  288. }
  289. else {
  290. ringColor(NUM_RINGS - (index - NUM_RINGS-1) - 2, YELLOW);
  291. }
  292. break;
  293. case 2:
  294. if (index < NUM_RINGS) {
  295. ringColor(index, GREEN);
  296. }
  297. else {
  298. ringColor(NUM_RINGS - (index - NUM_RINGS-1) - 2, BLUENESS);
  299. }
  300. break;
  301. case 3:
  302. if (index < NUM_RINGS) {
  303. ringColor(index, BLUE);
  304. }
  305. else {
  306. ringColor(NUM_RINGS - (index - NUM_RINGS-1) - 2, PURPLE);
  307. }
  308. break;
  309. }
  310. if (param.state == AnimationState_Completed) {
  311. animations.RestartAnimation(0);
  312. }
  313. }
  314. void rayAnimation(const AnimationParam& param) {
  315. int index = param.progress * (RING_MAX_PIXELS / 2);
  316. static int static_colorCnt = 0;
  317. RgbColor color;
  318. switch(static_colorCnt){
  319. case 0:color = RED;break;
  320. case 1:color = ORANGE;break;
  321. case 2:color = YELLOW;break;
  322. case 3:color = GREEN;break;
  323. case 4:color = BLUENESS;break;
  324. case 5:color = BLUE;break;
  325. case 6:color = PURPLE;break;
  326. default:color = RED;break;
  327. }
  328. if (index > 12) {
  329. index = 12;
  330. static_colorCnt += 1;
  331. if(static_colorCnt>6){
  332. static_colorCnt = 0;
  333. }
  334. }
  335. leds.ClearTo(BLACK);
  336. rayColor(index, color);
  337. rayColor(index + (RING_MAX_PIXELS / 2), color);
  338. if (param.state == AnimationState_Completed) {
  339. animations.RestartAnimation(0);
  340. }
  341. }
  342. void rayAnimation2(const AnimationParam& param) {
  343. static int static_cnt = 0;
  344. int index = param.progress * (RING_MAX_PIXELS+1);
  345. if (index>=26) {
  346. static_cnt += 1;
  347. if(static_cnt > 6){
  348. static_cnt = 0;
  349. }
  350. }
  351. switch(static_cnt){
  352. case 0:
  353. rayColor(index, RED);
  354. break;
  355. case 1:
  356. rayColor(index, ORANGE);
  357. break;
  358. case 2:
  359. rayColor(index, YELLOW);
  360. break;
  361. case 3:
  362. rayColor(index, GREEN);
  363. break;
  364. case 4:
  365. rayColor(index, BLUENESS);
  366. break;
  367. case 5:
  368. rayColor(index, BLUE);
  369. break;
  370. case 6:
  371. rayColor(index, PURPLE);
  372. break;
  373. }
  374. if (param.state == AnimationState_Completed) {
  375. animations.RestartAnimation(0);
  376. }
  377. }
  378. void rayColor(byte rayIndex, RgbColor color) {
  379. int pixelIndex;
  380. byte pixel;
  381. if (rayIndex >= RING_MAX_PIXELS) {
  382. return; // prevents out of bounds
  383. }
  384. for (byte i = 0; i < NUM_RINGS; i ++) {
  385. pixelIndex = round((float) RING_SIZES[i] / (float) RING_MAX_PIXELS * rayIndex);
  386. pixel = RINGS[i][pixelIndex];
  387. if (pixel == 0) {
  388. continue; // skip condition
  389. }
  390. leds.SetPixelColor(pixel - 1, color); // index starts from 1 (0 is stop condition)
  391. }
  392. }
  393. void ringColor(byte ringIndex, RgbColor color) {
  394. byte pixel;
  395. if (ringIndex >= NUM_RINGS) {
  396. return; // prevents out of bounds
  397. }
  398. for (byte i = 0; i < RING_MAX_PIXELS; i ++) {
  399. pixel = RINGS[ringIndex][i];
  400. if (pixel == 0) {
  401. return; // end condition
  402. }
  403. leds.SetPixelColor(pixel - 1, color); // index starts from 1 (0 is stop condition)
  404. }
  405. }
  406. void initRandom() {
  407. // random works best with a seed that can use 31 bits
  408. // analogRead on a unconnected pin tends toward less than four bits
  409. uint32_t seed = analogRead(0);
  410. delay(1);
  411. for (int shifts = 3; shifts < 31; shifts += 3) {
  412. seed ^= analogRead(0) << shifts;
  413. delay(1);
  414. }
  415. // Serial.println(seed);
  416. randomSeed(seed);
  417. }
  418. RgbColor colorWheel(byte wheelPos) {
  419. // Input a value 0 to 255 to get a color value.
  420. // The colours are a transition r - g - b - back to r.
  421. wheelPos = 255 - wheelPos;
  422. if (wheelPos < 85) {
  423. return RgbColor(255 - wheelPos * 3, 0, wheelPos * 3);
  424. }
  425. if (wheelPos < 170) {
  426. wheelPos -= 85;
  427. return RgbColor(0, wheelPos * 3, 255 - wheelPos * 3);
  428. }
  429. wheelPos -= 170;
  430. return RgbColor(wheelPos * 3, 255 - wheelPos * 3, 0);
  431. }