!27 增加traffic_light案例

Merge pull request !27 from 庞建文/master
This commit is contained in:
候鹏飞 2021-10-11 07:10:55 +00:00 committed by Gitee
commit 77eee5e02e
3 changed files with 305 additions and 0 deletions

View File

@ -0,0 +1,24 @@
# Copyright (c) 2021 Talkweb Co.,ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
static_library("module_traffic_light_example") {
sources = [
"traffic_light_example.c"
]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/kal/cmsis",
"//base/iot_hardware/peripheral/interfaces/kits"
]
}

View File

@ -0,0 +1,75 @@
# OpenHarmony模组开发-灯光控制和蜂鸣器使用
本案例使用到了四组LED灯、蜂鸣器和按键。通过按键每次按下来控制不同的灯亮以及改变蜂鸣器的状态、实现蜂鸣器的鸣叫。
# 目录结构:
## src
traffic_light_example.c --灯光和蜂鸣器控制源文件
# 相关API介绍
## traffic_light_example demo
IoTWatchDogDisable 关闭看门狗函数
IoTGpioInit(unsigned int id) 初始化GPIO
参数说明:
id Indicates the GPIO pin number.
IotIoSetFunc(IOT_GPIO_IO_NAME id, unsigned char val) 注册功能
参数说明:
-id Indicates the GPIO pin number.
-val 复用功能
IoTGpioSetDir(unsigned int id, IotGpioDir dir)Sets the direction for a GPIO pin.
参数说明:
-id Indicates the GPIO pin number.
-dir IO方向
IotIoSetPull(IOT_GPIO_IO_NAME id, IOT_IO_PULL val)设置GPIO为上拉或下拉
参数说明:
-id Indicates the GPIO pin number.
-val 上拉或下拉
IoTGpioRegisterIsrFunc(unsigned int id, IotGpioIntType intType, IotGpioIntPolarity intPolarity, GpioIsrCallbackFunc func, char *arg) Enables the interrupt feature for a GPIO pin.
参数说明:
-id Indicates the GPIO pin number.
-intType Indicates the interrupt type.
-intPolarity Indicates the interrupt polarity.
-func Indicates the interrupt callback function.
-arg Indicates the pointer to the argument used in the interrupt callback function.
IoTGpioSetOutputVal(unsigned int id, IotGpioValue val)Sets the output level value for a GPIO pin.
参数说明:
-id Indicates the GPIO pin number.
-val Indicates the output level value.
IoTPwmInit(unsigned int port) Initializes a PWM device.
参数说明:
-port Indicates the port number of the PWM device
osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)Creates an active thread.
参数说明:
-func Indicates the entry of the thread callback function.
-argument Indicates the pointer to the argument passed to the thread.
-attr Indicates the thread attributes.
IoTPwmStart(unsigned int port, unsigned short duty, unsigned int freq)Starts PWM signal output from a specified port based on the given output frequency and duty cycle.
参数说明:
-port Indicates the port number of the PWM device.
-duty Indicates the duty cycle for PWM signal output. The value ranges from 1 to 99.
-freq Indicates the frequency for PWM signal output.
IoTPwmStop(unsigned int port)Stops PWM signal output from a specified port.
参数说明:
-port Indicates the port number of the PWM device.
# 运行结果
先每个灯闪烁一下亮2秒灭1秒2遍测试4个灯是否正常。
按键每按下一次切换一次灯亮同时改变蜂鸣器状态蜂鸣器状态为1时蜂鸣。

View File

