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 <unistd.h>
17 #include <hi_io.h>
18 #include "hi_time.h"
19 #include "iot_i2c.h"
20 #include "ohos_init.h"
21 #include "cmsis_os2.h"
22 #include "iot_gpio.h"
23 #include "hi_gpio.h"
24 #include "ssd1306_oled.h"
25 #include "app_demo_multi_sample.h"
26 #include "ssd1306_oled.h"
27
28 #define I2C_REG_ARRAY_LEN (64)
29 #define OLED_SEND_BUFF_LEN (28)
30 #define OLED_SEND_BUFF_LEN2 (25)
31 #define OLED_SEND_BUFF_LEN3 (27)
32 #define OLED_SEND_BUFF_LEN4 (29)
33 #define MAX_COLUM (128)
34 #define OLED_DEMO_TASK_STAK_SIZE (1024 * 2)
35 #define OLED_DEMO_TASK_PRIORITY (25)
36 #define OLED_DISPLAY_INTERVAL_TIME (1)
37 #define SEND_CMD_LEN (2)
38
39 #define CHAR_SIZE 16
40 #define Y_PIXEL_POINT 16
41 #define X_PIXEL_POINT 8
42 #define X_REMAINING_PIXELS 6
43
44 #define X_PIXEL_POINT_POSITION_120 (120)
45 #define Y_LINES_PIXEL_2 (2)
46 #define X_COLUMNS_PIXEL_8 (8)
47
48 /* 6*8的点阵 */
49 static const unsigned char f6X8[][6] = {
50 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // sp
51 { 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 }, // !
52 { 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 }, // "
53 { 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 }, // #
54 { 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 }, // $
55 { 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 }, // %
56 { 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 }, // &
57 { 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 }, // '
58 { 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 }, // (
59 { 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 }, // )
60 { 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 }, // *
61 { 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 }, // +
62 { 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 }, // ,
63 { 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 }, // -
64 { 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 }, // .
65 { 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 }, // /
66 { 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E }, // 0
67 { 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 }, // 1
68 { 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 }, // 2
69 { 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 }, // 3
70 { 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 }, // 4
71 { 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 }, // 5
72 { 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 }, // 6
73 { 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 }, // 7
74 { 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 }, // 8
75 { 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E }, // 9
76 { 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 }, // :
77 { 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 }, // ;号
78 { 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 }, // <
79 { 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 }, // =
80 { 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 }, // >
81 { 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 }, // ?
82 { 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E }, // @
83 { 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C }, // A
84 { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 }, // B
85 { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 }, // C
86 { 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C }, // D
87 { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 }, // E
88 { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 }, // F
89 { 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A }, // G
90 { 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F }, // H
91 { 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 }, // I
92 { 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 }, // J
93 { 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 }, // K
94 { 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 }, // L
95 { 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F }, // M
96 { 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F }, // N
97 { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E }, // O
98 { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 }, // P
99 { 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E }, // Q
100 { 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 }, // R
101 { 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 }, // S
102 { 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 }, // T
103 { 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F }, // U
104 { 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F }, // V
105 { 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F }, // W
106 { 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 }, // X
107 { 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 }, // Y
108 { 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 }, // Z
109 { 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 }, // [
110 { 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55 }, // 55
111 { 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 }, // ]
112 { 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 }, // ^
113 { 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 }, // _
114 { 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 }, // '
115 { 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 }, // a
116 { 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 }, // b
117 { 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 }, // c
118 { 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F }, // d
119 { 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 }, // e
120 { 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 }, // f
121 { 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C }, // g
122 { 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 }, // h
123 { 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 }, // i
124 { 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 }, // j
125 { 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 }, // k
126 { 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 }, // l
127 { 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 }, // m
128 { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 }, // n
129 { 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 }, // o
130 { 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 }, // p
131 { 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC }, // q
132 { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 }, // r
133 { 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 }, // s
134 { 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 }, // t
135 { 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C }, // u
136 { 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C }, // v
137 { 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C }, // w
138 { 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 }, // x
139 { 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C }, // y
140 { 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 }, // z
141 { 0x14, 0x14, 0x14, 0x14, 0x14, 0x14 }, // horiz lines
142 };
143 /* 8*16的点阵 */
144 static const unsigned char f8X16[] = {
145 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0
146 0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x30, 0x00, 0x00, 0x00, // ! 1
147 0x00, 0x10, 0x0C, 0x06, 0x10, 0x0C, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // " 2
148 0x40, 0xC0, 0x78, 0x40, 0xC0, 0x78, 0x40, 0x00, 0x04, 0x3F, 0x04, 0x04, 0x3F, 0x04, 0x04, 0x00, // # 3
149 0x00, 0x70, 0x88, 0xFC, 0x08, 0x30, 0x00, 0x00, 0x00, 0x18, 0x20, 0xFF, 0x21, 0x1E, 0x00, 0x00, // $ 4
150 0xF0, 0x08, 0xF0, 0x00, 0xE0, 0x18, 0x00, 0x00, 0x00, 0x21, 0x1C, 0x03, 0x1E, 0x21, 0x1E, 0x00, // % 5
151 0x00, 0xF0, 0x08, 0x88, 0x70, 0x00, 0x00, 0x00, 0x1E, 0x21, 0x23, 0x24, 0x19, 0x27, 0x21, 0x10, // & 6
152 0x10, 0x16, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ' 7
153 0x00, 0x00, 0x00, 0xE0, 0x18, 0x04, 0x02, 0x00, 0x00, 0x00, 0x00, 0x07, 0x18, 0x20, 0x40, 0x00, // ( 8
154 0x00, 0x02, 0x04, 0x18, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x40, 0x20, 0x18, 0x07, 0x00, 0x00, 0x00, // ) 9
155 0x40, 0x40, 0x80, 0xF0, 0x80, 0x40, 0x40, 0x00, 0x02, 0x02, 0x01, 0x0F, 0x01, 0x02, 0x02, 0x00, // * 10
156 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x1F, 0x01, 0x01, 0x01, 0x00, // + 11
157 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xB0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, // , 12
158 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // - 13
159 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, // . 14
160 0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x18, 0x04, 0x00, 0x60, 0x18, 0x06, 0x01, 0x00, 0x00, 0x00, // / 15
161 0x00, 0xE0, 0x10, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x00, 0x0F, 0x10, 0x20, 0x20, 0x10, 0x0F, 0x00, // 0 16
162 0x00, 0x10, 0x10, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, // 1 17
163 0x00, 0x70, 0x08, 0x08, 0x08, 0x88, 0x70, 0x00, 0x00, 0x30, 0x28, 0x24, 0x22, 0x21, 0x30, 0x00, // 2 18
164 0x00, 0x30, 0x08, 0x88, 0x88, 0x48, 0x30, 0x00, 0x00, 0x18, 0x20, 0x20, 0x20, 0x11, 0x0E, 0x00, // 3 19
165 0x00, 0x00, 0xC0, 0x20, 0x10, 0xF8, 0x00, 0x00, 0x00, 0x07, 0x04, 0x24, 0x24, 0x3F, 0x24, 0x00, // 4 20
166 0x00, 0xF8, 0x08, 0x88, 0x88, 0x08, 0x08, 0x00, 0x00, 0x19, 0x21, 0x20, 0x20, 0x11, 0x0E, 0x00, // 5 21
167 0x00, 0xE0, 0x10, 0x88, 0x88, 0x18, 0x00, 0x00, 0x00, 0x0F, 0x11, 0x20, 0x20, 0x11, 0x0E, 0x00, // 6 22
168 0x00, 0x38, 0x08, 0x08, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, // 7 23
169 0x00, 0x70, 0x88, 0x08, 0x08, 0x88, 0x70, 0x00, 0x00, 0x1C, 0x22, 0x21, 0x21, 0x22, 0x1C, 0x00, // 8 24
170 0x00, 0xE0, 0x10, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x00, 0x00, 0x31, 0x22, 0x22, 0x11, 0x0F, 0x00, // 9 25
171 0x00, 0x00, 0x00, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, // : 26
172 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x00, 0x00, 0x00, 0x00, // ;号 27
173 0x00, 0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00, // < 28
174 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, // = 29
175 0x00, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00, // > 30
176 0x00, 0x70, 0x48, 0x08, 0x08, 0x08, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x30, 0x36, 0x01, 0x00, 0x00, // ? 31
177 0xC0, 0x30, 0xC8, 0x28, 0xE8, 0x10, 0xE0, 0x00, 0x07, 0x18, 0x27, 0x24, 0x23, 0x14, 0x0B, 0x00, // @ 32
178 0x00, 0x00, 0xC0, 0x38, 0xE0, 0x00, 0x00, 0x00, 0x20, 0x3C, 0x23, 0x02, 0x02, 0x27, 0x38, 0x20, // A 33
179 0x08, 0xF8, 0x88, 0x88, 0x88, 0x70, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x11, 0x0E, 0x00, // B 34
180 0xC0, 0x30, 0x08, 0x08, 0x08, 0x08, 0x38, 0x00, 0x07, 0x18, 0x20, 0x20, 0x20, 0x10, 0x08, 0x00, // C 35
181 0x08, 0xF8, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x10, 0x0F, 0x00, // D 36
182 0x08, 0xF8, 0x88, 0x88, 0xE8, 0x08, 0x10, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x23, 0x20, 0x18, 0x00, // E 37
183 0x08, 0xF8, 0x88, 0x88, 0xE8, 0x08, 0x10, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x03, 0x00, 0x00, 0x00, // F 38
184 0xC0, 0x30, 0x08, 0x08, 0x08, 0x38, 0x00, 0x00, 0x07, 0x18, 0x20, 0x20, 0x22, 0x1E, 0x02, 0x00, // G 39
185 0x08, 0xF8, 0x08, 0x00, 0x00, 0x08, 0xF8, 0x08, 0x20, 0x3F, 0x21, 0x01, 0x01, 0x21, 0x3F, 0x20, // H 40
186 0x00, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, // I 41
187 0x00, 0x00, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x00, 0xC0, 0x80, 0x80, 0x80, 0x7F, 0x00, 0x00, 0x00, // J 42
188 0x08, 0xF8, 0x88, 0xC0, 0x28, 0x18, 0x08, 0x00, 0x20, 0x3F, 0x20, 0x01, 0x26, 0x38, 0x20, 0x00, // K 43
189 0x08, 0xF8, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x30, 0x00, // L 44
190 0x08, 0xF8, 0xF8, 0x00, 0xF8, 0xF8, 0x08, 0x00, 0x20, 0x3F, 0x00, 0x3F, 0x00, 0x3F, 0x20, 0x00, // M 45
191 0x08, 0xF8, 0x30, 0xC0, 0x00, 0x08, 0xF8, 0x08, 0x20, 0x3F, 0x20, 0x00, 0x07, 0x18, 0x3F, 0x00, // N 46
192 0xE0, 0x10, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x0F, 0x10, 0x20, 0x20, 0x20, 0x10, 0x0F, 0x00, // O 47
193 0x08, 0xF8, 0x08, 0x08, 0x08, 0x08, 0xF0, 0x00, 0x20, 0x3F, 0x21, 0x01, 0x01, 0x01, 0x00, 0x00, // P 48
194 0xE0, 0x10, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x0F, 0x18, 0x24, 0x24, 0x38, 0x50, 0x4F, 0x00, // Q 49
195 0x08, 0xF8, 0x88, 0x88, 0x88, 0x88, 0x70, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x03, 0x0C, 0x30, 0x20, // R 50
196 0x00, 0x70, 0x88, 0x08, 0x08, 0x08, 0x38, 0x00, 0x00, 0x38, 0x20, 0x21, 0x21, 0x22, 0x1C, 0x00, // S 51
197 0x18, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x18, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x00, 0x00, // T 52
198 0x08, 0xF8, 0x08, 0x00, 0x00, 0x08, 0xF8, 0x08, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x20, 0x1F, 0x00, // U 53
199 0x08, 0x78, 0x88, 0x00, 0x00, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x07, 0x38, 0x0E, 0x01, 0x00, 0x00, // V 54
200 0xF8, 0x08, 0x00, 0xF8, 0x00, 0x08, 0xF8, 0x00, 0x03, 0x3C, 0x07, 0x00, 0x07, 0x3C, 0x03, 0x00, // W 55
201 0x08, 0x18, 0x68, 0x80, 0x80, 0x68, 0x18, 0x08, 0x20, 0x30, 0x2C, 0x03, 0x03, 0x2C, 0x30, 0x20, // X 56
202 0x08, 0x38, 0xC8, 0x00, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x00, 0x00, // Y 57
203 0x10, 0x08, 0x08, 0x08, 0xC8, 0x38, 0x08, 0x00, 0x20, 0x38, 0x26, 0x21, 0x20, 0x20, 0x18, 0x00, // Z 58
204 0x00, 0x00, 0x00, 0xFE, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x40, 0x40, 0x40, 0x00, // [ 59
205 0x00, 0x0C, 0x30, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x38, 0xC0, 0x00, // \ 60
206 0x00, 0x02, 0x02, 0x02, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x7F, 0x00, 0x00, 0x00, // ] 61
207 0x00, 0x00, 0x04, 0x02, 0x02, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ^ 62
208 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // _ 63
209 0x00, 0x02, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ` 64
210 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x19, 0x24, 0x22, 0x22, 0x22, 0x3F, 0x20, // a 65
211 0x08, 0xF8, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x11, 0x20, 0x20, 0x11, 0x0E, 0x00, // b 66
212 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0x20, 0x11, 0x00, // c 67
213 0x00, 0x00, 0x00, 0x80, 0x80, 0x88, 0xF8, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0x10, 0x3F, 0x20, // d 68
214 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x1F, 0x22, 0x22, 0x22, 0x22, 0x13, 0x00, // e 69
215 0x00, 0x80, 0x80, 0xF0, 0x88, 0x88, 0x88, 0x18, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, // f 70
216 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x6B, 0x94, 0x94, 0x94, 0x93, 0x60, 0x00, // g 71
217 0x08, 0xF8, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x3F, 0x21, 0x00, 0x00, 0x20, 0x3F, 0x20, // h 72
218 0x00, 0x80, 0x98, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, // i 73
219 0x00, 0x00, 0x00, 0x80, 0x98, 0x98, 0x00, 0x00, 0x00, 0xC0, 0x80, 0x80, 0x80, 0x7F, 0x00, 0x00, // j 74
220 0x08, 0xF8, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x20, 0x3F, 0x24, 0x02, 0x2D, 0x30, 0x20, 0x00, // k 75
221 0x00, 0x08, 0x08, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, // l 76
222 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x3F, 0x20, 0x00, 0x3F, // m 77
223 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x3F, 0x21, 0x00, 0x00, 0x20, 0x3F, 0x20, // n 78
224 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x20, 0x1F, 0x00, // o 79
225 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x80, 0xFF, 0xA1, 0x20, 0x20, 0x11, 0x0E, 0x00, // p 80
226 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0xA0, 0xFF, 0x80, // q 81
227 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x20, 0x20, 0x3F, 0x21, 0x20, 0x00, 0x01, 0x00, // r 82
228 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x33, 0x24, 0x24, 0x24, 0x24, 0x19, 0x00, // s 83
229 0x00, 0x80, 0x80, 0xE0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x00, 0x00, // t 84
230 0x80, 0x80, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x10, 0x3F, 0x20, // u 85
231 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x01, 0x0E, 0x30, 0x08, 0x06, 0x01, 0x00, // v 86
232 0x80, 0x80, 0x00, 0x80, 0x00, 0x80, 0x80, 0x80, 0x0F, 0x30, 0x0C, 0x03, 0x0C, 0x30, 0x0F, 0x00, // w 87
233 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x31, 0x2E, 0x0E, 0x31, 0x20, 0x00, // x 88
234 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x81, 0x8E, 0x70, 0x18, 0x06, 0x01, 0x00, // y 89
235 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x21, 0x30, 0x2C, 0x22, 0x21, 0x30, 0x00, // z 90
236 0x00, 0x00, 0x00, 0x00, 0x80, 0x7C, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x40, 0x40, // { 91
237 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, // | 92
238 0x00, 0x02, 0x02, 0x7C, 0x80, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x3F, 0x00, 0x00, 0x00, 0x00, // } 93
239 0x00, 0x06, 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ~ 94
240 };
241 /*
242 * @bref 向ssd1306 屏幕寄存器写入命令
243 * status 0:表示写入成功,否则失败
244 */
I2cWriteByte(unsigned char regAddr,unsigned char cmd)245 static unsigned int I2cWriteByte(unsigned char regAddr, unsigned char cmd)
246 {
247 unsigned int status;
248
249 unsigned char sendLen = 2;
250 unsigned char userData = cmd;
251 IotI2cData oledI2cCmd = { 0 };
252 IotI2cData oledI2cWriteCmd = { 0 };
253
254 unsigned char sendUserCmd [SEND_CMD_LEN] = {OLED_ADDRESS_WRITE_CMD, userData};
255 unsigned char sendUserData [SEND_CMD_LEN] = {OLED_ADDRESS_WRITE_DATA, userData};
256
257 /* 如果是写命令,发写命令地址0x00 */
258 if (regAddr == OLED_ADDRESS_WRITE_CMD) {
259 oledI2cWriteCmd.sendBuf = sendUserCmd;
260 oledI2cWriteCmd.sendLen = sendLen;
261 status = IoTI2cWrite(IOT_I2C_IDX_0, OLED_ADDRESS, oledI2cWriteCmd.sendBuf, oledI2cWriteCmd.sendLen);
262 if (status != 0) {
263 return status;
264 }
265 } else if (regAddr == OLED_ADDRESS_WRITE_DATA) {
266 oledI2cCmd.sendBuf = sendUserData;
267 oledI2cCmd.sendLen = sendLen;
268 status = IoTI2cWrite(IOT_I2C_IDX_0, OLED_ADDRESS, oledI2cCmd.sendBuf, oledI2cCmd.sendLen);
269 if (status != 0) {
270 return status;
271 }
272 }
273
274 return 0;
275 }
276
277 /* 写命令操作 */
WriteCmd(unsigned char cmd)278 static unsigned int WriteCmd(unsigned char cmd) // 写命令
279 {
280 unsigned char status;
281 /* 写设备地址 */
282 status = I2cWriteByte(OLED_ADDRESS_WRITE_CMD, cmd);
283 if (status != 0) {
284 return -1;
285 }
286 }
287 /* 写数据操作 */
WriteData(unsigned char i2cData)288 static unsigned int WriteData(unsigned char i2cData) // 写数据
289 {
290 unsigned char status;
291 /* 写设备地址 */
292 status = I2cWriteByte(OLED_ADDRESS_WRITE_DATA, i2cData);
293 if (status != 0) {
294 return -1;
295 }
296 }
SetOledAddress(void)297 static int SetOledAddress(void)
298 {
299 int status;
300
301 status = WriteCmd(DISPLAY_OFF); // --display off
302 if (status != 0) {
303 return -1;
304 }
305 status = WriteCmd(SET_LOW_COLUMN_ADDRESS); // ---set low column address
306 if (status != 0) {
307 return -1;
308 }
309 status = WriteCmd(SET_HIGH_COLUMN_ADDRESS); // ---set high column address
310 if (status != 0) {
311 return -1;
312 }
313 status = WriteCmd(SET_START_LINE_ADDRESS); // --set start line address
314 if (status != 0) {
315 return -1;
316 }
317 status = WriteCmd(SET_PAGE_ADDRESS); // --set page address
318 if (status != 0) {
319 return -1;
320 }
321 return 0;
322 }
323
SetOledControlCmd(void)324 static int SetOledControlCmd(void)
325 {
326 int status;
327
328 status = WriteCmd(CONTRACT_CONTROL); // contract control
329 if (status != 0) {
330 return -1;
331 }
332 status = WriteCmd(FULL_SCREEN); // --128
333 if (status != 0) {
334 return -1;
335 }
336 status = WriteCmd(SET_SEGMENT_REMAP); // set segment remap
337 if (status != 0) {
338 return -1;
339 }
340 status = WriteCmd(NORMAL); // --normal / reverse
341 if (status != 0) {
342 return -1;
343 }
344 return 0;
345 }
346
SetOledScanDisplayCmd(void)347 static int SetOledScanDisplayCmd(void)
348 {
349 int status;
350
351 status = WriteCmd(SET_MULTIPLEX); // --set multiplex ratio(1 to 64)
352 if (status != 0) {
353 return -1;
354 }
355 status = WriteCmd(DUTY); // --1/32 duty
356 if (status != 0) {
357 return -1;
358 }
359 status = WriteCmd(SCAN_DIRECTION); // Com scan direction
360 if (status != 0) {
361 return -1;
362 }
363 status = WriteCmd(DISPLAY_OFFSET); // -set display offset
364 if (status != 0) {
365 return -1;
366 }
367 status = WriteCmd(DISPLAY_TYPE);
368 if (status != 0) {
369 return -1;
370 }
371 status = WriteCmd(OSC_DIVISION); // set osc division
372 if (status != 0) {
373 return -1;
374 }
375 status = WriteCmd(DIVISION);
376 if (status != 0) {
377 return -1;
378 }
379 return 0;
380 }
381
SetOledColorPreChargeCmd(void)382 static int SetOledColorPreChargeCmd(void)
383 {
384 int status;
385
386 status = WriteCmd(COLOR_MODE_OFF); // set area color mode off
387 if (status != 0) {
388 return -1;
389 }
390 status = WriteCmd(COLOR);
391 if (status != 0) {
392 return -1;
393 }
394 status = WriteCmd(PRE_CHARGE_PERIOD); // Set Pre-Charge Period
395 if (status != 0) {
396 return -1;
397 }
398 status = WriteCmd(PERIOD);
399 if (status != 0) {
400 return -1;
401 }
402 status = WriteCmd(PIN_CONFIGURATION); // set com pin configuartion
403 if (status != 0) {
404 return -1;
405 }
406 status = WriteCmd(CONFIGURATION);
407 if (status != 0) {
408 return -1;
409 }
410 return 0;
411 }
412
SetOledVcomhChargePumpCmd(void)413 static int SetOledVcomhChargePumpCmd(void)
414 {
415 int status;
416
417 status = WriteCmd(SET_VCOMH); // set Vcomh
418 if (status != 0) {
419 return -1;
420 }
421 status = WriteCmd(VCOMH);
422 if (status != 0) {
423 return -1;
424 }
425 status = WriteCmd(SET_CHARGE_PUMP_ENABLE); // set charge pump enable
426 if (status != 0) {
427 return -1;
428 }
429 status = WriteCmd(PUMP_ENABLE);
430 if (status != 0) {
431 return -1;
432 }
433 status = WriteCmd(TURN_ON_OLED_PANEL); // --turn on oled panel
434 if (status != 0) {
435 return -1;
436 }
437 return 0;
438 }
439
440 /* ssd1306 oled 初始化 */
OledInit(void)441 unsigned int OledInit(void)
442 {
443 unsigned int status;
444 hi_udelay(DELAY_100_MS); // 100ms 这里的延时很重要
445
446 status = SetOledAddress();
447 if (status != 0) {
448 return -1;
449 }
450 status = SetOledControlCmd();
451 if (status != 0) {
452 return -1;
453 }
454 status = SetOledScanDisplayCmd();
455 if (status != 0) {
456 return -1;
457 }
458 status = SetOledColorPreChargeCmd();
459 if (status != 0) {
460 return -1;
461 }
462 status = SetOledVcomhChargePumpCmd();
463 if (status != 0) {
464 return -1;
465 }
466 return 0;
467 }
468 /*
469 * @bref set start position 设置起始点坐标
470 * @param unsigned char x:write start from x axis
471 * unsigned char y:write start from y axis
472 */
OledSetPosition(unsigned char x,unsigned char y)473 void OledSetPosition(unsigned char x, unsigned char y)
474 {
475 WriteCmd(0xb0 + y); /* send 0xb0 +y */
476 WriteCmd(((x & 0xf0) >> 4) | 0x10); /* 设置起点坐标0x10,4 */
477 WriteCmd(x & 0x0f); /* send x & 0x0f */
478 }
479 /* 全屏填充 */
480 #define Y_PIXEL_POINT_MAX (128)
481 #define X_PIXEL_8 (8)
OledFillScreen(unsigned char fiiData)482 void OledFillScreen(unsigned char fiiData)
483 {
484 for (unsigned char m = 0; m < X_PIXEL_8; m++) { /* 从OLED 的第0行开始,填充屏幕 */
485 WriteCmd(0xb0 + m); /* 0xb0 */
486 WriteCmd(0x00); /* 0x00 */
487 WriteCmd(0x10); /* 0x10 */
488 for (unsigned char n = 0; n < Y_PIXEL_POINT_MAX; n++) { /* 从OLED的第0列个像素点开始填充屏幕 */
489 WriteData(fiiData);
490 }
491 }
492 }
493 /*
494 * bref: Clear from a location
495 * fill_data: write data to screen register
496 * line:write positon start from Y axis
497 * pos :write positon start from x axis
498 * len:write data len
499 */
OledPositionCleanScreen(unsigned char fillData,unsigned char line,unsigned char pos,unsigned char len)500 void OledPositionCleanScreen(unsigned char fillData, unsigned char line, unsigned char pos, unsigned char len)
501 {
502 unsigned char m = line;
503 WriteCmd(0xb0 + m); /* 0xb0 */
504 WriteCmd(0x00); /* 0x00 */
505 WriteCmd(0x10); /* 0x10 */
506
507 for (unsigned char n = pos; n < len; n++) {
508 WriteData(fillData);
509 }
510 }
511 /*
512 * bref: 8*16 typeface
513 * x:write positon start from x axis
514 * y:write positon start from y axis
515 * chr:write data
516 * char_size:select typeface
517 */
518
519
OledShowChar(unsigned char x,unsigned char y,unsigned char chr,unsigned char charSize)520 void OledShowChar(unsigned char x, unsigned char y, unsigned char chr, unsigned char charSize)
521 {
522 unsigned char c = chr - ' '; // 得到偏移后的值
523 unsigned char xPosition = x;
524 unsigned char yPosition = y;
525
526 if (xPosition > MAX_COLUM - 1) {
527 xPosition = 0;
528 yPosition = yPosition + Y_LINES_PIXEL_2; /* 每2行写 */
529 }
530 if (charSize == CHAR_SIZE) { /* 当要写入的字为两个字节 */
531 OledSetPosition(xPosition, yPosition);
532 for (unsigned char i = 0; i < X_PIXEL_POINT; i++) { /* 从OLED的第0列个像素点开始 */
533 WriteData(f8X16[c * Y_PIXEL_POINT + i]); /* 16,8 */
534 }
535 OledSetPosition(xPosition, yPosition + 1);
536 for (unsigned char j = 0; j < X_PIXEL_POINT; j++) { /* 从OLED的第0列个像素点开始 */
537 WriteData(f8X16[c * Y_PIXEL_POINT + j + X_PIXEL_POINT]); /* 16,8 */
538 }
539 } else {
540 OledSetPosition(xPosition, yPosition);
541 for (unsigned char k = 0; k < X_REMAINING_PIXELS; k++) { /* 从OLED的第0列个像素点开始 */
542 WriteData(f6X8[c][k]);
543 }
544 }
545 }
546
547 /*
548 * bref: display string
549 * x:write positon start from x axis
550 * y:write positon start from y axis
551 * chr:write data
552 * char_size:select typeface
553 */
OledShowStr(unsigned char x,unsigned char y,unsigned char * chr,unsigned char charSize)554 void OledShowStr(unsigned char x, unsigned char y, unsigned char *chr, unsigned char charSize)
555 {
556 unsigned char j = 0;
557 unsigned char xPosition = x;
558 unsigned char yPosition = y;
559
560 if (chr == NULL) {
561 printf("param is NULL,Please check!!!\r\n");
562 return;
563 }
564 while (chr[j] != '\0') {
565 OledShowChar(xPosition, yPosition, chr[j], charSize);
566 xPosition += X_COLUMNS_PIXEL_8; /* 8列组成一个字符位置 */
567 if (xPosition > X_PIXEL_POINT_POSITION_120) { /* 120 */
568 xPosition = 0;
569 yPosition += Y_LINES_PIXEL_2; /* 每2行写 */
570 }
571 j++;
572 }
573 }
574
575 /* 显示主界面 */
OledMainMenuDisplay(void)576 void OledMainMenuDisplay(void)
577 {
578 OledFillScreen(0x00); // clear screen
579 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_1,
580 "1.Colorful light", OLED_DISPLAY_STRING_TYPE_1); /* 0, 1, xx, 1 */
581 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_2,
582 "2.Traffic light", OLED_DISPLAY_STRING_TYPE_1); /* 0, 2, xx, 1 */
583 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_3,
584 "3.Environment", OLED_DISPLAY_STRING_TYPE_1); /* 0, 3, xx, 1 */
585 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_4,
586 "4.NFC test", OLED_DISPLAY_STRING_TYPE_1); /* 0, 4, xx, 1 */
587
588 if (GetKeyStatus(MENU_SELECT) == COLORFUL_LIGHT_MENU) {
589 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_7,
590 "Select:1 Enter", OLED_DISPLAY_STRING_TYPE_1); /* 0, 7, xx, 1 */
591 } else if (GetKeyStatus(MENU_SELECT) == TRAFFIC_LIGHT_MENU) {
592 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_7,
593 "Select:2 Enter", OLED_DISPLAY_STRING_TYPE_1); /* 0, 7, xx, 1 */
594 } else if (GetKeyStatus(MENU_SELECT) == ENVIRONMENT_MENU) {
595 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_7,
596 "Select:3 Enter", OLED_DISPLAY_STRING_TYPE_1); /* 0, 7, xx, 1 */
597 } else if (GetKeyStatus(MENU_SELECT) == NFC_TEST_MENU) {
598 OledShowStr(OLED_X_POSITION_0, OLED_Y_POSITION_7,
599 "Select:4 Enter", OLED_DISPLAY_STRING_TYPE_1); /* 0, 7, xx, 1 */
600 }
601 }
602
603 /* shut down all led */
AllLedOff(void)604 void AllLedOff(void)
605 {
606 GpioControl(HI_IO_NAME_GPIO_10, HI_GPIO_IDX_10, HI_GPIO_DIR_OUT, HI_GPIO_VALUE0, HI_IO_FUNC_GPIO_10_GPIO);
607 GpioControl(HI_IO_NAME_GPIO_11, HI_GPIO_IDX_11, HI_GPIO_DIR_OUT, HI_GPIO_VALUE0, HI_IO_FUNC_GPIO_11_GPIO);
608 GpioControl(HI_IO_NAME_GPIO_12, HI_GPIO_IDX_12, HI_GPIO_DIR_OUT, HI_GPIO_VALUE0, HI_IO_FUNC_GPIO_12_GPIO);
609 }