ws2812.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "ws2812.h"
  2. #include <string.h>
  3. #include "onechip_include_header.h"
  4. static struct str_ws2812 lihgt_ctrl = {0}; //灯带控制结构
  5. static uint32_t tick = 0;
  6. /********************************************************************
  7. * void ws2812_0()
  8. *
  9. *描述:灯带0信号
  10. *参数:无
  11. *返回:无
  12. *其他:无
  13. ********************************************************************/
  14. static void ws2812_0()
  15. {
  16. GPIO_TypeDef *GPIOx = (GPIO_TypeDef *)sysinf.rgb_band_port;
  17. uint32_t GPIO_Pin_RESET = sysinf.rgb_band_pin << 16;
  18. uint32_t GPIO_Pin_SET = sysinf.rgb_band_pin;
  19. GPIOx->BSRR = (uint32_t)GPIO_Pin_SET;
  20. GPIOx->BSRR = (uint32_t)GPIO_Pin_SET;
  21. GPIOx->BSRR = (uint32_t)GPIO_Pin_RESET;
  22. GPIOx->BSRR = (uint32_t)GPIO_Pin_RESET;
  23. GPIOx->BSRR = (uint32_t)GPIO_Pin_RESET;
  24. GPIOx->BSRR = (uint32_t)GPIO_Pin_RESET;
  25. }
  26. /********************************************************************
  27. * void ws2812_1()
  28. *
  29. *描述:灯带1信号
  30. *参数:无
  31. *返回:无
  32. *其他:无
  33. ********************************************************************/
  34. static void ws2812_1()
  35. {
  36. GPIO_TypeDef *GPIOx = (GPIO_TypeDef *)sysinf.rgb_band_port;
  37. uint32_t GPIO_Pin_RESET = sysinf.rgb_band_pin << 16;
  38. uint32_t GPIO_Pin_SET = sysinf.rgb_band_pin;
  39. GPIOx->BSRR = (uint32_t)GPIO_Pin_SET;
  40. GPIOx->BSRR = (uint32_t)GPIO_Pin_SET;
  41. GPIOx->BSRR = (uint32_t)GPIO_Pin_SET;
  42. GPIOx->BSRR = (uint32_t)GPIO_Pin_SET;
  43. GPIOx->BSRR = (uint32_t)GPIO_Pin_SET;
  44. GPIOx->BSRR = (uint32_t)GPIO_Pin_RESET;
  45. GPIOx->BSRR = (uint32_t)GPIO_Pin_RESET;
  46. GPIOx->BSRR = (uint32_t)GPIO_Pin_RESET;
  47. GPIOx->BSRR = (uint32_t)GPIO_Pin_RESET;
  48. GPIOx->BSRR = (uint32_t)GPIO_Pin_RESET;
  49. }
  50. /********************************************************************
  51. * void ws2812_set_rgb(uint32_t rgb)
  52. *
  53. *描述:设置灯珠RGB颜色
  54. *参数:uint32_t rgb:颜色, 低24位有效
  55. *返回:无
  56. *其他:无
  57. ********************************************************************/
  58. void ws2812_set_rgb(uint32_t rgb)
  59. {
  60. for(int i = 0; i < 24; i++)
  61. {
  62. if(rgb & 0x800000)
  63. {
  64. ws2812_1();
  65. }
  66. else
  67. {
  68. ws2812_0();
  69. }
  70. rgb <<= 1;
  71. }
  72. }
  73. /********************************************************************
  74. * void light_set(uint8_t mode_select, uint16_t speed_ms, uint32_t rgb_color, uint8_t channel)
  75. *
  76. *描述:设置灯带
  77. *参数:uint8_t mode_select:模式
  78. *参数:uint16_t speed_ms:变化周期
  79. *参数:uint32_t rgb_color:颜色, 低24位有效
  80. *参数:uint8_t channel:通道,0左,1右
  81. *返回:无
  82. *其他:无
  83. ********************************************************************/
  84. void light_set(uint8_t mode_select, uint16_t speed_ms, uint32_t rgb_color, uint8_t channel)
  85. {
  86. channel = channel ? 1 : 0;
  87. if(lihgt_ctrl.light_color[channel] != rgb_color)
  88. tick = 0;
  89. lihgt_ctrl.light_mode[channel] = mode_select;
  90. lihgt_ctrl.light_speed[channel] = speed_ms;
  91. lihgt_ctrl.light_color[channel] = rgb_color;
  92. //两边步调同步
  93. if(lihgt_ctrl.light_mode[0] == lihgt_ctrl.light_mode[1])
  94. {
  95. lihgt_ctrl.breath_count[0] = lihgt_ctrl.breath_count[1];
  96. lihgt_ctrl.breath_dir[0] = lihgt_ctrl.breath_dir[1];
  97. lihgt_ctrl.light_color_use[0] = lihgt_ctrl.light_color_use[1];
  98. }
  99. }
  100. /********************************************************************
  101. * void light_set(uint8_t mode_select, uint16_t speed_ms, uint32_t rgb_color, uint8_t channel)
  102. *
  103. *描述:设置灯带
  104. *参数:uint8_t mode_select:模式
  105. *参数:uint16_t speed_ms:变化周期
  106. *参数:uint32_t rgb_color:颜色, 低24位有效
  107. *参数:uint8_t channel:通道,0左,1右
  108. *返回:无
  109. *其他:无
  110. ********************************************************************/
  111. void light_update(void){
  112. if(sysinf.amplifState == SYS_STATE_ERROR_HM){
  113. light_set(MODE_BLINK,500, COLOR_RED,0); //
  114. }else if(sysinf.amplifState == SYS_STATE_ERROR_HW){
  115. light_set(MODE_NORMAL,1000, COLOR_RED,0); //
  116. } else if(sysinf.pc_link_timeout == OFFLINE_TIME){
  117. light_set(MODE_BLINK,1000, COLOR_YELLOW,0); //
  118. light_set(MODE_BLINK,1000, COLOR_YELLOW,1); //
  119. sysinf.pc_channel_recv = PC_CONNECT_TYPE_NULL;
  120. return ;
  121. } else if(sysinf.pc_link_timeout < OFFLINE_TIME){
  122. switch (sysinf.amplifState){
  123. case SYS_STATE_PREPARE:
  124. light_set(MODE_NORMAL,1000, COLOR_YELLOW,0);
  125. break;
  126. case SYS_STATE_READY:
  127. light_set(MODE_NORMAL,1000, COLOR_GREEN,0);
  128. break;
  129. case SYS_STATE_CHECK:
  130. light_set(MODE_NORMAL,1000, COLOR_BLUE,0);
  131. break;
  132. case SYS_STATE_RUNING:
  133. light_set(MODE_BREATH,1000, COLOR_BLUE,0);
  134. break;
  135. case SYS_STATE_WORKEND:
  136. light_set(MODE_BLINK,1000, COLOR_GREEN,0);
  137. break;
  138. }
  139. }
  140. if(sysinf.lysisState == SYS_STATE_ERROR_HM){
  141. light_set(MODE_BLINK,500, COLOR_RED,1); //离线,黄色1S闪烁
  142. }else if(sysinf.lysisState == SYS_STATE_ERROR_HW){
  143. light_set(MODE_NORMAL,1000, COLOR_RED,1); //离线,黄色1S闪烁
  144. } else if(sysinf.pc_link_timeout < OFFLINE_TIME){
  145. switch (sysinf.lysisState){
  146. case SYS_STATE_PREPARE:
  147. light_set(MODE_NORMAL,1000, COLOR_YELLOW,1);
  148. break;
  149. case SYS_STATE_READY:
  150. light_set(MODE_NORMAL,1000, COLOR_GREEN,1);
  151. break;
  152. case SYS_STATE_CHECK:
  153. light_set(MODE_NORMAL,1000, COLOR_BLUE,1);
  154. break;
  155. case SYS_STATE_RUNING:
  156. light_set(MODE_BREATH,1000, COLOR_BLUE,1);
  157. break;
  158. case SYS_STATE_WORKEND:
  159. light_set(MODE_BLINK,1000, COLOR_GREEN,1);
  160. break;
  161. }
  162. }
  163. sysinf.pc_link_timeout++;
  164. }
  165. /********************************************************************
  166. * void light_refresh(uint32_t rgb_left, uint32_t rgb_right)
  167. *
  168. *描述:刷新灯带
  169. *参数:uint32_t rgb_left:左边颜色
  170. *参数:uint32_t rgb_right:右边颜色
  171. *返回:无
  172. *其他:无
  173. ********************************************************************/
  174. void light_refresh(uint32_t rgb_left, uint32_t rgb_right)
  175. {
  176. for(int i = 0; i < WS2812_NUM; i++)
  177. {
  178. ws2812_set_rgb(rgb_right);
  179. }
  180. for(int i = 0; i < WS2812_NUM; i++)
  181. {
  182. ws2812_set_rgb(rgb_left);
  183. }
  184. }
  185. /********************************************************************
  186. * void light_ctrl()
  187. *
  188. *描述:刷新控制调度
  189. *参数:无
  190. *返回:无
  191. *其他:无
  192. ********************************************************************/
  193. void light_ctrl()
  194. {
  195. uint32_t r_g_b = 0; //r/g/b单色分量
  196. //计算颜色
  197. light_update();
  198. for(int i = 0; i < 2; i++)
  199. {
  200. switch (lihgt_ctrl.light_mode[i])
  201. {
  202. case MODE_NORMAL: //常亮,只改变一次
  203. if(lihgt_ctrl.light_color_use[i] != lihgt_ctrl.light_color[i]) //未刷新
  204. {
  205. lihgt_ctrl.light_color_use[i] = lihgt_ctrl.light_color[i];
  206. }
  207. break;
  208. case MODE_BLINK: //闪烁,一个周期反转一次
  209. if(tick % lihgt_ctrl.light_speed[i])
  210. break;
  211. if(lihgt_ctrl.light_color_use[i])
  212. lihgt_ctrl.light_color_use[i] = 0;
  213. else
  214. lihgt_ctrl.light_color_use[i] = lihgt_ctrl.light_color[i];
  215. break;
  216. case MODE_BREATH: //呼吸灯 y = x*x *r_g_b
  217. lihgt_ctrl.light_color_use[i] = 0;
  218. for(int j = 0; j < 3; j++)
  219. {
  220. //计算分量
  221. r_g_b = (lihgt_ctrl.light_color[i] >> (j * 8)) & 0xFF;
  222. if(r_g_b)
  223. {
  224. r_g_b = (uint32_t)(r_g_b * lihgt_ctrl.breath_count[i] * lihgt_ctrl.breath_count[i] / BREATH_NUM / BREATH_NUM); //y = x*x*r_g_b
  225. lihgt_ctrl.light_color_use[i] |= (r_g_b << (j * 8));
  226. }
  227. }
  228. //方向和x处理,x++,x--
  229. if(lihgt_ctrl.breath_dir[i])
  230. {
  231. if(lihgt_ctrl.breath_count[i] == BREATH_NUM)
  232. lihgt_ctrl.breath_dir[i] = 0;
  233. else
  234. lihgt_ctrl.breath_count[i]++;
  235. }
  236. else
  237. {
  238. if(lihgt_ctrl.breath_count[i] == 0)
  239. lihgt_ctrl.breath_dir[i] = 1;
  240. else
  241. lihgt_ctrl.breath_count[i]--;
  242. }
  243. break;
  244. default:
  245. break;
  246. }
  247. }
  248. tick += 50; //ms计时
  249. //刷新灯带
  250. light_refresh(lihgt_ctrl.light_color_use[0], lihgt_ctrl.light_color_use[1]);
  251. }