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