u_app_handler.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. #include "u_app_handler.h"
  2. #include "u_main.h"
  3. #include "u_time.h"
  4. #include "u_ble.h"
  5. #include "u_light_ctrl.h"
  6. #include "u_effect_normal.h"
  7. #include "u_effect_color.h"
  8. #include "u_effect_rhythm.h"
  9. #include "user_driver.h"
  10. #include "LH_TaskManager.h"
  11. uint32_t m_clr_play_buff[APP_MODE_CLR_MAX_NUM] = {0x00};
  12. AppModeInfo_t m_app_mode_info[APP_MODE_SCENE_MAX_NUM] = {0x00};
  13. void app_mode_info_init(void)
  14. {
  15. //晚安
  16. m_app_mode_info[0].bright = 5;
  17. m_app_mode_info[0].speed = 1;
  18. m_app_mode_info[0].effect = 0xc0;
  19. // m_app_mode_info[0].last_effect = 1;
  20. m_app_mode_info[0].clr_num = 1;
  21. m_app_mode_info[0].clr_data[0] = 0xff;
  22. m_app_mode_info[0].clr_data[1] = 0x75;
  23. m_app_mode_info[0].clr_data[2] = 0x00;
  24. //阅读
  25. m_app_mode_info[1].bright = 100;
  26. m_app_mode_info[1].speed = 1;
  27. m_app_mode_info[1].effect = 0xc0;
  28. // m_app_mode_info[1].last_effect = 1;
  29. m_app_mode_info[1].clr_num = 1;
  30. m_app_mode_info[1].clr_data[0] = 0xff;
  31. m_app_mode_info[1].clr_data[1] = 0xc4;
  32. m_app_mode_info[1].clr_data[2] = 0x37;
  33. //自定义
  34. m_app_mode_info[2].bright = 50;
  35. m_app_mode_info[2].speed = 5;
  36. m_app_mode_info[2].effect = 0xc1;
  37. // m_app_mode_info[2].last_effect = 1;
  38. m_app_mode_info[2].clr_num = 3;
  39. m_app_mode_info[2].clr_data[0] = 0xff;
  40. m_app_mode_info[2].clr_data[1] = 0x00;
  41. m_app_mode_info[2].clr_data[2] = 0x00;
  42. m_app_mode_info[2].clr_data[3] = 0x2b;
  43. m_app_mode_info[2].clr_data[4] = 0xfc;
  44. m_app_mode_info[2].clr_data[5] = 0x0a;
  45. m_app_mode_info[2].clr_data[6] = 0x02;
  46. m_app_mode_info[2].clr_data[7] = 0x28;
  47. m_app_mode_info[2].clr_data[8] = 0xfe;
  48. //缤纷
  49. m_app_mode_info[3].bright = 100;
  50. m_app_mode_info[3].speed = 7;
  51. m_app_mode_info[3].effect = 0xc1;
  52. // m_app_mode_info[3].last_effect = 1;
  53. m_app_mode_info[3].clr_num = 7;
  54. m_app_mode_info[3].clr_data[0] = 0xff;
  55. m_app_mode_info[3].clr_data[1] = 0x00;
  56. m_app_mode_info[3].clr_data[2] = 0x00;
  57. m_app_mode_info[3].clr_data[3] = 0xfd;
  58. m_app_mode_info[3].clr_data[4] = 0xfd;
  59. m_app_mode_info[3].clr_data[5] = 0x00;
  60. m_app_mode_info[3].clr_data[6] = 0x2b;
  61. m_app_mode_info[3].clr_data[7] = 0xfc;
  62. m_app_mode_info[3].clr_data[8] = 0x0a;
  63. m_app_mode_info[3].clr_data[9] = 0x02;
  64. m_app_mode_info[3].clr_data[10] = 0xf9;
  65. m_app_mode_info[3].clr_data[11] = 0xfe;
  66. m_app_mode_info[3].clr_data[12] = 0x02;
  67. m_app_mode_info[3].clr_data[13] = 0x28;
  68. m_app_mode_info[3].clr_data[14] = 0xfe;
  69. m_app_mode_info[3].clr_data[15] = 0x93;
  70. m_app_mode_info[3].clr_data[16] = 0x0a;
  71. m_app_mode_info[3].clr_data[17] = 0xfc;
  72. m_app_mode_info[3].clr_data[18] = 0xff;
  73. m_app_mode_info[3].clr_data[19] = 0xff;
  74. m_app_mode_info[3].clr_data[20] = 0xff;
  75. // //炫彩
  76. m_app_mode_info[4].bright = 100;
  77. m_app_mode_info[4].speed = 7;
  78. m_app_mode_info[4].effect = 0xc1;
  79. // m_app_mode_info[4].last_effect = 1;
  80. m_app_mode_info[4].clr_num = 3;
  81. m_app_mode_info[4].clr_data[0] = 0xff;
  82. m_app_mode_info[4].clr_data[1] = 0x00;
  83. m_app_mode_info[4].clr_data[2] = 0x00;
  84. m_app_mode_info[4].clr_data[3] = 0x2b;
  85. m_app_mode_info[4].clr_data[4] = 0xfc;
  86. m_app_mode_info[4].clr_data[5] = 0x0a;
  87. m_app_mode_info[4].clr_data[6] = 0x02;
  88. m_app_mode_info[4].clr_data[7] = 0x28;
  89. m_app_mode_info[4].clr_data[8] = 0xfe;
  90. //斑斓
  91. m_app_mode_info[5].bright = 100;
  92. m_app_mode_info[5].speed = 5;
  93. m_app_mode_info[5].effect = 0xc2;
  94. // m_app_mode_info[5].last_effect = 2;
  95. m_app_mode_info[5].clr_num = 7;
  96. m_app_mode_info[5].clr_data[0] = 0xff;
  97. m_app_mode_info[5].clr_data[1] = 0x00;
  98. m_app_mode_info[5].clr_data[2] = 0x00;
  99. m_app_mode_info[5].clr_data[3] = 0xfd;
  100. m_app_mode_info[5].clr_data[4] = 0xfd;
  101. m_app_mode_info[5].clr_data[5] = 0x00;
  102. m_app_mode_info[5].clr_data[6] = 0x2b;
  103. m_app_mode_info[5].clr_data[7] = 0xfc;
  104. m_app_mode_info[5].clr_data[8] = 0x0a;
  105. m_app_mode_info[5].clr_data[9] = 0x02;
  106. m_app_mode_info[5].clr_data[10] = 0xf9;
  107. m_app_mode_info[5].clr_data[11] = 0xfe;
  108. m_app_mode_info[5].clr_data[12] = 0x02;
  109. m_app_mode_info[5].clr_data[13] = 0x28;
  110. m_app_mode_info[5].clr_data[14] = 0xfe;
  111. m_app_mode_info[5].clr_data[15] = 0x93;
  112. m_app_mode_info[5].clr_data[16] = 0x0a;
  113. m_app_mode_info[5].clr_data[17] = 0xfc;
  114. m_app_mode_info[5].clr_data[18] = 0xff;
  115. m_app_mode_info[5].clr_data[19] = 0xff;
  116. m_app_mode_info[5].clr_data[20] = 0xff;
  117. //火焰
  118. m_app_mode_info[6].bright = 100;
  119. m_app_mode_info[6].speed = 2;
  120. m_app_mode_info[6].effect = 0xc2;
  121. // m_app_mode_info[6].last_effect = 1;
  122. m_app_mode_info[6].clr_num = 1;
  123. m_app_mode_info[6].clr_data[0] = 0xff;
  124. m_app_mode_info[6].clr_data[1] = 0x00;
  125. m_app_mode_info[6].clr_data[2] = 0x00;
  126. //闪动
  127. m_app_mode_info[7].bright = 100;
  128. m_app_mode_info[7].speed = 2;
  129. m_app_mode_info[7].effect = 0xc1;
  130. // m_app_mode_info[7].last_effect = 1;
  131. m_app_mode_info[7].clr_num = 3;
  132. m_app_mode_info[7].clr_data[0] = 0xff;
  133. m_app_mode_info[7].clr_data[1] = 0x00;
  134. m_app_mode_info[7].clr_data[2] = 0x00;
  135. m_app_mode_info[7].clr_data[3] = 0x2b;
  136. m_app_mode_info[7].clr_data[4] = 0xfc;
  137. m_app_mode_info[7].clr_data[5] = 0x0a;
  138. m_app_mode_info[7].clr_data[6] = 0x02;
  139. m_app_mode_info[7].clr_data[7] = 0x28;
  140. m_app_mode_info[7].clr_data[8] = 0xfe;
  141. //灯光秀
  142. m_app_mode_info[8].bright = 100;
  143. m_app_mode_info[8].speed = 2;
  144. m_app_mode_info[8].effect = 0xc1;
  145. // m_app_mode_info[8].last_effect = 1;
  146. m_app_mode_info[8].clr_num = 7;
  147. m_app_mode_info[8].clr_data[0] = 0xff;
  148. m_app_mode_info[8].clr_data[1] = 0x00;
  149. m_app_mode_info[8].clr_data[2] = 0x00;
  150. m_app_mode_info[8].clr_data[3] = 0xfd;
  151. m_app_mode_info[8].clr_data[4] = 0xfd;
  152. m_app_mode_info[8].clr_data[5] = 0x00;
  153. m_app_mode_info[8].clr_data[6] = 0x2b;
  154. m_app_mode_info[8].clr_data[7] = 0xfc;
  155. m_app_mode_info[8].clr_data[8] = 0x0a;
  156. m_app_mode_info[8].clr_data[9] = 0x02;
  157. m_app_mode_info[8].clr_data[10] = 0xf9;
  158. m_app_mode_info[8].clr_data[11] = 0xfe;
  159. m_app_mode_info[8].clr_data[12] = 0x02;
  160. m_app_mode_info[8].clr_data[13] = 0x28;
  161. m_app_mode_info[8].clr_data[14] = 0xfe;
  162. m_app_mode_info[8].clr_data[15] = 0x93;
  163. m_app_mode_info[8].clr_data[16] = 0x0a;
  164. m_app_mode_info[8].clr_data[17] = 0xfc;
  165. m_app_mode_info[8].clr_data[18] = 0xff;
  166. m_app_mode_info[8].clr_data[19] = 0xff;
  167. m_app_mode_info[8].clr_data[20] = 0xff;
  168. }
  169. void light_state_upload(void) //回发灯的状态给app
  170. {
  171. uint8_t upload_buff[14] = {0};
  172. if(g_light_para.power_st == GLO_EN) //灯当前是否开关
  173. {
  174. upload_buff[0] = 1;
  175. }
  176. else
  177. {
  178. upload_buff[0] = 0;
  179. }
  180. if (g_light_para.app_mode[0] >= LIGHT_APP_MODE_RC_MIN)
  181. {
  182. upload_buff[1] = LIGHT_APP_MODE_COLORUR;
  183. }
  184. else
  185. {
  186. upload_buff[1] = g_light_para.app_mode[0]; //灯当前的模式
  187. upload_buff[2] = g_light_para.app_mode[1];
  188. }
  189. upload_buff[3] = g_light_para.bright; //灯当前的亮度
  190. upload_buff[4] = g_light_para.app_speed;
  191. upload_buff[5] = 100 - g_light_para.rhythm_sensit * APP_SENSIT_K;
  192. upload_buff[6] = g_light_para.clr_temp[0];
  193. upload_buff[7] = g_light_para.clr_temp[1];
  194. upload_buff[8] = g_light_para.clr_temp[2];
  195. upload_buff[9] = g_light_para.clr_temp[3];
  196. upload_buff[10] = g_light_para.rgb_order[0];
  197. upload_buff[11] = g_light_para.rgb_order[1];
  198. upload_buff[12] = g_light_para.rgb_order[2];
  199. if( g_light_para.mic==0)
  200. {
  201. upload_buff[13] = 0x00; //区分带麦
  202. #ifdef DEBUG_DRV_APP
  203. U_UART_PRINTF("no mic");
  204. U_UART_PRINTF("\r\n");
  205. #endif
  206. }else
  207. {
  208. upload_buff[13] = 0x01; //区分带麦
  209. #ifdef DEBUG_DRV_APP
  210. U_UART_PRINTF("have mic");
  211. U_UART_PRINTF("\r\n");
  212. #endif
  213. }
  214. //UART_PRINTF("user_send_light_state %d %d %d\r\n",Rgb_Para.color_arr[10],Rgb_Para.color_arr[11],Rgb_Para.color_arr[12]);
  215. reply_app_encode(0x10, upload_buff, 14);
  216. }
  217. static void app_light_colour_enter(const uint32_t *static_clr, uint8_t static_bright)
  218. {
  219. u_rhythm_timer_ctrl(GLO_DIS);
  220. light_mode_static_cfg(&g_light_info, static_clr, static_bright);
  221. }
  222. static void app_rhythm_enter(uint8_t *app_mode)
  223. {
  224. if (app_mode[0] == APP_RHYTHM_MODE_MUSIC)
  225. {
  226. g_light_para.mic_AppOrDriver=0;
  227. light_rhythm_enter(RHYTHM_MODE_GRADUAL_7);
  228. }
  229. else
  230. {
  231. switch (app_mode[1])
  232. {
  233. case APP_RHYTHM_MODE_FADE3:
  234. {
  235. light_rhythm_enter(RHYTHM_MODE_FADE_3);
  236. }
  237. break;
  238. case APP_RHYTHM_MODE_FADE7:
  239. {
  240. light_rhythm_enter(RHYTHM_MODE_FADE_7);
  241. }
  242. break;
  243. case APP_RHYTHM_MODE_JUMP3:
  244. {
  245. light_rhythm_enter(RHYTHM_MODE_JUMP_3);
  246. }
  247. break;
  248. case APP_RHYTHM_MODE_JUMP7:
  249. {
  250. light_rhythm_enter(RHYTHM_MODE_JUMP_7);
  251. }
  252. break;
  253. }
  254. }
  255. }
  256. void light_mode_set(uint8_t *app_mode)
  257. {
  258. uint8_t app_mode_info_ind = 0;
  259. memcpy(g_light_para.app_mode, app_mode, 2);
  260. switch (g_light_para.app_mode[0])
  261. {
  262. case LIGHT_APP_MODE_WHITE_LIGHT:
  263. case LIGHT_APP_MODE_COLORUR:
  264. {
  265. clr_format_transition(CLR_TS_TYPE_8_32, 1, g_light_para.clr_temp, m_clr_play_buff);
  266. app_light_colour_enter(m_clr_play_buff, g_light_para.bright);
  267. }
  268. break;
  269. case LIGHT_APP_MODE_S_RED:
  270. case LIGHT_APP_MODE_S_BLUE:
  271. case LIGHT_APP_MODE_S_GREEN:
  272. case LIGHT_APP_MODE_S_CYAN:
  273. case LIGHT_APP_MODE_S_YELLOW:
  274. case LIGHT_APP_MODE_S_PURPLE:
  275. case LIGHT_APP_MODE_S_WHITE:
  276. {
  277. uint8_t clr_ind = g_light_para.app_mode[0] - LIGHT_APP_MODE_S_RED;
  278. light_mode_static_cfg(&g_light_info, &app_static_clr[clr_ind], g_light_para.bright);
  279. }
  280. break;
  281. case LIGHT_APP_MODE_JUMP_3:
  282. {
  283. light_mode_jump_cfg(&g_light_info, base_clr, 3, g_light_para.speed, g_light_para.bright);
  284. }
  285. break;
  286. case LIGHT_APP_MODE_JUMP_7:
  287. {
  288. light_mode_jump_cfg(&g_light_info, base_clr, 7, g_light_para.speed, g_light_para.bright);
  289. }
  290. break;
  291. case LIGHT_APP_MODE_FADE_3:
  292. {
  293. light_mode_gradual_cfg(&g_light_info, base_clr, 3, g_light_para.speed, g_light_para.bright);
  294. }
  295. break;
  296. case LIGHT_APP_MODE_FADE_7:
  297. {
  298. light_mode_gradual_cfg(&g_light_info, base_clr, 7, g_light_para.speed, g_light_para.bright);
  299. }
  300. break;
  301. case LIGHT_APP_MODE_BREATH_RED:
  302. case LIGHT_APP_MODE_BREATH_BLUE:
  303. case LIGHT_APP_MODE_BREATH_GREEN:
  304. case LIGHT_APP_MODE_BREATH_YELLOW:
  305. case LIGHT_APP_MODE_BREATH_CYAN:
  306. case LIGHT_APP_MODE_BREATH_PURPLE:
  307. case LIGHT_APP_MODE_BREATH_WHITE:
  308. {
  309. uint8_t clr_ind = g_light_para.app_mode[0] - LIGHT_APP_MODE_BREATH_RED;
  310. light_mode_breath_cfg(&g_light_info, &base_clr[clr_ind], 1, g_light_para.speed, g_light_para.bright);
  311. }
  312. break;
  313. case LIGHT_APP_MODE_FADE_RG:
  314. {
  315. light_mode_gradual_cfg(&g_light_info, base_clr, 2, g_light_para.speed, g_light_para.bright);
  316. }
  317. break;
  318. case LIGHT_APP_MODE_FADE_RB:
  319. {
  320. light_mode_gradual_cfg(&g_light_info, app_static_clr, 2, g_light_para.speed, g_light_para.bright);
  321. }
  322. break;
  323. case LIGHT_APP_MODE_FADE_GB:
  324. {
  325. light_mode_gradual_cfg(&g_light_info, &base_clr[1], 2, g_light_para.speed, g_light_para.bright);
  326. }
  327. break;
  328. case LIGHT_APP_MODE_BLINK_7:
  329. {
  330. light_mode_blink_cfg(&g_light_info, base_clr, 7, 5, g_light_para.speed, g_light_para.bright);
  331. }
  332. break;
  333. case LIGHT_APP_MODE_BLINK_RED:
  334. case LIGHT_APP_MODE_BLINK_GREEN:
  335. case LIGHT_APP_MODE_BLINK_BLUE:
  336. case LIGHT_APP_MODE_BLINK_YELLOW:
  337. case LIGHT_APP_MODE_BLINK_CYAN:
  338. case LIGHT_APP_MODE_BLINK_PURPLE:
  339. case LIGHT_APP_MODE_BLINK_WHITE:
  340. {
  341. uint8_t clr_ind = g_light_para.app_mode[0] - LIGHT_APP_MODE_BLINK_RED;
  342. light_mode_blink_cfg(&g_light_info, &base_clr[clr_ind], 1, 5, g_light_para.speed, g_light_para.bright);
  343. }
  344. break;
  345. case LIGHT_APP_MODE_RHYTHM_APP: //手机麦
  346. {
  347. if(g_light_para.mic == 1)
  348. {
  349. g_light_para.mic_AppOrDriver = 0;
  350. }else if(g_light_para.mic == 0)
  351. {
  352. g_light_para.mic_AppOrDriver = 0;
  353. }
  354. app_rhythm_enter(app_mode);
  355. }
  356. break;
  357. case LIGHT_APP_MODE_RHYTHM_DRIVER: //外部麦
  358. {
  359. if(g_light_para.mic == 1)
  360. {
  361. g_light_para.mic_AppOrDriver = 1;
  362. app_rhythm_enter(app_mode);
  363. if(g_light_para.rhythm_mode!=RHYTHM_MODE_STOP)//增加对律动保存功能支持 2021/10/12 liuhao
  364. {
  365. u_rhythm_timer_ctrl(GLO_EN);
  366. }
  367. }else if(g_light_para.mic == 0)
  368. {
  369. g_light_para.mic_AppOrDriver=0;
  370. }
  371. //app_rhythm_enter(app_mode);
  372. }
  373. break;
  374. case LIGHT_APP_MODE_RHYTHM_MUSIC: //律动
  375. {
  376. g_light_para.mic_AppOrDriver = 0;
  377. app_rhythm_enter(app_mode);
  378. }
  379. break;
  380. case LIGHT_APP_MODE_GOODNIGHT :
  381. case LIGHT_APP_MODE_READ :
  382. case LIGHT_APP_MODE_WORD :
  383. case LIGHT_APP_MODE_COLORFUL :
  384. case LIGHT_APP_MODE_DAZZLING :
  385. case LIGHT_APP_MODE_MULT :
  386. case LIGHT_APP_MODE_FLAME :
  387. case LIGHT_APP_MODE_BLINK :
  388. case LIGHT_APP_MODE_SHOW :
  389. {
  390. app_mode_info_ind = g_light_para.app_mode[0] - LIGHT_APP_MODE_SCENE_MIN;
  391. // if(app_mode[1] == 0xFF)
  392. // {
  393. // g_light_para.app_mode[1] = m_app_mode_info[app_mode_info_ind].effect;
  394. // U_UART_PRINTF("app_mode[1]=%x \r\n",g_light_para.app_mode[1]);
  395. // }else
  396. // {
  397. // m_app_mode_info[app_mode_info_ind].effect = g_light_para.app_mode[1];
  398. // }
  399. if(g_light_para.app_mode[0] == 0xC2)
  400. {
  401. if(app_mode[1] == 0xFF)
  402. {
  403. // g_light_para.app_mode[1] = m_app_mode_info[app_mode_info_ind].effect;
  404. // U_UART_PRINTF("app_mode[1]=%x \r\n",g_light_para.app_mode[1]);
  405. }else
  406. {
  407. m_app_mode_info[app_mode_info_ind].effect = g_light_para.app_mode[1];
  408. }
  409. m_app_mode_info[app_mode_info_ind].speed = g_light_para.speed;
  410. m_app_mode_info[app_mode_info_ind].bright = g_light_para.bright;
  411. }
  412. g_light_para.app_mode[1] = m_app_mode_info[app_mode_info_ind].effect;
  413. clr_format_transition(CLR_TS_TYPE_8_32, m_app_mode_info[app_mode_info_ind].clr_num, m_app_mode_info[app_mode_info_ind].clr_data, m_clr_play_buff);
  414. app_light_normal_effect_enter(m_app_mode_info[app_mode_info_ind].effect, m_clr_play_buff, m_app_mode_info[app_mode_info_ind].clr_num, m_app_mode_info[app_mode_info_ind].speed, m_app_mode_info[app_mode_info_ind].bright);
  415. // app_light_normal_effect_enter(g_light_para.app_mode[1], m_clr_play_buff, m_app_mode_info[app_mode_info_ind].clr_num, g_light_para.speed,g_light_para.bright);
  416. if(app_mode[1] == 0xFF)
  417. {
  418. light_state_upload();
  419. }
  420. }
  421. break;
  422. }
  423. }
  424. void app_light_normal_effect_enter(uint8_t app_effect, uint32_t *clr_buff, uint8_t clr_num, uint8_t speed, uint8_t bright)
  425. {
  426. switch(app_effect) //灯的效果
  427. {
  428. case LIGHT_APP_EFFECT_STATIC: //静态模式
  429. {
  430. light_mode_static_cfg(&g_light_info, clr_buff, bright);
  431. }
  432. break;
  433. case LIGHT_APP_EFFECT_JUMP:
  434. {
  435. light_mode_jump_cfg(&g_light_info, clr_buff, clr_num, speed, bright);
  436. }
  437. break;
  438. case LIGHT_APP_EFFECT_GRADUAL:
  439. {
  440. light_mode_gradual_cfg(&g_light_info, clr_buff, clr_num, speed, bright);
  441. }
  442. break;
  443. }
  444. }
  445. void app_read_mode_clr(uint8_t mode,uint8_t *reply_buff, uint8_t *len)
  446. {
  447. uint32_t i = 0;
  448. reply_buff[0] = mode;//g_light_para.app_mode[1];
  449. if (mode >= LIGHT_APP_MODE_SCENE_MIN && mode <= LIGHT_APP_MODE_SCENE_MAX)
  450. {
  451. uint8_t app_mode_info_ind = mode - LIGHT_APP_MODE_SCENE_MIN;
  452. if (m_app_mode_info[app_mode_info_ind].clr_num > APP_MODE_CLR_MAX_NUM)
  453. {
  454. m_app_mode_info[app_mode_info_ind].clr_num = APP_MODE_CLR_MAX_NUM;
  455. }
  456. for(i = 0; i < m_app_mode_info[app_mode_info_ind].clr_num; i++)
  457. {
  458. reply_buff[i*4+1] = m_app_mode_info[app_mode_info_ind].clr_data[i*3+0];
  459. reply_buff[i*4+2] = m_app_mode_info[app_mode_info_ind].clr_data[i*3+1];
  460. reply_buff[i*4+3] = m_app_mode_info[app_mode_info_ind].clr_data[i*3+2];
  461. reply_buff[i*4+4] = 0x65;
  462. // U_UART_PRINTF("i: %d, %d,%d,%d \r\n", i, m_app_mode_info[app_mode_info_ind].clr_data[i*3+0],m_app_mode_info[app_mode_info_ind].clr_data[i*3+1],m_app_mode_info[app_mode_info_ind].clr_data[i*3+2]);
  463. }
  464. *len = m_app_mode_info[app_mode_info_ind].clr_num * 4 + 1;
  465. // U_UART_PRINTF("app_mode_info_ind: %d\r\n", app_mode_info_ind);
  466. }
  467. else
  468. {
  469. *len = 1;
  470. }
  471. }
  472. void app_set_mode_clr(uint8_t *data, uint8_t len)
  473. {
  474. uint8_t j= 0, k = 0;
  475. uint8_t app_mode_info_ind = data[0] - LIGHT_APP_MODE_SCENE_MIN;
  476. uint32_t i = 0;
  477. len = (len - 4) / 4; //有效组数
  478. // if(len>=APP_MODE_CLR_MAX_NUM)
  479. // {
  480. // len=APP_MODE_CLR_MAX_NUM;
  481. // }
  482. #ifdef DEBUG_DRV_APP
  483. U_UART_PRINTF("len: %d\r\n", len);
  484. #endif
  485. m_app_mode_info[app_mode_info_ind].clr_num = 0;
  486. for(i = 0; i <= len; i++)
  487. {
  488. m_app_mode_info[app_mode_info_ind].clr_data[k] = data[j + 1];
  489. m_app_mode_info[app_mode_info_ind].clr_data[k + 1] = data[j + 2];
  490. m_app_mode_info[app_mode_info_ind].clr_data[k + 2] = data[j + 3];
  491. // U_UART_PRINTF("[%d]: %d, [%d]:%d, [%d]:%d",k,m_app_mode_info[app_mode_info_ind].clr_data[k] ,k+1,m_app_mode_info[app_mode_info_ind].clr_data[k+1],k+2,m_app_mode_info[app_mode_info_ind].clr_data[k+2]);
  492. // U_UART_PRINTF(" CC: %d, %d,%d,%d,%d\r\n", m_app_mode_info[app_mode_info_ind].clr_num, data[j+1],data[j+2],data[j+3],data[j+4]);
  493. m_app_mode_info[app_mode_info_ind].clr_num++;
  494. j += 4;
  495. k += 3;
  496. }
  497. clr_format_transition(CLR_TS_TYPE_8_32, m_app_mode_info[app_mode_info_ind].clr_num, m_app_mode_info[app_mode_info_ind].clr_data, m_clr_play_buff);
  498. app_light_normal_effect_enter(m_app_mode_info[app_mode_info_ind].effect, m_clr_play_buff, m_app_mode_info[app_mode_info_ind].clr_num, g_light_para.speed, g_light_para.bright);
  499. // U_UART_PRINTF("app_mode_info_ind: %d\r\n", app_mode_info_ind);
  500. }
  501. static void app_mode_enter(uint8_t *app_mode)
  502. {
  503. light_mode_set(app_mode);
  504. }
  505. void reply_app_encode(uint8_t cmd, uint8_t *data, uint8_t len) //回发app解码数据
  506. {
  507. static uint8_t send[64]={0};
  508. uint8_t i=0;
  509. uint16_t crc=0;
  510. send[0] = 0xA1;
  511. send[1] = cmd;
  512. send[2] = len + 3;
  513. for(i = 0; i<len; i++)
  514. {
  515. send[3+i] = data[i];
  516. }
  517. crc = crc_16(send, len +3);
  518. send[3+len] = crc & 0xff;
  519. send[4+len] = crc >> 8;
  520. //UART_PRINTF("crc: %x\r\n", crc);
  521. #ifdef DEBUG_DRV_APP
  522. U_UART_PRINTF("tx:");
  523. for(i = 0; i < len + 5; i++)
  524. {
  525. U_UART_PRINTF("%02X ", send[i]);
  526. }
  527. U_UART_PRINTF("\r\n");
  528. #endif
  529. u_ble_data_send(send, len + 5);
  530. }
  531. //app下发数据的处理函数
  532. void decode_app_cmd(uint8_t cmd, uint8_t *data, uint8_t len)
  533. {
  534. static uint8_t xx = 0, yy = 0;
  535. uint8_t reply_state;
  536. #if 0
  537. //UART_PRINTF("app cmd: 0x%x, %d\r\n", cmd, len);
  538. UART_PRINTF("app cmd: %02x, %02x %02x\r\n",data[0],data[1],data[2]);
  539. #endif
  540. switch(cmd)
  541. {
  542. case 0x00: //握手指令
  543. {
  544. if(memcmp(&data[0], "CCHIP", 5) == 0)
  545. {
  546. uint8_t str[7] = "CCHIP";
  547. xx = HW_Get_Native_Clk_Avoid_Race() % 8;
  548. yy = HW_Get_Native_Clk_Avoid_Race() /100 % 8;
  549. str[5] = xx;
  550. str[6] = yy;
  551. reply_app_encode(0x00, str, 7);
  552. #ifdef DEBUG_DRV_APP
  553. U_UART_PRINTF("check device1: %02x,%02x\r\n", xx, yy);
  554. #endif
  555. }
  556. }
  557. break;
  558. case 0x01: //握手指令
  559. {
  560. if(memcmp(&data[0], "CCHIP", 5) == 0)
  561. {
  562. uint8_t send[6] = "CCHIP";
  563. //U_UART_PRINTF("check device2: %02x,%02x, %02x,%02x\r\n", (xx^36),data[5],(yy^63+13)&0xff,data[6]);
  564. if((((xx^36) &0xff) == data[5]) && (((yy^63+13) &0xff) == data[6]))
  565. {
  566. send[5] = 0x00;
  567. reply_app_encode(0x01, send, 6);
  568. //user_connect();
  569. #ifdef DEBUG_DRV_APP
  570. U_UART_PRINTF("check ok\r\n");
  571. #endif
  572. }
  573. else
  574. {
  575. send[5] = 0x01;
  576. reply_app_encode(0x01, send, 6);
  577. #ifdef DEBUG_DRV_APP
  578. U_UART_PRINTF("check error\r\n");
  579. #endif
  580. }
  581. }
  582. }
  583. break;
  584. case 0x10: //读取灯属性的指令
  585. {
  586. light_state_upload();
  587. }
  588. break;
  589. case 0x11: //设置灯开关的指令
  590. {
  591. #ifdef DEBUG_DRV_APP
  592. U_UART_PRINTF("light_power_st = %d\n", data[0]);
  593. #endif
  594. light_power_set(data[0]);
  595. reply_state = 1;
  596. reply_app_encode(cmd, &reply_state, 1);
  597. }
  598. break;
  599. case 0x12: //设置灯模式
  600. {
  601. reply_state = 1;
  602. reply_app_encode(cmd, &reply_state, 1);
  603. #ifdef DEBUG_DRV_APP
  604. U_UART_PRINTF("\r\n app_mode = %X, app_effect = %X\r\n",data[0], data[1]);
  605. #endif
  606. app_mode_enter(data);
  607. light_power_set(g_light_para.power_st);
  608. }
  609. break;
  610. case 0x13: //设置灯亮度
  611. {
  612. //data[0] bright
  613. light_bright_ctrl(data[0]);
  614. reply_state = 1;
  615. reply_app_encode(cmd, &reply_state, 1);
  616. }
  617. break;
  618. case 0x15: //设置灯颜色
  619. {
  620. memcpy(g_light_para.clr_temp, &data[0], 4);
  621. reply_state = 0x01;
  622. reply_app_encode(cmd, &reply_state, 1);
  623. clr_format_transition(CLR_TS_TYPE_8_32, 1, g_light_para.clr_temp, m_clr_play_buff);
  624. app_light_colour_enter(m_clr_play_buff, g_light_para.bright);
  625. g_light_para.app_mode[0] = LIGHT_APP_MODE_COLORUR;
  626. light_power_set(g_light_para.power_st);
  627. }
  628. break;
  629. case 0x16: //设置灯速度
  630. {
  631. //data[0] speed
  632. light_speed_ctrl(data[0]);
  633. reply_state = 1;
  634. reply_app_encode(cmd, &reply_state, 1);
  635. }
  636. break;
  637. case 0x17: //设置律动灵敏度
  638. {
  639. rhythm_sensit_ctrl(NULL, data[0]);
  640. reply_state = 1;
  641. reply_app_encode(cmd, &reply_state, 1);
  642. }
  643. break;
  644. case 0x18: //设置RGB线序
  645. {
  646. memcpy(g_light_para.rgb_order, data, 3);
  647. rgb_line_order_check();
  648. light_power_set(g_light_para.power_st);
  649. reply_state = 1;
  650. reply_app_encode(cmd, &reply_state, 1);
  651. }
  652. break;
  653. case 0x19: //设置白光
  654. {
  655. reply_state = 1;
  656. reply_app_encode(cmd, &reply_state, 1);
  657. }
  658. break;
  659. case 0x1A: //设置律动 颜色&亮度
  660. {
  661. }
  662. break;
  663. case 0x20: //白光颜色&色温
  664. {
  665. memcpy(g_light_para.clr_temp, data, 4);
  666. reply_state = 1;
  667. reply_app_encode(cmd, &reply_state, 1);
  668. clr_format_transition(CLR_TS_TYPE_8_32, 1, g_light_para.clr_temp, m_clr_play_buff);
  669. app_light_colour_enter(m_clr_play_buff, g_light_para.bright);
  670. g_light_para.app_mode[0] = LIGHT_APP_MODE_COLORUR;
  671. }
  672. break;
  673. case 0x21: //律动开始&暂停
  674. {
  675. u_rhythm_timer_ctrl(data[0]);
  676. reply_state = 1;
  677. reply_app_encode(cmd, &reply_state, 1);
  678. }
  679. break;
  680. case 0x28: //读取设置
  681. {
  682. uint8_t reply_buff[2] = {0x00};
  683. reply_buff[0] = g_light_para.app_seting >> 4;
  684. reply_buff[1] = g_light_para.app_seting & 0x0F;
  685. reply_app_encode(cmd, reply_buff, 2);
  686. }
  687. break;
  688. case 0x29: //设置连接状态
  689. {
  690. g_light_para.app_seting = (data[0] << 4) | (g_light_para.app_seting & 0x0F);
  691. reply_state = 0x01;
  692. reply_app_encode(cmd, &reply_state, 1);
  693. }
  694. break;
  695. case 0x2A: //设置断开状态
  696. {
  697. g_light_para.app_seting = (g_light_para.app_seting & 0xF0) | (data[0] & 0x0F);
  698. reply_state = 0x01;
  699. reply_app_encode(cmd, &reply_state, 1);
  700. }
  701. break;
  702. case 0x31: //同步当前的时间
  703. {
  704. time_app_driver_sync(data);
  705. reply_state = 1;
  706. reply_app_encode(cmd, &reply_state, 1);
  707. }
  708. break;
  709. case 0x34: //闹钟的设置
  710. {
  711. time_alarm_set(data);
  712. reply_state = 1;
  713. reply_app_encode(cmd, &reply_state, 1);
  714. }
  715. break;
  716. case 0x35: //读取闹钟的状态
  717. {
  718. uint8_t reply_len = 0, alarm_st[24] = {0x00};
  719. time_alarm_state_query(alarm_st, &reply_len);
  720. reply_app_encode(cmd, alarm_st, reply_len);
  721. }
  722. break;
  723. case 0x51: //律动值
  724. {
  725. app_rhythm_val_up(data[0]);
  726. reply_state = 1;
  727. reply_app_encode(cmd, &reply_state, 1);
  728. }
  729. // case 0x52: // 上报律动开始/停止
  730. // {
  731. //
  732. //
  733. // reply_state = 1;
  734. // reply_app_encode(cmd, &reply_state, 1);
  735. // }
  736. break;
  737. case 0x41: //读取版本号
  738. {
  739. uint8_t temp[] = "1.1.1";
  740. reply_app_encode(0x41, temp, sizeof(temp) - 1);
  741. }
  742. break;
  743. case 0xA1: //设置某模式颜色
  744. {
  745. uint8_t bao_len = data[1] - 1 ,temp_len = 0,i;
  746. static uint8_t mode_set_buff[5] = {0x00};
  747. static uint8_t mode_clr_set_buff[40] = {0x00};
  748. static uint8_t clr_buff_len = 0;
  749. if(data[2]==0) //第一包数据
  750. {
  751. mode_set_buff[0] = data[3];
  752. mode_set_buff[1] = g_light_para.app_mode[1];
  753. mode_clr_set_buff[0] = data[3];
  754. clr_buff_len = len - 4 - 3;
  755. memcpy(&mode_clr_set_buff[1], &data[4], clr_buff_len);
  756. if( bao_len == data[2])
  757. {
  758. light_mode_set(mode_set_buff);
  759. app_set_mode_clr(mode_clr_set_buff, clr_buff_len );
  760. }
  761. }else if(data[2] == 1)
  762. {
  763. temp_len = len - 3 -3;
  764. clr_buff_len = clr_buff_len + temp_len;
  765. memcpy(&mode_clr_set_buff[12], &data[3], temp_len);
  766. if( bao_len == data[2] )
  767. {
  768. light_mode_set(mode_set_buff);
  769. app_set_mode_clr(mode_clr_set_buff, clr_buff_len);
  770. }
  771. }else if(data[2] == 2)
  772. {
  773. temp_len = len - 3 - 3;
  774. clr_buff_len = clr_buff_len+temp_len;
  775. memcpy(&mode_clr_set_buff[24], &data[3], temp_len);
  776. if( bao_len == data[2] )
  777. {
  778. light_mode_set(mode_set_buff);
  779. app_set_mode_clr(mode_clr_set_buff, clr_buff_len);
  780. }
  781. }
  782. reply_state = 0x01;
  783. reply_app_encode(cmd, &reply_state, 1);
  784. }
  785. break;
  786. case 0xA3: //设置群组 模式-灯效-亮度-速度-色值
  787. {
  788. uint8_t i=0;
  789. uint8_t bao_len = data[1] - 1 , temp_len = 0;
  790. static uint8_t mode_set_buff[5] = {0x00};
  791. static uint8_t mode_clr_set_buff[40] = {0x00};
  792. static uint8_t bao_bright = 0,bao_speed = 0,clr_buff_len = 0;
  793. if(data[2]== 0 ) //第一包数据
  794. {
  795. mode_set_buff[0] = data[3];
  796. mode_set_buff[1] = data[4];
  797. mode_clr_set_buff[0] = data[3];
  798. bao_bright = data[5];
  799. bao_speed = data[6];
  800. clr_buff_len = len - 7 -3;
  801. memcpy(&mode_clr_set_buff[1], &data[7], clr_buff_len);
  802. if( bao_len==data[2])
  803. {
  804. light_bright_ctrl(bao_bright);
  805. light_speed_ctrl(bao_speed);
  806. light_mode_set(mode_set_buff);
  807. app_set_mode_clr(mode_clr_set_buff, clr_buff_len);
  808. }
  809. }else if(data[2] == 1 )
  810. {
  811. temp_len = len - 3 -3;
  812. clr_buff_len = clr_buff_len + temp_len;
  813. memcpy(&mode_clr_set_buff[9], &data[3], temp_len);
  814. if( bao_len == data[2] )
  815. {
  816. light_bright_ctrl(bao_bright);
  817. light_speed_ctrl(bao_speed);
  818. light_mode_set(mode_set_buff);
  819. app_set_mode_clr(mode_clr_set_buff, clr_buff_len);
  820. }
  821. }else if(data[2] == 2 )
  822. {
  823. temp_len = len - 3 -3;
  824. clr_buff_len = clr_buff_len + temp_len ;
  825. memcpy(&mode_clr_set_buff[21], &data[3], temp_len);
  826. if( bao_len == data[2] )
  827. {
  828. light_bright_ctrl(bao_bright);
  829. light_speed_ctrl(bao_speed);
  830. light_mode_set(mode_set_buff);
  831. app_set_mode_clr(mode_clr_set_buff, clr_buff_len);
  832. }
  833. }
  834. reply_state = 0x01;
  835. reply_app_encode(cmd, &reply_state, 1);
  836. }
  837. break;
  838. case 0xA2: //读取某模式颜色
  839. {
  840. uint8_t FrameReq = 0;
  841. uint8_t bao_set_len = 0; //包数量
  842. uint8_t reply_len = 0; //总长度
  843. uint8_t reply_buff[APP_MODE_CLR_MAX_NUM * 4] = {0x00};
  844. uint8_t set_buff[45] = {0x00};
  845. app_read_mode_clr(data[1],reply_buff, &reply_len);
  846. FrameReq = data[0];
  847. bao_set_len = reply_len / 12 ;
  848. if( bao_set_len == 0 )
  849. {
  850. set_buff[0] = FrameReq;
  851. set_buff[1] = 1;
  852. set_buff[2] = 0;
  853. memcpy(&set_buff[3],&reply_buff[0],reply_len);
  854. reply_app_encode(cmd, &set_buff[0], reply_len+3);
  855. } else if ( bao_set_len == 1 )
  856. {
  857. set_buff[0] = FrameReq;
  858. set_buff[1] = 2;
  859. set_buff[2] = 0;
  860. memcpy(&set_buff[3],&reply_buff[0],12);
  861. reply_app_encode(cmd, &set_buff[0], 15);
  862. reply_len = reply_len - 12;
  863. set_buff[15]=FrameReq;
  864. set_buff[16]=2;
  865. set_buff[17]=1;
  866. memcpy(&set_buff[18],&reply_buff[12],reply_len);
  867. reply_app_encode(cmd, &set_buff[15], reply_len+3);
  868. }else if ( bao_set_len == 2 )
  869. {
  870. set_buff[0] = FrameReq;
  871. set_buff[1] = 3;
  872. set_buff[2] = 0;
  873. memcpy(&set_buff[3],&reply_buff[0],12);
  874. reply_app_encode(cmd, &set_buff[0], 0x0f);
  875. set_buff[15]=FrameReq;
  876. set_buff[16]=3;
  877. set_buff[17]=1;
  878. memcpy(&set_buff[18],&reply_buff[12],12);
  879. reply_app_encode(cmd, &set_buff[15], 0x0f);
  880. reply_len = reply_len - 24;
  881. set_buff[30]=FrameReq;
  882. set_buff[31]=3;
  883. set_buff[32]=2;
  884. memcpy(&set_buff[33],&reply_buff[24],reply_len);
  885. reply_app_encode(cmd, &set_buff[30], reply_len+3);
  886. }
  887. //reply_app_encode(cmd, reply_buff, reply_len);
  888. }
  889. break;
  890. case 0xF1:
  891. {
  892. uint32_t sys_time=sys_time_handle.get_run_ticks(); //获取运行时间
  893. uint8_t sys_temp[4] = {0};
  894. memcpy(sys_temp,&sys_time,4);
  895. reply_app_encode(0xF1, sys_temp, 4);
  896. }
  897. break;
  898. case 0xF2:
  899. {
  900. reply_app_encode(0xF2, (uint8_t*)firmware_version, sizeof(firmware_version) - 1);
  901. }
  902. break;
  903. }
  904. light_info_save_set();
  905. }