鸿蒙套件,智能家居套件(蜂鸣器,按键,LED灯)
利用鸿蒙套件进行简单的物联网模块(雏形)
代码:
static void OnButtonPressed_isr(char* arg)
{
(void)arg;
printf("[HelloWorld] OnButtonPressed()\n");
BtnPressed = 1;
BtnPressed_idx++;
BtnPressed_idx %= 4;
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_8,
WIFI_IOT_INT_TYPE_EDGE,
WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH,
OnButtonReleased_isr, NULL);
}
static void OnButtonReleased_isr(char* arg)
{
(void)arg;
printf("[HelloWorld] OnButtonReleased()\n");
BtnPressed = 0;
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_8,
WIFI_IOT_INT_TYPE_EDGE,
WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,
OnButtonPressed_isr, NULL);
}
//1.任务处理函数
static void* HelloWorld_Task(const char* arg)
{
int i = 0;
(void)arg;
printf("[HelloWorld] HelloWorld_Task()\n");
GpioInit();//使用GPIO,都需要调用该接口
//红黄绿灯扩展板上,对应的LED GPIO10,11,12初始化
printf("LED GPIO init...\r\n");
for(i=0; i<3; i++)
{
//复用引脚为 GPIO
IoSetFunc(LightGPIO[i], 0);
//设置为输出
GpioSetDir(LightGPIO[i], WIFI_IOT_GPIO_DIR_OUT);
}
//红黄绿灯扩展板上,对应的按键 GPIO8 初始化为GPIO,输入
printf("KEY GPIO init...\r\n");
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_FUNC_GPIO_8_GPIO);//GPIO
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_GPIO_DIR_IN);//设置为输入
IoSetPull(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_PULL_UP);
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_8,
WIFI_IOT_INT_TYPE_EDGE,
WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,
OnButtonPressed_isr, NULL);
//红黄绿灯扩展板上,对应的蜂鸣器 GPIO9 初始化为PWM功能
printf("PWM GPIO init...\r\n");
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT);//PWM
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_OUT);//设置为输出
PwmInit(WIFI_IOT_PWM_PORT_PWM0);//初始化PWM
while(1)
{
//logic code for task
//按一次按键,顺序点亮一个灯
int j = 0;
for(j = 0; j < 3; j++)
{
if(BtnPressed_idx == j)
{
GpioSetOutputVal(LightGPIO[j],WIFI_IOT_GPIO_VALUE1);//高电平,点亮灯
}
else
{
GpioSetOutputVal(LightGPIO[j],WIFI_IOT_GPIO_VALUE0);//关闭灯
}
}
#if 0
//流水灯点亮
for(j = 0; j< 3; j++)
{
GpioSetOutputVal(LightGPIO[j],WIFI_IOT_GPIO_VALUE0);//关闭所有灯
}
i = (i + 1) % 3;
GpioSetOutputVal(LightGPIO[i], WIFI_IOT_GPIO_VALUE1);//高电平,点亮灯
sleep(1);//延时1秒
#endif
//按键触发的处理
if(BtnPressed)
{
PwmStart(WIFI_IOT_PWM_PORT_PWM0, 5 * 1000, 40 * 1000);//按键按下,蜂鸣器发声
}else{
PwmStop(WIFI_IOT_PWM_PORT_PWM0);//按键抬起,蜂鸣器停止
}
//usleep(500000);
usleep(200000);
}
return NULL;
}
//2.任务入口函数
static void HelloWorld_Entry(void)
{
osThreadAttr_t attr = {0};
printf("[HelloWorld] HelloWorld_Entry()\n");
attr.name = "HelloWorld_Task";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 1024;//
attr.priority = osPriorityNormal;//
if (osThreadNew((osThreadFunc_t)HelloWorld_Task, NULL, &attr) == NULL)
{
printf("[HelloWorld] Falied to create LedTask!\n");
}
}