【机智云Gokit3测评】设备接入-步骤二:程序移植
[复制链接]
1.写在前面
按照新建工程的步骤(https://bbs.eeworld.com.cn/thread-1155807-1-1.html)生成程序模板以后,此时程序还不能使用,必须进行移植以后才能使用。
2.下载软件包
进入机智云官网的开发者中心后,点击“下载中心”(https://download.gizwits.com/zh-cn/p/92/93),点击“GoKit MCU示例工程”,选择“微信宠物屋 for GoKit 2/3 STM32 V03010101”,点击下载。
3.解压下载的文件包
该程序文件主要时驱动包,包含了STM32的各种驱动库,以及机智云做的外设驱动程序等。
4.替换配置文件
将下载中心得到的CubeMX配置文件,替换掉自动生成代码中的原配置文件:
5.打开配置文件后重新生产配置文件
打开配置文件,点击重新生成对应配置的STM32代码。
6.移植驱动代码
将下载的文件夹名“驱动库代码_CubeMX版”里的内容全部复制到MCU_STM32F103C8x_source/hal文件目录下。
7.添加移植的驱动文件
打开工程文件后,将移植的驱动文件全部添加到工程目录下;
8.添加头文件
在驱动代码的delay.c、hal_infrared.c、hal_motor.c、hal_rgb_led.c、hal_temp_hum.c文件加入”main.h”头文件
#include "main.h"
9.在代码中添加相应的函数调用
(1)在 MCU_STM32F103C8x_source\Src\main.c 和 MCU_STM32F103C8x_source\Gizwits\gizwits_product.c 文件中添加驱动库的头文件
#include "delay.h"
#include "hal_motor.h"
#include "hal_rgb_led.h"
#include "hal_temp_hum.h"
#include "hal_infrared.h"
(2)在 MCU_STM32F103C8x_source\Gizwits\gizwits_product.c 文件的 userInit( ) 函数中添加各sensor的初始化
void userInit(void)
{
memset((uint8_t *)¤tDataPoint, 0, sizeof(dataPoint_t));
delay_init(72); // 延时 初始化
rgbLedInit(); // RGB LED 初始化
dht11Init(); // 温湿度初始化
irInit(); // 红外初始化
motorInit(); // 电机初始化
motorStatus(0); // 电机转速初始化
}
(3)在 MCU_STM32F103C8x_source\Gizwits\gizwits_product.c 文件的 userHandle( ) 函数中添加只读型传感器数据点相关的代码
void userHandle(void)
{
uint8_t ret = 0;
static uint32_t thLastTimer = 0;
///< 新添加代码: 红外传感器数据获取
currentDataPoint.valueInfrared = irHandle();
///< 新添加代码: 温湿度传感器数据获取
if((gizGetTimerCount() - thLastTimer) > 2000) //上报间隔2S
{
ret = dht11Read((uint8_t *)¤tDataPoint.valueTemperature,
(uint8_t *)¤tDataPoint.valueHumidity);
if(ret != 0)
{
printf("Failed to read DHT11 [%d] \n", ret);
}
thLastTimer = gizGetTimerCount();
}
}
(4)在 MCU_STM32F103C8x_source\User\main.c 文件的 key2ShortPress( ) 函数与 key2LongPress( ) 函数中添加长/短按key2时的LED点亮代码
void key2ShortPress(void)
{
GIZWITS_LOG("KEY2 PRESS ,Soft AP mode\n");
#if !MODULE_TYPE
gizwitsSetMode(WIFI_SOFTAP_MODE);
#endif
//Soft AP mode, RGB red
ledRgbControl(250, 0, 0);
}
void key2LongPress(void)
{
//AirLink mode
GIZWITS_LOG("KEY2 PRESS LONG ,AirLink mode\n");
#if !MODULE_TYPE
gizwitsSetMode(WIFI_AIRLINK_MODE);
#endif
//AirLink mode, RGB Green
ledRgbControl(0, 250, 0);
}
(5)在代码\Gizwits\gizwits_product.c 中 gizwitsEventProcess() 函数中添加相应代码(代码中的注释//user handle的下一行即可加入自己的应用代码):
该函数主要功能是完成写类型外设的事件处理。
int8_t gizwitsEventProcess(eventInfo_t *info, uint8_t *gizdata, uint32_t len)
{
uint8_t i = 0;
dataPoint_t *dataPointPtr = (dataPoint_t *)gizdata;
moduleStatusInfo_t *wifiData = (moduleStatusInfo_t *)gizdata;
protocolTime_t *ptime = (protocolTime_t *)gizdata;
#if MODULE_TYPE
gprsInfo_t *gprsInfoData = (gprsInfo_t *)gizdata;
#else
moduleInfo_t *ptModuleInfo = (moduleInfo_t *)gizdata;
#endif
if((NULL == info) || (NULL == gizdata))
{
return -1;
}
for(i=0; i<info->num; i++)
{
switch(info->event[i])
{
case EVENT_LED_OnOff:
currentDataPoint.valueLED_OnOff = dataPointPtr->valueLED_OnOff;
GIZWITS_LOG("Evt: EVENT_LED_OnOff %d \n", currentDataPoint.valueLED_OnOff);
if(0x01 == currentDataPoint.valueLED_OnOff)
{
//user handle
ledRgbControl(254, 0, 0);
}
else
{
//user handle
ledRgbControl(0, 0, 0);
}
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:
//user handle
ledRgbControl(currentDataPoint.valueLED_R, currentDataPoint.valueLED_G,
currentDataPoint.valueLED_B);
break;
case LED_Color_VALUE1:
//user handle
ledRgbControl(254, 254, 0);
break;
case LED_Color_VALUE2:
//user handle
ledRgbControl(254, 0, 70);
break;
case LED_Color_VALUE3:
//user handle
ledRgbControl(238, 30, 30);
break;
default:
break;
}
break;
case EVENT_LED_R:
currentDataPoint.valueLED_R = dataPointPtr->valueLED_R;
GIZWITS_LOG("Evt:EVENT_LED_R %d\n",currentDataPoint.valueLED_R);
//user handle
ledRgbControl(currentDataPoint.valueLED_R, currentDataPoint.valueLED_G,
currentDataPoint.valueLED_B);
break;
case EVENT_LED_G:
currentDataPoint.valueLED_G = dataPointPtr->valueLED_G;
GIZWITS_LOG("Evt:EVENT_LED_G %d\n",currentDataPoint.valueLED_G);
//user handle
ledRgbControl(currentDataPoint.valueLED_R, currentDataPoint.valueLED_G,
currentDataPoint.valueLED_B);
break;
case EVENT_LED_B:
currentDataPoint.valueLED_B = dataPointPtr->valueLED_B;
GIZWITS_LOG("Evt:EVENT_LED_B %d\n",currentDataPoint.valueLED_B);
//user handle
ledRgbControl(currentDataPoint.valueLED_R, currentDataPoint.valueLED_G,
currentDataPoint.valueLED_B);
break;
case EVENT_Motor_Speed:
currentDataPoint.valueMotor_Speed = dataPointPtr->valueMotor_Speed;
GIZWITS_LOG("Evt:EVENT_Motor_Speed %d\n",currentDataPoint.valueMotor_Speed);
//user handle
motorStatus(currentDataPoint.valueMotor_Speed);
break;
case WIFI_SOFTAP:
break;
case WIFI_AIRLINK:
break;
case WIFI_STATION:
break;
case WIFI_CON_ROUTER:
ledRgbControl(0, 0, 0);
break;
case WIFI_DISCON_ROUTER:
break;
case WIFI_CON_M2M:
break;
case WIFI_DISCON_M2M:
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 [data] , size is [len]
break;
case WIFI_NTP:
GIZWITS_LOG("WIFI_NTP : [%d-%d-%d %02d:%02d:%02d][%d] \n",ptime->year,ptime->month,
ptime->day,ptime->hour,ptime->minute,ptime->second,ptime->ntp);
break;
case MODULE_INFO:
GIZWITS_LOG("MODULE INFO ...\n");
#if MODULE_TYPE
GIZWITS_LOG("GPRS MODULE ...\n");
//Format By gprsInfo_t
#else
GIZWITS_LOG("WIF MODULE ...\n");
//Format By moduleInfo_t
GIZWITS_LOG("moduleType : [%d] \n",ptModuleInfo->moduleType);
#endif
break;
default:
break;
}
}
return 0;
}
9.编译文件
编译没有错误。
10.下一章节讲解烧写固件程序。
|