WS2812_ATY.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #ifdef __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. #ifdef __ESP8266_ATY
  77. #include "HW_SPI_ATY.h"
  78. /**
  79. * @brief Set IO level for WS2812 bus
  80. * @param data_bit Generate buf
  81. * @warning this type use spi bus, so WS2812 DI must connect GPIO13,
  82. * and cannot use SPI anymore
  83. * and put esp8266_spi_init(); at MainInit_User(){}
  84. */
  85. void WS2812_SetBit(uint8_t dataBit)
  86. {
  87. if(dataBit)
  88. SPI_Write(0xFC, 1);
  89. else
  90. SPI_Write(0x3F, 1);
  91. }
  92. #endif /* __ESP8266_ATY */
  93. /**
  94. * @brief Set colors
  95. * @param R Color red
  96. * @param G Color green
  97. * @param B Color blue
  98. */
  99. void WS2812_SetColor(uint8_t R, uint8_t G, uint8_t B)
  100. {
  101. WS2812_SetBit(G & 0X80);
  102. WS2812_SetBit(G & 0X40);
  103. WS2812_SetBit(G & 0X20);
  104. WS2812_SetBit(G & 0X10);
  105. WS2812_SetBit(G & 0X08);
  106. WS2812_SetBit(G & 0X04);
  107. WS2812_SetBit(G & 0X02);
  108. WS2812_SetBit(G & 0X01);
  109. WS2812_SetBit(R & 0X80);
  110. WS2812_SetBit(R & 0X40);
  111. WS2812_SetBit(R & 0X20);
  112. WS2812_SetBit(R & 0X10);
  113. WS2812_SetBit(R & 0X08);
  114. WS2812_SetBit(R & 0X04);
  115. WS2812_SetBit(R & 0X02);
  116. WS2812_SetBit(R & 0X01);
  117. WS2812_SetBit(B & 0X80);
  118. WS2812_SetBit(B & 0X40);
  119. WS2812_SetBit(B & 0X20);
  120. WS2812_SetBit(B & 0X10);
  121. WS2812_SetBit(B & 0X08);
  122. WS2812_SetBit(B & 0X04);
  123. WS2812_SetBit(B & 0X02);
  124. WS2812_SetBit(B & 0X01);
  125. }
  126. #ifdef __DEBUG_WS2812_ATY
  127. /**
  128. * @brief Basic colors diaplay to test the code is right or not
  129. */
  130. void WS2812_Test(void)
  131. {
  132. #define MAX_TIME_WS2812_TEST 10000
  133. uint16_t i = 0;
  134. WS2812_SetColor(255, 0, 0);
  135. for(i = 0; i < 10000; i++){}
  136. WS2812_SetColor(255, 255, 0);
  137. for(i = 0; i < 10000; i++){}
  138. WS2812_SetColor(0, 255, 0);
  139. for(i = 0; i < 10000; i++){}
  140. WS2812_SetColor(0, 255, 255);
  141. for(i = 0; i < 10000; i++){}
  142. WS2812_SetColor(0, 0, 255);
  143. for(i = 0; i < 10000; i++){}
  144. WS2812_SetColor(255, 0, 255);
  145. for(i = 0; i < 10000; i++){}
  146. }
  147. /**
  148. * @brief RGB flow with color gradual change
  149. */
  150. void WS2812_StyleA(void)
  151. {
  152. // (255, 0 ,0)
  153. // (255, 255 ,0)
  154. // (0, 255 ,0)
  155. // (0, 255 ,255)
  156. // (0, 0 ,255)
  157. // (255, 0 ,255)
  158. // (255, 0 ,0)
  159. uint16_t i = 0, j = 0;
  160. uint16_t r = 255, g = 0, b = 0;
  161. uint8_t staRGB = 0;
  162. while(1)
  163. {
  164. // for(i = 0; i < 80; i++)
  165. {
  166. // for(j = 0; j < 7; j++)
  167. WS2812_SetColor(r, g, b);
  168. {
  169. uint8_t i_t = 0;
  170. for(i = 0; i < 100; i++){}
  171. }
  172. }
  173. switch(staRGB)
  174. {
  175. case 0: g++; break;
  176. case 1: r--; break;
  177. case 2: b++; break;
  178. case 3: g--; break;
  179. case 4: r++; break;
  180. case 5: b--; break;
  181. }
  182. if(r >= 255 && g <= 0 && b <= 0)
  183. staRGB = 0;
  184. else if(r >= 255 && g >= 255 && b <= 0)
  185. staRGB = 1;
  186. else if(r <= 0 && g >= 255 && b <= 0)
  187. staRGB = 2;
  188. else if(r <= 0 && g >= 255 && b >= 255)
  189. staRGB = 3;
  190. else if(r <= 0 && g <= 0 && b >= 255)
  191. staRGB = 4;
  192. else if(r >= 255 && g <= 0 && b >= 255)
  193. staRGB = 5;
  194. }
  195. }
  196. //void main()
  197. //{
  198. // while (1)
  199. // {
  200. // // WS2812_test();
  201. // WS2812_styleA();
  202. // }
  203. //}
  204. #endif /* __DEBUG_WS2812_ATY */
  205. #endif /* __WS2812_ATY_C */
  206. /******************************** End Of File *********************************/