继开发环境搭建、第一行代码编写之后。来一个新的大应用,主要驱动SSD1306和LED灯。
开发板插上OLED模块和LED交通灯模块,程序模仿交通灯,红灯常亮5秒,然后闪烁3秒,后黄灯闪烁3秒,后绿灯常亮5秒,再是绿灯闪烁3秒,一直循环。OLED显示灯亮时间并倒计时。
SSD1306驱动接口如下:
/*
* @bref 向ssd1306 屏幕寄存器写入命令
* status 0:表示写入成功,否则失败
*/
static unsigned int I2cWriteByte(unsigned char regAddr, unsigned char cmd)
{
unsigned int status;
unsigned char sendLen = 2;
unsigned char userData = cmd;
IotI2cData oledI2cCmd = { 0 };
IotI2cData oledI2cWriteCmd = { 0 };
unsigned char sendUserCmd [SEND_CMD_LEN] = {OLED_ADDRESS_WRITE_CMD, userData};
unsigned char sendUserData [SEND_CMD_LEN] = {OLED_ADDRESS_WRITE_DATA, userData};
/* 如果是写命令,发写命令地址0x00 */
if (regAddr == OLED_ADDRESS_WRITE_CMD) {
oledI2cWriteCmd.sendBuf = sendUserCmd;
oledI2cWriteCmd.sendLen = sendLen;
status = IoTI2cWrite(IOT_I2C_IDX_0, OLED_ADDRESS, oledI2cWriteCmd.sendBuf, oledI2cWriteCmd.sendLen);
if (status != 0) {
return status;
}
} else if (regAddr == OLED_ADDRESS_WRITE_DATA) {
oledI2cCmd.sendBuf = sendUserData;
oledI2cCmd.sendLen = sendLen;
status = IoTI2cWrite(IOT_I2C_IDX_0, OLED_ADDRESS, oledI2cCmd.sendBuf, oledI2cCmd.sendLen);
if (status != 0) {
return status;
}
}
return 0;
}
/* 写命令操作 */
static unsigned int WriteCmd(unsigned char cmd) // 写命令
{
unsigned char status;
/* 写设备地址 */
status = I2cWriteByte(OLED_ADDRESS_WRITE_CMD, cmd);
if (status != 0) {
return -1;
}
}
/* 写数据操作 */
static unsigned int WriteData(unsigned char i2cData) // 写数据
{
unsigned char status;
/* 写设备地址 */
status = I2cWriteByte(OLED_ADDRESS_WRITE_DATA, i2cData);
if (status != 0) {
return -1;
}
}
应用使用到I2C和PWM功能,需要在配置文件中打开,具体操作打开device/soc/hisilicon/hi3861v100/sdk_liteos/build/config/usr_config.mk文件,做如下修改启用I2C和PWM功能:
CONFIG_I2C_SUPPORT=y
CONFIG_PWM_SUPPORT=y
基于之前搭建的HarmonyOS开发环境,将交通灯应用的源码下载到./applications/sample/wifi-iot/app/目录中,同时修改app种的BUILD.gn文件,在features字段中增加索引,使目标模块参与编译,具体字段配置如下。
import("//build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [
"traffic_light_demo:appDemoTrafficSample",
]
}
尤其注意的是 "traffic_light_demo:appDemoTrafficSample",前面是文件夹名字,后面是编译后静态库文件名,要跟traffic_light_demo文件夹中BUILD.gn的sources内容对应上。
hb set之后选板子设置环境,然后hb build -f即可。

IMG烧写进去后,RST板子,log打印如下:
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
FileSystem mount ok.
wifi init success!
hilog will init.
hievent will init.
hievent init success.
Please implement the interface according to the platform!
register gpio 5
register gpio8
traffic light open ok
hiview init success.
程序工作正常:

最后来一段工作视频。