SSD1306 OLED display driver  1.7.2
This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
ssd1306_8bit.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 "ssd1306_8bit.h"
26 #include "intf/ssd1306_interface.h"
27 #include "intf/spi/ssd1306_spi.h"
28 #include "ssd1306_hal/io.h"
29 
30 #include "lcd/ssd1331_commands.h"
31 #include "lcd/lcd_common.h"
32 
33 extern uint16_t ssd1306_color;
34 extern uint8_t s_ssd1306_invertByte;
35 extern lcduint_t ssd1306_cursorX;
36 extern lcduint_t ssd1306_cursorY;
38 
39 void ssd1331_setColor(uint16_t color)
40 {
41  ssd1306_color = color;
42 }
43 
44 void ssd1331_setRgbColor(uint8_t r, uint8_t g, uint8_t b)
45 {
46  ssd1306_color = RGB_COLOR8(r,g,b);
47 }
48 
49 void ssd1331_drawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint16_t color)
50 {
53  ssd1306_intf.send(SSD1331_DRAWLINE);
54  ssd1306_intf.send(x1);
55  ssd1306_intf.send(y1);
56  ssd1306_intf.send(x2);
57  ssd1306_intf.send(y2);
58  ssd1306_intf.send( (color & 0x03) << 4 );
59  ssd1306_intf.send( (color & 0x1C) << 2 );
60  ssd1306_intf.send( (color & 0xE0) >> 2 );
62 }
63 
64 void ssd1331_copyBlock(uint8_t left, uint8_t top, uint8_t right, uint8_t bottom, uint8_t newLeft, uint8_t newTop)
65 {
68  ssd1306_intf.send(0x23);
69  ssd1306_intf.send(left);
70  ssd1306_intf.send(top);
71  ssd1306_intf.send(right);
72  ssd1306_intf.send(bottom);
73  ssd1306_intf.send(newLeft);
74  ssd1306_intf.send(newTop);
76 }
77 
78 void ssd1331_drawMonoBuffer8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
79 {
80  uint8_t bit = 1;
81  uint8_t blackColor = s_ssd1306_invertByte ? ssd1306_color : 0x00;
82  uint8_t color = s_ssd1306_invertByte ? 0x00 : ssd1306_color;
83  ssd1306_lcd.set_block(xpos, ypos, w);
84  while (h--)
85  {
86  uint8_t wx = w;
87  while (wx--)
88  {
89  uint8_t data = *bitmap;
90  if ( data & bit )
91  ssd1306_lcd.send_pixels8( color );
92  else
93  ssd1306_lcd.send_pixels8( blackColor );
94  bitmap++;
95  }
96  bit <<= 1;
97  if (bit == 0)
98  {
99  bit = 1;
100  }
101  else
102  {
103  bitmap -= w;
104  }
105  }
106  ssd1306_intf.stop();
107 }
108 
109 void ssd1331_drawBufferFast8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
110 {
111  uint16_t count = w * h;
112  ssd1306_lcd.set_block(x, y, w);
113  while (count--)
114  {
115  ssd1306_lcd.send_pixels8( *data );
116  data++;
117  }
118  ssd1306_intf.stop();
119 }
120 
121 void ssd1331_drawBufferFast16(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
122 {
123  uint16_t count = (w * h) << 1;
124  ssd1306_lcd.set_block(x, y, w);
125  while (count--)
126  {
127  ssd1306_intf.send( *data );
128  data++;
129  }
130  ssd1306_intf.stop();
131 }
132 
133 void ssd1331_fillScreen8(uint8_t fill_Data)
134 {
135  ssd1306_lcd.set_block(0, 0, 0);
136  uint16_t count = ssd1306_lcd.width * ssd1306_lcd.height;
137  while (count--)
138  {
139  ssd1306_lcd.send_pixels8( fill_Data );
140  }
141  ssd1306_intf.stop();
142 }
143 
145 {
146  ssd1331_fillScreen8( 0x00 );
147 }
148 
149 void ssd1331_putPixel8(lcdint_t x, lcdint_t y)
150 {
151  ssd1306_lcd.set_block(x, y, 0);
152  ssd1306_lcd.send_pixels8( ssd1306_color );
153  ssd1306_intf.stop();
154 }
155 
156 void ssd1331_drawVLine8(lcdint_t x1, lcdint_t y1, lcdint_t y2)
157 {
158  ssd1306_lcd.set_block(x1, y1, 1);
159  while (y1<=y2)
160  {
161  ssd1306_lcd.send_pixels8( ssd1306_color );
162  y1++;
163  }
164  ssd1306_intf.stop();
165 }
166 
167 void ssd1331_drawHLine8(lcdint_t x1, lcdint_t y1, lcdint_t x2)
168 {
169  ssd1306_lcd.set_block(x1, y1, 0);
170  while (x1 < x2)
171  {
172  ssd1306_lcd.send_pixels8( ssd1306_color );
173  x1++;
174  }
175  ssd1306_intf.stop();
176 }
177 
178 void ssd1331_drawLine8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
179 {
180  lcduint_t dx = x1 > x2 ? (x1 - x2): (x2 - x1);
181  lcduint_t dy = y1 > y2 ? (y1 - y2): (y2 - y1);
182  lcduint_t err = 0;
183  if (dy > dx)
184  {
185  if (y1 > y2)
186  {
187  ssd1306_swap_data(x1, x2, lcdint_t);
188  ssd1306_swap_data(y1, y2, lcdint_t);
189  }
190  for(; y1<=y2; y1++)
191  {
192  err += dx;
193  if (err >= dy)
194  {
195  err -= dy;
196  x1 < x2 ? x1++: x1--;
197  }
198  ssd1331_putPixel8( x1, y1 );
199  }
200  }
201  else
202  {
203  if (x1 > x2)
204  {
205  ssd1306_swap_data(x1, x2, lcdint_t);
206  ssd1306_swap_data(y1, y2, lcdint_t);
207  }
208  for(; x1<=x2; x1++)
209  {
210  err += dy;
211  if (err >= dx)
212  {
213  err -= dx;
214  if (y1 < y2) y1++; else y1--;
215  }
216  ssd1331_putPixel8( x1, y1 );
217  }
218  }
219 }
220 
221 void ssd1331_drawRect8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
222 {
223  ssd1331_drawHLine8(x1,y1,x2);
224  ssd1331_drawHLine8(x1,y2,x2);
225  ssd1331_drawVLine8(x1,y1,y2);
226  ssd1331_drawVLine8(x2,y1,y2);
227 }
228 
229 void ssd1331_fillRect8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
230 {
231  if (y1 > y2)
232  {
233  ssd1306_swap_data(y1, y2, lcdint_t);
234  }
235  if (x1 > x2)
236  {
237  ssd1306_swap_data(x1, x2, lcdint_t);
238  }
239  ssd1306_lcd.set_block(x1, y1, x2 - x1 + 1);
240  uint16_t count = (x2 - x1 + 1) * (y2 - y1 + 1);
241  while (count--)
242  {
243  ssd1306_lcd.send_pixels8( ssd1306_color );
244  }
245  ssd1306_intf.stop();
246 }
247 
248 void ssd1331_drawMonoBitmap8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
249 {
250  uint8_t bit = 1;
251  uint8_t blackColor = s_ssd1306_invertByte ? ssd1306_color : 0x00;
252  uint8_t color = s_ssd1306_invertByte ? 0x00 : ssd1306_color;
253  ssd1306_lcd.set_block(xpos, ypos, w);
254  while (h--)
255  {
256  uint8_t wx = w;
257  while ( wx-- )
258  {
259  uint8_t data = pgm_read_byte( bitmap );
260  if ( data & bit )
261  ssd1306_lcd.send_pixels8( color );
262  else
263  ssd1306_lcd.send_pixels8( blackColor );
264  bitmap++;
265  }
266  bit <<= 1;
267  if ( bit == 0 )
268  {
269  bit = 1;
270  }
271  else
272  {
273  bitmap -= w;
274  }
275  }
276  ssd1306_intf.stop();
277 }
278 
279 void ssd1331_drawBitmap8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
280 {
281  ssd1306_lcd.set_block(xpos, ypos, w);
282  uint16_t count = (w) * (h);
283  while (count--)
284  {
285  ssd1306_lcd.send_pixels8( pgm_read_byte( bitmap ) );
286  bitmap++;
287  }
288  ssd1306_intf.stop();
289 }
290 
291 void ssd1331_clearBlock8(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
292 {
293  ssd1306_lcd.set_block(x, y, w);
294  uint16_t count = w * h;
295  while (count--)
296  {
297  ssd1306_lcd.send_pixels8( 0x00 );
298  }
299  ssd1306_intf.stop();
300 }
301 
302 void ssd1331_setCursor8(lcduint_t x, lcduint_t y)
303 {
304  ssd1306_cursorX = x;
305  ssd1306_cursorY = y;
306 }
307 
308 void ssd1331_printChar8(uint8_t c)
309 {
311  ssd1331_drawMonoBitmap8(ssd1306_cursorX,
312  ssd1306_cursorY,
316 }
317 
318 size_t ssd1331_write8(uint8_t ch)
319 {
320  if (ch == '\r')
321  {
322  ssd1306_cursorX = 0;
323  return 0;
324  }
325  else if ( (ssd1306_cursorX > ssd1306_lcd.width - s_fixedFont.width) || (ch == '\n') )
326  {
327  ssd1306_cursorX = 0;
328  ssd1306_cursorY += s_fixedFont.height;
329  if ( ssd1306_cursorY > ssd1306_lcd.height - s_fixedFont.height )
330  {
331  ssd1306_cursorY = 0;
332  }
334  if (ch == '\n')
335  {
336  return 0;
337  }
338  }
340  ssd1331_drawMonoBitmap8( ssd1306_cursorX,
341  ssd1306_cursorY,
345  ssd1306_cursorX += s_fixedFont.width;
346  return 1;
347 }
348 
349 size_t ssd1331_print8(const char ch[])
350 {
351  size_t n = 0;
352  while (*ch)
353  {
354  n += ssd1331_write8(*ch);
355  ch++;
356  }
357  return n;
358 }
359 
360 uint8_t ssd1331_printFixed8(lcdint_t x, lcdint_t y, const char *ch, EFontStyle style)
361 {
362  ssd1306_cursorX = x;
363  ssd1306_cursorY = y;
364  return ssd1331_print8(ch);
365 }
366 
367 
void ssd1331_setCursor8(lcduint_t x, lcduint_t y)
Definition: ssd1306_8bit.c:302
void ssd1331_drawLine8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
Definition: ssd1306_8bit.c:178
void(* send)(uint8_t data)
void ssd1331_fillRect8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
Definition: ssd1306_8bit.c:229
void ssd1331_setRgbColor(uint8_t r, uint8_t g, uint8_t b)
Sets default color.
Definition: ssd1306_8bit.c:44
void ssd1331_clearScreen8(void)
Definition: ssd1306_8bit.c:144
#define RGB_COLOR8(r, g, b)
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 ssd1331_drawHLine8(lcdint_t x1, lcdint_t y1, lcdint_t x2)
Definition: ssd1306_8bit.c:167
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
void ssd1331_drawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint16_t color)
Definition: ssd1306_8bit.c:49
void ssd1331_drawBufferFast16(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
Definition: ssd1306_8bit.c:121
void ssd1331_printChar8(uint8_t c)
Definition: ssd1306_8bit.c:308
uint8_t ascii_offset
ascii offset
size_t ssd1331_write8(uint8_t ch)
Prints single character to display at current cursor position.
Definition: ssd1306_8bit.c:318
void ssd1331_clearBlock8(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
Definition: ssd1306_8bit.c:291
ssd1306_lcd_t ssd1306_lcd
Definition: lcd_common.c:32
void ssd1331_setColor(uint16_t color)
Sets default color, generated by RGB_COLOR8 or RGB_COLOR16 macros.
Definition: ssd1306_8bit.c:39
ssd1306_interface_t ssd1306_intf
lcduint_t height
Definition: lcd_common.h:97
uint8_t width
width in pixels
void ssd1331_drawBufferFast8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
Definition: ssd1306_8bit.c:109
void ssd1331_drawRect8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
Definition: ssd1306_8bit.c:221
SFixedFontInfo s_fixedFont
Definition: tiler.h:44
void ssd1331_drawVLine8(lcdint_t x1, lcdint_t y1, lcdint_t y2)
Definition: ssd1306_8bit.c:156
void ssd1331_fillScreen8(uint8_t fill_Data)
Definition: ssd1306_8bit.c:133
size_t ssd1331_print8(const char ch[])
Prints null-terminated string to display at current cursor position.
Definition: ssd1306_8bit.c:349
void ssd1331_putPixel8(lcdint_t x, lcdint_t y)
Definition: ssd1306_8bit.c:149
lcduint_t width
Definition: lcd_common.h:94
void ssd1331_drawBitmap8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
Definition: ssd1306_8bit.c:279
void ssd1331_copyBlock(uint8_t left, uint8_t top, uint8_t right, uint8_t bottom, uint8_t newLeft, uint8_t newTop)
Definition: ssd1306_8bit.c:64
const uint8_t * data
font chars bits
EFontStyle
uint8_t pages
height in pages (each page height is 8-pixels)
uint8_t ssd1331_printFixed8(lcdint_t x, lcdint_t y, const char *ch, EFontStyle style)
Definition: ssd1306_8bit.c:360
#define ssd1306_swap_data(a, b, type)
Definition: io.h:69
void ssd1331_drawMonoBuffer8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
Definition: ssd1306_8bit.c:78
uint8_t height
height in pixels
void ssd1331_drawMonoBitmap8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
Definition: ssd1306_8bit.c:248