/** * @file WS2812_ATY.c * * @param Project DEVICE_GENERAL_ATY_LIB * * @author ATY * * @copyright * - Copyright 2017 - 2025 MZ-ATY * - This code follows: * - MZ-ATY Various Contents Joint Statement - * * https://mengze.top/MZ-ATY_VCJS * - CC 4.0 BY-NC-SA - * * https://creativecommons.org/licenses/by-nc-sa/4.0/ * - Your use will be deemed to have accepted the terms of this statement. * * @brief Base functions of WS2812 for all embedded device * * @version * - 1_01_220901 > ATY * -# Preliminary version, first Release ******************************************************************************** */ #ifndef __WS2812_ATY_C #define __WS2812_ATY_C #include "WS2812_ATY.h" /******************************* For user *************************************/ /******************************************************************************/ #if defined (__STC51_ATY) /** * @brief Set IO level for WS2812 bus * @param data_bit Generate buf * @warning "_nop()_" is counts at 24MHz for 1T 8051 MCU, * and it still needs to adjust the time delay for better stability with * the oscilloscope */ void WS2812_SetBit(uint8_t dataBit) { if(dataBit) { // 24MHz-1T GPIO_SET_H(WS2812_PIN); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); GPIO_SET_L(WS2812_PIN); _nop_(); _nop_(); _nop_(); } else { GPIO_SET_H(WS2812_PIN); _nop_(); _nop_(); _nop_(); GPIO_SET_L(WS2812_PIN); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } } #endif /* __STC51_ATY */ #elif defined (__STM32_HAL_ATY) #elif defined (__ESP8266_ATY) #include "HW_SPI_ATY.h" /** * @brief Set IO level for WS2812 bus * @param data_bit Generate buf * @warning this type use spi bus, so WS2812 DI must connect GPIO13, * and cannot use SPI anymore * and put esp8266_spi_init(); at MainInit_User(){} */ void WS2812_SetBit(uint8_t dataBit) { if(dataBit) SPI_Write(0xFC, 1); else SPI_Write(0x3F, 1); } #endif /* PLATFORM */ /** * @brief Set colors * @param R Color red * @param G Color green * @param B Color blue */ void WS2812_SetColor(uint8_t R, uint8_t G, uint8_t B) { WS2812_SetBit(G & 0X80); WS2812_SetBit(G & 0X40); WS2812_SetBit(G & 0X20); WS2812_SetBit(G & 0X10); WS2812_SetBit(G & 0X08); WS2812_SetBit(G & 0X04); WS2812_SetBit(G & 0X02); WS2812_SetBit(G & 0X01); WS2812_SetBit(R & 0X80); WS2812_SetBit(R & 0X40); WS2812_SetBit(R & 0X20); WS2812_SetBit(R & 0X10); WS2812_SetBit(R & 0X08); WS2812_SetBit(R & 0X04); WS2812_SetBit(R & 0X02); WS2812_SetBit(R & 0X01); WS2812_SetBit(B & 0X80); WS2812_SetBit(B & 0X40); WS2812_SetBit(B & 0X20); WS2812_SetBit(B & 0X10); WS2812_SetBit(B & 0X08); WS2812_SetBit(B & 0X04); WS2812_SetBit(B & 0X02); WS2812_SetBit(B & 0X01); } #ifdef __DEBUG_WS2812_ATY /** * @brief Basic colors diaplay to test the code is right or not */ void WS2812_Test(void) { #define MAX_TIME_WS2812_TEST 10000 uint16_t i = 0; WS2812_SetColor(255, 0, 0); for(i = 0; i < 10000; i++){} WS2812_SetColor(255, 255, 0); for(i = 0; i < 10000; i++){} WS2812_SetColor(0, 255, 0); for(i = 0; i < 10000; i++){} WS2812_SetColor(0, 255, 255); for(i = 0; i < 10000; i++){} WS2812_SetColor(0, 0, 255); for(i = 0; i < 10000; i++){} WS2812_SetColor(255, 0, 255); for(i = 0; i < 10000; i++){} } /** * @brief RGB flow with color gradual change */ void WS2812_StyleA(void) { // (255, 0 ,0) // (255, 255 ,0) // (0, 255 ,0) // (0, 255 ,255) // (0, 0 ,255) // (255, 0 ,255) // (255, 0 ,0) uint16_t i = 0, j = 0; uint16_t r = 255, g = 0, b = 0; uint8_t staRGB = 0; while(1) { // for(i = 0; i < 80; i++) { // for(j = 0; j < 7; j++) WS2812_SetColor(r, g, b); { uint8_t i_t = 0; for(i = 0; i < 100; i++){} } } switch(staRGB) { case 0: g++; break; case 1: r--; break; case 2: b++; break; case 3: g--; break; case 4: r++; break; case 5: b--; break; } if(r >= 255 && g <= 0 && b <= 0) staRGB = 0; else if(r >= 255 && g >= 255 && b <= 0) staRGB = 1; else if(r <= 0 && g >= 255 && b <= 0) staRGB = 2; else if(r <= 0 && g >= 255 && b >= 255) staRGB = 3; else if(r <= 0 && g <= 0 && b >= 255) staRGB = 4; else if(r >= 255 && g <= 0 && b >= 255) staRGB = 5; } } // todo: not tested //void main() //{ // while (1) // { // // WS2812_test(); // WS2812_styleA(); // } //} #endif /* __DEBUG_WS2812_ATY */ #endif /* __WS2812_ATY_C */ /******************************** End Of File *********************************/