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 "ssd1306.h"
27 #include "iot_i2c.h"
28 #include "iot_watchdog.h"
29 #include "robot_control.h"
30 #include "iot_errno.h"
31 
32 #define OLED_I2C_BAUDRATE (400*1000)
33 #define GPIO13 13
34 #define GPIO14 14
35 #define FUNC_SDA 6
36 #define FUNC_SCL 6
37 #define I2C0 0
38 #define STACKSIZE 10240
39 #define PRIORITY 25
40 
Ssd1306TestTask(void* arg)41 void Ssd1306TestTask(void* arg)
42 {
43     (void) arg;
44     hi_io_set_func(GPIO13, FUNC_SDA);
45     hi_io_set_func(GPIO14, FUNC_SCL);
46     IoTI2cInit(I2C0, OLED_I2C_BAUDRATE);
47 
48     IoTWatchDogDisable();
49 
50     unsigned int time1 = 20000;
51     unsigned int time2 = 100;
52     unsigned int time3 = 10;
53     unsigned int x1 = 0;
54     unsigned int y1 = 0;
55     unsigned int x2 = 10;
56     unsigned int y2 = 10;
57     usleep(time1);
58     ssd1306_Init();
59     ssd1306_Fill(Black);
60     ssd1306_SetCursor(x1, y1);
61     ssd1306_DrawString("Hello OpenHarmony!", Font_7x10, White);
62 
63     uint32_t start = HAL_GetTick();
64     ssd1306_UpdateScreen();
65     uint32_t end = HAL_GetTick();
66     printf("ssd1306_UpdateScreen time cost: %ud ms.\r\n", end - start);
67     osDelay(time2);
68 
69     while (1) {
70         ssd1306_Fill(Black);
71         ssd1306_SetCursor(x2, y2);
72         unsigned char status = GetCarStatus();
73         if (status == CAR_OBSTACLE_AVOIDANCE_STATUS) {
74             ssd1306_DrawString("ultrasonic", Font_7x10, White);
75         } else if (status == CAR_STOP_STATUS) {
76             ssd1306_DrawString("Robot Car Stop", Font_7x10, White);
77         } else if (status == CAR_TRACE_STATUS) {
78             ssd1306_DrawString("trace", Font_7x10, White);
79         }
80         ssd1306_UpdateScreen();
81         osDelay(time3);
82     }
83 }
84 
Ssd1306TestDemo(void)85 void Ssd1306TestDemo(void)
86 {
87     osThreadAttr_t attr;
88 
89     attr.name = "Ssd1306Task";
90     attr.attr_bits = 0U;
91     attr.cb_mem = NULL;
92     attr.cb_size = 0U;
93     attr.stack_mem = NULL;
94     attr.stack_size = STACKSIZE;
95     attr.priority = PRIORITY;
96 
97     if (osThreadNew(Ssd1306TestTask, NULL, &attr) == NULL) {
98         printf("[Ssd1306TestDemo] Failed to create Ssd1306TestTask!\n");
99     }
100 }
101 APP_FEATURE_INIT(Ssd1306TestDemo);
102