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

oled_ssd1327.c
1 /*
2 MIT License
3
4 Copyright (c) 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_ssd1327.h"
26 #include "lcd_common.h"
27 #include "intf/ssd1306_interface.h"
28 #include "intf/spi/ssd1306_spi.h"
29 #include "ssd1306_hal/io.h"
30 #ifdef SDL_EMULATION
31 #include "sdl_core.h"
32 #endif
33
34 extern uint16_t ssd1306_color;
35
36 static const PROGMEM uint8_t s_oled_128x128_initData[] =
37 {
38 #ifdef SDL_EMULATION
39 SDL_LCD_SSD1327,
40 SDL_LCD_SSD1327_GENERIC,
41 #endif
42 0xFD, 0x12, // Unlock OLED
43 0xAE, // OFF /* display off */
44 0xA8, 0x7F, // multiplex 128
45 0xA1, 0x00, // Start line
46 0xA2, 0x00, // Display offset
47 0xA0, 0x40 | 0x10 | 0x04 | (0x02 | 0x01), // REMAP: vertical increment mode
48 0xAB, 0x01, // VDD internal
49 0x81, 0x70, // CONTRAST
50 0xB1, 0x55, // PHASE 0x51
51 0xB3, 0x01, // CLK
52 // 0xB9, //Reload grey scale
53 0xBC, 0x08, // PRECHARGE
54 0xBE, 0x07, // VCOMH voltage
55 0xB6, 0x01, // Second pre-charge
56 0xA4, // NORMAL
57 0x2E, // Deactivate scroll
58 0xAF, // Display ON
59 };
60
63
65
66 static uint8_t __s_column;
67 static uint8_t __s_w;
68 static uint8_t __s_w2;
69 static uint8_t __s_page;
70 static uint8_t __s_leftPixel;
71 static uint8_t __s_pos;
72
73 static void set_block_compat(lcduint_t x, lcduint_t y, lcduint_t w)
74 {
75 uint8_t rx = w ? (x + w - 1) : (ssd1306_lcd.width - 1);
76 rx = rx < ssd1306_lcd.width ? rx : (ssd1306_lcd.width - 1);
77 __s_column = x;
78 __s_page = y;
79 __s_w = w;
80 __s_w2 = rx - x + 1;
81 __s_leftPixel = 0;
82 __s_pos = __s_column;
83 ssd1306_intf.start();
84 ssd1306_spiDataMode(0);
85 ssd1306_intf.send(0x15);
86 ssd1306_intf.send(x / 2);
87 ssd1306_intf.send(rx / 2);
88 ssd1306_intf.send(0x75);
89 ssd1306_intf.send(y<<3);
90 ssd1306_intf.send(((y<<3) + 7) < ssd1306_lcd.height ? ((y<<3) + 7) : (ssd1306_lcd.height - 1));
91 ssd1306_spiDataMode(1);
92 }
93
94 static void ssd1327_sendPixels(uint8_t data);
95
96 static void next_page_compat(void)
97 {
98 ssd1306_intf.stop();
99 set_block_compat(__s_column,__s_page + 1, __s_w);
100 }
101
102 //SSD1306_COMPAT_SPI_BLOCK_8BIT_CMDS( 0x15, 0x75 );
103
104 static void ssd1327_sendPixels(uint8_t data)
105 {
106 if (!(__s_pos & 0x01))
107 {
108 __s_leftPixel = data;
109 data = 0x00;
110 }
111 if ((__s_pos & 0x01) || (__s_pos == __s_column + __s_w2 - 1))
112 {
113 for (uint8_t i=8; i>0; i--)
114 {
115 uint8_t color = (__s_leftPixel & 0x01) ? (ssd1306_color & 0x0F) : 0;
116 color |= (((data & 0x01) ? (ssd1306_color & 0x0F): 0) << 4);
117 ssd1306_intf.send(color);
118 data >>= 1;
119 __s_leftPixel >>= 1;
120 }
121 }
122 __s_pos++;
123 }
124
125 static void ssd1327_sendPixels8(uint8_t data)
126 {
127 ssd1306_intf.send( data );
128 }
129
130 static void ssd1327_sendPixelsBuffer(const uint8_t *buffer, uint16_t len)
131 {
132 while (len--)
133 {
134 ssd1327_sendPixels(*buffer);
135 buffer++;
136 }
137 }
138
140
141 CONTROLLER_NATIVE_SPI_BLOCK_8BIT_CMDS( 0x15, 0x75 );
142
145
146 static void ssd1327_setMode(lcd_mode_t mode)
147 {
148 if (mode == LCD_MODE_NORMAL)
149 {
150 ssd1306_lcd.set_block = set_block_native;
151 ssd1306_lcd.next_page = next_page_native;
152 }
153 else if (mode == LCD_MODE_SSD1306_COMPAT )
154 {
155 ssd1306_lcd.set_block = set_block_compat;
156 ssd1306_lcd.next_page = next_page_compat;
157 }
158 ssd1306_intf.start();
159 ssd1306_spiDataMode(0);
160 ssd1306_intf.send( 0xA0 );
161 ssd1306_intf.send( 0x10 | (mode == LCD_MODE_NORMAL ? 0x00 : 0x04) );
162 ssd1306_intf.stop();
163 return;
164 }
165
166 void ssd1327_128x128_init()
167 {
168 ssd1306_lcd.type = LCD_TYPE_CUSTOM;
169 ssd1306_lcd.width = 128; // specify width
170 ssd1306_lcd.height = 128; // specify height
171 // Set functions for compatible mode
172 ssd1306_lcd.set_block = set_block_compat;
173 ssd1306_lcd.next_page = next_page_compat;
174 ssd1306_lcd.send_pixels1 = ssd1327_sendPixels;
175 ssd1306_lcd.send_pixels_buffer1 = ssd1327_sendPixelsBuffer;
176 // Set function for 8-bit mode
177 ssd1306_lcd.send_pixels8 = ssd1327_sendPixels8;
178 ssd1306_lcd.set_mode = ssd1327_setMode;
179 // Use one of 2 functions for initialization below
180 // Please, read help on this functions and read datasheet before you decide, which
181 // one needs to be used. For example, ssd1331 is OK with ssd1306_configureI2cDisplay(),
182 // while st7735 can be initialized only with ssd1306_configureSpiDisplay().
183 ssd1306_configureI2cDisplay(s_oled_128x128_initData, sizeof(s_oled_128x128_initData));
184 delay(100);
185 }
186
187 void ssd1327_128x128_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
188 {
189 if (rstPin >=0)
190 {
191 ssd1306_resetController( rstPin, 10 );
192 }
193 ssd1306_spiInit(cesPin, dcPin);
194 ssd1327_128x128_init();
195 }
CONTROLLER_NATIVE_SPI_BLOCK_8BIT_CMDS
#define CONTROLLER_NATIVE_SPI_BLOCK_8BIT_CMDS(column_cmd, row_cmd)
Definition: lcd_common.h:366
ssd1306_interface_t::send
void(* send)(uint8_t data)
Definition: ssd1306_interface.h:70
lcd_common.h
LCD_MODE_NORMAL
Definition: lcd_common.h:75
ssd1306_configureI2cDisplay
void ssd1306_configureI2cDisplay(const uint8_t *config, uint8_t configSize)
Sends configuration being passed to lcd display i2c/spi controller.
Definition: lcd_common.c:42
ssd1306_interface_t::stop
void(* stop)(void)
Definition: ssd1306_interface.h:65
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_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:139
ssd1306_lcd_t::send_pixels1
void(* send_pixels1)(uint8_t data)
Definition: lcd_common.h:128
ssd1327_128x128_init
void ssd1327_128x128_init()
Inits 128x128 SSD1327 OLED display (based on SSD1327 controller).
Definition: oled_ssd1327.c:166
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:33
ssd1327_128x128_spi_init
void ssd1327_128x128_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
Inits 128x128 SSD1327 OLED display over spi (based on SSD1327 controller).
Definition: oled_ssd1327.c:187
ssd1306_intf
ssd1306_interface_t ssd1306_intf
Definition: ssd1306_interface.c:31
LCD_MODE_SSD1306_COMPAT
Definition: lcd_common.h:82
ssd1306_lcd_t::height
lcduint_t height
Definition: lcd_common.h:97
ssd1306_interface.h
ssd1306_lcd_t::next_page
void(* next_page)(void)
Definition: lcd_common.h:122
LCD_TYPE_CUSTOM
Definition: lcd_common.h:61
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:164