ws2812.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. *描述:蜂鸣器设置
  79. ********************************************************************/
  80. void beep_set(BEEPTYPE beep){
  81. switch(beep){
  82. case BEEP_ERR_HW:{
  83. sysinf.QRBeepTimeer = 0xfffffff;
  84. sysinf.QRBeepFlash = 250;
  85. sysinf.QRBeepFlashAll = 500;
  86. }
  87. break;
  88. case BEEP_ERR_HM:{
  89. sysinf.QRBeepTimeer = 0xfffffff;
  90. sysinf.QRBeepFlash = 250;
  91. sysinf.QRBeepFlashAll = 500;
  92. }
  93. break;
  94. case BEEP_SCANOK:{
  95. sysinf.QRBeepTimeer = 200;
  96. sysinf.QRBeepFlash = 1;
  97. sysinf.QRBeepFlashAll = 200;
  98. }
  99. break;
  100. case BEEP_WORKEND:{
  101. sysinf.QRBeepTimeer = 30*1000;
  102. sysinf.QRBeepFlash = 500;
  103. sysinf.QRBeepFlashAll = 1000;
  104. }
  105. break;
  106. default:{
  107. sysinf.QRBeepTimeer = 0;
  108. sysinf.QRBeepFlash = 0;
  109. sysinf.QRBeepFlashAll = 0;
  110. HAL_GPIO_WritePin((GPIO_TypeDef *)sysinf.beep_port, sysinf.beep_pin,GPIO_PIN_RESET);
  111. }
  112. }
  113. }
  114. /********************************************************************
  115. * void light_set(uint8_t mode_select, uint16_t speed_ms, uint32_t rgb_color, uint8_t channel)
  116. *
  117. *描述:设置灯带
  118. *参数:uint8_t mode_select:模式
  119. *参数:uint16_t speed_ms:变化周期
  120. *参数:uint32_t rgb_color:颜色, 低24位有效
  121. *参数:uint8_t channel:通道,0左,1右
  122. *返回:无
  123. *其他:无
  124. ********************************************************************/
  125. void light_set(uint8_t mode_select, uint16_t speed_ms, uint32_t rgb_color, uint8_t channel)
  126. {
  127. channel = channel ? 1 : 0;
  128. if(lihgt_ctrl.light_color[channel] != rgb_color)
  129. tick = 0;
  130. lihgt_ctrl.light_mode[channel] = mode_select;
  131. lihgt_ctrl.light_speed[channel] = speed_ms;
  132. lihgt_ctrl.light_color[channel] = rgb_color;
  133. //两边步调同步
  134. if(lihgt_ctrl.light_mode[0] == lihgt_ctrl.light_mode[1])
  135. {
  136. lihgt_ctrl.breath_count[0] = lihgt_ctrl.breath_count[1];
  137. lihgt_ctrl.breath_dir[0] = lihgt_ctrl.breath_dir[1];
  138. lihgt_ctrl.light_color_use[0] = lihgt_ctrl.light_color_use[1];
  139. }
  140. }
  141. /********************************************************************
  142. * void light_set(uint8_t mode_select, uint16_t speed_ms, uint32_t rgb_color, uint8_t channel)
  143. *
  144. *描述:设置灯带
  145. *参数:uint8_t mode_select:模式
  146. *参数:uint16_t speed_ms:变化周期
  147. *参数:uint32_t rgb_color:颜色, 低24位有效
  148. *参数:uint8_t channel:通道,0左,1右
  149. *返回:无
  150. *其他:无
  151. ********************************************************************/
  152. void light_update(void){
  153. if(sysinf.amplifState == SYS_STATE_ERROR_HM){
  154. light_set(MODE_BLINK,500, COLOR_RED,0); //
  155. }else if(sysinf.amplifState == SYS_STATE_ERROR_HW){
  156. light_set(MODE_NORMAL,1000, COLOR_RED,0); //仪器故障,双边同步
  157. light_set(MODE_NORMAL,1000, COLOR_RED,1); //
  158. return ;
  159. } else if(sysinf.pc_link_timeout == OFFLINE_TIME){
  160. light_set(MODE_BLINK,1000, COLOR_YELLOW,0); //
  161. light_set(MODE_BLINK,1000, COLOR_YELLOW,1); //
  162. sysinf.pc_channel_recv = PC_CONNECT_TYPE_NULL;
  163. return ;
  164. } else if(sysinf.pc_link_timeout < OFFLINE_TIME){
  165. switch (sysinf.amplifState){
  166. case SYS_STATE_PREPARE:
  167. light_set(MODE_NORMAL,1000, COLOR_YELLOW,0);
  168. break;
  169. case SYS_STATE_READY:
  170. light_set(MODE_NORMAL,1000, COLOR_GREEN,0);
  171. break;
  172. case SYS_STATE_CHECK:
  173. light_set(MODE_NORMAL,1000, COLOR_BLUE,0);
  174. break;
  175. case SYS_STATE_RUNING:
  176. light_set(MODE_BREATH,1000, COLOR_BLUE,0);
  177. break;
  178. case SYS_STATE_WORKEND:
  179. light_set(MODE_BLINK,1000, COLOR_GREEN,0);
  180. break;
  181. }
  182. }
  183. if(sysinf.lysisState == SYS_STATE_ERROR_HM){
  184. light_set(MODE_BLINK,500, COLOR_RED,1); //离线,黄色1S闪烁
  185. }else if(sysinf.lysisState == SYS_STATE_ERROR_HW){
  186. light_set(MODE_NORMAL,1000, COLOR_RED,0); //仪器故障,双边同步
  187. light_set(MODE_NORMAL,1000, COLOR_RED,1); //
  188. } else if(sysinf.pc_link_timeout < OFFLINE_TIME){
  189. switch (sysinf.lysisState){
  190. case SYS_STATE_PREPARE:
  191. case SYS_STATE_PREHEAT:
  192. light_set(MODE_NORMAL,1000, COLOR_YELLOW,1);
  193. break;
  194. case SYS_STATE_READY:
  195. light_set(MODE_NORMAL,1000, COLOR_GREEN,1);
  196. break;
  197. case SYS_STATE_CHECK:
  198. light_set(MODE_NORMAL,1000, COLOR_BLUE,1);
  199. break;
  200. case SYS_STATE_RUNING:
  201. light_set(MODE_BREATH,1000, COLOR_BLUE,1);
  202. break;
  203. case SYS_STATE_WORKEND:
  204. light_set(MODE_BLINK,1000, COLOR_GREEN,1);
  205. break;
  206. }
  207. }
  208. if(sysinf.system_debugFlag == 0){
  209. sysinf.pc_link_timeout++;
  210. }
  211. }
  212. /********************************************************************
  213. * void light_refresh(uint32_t rgb_left, uint32_t rgb_right)
  214. *
  215. *描述:刷新灯带
  216. *参数:uint32_t rgb_left:左边颜色
  217. *参数:uint32_t rgb_right:右边颜色
  218. *返回:无
  219. *其他:无
  220. ********************************************************************/
  221. void light_refresh(uint32_t rgb_left, uint32_t rgb_right)
  222. {
  223. for(int i = 0; i < WS2812_NUM; i++)
  224. {
  225. ws2812_set_rgb(rgb_right);
  226. }
  227. for(int i = 0; i < WS2812_NUM; i++)
  228. {
  229. ws2812_set_rgb(rgb_left);
  230. }
  231. }
  232. /********************************************************************
  233. * void light_ctrl()
  234. *
  235. *描述:刷新控制调度
  236. *参数:无
  237. *返回:无
  238. *其他:无
  239. ********************************************************************/
  240. void light_ctrl()
  241. {
  242. uint32_t r_g_b = 0; //r/g/b单色分量
  243. //计算颜色
  244. light_update();
  245. for(int i = 0; i < 2; i++)
  246. {
  247. switch (lihgt_ctrl.light_mode[i])
  248. {
  249. case MODE_NORMAL: //常亮,只改变一次
  250. if(lihgt_ctrl.light_color_use[i] != lihgt_ctrl.light_color[i]) //未刷新
  251. {
  252. lihgt_ctrl.light_color_use[i] = lihgt_ctrl.light_color[i];
  253. }
  254. break;
  255. case MODE_BLINK: //闪烁,一个周期反转一次
  256. if(tick % lihgt_ctrl.light_speed[i])
  257. break;
  258. if(lihgt_ctrl.light_color_use[i])
  259. lihgt_ctrl.light_color_use[i] = 0;
  260. else
  261. lihgt_ctrl.light_color_use[i] = lihgt_ctrl.light_color[i];
  262. break;
  263. case MODE_BREATH: //呼吸灯 y = x*x *r_g_b
  264. lihgt_ctrl.light_color_use[i] = 0;
  265. for(int j = 0; j < 3; j++)
  266. {
  267. //计算分量
  268. r_g_b = (lihgt_ctrl.light_color[i] >> (j * 8)) & 0xFF;
  269. if(r_g_b)
  270. {
  271. 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
  272. lihgt_ctrl.light_color_use[i] |= (r_g_b << (j * 8));
  273. }
  274. }
  275. //方向和x处理,x++,x--
  276. if(lihgt_ctrl.breath_dir[i])
  277. {
  278. if(lihgt_ctrl.breath_count[i] == BREATH_NUM)
  279. lihgt_ctrl.breath_dir[i] = 0;
  280. else
  281. lihgt_ctrl.breath_count[i]++;
  282. }
  283. else
  284. {
  285. if(lihgt_ctrl.breath_count[i] == 0)
  286. lihgt_ctrl.breath_dir[i] = 1;
  287. else
  288. lihgt_ctrl.breath_count[i]--;
  289. }
  290. break;
  291. default:
  292. break;
  293. }
  294. }
  295. tick += 50; //ms计时
  296. //刷新灯带
  297. light_refresh(lihgt_ctrl.light_color_use[0], lihgt_ctrl.light_color_use[1]);
  298. }