u_effect_rhythm.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #include "u_effect_rhythm.h"
  2. #include "u_main.h"
  3. #include "drv_mic.h"
  4. #include "user_driver.h"
  5. RhythmPara_t g_normal_rhythm = {0x00}; //全彩律动
  6. static uint8_t s_app_rhythm_val = 0x00;
  7. extern void delay_ms(unsigned int tt);
  8. void u_set_one_rhythm_color(void)
  9. {
  10. //u_set_color(m_rhythm.vol_value, m_rhythm.mode_info[12].data[0], m_rhythm.mode_info[12].data[1], m_rhythm.mode_info[12].data[2]);
  11. }
  12. void app_rhythm_val_up(uint8_t rhythm_val)
  13. {
  14. s_app_rhythm_val = rhythm_val;
  15. #ifdef DEBUG_DRV_APP
  16. U_UART_PRINTF("v = %d\n", s_app_rhythm_val);
  17. #endif
  18. }
  19. void rhythm_timer_cd(void)//app控制律动模式
  20. {
  21. #define RHYTHM_SENSIT_K (3)
  22. static uint8_t s_run_cnt = 0, vol_val = 0;
  23. uint8_t adc_vol;
  24. //static uint8_t s_music_keep = 0;
  25. uint8_t rhythm_pvt = 10;
  26. if( g_light_para.mic == 0)
  27. {
  28. adc_vol = s_app_rhythm_val;//rhythm_vol_get_cb();
  29. }
  30. else
  31. {
  32. if(g_light_para.mic_AppOrDriver == 1)
  33. {
  34. adc_vol = rhythm_vol_get_cb();
  35. //U_UART_PRINTF("i am driver mic \r\n");
  36. }else
  37. {
  38. adc_vol = s_app_rhythm_val;
  39. //U_UART_PRINTF("i am app mic \r\n");
  40. }
  41. }
  42. switch (g_light_para.rhythm_mode)
  43. {
  44. case RHYTHM_MODE_JUMP_7:
  45. case RHYTHM_MODE_JUMP_3:
  46. {
  47. rhythm_pvt = g_light_para.rhythm_sensit + VOT_JUMP_VPT;
  48. //U_UART_PRINTF(" u_pvt = %d\n", rhythm_pvt);
  49. if (adc_vol > rhythm_pvt)
  50. {
  51. int16_t speed = (adc_vol - rhythm_pvt) / 8;
  52. if (speed >= 4)
  53. speed = 4;
  54. else if (speed <= 0)
  55. speed = 0;
  56. //U_UART_PRINTF("speed = %d\n", speed);
  57. g_normal_rhythm.bright = MODE_JUMP_BRIGHT;
  58. //U_UART_PRINTF("MODE_JUMP_BRIGHT\n");
  59. g_normal_rhythm.speed = 27 - speed * 5;
  60. //U_UART_PRINTF("f3_sd = %d\n", g_normal_rhythm.speed);
  61. }
  62. else
  63. {
  64. g_normal_rhythm.bright = LIGHT_BRIGHT_MIN;
  65. g_normal_rhythm.speed = 0;
  66. //U_UART_PRINTF("MODE_JUMP_MIN_BRIGHT\n");
  67. }
  68. light_rhythm_mode_jump_timer_cb(&g_normal_rhythm);
  69. }
  70. break;
  71. case RHYTHM_MODE_GRADUAL_7:
  72. {
  73. rhythm_pvt = 30; //g_light_para.rhythm_sensit + 10;
  74. if (adc_vol > rhythm_pvt)
  75. {
  76. int16_t speed = (adc_vol - rhythm_pvt) / 14;
  77. if (speed >= 4)
  78. speed = 4;
  79. else if (speed <= 0)
  80. speed = 0;
  81. g_normal_rhythm.bright = adc_vol;
  82. g_normal_rhythm.speed = speed;
  83. }
  84. else
  85. {
  86. g_normal_rhythm.bright = LIGHT_BRIGHT_MIN;
  87. }
  88. light_rhythm_mode_gradual_timer_cb(&g_normal_rhythm);
  89. }
  90. break;
  91. case RHYTHM_MODE_FADE_7:
  92. {
  93. rhythm_pvt = g_light_para.rhythm_sensit + VOT_GRADUAL_7_VPT;
  94. if (adc_vol > rhythm_pvt )
  95. {
  96. //U_UART_PRINTF(" u_pvt = %d\n", rhythm_pvt);
  97. int16_t speed = (adc_vol - rhythm_pvt) / 8;
  98. if (speed >= 4)
  99. speed = 4;
  100. else if (speed <= 0)
  101. speed = 0;
  102. g_normal_rhythm.speed = 27 - speed * 5;
  103. g_normal_rhythm.music_start = 1;
  104. //U_UART_PRINTF("f7_sd = %d\n", g_normal_rhythm.speed);
  105. }
  106. light_rhythm_mode_fade_timer_cb(&g_normal_rhythm);
  107. }
  108. break;
  109. case RHYTHM_MODE_FADE_3:
  110. {
  111. rhythm_pvt = g_light_para.rhythm_sensit + VOT_GRADUAL_3_VPT;
  112. if (adc_vol > rhythm_pvt )
  113. {
  114. //U_UART_PRINTF(" u_pvt = %d\n", rhythm_pvt);
  115. g_normal_rhythm.music_start = 1;
  116. g_normal_rhythm.speed = 15;
  117. //U_UART_PRINTF("f3_sd = %d\n", g_normal_rhythm.speed);
  118. }
  119. light_rhythm_mode_fade_timer_cb(&g_normal_rhythm);
  120. }
  121. break;
  122. }
  123. }
  124. void driver_rhythm_timer_cd(void)//设备mic律动模式
  125. {
  126. #define RHYTHM_SENSIT_K (3)
  127. static uint8_t s_run_cnt = 0, vol_val = 0;
  128. uint8_t adc_vol=0;
  129. uint8_t rhythm_pvt = 10;
  130. if( g_light_para.mic ==0)
  131. {
  132. adc_vol = s_app_rhythm_val;//rhythm_vol_get_cb();
  133. }
  134. else
  135. {
  136. if(g_light_para.mic_AppOrDriver == 1)
  137. {
  138. adc_vol = rhythm_vol_get_cb();
  139. }else if(g_light_para.mic_AppOrDriver == 0)
  140. {
  141. adc_vol = s_app_rhythm_val;
  142. }
  143. }
  144. switch (g_light_para.rhythm_mode)
  145. {
  146. case RHYTHM_MODE_JUMP_7:
  147. case RHYTHM_MODE_JUMP_3:
  148. {
  149. rhythm_pvt = g_light_para.rhythm_sensit * 100 / 125 + VOT_JUMP_VPT;//rhythm_pvt 最大为50
  150. if (adc_vol > rhythm_pvt)
  151. {
  152. int16_t speed = (adc_vol - rhythm_pvt) / 26; //计算速度
  153. if (speed >= 4)
  154. speed = 4;
  155. else if (speed <= 0)
  156. speed = 0;
  157. //U_UART_PRINTF("speed = %d\n", speed);
  158. g_normal_rhythm.bright = MODE_JUMP_BRIGHT; //律动模式
  159. //U_UART_PRINTF("MODE_JUMP_BRIGHT\n");
  160. g_normal_rhythm.speed = 27 - speed * 5; //律动速度
  161. //U_UART_PRINTF("f3_sd = %d\n", g_normal_rhythm.speed);
  162. }
  163. else
  164. {
  165. g_normal_rhythm.bright = LIGHT_BRIGHT_MIN;
  166. g_normal_rhythm.speed = 0;
  167. //U_UART_PRINTF("MODE_JUMP_MIN_BRIGHT\n");
  168. }
  169. light_rhythm_mode_jump_timer_cb(&g_normal_rhythm);
  170. }
  171. break;
  172. case RHYTHM_MODE_GRADUAL_7://渐变律动模式
  173. {
  174. rhythm_pvt = 30; //g_light_para.rhythm_sensit + 10;
  175. if (adc_vol > rhythm_pvt)
  176. {
  177. int16_t speed = (adc_vol - rhythm_pvt) / 14;
  178. if (speed >= 4)
  179. speed = 4;
  180. else if (speed <= 0)
  181. speed = 0;
  182. g_normal_rhythm.bright = adc_vol;
  183. g_normal_rhythm.speed = speed;
  184. }
  185. else
  186. {
  187. g_normal_rhythm.bright = 0;
  188. }
  189. light_rhythm_mode_gradual_timer_cb(&g_normal_rhythm);
  190. }
  191. break;
  192. case RHYTHM_MODE_FADE_7:
  193. {
  194. rhythm_pvt = g_light_para.rhythm_sensit * 100 / 125 + VOT_GRADUAL_7_VPT;
  195. if (adc_vol > rhythm_pvt )
  196. {
  197. //U_UART_PRINTF(" u_pvt = %d\n", rhythm_pvt);
  198. int16_t speed = (adc_vol - rhythm_pvt) / 26;
  199. if (speed >= 4)
  200. speed = 4;
  201. else if (speed <= 0)
  202. speed = 0;
  203. g_normal_rhythm.speed = 27 - speed * 5;
  204. g_normal_rhythm.music_start = 1;
  205. //U_UART_PRINTF("f7_sd = %d\n", g_normal_rhythm.speed);
  206. }
  207. light_rhythm_mode_fade_timer_cb(&g_normal_rhythm);
  208. }
  209. break;
  210. case RHYTHM_MODE_FADE_3:
  211. {
  212. rhythm_pvt = g_light_para.rhythm_sensit + VOT_GRADUAL_3_VPT;
  213. if (adc_vol > rhythm_pvt )
  214. {
  215. //U_UART_PRINTF(" u_pvt = %d\n", rhythm_pvt);
  216. g_normal_rhythm.music_start = 1;
  217. g_normal_rhythm.speed = 15;
  218. //U_UART_PRINTF("f3_sd = %d\n", g_normal_rhythm.speed);
  219. }
  220. light_rhythm_mode_fade_timer_cb(&g_normal_rhythm);
  221. }
  222. break;
  223. }
  224. }
  225. /****************************** mic_adc **********************************/
  226. static void bubble_sort(uint8_t *sample_data,uint16_t len) //冒泡
  227. {
  228. uint16_t i,j;
  229. uint16_t temp;
  230. for(j = 0; j < (len - 1); j++)
  231. {
  232. for(i=0;i < (len - 1 - j);i++)
  233. {
  234. if(sample_data[i] > sample_data[i+1])
  235. {
  236. temp = sample_data[i];
  237. sample_data[i] = sample_data[i+1];
  238. sample_data[i+1] = temp;
  239. }
  240. }
  241. }
  242. }
  243. #if 0
  244. uint8_t rhythm_vol_get_cb(void)
  245. {
  246. static uint8_t buff_ind = 0, vol_value = 0;
  247. static uint8_t adc_sample[10] = {0};
  248. //adc_temp = ADC_Get_Value();
  249. //初始化ADC
  250. // ADC_Deinit(2);
  251. ADC_Init(1);
  252. //delay_ms(2);
  253. ADC_Get_Value();
  254. adc_sample[buff_ind] = ADC_Get_Value();
  255. //U_UART_PRINTF("adc_sample = %d\n", adc_sample[buff_ind]);
  256. buff_ind++;
  257. if (buff_ind >= 10)
  258. {
  259. int i = 0;
  260. uint16_t sample_num = 0;
  261. buff_ind = 0;
  262. bubble_sort(adc_sample, 10);
  263. for (i = 0; i < 5; i++)
  264. {
  265. sample_num += adc_sample[i+5];
  266. }
  267. vol_value = sample_num / 5;
  268. //U_UART_PRINTF("%d\n", vol_value);
  269. }
  270. return vol_value;
  271. }
  272. #endif
  273. //char show_buffer[10]={0};
  274. //#include "stdio.h"
  275. uint8_t rhythm_vol_get_cb(void)
  276. {
  277. static uint8_t buff_ind = 0, vol_value = 0;
  278. static uint8_t adc_sample[10] = {0};
  279. adc_sample[buff_ind] = Get_Audio_val();
  280. //U_UART_PRINTF("adc_sample = %d\n", adc_sample[buff_ind]);
  281. buff_ind++;
  282. if (buff_ind >= 10)
  283. {
  284. int i = 0;
  285. uint16_t sample_num = 0;
  286. buff_ind = 0;
  287. bubble_sort(adc_sample, 10);
  288. for (i = 0; i < 5; i++)
  289. {
  290. sample_num += adc_sample[i+5];
  291. }
  292. vol_value = sample_num / 5;
  293. // U_UART_PRINTF("adc_val:");
  294. // for(i=0;i<10;i++)
  295. // {
  296. // U_UART_PRINTF(" %d ", (int)adc_sample[i]);
  297. // }
  298. // U_UART_PRINTF("\r\n");
  299. // sprintf(show_buffer,"v:%3d ",vol_value);
  300. // u_ble_data_send(show_buffer,strlen(show_buffer));
  301. //U_UART_PRINTF("%d\n", vol_value);
  302. }
  303. // vol_value = Get_Audio_val();
  304. return vol_value;
  305. }
  306. /****************************** mic_adc end **********************************/
  307. /*
  308. sensit_step 红外遥控调节灵敏度加减
  309. rhythm_sensit app调节灵敏度
  310. */
  311. void rhythm_sensit_ctrl(const int8_t sensit_step, const uint8_t rhythm_sensit)// 调整mic灵敏度
  312. {
  313. uint8_t dri_rhythm_sensit = LIGHT_SENSIT_MAX - rhythm_sensit / APP_SENSIT_K;
  314. if (dri_rhythm_sensit > LIGHT_SENSIT_MAX)
  315. {
  316. return;
  317. }
  318. if (sensit_step != 0)
  319. {
  320. int8_t sensit = 0;
  321. sensit = g_light_para.rhythm_sensit + sensit_step;
  322. if (sensit >= LIGHT_SENSIT_MIN && sensit <= LIGHT_SENSIT_MAX)
  323. {
  324. g_light_para.rhythm_sensit = sensit;
  325. }
  326. else if (sensit <= LIGHT_SENSIT_MIN)
  327. {
  328. g_light_para.rhythm_sensit = LIGHT_SENSIT_MIN;
  329. }
  330. else if (sensit >= LIGHT_SENSIT_MAX)
  331. {
  332. g_light_para.rhythm_sensit = LIGHT_SENSIT_MAX;
  333. }
  334. }
  335. else
  336. {
  337. g_light_para.rhythm_sensit = dri_rhythm_sensit;
  338. }
  339. // sprintf(show_buffer,"s:%3d ",(int)g_light_para.rhythm_sensit);
  340. // u_ble_data_send(show_buffer,strlen(show_buffer));
  341. #ifdef DEBUG_DRV_APP
  342. U_UART_PRINTF("sensit = [%d] [%d] \n", g_light_para.rhythm_sensit, g_light_para.rhythm_sensit);
  343. #endif
  344. }
  345. /*
  346. void rhythm_speed_ctrl(const uint8_t rhythm_mode, const int8_t speed_step, uint8_t rhythm_speed)
  347. {
  348. uint8_t rhythm_speed_ind = 0;
  349. if (rhythm_speed > LIGHT_SPEED_MAX)
  350. {
  351. rhythm_speed = LIGHT_SPEED_MAX;
  352. }
  353. else if (rhythm_speed < LIGHT_SPEED_MIN)
  354. {
  355. rhythm_speed = LIGHT_SPEED_MIN;
  356. }
  357. if (rhythm_mode > RHYTHM_MODE_FULL_MAX)
  358. {
  359. rhythm_speed_ind = RHYTHM_SPEED_IND_DREAM;
  360. }
  361. else
  362. {
  363. rhythm_speed_ind = RHYTHM_SPEED_IND_FULL;
  364. }
  365. if (speed_step != 0)
  366. {
  367. int8_t speed = 0;
  368. speed = g_light_para.rhythm_speed[rhythm_speed_ind] + speed_step;
  369. if (speed >= LIGHT_SPEED_MIN && speed <= LIGHT_SPEED_MAX)
  370. {
  371. g_light_para.rhythm_speed[rhythm_speed_ind] = speed;
  372. }
  373. else if (speed <= LIGHT_SPEED_MIN)
  374. {
  375. g_light_para.rhythm_speed[rhythm_speed_ind] = LIGHT_SPEED_MIN;
  376. }
  377. else if (speed >= LIGHT_SPEED_MAX)
  378. {
  379. g_light_para.rhythm_speed[rhythm_speed_ind] = LIGHT_SPEED_MAX;
  380. }
  381. }
  382. else
  383. {
  384. g_light_para.rhythm_speed[rhythm_speed_ind] = rhythm_speed;
  385. }
  386. U_UART_PRINTF("speed = [%d] [%d] \n", g_light_para.rhythm_speed[0], g_light_para.rhythm_speed[1]);
  387. }
  388. */