ws2812.c 9.1 KB

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