SSD1306 OLED display driver  1.7.1
This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
tiler.h
Go to the documentation of this file.
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 */
29 #ifndef _NANO_ENGINE_TILER_H_
30 #define _NANO_ENGINE_TILER_H_
31 
32 #include "canvas.h"
33 #include "lcd/lcd_common.h"
34 
40 
41 /* The table below defines arguments for NanoEngineTiler. *
42  * canvas width height bits */
43 #define BUFFER_128x64_MONO NanoCanvas1, 128, 64, 7
44 #define TILE_8x8_RGB16 NanoCanvas16, 8, 8, 3
45 #define TILE_8x8_RGB8 NanoCanvas8, 8, 8, 3
46 #define TILE_8x8_MONO NanoCanvas1, 8, 8, 3
47 #define TILE_8x8_MONO_8 NanoCanvas1_8,8, 8, 3
48 
49 #define ADATILE_8x8_MONO AdafruitCanvas1, 8, 8, 3
50 #define ADATILE_8x8_RGB8 AdafruitCanvas8, 8, 8, 3
51 #define ADATILE_8x8_RGB16 AdafruitCanvas16, 8, 8, 3
52 
53 
54 typedef bool (*TNanoEngineOnDraw)(void);
55 
65 template<class C, uint8_t W, uint8_t H, uint8_t B>
67 {
68 protected:
71  {
72  refresh();
73  };
74 
75 public:
77  static const uint8_t NE_TILE_SIZE_BITS = B;
79  static const uint8_t NE_TILE_WIDTH = W;
81  static const uint8_t NE_TILE_HEIGHT = H;
83  static const uint8_t NE_MAX_TILES_NUM = 16 >> (B - 3);
84 
86  static C canvas;
87 
91  static void refresh()
92  {
93  memset(m_refreshFlags,0xFF,sizeof(uint16_t) * NanoEngineTiler<C,W,H,B>::NE_MAX_TILES_NUM);
94  }
95 
100  static void refresh(const NanoRect &rect)
101  {
102  refresh(rect.p1.x, rect.p1.y, rect.p2.x, rect.p2.y);
103  }
104 
109  static void refresh(const NanoPoint &point)
110  {
111  if ((point.y<0) || ((point.y>>B)>=NE_MAX_TILES_NUM)) return;
112  m_refreshFlags[(point.y>>B)] |= (1<<(point.x>>B));
113  }
114 
119  static void refresh(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
120  {
121  if (y2 < 0) return;
122  if (y1 < 0) y1 = 0;
123  if (x1 < 0) x1 = 0;
124  y1 = y1>>B;
125  y2 = min((y2>>B), NE_MAX_TILES_NUM - 1);
126  for (uint8_t y=y1; y<=y2; y++)
127  {
128  for(uint8_t x=x1>>B; x<=(x2>>B); x++)
129  {
130  m_refreshFlags[y] |= (1<<x);
131  }
132  }
133  }
134 
143  static void drawCallback(TNanoEngineOnDraw callback) { m_onDraw = callback; };
144 
152  static bool collision(NanoPoint &p, NanoRect &rect) { return rect.collision( p ); }
153 
154 protected:
160 
163 
170  static void displayBuffer();
171 
177  static void displayPopup(const char *msg);
178 private:
180  static uint8_t m_buffer[W * H * C::BITS_PER_PIXEL / 8];
181 };
182 
183 template<class C, uint8_t W, uint8_t H, uint8_t B>
184 uint16_t NanoEngineTiler<C,W,H,B>::m_refreshFlags[NE_MAX_TILES_NUM];
185 
186 template<class C, uint8_t W, uint8_t H, uint8_t B>
187 uint8_t NanoEngineTiler<C,W,H,B>::m_buffer[W * H * C::BITS_PER_PIXEL / 8];
188 
189 template<class C, uint8_t W, uint8_t H, uint8_t B>
190 C NanoEngineTiler<C,W,H,B>::canvas(W, H, m_buffer);
191 
192 template<class C, uint8_t W, uint8_t H, uint8_t B>
194 
195 template<class C, uint8_t W, uint8_t H, uint8_t B>
197 {
198  if (!m_onDraw) // If onDraw handler is not set, just output current canvas
199  {
200  canvas.blt();
201  return;
202  }
203  for (uint8_t y = 0; y < ssd1306_lcd.height; y = y + NE_TILE_HEIGHT)
204  {
205  uint16_t flag = m_refreshFlags[y >> NE_TILE_SIZE_BITS];
206  m_refreshFlags[y >> NE_TILE_SIZE_BITS] = 0;
207  for (uint8_t x = 0; x < ssd1306_lcd.width; x = x + NE_TILE_WIDTH)
208  {
209  if (flag & 0x01)
210  {
211  canvas.setOffset(x, y);
212  if (m_onDraw()) canvas.blt();
213  }
214  flag >>=1;
215  }
216  }
217 }
218 
219 template<class C, uint8_t W, uint8_t H, uint8_t B>
221 {
222  NanoRect rect = { 8, (ssd1306_lcd.height>>1) - 8, ssd1306_lcd.width - 8, (ssd1306_lcd.height>>1) + 8 };
223  // TODO: It would be nice to calculate message height
224  NanoPoint textPos = { (ssd1306_lcd.width - (lcdint_t)strlen(msg)*s_fixedFont.width) >> 1, (ssd1306_lcd.height>>1) - 4 };
225  refresh(rect);
226  for (uint8_t y = 0; y < ssd1306_lcd.height; y = y + NE_TILE_HEIGHT)
227  {
228  uint16_t flag = m_refreshFlags[y >> NE_TILE_SIZE_BITS];
229  m_refreshFlags[y >> NE_TILE_SIZE_BITS] = 0;
230  for (uint8_t x = 0; x < ssd1306_lcd.width; x = x + NE_TILE_WIDTH)
231  {
232  if (flag & 0x01)
233  {
234  canvas.setOffset(x, y);
235  if (m_onDraw) m_onDraw();
236  canvas.setColor(RGB_COLOR8(0,0,0));
237  canvas.fillRect(rect);
238  canvas.setColor(RGB_COLOR8(192,192,192));
239  canvas.drawRect(rect);
240  canvas.printFixed( textPos.x, textPos.y, msg);
241 
242  canvas.blt();
243  }
244  flag >>=1;
245  }
246  }
247 }
248 
249 #endif
250 
static const uint8_t NE_TILE_SIZE_BITS
Definition: tiler.h:77
Definition: rect.h:37
NanoEngineTiler()
Definition: tiler.h:70
static void refresh(const NanoPoint &point)
Definition: tiler.h:109
int lcdint_t
Definition: io.h:63
#define RGB_COLOR8(r, g, b)
NanoPoint p2
Definition: rect.h:43
SFixedFontInfo s_fixedFont
Definition: tiler.h:39
static void refresh()
Definition: tiler.h:91
lcdint_t y
Definition: point.h:40
static bool collision(NanoPoint &p, NanoRect &rect)
Returns true if point is inside the rectangle area. Returns true if point is inside the rectangle are...
Definition: tiler.h:152
ssd1306_lcd_t ssd1306_lcd
Definition: lcd_common.c:32
static void refresh(const NanoRect &rect)
Definition: tiler.h:100
#define min(a, b)
static void drawCallback(TNanoEngineOnDraw callback)
Definition: tiler.h:143
lcduint_t height
Definition: lcd_common.h:97
uint8_t width
width in pixels
static const uint8_t NE_TILE_HEIGHT
Definition: tiler.h:81
static void displayBuffer()
refreshes content on oled display. Refreshes content on oled display. Call it, if you want to update ...
Definition: tiler.h:196
bool collision(const NanoPoint &p) const
Definition: rect.h:104
static void displayPopup(const char *msg)
prints popup message over display content prints popup message over display content ...
Definition: tiler.h:220
static TNanoEngineOnDraw m_onDraw
Definition: tiler.h:162
static C canvas
Definition: tiler.h:86
static void refresh(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
Definition: tiler.h:119
bool(* TNanoEngineOnDraw)(void)
Definition: tiler.h:54
lcduint_t width
Definition: lcd_common.h:94
NanoPoint p1
Definition: rect.h:40
lcdint_t x
Definition: point.h:38
static const uint8_t NE_MAX_TILES_NUM
Definition: tiler.h:83
static uint16_t m_refreshFlags[NE_MAX_TILES_NUM]
Definition: tiler.h:159
static const uint8_t NE_TILE_WIDTH
Definition: tiler.h:79