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

oled_ssd1351.c
1 /*
2 MIT License
3
4 Copyright (c) 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_ssd1351.h"
26 #include "lcd_common.h"
27 #include "ssd1351_commands.h"
28 #include "intf/ssd1306_interface.h"
29 #include "intf/spi/ssd1306_spi.h"
30 #include "ssd1306_hal/io.h"
31 #include "nano_gfx_types.h"
32 #ifdef SDL_EMULATION
33 #include "sdl_core.h"
34 #endif
35
36 #define CMD_ARG 0xFF
37
38 extern uint16_t ssd1306_color;
39 extern uint32_t s_ssd1306_spi_clock;
40
41 static const PROGMEM uint8_t s_oled128x128_initData[] =
42 {
43 #ifdef SDL_EMULATION
44 SDL_LCD_SSD1351,
45 0x00,
46 #endif
47 SSD1351_UNLOCK, CMD_ARG, 0x12,
48 SSD1351_UNLOCK, CMD_ARG, 0xB1,
49 SSD1351_SLEEP_ON,
50 SSD1351_CLOCKDIV, CMD_ARG, 0xF1, // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
51 SSD1351_SETMULTIPLEX, CMD_ARG, 127, // Reset to default MUX. See datasheet
52 SSD1351_SEGREMAP, CMD_ARG, 0B00110101, // 16-bit rgb color mode
53 SSD1351_SETSTARTLINE, CMD_ARG, 0x00, // First line to start scanning from
54 SSD1351_SETDISPLAYOFFSET, CMD_ARG, 0x00, // Set display offset
55 SSD1351_SETGPIO, CMD_ARG, 0x00, // GPIO OFF
56 SSD1351_SETFUNCTION, CMD_ARG, 0x01,
57 SSD1351_SETPRECHARGE, CMD_ARG, 0x32, // Phase 1 and Phase 2 periods
58 SSD1351_VCOMH, CMD_ARG, 0x05, //
59 SSD1351_PRECHARGELEVEL, CMD_ARG, 0x17,
60 SSD1351_NORMALDISPLAY,
61 SSD1351_CONTRAST, CMD_ARG, 0xC8, // RED
62 CMD_ARG, 0x80, // GREEN
63 CMD_ARG, 0xC8, // BLUE
64 SSD1351_MASTERCURRENT, CMD_ARG, 0x0F, //
65 SSD1351_EXTVSL, CMD_ARG, 0xA0, CMD_ARG, 0xB5, CMD_ARG, 0x55,
66 SSD1351_PRECHARGESECOND, CMD_ARG, 0x01, //
67 SSD1351_SLEEP_OFF, // Disable power-safe mode
68 SSD1351_NORMALDISPLAY,
69 };
70
71 static uint8_t s_column;
72 static uint8_t s_page;
73
74 static void ssd1351_setBlock(lcduint_t x, lcduint_t y, lcduint_t w)
75 {
76 uint8_t rx = w ? (x + w - 1) : (ssd1306_lcd.width - 1);
77 s_column = x;
78 s_page = y;
79 ssd1306_intf.start();
80 ssd1306_spiDataMode(0);
81 ssd1306_intf.send(SSD1351_COLUMNADDR);
82 ssd1306_spiDataMode(1); // According to datasheet all args must be passed in data mode
83 ssd1306_intf.send(x);
84 ssd1306_intf.send(rx < ssd1306_lcd.width ? rx : (ssd1306_lcd.width - 1));
85 ssd1306_spiDataMode(0);
86 ssd1306_intf.send(SSD1351_ROWADDR);
87 ssd1306_spiDataMode(1); // According to datasheet all args must be passed in data mode
88 ssd1306_intf.send(y<<3);
89 ssd1306_intf.send(((y<<3) + 7) < ssd1306_lcd.height ? ((y<<3) + 7) : (ssd1306_lcd.height - 1));
90 ssd1306_spiDataMode(0);
91 ssd1306_intf.send(SSD1331_WRITEDATA);
92 ssd1306_spiDataMode(1);
93 }
94
95 static void ssd1351_setBlock2(lcduint_t x, lcduint_t y, lcduint_t w)
96 {
97 uint8_t rx = w ? (x + w - 1) : (ssd1306_lcd.width - 1);
98 ssd1306_intf.start();
99 ssd1306_spiDataMode(0);
100 ssd1306_intf.send(SSD1351_COLUMNADDR);
101 ssd1306_spiDataMode(1); // According to datasheet all args must be passed in data mode
102 ssd1306_intf.send(x);
103 ssd1306_intf.send(rx < ssd1306_lcd.width ? rx : (ssd1306_lcd.width - 1));
104 ssd1306_spiDataMode(0);
105 ssd1306_intf.send(SSD1351_ROWADDR);
106 ssd1306_spiDataMode(1); // According to datasheet all args must be passed in data mode
107 ssd1306_intf.send(y);
108 ssd1306_intf.send(ssd1306_lcd.height - 1);
109 ssd1306_spiDataMode(0);
110 ssd1306_intf.send(SSD1331_WRITEDATA);
111 ssd1306_spiDataMode(1);
112 }
113
114 static void ssd1351_nextPage(void)
115 {
116 ssd1306_intf.stop();
117 ssd1351_setBlock(s_column,s_page+1,0);
118 }
119
120 static void ssd1351_nextPage2(void)
121 {
122 }
123
124 void ssd1351_setMode(lcd_mode_t mode)
125 {
126 ssd1306_intf.start();
127 ssd1306_spiDataMode(0);
128 ssd1306_intf.send( SSD1351_SEGREMAP );
129 ssd1306_spiDataMode(1);
130 ssd1306_intf.send( 0B00110100 | mode );
131 ssd1306_intf.stop();
132 if (mode)
133 {
134 ssd1306_lcd.set_block = ssd1351_setBlock;
135 ssd1306_lcd.next_page = ssd1351_nextPage;
136 }
137 else
138 {
139 ssd1306_lcd.set_block = ssd1351_setBlock2;
140 ssd1306_lcd.next_page = ssd1351_nextPage2;
141 }
142 }
143
144 static void ssd1351_sendPixels(uint8_t data)
145 {
146 for (uint8_t i=8; i>0; i--)
147 {
148 if ( data & 0x01 )
149 {
150 ssd1306_intf.send( (uint8_t)(ssd1306_color>>8) );
151 ssd1306_intf.send( (uint8_t)(ssd1306_color) );
152 }
153 else
154 {
155 ssd1306_intf.send( 0B00000000 );
156 ssd1306_intf.send( 0B00000000 );
157 }
158 data >>= 1;
159 }
160 }
161
162 static void ssd1351_sendPixelsBuffer(const uint8_t *buffer, uint16_t len)
163 {
164 while(len--)
165 {
166 ssd1351_sendPixels(*buffer);
167 buffer++;
168 }
169 }
170
171 static void ssd1351_sendPixel8(uint8_t data)
172 {
173 uint16_t color = RGB8_TO_RGB16(data);
174 ssd1306_intf.send( color >> 8 );
175 ssd1306_intf.send( color & 0xFF );
176 }
177
178 void ssd1351_128x128_init()
179 {
180 ssd1306_lcd.type = LCD_TYPE_SSD1331;
181 ssd1306_lcd.height = 128;
182 ssd1306_lcd.width = 128;
183 ssd1306_lcd.set_block = ssd1351_setBlock;
184 ssd1306_lcd.next_page = ssd1351_nextPage;
185 ssd1306_lcd.send_pixels1 = ssd1351_sendPixels;
186 ssd1306_lcd.send_pixels_buffer1 = ssd1351_sendPixelsBuffer;
187 ssd1306_lcd.send_pixels8 = ssd1351_sendPixel8;
188 ssd1306_lcd.set_mode = ssd1351_setMode;
189 ssd1306_intf.start();
190 ssd1306_spiDataMode(0);
191 for( uint8_t i=0; i<sizeof(s_oled128x128_initData); i++)
192 {
193 uint8_t data = pgm_read_byte(&s_oled128x128_initData[i]);
194 if (data == CMD_ARG)
195 {
196 data = pgm_read_byte(&s_oled128x128_initData[++i]);
197 ssd1306_spiDataMode(1);
198 ssd1306_intf.send(data);
199 ssd1306_spiDataMode(0);
200 }
201 else
202 {
203 ssd1306_intf.send(data);
204 }
205 }
206 ssd1306_intf.stop();
207 }
208
209 void ssd1351_128x128_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
210 {
211 if (rstPin >=0)
212 {
213 ssd1306_resetController( rstPin, 20 );
214 }
215 /* ssd1351 cannot work faster than at 4MHz per datasheet */
216 s_ssd1306_spi_clock = 4000000;
217 ssd1306_spiInit(cesPin, dcPin);
218 ssd1351_128x128_init();
219 }
oled_ssd1351.h
ssd1351_setMode
void ssd1351_setMode(lcd_mode_t mode)
Sets GDRAM autoincrement mode.
Definition: oled_ssd1351.c:124
ssd1306_interface_t::send
void(* send)(uint8_t data)
Definition: ssd1306_interface.h:70
lcd_common.h
ssd1306_interface_t::stop
void(* stop)(void)
Definition: ssd1306_interface.h:65
ssd1351_commands.h
s_ssd1306_spi_clock
uint32_t s_ssd1306_spi_clock
Definition: ssd1306_spi.c:35
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
ssd1351_128x128_init
void ssd1351_128x128_init(void)
Inits 128x128 RGB OLED display (based on SSD1351 controller).
Definition: oled_ssd1351.c:178
ssd1306_lcd_t::send_pixels8
void(* send_pixels8)(uint8_t data)
Sends RGB pixel encoded in 3-3-2 format to OLED driver. Sends RGB pixel encoded in 3-3-2 format to OL...
Definition: lcd_common.h:142
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:82
ssd1306_lcd_t::send_pixels1
void(* send_pixels1)(uint8_t data)
Definition: lcd_common.h:128
ssd1306_spi.h
ssd1306_interface_t::start
void(* start)(void)
Definition: ssd1306_interface.h:61
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:32
ssd1351_128x128_spi_init
void ssd1351_128x128_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
Inits 128x128 RGB OLED display over spi (based on SSD1351 controller).
Definition: oled_ssd1351.c:209
nano_gfx_types.h
ssd1306_intf
ssd1306_interface_t ssd1306_intf
Definition: ssd1306_interface.c:29
ssd1306_lcd_t::height
lcduint_t height
Definition: lcd_common.h:97
ssd1306_interface.h
RGB8_TO_RGB16
#define RGB8_TO_RGB16(c)
Definition: nano_gfx_types.h:50
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_lcd_t::set_mode
void(* set_mode)(lcd_mode_t mode)
Sets library display mode for direct draw functions.
Definition: lcd_common.h:157
LCD_TYPE_SSD1331
Definition: lcd_common.h:59