SSD1306 OLED display driver  1.6.99
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-2018, 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 
33 static const uint8_t PROGMEM s_oled128x64_initData[] =
34 {
35  SSD1306_DISPLAYOFF, // display off
36  SSD1306_MEMORYMODE, HORIZONTAL_ADDRESSING_MODE, // Page Addressing mode
37  SSD1306_COMSCANDEC, // Scan from 127 to 0 (Reverse scan)
38  SSD1306_SETSTARTLINE | 0x00, // First line to start scanning from
39  SSD1306_SETCONTRAST, 0x7F, // contast value to 0x7F according to datasheet
40  SSD1306_SEGREMAP | 0x01, // Use reverse mapping. 0x00 - is normal mapping
41  SSD1306_NORMALDISPLAY,
42  SSD1306_SETMULTIPLEX, 63, // Reset to default MUX. See datasheet
43  SSD1306_SETDISPLAYOFFSET, 0x00, // no offset
44  SSD1306_SETDISPLAYCLOCKDIV, 0x80,// set to default ratio/osc frequency
45  SSD1306_SETPRECHARGE, 0x22, // switch precharge to 0x22 // 0xF1
46  SSD1306_SETCOMPINS, 0x12, // set divide ratio
47  SSD1306_SETVCOMDETECT, 0x20, // vcom deselect to 0x20 // 0x40
48  SSD1306_CHARGEPUMP, 0x14, // Enable charge pump
49  SSD1306_DISPLAYALLON_RESUME,
50  SSD1306_DISPLAYON,
51 };
52 
53 static const uint8_t PROGMEM s_oled128x32_initData[] =
54 {
55  SSD1306_DISPLAYOFF, // display off
56  SSD1306_SETDISPLAYCLOCKDIV, 0x80,
57  SSD1306_SETMULTIPLEX, 31,
58  SSD1306_SETDISPLAYOFFSET, 0x00, // --no offset
59  SSD1306_SETSTARTLINE,
60  SSD1306_CHARGEPUMP, 0x14, // 0x10
61  SSD1306_SEGREMAP | 0x01, // Reverse mapping
62  SSD1306_COMSCANDEC,
63  SSD1306_SETCOMPINS, 0x02,
64  SSD1306_SETCONTRAST, 0x7F, // contast value
65  SSD1306_SETPRECHARGE, 0x22, // 0x1F
66  SSD1306_SETVCOMDETECT, 0x40,
67  SSD1306_MEMORYMODE, HORIZONTAL_ADDRESSING_MODE,
68  SSD1306_DISPLAYALLON_RESUME,
69  SSD1306_NORMALDISPLAY,
70  SSD1306_DISPLAYON,
71 };
72 
73 static void ssd1306_setBlock(lcduint_t x, lcduint_t y, lcduint_t w)
74 {
76  if (ssd1306_intf.spi)
78  else
79  ssd1306_intf.send(0x00);
80  ssd1306_intf.send(SSD1306_COLUMNADDR);
81  ssd1306_intf.send(x);
82  ssd1306_intf.send(w ? (x + w - 1) : (ssd1306_lcd.width - 1));
83  ssd1306_intf.send(SSD1306_PAGEADDR);
84  ssd1306_intf.send(y);
85  ssd1306_intf.send((ssd1306_lcd.height >> 3) - 1);
86  if (ssd1306_intf.spi)
87  {
89  }
90  else
91  {
94  ssd1306_intf.send(0x40);
95  }
96 }
97 
98 static void ssd1306_nextPage(void)
99 {
100 }
101 
102 static void ssd1306_setMode_int(lcd_mode_t mode)
103 {
104 }
105 
107 // I2C SSD1306 128x64
109 
111 {
113 }
114 
115 
117 {
119  ssd1306_lcd.height = 64;
120  ssd1306_lcd.width = 128;
121  ssd1306_lcd.set_block = ssd1306_setBlock;
122  ssd1306_lcd.next_page = ssd1306_nextPage;
125  ssd1306_lcd.set_mode = ssd1306_setMode_int;
126  for( uint8_t i=0; i<sizeof(s_oled128x64_initData); i++)
127  {
128  ssd1306_sendCommand(pgm_read_byte(&s_oled128x64_initData[i]));
129  }
130 }
131 
133 {
134  ssd1306_i2cInit();
136 }
137 
138 void ssd1306_128x64_i2c_initEx(int8_t scl, int8_t sda, int8_t sa)
139 {
140  ssd1306_i2cInitEx(scl, sda, sa);
142 }
143 
145 // SPI SSD1306 128x64
147 
148 void ssd1306_128x64_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
149 {
150  if (rstPin >=0)
151  {
152  pinMode(rstPin, OUTPUT);
153  digitalWrite(rstPin, HIGH);
154  /* Wait at least 1ms after VCC is up for LCD */
155  delay(1);
156  /* Perform reset operation of LCD display */
157  digitalWrite(rstPin, LOW);
158  delay(10);
159  digitalWrite(rstPin, HIGH);
160  }
161  ssd1306_spiInit(cesPin, dcPin);
163 }
164 
166 // I2C SSD1306 128x32
168 
170 {
172  ssd1306_lcd.height = 32;
173  ssd1306_lcd.width = 128;
174  ssd1306_lcd.set_block = ssd1306_setBlock;
175  ssd1306_lcd.next_page = ssd1306_nextPage;
178  for( uint8_t i=0; i < sizeof(s_oled128x32_initData); i++)
179  {
180  ssd1306_sendCommand(pgm_read_byte(&s_oled128x32_initData[i]));
181  }
182 }
183 
184 
186 {
187  ssd1306_i2cInit();
189 }
void ssd1306_setMode(lcd_mode_t mode)
Sets library display mode for direct draw functions.
Definition: lcd_common.c:74
void ssd1306_i2cInit(void)
void(* send)(uint8_t data)
void ssd1306_128x32_init(void)
Inits 128x32 OLED display (based on ssd1306 controller).
Definition: oled_ssd1306.c:169
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:148
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
void ssd1306_spiDataMode(uint8_t mode)
Definition: ssd1306_spi.c:56
lcd_mode_t
Definition: lcd_common.h:69
void(* send_pixels_buffer1)(const uint8_t *buffer, uint16_t len)
Definition: lcd_common.h:135
void ssd1306_128x64_init()
Inits 128x64 OLED display (based on SSD1306 controller).
Definition: oled_ssd1306.c:116
void(* send_pixels1)(uint8_t data)
Definition: lcd_common.h:128
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:138
void ssd1306_spiInit(int8_t cesPin, int8_t dcPin)
Definition: ssd1306_spi.c:39
ssd1306_lcd_t ssd1306_lcd
Definition: lcd_common.c:32
void ssd1306_128x32_i2c_init(void)
Inits 128x32 OLED display over i2c (based on SSD1306 controller).
Definition: oled_ssd1306.c:185
ssd1306_interface_t ssd1306_intf
void ssd1306_128x64_i2c_init(void)
Inits 128x64 OLED display over i2c (based on SSD1306 controller).
Definition: oled_ssd1306.c:132
void ssd1306_i2cInitEx(int8_t scl, int8_t sda, int8_t sa)
lcduint_t height
Definition: lcd_common.h:97
void ssd1306_sendCommand(uint8_t command)
void(* send_buffer)(const uint8_t *buffer, uint16_t size)
Sends bytes to SSD1306 device.
void(* next_page)(void)
Definition: lcd_common.h:122
lcduint_t width
Definition: lcd_common.h:94
lcd_type_t type
Definition: lcd_common.h:91
void ssd1306_init(void) __attribute__((deprecated))
Inits 128x64 OLED display over i2c (based on SSD1306 controller).
Definition: oled_ssd1306.c:110
void(* set_mode)(lcd_mode_t mode)
Sets library display mode for direct draw functions.
Definition: lcd_common.h:157