SSD1306 OLED display driver  1.7.6
This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
oled_ssd1331.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_ssd1331.h"
26 #include "lcd_common.h"
27 #include "ssd1331_commands.h"
28 #include "intf/ssd1306_interface.h"
29 #include "intf/spi/ssd1306_spi.h"
30 #include "ssd1306_hal/io.h"
31 #ifdef SDL_EMULATION
32 #include "sdl_core.h"
33 #endif
34 
35 extern uint16_t ssd1306_color;
36 
37 static const PROGMEM uint8_t s_oled96x64_initData[] =
38 {
39 #ifdef SDL_EMULATION
40  SDL_LCD_SSD1331,
41  0x00,
42 #endif
43  SSD1331_DISPLAYOFF, // display off
44  SSD1331_SEGREMAP, 0x00 | 0x20 | 0x10 | 0x02 | 0x01, /* 8-bit rgb color mode */
45  SSD1331_SETSTARTLINE, 0x00, // First line to start scanning from
46  SSD1331_SETDISPLAYOFFSET, 0x00, // Set display offset
47  SSD1331_NORMALDISPLAY,
48  SSD1331_SETMULTIPLEX, 63, // Reset to default MUX. See datasheet
49  SSD1331_SETMASTER, 0x8E, // Set master mode
50  SSD1331_POWERMODE, 0x0B, // Disable power-safe mode
51  SSD1331_SETPRECHARGE, 0x31, // Phase 1 and Phase 2 periods
52  SSD1331_CLOCKDIV, 0xF0, // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
53  SSD1331_PRECHARGEA, 0x64,
54  SSD1331_PRECHARGEB, 0x78,
55  SSD1331_PRECHARGELEVEL, 0x3A,
56  SSD1331_VCOMH, 0x3E,
57  SSD1331_MASTERCURRENT, 0x09,
58  SSD1331_CONTRASTA, 0x91, // RED
59  SSD1331_CONTRASTB, 0x50, // GREEN
60  SSD1331_CONTRASTC, 0x7D, // BLUE
61  SSD1331_DISPLAYON,
62 };
63 
64 static uint8_t s_rotation = 0x04;
65 
67 
69  (s_rotation & 1) ? SSD1331_ROWADDR: SSD1331_COLUMNADDR,
70  (s_rotation & 1) ? SSD1331_COLUMNADDR: SSD1331_ROWADDR );
71 
73 
75 
77  (s_rotation & 1) ? SSD1331_ROWADDR: SSD1331_COLUMNADDR,
78  (s_rotation & 1) ? SSD1331_COLUMNADDR: SSD1331_ROWADDR );
79 
81 
83 {
84  if (mode == LCD_MODE_NORMAL)
85  {
86  s_rotation &= ~0x04;
87  ssd1306_lcd.set_block = set_block_native;
88  ssd1306_lcd.next_page = next_page_native;
89  }
90  else
91  {
92  s_rotation |= 0x04;
93  ssd1306_lcd.set_block = set_block_compat;
94  ssd1306_lcd.next_page = next_page_compat;
95  }
96  ssd1331_setRotation( s_rotation );
97  return;
98 }
99 
100 void ssd1331_setRotation(uint8_t rotation)
101 {
102  uint8_t ram_mode;
103  if ((rotation^s_rotation) & 0x01)
104  {
105  uint16_t t = ssd1306_lcd.width;
107  ssd1306_lcd.height = t;
108  }
109  s_rotation = (rotation & 0x03) | (s_rotation & 0x04);
112  ssd1306_intf.send( SSD1331_SEGREMAP );
113  switch (s_rotation)
114  {
115  // NORMAL FULL COLOR MODE
116  case 0:
117  ram_mode = 0b00110010;
118  break;
119  case 1: // 90 degree CW
120  ram_mode = 0b00110001;
121  break;
122  case 2: // 180 degree CW
123  ram_mode = 0b00100000;
124  break;
125  case 3: // 270 degree CW
126  ram_mode = 0b00100011;
127  break;
128  // SSD1306_COMPATIBLE mode
129  case 4:
130  ram_mode = 0b00110011;
131  break;
132  case 5: // 90 degree CW
133  ram_mode = 0b00110000;
134  break;
135  case 6: // 180 degree CW
136  ram_mode = 0b00100001;
137  break;
138  case 7: // 270 degree CW
139  ram_mode = 0b00100010;
140  break;
141  default: // 270 degree CW
142  ram_mode = 0b00100000;
143  break;
144  }
145  ssd1306_intf.send( ram_mode );
146  ssd1306_intf.stop();
147 }
148 
150 {
152  ssd1306_lcd.height = 64;
153  ssd1306_lcd.width = 96;
154  ssd1306_lcd.set_block = set_block_compat;
155  ssd1306_lcd.next_page = next_page_compat;
156  ssd1306_lcd.send_pixels1 = send_pixels_compat;
157  ssd1306_lcd.send_pixels_buffer1 = send_pixels_buffer_compat;
158 
161  for( uint8_t i=0; i<sizeof(s_oled96x64_initData); i++)
162  {
163  ssd1306_sendCommand(pgm_read_byte(&s_oled96x64_initData[i]));
164  }
165 }
166 
167 void ssd1331_96x64_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
168 {
169  if (rstPin >=0)
170  {
171  ssd1306_resetController( rstPin, 10 );
172  }
173  ssd1306_spiInit(cesPin, dcPin);
175 }
176 
177 void ssd1331_drawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint16_t color)
178 {
181  ssd1306_intf.send(SSD1331_DRAWLINE);
182  ssd1306_intf.send(x1);
183  ssd1306_intf.send(y1);
184  ssd1306_intf.send(x2);
185  ssd1306_intf.send(y2);
186  ssd1306_intf.send( (color & 0x03) << 4 );
187  ssd1306_intf.send( (color & 0x1C) << 2 );
188  ssd1306_intf.send( (color & 0xE0) >> 2 );
189  ssd1306_intf.stop();
190 }
191 
192 void ssd1331_copyBlock(uint8_t left, uint8_t top, uint8_t right, uint8_t bottom, uint8_t newLeft, uint8_t newTop)
193 {
196  ssd1306_intf.send(0x23);
197  ssd1306_intf.send(left);
198  ssd1306_intf.send(top);
199  ssd1306_intf.send(right);
200  ssd1306_intf.send(bottom);
201  ssd1306_intf.send(newLeft);
202  ssd1306_intf.send(newTop);
203  ssd1306_intf.stop();
204 }
205 
#define CONTROLLER_NATIVE_SPI_BLOCK_8BIT_CMDS(column_cmd, row_cmd)
Definition: lcd_common.h:345
void(* send)(uint8_t data)
#define SSD1306_COMPAT_SEND_PIXELS_RGB8_CMDS()
Definition: lcd_common.h:369
void ssd1331_setMode(lcd_mode_t mode)
Sets GDRAM autoincrement mode.
Definition: oled_ssd1331.c:82
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(* 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
void ssd1306_spiDataMode(uint8_t mode)
Definition: ssd1306_spi.c:50
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_resetController(int8_t rstPin, uint8_t delayMs)
Does hardware reset for oled controller.
Definition: lcd_common.c:82
void(* send_pixels1)(uint8_t data)
Definition: lcd_common.h:128
void ssd1331_drawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint16_t color)
Definition: oled_ssd1331.c:177
void ssd1306_spiInit(int8_t cesPin, int8_t dcPin)
Definition: ssd1306_spi.c:37
ssd1306_lcd_t ssd1306_lcd
Definition: lcd_common.c:32
void ssd1331_copyBlock(uint8_t left, uint8_t top, uint8_t right, uint8_t bottom, uint8_t newLeft, uint8_t newTop)
Definition: oled_ssd1331.c:192
ssd1306_interface_t ssd1306_intf
lcduint_t height
Definition: lcd_common.h:97
#define SSD1306_COMPAT_SPI_BLOCK_8BIT_CMDS(column_cmd, row_cmd)
Definition: lcd_common.h:309
void ssd1306_sendCommand(uint8_t command)
void(* next_page)(void)
Definition: lcd_common.h:122
void ssd1331_setRotation(uint8_t rotation)
Sets screen orientation (rotation)
Definition: oled_ssd1331.c:100
void ssd1331_96x64_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
Inits 96x64 RGB OLED display over spi (based on SSD1331 controller).
Definition: oled_ssd1331.c:167
lcduint_t width
Definition: lcd_common.h:94
lcd_type_t type
Definition: lcd_common.h:91
void(* set_mode)(lcd_mode_t mode)
Sets library display mode for direct draw functions.
Definition: lcd_common.h:157
void ssd1331_96x64_init(void)
Inits 96x64 RGB OLED display (based on SSD1331 controller).
Definition: oled_ssd1331.c:149