SSD1306 OLED display driver  1.7.1
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 
32 extern uint16_t ssd1306_color;
33 
34 static const PROGMEM uint8_t s_oled96x64_initData[] =
35 {
36  SSD1331_DISPLAYOFF, // display off
37  SSD1331_SEGREMAP, 0x00 | 0x20 | 0x10 | 0x02 | 0x01, /* 8-bit rgb color mode */
38  SSD1331_SETSTARTLINE, 0x00, // First line to start scanning from
39  SSD1331_SETDISPLAYOFFSET, 0x00, // Set display offset
40  SSD1331_NORMALDISPLAY,
41  SSD1331_SETMULTIPLEX, 63, // Reset to default MUX. See datasheet
42  SSD1331_SETMASTER, 0x8E, // Set master mode
43  SSD1331_POWERMODE, 0x0B, // Disable power-safe mode
44  SSD1331_SETPRECHARGE, 0x31, // Phase 1 and Phase 2 periods
45  SSD1331_CLOCKDIV, 0xF0, // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
46  SSD1331_PRECHARGEA, 0x64,
47  SSD1331_PRECHARGEB, 0x78,
48  SSD1331_PRECHARGELEVEL, 0x3A,
49  SSD1331_VCOMH, 0x3E,
50  SSD1331_MASTERCURRENT, 0x09,
51  SSD1331_CONTRASTA, 0x91, // RED
52  SSD1331_CONTRASTB, 0x50, // GREEN
53  SSD1331_CONTRASTC, 0x7D, // BLUE
54  SSD1331_DISPLAYON,
55 };
56 
57 static uint8_t s_column;
58 static uint8_t s_page;
59 static uint8_t s_rotation = 0x04;
60 
61 static void ssd1331_setBlock(lcduint_t x, lcduint_t y, lcduint_t w)
62 {
63  uint8_t rx = w ? (x + w - 1) : (ssd1306_lcd.width - 1);
64  s_column = x;
65  s_page = y;
68  ssd1306_intf.send((s_rotation & 1) ? SSD1331_ROWADDR: SSD1331_COLUMNADDR);
69  ssd1306_intf.send(x);
71  ssd1306_intf.send((s_rotation & 1) ? SSD1331_COLUMNADDR: SSD1331_ROWADDR);
72  ssd1306_intf.send(y<<3);
73  ssd1306_intf.send(((y<<3) + 7) < ssd1306_lcd.height ? ((y<<3) + 7) : (ssd1306_lcd.height - 1));
75 }
76 
77 static void ssd1331_setBlock2(lcduint_t x, lcduint_t y, lcduint_t w)
78 {
79  uint8_t rx = w ? (x + w - 1) : (ssd1306_lcd.width - 1);
82  ssd1306_intf.send((s_rotation & 1) ? SSD1331_ROWADDR: SSD1331_COLUMNADDR);
83  ssd1306_intf.send(x);
85  ssd1306_intf.send((s_rotation & 1) ? SSD1331_COLUMNADDR: SSD1331_ROWADDR);
86  ssd1306_intf.send(y);
89 }
90 
91 static void ssd1331_nextPage(void)
92 {
94  ssd1331_setBlock(s_column,s_page+1,0);
95 }
96 
97 static void ssd1331_nextPage2(void)
98 {
99 }
100 
101 static void ssd1331_sendPixels(uint8_t data)
102 {
103  for (uint8_t i=8; i>0; i--)
104  {
105  if ( data & 0x01 )
106  {
107  ssd1306_intf.send( (uint8_t)ssd1306_color );
108  }
109  else
110  {
111  ssd1306_intf.send( 0B00000000 );
112  }
113  data >>= 1;
114  }
115 }
116 
117 static void ssd1331_sendPixelsBuffer(const uint8_t *buffer, uint16_t len)
118 {
119  while(len--)
120  {
121  ssd1331_sendPixels(*buffer);
122  buffer++;
123  }
124 }
125 
127 {
128  if (mode == LCD_MODE_NORMAL)
129  {
130  s_rotation &= ~0x04;
131  ssd1306_lcd.set_block = ssd1331_setBlock2;
132  ssd1306_lcd.next_page = ssd1331_nextPage2;
133  }
134  else
135  {
136  s_rotation |= 0x04;
137  ssd1306_lcd.set_block = ssd1331_setBlock;
138  ssd1306_lcd.next_page = ssd1331_nextPage;
139  }
140  ssd1331_setRotation( s_rotation );
141  return;
142 }
143 
144 void ssd1331_setRotation(uint8_t rotation)
145 {
146  uint8_t ram_mode;
147  if ((rotation^s_rotation) & 0x01)
148  {
149  uint16_t t = ssd1306_lcd.width;
151  ssd1306_lcd.height = t;
152  }
153  s_rotation = (rotation & 0x03) | (s_rotation & 0x04);
156  ssd1306_intf.send( SSD1331_SEGREMAP );
157  switch (s_rotation)
158  {
159  // NORMAL FULL COLOR MODE
160  case 0:
161  ram_mode = 0b00110010;
162  break;
163  case 1: // 90 degree CW
164  ram_mode = 0b00110001;
165  break;
166  case 2: // 180 degree CW
167  ram_mode = 0b00100000;
168  break;
169  case 3: // 270 degree CW
170  ram_mode = 0b00100011;
171  break;
172  // SSD1306_COMPATIBLE mode
173  case 4:
174  ram_mode = 0b00110011;
175  break;
176  case 5: // 90 degree CW
177  ram_mode = 0b00110000;
178  break;
179  case 6: // 180 degree CW
180  ram_mode = 0b00100001;
181  break;
182  case 7: // 270 degree CW
183  ram_mode = 0b00100010;
184  break;
185  default: // 270 degree CW
186  ram_mode = 0b00100000;
187  break;
188  }
189  ssd1306_intf.send( ram_mode );
190  ssd1306_intf.stop();
191 }
192 
193 
194 
196 {
198  ssd1306_lcd.height = 64;
199  ssd1306_lcd.width = 96;
200  ssd1306_lcd.set_block = ssd1331_setBlock;
201  ssd1306_lcd.next_page = ssd1331_nextPage;
202  ssd1306_lcd.send_pixels1 = ssd1331_sendPixels;
203  ssd1306_lcd.send_pixels_buffer1 = ssd1331_sendPixelsBuffer;
206  for( uint8_t i=0; i<sizeof(s_oled96x64_initData); i++)
207  {
208  ssd1306_sendCommand(pgm_read_byte(&s_oled96x64_initData[i]));
209  }
210 }
211 
212 void ssd1331_96x64_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
213 {
214  if (rstPin >=0)
215  {
216  pinMode(rstPin, OUTPUT);
217  digitalWrite(rstPin, HIGH);
218  /* Wait at least 1ms after VCC is up for LCD */
219  delay(1);
220  /* Perform reset operation of LCD display */
221  digitalWrite(rstPin, LOW);
222  delay(10);
223  digitalWrite(rstPin, HIGH);
224  }
225  ssd1306_spiInit(cesPin, dcPin);
227 }
void(* send)(uint8_t data)
void ssd1331_setMode(lcd_mode_t mode)
Sets GDRAM autoincrement mode.
Definition: oled_ssd1331.c:126
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(* send_pixels1)(uint8_t data)
Definition: lcd_common.h:128
void ssd1306_spiInit(int8_t cesPin, int8_t dcPin)
Definition: ssd1306_spi.c:37
ssd1306_lcd_t ssd1306_lcd
Definition: lcd_common.c:32
ssd1306_interface_t ssd1306_intf
lcduint_t height
Definition: lcd_common.h:97
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:144
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:212
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:195