1 /*
2 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdio.h>
17 #include <unistd.h>
18 #include "ohos_init.h"
19 #include "cmsis_os2.h"
20 #include "iot_i2c.h"
21 #include "iot_gpio.h"
22 #include "iot_pwm.h"
23 #include "hi_gpio.h"
24 #include "app_demo_multi_sample.h"
25 #include "ssd1306_oled.h"
26 #include "app_demo_traffic_sample.h"
27
28 #define IOT_GPIO_INDEX_9 9
29 #define IOT_GPIO_INDEX_10 10
30 #define IOT_GPIO_INDEX_11 11
31 #define IOT_GPIO_INDEX_12 12
32 #define PWM_CLK_160M 160000000
33 #define IO_FUNC_GPIO_9_PWM0_OUT 5
34 #define IO_FUNC_GPIO_OUT 0
35 #define HI_FRQ_31 (31)
36 /*
37 * @bref traffic control mode
38 * @param void
39 */
TrafficControlModeSample(void)40 void TrafficControlModeSample(void)
41 {
42 IoTPwmInit(0); /* PWM0 */
43 IoSetFunc(IOT_GPIO_INDEX_9, IO_FUNC_GPIO_9_PWM0_OUT); // set Gpio9 PWM
44 IoTGpioSetDir(IOT_GPIO_INDEX_9, IOT_GPIO_DIR_OUT);
45 unsigned char beep = 0;
46
47 unsigned char currentType = 0;
48 unsigned char currentMode = 0;
49
50 currentMode = GetKeyStatus(CURRENT_MODE);
51 currentType = GetKeyStatus(CURRENT_TYPE);
52 switch (GetKeyStatus(CURRENT_TYPE)) {
53 case RED_ON:
54 GpioControl(IOT_GPIO_INDEX_10, IOT_GPIO_INDEX_10,
55 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE1, IO_FUNC_GPIO_OUT);
56 GpioControl(IOT_GPIO_INDEX_11, IOT_GPIO_INDEX_11,
57 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE0, IO_FUNC_GPIO_OUT);
58 break;
59 case YELLOW_ON:
60 GpioControl(IOT_GPIO_INDEX_10, IOT_GPIO_INDEX_10,
61 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE0, IO_FUNC_GPIO_OUT);
62 GpioControl(IOT_GPIO_INDEX_12, IOT_GPIO_INDEX_12,
63 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE1, IO_FUNC_GPIO_OUT);
64 break;
65 case GREEN_ON:
66 GpioControl(IOT_GPIO_INDEX_12, IOT_GPIO_INDEX_12,
67 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE0, IO_FUNC_GPIO_OUT);
68 GpioControl(IOT_GPIO_INDEX_11, IOT_GPIO_INDEX_11,
69 IOT_GPIO_DIR_OUT, IOT_GPIO_VALUE1, IO_FUNC_GPIO_OUT);
70 break;
71 default:
72 break;
73 }
74
75 while (1) {
76 if ((currentMode != GetKeyStatus(CURRENT_MODE)) || (currentType != GetKeyStatus(CURRENT_TYPE))) {
77 break;
78 }
79 if (GetKeyStatus(OC_BEEP_STATUS) == BEEP_ON) {
80 if (beep == BEEP_OFF) {
81 beep = BEEP_ON;
82 IoTPwmStart(HI_PWM0, HI_FRQ_31, PWM_CLK_160M / PWM_FULL_DUTY); /* PWM0 */
83 hi_udelay(DELAY_1_S);
84 } else {
85 beep = BEEP_OFF;
86 IoTPwmStart(HI_PWM0, PWM_LOW_DUTY, PWM_CLK_160M / PWM_FULL_DUTY); /* PWM0 */
87 hi_udelay(DELAY_1_S);
88 }
89 } else {
90 IoTPwmStart(HI_PWM0, PWM_LOW_DUTY, PWM_FULL_DUTY); /* PWM0 */
91 }
92
93 TaskMsleep(SLEEP_1_MS); // 1ms
94 }
95 }
96
97 /* traffic light function handle and display */
TrafficLightFunc(void)98 void TrafficLightFunc(void)
99 {
100 /* 初始化时屏幕 i2c baudrate setting */
101 IoTI2cInit(IOT_I2C_IDX_0, HI_I2C_IDX_BAUDRATE); /* baudrate: 400kbps */
102 IoTGpioInit(HI_GPIO_13); /* GPIO13 */
103 IoSetFunc(HI_GPIO_13, HI_I2C_SDA_SCL); /* GPIO13 SDA */
104 IoTGpioInit(HI_GPIO_14); /* GPIO14 */
105 IoSetFunc(HI_GPIO_14, HI_I2C_SDA_SCL); /* GPIO14 SCL */
106
107 TrafficDisplay();
108 }
109