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 <stdio.h>
18 #include <stdlib.h>
19 #include <memory.h>
20
21 #include "ohos_init.h"
22 #include "cmsis_os2.h"
23 #include "iot_gpio.h"
24 #include "hi_io.h"
25 #include "hi_time.h"
26 #include "iot_pwm.h"
27 #include "hi_pwm.h"
28
29 #define GPIO0 0
30 #define GPIO1 1
31 #define GPIO9 9
32 #define GPIO10 10
33 #define GPIOFUNC 0
34 #define TASK_STAK_SIZE (1024*10)
35
gpio_control(unsigned int gpio,IotGpioValue value)36 void gpio_control (unsigned int gpio, IotGpioValue value)
37 {
38 hi_io_set_func(gpio, GPIOFUNC);
39 IoTGpioSetDir(gpio, IOT_GPIO_DIR_OUT);
40 IoTGpioSetOutputVal(gpio, value);
41 }
42
car_backward(void)43 void car_backward(void)
44 {
45 gpio_control(GPIO0, IOT_GPIO_VALUE0);
46 gpio_control(GPIO1, IOT_GPIO_VALUE1);
47 gpio_control(GPIO9, IOT_GPIO_VALUE0);
48 gpio_control(GPIO10, IOT_GPIO_VALUE1);
49 }
50
car_forward(void)51 void car_forward(void)
52 {
53 gpio_control(GPIO0, IOT_GPIO_VALUE1);
54 gpio_control(GPIO1, IOT_GPIO_VALUE0);
55 gpio_control(GPIO9, IOT_GPIO_VALUE1);
56 gpio_control(GPIO10, IOT_GPIO_VALUE0);
57 }
58
car_left(void)59 void car_left(void)
60 {
61 gpio_control(GPIO0, IOT_GPIO_VALUE0);
62 gpio_control(GPIO1, IOT_GPIO_VALUE0);
63 gpio_control(GPIO9, IOT_GPIO_VALUE1);
64 gpio_control(GPIO10, IOT_GPIO_VALUE0);
65 }
66
car_right(void)67 void car_right(void)
68 {
69 gpio_control(GPIO0, IOT_GPIO_VALUE1);
70 gpio_control(GPIO1, IOT_GPIO_VALUE0);
71 gpio_control(GPIO9, IOT_GPIO_VALUE0);
72 gpio_control(GPIO10, IOT_GPIO_VALUE0);
73 }
74
car_stop(void)75 void car_stop(void)
76 {
77 gpio_control(GPIO0, IOT_GPIO_VALUE1);
78 gpio_control(GPIO1, IOT_GPIO_VALUE1);
79 gpio_control(GPIO9, IOT_GPIO_VALUE1);
80 gpio_control(GPIO10, IOT_GPIO_VALUE1);
81 }
82
RobotTask(void * parame)83 void RobotTask(void* parame)
84 {
85 (void)parame;
86 printf("start test l9110s\r\n");
87 unsigned int time = 500;
88 car_forward();
89 osDelay(time);
90 car_backward();
91 osDelay(time);
92 car_left();
93 osDelay(time);
94 car_right();
95 osDelay(time);
96 car_stop();
97 osDelay(time);
98 }
99
100
RobotDemo(void)101 static void RobotDemo(void)
102 {
103 osThreadAttr_t attr;
104
105 attr.name = "RobotTask";
106 attr.attr_bits = 0U;
107 attr.cb_mem = NULL;
108 attr.cb_size = 0U;
109 attr.stack_mem = NULL;
110 attr.stack_size = TASK_STAK_SIZE;
111 attr.priority = osPriorityNormal;
112
113 if (osThreadNew(RobotTask, NULL, &attr) == NULL) {
114 printf("[RobotDemo] Failed to create RobotTask!\n");
115 }
116 }
117
118 APP_FEATURE_INIT(RobotDemo);
119