Init a project
[apps/native/smart-ruler.git] / src / resource / resource_oled.c
1 /*
2  * Copyright (c) 2019 G.camp,
3  *
4  * Contact: Jin Seog Bang <seog814@gmail.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <peripheral_io.h>
20 #include "log.h"
21 #include "resource.h"
22
23 /* I2C */
24 #define SSD1306_ADDR 0x03c /* Address of SSD1306 OLED */
25
26 static struct {
27         int opened;
28         peripheral_i2c_h sensor_h;
29 } resource_sensor_s1;
30
31 void resource_close_OLED(void)
32 {
33         if (!resource_sensor_s1.opened)
34                 return;
35
36         _I("OLED display is finishing...");
37         peripheral_i2c_close(resource_sensor_s1.sensor_h);
38         resource_sensor_s1.opened = 0;
39 }
40
41 int resource_OLED_initial(int i2c_bus)
42 {
43         int ret = PERIPHERAL_ERROR_NONE;
44         static int write = 0;
45         unsigned char buff_count ;
46         unsigned char page_count;
47         unsigned char buf[10] = { 0, };
48         unsigned char tcbuf[10] = { 0, };
49         unsigned char tmbuf[30] = { 0xae, 0xd5, 0x80, 0xa8, 0x3f, 
50                                                                 0xd3, 0x00, 0x40, 0x8d, 0x14,
51                                                                 0x20, 0x00, 0xda, 0x12, 0x81,
52                                                                 0x9f, 0xd9, 0x22, 0xdb, 0x40,
53                                                                 0xa4, 0xa6, 0xaf};
54
55         if (!resource_sensor_s1.opened) {
56                 
57                 ret = peripheral_i2c_open(i2c_bus, SSD1306_ADDR, &resource_sensor_s1.sensor_h);
58                 if (ret != PERIPHERAL_ERROR_NONE) {
59                         _E("i2c open error OLED1 : %s", get_error_message(ret));
60                          
61                         return -1;
62                 }
63                 resource_sensor_s1.opened = 1;
64                 write = 0;
65         }
66
67 //       buf[0] = 0x00; // OLED control command
68 //       buf[1] = 0xae; // OLED command
69
70 #if 1
71         for (buff_count = 0; buff_count < 24; buff_count++)
72         {       
73                 if (!write) {
74                         buf[0] = 0x00;
75                         buf[1] = tmbuf[buff_count];
76
77                         ret = peripheral_i2c_write(resource_sensor_s1.sensor_h, buf, 2);
78                         if (ret != PERIPHERAL_ERROR_NONE) {
79                                 _E("i2c write error OLED : %s", get_error_message(ret));
80                                 return -1;
81                         }
82                         write = 0;      //write = 1;???
83                 }
84         
85                 _I("iic OLED control initial .. step ");
86         }
87 #endif  
88
89 #if 1
90         buf[0] = 0x20;
91         buf[1] = 0x10;
92         for (buff_count = 0; buff_count < 2; buff_count++)
93         {
94                 if (!write) {
95                         buf[0] = 0x00;
96                         buf[1] = tcbuf[buff_count];
97                         ret = peripheral_i2c_write(resource_sensor_s1.sensor_h, buf, 2);
98                         if (ret != PERIPHERAL_ERROR_NONE) {
99                                 _E("i2c write error OLED 1: %s", get_error_message(ret));
100
101                                 return -1;
102                         }
103                         write = 0;      //write = 1;???
104                 }
105         }
106
107         _I("iic OLED control pageaddr .. step ");
108 #endif
109
110 #if 1
111         tcbuf[0] = 0x21;
112         tcbuf[1] = 0x00;
113         tcbuf[2] = 0x7f;
114         for (buff_count = 0; buff_count < 3; buff_count++)
115         {
116                 if (!write) {
117                         buf[0] = 0x00;
118                         buf[1] = tcbuf[buff_count];
119                         ret = peripheral_i2c_write(resource_sensor_s1.sensor_h, buf, 2);
120                         if (ret != PERIPHERAL_ERROR_NONE) {
121                                 _E("i2c write error OLED 2: %s", get_error_message(ret));
122                                 return -1;
123                         }
124                         write = 0;      //write = 1;???
125
126                 }
127         }
128         _I("iic OLED control columnaddr .. step ");
129 #endif
130
131 #if 0   
132         buf[0] = 0x22;
133         buf[1] = 0x00;
134         buf[2] = 0x00;
135         for (buff_count = 0; buff_count < 3; buff_count++)
136         {       
137                 if (!write) {
138                         buf[0] = 0x00;
139                         buf[1] = tcbuf[buff_count];
140                         ret = peripheral_i2c_write(resource_sensor_s1.sensor_h, buf, 2);
141                         if (ret != PERIPHERAL_ERROR_NONE) {
142                                 _E("i2c write error OLED 3: %s", get_error_message(ret));
143                                 return -1;
144                         }
145                         write = 0;      //write = 1;???
146                 }
147         }
148         _I("iic OLED control pageaddr .. step ");
149 #endif
150
151 #if 1
152         for (page_count = 0; page_count < 8; page_count ++)
153         {
154                 tcbuf[0] = (0xb0 + page_count);
155                 buff_count = 0;
156                         if (!write) {
157                                 buf[0] = 0x00;
158                                 buf[1] = tcbuf[buff_count];
159                                 ret = peripheral_i2c_write(resource_sensor_s1.sensor_h, buf, 2);
160                                 if (ret != PERIPHERAL_ERROR_NONE) {
161                                         _E("i2c write error OLED 4: %s", get_error_message(ret));
162                                         return -1;
163                                 }
164                         write = 0;      //write = 1;???
165                         }
166                         _I("iic OLED control pageaddr .. step ");
167
168
169                 for (buff_count = 0; buff_count < 128; buff_count++)
170                 {       
171                         if (!write) {
172                                 buf[0] = 0x40;
173                                 buf[1] = 0x00;
174                         
175                                 ret = peripheral_i2c_write(resource_sensor_s1.sensor_h, buf, 2);
176                                 if (ret != PERIPHERAL_ERROR_NONE) {
177                                         _E("i2c write error OLED 5 : %s", get_error_message(ret));
178                                         return -1;
179                                 }
180                                 write = 0;      //write = 1;???
181                         }
182                         _I("iic OLED data .. step ");
183                 
184                 }
185         }
186 #endif  
187
188         return 0;
189 }
190
191 int resource_OLED_disp_logo(int i2c_bus)
192 {
193         int ret = PERIPHERAL_ERROR_NONE;
194         static int write = 0;
195         unsigned char buff_count ;
196         unsigned char page_count;
197         unsigned char buf[10] = { 0, };
198         unsigned char tcbuf[10] = { 0, };
199         unsigned char font_G_l[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
200                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
201                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
203                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
204                                                                 0x00, 0x1f, 0x20, 0x20, 0x20, 0x20, 0x1f, 0x00, // G font 
205                                                                 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, // - font
206                                                                 0x00, 0x1f, 0x20, 0x20, 0x20, 0x20, 0x10, 0x00, // C font
207                                                                 0x00, 0x3f, 0x01, 0x01, 0x01, 0x01, 0x3f, 0x00, // A font
208                                                                 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, // M font
209                                                                 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // P font
210                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
211                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
212                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
213                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};        
214         unsigned char font_G_h[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
215                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
216                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
217                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
218                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
219                                                                 0x00, 0xf8, 0x04, 0x04, 0x84, 0x84, 0x88, 0x00,
220                                                                 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00,
221                                                                 0x00, 0xf8, 0x04, 0x04, 0x04, 0x04, 0x08, 0x00,
222                                                                 0x00, 0xc0, 0x30, 0x0c, 0x0c, 0x30, 0xc0, 0x00,
223                                                                 0x00, 0xfc, 0x08, 0x70, 0x70, 0x08, 0xfc, 0x00,
224                                                                 0x00, 0xfc, 0x84, 0x84, 0x84, 0x84, 0x78, 0x00,
225                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
226                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
228                                                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
229         
230 // resource_write_led(5, 1);
231
232
233         tcbuf[0] = 0x21;
234         tcbuf[1] = 0x00;
235         tcbuf[2] = 0x7f;
236         for (buff_count = 0; buff_count < 3; buff_count++)
237         {
238                 if (!write) {
239                         buf[0] = 0x00;
240                         buf[1] = tcbuf[buff_count];
241                         ret = peripheral_i2c_write(resource_sensor_s1.sensor_h, buf, 2);
242                         if (ret != PERIPHERAL_ERROR_NONE) {
243                                 _E("i2c write error OLED 2: %s", get_error_message(ret));
244                                 return -1;
245                         }
246                         write = 0;      //write = 1;???
247                 }
248         }       
249         _I("iic OLED control columnaddr .. step ");
250         
251                 page_count = 2;
252                 tcbuf[0] = (0xb0 + page_count);
253                 buff_count = 0;
254                         if (!write) {
255                                 buf[0] = 0x00;
256                                 buf[1] = tcbuf[buff_count];
257                                 ret = peripheral_i2c_write(resource_sensor_s1.sensor_h, buf, 2);
258                                 if (ret != PERIPHERAL_ERROR_NONE) {
259                                         _E("i2c write error OLED 6: %s", get_error_message(ret));
260                                         return -1;
261                                 }
262                         write = 0;      //write = 1;???
263                         }
264                         _I("iic OLED logo disp high pageaddr .. step ");
265
266
267                 for (buff_count = 0; buff_count < 135; buff_count++)
268                 {       
269                         if (!write) {
270                                 buf[0] = 0x40;
271                                 buf[1] = font_G_h[buff_count];
272                         
273                                 ret = peripheral_i2c_write(resource_sensor_s1.sensor_h, buf, 2);
274                                 if (ret != PERIPHERAL_ERROR_NONE) {
275                                         _E("i2c write error OLED 7 : %s", get_error_message(ret));
276                                         return -1;
277                                 }
278                                 write = 0;      //write = 1;???
279                         }
280                         _I("iic OLED logo font high data .. step ");
281                 
282                 }
283                 
284         tcbuf[0] = 0x21;
285         tcbuf[1] = 0x00;
286         tcbuf[2] = 0x7e;
287         for (buff_count = 0; buff_count < 3; buff_count++)
288         {
289                 if (!write) {
290                         buf[0] = 0x00;
291                         buf[1] = tcbuf[buff_count];
292                         ret = peripheral_i2c_write(resource_sensor_s1.sensor_h, buf, 2);
293                         if (ret != PERIPHERAL_ERROR_NONE) {
294                                 _E("i2c write error OLED 2: %s", get_error_message(ret));
295                                 return -1;
296                         }
297                         write = 0;      //write = 1;???
298                 }
299         }       
300         _I("iic OLED control columnaddr .. step ");
301         
302                 page_count = 3;
303                 tcbuf[0] = (0xb0 + page_count );
304                 buff_count = 0;
305                         if (!write) {
306                                 buf[0] = 0x00;
307                                 buf[1] = tcbuf[buff_count];
308                                 ret = peripheral_i2c_write(resource_sensor_s1.sensor_h, buf, 2);
309                                 if (ret != PERIPHERAL_ERROR_NONE) {
310                                         _E("i2c write error OLED 6: %s", get_error_message(ret));
311                                         return -1;
312                                 }
313                         write = 0;      //write = 1;???
314                         }
315                         _I("iic OLED logo disp low pageaddr .. step ");
316                         
317                                         for (buff_count = 0; buff_count < 135; buff_count++)
318                 {       
319                         if (!write) {
320                                 buf[0] = 0x40;
321                                 buf[1] = font_G_l[buff_count];
322                         
323                                 ret = peripheral_i2c_write(resource_sensor_s1.sensor_h, buf, 2);
324                                 if (ret != PERIPHERAL_ERROR_NONE) {
325                                         _E("i2c write error OLED 7 : %s", get_error_message(ret));
326                                         return -1;
327                                 }
328                                 write = 0;      //write = 1;???
329                         }
330                         _I("iic OLED logo font high data .. step ");
331                 
332                 }
333         return 0;
334 }