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