user_light_adj.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #include "ets_sys.h"
  2. #include "osapi.h"
  3. #include "os_type.h"
  4. #include "mem.h"
  5. #include "user_interface.h"
  6. #include "user_light.h"
  7. #include "user_light_adj.h"
  8. #include "pwm.h"
  9. #define ABS_MINUS(x,y) (x<y?(y-x):(x-y))
  10. uint8 light_sleep_flg = 0;
  11. uint16 min_ms = 15;
  12. uint32 current_duty[PWM_CHANNEL] = {0};
  13. bool change_finish=true;
  14. os_timer_t timer_pwm_adj;
  15. static u32 duty_now[PWM_CHANNEL] = {0};
  16. //-----------------------------------Light para storage---------------------------
  17. #define LIGHT_EVT_QNUM (40)
  18. static struct pwm_param LightEvtArr[LIGHT_EVT_QNUM];
  19. static u8 CurFreeLightEvtIdx = 0;
  20. static u8 TotalUsedLightEvtNum = 0;
  21. static u8 CurEvtIdxToBeUse = 0;
  22. static struct pwm_param *LightEvtMalloc(void)
  23. {
  24. struct pwm_param *tmp = NULL;
  25. TotalUsedLightEvtNum++;
  26. if(TotalUsedLightEvtNum > LIGHT_EVT_QNUM ){
  27. TotalUsedLightEvtNum--;
  28. }
  29. else{
  30. tmp = &(LightEvtArr[CurFreeLightEvtIdx]);
  31. CurFreeLightEvtIdx++;
  32. if( CurFreeLightEvtIdx > (LIGHT_EVT_QNUM-1) )
  33. CurFreeLightEvtIdx = 0;
  34. }
  35. os_printf("malloc:%u\n",TotalUsedLightEvtNum);
  36. return tmp;
  37. }
  38. static void ICACHE_FLASH_ATTR LightEvtFree(void)
  39. {
  40. TotalUsedLightEvtNum--;
  41. os_printf("free:%u\n",TotalUsedLightEvtNum);
  42. }
  43. //------------------------------------------------------------------------------------
  44. static void ICACHE_FLASH_ATTR light_pwm_smooth_adj_proc(void);
  45. void ICACHE_FLASH_ATTR
  46. light_save_target_duty()
  47. {
  48. extern struct light_saved_param light_param;
  49. os_memcpy(light_param.pwm_duty,current_duty,sizeof(light_param.pwm_duty));
  50. light_param.pwm_period = pwm_get_period();
  51. #if SAVE_LIGHT_PARAM
  52. spi_flash_erase_sector(PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE);
  53. spi_flash_write((PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
  54. (uint32 *)&light_param, sizeof(struct light_saved_param));
  55. #endif
  56. }
  57. void ICACHE_FLASH_ATTR
  58. light_set_aim_r(uint32 r)
  59. {
  60. current_duty[LIGHT_RED]=r;
  61. light_pwm_smooth_adj_proc();
  62. }
  63. void ICACHE_FLASH_ATTR
  64. light_set_aim_g(uint32 g)
  65. {
  66. current_duty[LIGHT_GREEN]=g;
  67. light_pwm_smooth_adj_proc();
  68. }
  69. void ICACHE_FLASH_ATTR
  70. light_set_aim_b(uint32 b)
  71. {
  72. current_duty[LIGHT_BLUE]=b;
  73. light_pwm_smooth_adj_proc();
  74. }
  75. void ICACHE_FLASH_ATTR
  76. light_set_aim_cw(uint32 cw)
  77. {
  78. current_duty[LIGHT_COLD_WHITE]=cw;
  79. light_pwm_smooth_adj_proc();
  80. }
  81. void ICACHE_FLASH_ATTR
  82. light_set_aim_ww(uint32 ww)
  83. {
  84. current_duty[LIGHT_WARM_WHITE]=ww;
  85. light_pwm_smooth_adj_proc();
  86. }
  87. LOCAL bool ICACHE_FLASH_ATTR
  88. check_pwm_current_duty_diff()
  89. {
  90. int i;
  91. for(i=0;i<PWM_CHANNEL;i++){
  92. if(pwm_get_duty(i) != current_duty[i]){
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. void light_dh_pwm_adj_proc(void *Targ)
  99. {
  100. uint8 i;
  101. for(i=0;i<PWM_CHANNEL;i++){
  102. duty_now[i] = (duty_now[i]*15 + current_duty[i])>>4;
  103. if( ABS_MINUS(duty_now[i],current_duty[i])<20 )
  104. duty_now[i] = current_duty[i];
  105. user_light_set_duty(duty_now[i],i);
  106. }
  107. //os_printf("duty:%u,%u,%u\r\n", pwm.duty[0],pwm.duty[1],pwm.duty[2] );
  108. pwm_start();
  109. if(check_pwm_current_duty_diff()){
  110. change_finish = 0;
  111. os_timer_disarm(&timer_pwm_adj);
  112. os_timer_setfn(&timer_pwm_adj, (os_timer_func_t *)light_dh_pwm_adj_proc, NULL);
  113. os_timer_arm(&timer_pwm_adj, min_ms, 0);
  114. }
  115. else{
  116. os_printf("finish\n");
  117. change_finish = 1;
  118. //light_save_target_duty();
  119. os_timer_disarm(&timer_pwm_adj);
  120. light_pwm_smooth_adj_proc();
  121. }
  122. }
  123. LOCAL bool ICACHE_FLASH_ATTR
  124. check_pwm_duty_zero()
  125. {
  126. int i;
  127. for(i=0;i<PWM_CHANNEL;i++){
  128. if(pwm_get_duty(i) != 0){
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. static void ICACHE_FLASH_ATTR light_pwm_smooth_adj_proc(void)
  135. {
  136. if( TotalUsedLightEvtNum>0 ){
  137. user_light_set_period( LightEvtArr[CurEvtIdxToBeUse].period );
  138. os_memcpy(current_duty,LightEvtArr[CurEvtIdxToBeUse].duty,sizeof(current_duty));
  139. CurEvtIdxToBeUse++;
  140. if(CurEvtIdxToBeUse > (LIGHT_EVT_QNUM-1) ){
  141. CurEvtIdxToBeUse = 0;
  142. }
  143. LightEvtFree();
  144. if(change_finish){
  145. light_dh_pwm_adj_proc(NULL);
  146. }
  147. }
  148. if(change_finish){
  149. light_save_target_duty();
  150. if(check_pwm_duty_zero()){
  151. if(light_sleep_flg==0){
  152. os_printf("light sleep en\r\n");
  153. wifi_set_sleep_type(LIGHT_SLEEP_T);
  154. light_sleep_flg = 1;
  155. }
  156. }
  157. }
  158. }
  159. #if LIGHT_CURRENT_LIMIT
  160. uint32 light_get_cur(uint32 duty , uint8 channel, uint32 period)
  161. {
  162. uint32 duty_max_limit = (period*1000/45);
  163. uint32 duty_mapped = duty*22727/duty_max_limit;
  164. switch(channel){
  165. case LIGHT_RED :
  166. if(duty_mapped>=0 && duty_mapped<23000){
  167. return (duty_mapped*151000/22727);
  168. }
  169. break;
  170. case LIGHT_GREEN:
  171. if(duty_mapped>=0 && duty_mapped<23000){
  172. return (duty_mapped*82000/22727);
  173. }
  174. break;
  175. case LIGHT_BLUE:
  176. if(duty_mapped>=0 && duty_mapped<23000){
  177. return (duty_mapped*70000/22727);
  178. }
  179. break;
  180. case LIGHT_COLD_WHITE:
  181. case LIGHT_WARM_WHITE:
  182. if(duty_mapped>=0 && duty_mapped<23000){
  183. return (duty_mapped*115000/22727);
  184. }
  185. break;
  186. default:
  187. os_printf("CHANNEL ERROR IN GET_CUR\r\n");
  188. break;
  189. }
  190. }
  191. #endif
  192. void ICACHE_FLASH_ATTR
  193. light_set_aim(uint32 r,uint32 g,uint32 b,uint32 cw,uint32 ww,uint32 period)
  194. {
  195. struct pwm_param *tmp = LightEvtMalloc();
  196. if(tmp != NULL){
  197. tmp->period = (period<10000?period:10000);
  198. uint32 duty_max_limit = (period*1000/45);
  199. tmp->duty[LIGHT_RED] = (r<duty_max_limit?r:duty_max_limit);
  200. tmp->duty[LIGHT_GREEN] = (g<duty_max_limit?g:duty_max_limit);
  201. tmp->duty[LIGHT_BLUE] = (b<duty_max_limit?b:duty_max_limit);
  202. tmp->duty[LIGHT_COLD_WHITE] = (cw<duty_max_limit?cw:duty_max_limit);
  203. tmp->duty[LIGHT_WARM_WHITE] = (ww<duty_max_limit?ww:duty_max_limit);//chg
  204. #if LIGHT_CURRENT_LIMIT
  205. uint32 cur_r,cur_g,cur_b,cur_rgb;
  206. //if(cw>0 || ww>0){
  207. cur_r = light_get_cur(tmp->duty[LIGHT_RED] , LIGHT_RED, tmp->period);
  208. cur_g = light_get_cur(tmp->duty[LIGHT_GREEN] , LIGHT_GREEN, tmp->period);
  209. cur_b = light_get_cur(tmp->duty[LIGHT_BLUE] , LIGHT_BLUE, tmp->period);
  210. cur_rgb = (cur_r+cur_g+cur_b);
  211. //}
  212. uint32 cur_cw = light_get_cur( tmp->duty[LIGHT_COLD_WHITE],LIGHT_COLD_WHITE, tmp->period);
  213. uint32 cur_ww = light_get_cur( tmp->duty[LIGHT_WARM_WHITE],LIGHT_WARM_WHITE, tmp->period);
  214. uint32 cur_remain,cur_mar;
  215. cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN);
  216. cur_mar = LIGHT_CURRENT_MARGIN;
  217. /*
  218. if((cur_cw < 50000) || (cur_ww < 50000)){
  219. cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN);
  220. cur_mar = LIGHT_CURRENT_MARGIN;
  221. }else if((cur_cw < 99000) || (cur_ww < 99000)){
  222. cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN_L2);
  223. cur_mar = LIGHT_CURRENT_MARGIN_L2;
  224. }else{
  225. cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN_L3);
  226. cur_mar = LIGHT_CURRENT_MARGIN_L2;
  227. }
  228. */
  229. /*
  230. if((LIGHT_TOTAL_CURRENT_MAX-cur_rgb)>120){
  231. cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN);
  232. cur_mar = LIGHT_CURRENT_MARGIN;
  233. }else if((LIGHT_TOTAL_CURRENT_MAX-cur_rgb)>100){
  234. cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN_L2);
  235. cur_mar = LIGHT_CURRENT_MARGIN_L2;
  236. }else{
  237. cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN_L3);
  238. cur_mar = LIGHT_CURRENT_MARGIN_L2;
  239. }
  240. */
  241. os_printf("cur_remain: %d \r\n",cur_remain);
  242. while((cur_cw+cur_ww) > cur_remain){
  243. tmp->duty[LIGHT_COLD_WHITE] = tmp->duty[LIGHT_COLD_WHITE] * 9 / 10;
  244. tmp->duty[LIGHT_WARM_WHITE] = tmp->duty[LIGHT_WARM_WHITE] * 9 / 10;
  245. cur_cw = light_get_cur( tmp->duty[LIGHT_COLD_WHITE],LIGHT_COLD_WHITE, tmp->period);
  246. cur_ww = light_get_cur( tmp->duty[LIGHT_WARM_WHITE],LIGHT_WARM_WHITE, tmp->period);
  247. }
  248. os_printf("debug : %d %d %d %d %d\r\n",cur_r/1000,cur_g/1000,cur_b/1000,cur_cw/1000,cur_ww/1000);
  249. os_printf("debug:total current after adj : %d + %d mA \r\n",(cur_cw+cur_ww+cur_r+cur_g+cur_b)/1000,cur_mar/1000);
  250. #endif
  251. os_printf("prd:%u r : %u g: %u b: %u cw: %u ww: %u \r\n",period,
  252. tmp->duty[0],tmp->duty[1],tmp->duty[2],tmp->duty[3],tmp->duty[4]);
  253. light_pwm_smooth_adj_proc();
  254. }
  255. else{
  256. os_printf("light para full\n");
  257. }
  258. }