• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SSD1306_OLED_H
17 #define SSD1306_OLED_H
18 
19 #include <hi_types_base.h>
20 #include <hi_i2c.h>
21 #include "ssd1306_oled.h"
22 
23 #define OLED_ADDRESS                0x78 // 默认地址为0x78
24 #define OLED_ADDRESS_WRITE_CMD      0x00 // 0000000 写命令
25 #define OLED_ADDRESS_WRITE_DATA     0x40 // 0100 0000(0x40)
26 #define HI_I2C_IDX_BAUDRATE         400000 // 400k
27 /* delay */
28 #define SLEEP_1_MS                  1
29 #define SLEEP_10_MS                 10
30 #define SLEEP_20_MS                 20
31 #define SLEEP_30_MS                 30
32 #define SLEEP_50_MS                 50
33 #define SLEEP_100_MS                100
34 #define SLEEP_1S                    1000
35 #define SLEEP_6_S                   6000
36 #define DELAY_5_MS                  5000
37 #define DELAY_10_MS                 10000
38 #define DELAY_100_MS                100000
39 #define DELAY_250_MS                250000
40 #define DELAY_500_MS                500000
41 #define DELAY_1_S                   1000000
42 #define DELAY_5_S                   5000000
43 #define DELAY_6_S                   6000000
44 #define DELAY_10_S                  10000000
45 #define DELAY_30_S                  30000000
46 #define DEFAULT_TYPE                ((hi_u8)0)
47 #define INIT_TIME_COUNT             ((hi_u8)1)
48 #define TIME_PERIOD_COUNT           10
49 /* pwm duty */
50 #define PWM_LOW_DUTY                1
51 #define PWM_SLOW_DUTY               1000
52 #define PWM_SMALL_DUTY              4000
53 #define PWM_LITTLE_DUTY             10000
54 #define PWM_DUTY                    50
55 #define PWM_MIDDLE_DUTY             40000
56 #define PWM_FULL_DUTY               65530
57 /* oled init */
58 #define OLED_CLEAN_SCREEN           ((hi_u8)0x00)
59 /* ssd1306 register cmd */
60 #define DISPLAY_OFF                 0xAE
61 #define SET_LOW_COLUMN_ADDRESS      0x00
62 #define SET_HIGH_COLUMN_ADDRESS     0x10
63 #define SET_START_LINE_ADDRESS      0x40
64 #define SET_PAGE_ADDRESS            0xB0
65 #define CONTRACT_CONTROL            0x81
66 #define FULL_SCREEN                 0xFF
67 #define SET_SEGMENT_REMAP           0xA1
68 #define NORMAL                      0xA6
69 #define SET_MULTIPLEX               0xA8
70 #define DUTY                        0x3F
71 #define SCAN_DIRECTION              0xC8
72 #define DISPLAY_OFFSET              0xD3
73 #define DISPLAY_TYPE                0x00
74 #define OSC_DIVISION                0xD5
75 #define DIVISION                    0x80
76 #define COLOR_MODE_OFF              0xD8
77 #define COLOR                       0x05
78 #define PRE_CHARGE_PERIOD           0xD9
79 #define PERIOD                      0xF1
80 #define PIN_CONFIGURATION           0xDA
81 #define CONFIGURATION               0x12
82 #define SET_VCOMH                   0xDB
83 #define VCOMH                       0x30
84 #define SET_CHARGE_PUMP_ENABLE      0x8D
85 #define PUMP_ENABLE                 0x14
86 #define TURN_ON_OLED_PANEL          0xAF
87 
88 #define IOT_IO_NAME_GPIO9            (9)
89 #define IOT_GPIO_IDX_9               (9)
90 
91 typedef struct {
92     /** Pointer to the buffer storing data to send */
93     unsigned char *sendBuf;
94     /** Length of data to send */
95     unsigned int  sendLen;
96     /** Pointer to the buffer for storing data to receive */
97     unsigned char *receiveBuf;
98     /** Length of data received */
99     unsigned int  receiveLen;
100 } IotI2cData;
101 
102 typedef enum {
103     IOT_I2C_IDX_0,
104     TOT_I2C_IDX_1,
105 } IotI2cIdx;
106 
107 static unsigned int I2cWriteByte(unsigned char regAddr, unsigned char cmd);
108 static unsigned int WriteCmd(unsigned char cmd);
109 static unsigned int WriteData(unsigned char i2cData);
110 unsigned int OledInit(void);
111 void OledSetPosition(unsigned char x, unsigned char y);
112 void OledFillScreen(unsigned char fiiData);
113 void OledShowChar(unsigned char x, unsigned char y, unsigned char chr, unsigned char charSize);
114 void OledShowStr(unsigned char x, unsigned char y, unsigned char *chr, unsigned char charSize);
115 #endif