summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/ili9341/ili9341_gfx.hpp
blob: c62e81e4660904d2fea808ca0bd6982e3b986502 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
 * Author: Shawn Hymel
 * Copyright (c) 2016 SparkFun Electronics
 *
 * Based on GFX interface by Yevgeniy Kiveisha and Adafruit Industries.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#pragma once

#include <mraa.hpp>

#define adagfxswap(a, b) { int16_t t = a; a = b; b = t; }

namespace upm 
{
    /**
     * @brief GFX helper class
     */
    class GFX {
        public:
        
            /**
             * Creates a GFX object
             *
             * @param w Screen width
             * @param h Screen height
             */
            GFX(int16_t w, int16_t h);
             
            /**
             * Sends a pixel color (RGB) to the driver chip. This must be
             * defined by the subclass (pure virtual function).
             *
             * @param x Axis on the horizontal scale
             * @param y Axis on the vertical scale
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */
            virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0;
            
            /**
             * Draw a line.
             *
             * @param x0 Start of line x coordinate
             * @param y0 Start of line y coordinate
             * @param x1 End of line x coordinate
             * @param y1 End of line y coordinate
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */            
            virtual void drawLine(int16_t x0, 
                                  int16_t y0, 
                                  int16_t x1, 
                                  int16_t y1, 
                                  uint16_t color);
            
            /**
             * Draws a vertical line using minimal SPI writes.
             *
             * @param x Axis on the horizontal scale to begin line
             * @param y Axis on the vertical scale to begin line
             * @param h Height of line in pixels
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */
            virtual void drawFastVLine(int16_t x, 
                                       int16_t y, 
                                       int16_t h, 
                                       uint16_t color);
            
            /**
             * Draws a horizontal line using  minimal SPI writes.
             *
             * @param x Axis on the horizontal scale to begin line
             * @param y Axis on the vertical scale to begin line
             * @param w Width of line in pixels
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */
            virtual void drawFastHLine(int16_t x, 
                                       int16_t y, 
                                       int16_t w, 
                                       uint16_t color);
    
            /**
             * Draws a rectangle (not filled).
             *
             * @param x Position of upper left corner on horizontal axis
             * @param y Position of upper left corner on vertical axis
             * @param w Width of rectangle
             * @param h Height of rectangle
             * @color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */
            virtual void drawRect(int16_t x, 
                                  int16_t y, 
                                  int16_t w, 
                                  int16_t h, 
                                  uint16_t color);
                                 
            /**
             * Draw a filled rectangle.
             *
             * @param x Axis on the horizontal scale of upper-left corner
             * @param y Axis on the vertical scale of upper-left corner
             * @param w Width of rectangle in pixels
             * @param h Height of rectangle in pixels
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */ 
            virtual void fillRect(int16_t x, 
                                  int16_t y, 
                                  int16_t w, 
                                  int16_t h, 
                                  uint16_t color);
    
            /**
             * Fill the screen with a single color.
             *
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */
            virtual void fillScreen(uint16_t color);
    
            /**
             * Invert colors on the display.
             *
             * @param i True or false to invert colors
             */
            virtual void invertDisplay(bool i);
            
            /**
             * Draw a circle outline.
             *
             * @param x0 Center point of circle on x-axis
             * @param y0 Center point of circle on y-axis
             * @param r Radius of circle
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */
            void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
            
            /**
             * Used to draw rounded corners.
             *
             * @param x0 Center point of circle on x-axis
             * @param y0 Center point of circle on y-axis
             * @param r Radius of circle
             * @param cornername Mask of corner number (1, 2, 4, 8)
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */
            void drawCircleHelper(int16_t x0, 
                                  int16_t y0, 
                                  int16_t r, 
                                  uint8_t cornername,
                                  uint16_t color);

            /**
             * Draws a filled circle.
             *
             * @param x0 Center point of circle on x-axis
             * @param y0 Center point of circle on y-axis
             * @param r Radius of circle
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */    
            void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
    
            /**
             * Used to draw a filled circle and rounded rectangles.
             *
             * @param x0 Center point of circle on x-axis
             * @param y0 Center point of circle on y-axis
             * @param r Radius of circle
             * @param cornername Mask of corner number (1, 2, 4, 8)
             * @param delta Line offset
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */
            void fillCircleHelper(int16_t x0, 
                                  int16_t y0, 
                                  int16_t r, 
                                  uint8_t cornername,
                                  int16_t delta,
                                  uint16_t color);
                                  
            /**
             * Draw a triangle.
             *
             * @param x0 First point coordinate on x-axis
             * @param y0 First point coordinate on y-axis
             * @param x1 Second point coordinate on x-axis
             * @param y1 Second point coordinate on y-axis
             * @param x2 Third point coordinate on x-axis
             * @param y2 Third point coordinate on y-axis
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */
            void drawTriangle(int16_t x0, 
                              int16_t y0, 
                              int16_t x1, 
                              int16_t y1,
                              int16_t x2, 
                              int16_t y2, 
                              uint16_t color);
                              
