SSD1306 OLED display driver  1.5.6
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 
48 
49 typedef bool (*TNanoEngineOnDraw)(void);
50 
60 template<class C, uint8_t W, uint8_t H, uint8_t B>
62 {
63 protected:
66 
67 public:
69  static const uint8_t NE_TILE_SIZE_BITS = B;
71  static const uint8_t NE_TILE_WIDTH = W;
73  static const uint8_t NE_TILE_HEIGHT = H;
75  static const uint8_t NE_MAX_TILES_NUM = 16 >> (B - 3);
76 
78  static C canvas;
79 
83  static void refresh()
84  {
85  memset(m_refreshFlags,0xFF,sizeof(uint16_t) * NanoEngineTiler<C,W,H,B>::NE_MAX_TILES_NUM);
86  }
87 
92  static void refresh(const NanoRect &rect)
93  {
94  refresh(rect.p1.x, rect.p1.y, rect.p2.x, rect.p2.y);
95  }
96 
101  static void refresh(const NanoPoint &point)
102  {
103  if ((point.y<0) || ((point.y>>B)>=NE_MAX_TILES_NUM)) return;
104  m_refreshFlags[(point.y>>B)] |= (1<<(point.x>>B));
105  }
106 
111  static void refresh(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
112  {
113  if (y1 < 0) y1 = 0;
114  if (x1 < 0) x1 = 0;
115  y1 = y1>>B;
116  y2 = min((y2>>B), NE_MAX_TILES_NUM - 1);
117  for (uint8_t y=y1; y<=y2; y++)
118  {
119  for(uint8_t x=x1>>B; x<=(x2>>B); x++)
120  {
121  m_refreshFlags[y] |= (1<<x);
122  }
123  }
124  }
125 
134  static void drawCallback(TNanoEngineOnDraw callback) { m_onDraw = callback; };
135 
143  static bool collision(NanoPoint &p, NanoRect &rect) { return rect.collision( p ); }
144 
145 protected:
151 
154 
161  static void displayBuffer();
162 
168  static void displayPopup(const char *msg);
169 private:
171  static uint8_t m_buffer[W * H * C::BITS_PER_PIXEL / 8];
172 };
173 
174 template<class C, uint8_t W, uint8_t H, uint8_t B>
176 
177 template<class C, uint8_t W, uint8_t H, uint8_t B>
178 uint8_t NanoEngineTiler<C,W,H,B>::m_buffer[W * H * C::BITS_PER_PIXEL / 8];
179 
180 template<class C, uint8_t W, uint8_t H, uint8_t B>
182 
183 template<class C, uint8_t W, uint8_t H, uint8_t B>
185 
186 template<class C, uint8_t W, uint8_t H, uint8_t B>
188 {
189  if (!m_onDraw) // If onDraw handler is not set, just output current canvas
190  {
191  canvas.blt();
192  return;
193  }
194  for (uint8_t y = 0; y < ssd1306_lcd.height; y = y + NE_TILE_HEIGHT)
195  {
196  uint16_t flag = m_refreshFlags[y >> NE_TILE_SIZE_BITS];
198  for (uint8_t x = 0; x < ssd1306_lcd.width; x = x + NE_TILE_WIDTH)
199  {
200  if (flag & 0x01)
201  {
202  canvas.setOffset(x, y);
203  if (m_onDraw()) canvas.blt();
204  }
205  flag >>=1;
206  }
207  }
208 }
209 
210 template<class C, uint8_t W, uint8_t H, uint8_t B>
212 {
213  NanoRect rect = { 8, (ssd1306_lcd.height>>1) - 8, ssd1306_lcd.width - 8, (ssd1306_lcd.height>>1) + 8 };
214  // TODO: It would be nice to calculate message height
215  NanoPoint textPos = { (ssd1306_lcd.width - (lcdint_t)strlen(msg)*s_fixedFont.width) >> 1, (ssd1306_lcd.height>>1) - 4 };
216  refresh(rect);
217  for (uint8_t y = 0; y < ssd1306_lcd.height; y = y + NE_TILE_HEIGHT)
218  {
219  uint16_t flag = m_refreshFlags[y >> NE_TILE_SIZE_BITS];
221  for (uint8_t x = 0; x < ssd1306_lcd.width; x = x + NE_TILE_WIDTH)
222  {
223  if (flag & 0x01)
224  {
225  canvas.setOffset(x, y);
226  if (m_onDraw) m_onDraw();
227  canvas.setColor(RGB_COLOR8(0,0,0));
228  canvas.fillRect(rect);
229  canvas.setColor(RGB_COLOR8(192,192,192));
230  canvas.drawRect(rect);
231  canvas.printFixed( textPos.x, textPos.y, msg);
232 
233  canvas.blt();
234  }
235  flag >>=1;
236  }
237  }
238 }
239 
240 #endif
241 
static const uint8_t NE_TILE_SIZE_BITS
Definition: tiler.h:69
NanoEngineTiler()
Definition: tiler.h:65
static void refresh(const NanoPoint &point)
Definition: tiler.h:101
#define RGB_COLOR8(r, g, b)
NanoPoint p2
Definition: canvas.h:152
SFixedFontInfo s_fixedFont
Definition: tiler.h:39
static void refresh()
Definition: tiler.h:83
lcdint_t y
Definition: canvas.h:48
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:143
ssd1306_lcd_t ssd1306_lcd
Definition: lcd_common.c:32
static void refresh(const NanoRect &rect)
Definition: tiler.h:92
#define min(a, b)
static void drawCallback(TNanoEngineOnDraw callback)
Definition: tiler.h:134
lcduint_t height
Definition: lcd_common.h:93
int lcdint_t
Definition: io.h:40
uint8_t width
width in pixels
static const uint8_t NE_TILE_HEIGHT
Definition: tiler.h:73
static void displayBuffer()
refreshes content on oled display. Refreshes content on oled display. Call it, if you want to update ...
Definition: tiler.h:187
bool collision(const NanoPoint &p) const
Definition: canvas.h:213
static void displayPopup(const char *msg)
prints popup message over display content prints popup message over display content ...
Definition: tiler.h:211
static TNanoEngineOnDraw m_onDraw
Definition: tiler.h:153
static C canvas
Definition: tiler.h:78
static void refresh(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
Definition: tiler.h:111
bool(* TNanoEngineOnDraw)(void)
Definition: tiler.h:49
lcduint_t width
Definition: lcd_common.h:90
NanoPoint p1
Definition: canvas.h:149
lcdint_t x
Definition: canvas.h:46
static const uint8_t NE_MAX_TILES_NUM
Definition: tiler.h:75
static uint16_t m_refreshFlags[NE_MAX_TILES_NUM]
Definition: tiler.h:150
static const uint8_t NE_TILE_WIDTH
Definition: tiler.h:71