SSD1306 OLED display driver  1.4.12
This library is developed to control SSD1306/SSD1331 RGB i2c/spi OLED displays and spi PCD8544 LED display
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 "spi/ssd1306_spi.h"
30 #include "hal/io.h"
31 
32 extern uint16_t ssd1306_color;
33 
34 static const PROGMEM uint8_t s_oled128x128_initData[] =
35 {
36  SSD1351_UNLOCK, 0xFF, 0x12,
37  SSD1351_UNLOCK, 0xFF, 0xB1,
38  SSD1351_SLEEP_ON,
39  SSD1351_CLOCKDIV, 0xFF, 0xF1, // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
40  SSD1351_SETMULTIPLEX, 0xFF, 127, // Reset to default MUX. See datasheet
41  SSD1351_SEGREMAP, 0xFF, 0B00110101, // 16-bit rgb color mode
42  SSD1351_SETSTARTLINE, 0xFF, 0x00, // First line to start scanning from
43  SSD1351_SETDISPLAYOFFSET, 0xFF, 0x00, // Set display offset
44  SSD1351_SETGPIO, 0xFF, 0x00, // GPIO OFF
45  SSD1351_SETFUNCTION, 0xFF, 0x01,
46  SSD1351_SETPRECHARGE, 0xFF, 0x32, // Phase 1 and Phase 2 periods
47  SSD1351_VCOMH, 0xFF, 0x05, //
48  SSD1351_PRECHARGELEVEL, 0xFF, 0x17,
49  SSD1351_NORMALDISPLAY,
50  SSD1351_CONTRAST, 0xFF, 0xC8, // RED
51  0xFF, 0x80, // GREEN
52  0xFF, 0xC8, // BLUE
53  SSD1351_MASTERCURRENT, 0xFF, 0x0F, //
54  SSD1351_EXTVSL, 0xFF, 0xA0, 0xFF, 0xB5, 0xFF, 0x55,
55  SSD1351_PRECHARGESECOND, 0xFF, 0x01, //
56  SSD1351_SLEEP_OFF, // Disable power-safe mode
57  SSD1351_NORMALDISPLAY,
58 };
59 
60 static uint8_t s_column;
61 static uint8_t s_page;
62 
63 static void ssd1351_setBlock(uint8_t x, uint8_t y, uint8_t w)
64 {
65  uint8_t rx = w ? (x + w - 1) : (s_displayWidth - 1);
66  s_column = x;
67  s_page = y;
69  ssd1306_sendByte(SSD1351_COLUMNADDR);
74  ssd1306_sendByte(SSD1351_ROWADDR);
76  ssd1306_sendByte(y<<3);
77  ssd1306_sendByte(((y<<3) + 7) < s_displayHeight ? ((y<<3) + 7) : (s_displayHeight - 1));
79  ssd1306_sendByte(0x5C);
81 }
82 
83 static void ssd1351_nextPage(void)
84 {
86  ssd1351_setBlock(s_column,s_page+1,0);
87 }
88 
89 static void ssd1351_sendPixels(uint8_t data)
90 {
91  for (uint8_t i=8; i>0; i--)
92  {
93  if ( data & 0x01 )
94  {
95  ssd1306_sendByte( (uint8_t)(ssd1306_color>>8) );
96  ssd1306_sendByte( (uint8_t)(ssd1306_color) );
97  }
98  else
99  {
100  ssd1306_sendByte( 0B00000000 );
101  ssd1306_sendByte( 0B00000000 );
102  }
103  data >>= 1;
104  }
105 }
106 
108 {
110  s_displayHeight = 128;
111  s_displayWidth = 128;
112  ssd1306_setRamBlock = ssd1351_setBlock;
113  ssd1306_nextRamPage = ssd1351_nextPage;
114  ssd1306_sendPixels = ssd1351_sendPixels;
116  for( uint8_t i=0; i<sizeof(s_oled128x128_initData); i++)
117  {
118  uint8_t data = pgm_read_byte(&s_oled128x128_initData[i]);
119  if (data == 0xFF)
120  {
121  data = pgm_read_byte(&s_oled128x128_initData[++i]);
123  ssd1306_sendByte(data);
125  }
126  else
127  {
128  ssd1306_sendByte(data);
129  }
130  }
132 }
133 
134 void ssd1351_128x128_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
135 {
136  if (rstPin >=0)
137  {
138  pinMode(rstPin, OUTPUT);
139  digitalWrite(rstPin, HIGH);
140  /* Wait at least 1ms after VCC is up for LCD */
141  delay(1);
142  /* Perform reset operation of LCD display */
143  digitalWrite(rstPin, LOW);
144  delay(20);
145  digitalWrite(rstPin, HIGH);
146  }
147  ssd1306_spiInit(cesPin, dcPin);
149 }
void(* ssd1306_sendByte)(uint8_t data)
void ssd1351_128x128_init()
Inits 128x128 RGB OLED display (based on SSD1351 controller).
Definition: oled_ssd1351.c:107
void(* ssd1306_endTransmission)(void)
void ssd1306_spiDataMode(uint8_t mode)
Definition: ssd1306_spi.c:64
uint8_t g_lcd_type
Definition: ssd1306.c:41
uint8_t s_displayHeight
Definition: ssd1306.c:38
void(* ssd1306_commandStart)(void)
void(* ssd1306_setRamBlock)(uint8_t x, uint8_t y, uint8_t w)
void(* ssd1306_nextRamPage)(void)
void(* ssd1306_sendPixels)(uint8_t data)
void ssd1306_spiInit(int8_t cesPin, int8_t dcPin)
Definition: ssd1306_spi.c:38
uint8_t s_displayWidth
Definition: ssd1306.c:39
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:134