#include "osapi.h"
#include "ets_sys.h"
#include "gpio.h"
#define GPIO_OUTPUT_SET(gpio_no, bit_value) gpio_output_set(bit_value<<gpio_no, ((~bit_value)&0x01)<<gpio_no, 1<<gpio_no,0)
void ICACHE_FLASH_ATTR SEND_WS_0()
{
uint8_t time;
time = 5; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 1 );
time = 17; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0 );
}
void ICACHE_FLASH_ATTR SEND_WS_1()
{
uint8_t time;
time = 19; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 1 );
time = 7; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0 );
}
void ICACHE_FLASH_ATTR SEND_WS_2()
{
uint8_t time;
time = 5; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0x4 );
time = 17; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0 );
}
void ICACHE_FLASH_ATTR SEND_WS_3()
{
uint8_t time;
time = 19; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0x4 );
time = 7; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0 );
}
void ICACHE_FLASH_ATTR SEND_WS_4()
{
uint8_t time;
time = 5; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0x2000 );
time = 17; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0 );
}
void ICACHE_FLASH_ATTR SEND_WS_5()
{
uint8_t time;
time = 19; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0x2000 );
time = 7; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0 );
}
void ICACHE_FLASH_ATTR SEND_WS_6()
{
uint8_t time;
time = 5; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0x4000 );
time = 17; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0 );
}
void ICACHE_FLASH_ATTR SEND_WS_7()
{
uint8_t time;
time = 19; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0x4000 );
time = 7; while(time--) WRITE_PERI_REG( PERIPHS_GPIO_BASEADDR , 0 );
}
void ICACHE_FLASH_ATTR ws2812_strip1(uint8_t * buffer, uint16_t length)
{
uint16_t i;
GPIO_OUTPUT_SET(GPIO_ID_PIN(0), 0);
os_intr_lock();
for( i = 0; i < length; i++ )
{
uint8_t mask = 0x80;
uint8_t byte = buffer[i];
while (mask)
{
( byte & mask ) ? SEND_WS_1() : SEND_WS_0();
mask >>= 1;
}
}
os_intr_unlock();
}
void ICACHE_FLASH_ATTR ws2812_strip2(uint8_t * buffer, uint16_t length)
{
uint16_t i;
GPIO_OUTPUT_SET(GPIO_ID_PIN(2), 0);
os_intr_lock();
for( i = 0; i < length; i++ )
{
uint8_t mask = 0x80;
uint8_t byte = buffer[i];
while (mask)
{
( byte & mask ) ? SEND_WS_3() : SEND_WS_2();
mask >>= 1;
}
}
os_intr_unlock();
}
void ICACHE_FLASH_ATTR ws2812_strip3(uint8_t * buffer, uint16_t length)
{
uint16_t i;
GPIO_OUTPUT_SET(GPIO_ID_PIN(13), 0);
os_intr_lock();
for( i = 0; i < length; i++ )
{
uint8_t mask = 0x80;
uint8_t byte = buffer[i];
while (mask)
{
( byte & mask ) ? SEND_WS_5() : SEND_WS_4();
mask >>= 1;
}
}
os_intr_unlock();
}
void ICACHE_FLASH_ATTR ws2812_strip4(uint8_t * buffer, uint16_t length)
{
uint16_t i;
GPIO_OUTPUT_SET(GPIO_ID_PIN(14), 0);
os_intr_lock();
for( i = 0; i < length; i++ )
{
uint8_t mask = 0x80;
uint8_t byte = buffer[i];
while (mask)
{
( byte & mask ) ? SEND_WS_7() : SEND_WS_6();
mask >>= 1;
}
}
os_intr_unlock();
}
void ICACHE_FLASH_ATTR ws2812_init()
{
ets_wdt_disable();
char outbuffer[] = { 0x00, 0x00, 0x00 };
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U,FUNC_GPIO0);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U,FUNC_GPIO2);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U,FUNC_GPIO13);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U,FUNC_GPIO14);
ws2812_strip1( outbuffer, sizeof(outbuffer));
ws2812_strip2( outbuffer, sizeof(outbuffer));
ws2812_strip3( outbuffer, sizeof(outbuffer));
ws2812_strip4( outbuffer, sizeof(outbuffer));
}