SSD1306 OLED display driver  1.8.2
This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
oled_template.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_template.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_WxH_initData[] =
37 {
38 #ifdef SDL_EMULATION
39  SDL_LCD_TEMPLATE,
40  0x00,
41 #endif
42  // Specify display initialization commands here
43  // The commands should be in format, required by
44  // ssd1306_configureI2cDisplay() or ssd1306_configureSpiDisplay()
45  // functions. Refer to their documenation.
46  0x00, 0x00,
47 };
48 
51 
52 static uint8_t s_column;
53 static uint8_t s_page;
54 
55 // The function must set block to draw data
56 static void template_setBlock_compat(lcduint_t x, lcduint_t y, lcduint_t w)
57 {
58  uint8_t rx = w ? (x + w - 1) : (ssd1306_lcd.width - 1);
59  s_column = x;
60  s_page = y;
63  ssd1306_intf.send(0x00); // Send column addr command to controller
64  ssd1306_intf.send(x);
66  ssd1306_intf.send(0x00); // Replace with row addr command to controller
67  ssd1306_intf.send(y<<3);
68  ssd1306_intf.send(((y<<3) + 7) < ssd1306_lcd.height ? ((y<<3) + 7) : (ssd1306_lcd.height - 1));
70 }
71 
72 static void template_nextPage_compat(void)
73 {
75  template_setBlock_compat(s_column,s_page+1,0);
76 }
77 
78 static void template_sendPixels(uint8_t data)
79 {
80  for (uint8_t i=8; i>0; i--)
81  {
82  if ( data & 0x01 )
83  {
84  ssd1306_intf.send( (uint8_t)ssd1306_color );
85  }
86  else
87  {
88  ssd1306_intf.send( 0B00000000 );
89  }
90  data >>= 1;
91  }
92 }
93 
94 static void template_sendPixelsBuffer(const uint8_t *buffer, uint16_t len)
95 {
96  while(len--)
97  {
98  template_sendPixels(*buffer);
99  buffer++;
100  }
101 }
102 
105 
106 static void template_setBlock(lcduint_t x, lcduint_t y, lcduint_t w)
107 {
108  uint8_t rx = w ? (x + w - 1) : (ssd1306_lcd.width - 1);
111  ssd1306_intf.send(0x00); // Send column address command here
112  ssd1306_intf.send(x);
113  ssd1306_intf.send(rx < ssd1306_lcd.width ? rx : (ssd1306_lcd.width - 1));
114  ssd1306_intf.send(0x00); // Send row address command here
115  ssd1306_intf.send(y);
118 }
119 
120 static void template_nextPage(void)
121 {
122 }
123 
125 {
126  if (mode == LCD_MODE_NORMAL)
127  {
128  ssd1306_lcd.set_block = template_setBlock;
129  ssd1306_lcd.next_page = template_nextPage;
130  }
131  else if (mode == LCD_MODE_SSD1306_COMPAT)
132  {
133  ssd1306_lcd.set_block = template_setBlock_compat;
134  ssd1306_lcd.next_page = template_nextPage_compat;
135  }
136  // Send command to update controller
137 // ssd1306_intf.start();
138 // ssd1306_spiDataMode(0);
139 // ssd1306_intf.send( SSD1331_SEGREMAP );
140 // ssd1306_intf.stop();
141  return;
142 }
143 
145 {
147  ssd1306_lcd.width = 96; // specify width
148  ssd1306_lcd.height = 64; // specify height
149  // Set functions for compatible mode
150  ssd1306_lcd.set_block = template_setBlock_compat;
151  ssd1306_lcd.next_page = template_nextPage_compat;
152  ssd1306_lcd.send_pixels1 = template_sendPixels;
153  ssd1306_lcd.send_pixels_buffer1 = template_sendPixelsBuffer;
154  // Set function for 8-bit mode
157  // Use one of 2 functions for initialization below
158  // Please, read help on this functions and read datasheet before you decide, which
159  // one needs to be used. For example, ssd1331 is OK with ssd1306_configureI2cDisplay(),
160  // while st7735 can be initialized only with ssd1306_configureSpiDisplay().
161  ssd1306_configureI2cDisplay(s_oled_WxH_initData, sizeof(s_oled_WxH_initData));
162 // ssd1306_configureSpiDisplay(s_oled_WxH_initData, sizeof(s_oled_WxH_initData));
163 }
164 
165 void template_WxH_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
166 {
167  if (rstPin >=0)
168  {
169  ssd1306_resetController( rstPin, 10 );
170  }
171  ssd1306_spiInit(cesPin, dcPin);
173 }
void template_setMode(lcd_mode_t mode)
Sets GDRAM autoincrement mode.
void(* send)(uint8_t data)
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
void template_WxH_init(void)
Inits WxH TEMPLATE OLED display (based on TEMPLATE controller).
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:139
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:33
void template_WxH_spi_init(int8_t rstPin, int8_t cesPin, int8_t dcPin)
Inits WxH TEMPLATE OLED display over spi (based on TEMPLATE controller).
ssd1306_interface_t ssd1306_intf
lcduint_t height
Definition: lcd_common.h:97
void(* next_page)(void)
Definition: lcd_common.h:122
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:164