hal_ws2812.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "hal_ws2812.h"
  2. #include "bsp_gpio.h"
  3. #include "nrf_gpio.h"
  4. #include "usr_config.h"
  5. #include "bsp_time.h"
  6. #include "system.h"
  7. #include "hal_ble_client.h"
  8. #include "nrf_delay.h"
  9. #define COLOR_RED 0x00000F00
  10. #define COLOR_ORANGE 0x00070F00
  11. #define COLOR_YELLOW 0x000F0F00
  12. #define COLOR_GREEN 0x000F0000
  13. #define COLOR_CYAN 0x000F000F
  14. #define COLOR_BLUE 0x0000000F
  15. #define COLOR_PURPLE 0x00000F0F
  16. #define COLOR_WHITE 0x000F0F0F
  17. #define COLOR_BLACK 0x00000000
  18. uint32_t COL_TAB[] = {COLOR_RED,COLOR_ORANGE,COLOR_YELLOW,COLOR_GREEN,COLOR_CYAN,COLOR_BLUE,COLOR_PURPLE,COLOR_WHITE};
  19. #define LED_NUM 25
  20. uint32_t led_color[LED_NUM];
  21. uint32_t led_color2[LED_NUM];
  22. uint32_t led_color3[LED_NUM];
  23. void WS2812_DisplayDot(uint32_t col)
  24. {
  25. uint32_t t = 0x00800000;
  26. for(int i=0;i<24;i++){
  27. if(col&t){WS_H;} else{WS_L;}
  28. t = t>>1;
  29. }
  30. }
  31. void WS2812_DisplayN(uint32_t* p,uint32_t n)
  32. {
  33. for(int i=0;i<n;i++){
  34. WS2812_DisplayDot(p[i]);
  35. }
  36. nrf_gpio_pin_write(PIN_SEL,0);
  37. }
  38. static uint8_t flag = 0;
  39. void WS2812_Process(void* t)
  40. //void WS2812_Process(void)
  41. {
  42. static uint32_t tim=0;
  43. if(flag>0) return;
  44. if(TIME_GetTicks()-tim>=10){ tim = TIME_GetTicks();
  45. WS2812_DisplayN(led_color,LED_NUM);
  46. WS2812_DisplayN(led_color2,LED_NUM);
  47. WS2812_DisplayN(led_color3,2);
  48. WS2812_DisplayN(led_color,LED_NUM);
  49. WS2812_DisplayN(led_color2,LED_NUM);
  50. }
  51. }
  52. uint8_t randTab[] = {2,17,1,24,15,17,24,0,3,9,15,17,20,16,2,18,6,15,1,5,8,12,16,7,2,14,13,14,22,14,8,23,10,20,7,3,11,8,8,13,21,25,16,5,0,16,16,15,23,8,2,19,8,17,6,14,2,9,19,14,2,4,11,13,15,18,0,2,22,12,11,5,23,4,19,20,12,11,13,3,5,11,13,8,8,13,2,18,14,23,2,10,18,2,12,6,14,23,1,22};
  53. void WS2812_Test(void)
  54. {
  55. static uint32_t rand = 0;
  56. static uint8_t dex = 0;
  57. flag = 1;
  58. rand = randTab[dex%100];
  59. for(int i=0;i<LED_NUM;i++){
  60. if(i<=rand){
  61. led_color[i] = COL_TAB[(i)%8];
  62. led_color2[i] = COL_TAB[(i)%8];
  63. }else{
  64. led_color[i] = COLOR_BLACK;
  65. led_color2[i] = COLOR_BLACK;
  66. }
  67. led_color3[i] = COLOR_BLACK;
  68. }
  69. dex++;
  70. flag = 0;
  71. }
  72. void WS2812_Init(void)
  73. {
  74. nrf_gpio_cfg_output(PIN_SEL); nrf_gpio_pin_write(PIN_SEL,0);
  75. memset(led_color,0,sizeof(led_color));
  76. TIME_Regist(WS2812_Process);
  77. Process_Start(100,WS2812_Test);
  78. SEGGER_RTT_printf(0,"WS2812_Init============================\n",0);
  79. }