SSD1306 OLED display driver  1.6.99
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 (y1 < 0) y1 = 0;
122  if (x1 < 0) x1 = 0;
123  y1 = y1>>B;
124  y2 = min((y2>>B), NE_MAX_TILES_NUM - 1);
125  for (uint8_t y=y1; y<=y2; y++)
126  {
127  for(uint8_t x=x1>>B; x<=(x2>>B); x++)
128  {
129  m_refreshFlags[y] |= (1<<x);
130  }
131  }
132  }
133 
142  static void drawCallback(TNanoEngineOnDraw callback) { m_onDraw = callback; };
143 
151  static bool collision(NanoPoint &p, NanoRect &rect) { return rect.collision( p ); }
152 
153 protected:
159 
162 
169  static void displayBuffer();
170 
176  static void displayPopup(const char *msg);
177 private:
179  static uint8_t m_buffer[W * H * C::BITS_PER_PIXEL / 8];
180 };
181 
182 template<class C, uint8_t W, uint8_t H, uint8_t B>
184 
185 template<class C, uint8_t W, uint8_t H, uint8_t B>
186 uint8_t NanoEngineTiler<C,W,H,B>::m_buffer[W * H * C::BITS_PER_PIXEL / 8];
187 
188 template<class C, uint8_t W, uint8_t H, uint8_t B>
189 C NanoEngineTiler<C,W,H,B>::canvas(W, H, m_buffer);
190 
191 template<class C, uint8_t W, uint8_t H, uint8_t B>
193 
194 template<class C, uint8_t W, uint8_t H, uint8_t B>
196 {
197  if (!m_onDraw) // If onDraw handler is not set, just output current canvas
198  {
199  canvas.blt();
200  return;
201  }
202  for (uint8_t y = 0; y < ssd1306_lcd.height; y = y + NE_TILE_HEIGHT)
203  {
204  uint16_t flag = m_refreshFlags[y >> NE_TILE_SIZE_BITS];
206  for (uint8_t x = 0; x < ssd1306_lcd.width; x = x + NE_TILE_WIDTH)
207  {
208  if (flag & 0x01)
209  {
210  canvas.setOffset(x, y);
211  if (m_onDraw()) canvas.blt();
212  }
213  flag >>=1;
214  }
215  }
216 }
217 
218 template<class C, uint8_t W, uint8_t H, uint8_t B>
220 {
221  NanoRect rect = { 8, (ssd1306_lcd.height>>1) - 8, ssd1306_lcd.width - 8, (ssd1306_lcd.height>>1) + 8 };
222  // TODO: It would be nice to calculate message height
223  NanoPoint textPos = { (ssd1306_lcd.width - (lcdint_t)strlen(msg)*s_fixedFont.width) >> 1, (ssd1306_lcd.height>>1) - 4 };
224  refresh(rect);
225  for (uint8_t y = 0; y < ssd1306_lcd.height; y = y + NE_TILE_HEIGHT)
226  {
227  uint16_t flag = m_refreshFlags[y >> NE_TILE_SIZE_BITS];
229  for (uint8_t x = 0; x < ssd1306_lcd.width; x = x + NE_TILE_WIDTH)
230  {
231  if (flag & 0x01)
232  {
233  canvas.setOffset(x, y);
234  if (m_onDraw) m_onDraw();
235  canvas.setColor(RGB_COLOR8(0,0,0));
236  canvas.fillRect(rect);
237  canvas.setColor(RGB_COLOR8(192,192,192));
238  canvas.drawRect(rect);
239  canvas.printFixed( textPos.x, textPos.y, msg);
240 
241  canvas.blt();
242  }
243  flag >>=1;
244  }
245  }
246 }
247 
248 #endif
249 
static const uint8_t NE_TILE_SIZE_BITS
Definition: tiler.h:77
NanoEngineTiler()
Definition: tiler.h:70
static void refresh(const NanoPoint &point)
Definition: tiler.h:109
#define RGB_COLOR8(r, g, b)
NanoPoint p2
Definition: canvas.h:154
SFixedFontInfo s_fixedFont
Definition: tiler.h:39
static void refresh()
Definition: tiler.h:91
lcdint_t y
Definition: canvas.h:50
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:151
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:142
lcduint_t height
Definition: lcd_common.h:97
int lcdint_t
Definition: io.h:52
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:195
bool collision(const NanoPoint &p) const
Definition: canvas.h:215
static void displayPopup(const char *msg)
prints popup message over display content prints popup message over display content ...
Definition: tiler.h:219
static TNanoEngineOnDraw m_onDraw
Definition: tiler.h:161
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: canvas.h:151
lcdint_t x
Definition: canvas.h:48
static const uint8_t NE_MAX_TILES_NUM
Definition: tiler.h:83
static uint16_t m_refreshFlags[NE_MAX_TILES_NUM]
Definition: tiler.h:158
static const uint8_t NE_TILE_WIDTH
Definition: tiler.h:79