SSD1306 OLED display driver 1.8.2 This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays

oled_ssd1306.c
1 /*
2 MIT License
3
4 Copyright (c) 2017-2019, Alexey Dynda
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 #include "oled_ssd1306.h"
26 #include "lcd_common.h"
27 #include "ssd1306_commands.h"
28 #include "intf/ssd1306_interface.h"
29 #include "intf/i2c/ssd1306_i2c.h"
30 #include "intf/spi/ssd1306_spi.h"
31 #include "ssd1306_hal/io.h"
32 #ifdef SDL_EMULATION
33 #include "sdl_core.h"
34 #endif
35
36 uint8_t s_ssd1306_startLine = 0;
37
38 static const uint8_t PROGMEM s_oled128x64_initData[] =
39 {
40 #ifdef SDL_EMULATION
41 SDL_LCD_SSD1306,
42 0x00,
43 #endif
44 SSD1306_DISPLAYOFF, // display off
45 SSD1306_MEMORYMODE, HORIZONTAL_ADDRESSING_MODE, // Page Addressing mode
46 SSD1306_COMSCANDEC, // Scan from 127 to 0 (Reverse scan)
47 SSD1306_SETSTARTLINE | 0x00, // First line to start scanning from
48 SSD1306_SETCONTRAST, 0x7F, // contast value to 0x7F according to datasheet
49 SSD1306_SEGREMAP | 0x01, // Use reverse mapping. 0x00 - is normal mapping
50 SSD1306_NORMALDISPLAY,
51 SSD1306_SETMULTIPLEX, 63, // Reset to default MUX. See datasheet
52 SSD1306_SETDISPLAYOFFSET, 0x00, // no offset
53 SSD1306_SETDISPLAYCLOCKDIV, 0x80,// set to default ratio/osc frequency
54 SSD1306_SETPRECHARGE, 0x22, // switch precharge to 0x22 // 0xF1
55 SSD1306_SETCOMPINS, 0x12, // set divide ratio
56 SSD1306_SETVCOMDETECT, 0x20, // vcom deselect to 0x20 // 0x40
57 SSD1306_CHARGEPUMP, 0x14, // Enable charge pump
58 SSD1306_DISPLAYALLON_RESUME,
59 SSD1306_DISPLAYON,
60 };
61
62 static const uint8_t PROGMEM s_oled128x32_initData[] =
63 {
64 #ifdef SDL_EMULATION
65 SDL_LCD_SSD1306,
66 0x00,
67 #endif
68 SSD1306_DISPLAYOFF, // display off
69 SSD1306_SETDISPLAYCLOCKDIV, 0x80,
70 SSD1306_SETMULTIPLEX, 31,
71 SSD1306_SETDISPLAYOFFSET, 0x00, // --no offset
72 SSD1306_SETSTARTLINE | 0x00,
73 SSD1306_CHARGEPUMP, 0x14, // 0x10
74 SSD1306_SEGREMAP | 0x01, // Reverse mapping
75 SSD1306_COMSCANDEC,
76 SSD1306_SETCOMPINS, 0x02,
77 SSD1306_SETCONTRAST, 0x7F, // contast value
78 SSD1306_SETPRECHARGE, 0x22, // 0x1F
79 SSD1306_SETVCOMDETECT, 0x40,
80 SSD1306_MEMORYMODE, HORIZONTAL_ADDRESSING_MODE,
81 SSD1306_DISPLAYALLON_RESUME,
82 SSD1306_NORMALDISPLAY,
83 SSD1306_DISPLAYON,
84 };
85
86 static void ssd1306_setBlock(lcduint_t x, lcduint_t y, lcduint_t w)
87 {
88 ssd1306_intf.start();
89 if (ssd1306_intf.spi)
90 ssd1306_spiDataMode(0);
91 else
92 ssd1306_intf.send(0x00);
93 ssd1306_intf.send(SSD1306_COLUMNADDR);
94 ssd1306_intf.send(x);
95 ssd1306_intf.send(w ? (x + w - 1) : (ssd1306_lcd.width - 1));
96 ssd1306_intf.send(SSD1306_PAGEADDR);
97 ssd1306_intf.send(y);
98 ssd1306_intf.send((ssd1306_lcd.height >> 3) - 1);
99 if (ssd1306_intf.spi)
100 {
101 ssd1306_spiDataMode(1);
102 }
103 else
104 {
105 ssd1306_intf.stop();
106 ssd1306_intf.start();
107 ssd1306_intf.send(0x40);
108 }
109 }
110
111 static void ssd1306_nextPage(void)
112 {
113 }
114
115 static void ssd1306_setMode_int(lcd_mode_t mode)
116 {
117 }
118
119 void ssd1306_displayOff()
120 {
121 ssd1306_sendCommand(SSD1306_DISPLAYOFF);
122 }
123
124
125 void ssd1306_displayOn()
126 {
127 ssd1306_sendCommand(SSD1306_DISPLAYON);
128 }
129
130 void ssd1306_setContrast(uint8_t contrast)
131 {
132 ssd1306_commandStart();
133 ssd1306_intf.send(SSD1306_SETCONTRAST);
134 ssd1306_intf.send(contrast);
135 ssd1306_intf.stop();
136 }
137
138 void ssd1306_invertMode()
139 {
140 ssd1306_sendCommand(SSD1306_INVERTDISPLAY);
141 }
142
143 void ssd1306_normalMode()
144 {
145 ssd1306_sendCommand(SSD1306_NORMALDISPLAY);
146 }
147
148 void ssd1306_flipHorizontal(uint8_t mode)
149 {
150 ssd1306_sendCommand( SSD1306_SEGREMAP | (mode ? 0x00: 0x01 ) );
151 }
152
153 void ssd1306_flipVertical(uint8_t mode)
154 {
155 ssd1306_sendCommand( mode ? SSD1306_COMSCANINC : SSD1306_COMSCANDEC );
156 }
157
158 void ssd1306_setStartLine(uint8_t line)
159 {
160 s_ssd1306_startLine = line;
161 ssd1306_sendCommand( SSD1306_SETSTARTLINE | (line & 0x3F) );
162 }
163
164 uint8_t ssd1306_getStartLine(void)
165 {
166 return s_ssd1306_startLine;
167 }
168
170 // I2C SSD1306 128x64
172
173 void ssd1306_init()
174 {
175 ssd1306_128x64_i2c_init();
176 }
177
178
179 void ssd1306_128x64_init()
180 {
181 ssd1306_lcd.type = LCD_TYPE_SSD1306;
182 ssd1306_lcd.height = 64;
183 ssd1306_lcd.width = 128;
184 ssd1306_lcd.set_block = ssd1306_setBlock;
185 ssd1306_lcd.next_page = ssd1306_nextPage;
186 ssd1306_lcd.send_pixels1 = ssd1306_intf.send;
187 ssd1306_lcd.send_pixels_buffer1 = ssd1306_intf.send_buffer;
188 ssd1306_lcd.set_mode = ssd1306_setMode_int;
189 for( uint8_t i=0; i<sizeof(s_oled128x64_initData); i++)
190 {
191 ssd1306_sendCommand(pgm_read_byte(&s_oled128x64_initData[i]));
192 }
193 }
194
195 void ssd1306_128x64_i2c_init()
196 {
197 ssd1306_i2cInit();
198 ssd1306_128x64_init();
199 }
200
201 void ssd1306_128x64_i2c_initEx(int8_t scl, int8_t sda, int8_t sa)
202 {
203 ssd1306_i2cInitEx(scl, sda, sa);
204 ssd1306_128x64_init();
205 }
206
208 // SPI SSD1306 128x64
210
211 void ssd1306_128x64_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
212 {
213 if (rstPin >=0)
214 {
215 ssd1306_resetController( rstPin, 10 );
216 }
217 ssd1306_spiInit(cesPin, dcPin);
218 ssd1306_128x64_init();
219 }
220
222 // I2C SSD1306 128x32
224
225 void ssd1306_128x32_init()
226 {
227 ssd1306_lcd.type = LCD_TYPE_SSD1306;
228 ssd1306_lcd.height = 32;
229 ssd1306_lcd.width = 128;
230 ssd1306_lcd.set_block = ssd1306_setBlock;
231 ssd1306_lcd.next_page = ssd1306_nextPage;
232 ssd1306_lcd.send_pixels1 = ssd1306_intf.send;
233 ssd1306_lcd.set_mode = ssd1306_setMode_int;
234 for( uint8_t i=0; i < sizeof(s_oled128x32_initData); i++)
235 {
236 ssd1306_sendCommand(pgm_read_byte(&s_oled128x32_initData[i]));
237 }
238 }
239
240
241 void ssd1306_128x32_i2c_init()
242 {
243 ssd1306_i2cInit();
244 ssd1306_128x32_init();
245 }
246
248 // SPI SSD1306 128x32
250
251 void ssd1306_128x32_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
252 {
253 if (rstPin >=0)
254 {
255 ssd1306_resetController( rstPin, 10 );
256 }
257 ssd1306_spiInit(cesPin, dcPin);
258 ssd1306_128x32_init();
259 }
ssd1306_getStartLine
uint8_t ssd1306_getStartLine(void)
Definition: oled_ssd1306.c:164
ssd1306_i2cInit
void ssd1306_i2cInit()
Definition: ssd1306_i2c.c:45
ssd1306_flipVertical
void ssd1306_flipVertical(uint8_t mode)
performs vertical flip
Definition: oled_ssd1306.c:153
ssd1306_invertMode
void ssd1306_invertMode(void)
Definition: oled_ssd1306.c:138
ssd1306_setContrast
void ssd1306_setContrast(uint8_t contrast)
Definition: oled_ssd1306.c:130
ssd1306_interface_t::send
void(* send)(uint8_t data)
Definition: ssd1306_interface.h:70
lcd_common.h
ssd1306_128x32_init
void ssd1306_128x32_init(void)
Inits 128x32 OLED display (based on ssd1306 controller).
Definition: oled_ssd1306.c:225
ssd1306_128x64_spi_init
void ssd1306_128x64_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
Inits 128x64 OLED display over spi (based on SSD1306 controller).
Definition: oled_ssd1306.c:211
ssd1306_displayOff
void ssd1306_displayOff(void)
Definition: oled_ssd1306.c:119
ssd1306_interface_t::stop
void(* stop)(void)
Definition: ssd1306_interface.h:65
ssd1306_flipHorizontal
void ssd1306_flipHorizontal(uint8_t mode)
performs horizontal flip
Definition: oled_ssd1306.c:148
LCD_TYPE_SSD1306
Definition: lcd_common.h:53
ssd1306_lcd_t::set_block
void(* set_block)(lcduint_t x, lcduint_t y, lcduint_t w)
Sets block in RAM of lcd display controller to write data to.
Definition: lcd_common.h:114
io.h
ssd1306_i2c.h
ssd1306_spiDataMode
void ssd1306_spiDataMode(uint8_t mode)
Definition: ssd1306_spi.c:50
lcd_mode_t
lcd_mode_t
Definition: lcd_common.h:69
ssd1306_lcd_t::send_pixels_buffer1
void(* send_pixels_buffer1)(const uint8_t *buffer, uint16_t len)
Definition: lcd_common.h:135
ssd1306_resetController
void ssd1306_resetController(int8_t rstPin, uint8_t delayMs)
Does hardware reset for oled controller.
Definition: lcd_common.c:139
ssd1306_128x64_init
void ssd1306_128x64_init()
Inits 128x64 OLED display (based on SSD1306 controller).
Definition: oled_ssd1306.c:179
ssd1306_lcd_t::send_pixels1
void(* send_pixels1)(uint8_t data)
Definition: lcd_common.h:128
ssd1306_128x64_i2c_initEx
void ssd1306_128x64_i2c_initEx(int8_t scl, int8_t sda, int8_t sa)
Inits 128x64 OLED display over i2c (based on SSD1306 controller).
Definition: oled_ssd1306.c:201
ssd1306_spi.h
ssd1306_interface_t::start
void(* start)(void)
Definition: ssd1306_interface.h:61
ssd1306_commands.h
ssd1306_spiInit
void ssd1306_spiInit(int8_t cesPin, int8_t dcPin)
Definition: ssd1306_spi.c:37
ssd1306_lcd
ssd1306_lcd_t ssd1306_lcd
Definition: lcd_common.c:33
ssd1306_128x32_i2c_init
void ssd1306_128x32_i2c_init(void)
Inits 128x32 OLED display over i2c (based on SSD1306 controller).
Definition: oled_ssd1306.c:241
ssd1306_intf
ssd1306_interface_t ssd1306_intf
Definition: ssd1306_interface.c:31
ssd1306_128x64_i2c_init
void ssd1306_128x64_i2c_init(void)
Inits 128x64 OLED display over i2c (based on SSD1306 controller).
Definition: oled_ssd1306.c:195
ssd1306_i2cInitEx
void ssd1306_i2cInitEx(int8_t scl, int8_t sda, int8_t sa)
Definition: ssd1306_i2c.c:28
ssd1306_lcd_t::height
lcduint_t height
Definition: lcd_common.h:97
ssd1306_displayOn
void ssd1306_displayOn(void)
Definition: oled_ssd1306.c:125
ssd1306_setStartLine
void ssd1306_setStartLine(uint8_t line)
Definition: oled_ssd1306.c:158
ssd1306_interface.h
ssd1306_128x32_spi_init
void ssd1306_128x32_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
Inits 128x32 OLED display over spi (based on SSD1306 controller).
Definition: oled_ssd1306.c:251
ssd1306_interface_t::spi
uint8_t spi
Definition: ssd1306_interface.h:57
ssd1306_commandStart
void ssd1306_commandStart(void)
Definition: ssd1306_interface.c:36
ssd1306_sendCommand
void ssd1306_sendCommand(uint8_t command)
Definition: ssd1306_interface.c:54
ssd1306_interface_t::send_buffer
void(* send_buffer)(const uint8_t *buffer, uint16_t size)
Sends bytes to SSD1306 device.
Definition: ssd1306_interface.h:80
ssd1306_lcd_t::next_page
void(* next_page)(void)
Definition: lcd_common.h:122
ssd1306_lcd_t::width
lcduint_t width
Definition: lcd_common.h:94
ssd1306_lcd_t::type
lcd_type_t type
Definition: lcd_common.h:91
ssd1306_normalMode
void ssd1306_normalMode(void)
Definition: oled_ssd1306.c:143
ssd1306_init
void ssd1306_init(void) __attribute__((deprecated))
Inits 128x64 OLED display over i2c (based on SSD1306 controller).
Definition: oled_ssd1306.c:173
ssd1306_lcd_t::set_mode
void(* set_mode)(lcd_mode_t mode)
Sets library display mode for direct draw functions.
Definition: lcd_common.h:164
oled_ssd1306.h