SSD1306 OLED display driver  1.8.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-2019, 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 "ssd1306_generic.h"
27 //#include "ssd1306_fonts.h"
28 #include "intf/ssd1306_interface.h"
29 #include "intf/spi/ssd1306_spi.h"
30 #include "ssd1306_hal/io.h"
31 
32 #include "lcd/ssd1331_commands.h"
33 #include "lcd/lcd_common.h"
34 
35 extern uint16_t ssd1306_color;
36 extern uint8_t s_ssd1306_invertByte;
37 extern lcduint_t ssd1306_cursorX;
38 extern lcduint_t ssd1306_cursorY;
40 #ifdef CONFIG_SSD1306_UNICODE_ENABLE
41 extern uint8_t g_ssd1306_unicode;
42 #endif
43 
44 void ssd1306_setColor(uint16_t color)
45 {
46  ssd1306_color = color;
47 }
48 
49 void ssd1306_setRgbColor(uint8_t r, uint8_t g, uint8_t b)
50 {
51  ssd1306_color = RGB_COLOR8(r,g,b);
52 }
53 
54 void ssd1306_setRgbColor8(uint8_t r, uint8_t g, uint8_t b)
55 {
56  ssd1306_color = RGB_COLOR8(r,g,b);
57 }
58 
59 void ssd1306_drawMonoBuffer8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
60 {
61  uint8_t bit = 1;
62  uint8_t blackColor = s_ssd1306_invertByte ? ssd1306_color : 0x00;
63  uint8_t color = s_ssd1306_invertByte ? 0x00 : ssd1306_color;
64  ssd1306_lcd.set_block(xpos, ypos, w);
65  while (h--)
66  {
67  lcduint_t wx = w;
68  while (wx--)
69  {
70  uint8_t data = *bitmap;
71  if ( data & bit )
72  ssd1306_lcd.send_pixels8( color );
73  else
74  ssd1306_lcd.send_pixels8( blackColor );
75  bitmap++;
76  }
77  bit <<= 1;
78  if (bit == 0)
79  {
80  bit = 1;
81  }
82  else
83  {
84  bitmap -= w;
85  }
86  }
88 }
89 
90 static void ssd1306_drawBufferPitch8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, lcduint_t pitch, const uint8_t *data)
91 {
92  ssd1306_lcd.set_block(x, y, w);
93  while (h--)
94  {
95  lcduint_t line = w;
96  while (line--)
97  {
98  ssd1306_lcd.send_pixels8( *data );
99  data++;
100  }
101  data += pitch - w;
102  }
103  ssd1306_intf.stop();
104 }
105 
106 void ssd1306_drawBufferFast8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
107 {
108  ssd1306_drawBufferPitch8( x, y, w, h, w, data );
109 }
110 
111 void ssd1306_drawBufferEx8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, lcduint_t pitch, const uint8_t *data)
112 {
113  ssd1306_drawBufferPitch8( x, y, w, h, pitch, data );
114 }
115 
116 void ssd1306_fillScreen8(uint8_t fill_Data)
117 {
118  ssd1306_lcd.set_block(0, 0, 0);
119  uint32_t count = (uint32_t)ssd1306_lcd.width * (uint32_t)ssd1306_lcd.height;
120  while (count--)
121  {
122  ssd1306_lcd.send_pixels8( fill_Data );
123  }
124  ssd1306_intf.stop();
125 }
126 
128 {
129  ssd1306_fillScreen8( 0x00 );
130 }
131 
132 void ssd1306_putPixel8(lcdint_t x, lcdint_t y)
133 {
134  ssd1306_lcd.set_block(x, y, 0);
135  ssd1306_lcd.send_pixels8( ssd1306_color );
136  ssd1306_intf.stop();
137 }
138 
139 void ssd1306_putColorPixel8(lcdint_t x, lcdint_t y, uint8_t color)
140 {
141  ssd1306_lcd.set_block(x, y, 0);
142  ssd1306_lcd.send_pixels8( color );
143  ssd1306_intf.stop();
144 }
145 
146 void ssd1306_drawVLine8(lcdint_t x1, lcdint_t y1, lcdint_t y2)
147 {
148  ssd1306_lcd.set_block(x1, y1, 1);
149  while (y1<=y2)
150  {
151  ssd1306_lcd.send_pixels8( ssd1306_color );
152  y1++;
153  }
154  ssd1306_intf.stop();
155 }
156 
157 void ssd1306_drawHLine8(lcdint_t x1, lcdint_t y1, lcdint_t x2)
158 {
159  ssd1306_lcd.set_block(x1, y1, 0);
160  while (x1 < x2)
161  {
162  ssd1306_lcd.send_pixels8( ssd1306_color );
163  x1++;
164  }
165  ssd1306_intf.stop();
166 }
167 
168 void ssd1306_drawLine8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
169 {
170  lcduint_t dx = x1 > x2 ? (x1 - x2): (x2 - x1);
171  lcduint_t dy = y1 > y2 ? (y1 - y2): (y2 - y1);
172  lcduint_t err = 0;
173  if (dy > dx)
174  {
175  if (y1 > y2)
176  {
177  ssd1306_swap_data(x1, x2, lcdint_t);
178  ssd1306_swap_data(y1, y2, lcdint_t);
179  }
180  for(; y1<=y2; y1++)
181  {
182  err += dx;
183  if (err >= dy)
184  {
185  err -= dy;
186  x1 < x2 ? x1++: x1--;
187  }
188  ssd1306_putPixel8( x1, y1 );
189  }
190  }
191  else
192  {
193  if (x1 > x2)
194  {
195  ssd1306_swap_data(x1, x2, lcdint_t);
196  ssd1306_swap_data(y1, y2, lcdint_t);
197  }
198  for(; x1<=x2; x1++)
199  {
200  err += dy;
201  if (err >= dx)
202  {
203  err -= dx;
204  if (y1 < y2) y1++; else y1--;
205  }
206  ssd1306_putPixel8( x1, y1 );
207  }
208  }
209 }
210 
211 void ssd1306_drawRect8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
212 {
213  ssd1306_drawHLine8(x1,y1,x2);
214  ssd1306_drawHLine8(x1,y2,x2);
215  ssd1306_drawVLine8(x1,y1,y2);
216  ssd1306_drawVLine8(x2,y1,y2);
217 }
218 
219 void ssd1306_fillRect8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
220 {
221  if (y1 > y2)
222  {
223  ssd1306_swap_data(y1, y2, lcdint_t);
224  }
225  if (x1 > x2)
226  {
227  ssd1306_swap_data(x1, x2, lcdint_t);
228  }
229  ssd1306_lcd.set_block(x1, y1, x2 - x1 + 1);
230  uint16_t count = (x2 - x1 + 1) * (y2 - y1 + 1);
231  while (count--)
232  {
233  ssd1306_lcd.send_pixels8( ssd1306_color );
234  }
235  ssd1306_intf.stop();
236 }
237 
238 void ssd1306_drawMonoBitmap8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
239 {
240  uint8_t bit = 1;
241  uint8_t blackColor = s_ssd1306_invertByte ? ssd1306_color : 0x00;
242  uint8_t color = s_ssd1306_invertByte ? 0x00 : ssd1306_color;
243  ssd1306_lcd.set_block(xpos, ypos, w);
244  while (h--)
245  {
246  lcduint_t wx = w;
247  while ( wx-- )
248  {
249  uint8_t data = pgm_read_byte( bitmap );
250  if ( data & bit )
251  ssd1306_lcd.send_pixels8( color );
252  else
253  ssd1306_lcd.send_pixels8( blackColor );
254  bitmap++;
255  }
256  bit <<= 1;
257  if ( bit == 0 )
258  {
259  bit = 1;
260  }
261  else
262  {
263  bitmap -= w;
264  }
265  }
266  ssd1306_intf.stop();
267 }
268 
269 void ssd1306_drawBitmap8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
270 {
271  ssd1306_lcd.set_block(xpos, ypos, w);
272  uint32_t count = (w) * (h);
273  while (count--)
274  {
275  ssd1306_lcd.send_pixels8( pgm_read_byte( bitmap ) );
276  bitmap++;
277  }
278  ssd1306_intf.stop();
279 }
280 
281 void ssd1306_clearBlock8(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
282 {
283  ssd1306_lcd.set_block(x, y, w);
284  uint32_t count = w * h;
285  while (count--)
286  {
287  ssd1306_lcd.send_pixels8( 0x00 );
288  }
289  ssd1306_intf.stop();
290 }
291 
292 void ssd1306_setCursor8(lcduint_t x, lcduint_t y)
293 {
294  ssd1306_cursorX = x;
295  ssd1306_cursorY = y;
296 }
297 
298 void ssd1306_printChar8(uint8_t c)
299 {
300  uint16_t unicode = ssd1306_unicode16FromUtf8(c);
301  if (unicode == SSD1306_MORE_CHARS_REQUIRED) return;
302  SCharInfo char_info;
303  ssd1306_getCharBitmap(unicode, &char_info);
304  ssd1306_drawMonoBitmap8(ssd1306_cursorX,
305  ssd1306_cursorY,
306  char_info.width,
307  char_info.height,
308  char_info.glyph );
309 }
310 
311 size_t ssd1306_write8(uint8_t ch)
312 {
313  if (ch == '\r')
314  {
315  ssd1306_cursorX = 0;
316  return 0;
317  }
318  SCharInfo char_info;
319  uint8_t gotoNewLine = 1;
320  if (ch != '\n')
321  {
322  uint16_t unicode;
323  unicode = ssd1306_unicode16FromUtf8(ch);
324  if (unicode == SSD1306_MORE_CHARS_REQUIRED) return 0;
325  ssd1306_getCharBitmap(unicode, &char_info);
326  gotoNewLine = (ssd1306_cursorX > (ssd1306_lcd.width - char_info.width));
327  }
328  if ( gotoNewLine )
329  {
330  ssd1306_cursorX = 0;
331  ssd1306_cursorY += s_fixedFont.h.height;
332  if ( ssd1306_cursorY > ssd1306_lcd.height - s_fixedFont.h.height )
333  {
334  ssd1306_cursorY = 0;
335  }
336  ssd1306_clearBlock8(0, ssd1306_cursorY, ssd1306_lcd.width, s_fixedFont.h.height);
337  if (ch == '\n')
338  {
339  return 0;
340  }
341  }
342  if ( 1 )
343  {
344  uint8_t color = ssd1306_color;
345  ssd1306_color = s_ssd1306_invertByte ? color : 0x00;
346  ssd1306_fillRect8( ssd1306_cursorX, ssd1306_cursorY,
347  ssd1306_cursorX + char_info.width + char_info.spacing - 1,
348  ssd1306_cursorY + s_fixedFont.h.height - 1 );
349  ssd1306_color = color;
350  }
351  ssd1306_drawMonoBitmap8( ssd1306_cursorX,
352  ssd1306_cursorY,
353  char_info.width,
354  char_info.height,
355  char_info.glyph);
356  ssd1306_cursorX += char_info.width + char_info.spacing;
357  return 1;
358 }
359 
360 size_t ssd1306_print8(const char ch[])
361 {
362  size_t n = 0;
363  while (*ch)
364  {
365  n += ssd1306_write8(*ch);
366  ch++;
367  }
368  return n;
369 }
370 
371 uint8_t ssd1306_printFixed8(lcdint_t x, lcdint_t y, const char *ch, EFontStyle style)
372 {
373  ssd1306_cursorX = x;
374  ssd1306_cursorY = y;
375  return ssd1306_print8(ch);
376 }
377 
378 
void ssd1306_drawMonoBuffer8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
Definition: ssd1306_8bit.c:59
void ssd1306_setCursor8(lcduint_t x, lcduint_t y)
Definition: ssd1306_8bit.c:292
void ssd1306_drawRect8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
Definition: ssd1306_8bit.c:211
uint8_t ssd1306_printFixed8(lcdint_t x, lcdint_t y, const char *ch, EFontStyle style)
Definition: ssd1306_8bit.c:371
uint8_t height
char height in pixels
void ssd1306_drawVLine8(lcdint_t x1, lcdint_t y1, lcdint_t y2)
Definition: ssd1306_8bit.c:146
void ssd1306_printChar8(uint8_t c)
Definition: ssd1306_8bit.c:298
void ssd1306_setColor(uint16_t color)
Sets default color, generated by RGB_COLOR8 or RGB_COLOR16 macros.
Definition: ssd1306_8bit.c:44
void ssd1306_drawLine8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
Definition: ssd1306_8bit.c:168
void ssd1306_clearBlock8(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
Definition: ssd1306_8bit.c:281
#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(* 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_setRgbColor(uint8_t r, uint8_t g, uint8_t b)
Sets default color.
Definition: ssd1306_8bit.c:49
uint8_t height
height in pixels
void ssd1306_drawBufferEx8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, lcduint_t pitch, const uint8_t *data)
Definition: ssd1306_8bit.c:111
void ssd1306_putPixel8(lcdint_t x, lcdint_t y)
Definition: ssd1306_8bit.c:132
void ssd1306_putColorPixel8(lcdint_t x, lcdint_t y, uint8_t color)
Definition: ssd1306_8bit.c:139
ssd1306_lcd_t ssd1306_lcd
Definition: lcd_common.c:33
void ssd1306_drawHLine8(lcdint_t x1, lcdint_t y1, lcdint_t x2)
Definition: ssd1306_8bit.c:157
void ssd1306_setRgbColor8(uint8_t r, uint8_t g, uint8_t b)
Sets default color.
Definition: ssd1306_8bit.c:54
ssd1306_interface_t ssd1306_intf
lcduint_t height
Definition: lcd_common.h:97
uint8_t width
char width in pixels
size_t ssd1306_print8(const char ch[])
Prints null-terminated string to display at current cursor position.
Definition: ssd1306_8bit.c:360
SFixedFontInfo s_fixedFont
Definition: tiler.h:44
SFontHeaderRecord h
record, containing information on font
const uint8_t * glyph
char data, located in progmem.
void ssd1306_drawBufferFast8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
Definition: ssd1306_8bit.c:106
void ssd1306_clearScreen8(void)
Definition: ssd1306_8bit.c:127
void ssd1306_drawBitmap8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
Definition: ssd1306_8bit.c:269
uint8_t spacing
additional spaces after char in pixels
size_t ssd1306_write8(uint8_t ch)
Prints single character to display at current cursor position.
Definition: ssd1306_8bit.c:311
void ssd1306_getCharBitmap(uint16_t unicode, SCharInfo *info)
returns char data for currently set (active) font.
lcduint_t width
Definition: lcd_common.h:94
void ssd1306_drawMonoBitmap8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
Definition: ssd1306_8bit.c:238
void ssd1306_fillRect8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
Definition: ssd1306_8bit.c:219
#define SSD1306_MORE_CHARS_REQUIRED
EFontStyle
#define ssd1306_swap_data(a, b, type)
Definition: io.h:69
void ssd1306_fillScreen8(uint8_t fill_Data)
Definition: ssd1306_8bit.c:116