借助机智云平台用esp8266做一个商业化的七彩RGB彩灯
5.2 业务处理;其实整个业务处理是在 gizwits_product.c文件进行的!这里面的 gizwitsEventProcess()方法是处理从机智云 app下发的数据处理,这些数据定义都在服务器定义相约好的!有枚举、调色!
int8_t ICACHE_FLASH_ATTR gizwitsEventProcess(eventInfo_t *info, uint8_t *data, uint32_t len)
{
uint8_t i = 0;
dataPoint_t * dataPointPtr = (dataPoint_t *)data;
moduleStatusInfo_t * wifiData = (moduleStatusInfo_t *)data;
if((NULL == info) || (NULL == data))
{
GIZWITS_LOG("!!! gizwitsEventProcess Error \n");
return -1;
}
for(i = 0; i < info->num; i++)
{
switch(info->event)
{
//电源开关
case EVENT_power :
currentDataPoint.valueLED_Color = 3 ;
currentDataPoint.valuepower = dataPointPtr->valuepower;
GIZWITS_LOG("Evt: EVENT_power %d \n", currentDataPoint.valuepower);
//开灯
if (0x01 == currentDataPoint.valuepower) {
currentDataPoint.valueLED_R = 0;
currentDataPoint.valueLED_G = 250;
currentDataPoint.valueLED_B = 0;
//user handle
WS2812_SetFillColor(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B);
//关灯
} else {
currentDataPoint.valueLED_R = 0;
currentDataPoint.valueLED_G = 0;
currentDataPoint.valueLED_B = 0;
//user handle
WS2812_SetFillColor(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B);
}
break;
//指定颜色下发。
case EVENT_LED_Color:
currentDataPoint.valueLED_Color = dataPointPtr->valueLED_Color;
GIZWITS_LOG("Evt: EVENT_LED_Color %d\n", currentDataPoint.valueLED_Color) ;
switch (currentDataPoint.valueLED_Color) {
//黄色
case LED_Color_VALUE0:
currentDataPoint.valueLED_R = 254;
currentDataPoint.valueLED_G = 251;
currentDataPoint.valueLED_B = 0;
//user handle
WS2812_SetFillColor(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B);
break;
//紫色
case LED_Color_VALUE1:
currentDataPoint.valueLED_R = 167;
currentDataPoint.valueLED_G = 0;
currentDataPoint.valueLED_B = 254;
//user handle
WS2812_SetFillColor(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B);
break;
//橙色
case LED_Color_VALUE2:
//user handle
currentDataPoint.valueLED_R = 254;
currentDataPoint.valueLED_G = 0;
currentDataPoint.valueLED_B = 57;
//user handle
WS2812_SetFillColor(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B);
break;
case LED_Color_VALUE3:
break;
}
break;
case EVENT_LED_R:
currentDataPoint.valueLED_Color = 3 ;
currentDataPoint.valueLED_R= dataPointPtr->valueLED_R;
GIZWITS_LOG("Evt:EVENT_LED_R %d\n",currentDataPoint.valueLED_R);
//user handle
WS2812_SetFillColor(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B);
break;
case EVENT_LED_G:
currentDataPoint.valueLED_Color = 3 ;
currentDataPoint.valueLED_G= dataPointPtr->valueLED_G;
GIZWITS_LOG("Evt:EVENT_LED_G %d\n",currentDataPoint.valueLED_G);
//user handle
WS2812_SetFillColor(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B);
break;
case EVENT_LED_B:
currentDataPoint.valueLED_Color = 3 ;
currentDataPoint.valueLED_B= dataPointPtr->valueLED_B;
GIZWITS_LOG("Evt:EVENT_LED_B %d\n",currentDataPoint.valueLED_B);
//user handle
WS2812_SetFillColor(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B);
break;
case WIFI_SOFTAP:
break;
case WIFI_AIRLINK:
break;
case WIFI_STATION:
break;
case WIFI_CON_ROUTER:
GIZWITS_LOG("@@@@ connected router\n");
break;
case WIFI_DISCON_ROUTER:
GIZWITS_LOG("@@@@ disconnected router\n");
break;
case WIFI_CON_M2M:
GIZWITS_LOG("@@@@ connected m2m\n");
setConnectM2MStatus(0x01);
break;
case WIFI_DISCON_M2M:
GIZWITS_LOG("@@@@ disconnected m2m\n");
setConnectM2MStatus(0x00);
break;
case WIFI_RSSI:
GIZWITS_LOG("@@@@ RSSI %d\n", wifiData->rssi);
break;
case TRANSPARENT_DATA:
GIZWITS_LOG("TRANSPARENT_DATA \n");
//user handle , Fetch data from , size is
break;
case MODULE_INFO:
GIZWITS_LOG("MODULE INFO ...\n");
break;
default:
break;
}
}
system_os_post(USER_TASK_PRIO_2, SIG_UPGRADE_DATA, 0);
return 0;
}
原理图还有一个按键,我这里 的业务是 长按是 配网模式 ,短按是切换颜色,代码如下:
int flag = 0;
void timerSmartConfig_callback(void) {
flag++;
switch (flag) {
//切换为蓝色
case 1:
currentDataPoint.valueLED_R = 0;
currentDataPoint.valueLED_G = 0;
currentDataPoint.valueLED_B = 254;
//user handle
WS2812_SetFillColor(currentDataPoint.valueLED_R,
currentDataPoint.valueLED_G, currentDataPoint.valueLED_B);
break;
//切换为红色
case 2:
currentDataPoint.valueLED_R = 254;
currentDataPoint.valueLED_G = 0;
currentDataPoint.valueLED_B = 0;
//user handle
WS2812_SetFillColor(currentDataPoint.valueLED_R,
currentDataPoint.valueLED_G, currentDataPoint.valueLED_B);
break;
//切换为绿色
case 3:
currentDataPoint.valueLED_R = 0;
currentDataPoint.valueLED_G = 254;
currentDataPoint.valueLED_B = 0;
//user handle
WS2812_SetFillColor(currentDataPoint.valueLED_R,
currentDataPoint.valueLED_G, currentDataPoint.valueLED_B);
break;
case 4:
gizwitsSetMode(WIFI_AIRLINK_MODE);
flag = 0;
break;
}
}
LOCAL void ICACHE_FLASH_ATTR keyLongPress(void) {
currentDataPoint.valueLED_R = 0;
currentDataPoint.valueLED_G = 254;
currentDataPoint.valueLED_B = 0;
//user handle
WS2812_SetFillColor(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G, currentDataPoint.valueLED_B);
gizwitsSetMode(WIFI_AIRLINK_MODE);
GIZWITS_LOG("#### key2 long press, airlink mode\n");
}
/**
* Key to initialize
* @param none
* @return none
*/
LOCAL void ICACHE_FLASH_ATTR keyInit(void)
{
singleKey = keyInitOne(KEY_0_IO_NUM, KEY_0_IO_MUX, KEY_0_IO_FUNC,
keyLongPress, keyShortPress);
keys.singleKey = singleKey;
keyParaInit(&keys);
}
不错呀,学习了:)点赞
页:
[1]