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