SSD1306 OLED display driver  1.7.13
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 "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 void ssd1306_drawBufferFast8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
91 {
92  uint32_t count = w * h;
93  ssd1306_lcd.set_block(x, y, w);
94  while (count--)
95  {
96  ssd1306_lcd.send_pixels8( *data );
97  data++;
98  }
100 }
101 
102 void ssd1306_fillScreen8(uint8_t fill_Data)
103 {
104  ssd1306_lcd.set_block(0, 0, 0);
105  uint32_t count = (uint32_t)ssd1306_lcd.width * (uint32_t)ssd1306_lcd.height;
106  while (count--)
107  {
108  ssd1306_lcd.send_pixels8( fill_Data );
109  }
110  ssd1306_intf.stop();
111 }
112 
114 {
115  ssd1306_fillScreen8( 0x00 );
116 }
117 
118 void ssd1306_putPixel8(lcdint_t x, lcdint_t y)
119 {
120  ssd1306_lcd.set_block(x, y, 0);
121  ssd1306_lcd.send_pixels8( ssd1306_color );
122  ssd1306_intf.stop();
123 }
124 
125 void ssd1306_putColorPixel8(lcdint_t x, lcdint_t y, uint8_t color)
126 {
127  ssd1306_lcd.set_block(x, y, 0);
128  ssd1306_lcd.send_pixels8( color );
129  ssd1306_intf.stop();
130 }
131 
132 void ssd1306_drawVLine8(lcdint_t x1, lcdint_t y1, lcdint_t y2)
133 {
134  ssd1306_lcd.set_block(x1, y1, 1);
135  while (y1<=y2)
136  {
137  ssd1306_lcd.send_pixels8( ssd1306_color );
138  y1++;
139  }
140  ssd1306_intf.stop();
141 }
142 
143 void ssd1306_drawHLine8(lcdint_t x1, lcdint_t y1, lcdint_t x2)
144 {
145  ssd1306_lcd.set_block(x1, y1, 0);
146  while (x1 < x2)
147  {
148  ssd1306_lcd.send_pixels8( ssd1306_color );
149  x1++;
150  }
151  ssd1306_intf.stop();
152 }
153 
154 void ssd1306_drawLine8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
155 {
156  lcduint_t dx = x1 > x2 ? (x1 - x2): (x2 - x1);
157  lcduint_t dy = y1 > y2 ? (y1 - y2): (y2 - y1);
158  lcduint_t err = 0;
159  if (dy > dx)
160  {
161  if (y1 > y2)
162  {
163  ssd1306_swap_data(x1, x2, lcdint_t);
164  ssd1306_swap_data(y1, y2, lcdint_t);
165  }
166  for(; y1<=y2; y1++)
167  {
168  err += dx;
169  if (err >= dy)
170  {
171  err -= dy;
172  x1 < x2 ? x1++: x1--;
173  }
174  ssd1306_putPixel8( x1, y1 );
175  }
176  }
177  else
178  {
179  if (x1 > x2)
180  {
181  ssd1306_swap_data(x1, x2, lcdint_t);
182  ssd1306_swap_data(y1, y2, lcdint_t);
183  }
184  for(; x1<=x2; x1++)
185  {
186  err += dy;
187  if (err >= dx)
188  {
189  err -= dx;
190  if (y1 < y2) y1++; else y1--;
191  }
192  ssd1306_putPixel8( x1, y1 );
193  }
194  }
195 }
196 
197 void ssd1306_drawRect8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
198 {
199  ssd1306_drawHLine8(x1,y1,x2);
200  ssd1306_drawHLine8(x1,y2,x2);
201  ssd1306_drawVLine8(x1,y1,y2);
202  ssd1306_drawVLine8(x2,y1,y2);
203 }
204 
205 void ssd1306_fillRect8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
206 {
207  if (y1 > y2)
208  {
209  ssd1306_swap_data(y1, y2, lcdint_t);
210  }
211  if (x1 > x2)
212  {
213  ssd1306_swap_data(x1, x2, lcdint_t);
214  }
215  ssd1306_lcd.set_block(x1, y1, x2 - x1 + 1);
216  uint16_t count = (x2 - x1 + 1) * (y2 - y1 + 1);
217  while (count--)
218  {
219  ssd1306_lcd.send_pixels8( ssd1306_color );
220  }
221  ssd1306_intf.stop();
222 }
223 
224 void ssd1306_drawMonoBitmap8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
225 {
226  uint8_t bit = 1;
227  uint8_t blackColor = s_ssd1306_invertByte ? ssd1306_color : 0x00;
228  uint8_t color = s_ssd1306_invertByte ? 0x00 : ssd1306_color;
229  ssd1306_lcd.set_block(xpos, ypos, w);
230  while (h--)
231  {
232  lcduint_t wx = w;
233  while ( wx-- )
234  {
235  uint8_t data = pgm_read_byte( bitmap );
236  if ( data & bit )
237  ssd1306_lcd.send_pixels8( color );
238  else
239  ssd1306_lcd.send_pixels8( blackColor );
240  bitmap++;
241  }
242  bit <<= 1;
243  if ( bit == 0 )
244  {
245  bit = 1;
246  }
247  else
248  {
249  bitmap -= w;
250  }
251  }
252  ssd1306_intf.stop();
253 }
254 
255 void ssd1306_drawBitmap8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
256 {
257  ssd1306_lcd.set_block(xpos, ypos, w);
258  uint32_t count = (w) * (h);
259  while (count--)
260  {
261  ssd1306_lcd.send_pixels8( pgm_read_byte( bitmap ) );
262  bitmap++;
263  }
264  ssd1306_intf.stop();
265 }
266 
267 void ssd1306_clearBlock8(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
268 {
269  ssd1306_lcd.set_block(x, y, w);
270  uint32_t count = w * h;
271  while (count--)
272  {
273  ssd1306_lcd.send_pixels8( 0x00 );
274  }
275  ssd1306_intf.stop();
276 }
277 
278 void ssd1306_setCursor8(lcduint_t x, lcduint_t y)
279 {
280  ssd1306_cursorX = x;
281  ssd1306_cursorY = y;
282 }
283 
284 void ssd1306_printChar8(uint8_t c)
285 {
286  uint16_t unicode = ssd1306_unicode16FromUtf8(c);
287  if (unicode == SSD1306_MORE_CHARS_REQUIRED) return;
288  SCharInfo char_info;
289  ssd1306_getCharBitmap(unicode, &char_info);
290  ssd1306_drawMonoBitmap8(ssd1306_cursorX,
291  ssd1306_cursorY,
292  char_info.width,
293  char_info.height,
294  char_info.glyph );
295 }
296 
297 size_t ssd1306_write8(uint8_t ch)
298 {
299  if (ch == '\r')
300  {
301  ssd1306_cursorX = 0;
302  return 0;
303  }
304  else if ( (ssd1306_cursorX > ssd1306_lcd.width - s_fixedFont.h.width) || (ch == '\n') )
305  {
306  ssd1306_cursorX = 0;
307  ssd1306_cursorY += s_fixedFont.h.height;
308  if ( ssd1306_cursorY > ssd1306_lcd.height - s_fixedFont.h.height )
309  {
310  ssd1306_cursorY = 0;
311  }
312  ssd1306_clearBlock8(0, ssd1306_cursorY, ssd1306_lcd.width, s_fixedFont.h.height);
313  if (ch == '\n')
314  {
315  return 0;
316  }
317  }
318  uint16_t unicode = ssd1306_unicode16FromUtf8(ch);
319  if (unicode == SSD1306_MORE_CHARS_REQUIRED) return 0;
320  SCharInfo char_info;
321  ssd1306_getCharBitmap(unicode, &char_info);
322  ssd1306_drawMonoBitmap8( ssd1306_cursorX,
323  ssd1306_cursorY,
324  char_info.width,
325  char_info.height,
326  char_info.glyph);
327  ssd1306_cursorX += char_info.width + char_info.spacing;
328  return 1;
329 }
330 
331 size_t ssd1306_print8(const char ch[])
332 {
333  size_t n = 0;
334  while (*ch)
335  {
336  n += ssd1306_write8(*ch);
337  ch++;
338  }
339  return n;
340 }
341 
342 uint8_t ssd1306_printFixed8(lcdint_t x, lcdint_t y, const char *ch, EFontStyle style)
343 {
344  ssd1306_cursorX = x;
345  ssd1306_cursorY = y;
346  return ssd1306_print8(ch);
347 }
348 
349 
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:278
void ssd1306_drawRect8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
Definition: ssd1306_8bit.c:197
uint8_t ssd1306_printFixed8(lcdint_t x, lcdint_t y, const char *ch, EFontStyle style)
Definition: ssd1306_8bit.c:342
uint8_t height
char height in pixels
void ssd1306_drawVLine8(lcdint_t x1, lcdint_t y1, lcdint_t y2)
Definition: ssd1306_8bit.c:132
void ssd1306_printChar8(uint8_t c)
Definition: ssd1306_8bit.c:284
void ssd1306_setColor(uint16_t color)
Sets default color, generated by RGB_COLOR8 or RGB_COLOR16 macros.
Definition: ssd1306_8bit.c:44
uint8_t width
width in pixels
void ssd1306_clearBlock8(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
Definition: ssd1306_8bit.c:267
#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_putPixel8(lcdint_t x, lcdint_t y)
Definition: ssd1306_8bit.c:118
void ssd1306_putColorPixel8(lcdint_t x, lcdint_t y, uint8_t color)
Definition: ssd1306_8bit.c:125
ssd1306_lcd_t ssd1306_lcd
Definition: lcd_common.c:32
void ssd1306_drawHLine8(lcdint_t x1, lcdint_t y1, lcdint_t x2)
Definition: ssd1306_8bit.c:143
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:331
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:90
void ssd1306_drawLine8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
Definition: ssd1306_8bit.c:154
void ssd1306_clearScreen8(void)
Definition: ssd1306_8bit.c:113
void ssd1306_drawBitmap8(lcdint_t xpos, lcdint_t ypos, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
Definition: ssd1306_8bit.c:255
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:297
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:224
void ssd1306_fillRect8(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
Definition: ssd1306_8bit.c:205
#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:102