Brief introduction
This article documented the process of controlling the WS2812B_RGB lamp using the STONE display module.
RGB lamp is a kind of lamp that we often see in our daily life. It is widely used, such as: city night scene, light show, indoor lighting, auxiliary lighting, advertising module lighting, luminous word, counter lighting, shopping mall lighting, jewelry display case lighting, etc.
GUI design
Connection
part of code
Video demo
This article documented the process of controlling the WS2812B_RGB lamp using the STONE display module.
RGB lamp is a kind of lamp that we often see in our daily life. It is widely used, such as: city night scene, light show, indoor lighting, auxiliary lighting, advertising module lighting, luminous word, counter lighting, shopping mall lighting, jewelry display case lighting, etc.
GUI design
Connection
part of code
C:
#define RED_COLOR 0x07
#define ICON_WHITE_ADDR 0x02
#define ICON_RED_ADDR 0x03
#define ICON_GREEN_ADDR 0x04
#define ICON_BLUE_ADDR 0x05
#define TEXT_RED_ADDR 0x07
#define TEXT_GREEN_ADDR 0x08
#define TEXT_BLUE_ADDR 0x09
#define TEXT_WHITE_ADDR 0x06
#define SWITCH_ONOFF_ADDR 0x01
#define ICON_ON 0x01
#define ICON_OFF 0x00
u8 data_send[8]= {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x00, 0x00,0x00};
Data sent to the display screen should be sent according to the corresponding format:
U8 data_send[8]= {0xA5, 0x5A, 0x05, 0x82, 0x00,0x00,0x00,0x00};
Data [4]\ data[5] is the high and low order of component addresses.
Data [6]\ data[7] is the data to be displayed by the component.
The main logical code will be provided below:
#include "stm32f10x.h"
#include "usart.h"
#include "delay.h"
#include "../BOARD/ws2812/ws2812.h"
struct RGB_COLOR
{
u8 C_RED;
u8 C_GREEN;
u8 C_BLUE;
u8 C_WHITE;
u8 C_RED_FLAG;
u8 C_GREEN_FLAG;
u8 C_BLUE_FLAG;
};
#define ICON_WHITE_ADDR 0x02
#define ICON_RED_ADDR 0x03
#define ICON_GREEN_ADDR 0x04
#define ICON_BLUE_ADDR 0x05
#define TEXT_RED_ADDR 0x07
#define TEXT_GREEN_ADDR 0x08
#define TEXT_BLUE_ADDR 0x09
#define TEXT_WHITE_ADDR 0x06
#define SWITCH_ONOFF_ADDR 0x01
#define ICON_ON 0x01
#define ICON_OFF 0x00
u8 data_send[8]= {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x00, 0x00,0x00};
void UART1_Send_Array(u8 send_array[],unsigned char num)
{
u8 i=0;
while(i
{
USART_SendData(USART1,send_array[i]);
while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
i++;
}
}
int main(void)
{
uart_init(115200);
delay_init();
struct RGB_COLOR USER_RGB_COLOR;
USER_RGB_COLOR.C_BLUE=0;
USER_RGB_COLOR.C_GREEN=0;
USER_RGB_COLOR.C_RED=0;
USER_RGB_COLOR.C_RED_FLAG=1;
USER_RGB_COLOR.C_GREEN_FLAG=1;
USER_RGB_COLOR.C_BLUE_FLAG=1;
u16 k,q;
u8 BLINK_2=0;
u8 USER_R=0,USER_G=0,USER_B=0,COLOR_TYPE=0,COLOR_DIR=0;
u8 blink_type=0;
u16 times=0;
RGB_LED_Init();
while(1)
{
if(USART_RX_END)
{
switch (USART_RX_BUF[5])
{
case 0x33:
PIXEL_NUM=USART_RX_BUF[8];
break;
case LED_ALL_ONOFF:
blink_type=0;
if(USART_RX_BUF[8]==0)
{
data_send[5]=ICON_RED_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_RED_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_GREEN_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_GREEN_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_BLUE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_BLUE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_BLUE=0;
USER_RGB_COLOR.C_GREEN=0;
USER_RGB_COLOR.C_RED=0;
data_send[5]=ICON_WHITE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_WHITE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_WHITE=0;
}
else
{
USER_RGB_COLOR.C_BLUE=0x32;
USER_RGB_COLOR.C_GREEN=0x10;
USER_RGB_COLOR.C_RED=0x24;
USER_RGB_COLOR.C_RED_FLAG=0;
USER_RGB_COLOR.C_GREEN_FLAG=0;
USER_RGB_COLOR.C_BLUE_FLAG=0;
data_send[5]=ICON_RED_ADDR;
data_send[7]=ICON_ON;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_RED_ADDR;
data_send[7]=0x24;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_GREEN_ADDR;
data_send[7]=ICON_ON;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_GREEN_ADDR;
data_send[7]=0x10;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_BLUE_ADDR;
data_send[7]=ICON_ON;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_BLUE_ADDR;
data_send[7]=0x32;
UART1_Send_Array(data_send,8);
}
RGB_LED_Write_24Bits(USER_RGB_COLOR.C_RED, USER_RGB_COLOR.C_GREEN, USER_RGB_COLOR.C_BLUE);
break;
case RED_COLOR:
blink_type=0;
if(USER_RGB_COLOR.C_RED_FLAG==1)
{
if(USART_RX_BUF[8]==0)
break;
}
data_send[5]=ICON_WHITE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_WHITE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_WHITE=0;
data_send[5]=SWITCH_ONOFF_ADDR;
data_send[7]=ICON_ON;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_RED_ADDR;
if(USART_RX_BUF[8]>0)data_send[7]=ICON_ON;
else data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_RED=USART_RX_BUF[8];
USER_RGB_COLOR.C_RED_FLAG=0;
if(USER_RGB_COLOR.C_RED==0)USER_RGB_COLOR.C_RED_FLAG=1;
if((USER_RGB_COLOR.C_RED==0x00)&&(USER_RGB_COLOR.C_GREEN==0x00)&&(USER_RGB_COLOR.C_BLUE==0x00)&&(USER_RGB_COLOR.C_WHITE==0x00))
{
data_send[5]=SWITCH_ONOFF_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
}
RGB_LED_Write_24Bits(USER_RGB_COLOR.C_RED, USER_RGB_COLOR.C_GREEN, USER_RGB_COLOR.C_BLUE); // Red
break;
case GREEN_COLOR:
blink_type=0;
if(USER_RGB_COLOR.C_GREEN_FLAG==1)
{
if(USART_RX_BUF[8]==0)
break;
}
data_send[5]=ICON_GREEN_ADDR;
if(USART_RX_BUF[8]>0)data_send[7]=ICON_ON;
else data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_WHITE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_WHITE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_WHITE=0;
data_send[5]=SWITCH_ONOFF_ADDR;
data_send[7]=ICON_ON;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_GREEN=USART_RX_BUF[8];
USER_RGB_COLOR.C_GREEN_FLAG=0;
if(USER_RGB_COLOR.C_GREEN==0)USER_RGB_COLOR.C_GREEN_FLAG=1;
if((USER_RGB_COLOR.C_RED==0x00)&&(USER_RGB_COLOR.C_GREEN==0x00)&&(USER_RGB_COLOR.C_BLUE==0x00)&&(USER_RGB_COLOR.C_WHITE==0x00))
{
data_send[5]=SWITCH_ONOFF_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
}
RGB_LED_Write_24Bits(USER_RGB_COLOR.C_RED, USER_RGB_COLOR.C_GREEN, USER_RGB_COLOR.C_BLUE); // Green
break;
case BLUE_COLOR:
blink_type=0;
if(USER_RGB_COLOR.C_BLUE_FLAG==1)
{
if(USART_RX_BUF[8]==0)
break;
}
data_send[5]=ICON_BLUE_ADDR;
if(USART_RX_BUF[8]>0)data_send[7]=ICON_ON;
else data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_WHITE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_WHITE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_WHITE=0;
data_send[5]=SWITCH_ONOFF_ADDR;
data_send[7]=ICON_ON;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_BLUE=USART_RX_BUF[8];
USER_RGB_COLOR.C_BLUE_FLAG=0;
if(USER_RGB_COLOR.C_BLUE==0)USER_RGB_COLOR.C_BLUE_FLAG=1;
if((USER_RGB_COLOR.C_RED==0x00)&&(USER_RGB_COLOR.C_GREEN==0x00)&&(USER_RGB_COLOR.C_BLUE==0x00)&&(USER_RGB_COLOR.C_WHITE==0x00))
{
data_send[5]=SWITCH_ONOFF_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
}
RGB_LED_Write_24Bits(USER_RGB_COLOR.C_RED, USER_RGB_COLOR.C_GREEN, USER_RGB_COLOR.C_BLUE); // Blue
break;
case WHITE_COLOR:
blink_type=0;
data_send[5]=ICON_WHITE_ADDR;
if(USART_RX_BUF[8]>0)data_send[7]=ICON_ON;
else data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_RED_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_RED_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_GREEN_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_GREEN_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_BLUE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_BLUE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_BLUE=0;
USER_RGB_COLOR.C_GREEN=0;
USER_RGB_COLOR.C_RED=0;
USER_RGB_COLOR.C_RED_FLAG=1;
USER_RGB_COLOR.C_GREEN_FLAG=1;
USER_RGB_COLOR.C_BLUE_FLAG=1;
data_send[5]=SWITCH_ONOFF_ADDR;
data_send[7]=ICON_ON;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_WHITE=USART_RX_BUF[8];
if((USER_RGB_COLOR.C_RED==0x00)&&(USER_RGB_COLOR.C_GREEN==0x00)&&(USER_RGB_COLOR.C_BLUE==0x00)&&(USER_RGB_COLOR.C_WHITE==0x00))
{
data_send[5]=SWITCH_ONOFF_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
}
RGB_LED_Write_24Bits(USER_RGB_COLOR.C_WHITE, USER_RGB_COLOR.C_WHITE, USER_RGB_COLOR.C_WHITE);
break;
case BLINK1:
blink_type=1;
data_send[5]=ICON_RED_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_RED_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_GREEN_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_GREEN_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_BLUE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_BLUE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_BLUE=0;
USER_RGB_COLOR.C_GREEN=0;
USER_RGB_COLOR.C_RED=0;
data_send[5]=ICON_WHITE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_WHITE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_WHITE=0;
data_send[5]=SWITCH_ONOFF_ADDR;
data_send[7]=ICON_ON;
UART1_Send_Array(data_send,8);
break;
case BLINK2:
blink_type=2;
data_send[5]=ICON_RED_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_RED_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_GREEN_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_GREEN_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_BLUE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_BLUE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_BLUE=0;
USER_RGB_COLOR.C_GREEN=0;
USER_RGB_COLOR.C_RED=0;
data_send[5]=ICON_WHITE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_WHITE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_WHITE=0;
data_send[5]=SWITCH_ONOFF_ADDR;
data_send[7]=ICON_ON;
UART1_Send_Array(data_send,8);
break;
case BLINK3:
blink_type=3;
data_send[5]=ICON_RED_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_RED_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_GREEN_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_GREEN_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_BLUE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_BLUE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_BLUE=0;
USER_RGB_COLOR.C_GREEN=0;
USER_RGB_COLOR.C_RED=0;
// USER_RGB_COLOR.C_RED_FLAG=1;
// USER_RGB_COLOR.C_GREEN_FLAG=1;
// USER_RGB_COLOR.C_BLUE_FLAG=1;
data_send[5]=ICON_WHITE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_WHITE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_WHITE=0;
data_send[5]=SWITCH_ONOFF_ADDR;
data_send[7]=ICON_ON;
UART1_Send_Array(data_send,8);
break;
case BLINK4:
blink_type=4;
data_send[5]=ICON_RED_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_RED_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_GREEN_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_GREEN_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
data_send[5]=ICON_BLUE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_BLUE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_BLUE=0;
USER_RGB_COLOR.C_GREEN=0;
USER_RGB_COLOR.C_RED=0;
data_send[5]=ICON_WHITE_ADDR;
data_send[7]=ICON_OFF;
UART1_Send_Array(data_send,8);
data_send[5]=TEXT_WHITE_ADDR;
data_send[7]=0x00;
UART1_Send_Array(data_send,8);
USER_RGB_COLOR.C_WHITE=0;
data_send[5]=SWITCH_ONOFF_ADDR;
data_send[7]=ICON_ON;
UART1_Send_Array(data_send,8);
break;
default:
USART_RX_END=0;
USART_RX_STA=0;
break;
}
USART_RX_STA=0;
USART_RX_END=0;
}
else
{
if(blink_type==1)
{
times++;
if(times>=14)
{
times=0;
if(COLOR_DIR==0)
{
if(COLOR_TYPE==0)
{
USER_R++;
USER_G=0;
USER_B=0;
}
else if(COLOR_TYPE==1)
{
USER_R=0;
USER_G++;
USER_B=0;
}
else if(COLOR_TYPE==2)
{
USER_R=0;
USER_G=0;
USER_B++;
}
else if(COLOR_TYPE==3)
{
USER_R++;
USER_G++;
USER_B=0;
}
else if(COLOR_TYPE==4)
{
USER_R=0;
USER_G++;
USER_B++;
}
else if(COLOR_TYPE==5)
{
USER_R++;
USER_G=0;
USER_B++;
}
if((USER_R>=250)||(USER_G>=250)||(USER_B>=250))
{
COLOR_DIR=1;
}
}
else
{
if(COLOR_TYPE==0)
{
USER_R--;
USER_G=0;
USER_B=0;
}
else if(COLOR_TYPE==1)
{
USER_R=0;
USER_G--;
USER_B=0;
}
else if(COLOR_TYPE==2)
{
USER_R=0;
USER_G=0;
USER_B--;
}
else if(COLOR_TYPE==3)
{
USER_R--;
USER_G--;
USER_B=0;
}
else if(COLOR_TYPE==4)
{
USER_R=0;
USER_G--;
USER_B--;
}
else if(COLOR_TYPE==5)
{
USER_R--;
USER_G=0;
USER_B--;
}
if((USER_R==0x02)||(USER_G==0x02)||(USER_B==0x02))
{
COLOR_DIR=0;
COLOR_TYPE++;
if(COLOR_TYPE>5)
COLOR_TYPE=0;
}
}
RGB_LED_Write_24Bits(USER_R,USER_G,USER_B);
}
delay_ms(1);
}
else if(blink_type==2)
{
k++;
if(k>=150)
{
k=0;
q=200;
{
BLINK_2++;
if(BLINK_2>8)BLINK_2=0;
}
if(BLINK_2==0)
RGB_LED_Write_24Bits(q,0,0);
else if(BLINK_2==1)
RGB_LED_Write_24Bits(0,q,0);
else if(BLINK_2==2)
RGB_LED_Write_24Bits(0,0,q);
else if(BLINK_2==3)
RGB_LED_Write_24Bits(q,q,0);
else if(BLINK_2==4)
RGB_LED_Write_24Bits(0,q,q);
else if(BLINK_2==5)
RGB_LED_Write_24Bits(q,0,q);
else if(BLINK_2==6)
RGB_LED_Write_24Bits(q-100,q,0);
else if(BLINK_2==7)
RGB_LED_Write_24Bits(0,q-80,q);
else if(BLINK_2==8)
RGB_LED_Write_24Bits(q,0,q-120);
else if(BLINK_2==9)
RGB_LED_Write_24Bits(40,q-100,q-70);
else if(BLINK_2==10)
RGB_LED_Write_24Bits(q,100,q-80);
}
delay_ms(1);
}
else if(blink_type==3)
{
k++;
if(k>=1000)
{
k=0;
{
BLINK_2++;
if(BLINK_2>5)BLINK_2=0;
}
{
if(BLINK_2==0)
RGB_LED_Write_24Bits(q,0,0);
else if(BLINK_2==1)
RGB_LED_Write_24Bits(0,q,0);
else if(BLINK_2==2)
RGB_LED_Write_24Bits(0,0,q);
else if(BLINK_2==3)
RGB_LED_Write_24Bits(q,q,0);
else if(BLINK_2==4)
RGB_LED_Write_24Bits(0,q,q);
else if(BLINK_2==5)
RGB_LED_Write_24Bits(q,0,q);
}
}
delay_ms(1);
}
else if(blink_type==4)
{
k++;
if(k>=500)
{
k=0;
q=0;
BLINK_2++;
if(BLINK_2>5)BLINK_2=0;
}
q++;
if(q>=250)q=0;
if(BLINK_2==0)
RGB_LED_Write_24Bits(q,0,0);
else if(BLINK_2==1)
RGB_LED_Write_24Bits(0,q,0);
else if(BLINK_2==2)
RGB_LED_Write_24Bits(0,0,q);
else if(BLINK_2==3)
RGB_LED_Write_24Bits(q,q,0);
else if(BLINK_2==4)
RGB_LED_Write_24Bits(0,q,q);
else if(BLINK_2==5)
RGB_LED_Write_24Bits(q,0,q);
delay_ms(1);
}
else
{
}
}
}
}
Video demo