            /**
             * Draw a filled triangle.
             *
             * @param x0 First point coordinate on x-axis
             * @param y0 First point coordinate on y-axis
             * @param x1 Second point coordinate on x-axis
             * @param y1 Second point coordinate on y-axis
             * @param x2 Third point coordinate on x-axis
             * @param y2 Third point coordinate on y-axis
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */
            void fillTriangle(int16_t x0,
                              int16_t y0,
                              int16_t x1,
                              int16_t y1,
                              int16_t x2,
                              int16_t y2,
                              uint16_t color);

            /**
             * Draw a rectangle with rounded corners
             *
             * @param x0 X-axis coordinate of top-left corner
             * @param y0 Y-axis coordinate of top-left corner
             * @param w Width of rectangle
             * @param h height of rectangle
             * @param radius Radius of rounded corners
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */
            void drawRoundRect(int16_t x0, 
                               int16_t y0, 
                               int16_t w, 
                               int16_t h,
                               int16_t radius, 
                               uint16_t color);
                               
            /**
             * Draw a filled rectangle with rounded corners
             *
             * @param x0 X-axis coordinate of top-left corner
             * @param y0 Y-axis coordinate of top-left corner
             * @param w Width of rectangle
             * @param h height of rectangle
             * @param radius Radius of rounded corners
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */
            void fillRoundRect(int16_t x0, 
                               int16_t y0, 
                               int16_t w, 
                               int16_t h,
                               int16_t radius, 
                               uint16_t color);
                               
            /**
             * Draw a character at the specified point.
             *
             * @param x X-axis coordinate of the top-left corner
             * @param y Y-axis coordinate of the top-left corner
             * @param c Character to draw
             * @param color RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             * @param bg Background color (16-bit RGB)
             * @param size Font size
             */
            void drawChar(int16_t x, 
                          int16_t y, 
                          unsigned char c, 
                          uint16_t color,
                          uint16_t bg, 
                          uint8_t size);
            
            /**
             * Get the x-axis coordinate of the upper-left corner of the cursor.
             *
             * @return X-axis coordinate of the cursor
             */
            int16_t getCursorX(void) const;
            
            /**
             * Get the y-axis coordinate of the upper-left corner of the cursor.
             *
             * @return Y-axis coordinate of the cursor
             */
            int16_t getCursorY(void) const;
             
            /**
             * Set the cursor for writing text.
             *
             * @param x X-axis coordinate of the top-left corner of the cursor
             * @param y Y-axis coordinate of the top-left corner of the cursor
             */
            void setCursor(int16_t x, int16_t y);
             
            /**
             * Set the color for text.
             *
             * @param c RGB (16-bit) color (R[0-4], G[5-10], B[11-15]
             */
            void setTextColor(uint16_t c);
            
            /**
             * Set the color for text and text background (highlight).
             *
             * @param c Text color (RGB, 16-bit)
             * @param bg Background text color (RGB, 16-bit)
             */
            void setTextColor(uint16_t c, uint16_t bg);
            
            /**
             * Set the size of the text.
             *
             * @param s Font size (multiples of 8 pixel text height)
             */ 
            void setTextSize(uint8_t s);
            
            /**
             * Enable or disable text wrapping.
             *
             * @param w True to wrap text. False to truncate.
             */
            void setTextWrap(bool w);
            
            /**
             * Get the current rotation configuration of the screen.
             *
             * @return current rotation 0-3
             */
            uint8_t getRotation(void) const;
            
            /**
             * Sets the rotation of the screen. Can be overridden with another
             * screen-specific definition.
             *
             * @param r Rotation 0-3
             */
            void setRotation(uint8_t r);
                        
            /**
             * Enable (or disable) Code Page 437-compatible charset.
             *
             * @param x True to enable CP437 charset. False to disable.
             */
            void cp437(bool x);
            
            /**
             * Write a character at the current cursor position. Definition
             * can be overridden with board-specific code.
             *
             * @param c Character to draw
             */
            virtual void write(uint8_t c);
            
            /**
             * Prints a string to the screen.
             *
             * @param s Message to print
             */
            void print(std::string msg);
            
            /**
             * Get the current width of the screen.
             *
             * @return the width in pixels
             */
            int16_t width(void) const;
            
            /**
             * Get the current height of the screen.
             *
             * @return the height in pixels
             */
            int16_t height(void) const;
            
        protected:
        
            const int16_t WIDTH;
            const int16_t HEIGHT;
            
            int16_t _width;
            int16_t _height;
            
            uint8_t rotation;
            
            uint16_t textcolor;
            uint16_t textbgcolor;
            int16_t cursor_x;
            int16_t cursor_y;
            uint8_t textsize;
            bool wrap;
            bool _cp437;
            static const unsigned char font[];
    };
}