u_effect_normal.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. #include "u_effect_normal.h"
  2. #include "u_effect_color.h"
  3. #include "u_main.h"
  4. #include "u_pwm_drive.h"
  5. LightInfo_t g_light_info = {0x00};
  6. uint8_t u_fabs(uint8_t x, uint8_t y)
  7. {
  8. if (x > y)
  9. return x - y;
  10. else
  11. return y - x;
  12. }
  13. uint32_t light_color_bright_create(uint32_t light_color, uint8_t bright)
  14. {
  15. uint32_t create_color = 0;
  16. uint8_t color_buff[3] = {0x00};
  17. if (bright == 0)
  18. {
  19. return 0x00;
  20. }
  21. //U_UART_PRINTF("breath_color \n");
  22. color_buff[0] = (light_color >> 16 & 0xff) * bright / 100;
  23. color_buff[1] = (light_color >> 8 & 0xff) * bright / 100;
  24. color_buff[2] = (light_color & 0xff) * bright / 100;
  25. create_color = color_buff[0] << 16 | color_buff[1] << 8 | color_buff[2];
  26. //U_UART_PRINTF("create_color = %x\n", create_color);
  27. return create_color;
  28. }
  29. uint32_t light_color_bright_alter(uint32_t light_color, uint8_t old_bright, uint8_t new_bright)
  30. {
  31. uint32_t create_color = 0;
  32. uint8_t color_buff[3] = {0x00};
  33. //U_UART_PRINTF("breath_color \n");
  34. color_buff[0] = (light_color >> 16 & 0xff) * new_bright / old_bright;
  35. color_buff[1] = (light_color >> 8 & 0xff) * new_bright / old_bright;
  36. color_buff[2] = (light_color & 0xff) * new_bright / old_bright;
  37. create_color = color_buff[0] << 16 | color_buff[1] << 8 | color_buff[2];
  38. //U_UART_PRINTF("create_color = %x\n", create_color);
  39. return create_color;
  40. }
  41. /****************************** light_mode_gradual **********************************/
  42. void light_mode_gradual_timer_cb(LightInfo_t *gradual)
  43. {
  44. static uint8_t red_cnt = 0, green_cnt = 0, blue_cnt = 0;
  45. // float green_cnt=0;
  46. // float blue_cnt=0;
  47. static uint8_t index = 0;
  48. uint8_t temp_cnt = 0;
  49. if(gradual->change_cnt)
  50. {
  51. gradual->change_cnt--;
  52. if(gradual->clr_buff[gradual->curr_ind][0] < gradual->clr_buff[index][0])
  53. {
  54. //red_cnt=(float)(gradual->color_buff[index][0] - gradual->color_buff[gradual->curr_ind][0]) / ((gradual->change_time*100)/20);
  55. if(gradual->change_cnt > 0)
  56. {
  57. gradual->clr_temp[0] += red_cnt;
  58. }
  59. else
  60. {
  61. gradual->clr_temp[0] = gradual->clr_buff[index][0] * 10;
  62. }
  63. }
  64. else if(gradual->clr_buff[gradual->curr_ind][0] > gradual->clr_buff[index][0])
  65. {
  66. //red_cnt = (float)(gradual->color_buff[gradual->curr_ind][0] - gradual->color_buff[index][0]) / ((gradual->change_time*100)/20);
  67. if(gradual->change_cnt > 0)
  68. {
  69. gradual->clr_temp[0] -= red_cnt;
  70. }
  71. else
  72. {
  73. gradual->clr_temp[0] = gradual->clr_buff[index][0] * 10;
  74. }
  75. }
  76. if(gradual->clr_buff[gradual->curr_ind][1] < gradual->clr_buff[index][1])
  77. {
  78. //green_cnt=(float)(gradual->color_buff[index][1] - gradual->color_buff[gradual->curr_ind][1]) / ((gradual->change_time*100)/20);
  79. //U_UART_PRINTF("green_cnt+ = %d\n", (int)green_cnt);
  80. if(gradual->change_cnt > 0)
  81. {
  82. gradual->clr_temp[1] += green_cnt;
  83. }
  84. else
  85. {
  86. gradual->clr_temp[1] = gradual->clr_buff[index][1] * 10;
  87. }
  88. }
  89. else if(gradual->clr_buff[gradual->curr_ind][1] > gradual->clr_buff[index][1])
  90. {
  91. //green_cnt=(float)(gradual->color_buff[gradual->curr_ind][1] - gradual->color_buff[index][1]) / ((gradual->change_time*100)/20);
  92. //U_UART_PRINTF("green_cnt- = %d\n", (int)green_cnt);
  93. if(gradual->change_cnt > 0)
  94. {
  95. gradual->clr_temp[1] -= green_cnt;
  96. }
  97. else
  98. {
  99. gradual->clr_temp[1] = gradual->clr_buff[index][1] * 10;
  100. }
  101. }
  102. if(gradual->clr_buff[gradual->curr_ind][2] < gradual->clr_buff[index][2])
  103. {
  104. //blue_cnt=(float)(gradual->color_buff[index][2] - gradual->color_buff[gradual->curr_ind][2]) / ((gradual->change_time*100)/20);
  105. //U_UART_PRINTF("b_i = %d, b_c = %d, blue_cnt+ = %d\n",gradual->color_buff[index][2], gradual->color_buff[gradual->curr_ind][2], (int)blue_cnt);
  106. if(gradual->change_cnt > 0)
  107. {
  108. gradual->clr_temp[2] += blue_cnt;
  109. }
  110. else
  111. {
  112. gradual->clr_temp[2] = gradual->clr_buff[index][2] * 10;
  113. }
  114. }
  115. else if(gradual->clr_buff[gradual->curr_ind][2] > gradual->clr_buff[index][2])
  116. {
  117. //blue_cnt=(float)(gradual->color_buff[gradual->curr_ind][2] - gradual->color_buff[index][2]) / ((gradual->change_time*100)/20);
  118. //U_UART_PRINTF("b_i = %d, b_c = %d, blue_cnt- = %d\n",gradual->color_buff[index][2], gradual->color_buff[gradual->curr_ind][2], (int)blue_cnt);
  119. if(gradual->change_cnt > 0)
  120. {
  121. gradual->clr_temp[2] -= blue_cnt;
  122. }
  123. else
  124. {
  125. gradual->clr_temp[2] = gradual->clr_buff[index][2] * 10;
  126. }
  127. }
  128. light_set_color(light_color_bright_create((gradual->clr_temp[0] / 10) << 16 | (gradual->clr_temp[1] / 10) << 8 | (gradual->clr_temp[2] / 10) , gradual->set_bright));
  129. }
  130. else
  131. {
  132. // gradual->curr_ind = ++gradual->curr_ind % gradual->clr_num_max;
  133. // index = (gradual->curr_ind + 1) % gradual->clr_num_max;
  134. // gradual->change_cnt = gradual->set_speed * 18;
  135. //
  136. // red_cnt = u_fabs(gradual->clr_buff[index][0] , gradual->clr_buff[gradual->curr_ind][0]) * 10 / gradual->change_cnt;
  137. // green_cnt = u_fabs(gradual->clr_buff[index][1] , gradual->clr_buff[gradual->curr_ind][1]) * 10 / gradual->change_cnt;
  138. // blue_cnt = u_fabs(gradual->clr_buff[index][2] , gradual->clr_buff[gradual->curr_ind][2]) * 10 / gradual->change_cnt;
  139. // //U_UART_PRINTF("change_cnt = %d sp = %d, cnt: %d, %d, %d\n",gradual->change_cnt, gradual->set_speed, red_cnt, green_cnt, blue_cnt);
  140. // gradual->clr_temp[0] = gradual->clr_buff[gradual->curr_ind][0] * 10;
  141. // gradual->clr_temp[1] = gradual->clr_buff[gradual->curr_ind][1] * 10;
  142. // gradual->clr_temp[2] = gradual->clr_buff[gradual->curr_ind][2] * 10;
  143. temp_cnt= ++gradual->curr_ind ;
  144. gradual->curr_ind =temp_cnt % gradual->clr_num_max;
  145. index = (gradual->curr_ind + 1) % gradual->clr_num_max;
  146. gradual->change_cnt = gradual->set_speed * 18;
  147. if(gradual->change_cnt!=0) //这里非0才运算 不然会异常死机 不能做除0运算 20211011刘浩改
  148. {
  149. red_cnt = (u_fabs(gradual->clr_buff[index][0] , gradual->clr_buff[gradual->curr_ind][0]) * 10) / gradual->change_cnt;
  150. green_cnt = u_fabs(gradual->clr_buff[index][1] , gradual->clr_buff[gradual->curr_ind][1]) * 10 / gradual->change_cnt;
  151. blue_cnt = u_fabs(gradual->clr_buff[index][2] , gradual->clr_buff[gradual->curr_ind][2]) * 10 / gradual->change_cnt;
  152. }else
  153. {
  154. red_cnt=0;
  155. green_cnt=0;
  156. blue_cnt=0;
  157. }
  158. gradual->clr_temp[0] = gradual->clr_buff[gradual->curr_ind][0] * 10;
  159. gradual->clr_temp[1] = gradual->clr_buff[gradual->curr_ind][1] * 10;
  160. gradual->clr_temp[2] = gradual->clr_buff[gradual->curr_ind][2] * 10;
  161. }
  162. }
  163. #define GRADUAL_COLOR_NUM_MAX (8)
  164. void light_mode_gradual_cfg(LightInfo_t *gradual_cfg, const uint32_t *color_buff, uint8_t color_num, uint8_t speed, uint8_t bright)
  165. {
  166. #ifdef DEBUG_DRV_APP
  167. U_UART_PRINTF("gradual_clr_num = %d, speed = %d, bright = %d\n", color_num, speed, bright);
  168. #endif
  169. if (color_buff)
  170. {
  171. int i = 0;
  172. memset(gradual_cfg, 0, sizeof(LightInfo_t));
  173. gradual_cfg->change_cnt = 0;
  174. gradual_cfg->clr_num_max = color_num;
  175. if (gradual_cfg->clr_num_max > GRADUAL_COLOR_NUM_MAX)
  176. {
  177. return;
  178. }
  179. for (i = 0; i < gradual_cfg->clr_num_max; i++)
  180. {
  181. gradual_cfg->clr_buff[i][0] = (color_buff[i] >> 16) & 0xff;
  182. gradual_cfg->clr_buff[i][1] = (color_buff[i] >> 8) & 0xff;
  183. gradual_cfg->clr_buff[i][2] = color_buff[i] & 0xff;
  184. }
  185. g_light_para.effect = LIGHT_EFFECT_GRADUAL;
  186. //u_analog_timer_stop(&g_ag_timer_light_effect);
  187. //u_analog_timer_start(&g_ag_timer_light_effect, 20);
  188. if (color_num == 1) //只有一种颜色
  189. {
  190. gradual_cfg->clr_buff[1][0] = 0x00;
  191. gradual_cfg->clr_buff[1][1] = 0x00;
  192. gradual_cfg->clr_buff[1][2] = 0x00;
  193. gradual_cfg->clr_num_max = 2;
  194. }
  195. memset(gradual_cfg->color_temp, 0, 3);
  196. gradual_cfg->curr_ind = 0;
  197. light_mode_gradual_timer_cb(gradual_cfg);
  198. //light_effect_timer_stop();
  199. light_effect_timer_start(20);
  200. }
  201. if (bright)
  202. {
  203. gradual_cfg->set_bright = bright;
  204. }
  205. if (speed)
  206. {
  207. gradual_cfg->change_cnt = 0;
  208. gradual_cfg->curr_ind = 0;
  209. gradual_cfg->set_speed = speed;
  210. }
  211. }
  212. /**************************** light_mode_grautal end ********************************/
  213. /******************************** light_mode_jump ********************************/
  214. void light_mode_jump_timer_cb(LightInfo_t *jump)
  215. {
  216. if (jump->clr_num_max > 1)
  217. {
  218. jump->clr_ind = ++jump->clr_ind >= jump->clr_num_max ? 0 :jump->clr_ind;
  219. light_set_color(light_color_bright_create(jump->color_buff[jump->clr_ind], jump->set_bright));
  220. }
  221. else
  222. {
  223. jump->clr_ind = ++jump->clr_ind >= 2 ? 0 :jump->clr_ind;
  224. if (jump->clr_ind == 0)
  225. {
  226. light_set_color(light_color_bright_create(jump->color_buff[jump->clr_ind], jump->set_bright));
  227. }
  228. else
  229. {
  230. light_set_color(0x00);
  231. }
  232. }
  233. }
  234. void light_mode_jump_cfg(LightInfo_t *jump_cfg, const uint32_t *color_buff, uint8_t color_num, uint8_t speed, uint8_t bright)
  235. {
  236. static unsigned char i=0;
  237. #ifdef DEBUG_DRV_APP
  238. U_UART_PRINTF("jumpclr_num = %d, speed = %d, bright = %d\n", color_num, speed, bright);
  239. #endif
  240. if (color_buff)
  241. {
  242. i = 0;
  243. memset(jump_cfg, 0, sizeof(LightInfo_t));
  244. jump_cfg->color_buff = color_buff;
  245. jump_cfg->clr_num_max = color_num;
  246. jump_cfg->clr_ind = 0;
  247. }
  248. if (bright)
  249. {
  250. jump_cfg->set_bright = bright;
  251. }
  252. if (speed)
  253. {
  254. light_effect_timer_stop();
  255. if( i < jump_cfg->clr_num_max)
  256. {
  257. i++;
  258. }else
  259. {
  260. i = 0;
  261. }
  262. jump_cfg->clr_ind = i;
  263. jump_cfg->set_speed = speed - 1;
  264. g_light_para.effect = LIGHT_EFFECT_JUMP;
  265. light_effect_timer_start(250 + 275 * jump_cfg->set_speed); //475
  266. light_mode_jump_timer_cb(jump_cfg);
  267. }
  268. }
  269. /**************************** light_mode_jump end ********************************/
  270. /**************************** light_mode_breath ********************************/
  271. void light_mode_breath_timer_cb(LightInfo_t *breath)
  272. {
  273. uint32_t breath_color = 0;
  274. if (breath->bright_dir == BRIGHT_DIR_SUBT)
  275. {
  276. breath->curr_bright -= BREATH_BRIGHT_STEP_VAL;
  277. if (breath->curr_bright <= 10)
  278. {
  279. breath->curr_bright = 0;
  280. breath->bright_dir = !breath->bright_dir;
  281. breath->clr_ind = ++breath->clr_ind >= breath->clr_num_max ? 0 : breath->clr_ind;
  282. }
  283. }
  284. else
  285. {
  286. breath->curr_bright += BREATH_BRIGHT_STEP_VAL;
  287. if (breath->curr_bright >= breath->set_bright)
  288. {
  289. breath->curr_bright = breath->set_bright;
  290. if (++breath->change_cnt > 5)
  291. {
  292. breath->change_cnt = 0;
  293. breath->bright_dir = !breath->bright_dir;
  294. }
  295. }
  296. }
  297. breath_color = light_color_bright_create(breath->color_buff[breath->clr_ind], breath->curr_bright);
  298. //U_UART_PRINTF("c_color = %X \n", breath_color);
  299. light_set_color(breath_color);
  300. }
  301. void light_mode_breath_cfg(LightInfo_t *breath_cfg, const uint32_t *color_buff, uint8_t color_num, uint8_t speed, uint8_t bright)
  302. {
  303. if (color_buff)
  304. {
  305. memset(breath_cfg, 0, sizeof(LightInfo_t));
  306. breath_cfg->color_buff = color_buff;
  307. breath_cfg->clr_ind = 0;
  308. breath_cfg->clr_num_max = color_num;
  309. breath_cfg->bright_dir = BRIGHT_DIR_PLUS;
  310. breath_cfg->curr_bright = 100;
  311. }
  312. if (bright)
  313. {
  314. breath_cfg->set_bright = bright;
  315. }
  316. if (speed)
  317. {
  318. breath_cfg->set_speed = speed;
  319. g_light_para.effect = LIGHT_EFFECT_BREATH;
  320. //u_analog_timer_start(&g_ag_timer_light_effect, breath_cfg->set_speed * 4 + 10);
  321. light_effect_timer_start(breath_cfg->set_speed * 4 + 10);
  322. }
  323. }
  324. /**************************** light_mode_breath end ********************************/
  325. /**************************** light_mode_static ********************************/
  326. void light_mode_static_cfg(LightInfo_t *static_cfg, const uint32_t *color_buff, uint8_t bright)
  327. {
  328. #ifdef DEBUG_DRV_APP
  329. U_UART_PRINTF("static_clr = %06X\n", color_buff[0]);
  330. #endif
  331. if (color_buff)
  332. {
  333. memset(static_cfg, 0, sizeof(LightInfo_t));
  334. static_cfg->color_buff = color_buff;
  335. }
  336. if (bright)
  337. {
  338. static_cfg->set_bright = bright;
  339. }
  340. g_light_para.effect = LIGHT_EFFECT_STATIC;
  341. light_effect_timer_stop();
  342. light_set_color(light_color_bright_create(*static_cfg->color_buff, static_cfg->set_bright));
  343. }
  344. /******************************* light_mode_static end ********************************/
  345. /********************************* light_mode_blink ***********************************/
  346. void light_mode_blink_cfg(LightInfo_t *blink_cfg, const uint32_t *color_buff, uint8_t color_num, uint8_t switch_ration, uint8_t speed, uint8_t bright)
  347. {
  348. if (bright)
  349. {
  350. blink_cfg->set_bright = bright;
  351. //blink_cfg->color_buff = light_color_bright_create(blink_cfg->color_buff, blink_cfg->set_bright);
  352. //U_UART_PRINTF("blink_cfg->color_buff = %X\n", blink_cfg->color_buff);
  353. }
  354. if (switch_ration)
  355. {
  356. blink_cfg->change_cnt = switch_ration;
  357. }
  358. if (speed)
  359. {
  360. light_effect_timer_stop();
  361. blink_cfg->clr_ind = 0;
  362. blink_cfg->play_dir = 0;
  363. blink_cfg->set_speed = speed - 1;
  364. g_light_para.effect = LIGHT_EFFECT_BLINK;
  365. if (blink_cfg->change_cnt < 5)
  366. {
  367. light_effect_timer_start(10 + 9 * blink_cfg->set_speed); //95
  368. }
  369. else
  370. {
  371. light_effect_timer_start( 25 + 35 * blink_cfg->set_speed); //95
  372. }
  373. }
  374. if (color_buff)
  375. {
  376. blink_cfg->clr_ind = 0;
  377. blink_cfg->clr_num_max = color_num;
  378. blink_cfg->color_buff = color_buff;
  379. blink_cfg->play_dir = 0;
  380. light_mode_blink_timer_cb(blink_cfg);
  381. }
  382. }
  383. void light_mode_blink_timer_cb(LightInfo_t *light_blink)
  384. {
  385. //static uint8_t blink_run_cnt = 0;
  386. light_blink->play_dir = ++light_blink->play_dir > 10 ? 1 : light_blink->play_dir;
  387. //light_blink->play_dir =
  388. if (light_blink->play_dir == light_blink->change_cnt)
  389. {
  390. light_set_color(0x00);
  391. light_blink->clr_ind = ++light_blink->clr_ind >= light_blink->clr_num_max ? 0 : light_blink->clr_ind;
  392. }
  393. else if(light_blink->play_dir == 10)
  394. {
  395. light_set_color(light_color_bright_create(light_blink->color_buff[light_blink->clr_ind], light_blink->set_bright));
  396. //U_UART_PRINTF("ind = %d, clr = %X \r\n", light_blink->color_ind, light_blink->color_buff[light_blink->color_ind]);
  397. }
  398. // else
  399. // {
  400. // light_set_color(0x00);
  401. // }
  402. //U_UART_PRINTF("blink_timer_run\n");
  403. }
  404. /********************************* light_mode_blink end ********************************/
  405. //static void rhythm_Color_switch(RhythmParam_t *rhythm_info) //轮询颜色,跳变,呼吸完成后跳下一次颜色
  406. //{
  407. // uint8_t music_mode_ind = rhythm_info->music_mode_ind;//(*rhythm_info->mode & 0x0f) + 10;
  408. //
  409. // rhythm_info->color_ind++;
  410. // if (rhythm_info->color_ind >= rhythm_info->mode_info[music_mode_ind].cnt)
  411. // {
  412. // rhythm_info->color_ind = 0;
  413. // rhythm_info->color_end = 1;
  414. // }
  415. // //U_UART_PRINTF("cnt = %d ,s_color_ind = %d\r\n", rhythm_info->mode_info[music_mode_ind].cnt, rhythm_info->color_ind);
  416. //}
  417. /********************************* light_rhythm_mode_gradual ********************************/
  418. enum _color
  419. {
  420. red,
  421. orange,
  422. yellow,
  423. green,
  424. indigo,
  425. blue,
  426. purple,
  427. white,
  428. };
  429. void light_rhythm_mode_fade_timer_cb(RhythmPara_t *Grad_Cfg) //音乐模式呼吸事件处理,
  430. {
  431. int16_t temp_val;
  432. static uint8_t temp_arr[3];
  433. static uint8_t beyond_arr[3];
  434. static int16_t s_red_val = 0, s_blue_val= 0, s_green_val = 0;
  435. if(!Grad_Cfg->grad_first)
  436. {
  437. Grad_Cfg->grad_first = 1;
  438. clr_format_transition(CLR_TS_TYPE_32_8, 1, temp_arr, (uint32_t *)&Grad_Cfg->clr_buff[Grad_Cfg->clr_ind]);
  439. Grad_Cfg->grad_r_period = temp_arr[0] / Grad_Cfg->speed;
  440. Grad_Cfg->grad_g_period = temp_arr[1] / Grad_Cfg->speed;
  441. Grad_Cfg->grad_b_period = temp_arr[2] / Grad_Cfg->speed;
  442. if(temp_arr[0] == 255)
  443. {
  444. beyond_arr[0] = 5;
  445. Grad_Cfg->grad_refer = red;
  446. }
  447. else
  448. beyond_arr[0] = 0;
  449. if(temp_arr[1] == 255)
  450. {
  451. beyond_arr[1] = 5;
  452. Grad_Cfg->grad_refer = green;
  453. }
  454. else
  455. beyond_arr[1] = 0;
  456. if(temp_arr[2] == 255)
  457. {
  458. beyond_arr[2] = 5;
  459. Grad_Cfg->grad_refer = blue;
  460. }
  461. else
  462. beyond_arr[2] = 0;
  463. s_red_val = beyond_arr[0];
  464. s_green_val = beyond_arr[1];
  465. s_blue_val= beyond_arr[2];
  466. Grad_Cfg->grad_dir = 0;
  467. }
  468. if (Grad_Cfg->music_start)
  469. {
  470. if (!Grad_Cfg->grad_dir)
  471. {
  472. // uart_printf("1111 %d %d %d %d %d\r\n",red_val,green_val,blue_val,Grad_Cfg.grad_refer,Rgb_Para.rgb_color);
  473. s_red_val += Grad_Cfg->grad_r_period;
  474. s_green_val += Grad_Cfg->grad_g_period;
  475. s_blue_val += Grad_Cfg->grad_b_period;
  476. if (s_red_val >= temp_arr[0])
  477. {
  478. s_red_val = temp_arr[0];
  479. }
  480. if(s_green_val >= temp_arr[1])
  481. {
  482. s_green_val = temp_arr[1];
  483. }
  484. if(s_blue_val >= temp_arr[2])
  485. {
  486. s_blue_val = temp_arr[2];
  487. }
  488. if(Grad_Cfg->grad_refer == red && s_red_val >= temp_arr[0])
  489. Grad_Cfg->grad_dir = 1;
  490. if(Grad_Cfg->grad_refer == green && s_green_val >= temp_arr[1])
  491. Grad_Cfg->grad_dir = 1;
  492. if(Grad_Cfg->grad_refer == blue && s_blue_val >= temp_arr[2])
  493. Grad_Cfg->grad_dir = 1;
  494. }
  495. else
  496. {
  497. // uart_printf("2222 %d %d %d %d %d\r\n",red_val,green_val,blue_val,Grad_Cfg.grad_refer,Rgb_Para.rgb_color);
  498. s_red_val -= Grad_Cfg->grad_r_period;
  499. s_green_val -= Grad_Cfg->grad_g_period;
  500. s_blue_val -= Grad_Cfg->grad_b_period;
  501. if(s_red_val <= beyond_arr[0])
  502. s_red_val = beyond_arr[0];
  503. if(s_green_val <= beyond_arr[1])
  504. s_green_val = beyond_arr[1];
  505. if(s_blue_val <= beyond_arr[2])
  506. s_blue_val = beyond_arr[2];
  507. if(Grad_Cfg->grad_refer == red)
  508. temp_val = s_red_val;
  509. else if(Grad_Cfg->grad_refer == green)
  510. temp_val = s_green_val;
  511. else if(Grad_Cfg->grad_refer == blue)
  512. temp_val = s_blue_val;
  513. if(temp_val <= 5)
  514. {
  515. // uart_printf("3333 %d %d %d %d %d\r\n",red_val,green_val,blue_val,Grad_Cfg.grad_refer,Rgb_Para.rgb_color);
  516. Grad_Cfg->grad_dir = 0;
  517. Grad_Cfg->grad_first = 0;
  518. //Grad_Cfg->music_end = 1;
  519. Grad_Cfg->music_start = 0;
  520. if (++Grad_Cfg->clr_ind >= Grad_Cfg->clr_max_num)
  521. {
  522. Grad_Cfg->clr_ind = 0;
  523. }
  524. }
  525. }
  526. }
  527. light_set_color(s_blue_val << 16 | s_red_val << 8 | s_green_val);
  528. // //u_set_color(rhythm_grad->bright, s_red_val, s_green_val, s_blue_val);
  529. // static uint8_t s_printf_count = 0;
  530. // if (++s_printf_count > 50)
  531. // {
  532. // s_printf_count = 0;
  533. // U_UART_PRINTF("music_rgb = %X, %X, %X\r\n", s_red_val, s_green_val, s_blue_val);
  534. // }
  535. }
  536. void light_rhythm_mode_fade_cfg(RhythmPara_t *rhythm_gradual_cfg, const uint32_t *color_buff, uint8_t color_num, uint8_t speed)
  537. {
  538. memset(rhythm_gradual_cfg, 0, sizeof(RhythmPara_t));
  539. rhythm_gradual_cfg->clr_buff = color_buff;
  540. rhythm_gradual_cfg->clr_max_num = color_num;
  541. //rhythm_gradual_cfg->speed = speed * 10;
  542. // rhythm_gradual_cfg->music_start = 1;
  543. // rhythm_gradual_cfg->grad_first = 0;
  544. rhythm_gradual_cfg->speed = 15;
  545. g_light_para.effect = LIGHT_EFFECT_RHYTHM_BREATH;
  546. }
  547. /********************************* light_rhythm_mode_gradual end ********************************/
  548. /********************************* light_rhythm_mode_jump ********************************/
  549. void light_rhythm_mode_jump_timer_cb(RhythmPara_t *rhythm_jump) //跳变模式处理事件,颜色范围由多种,一种颜色跳另一种颜色
  550. {
  551. static uint8_t s_switch_count = 0;
  552. static uint8_t temp_arr[3];
  553. static uint8_t beyond_arr[3];
  554. static int16_t s_red_val = 0, s_blue_val= 0, s_green_val = 0;
  555. s_switch_count++;
  556. if (rhythm_jump->speed == 0)
  557. {
  558. light_set_color(light_color_bright_create((rhythm_jump->clr_buff[rhythm_jump->clr_ind]) , LIGHT_BRIGHT_MIN_MIN ));
  559. //U_UART_PRINTF(" %d %d \r\n",rhythm_jump->clr_buff[rhythm_jump->clr_ind], rhythm_jump->bright);
  560. }
  561. else if(s_switch_count >= rhythm_jump->speed) //判断时间到 切换颜色
  562. {
  563. s_switch_count = 0;
  564. rhythm_jump->clr_ind = ++rhythm_jump->clr_ind % rhythm_jump->clr_max_num;
  565. if (rhythm_jump->bright)
  566. {
  567. light_set_color(rhythm_jump->clr_buff[rhythm_jump->clr_ind]);
  568. }
  569. else
  570. {
  571. light_set_color(NULL);
  572. }
  573. //U_UART_PRINTF("bright = %d music_rgb = %X, %X, %X\r\n",rhythm_jump->bright, music_r, music_g, music_b);
  574. }
  575. //Rgb_Set(&Rgb_Para.color_arr[Rgb_Para.rgb_color *3]);
  576. }
  577. void light_rhythm_mode_jump_cfg(RhythmPara_t *rhythm_jump_cfg, const uint32_t *color_buff, uint8_t color_num, uint8_t speed)
  578. {
  579. memset(rhythm_jump_cfg, 0, sizeof(RhythmPara_t));
  580. rhythm_jump_cfg->clr_buff = color_buff;
  581. rhythm_jump_cfg->clr_max_num = color_num;
  582. rhythm_jump_cfg->speed = speed * 10;
  583. g_light_para.effect = LIGHT_EFFECT_RHYTHM_JUMP;
  584. }
  585. /********************************* light_rhythm_mode_jump end ********************************/
  586. /********************************* light_rhythm_mode_jump ********************************/
  587. void light_rhythm_mode_one_clr_timer_cb(RhythmPara_t *rhythm_one_clr)
  588. {
  589. static uint8_t s_run_count = 3, cd_cnt = 0;
  590. // if (++rhythm_one_clr->grad_first >= rhythm_one_clr->speed)
  591. // {
  592. // rhythm_one_clr->grad_first = 0;
  593. if (++cd_cnt >= 2)
  594. {
  595. cd_cnt = 0;
  596. switch (rhythm_one_clr->clr_ind)
  597. {
  598. case 0: //亮度加
  599. {
  600. s_run_count += rhythm_one_clr->speed;
  601. if (s_run_count > 100)
  602. {
  603. s_run_count = 100;
  604. rhythm_one_clr->clr_ind = 1;
  605. }
  606. light_set_color(light_color_bright_create(rhythm_one_clr->clr_buff[0], s_run_count));
  607. }
  608. break;
  609. case 1: //亮度减
  610. {
  611. s_run_count -= rhythm_one_clr->speed;
  612. if (s_run_count <= 5)
  613. {
  614. s_run_count = 5;
  615. rhythm_one_clr->clr_ind = 2;
  616. }
  617. light_set_color(light_color_bright_create(rhythm_one_clr->clr_buff[0], s_run_count));
  618. }
  619. break;
  620. case 2:
  621. {
  622. if (++rhythm_one_clr->grad_first >= 3 && rhythm_one_clr->bright)
  623. {
  624. rhythm_one_clr->grad_first = 0;
  625. rhythm_one_clr->clr_ind = 0;
  626. }
  627. }
  628. break;
  629. }
  630. }
  631. }
  632. void light_rhythm_mode_one_clr_cfg(RhythmPara_t *one_clr_cfg, const uint32_t *color_buff, uint8_t speed)
  633. {
  634. memset(one_clr_cfg, 0, sizeof(RhythmPara_t));
  635. one_clr_cfg->clr_buff = color_buff;
  636. one_clr_cfg->grad_dir = 0;
  637. one_clr_cfg->bright = LIGHT_BRIGHT_MAX;
  638. one_clr_cfg->clr_ind = 0; //流程
  639. one_clr_cfg->grad_first = 0;
  640. one_clr_cfg->speed = speed * 10;
  641. g_light_para.effect = LIGHT_EFFECT_RHYTHM_ONE_CLR;
  642. }
  643. /********************************* light_rhythm_mode_jump end ********************************/
  644. /****************************** light_rhythm_mode_gradual **********************************/
  645. void light_rhythm_mode_gradual_timer_cb(RhythmPara_t *gradual_rhythm)
  646. {
  647. static uint8_t index = 0;
  648. if (gradual_rhythm->bright <= LIGHT_BRIGHT_MIN)
  649. {
  650. return;
  651. }
  652. if(gradual_rhythm->grad_refer)
  653. {
  654. gradual_rhythm->grad_refer--;
  655. if(gradual_rhythm->color_buff[gradual_rhythm->grad_first][0] < gradual_rhythm->color_buff[index][0])
  656. {
  657. //gradual_rhythm->grad_r_period=(float)(gradual_rhythm->color_buff[index][0] - gradual_rhythm->color_buff[gradual_rhythm->grad_first][0]) / ((gradual_rhythm->change_time*100)/20);
  658. if(gradual_rhythm->grad_refer > 0)
  659. {
  660. gradual_rhythm->clr_temp[0] += gradual_rhythm->grad_r_period;
  661. }
  662. else
  663. {
  664. gradual_rhythm->clr_temp[0] = gradual_rhythm->color_buff[index][0] * 10;
  665. }
  666. }
  667. else if(gradual_rhythm->color_buff[gradual_rhythm->grad_first][0] > gradual_rhythm->color_buff[index][0])
  668. {
  669. //gradual_rhythm->grad_r_period = (float)(gradual_rhythm->color_buff[gradual_rhythm->grad_first][0] - gradual_rhythm->color_buff[index][0]) / ((gradual_rhythm->change_time*100)/20);
  670. if(gradual_rhythm->grad_refer > 0)
  671. {
  672. gradual_rhythm->clr_temp[0] -= gradual_rhythm->grad_r_period;
  673. }
  674. else
  675. {
  676. gradual_rhythm->clr_temp[0] = gradual_rhythm->color_buff[index][0] * 10;
  677. }
  678. }
  679. if(gradual_rhythm->color_buff[gradual_rhythm->grad_first][1] < gradual_rhythm->color_buff[index][1])
  680. {
  681. //gradual_rhythm->grad_g_period=(float)(gradual_rhythm->color_buff[index][1] - gradual_rhythm->color_buff[gradual_rhythm->grad_first][1]) / ((gradual_rhythm->change_time*100)/20);
  682. //U_UART_PRINTF("gradual_rhythm->grad_g_period+ = %d\n", (int)gradual_rhythm->grad_g_period);
  683. if(gradual_rhythm->grad_refer > 0)
  684. {
  685. gradual_rhythm->clr_temp[1] += gradual_rhythm->grad_g_period;
  686. }
  687. else
  688. {
  689. gradual_rhythm->clr_temp[1] = gradual_rhythm->color_buff[index][1] * 10;
  690. }
  691. }
  692. else if(gradual_rhythm->color_buff[gradual_rhythm->grad_first][1] > gradual_rhythm->color_buff[index][1])
  693. {
  694. //gradual_rhythm->grad_g_period=(float)(gradual_rhythm->color_buff[gradual_rhythm->grad_first][1] - gradual_rhythm->color_buff[index][1]) / ((gradual_rhythm->change_time*100)/20);
  695. //U_UART_PRINTF("gradual_rhythm->grad_g_period- = %d\n", (int)gradual_rhythm->grad_g_period);
  696. if(gradual_rhythm->grad_refer > 0)
  697. {
  698. gradual_rhythm->clr_temp[1] -= gradual_rhythm->grad_g_period;
  699. }
  700. else
  701. {
  702. gradual_rhythm->clr_temp[1] = gradual_rhythm->color_buff[index][1] * 10;
  703. }
  704. }
  705. if(gradual_rhythm->color_buff[gradual_rhythm->grad_first][2] < gradual_rhythm->color_buff[index][2])
  706. {
  707. //gradual_rhythm->grad_b_period=(float)(gradual_rhythm->color_buff[index][2] - gradual_rhythm->color_buff[gradual_rhythm->grad_first][2]) / ((gradual_rhythm->change_time*100)/20);
  708. //U_UART_PRINTF("b_i = %d, b_c = %d, gradual_rhythm->grad_b_period+ = %d\n",gradual_rhythm->color_buff[index][2], gradual_rhythm->color_buff[gradual_rhythm->grad_first][2], (int)gradual_rhythm->grad_b_period);
  709. if(gradual_rhythm->grad_refer > 0)
  710. {
  711. gradual_rhythm->clr_temp[2] += gradual_rhythm->grad_b_period;
  712. }
  713. else
  714. {
  715. gradual_rhythm->clr_temp[2] = gradual_rhythm->color_buff[index][2] * 10;
  716. }
  717. }
  718. else if(gradual_rhythm->color_buff[gradual_rhythm->grad_first][2] > gradual_rhythm->color_buff[index][2])
  719. {
  720. //gradual_rhythm->grad_b_period=(float)(gradual_rhythm->color_buff[gradual_rhythm->grad_first][2] - gradual_rhythm->color_buff[index][2]) / ((gradual_rhythm->change_time*100)/20);
  721. //U_UART_PRINTF("b_i = %d, b_c = %d, gradual_rhythm->grad_b_period- = %d\n",gradual_rhythm->color_buff[index][2], gradual_rhythm->color_buff[gradual_rhythm->grad_first][2], (int)gradual_rhythm->grad_b_period);
  722. if(gradual_rhythm->grad_refer > 0)
  723. {
  724. gradual_rhythm->clr_temp[2] -= gradual_rhythm->grad_b_period;
  725. }
  726. else
  727. {
  728. gradual_rhythm->clr_temp[2] = gradual_rhythm->color_buff[index][2] * 10;
  729. }
  730. }
  731. light_set_color(light_color_bright_create((gradual_rhythm->clr_temp[0] / 10) << 16 | (gradual_rhythm->clr_temp[1] / 10) << 8 | (gradual_rhythm->clr_temp[2] / 10) , gradual_rhythm->bright));
  732. }
  733. else
  734. {
  735. // extern uint8_t g_speed;
  736. gradual_rhythm->grad_first = ++gradual_rhythm->grad_first % gradual_rhythm->clr_max_num;
  737. index = (gradual_rhythm->grad_first + 1) % gradual_rhythm->clr_max_num;
  738. gradual_rhythm->grad_refer = 40 - gradual_rhythm->speed * 5;
  739. gradual_rhythm->grad_r_period = u_fabs(gradual_rhythm->color_buff[index][0] , gradual_rhythm->color_buff[gradual_rhythm->grad_first][0]) * 10 / gradual_rhythm->grad_refer;
  740. gradual_rhythm->grad_g_period = u_fabs(gradual_rhythm->color_buff[index][1] , gradual_rhythm->color_buff[gradual_rhythm->grad_first][1]) * 10 / gradual_rhythm->grad_refer;
  741. gradual_rhythm->grad_b_period = u_fabs(gradual_rhythm->color_buff[index][2] , gradual_rhythm->color_buff[gradual_rhythm->grad_first][2]) * 10 / gradual_rhythm->grad_refer;
  742. //U_UART_PRINTF("grad_refer = %d sp = %d, cnt: %d, %d, %d\n",gradual_rhythm->grad_refer, gradual_rhythm->speed, gradual_rhythm->grad_r_period, gradual_rhythm->grad_g_period, gradual_rhythm->grad_b_period);
  743. gradual_rhythm->clr_temp[0] = gradual_rhythm->color_buff[gradual_rhythm->grad_first][0] * 10;
  744. gradual_rhythm->clr_temp[1] = gradual_rhythm->color_buff[gradual_rhythm->grad_first][1] * 10;
  745. gradual_rhythm->clr_temp[2] = gradual_rhythm->color_buff[gradual_rhythm->grad_first][2] * 10;
  746. }
  747. }
  748. void light_rhythm_mode_gradual_cfg(RhythmPara_t *gradual_rhythm_cfg, const uint32_t *color_buff, uint8_t color_num, uint8_t speed)
  749. {
  750. #ifdef DEBUG_DRV_APP
  751. U_UART_PRINTF("r_gradual_clr_num = %d, speed = %d\n", color_num, speed);
  752. #endif
  753. if (color_buff)
  754. {
  755. int i = 0;
  756. memset(gradual_rhythm_cfg, 0, sizeof(RhythmPara_t));
  757. gradual_rhythm_cfg->grad_refer = 0;
  758. gradual_rhythm_cfg->clr_max_num = color_num;
  759. gradual_rhythm_cfg->bright = 100;
  760. gradual_rhythm_cfg->speed = 5;
  761. gradual_rhythm_cfg->clr_buff=app_static_clr;
  762. //gradual_rhythm_cfg->clr_buff 被上面memset清零后内存地址指向了0 防止后面程序调用gradual_rhythm_cfg->clr_buff[0]访问空指针导致异常添加一个静态初值app_static_clr 防止异常死机 2021/11/13 LH加
  763. if (gradual_rhythm_cfg->clr_max_num > GRADUAL_COLOR_NUM_MAX)
  764. {
  765. return;
  766. }
  767. for (i = 0; i < gradual_rhythm_cfg->clr_max_num; i++)
  768. {
  769. gradual_rhythm_cfg->color_buff[i][0] = (color_buff[i] >> 16) & 0xff;
  770. gradual_rhythm_cfg->color_buff[i][1] = (color_buff[i] >> 8) & 0xff;
  771. gradual_rhythm_cfg->color_buff[i][2] = color_buff[i] & 0xff;
  772. }
  773. memset(gradual_rhythm_cfg->clr_temp, 0, 3);
  774. gradual_rhythm_cfg->grad_first = 0;
  775. light_rhythm_mode_gradual_timer_cb(gradual_rhythm_cfg);
  776. light_set_color( light_color_bright_create( (gradual_rhythm_cfg->clr_buff[0]) << 16 | (gradual_rhythm_cfg->clr_buff[0]) << 8 | (gradual_rhythm_cfg->clr_buff[0]) , LIGHT_BRIGHT_MIN_MIN));
  777. //gradual_rhythm_cfg->clr_buff=app_static_clr 实现了切换到音乐模式下时调整到粉红色 亮度最小
  778. }
  779. // if (speed)
  780. // {
  781. // gradual_rhythm_cfg->speed = speed;
  782. // }
  783. }
  784. /**************************** light_rhythm_mode_gradual end ********************************/