1 /*
2  * Copyright (C) 2022 HiHope Open Source Organization .
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  *
14  * limitations under the License.
15  */
16 
17 #include "ohos_init.h"
18 #include "cmsis_os2.h"
19 #include "iot_gpio.h"
20 #include "hi_io.h"
21 #include "hi_time.h"
22 #include "iot_watchdog.h"
23 #include "robot_control.h"
24 #include "iot_errno.h"
25 #include "hi_pwm.h"
26 #include "hi_timer.h"
27 #include "iot_pwm.h"
28 
29 #define GPIO0 0
30 #define GPIO1 1
31 #define GPIO9 9
32 #define GPIO10 10
33 #define GPIO11 11
34 #define GPIO12 12
35 #define GPIO_FUNC 0
36 #define car_speed_left 0
37 #define car_speed_right 0
38 
39 unsigned int g_car_speed_left = car_speed_left;
40 unsigned int g_car_speed_right = car_speed_right;
41 IotGpioValue io_status_left;
42 IotGpioValue io_status_right;
timer1_callback(unsigned int arg)43 void timer1_callback(unsigned int arg)
44 {
45     IotGpioValue io_status;
46     if (g_car_speed_left != car_speed_left) {
47         IoTGpioGetInputVal(GPIO11, &io_status);
48         if (io_status != IOT_GPIO_VALUE0) {
49             g_car_speed_left = car_speed_left;
50             printf("left speed change \r\n");
51         }
52     }
53 
54     if (g_car_speed_right != car_speed_right) {
55         IoTGpioGetInputVal(GPIO12, &io_status);
56         if (io_status != IOT_GPIO_VALUE0) {
57             g_car_speed_right = car_speed_right;
58             printf("right speed change \r\n");
59         }
60     }
61     if (g_car_speed_left != car_speed_left && g_car_speed_right != car_speed_right) {
62         g_car_speed_left = car_speed_left;
63         g_car_speed_right = car_speed_right;
64     }
65     IoTGpioGetInputVal(GPIO11, &io_status_left);
66     IoTGpioGetInputVal(GPIO12, &io_status_right);
67     if (io_status_right == IOT_GPIO_VALUE0 && io_status_left != IOT_GPIO_VALUE0) {
68         g_car_speed_left = car_speed_left;
69         g_car_speed_right = 1;
70     }
71     if (io_status_right != IOT_GPIO_VALUE0 && io_status_left == IOT_GPIO_VALUE0) {
72         g_car_speed_left = 1;
73         g_car_speed_right = car_speed_right;
74     }
75 }
76 
trace_module(void)77 void trace_module(void)
78 {
79     unsigned int timer_id1;
80     hi_timer_create(&timer_id1);
81     hi_timer_start(timer_id1, HI_TIMER_TYPE_PERIOD, 1, timer1_callback, 0);
82     gpio_control(GPIO0, IOT_GPIO_VALUE1);
83     gpio_control(GPIO1, g_car_speed_left);
84     gpio_control(GPIO9, IOT_GPIO_VALUE1);
85     gpio_control(GPIO10, g_car_speed_right);
86     unsigned int delay_time = 20;
87 
88     while (1) {
89         gpio_control(GPIO1, g_car_speed_left);
90         gpio_control(GPIO10, g_car_speed_right);
91         unsigned char status = GetCarStatus();
92         hi_udelay(delay_time);
93 
94         if (status != CAR_TRACE_STATUS) {
95             break;
96         }
97     }
98     hi_timer_delete(timer_id1);
99 }
100