WS2812_ATY.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * @file WS2812_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Base functions of WS2812 for all embedded device
  20. *
  21. * @version
  22. * - 1_01_220901 > ATY
  23. * -# Preliminary version, first Release
  24. ********************************************************************************
  25. */
  26. #ifndef __WS2812_ATY_C
  27. #define __WS2812_ATY_C
  28. #include "WS2812_ATY.h"
  29. /******************************* For user *************************************/
  30. /******************************************************************************/
  31. #if defined (__STC51_ATY)
  32. /**
  33. * @brief Set IO level for WS2812 bus
  34. * @param data_bit Generate buf
  35. * @warning "_nop()_" is counts at 24MHz for 1T 8051 MCU,
  36. * and it still needs to adjust the time delay for better stability with
  37. * the oscilloscope
  38. */
  39. void WS2812_SetBit(uint8_t dataBit)
  40. {
  41. if(dataBit)
  42. {
  43. // 24MHz-1T
  44. GPIO_SET_H(WS2812_PIN);
  45. _nop_();
  46. _nop_();
  47. _nop_();
  48. _nop_();
  49. _nop_();
  50. _nop_();
  51. _nop_();
  52. _nop_();
  53. _nop_();
  54. GPIO_SET_L(WS2812_PIN);
  55. _nop_();
  56. _nop_();
  57. _nop_();
  58. }
  59. else
  60. {
  61. GPIO_SET_H(WS2812_PIN);
  62. _nop_();
  63. _nop_();
  64. _nop_();
  65. GPIO_SET_L(WS2812_PIN);
  66. _nop_();
  67. _nop_();
  68. _nop_();
  69. _nop_();
  70. _nop_();
  71. _nop_();
  72. _nop_();
  73. }
  74. }
  75. #endif /* __STC51_ATY */
  76. #elif defined (__STM32_HAL_ATY)
  77. #elif defined (__ESP8266_ATY)
  78. #include "HW_SPI_ATY.h"
  79. /**
  80. * @brief Set IO level for WS2812 bus
  81. * @param data_bit Generate buf
  82. * @warning this type use spi bus, so WS2812 DI must connect GPIO13,
  83. * and cannot use SPI anymore
  84. * and put esp8266_spi_init(); at MainInit_User(){}
  85. */
  86. void WS2812_SetBit(uint8_t dataBit)
  87. {
  88. if(dataBit)
  89. SPI_Write(0xFC, 1);
  90. else
  91. SPI_Write(0x3F, 1);
  92. }
  93. #endif /* PLATFORM */
  94. /**
  95. * @brief Set colors
  96. * @param R Color red
  97. * @param G Color green
  98. * @param B Color blue
  99. */
  100. void WS2812_SetColor(uint8_t R, uint8_t G, uint8_t B)
  101. {
  102. WS2812_SetBit(G & 0X80);
  103. WS2812_SetBit(G & 0X40);
  104. WS2812_SetBit(G & 0X20);
  105. WS2812_SetBit(G & 0X10);
  106. WS2812_SetBit(G & 0X08);
  107. WS2812_SetBit(G & 0X04);
  108. WS2812_SetBit(G & 0X02);
  109. WS2812_SetBit(G & 0X01);
  110. WS2812_SetBit(R & 0X80);
  111. WS2812_SetBit(R & 0X40);
  112. WS2812_SetBit(R & 0X20);
  113. WS2812_SetBit(R & 0X10);
  114. WS2812_SetBit(R & 0X08);
  115. WS2812_SetBit(R & 0X04);
  116. WS2812_SetBit(R & 0X02);
  117. WS2812_SetBit(R & 0X01);
  118. WS2812_SetBit(B & 0X80);
  119. WS2812_SetBit(B & 0X40);
  120. WS2812_SetBit(B & 0X20);
  121. WS2812_SetBit(B & 0X10);
  122. WS2812_SetBit(B & 0X08);
  123. WS2812_SetBit(B & 0X04);
  124. WS2812_SetBit(B & 0X02);
  125. WS2812_SetBit(B & 0X01);
  126. }
  127. #ifdef __DEBUG_WS2812_ATY
  128. /**
  129. * @brief Basic colors diaplay to test the code is right or not
  130. */
  131. void WS2812_Test(void)
  132. {
  133. #define MAX_TIME_WS2812_TEST 10000
  134. uint16_t i = 0;
  135. WS2812_SetColor(255, 0, 0);
  136. for(i = 0; i < 10000; i++){}
  137. WS2812_SetColor(255, 255, 0);
  138. for(i = 0; i < 10000; i++){}
  139. WS2812_SetColor(0, 255, 0);
  140. for(i = 0; i < 10000; i++){}
  141. WS2812_SetColor(0, 255, 255);
  142. for(i = 0; i < 10000; i++){}
  143. WS2812_SetColor(0, 0, 255);
  144. for(i = 0; i < 10000; i++){}
  145. WS2812_SetColor(255, 0, 255);
  146. for(i = 0; i < 10000; i++){}
  147. }
  148. /**
  149. * @brief RGB flow with color gradual change
  150. */
  151. void WS2812_StyleA(void)
  152. {
  153. // (255, 0 ,0)
  154. // (255, 255 ,0)
  155. // (0, 255 ,0)
  156. // (0, 255 ,255)
  157. // (0, 0 ,255)
  158. // (255, 0 ,255)
  159. // (255, 0 ,0)
  160. uint16_t i = 0, j = 0;
  161. uint16_t r = 255, g = 0, b = 0;
  162. uint8_t staRGB = 0;
  163. while(1)
  164. {
  165. // for(i = 0; i < 80; i++)
  166. {
  167. // for(j = 0; j < 7; j++)
  168. WS2812_SetColor(r, g, b);
  169. {
  170. uint8_t i_t = 0;
  171. for(i = 0; i < 100; i++){}
  172. }
  173. }
  174. switch(staRGB)
  175. {
  176. case 0: g++; break;
  177. case 1: r--; break;
  178. case 2: b++; break;
  179. case 3: g--; break;
  180. case 4: r++; break;
  181. case 5: b--; break;
  182. }
  183. if(r >= 255 && g <= 0 && b <= 0)
  184. staRGB = 0;
  185. else if(r >= 255 && g >= 255 && b <= 0)
  186. staRGB = 1;
  187. else if(r <= 0 && g >= 255 && b <= 0)
  188. staRGB = 2;
  189. else if(r <= 0 && g >= 255 && b >= 255)
  190. staRGB = 3;
  191. else if(r <= 0 && g <= 0 && b >= 255)
  192. staRGB = 4;
  193. else if(r >= 255 && g <= 0 && b >= 255)
  194. staRGB = 5;
  195. }
  196. }
  197. // todo: not tested
  198. //void main()
  199. //{
  200. // while (1)
  201. // {
  202. // // WS2812_test();
  203. // WS2812_styleA();
  204. // }
  205. //}
  206. #endif /* __DEBUG_WS2812_ATY */
  207. #endif /* __WS2812_ATY_C */
  208. /******************************** End Of File *********************************/