@ -0,0 +1,206 @@
/*
* Copyright (c) 2021 Talkweb Co.,ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_watchdog.h"
#include "iot_pwm.h"
#include "hi_pwm.h"
#include "hi_io.h"
//蜂鸣器PWM端口号
#define HI_PWM_BEEP_PORT HI_PWM_PORT_PWM0
//蜂鸣频率
#define PWM_BEEP_FREQ (4 * 1000)
//蜂鸣占空比
#define PWM_BEEP_DUTY (50)
// 蜂鸣器开关状态
static int gBeepState = 0;
typedef struct
{
IOT_GPIO_IO_NAME gpioName; //灯的GPIO
int ioFunc; //IO hi_io_func_gpio
IotGpioDir dir; //GPIO 方向
IotGpioValue value; //灯当前值
} TrafficLight;
//三色灯的状态数组,每个值代表某个颜色显示还是不显示
// 当前灯序号
static int gCurrentLightId = 0;
static TrafficLight lights[] = {
//red
{IOT_GPIO_IO_GPIO_10,
HI_IO_FUNC_GPIO_10_GPIO,
IOT_GPIO_DIR_OUT,
IOT_GPIO_VALUE0},
//GREEN
{IOT_GPIO_IO_GPIO_11,
HI_IO_FUNC_GPIO_11_GPIO,
IOT_GPIO_DIR_OUT,
IOT_GPIO_VALUE0},
//BLUE
{IOT_GPIO_IO_GPIO_12,
HI_IO_FUNC_GPIO_12_GPIO,
IOT_GPIO_DIR_OUT,
IOT_GPIO_VALUE0},
//WHITE
{IOT_GPIO_IO_GPIO_5,
HI_IO_FUNC_GPIO_5_GPIO,
IOT_GPIO_DIR_OUT,
IOT_GPIO_VALUE0}};
#define MAX_LIGHTS (sizeof(lights) / sizeof(lights[0]))
/**
*
* \param arg
*/
static void
OnKeyPressed(char *arg)
{
(void)arg;
printf("key pressed c\n");
//只亮当前灯亮
for (int i = 0; i < MAX_LIGHTS; i++)
{
if (i == gCurrentLightId)
{
lights[i].value = IOT_GPIO_VALUE1;
}
else
{
lights[i].value = IOT_GPIO_VALUE0;
}
}
//按一次键 切换一次当前灯
gCurrentLightId++;
if (gCurrentLightId == MAX_LIGHTS)
{
gCurrentLightId = 0;
}
//开关一次蜂鸣器
gBeepState = !gBeepState;
}
/**
* 线
* \param arg 线
*
*/
static void *LightTaskEntry(const char *arg)
{
int i, j;
(void)arg;
// 测试灯功能是否正常
// 先每个灯闪烁一下亮2秒灭1秒2遍
for (i = 0; i <= 1; i++)
{
for (j = 0; j < MAX_LIGHTS; j++)
{
IoTGpioSetOutputVal(lights[j].gpioName, IOT_GPIO_VALUE1);
usleep(200 * 1000);
IoTGpioSetOutputVal(lights[j].gpioName, IOT_GPIO_VALUE0);
usleep(100 * 1000);
}
}
//线程任务循环
while (1)
{
// 每个灯的状态输出到GPIO
for (i = 0; i < MAX_LIGHTS; i++)
{
IoTGpioSetOutputVal(lights[i].gpioName, lights[i].value);
}
// 根据蜂鸣器状态控制
if (gBeepState)
{
// 第二个参数duty分频系数第三个参数freq160M时钟
// freq选择40K占空达到一半输出的是4K频率
IoTPwmStart(HI_PWM_BEEP_PORT, PWM_BEEP_DUTY, PWM_BEEP_FREQ); //(WIFI_IOT_PWM_PORT_PWM0, 20*1000, 40*1000);
}
else
{
IoTPwmStop(HI_PWM_BEEP_PORT);
}
}
return NULL;
}
// 四色显示+蜂鸣。交通灯板,四色等对应
//red:GPIO10
//GREEN GPIO11
//YELLOW GPIO12
//WHITE GPIO13
//蜂鸣器GPIO09/PWM0按键中断GPIO08
static void LightExampleEntry(void)
{
IoTWatchDogDisable();
//初始化灯的 GPIO 状态
for (int i = 0; i < MAX_LIGHTS; i++)
{
TrafficLight *l = &lights[i];
IoTGpioInit(l->gpioName);
//注册功能
IotIoSetFunc(l->gpioName, l->ioFunc);
//注册io方向
IoTGpioSetDir(l->gpioName, l->dir);
}
//按键处理
IoTGpioInit(IOT_GPIO_IO_GPIO_8);
IotIoSetFunc(IOT_GPIO_IO_GPIO_8, HI_IO_FUNC_GPIO_8_GPIO);
//上升
IotIoSetPull(IOT_GPIO_IO_GPIO_8, IOT_IO_PULL_UP);
IoTGpioSetDir(IOT_GPIO_IO_GPIO_8, IOT_GPIO_DIR_IN);
//注册按键回调
IoTGpioRegisterIsrFunc(IOT_GPIO_IO_GPIO_8, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_RISE_LEVEL_HIGH, OnKeyPressed, NULL);
//蜂鸣器GPIO09/PWM0
IotIoSetFunc(IOT_GPIO_IO_GPIO_9, HI_IO_FUNC_GPIO_9_PWM0_OUT);
IoTGpioSetOutputVal(IOT_GPIO_IO_GPIO_9, IOT_GPIO_DIR_OUT);
// 初始化PWM设备这里是蜂鸣器
IoTPwmInit(HI_PWM_BEEP_PORT);
// 设置线程属性
osThreadAttr_t attr;
attr.name = "TrafficTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 1024;
attr.priority = osPriorityNormal;
// 开启线程执行LED任务。开启线程避免主线程阻塞
if (osThreadNew((osThreadFunc_t)LightTaskEntry, NULL, &attr) == NULL)
{
printf("TrafficLight thread Create failed.");
}
}
SYS_RUN(LightExampleEntry);