本帖最后由 kilmty 于 2017-3-30 13:51 编辑
本测试使用onenet云平台,实现网页或手机端对幻彩灯条的颜色控制。1,WS2812简介
2,程序更改
a,增加控制幻彩灯条颜色的数据流,如下
- //数据流
- DATA_STREAM dataStream[] = {
- {"colourlight_r", &rgbStatus.lightrSta, TYPE_UCHAR, 1},
- {"colourlight_g", &rgbStatus.lightgSta, TYPE_UCHAR, 1},
- {"colourlight_b", &rgbStatus.lightbSta, TYPE_UCHAR, 1},
- {"Red_Led", &ledStatus.Led4Sta, TYPE_BOOL, 1},
- {"Green_Led", &ledStatus.Led5Sta, TYPE_BOOL, 1},
- {"Yellow_Led", &ledStatus.Led6Sta, TYPE_BOOL, 1},
- {"Blue_Led", &ledStatus.Led7Sta, TYPE_BOOL, 1},
- {"beep", &beepInfo.Beep_Status, TYPE_BOOL, 1},
- {"temperature", &sht20Info.tempreture, TYPE_FLOAT, 1},
- {"humidity", &sht20Info.humidity, TYPE_FLOAT, 1},
- {"Xg", &adxlInfo.incidence_Xf, TYPE_FLOAT, 1},
- {"Yg", &adxlInfo.incidence_Yf, TYPE_FLOAT, 1},
- {"Zg", &adxlInfo.incidence_Zf, TYPE_FLOAT, 1},
- {"errType", &faultTypeReport, TYPE_UCHAR, 1},
- };
复制代码
b,WS2812驱动文件 rgbled.c
- //µ¥Æ¬»úÍ·Îļþ
- #include "STM32f10x.h"
- //LEDÍ·Îļþ
- #include "rgb_led.h"
- RGB_STATUS rgbStatus;
- /*
- ************************************************************
- * º¯ÊýÃû³Æ£º Led_Init
- *
- * º¯Êý¹¦ÄÜ£º LED³õʼ»¯
- *
- * Èë¿Ú²ÎÊý£º ÎÞ
- *
- * ·µ»Ø²ÎÊý£º ÎÞ
- *
- * ˵Ã÷£º LED4-PC7 LED5-PC8 LED6-PA12 LED7-PC10
- ¸ßµçƽ¹ØµÆ µÍµçƽ¿ªµÆ
- ************************************************************
- */
- void RGB_Led_Init(void)
- {
-
- GPIO_InitTypeDef gpioInitStrcut;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //´ò¿ªGPIOAºÍGPIOCµÄʱÖÓ
-
- gpioInitStrcut.GPIO_Mode = GPIO_Mode_Out_PP; //ÉèÖÃΪÍÆÍìÊä³öģʽ
- gpioInitStrcut.GPIO_Pin = GPIO_Pin_1; //³õʼ»¯Pin7¡¢8¡¢10
- gpioInitStrcut.GPIO_Speed = GPIO_Speed_50MHz; //³ÐÔصÄ×î´óƵÂÊ
- GPIO_Init(GPIOA, &gpioInitStrcut); //³õʼ»¯GPIOC
-
- }
- void Delay(__IO u32 nCount) //¼òµ¥µÄÑÓʱº¯Êý
- {
- for(; nCount != 0; nCount--);
- }
- /******************************************************************/
- /* Êý¾Ý´«Êä800Kº¯Êý */
- /******************************************************************/
- void send_dat(u8 dat) //¸ßλÏÈ´«
- {
- unsigned char a;
- for(a=0;a<8;a++) //6201 a=8; 6205 a=12
- {
- if(dat&0x80) //6201 dat & 0x80; 6205 dat & 0x800
- {
- GPIO_SetBits(GPIOA, GPIO_Pin_1);
- Delay(0x000001);
- GPIO_ResetBits(GPIOA, GPIO_Pin_1);
- }
- else
- {
- GPIO_SetBits(GPIOA, GPIO_Pin_1);
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- GPIO_ResetBits(GPIOA, GPIO_Pin_1);
- __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
- }
- dat <<=1; //dat×óÒÆ1λ
- }
- }
- /******************************************************************/
- /* resetÂ뺯Êý */
- /******************************************************************/
- void reset ()
- {
- GPIO_ResetBits(GPIOA, GPIO_Pin_1);
- Delay(0x00013F);
- }
- /******************************************************************/
- /* ͬ²½ÑÕÉ«ÏÔʾº¯Êý */
- /******************************************************************/
- void send_tongbu(unsigned char R_lever, unsigned char G_lever, unsigned char B_lever)
- {
- unsigned char n;
- for(n=20;n>0;n--)
- {
- send_dat(R_lever);
- send_dat(G_lever);
- send_dat(B_lever);
- }
-
- reset();
- rgbStatus.lightrSta = R_lever;
- rgbStatus.lightgSta = G_lever;
- rgbStatus.lightbSta = B_lever;
- }
复制代码
c,rgbled.h
- #ifndef _RGB_LED_H_
- #define _RGB_LED_H_
- typedef struct
- {
- unsigned char lightrSta;
- unsigned char lightgSta;
- unsigned char lightbSta;
- } RGB_STATUS;
- extern RGB_STATUS rgbStatus;
- void RGB_Led_Init(void);
- void send_tongbu(unsigned char R_lever,unsigned char G_lever,unsigned char B_lever);
- #endif
复制代码
3,云平台增加数据流,编辑应用
左边5个按钮使灯条呈现固定的5种颜色,并将颜色值在右边旋钮显示,右边的旋钮可以让灯条表现出任意颜色
4,成果展示
a,按钮控制
|