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 #define SEND_CMD_LEN (8) 20 21 #define OLED_X_POSITION_0 (0) 22 #define OLED_X_POSITION_15 (15) 23 #define OLED_X_POSITION_16 (16) 24 #define OLED_X_POSITION_18 (18) 25 #define OLED_X_POSITION_40 (40) 26 #define OLED_X_POSITION_48 (48) 27 #define OLED_X_POSITION_56 (56) 28 #define OLED_X_POSITION_60 (60) 29 #define OLED_X_POSITION_81 (81) 30 #define OLED_X_POSITION_120 (120) 31 32 #define OLED_Y_POSITION_0 ((unsigned char)0x0) 33 #define OLED_Y_POSITION_1 ((unsigned char)0x1) 34 #define OLED_Y_POSITION_2 ((unsigned char)0x2) 35 #define OLED_Y_POSITION_3 ((unsigned char)0x3) 36 #define OLED_Y_POSITION_4 ((unsigned char)0x4) 37 #define OLED_Y_POSITION_5 ((unsigned char)0x5) 38 #define OLED_Y_POSITION_6 ((unsigned char)0x6) 39 #define OLED_Y_POSITION_7 ((unsigned char)0x7) 40 41 #define OLED_DISPLAY_STRING_TYPE_1 (1) 42 #define OLED_DISPLAY_STRING_TYPE_16 (16) 43 44 #define OLED_ADDRESS 0x78 // 默认地址为0x78 45 #define OLED_ADDRESS_WRITE_CMD 0x00 // 0000000 写命令 46 #define OLED_ADDRESS_WRITE_DATA 0x40 // 0100 0000(0x40) 47 #define HI_I2C_IDX_BAUDRATE 400000 // 400k 48 /* delay */ 49 #define SLEEP_1_MS 1 50 #define SLEEP_10_MS 10 51 #define SLEEP_20_MS 20 52 #define SLEEP_30_MS 30 53 #define SLEEP_50_MS 50 54 #define SLEEP_100_MS 100 55 #define SLEEP_1S 1000 56 #define SLEEP_6_S 6000 57 #define DELAY_5_MS 5000 58 #define DELAY_10_MS 10000 59 #define DELAY_100_MS 100000 60 #define DELAY_250_MS 250000 61 #define DELAY_500_MS 500000 62 #define DELAY_1_S 1000000 63 #define DELAY_5_S 5000000 64 #define DELAY_6_S 6000000 65 #define DELAY_10_S 10000000 66 #define DELAY_30_S 30000000 67 #define DEFAULT_TYPE ((hi_u8)0) 68 #define INIT_TIME_COUNT ((hi_u8)1) 69 #define TIME_PEROID_COUNT 10 70 /* pwm duty */ 71 #define PWM_LOW_DUTY 1 72 #define PWM_SLOW_DUTY 1000 73 #define PWM_SMALL_DUTY 4000 74 #define PWM_LITTLE_DUTY 10000 75 #define PWM_DUTY 50 76 #define PWM_MIDDLE_DUTY 40000 77 #define PWM_FULL_DUTY 65530 78 /* oled init */ 79 #define OLED_CLEAN_SCREEN ((hi_u8)0x00) 80 /* ssd1306 register cmd */ 81 #define DISPLAY_OFF 0xAE 82 #define SET_LOW_COLUMN_ADDRESS 0x00 83 #define SET_HIGH_COLUMN_ADDRESS 0x10 84 #define SET_START_LINE_ADDRESS 0x40 85 #define SET_PAGE_ADDRESS 0xB0 86 #define CONTRACT_CONTROL 0x81 87 #define FULL_SCREEN 0xFF 88 #define SET_SEGMENT_REMAP 0xA1 89 #define NORMAL 0xA6 90 #define SET_MULTIPLEX 0xA8 91 #define DUTY 0x3F 92 #define SCAN_DIRECTION 0xC8 93 #define DISPLAY_OFFSET 0xD3 94 #define DISPLAY_TYPE 0x00 95 #define OSC_DIVISION 0xD5 96 #define DIVISION 0x80 97 #define COLOR_MODE_OFF 0xD8 98 #define COLOR 0x05 99 #define PRE_CHARGE_PERIOD 0xD9 100 #define PERIOD 0xF1 101 #define PIN_CONFIGUARTION 0xDA 102 #define CONFIGUARTION 0x12 103 #define SET_VCOMH 0xDB 104 #define VCOMH 0x30 105 #define SET_CHARGE_PUMP_ENABLE 0x8D 106 #define PUMP_ENABLE 0x14 107 #define TURN_ON_OLED_PANEL 0xAF 108 109 #define IOT_IO_NAME_GPIO9 (9) 110 #define IOT_GPIO_IDX_9 (9) 111 112 typedef struct { 113 /** Pointer to the buffer storing data to send */ 114 unsigned char *sendBuf; 115 /** Length of data to send */ 116 unsigned int sendLen; 117 /** Pointer to the buffer for storing data to receive */ 118 unsigned char *receiveBuf; 119 /** Length of data received */ 120 unsigned int receiveLen; 121 } IotI2cData; 122 123 typedef enum { 124 IOT_I2C_IDX_0, 125 TOT_I2C_IDX_1, 126 } IotI2cIdx; 127 128 unsigned int OledInit(void); 129 void OledSetPosition(unsigned char x, unsigned char y); 130 void OledFillScreen(unsigned char fiiData); 131 void OledShowChar(unsigned char x, unsigned char y, unsigned char chr, unsigned char charSize); 132 void OledShowStr(unsigned char x, unsigned char y, unsigned char *chr, unsigned char charSize); 133 #endif