mraa: change all existing code to use libmraa.
[contrib/upm.git] / src / st7735 / gfx.h
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25
26 #include <string>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <stdint.h>
30 #include <mraa.h>
31
32 #define swap(a, b) { int16_t t = a; a = b; b = t; }
33
34 namespace upm {
35
36 /**
37  * @brief GFX helper class
38  *
39  * This file used by the screen.
40  */
41 class GFX {
42     public:
43         /**
44          * Instanciates a GFX object
45          *
46          * @param width screen width
47          * @param height screen height
48          * @param screenBuffer pointer to screen buffer
49          * @param font pointer to font map
50          */
51         GFX (int width, int height, uint8_t * screenBuffer, const unsigned char * font);
52
53         /**
54          * GFX object destructor
55          */
56         ~GFX ();
57
58         /**
59          * Set the window address
60          *
61          * @param x0 first coordinate
62          * @param y0 first coordinate
63          * @param x1 second coordinate
64          * @param y1 second coordinate
65          */
66         virtual void setAddrWindow (uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) = 0;
67
68         /**
69          * Send pixel collor (RGB) to the chip. Must be implemented by
70          * inherited class.
71          *
72          * @param x axis on horizontal scale
73          * @param y axis on vertical scale
74          * @param color rgb value
75          */
76         virtual void drawPixel (int16_t x, int16_t y, uint16_t color) = 0;
77
78         /**
79          * Copy the buffer to the chip via SPI interface.
80          */
81         virtual void refresh () = 0;
82
83         /**
84          *
85          *
86          * @param x axis on horizontal scale
87          * @param y axis on vertical scale
88          * @param data character to write
89          * @param color character's color
90          * @param bg character's background color
91          * @param size size of the font
92          */
93         void drawChar (int16_t x, int16_t y, uint8_t data, uint16_t color, uint16_t bg, uint8_t size);
94
95         /**
96          * Print the message to the screen
97          *
98          * @param msg message which will be printed
99          */
100         void print (std::string msg);
101
102         /**
103          * Print the message to the screen
104          *
105          * @param x axis on horizontal scale
106          * @param y axis on vertical scale
107          * @param color pixel's color
108          */
109         mraa_result_t setPixel (int x, int y, uint16_t color);
110
111         /**
112          * Fill screen with selected color
113          *
114          * @param color selected's color
115          */
116         void fillScreen (uint16_t color);
117
118         /**
119          * Fill rectangle with selected color
120          *
121          * @param x axis on horizontal scale (top left corner)
122          * @param y axis on vertical scale (top left corner)
123          * @param w distanse from x
124          * @param h distanse from y
125          * @param color selected color
126          */
127         void fillRect (int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
128
129         /**
130          * Draw line in vertical scale.
131          *
132          * @param x axis on horizontal scale
133          * @param y axis on vertical scale
134          * @param h distanse from y
135          * @param color selected color
136          */
137         void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
138
139         /**
140          * Draw line from coordinate C0 to coordinate C1
141          *
142          * @param x0 first coordinate
143          * @param y0 first coordinate
144          * @param x1 second coordinate
145          * @param y1 second coordinate
146          * @param color selected color
147          */
148         void drawLine (int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
149
150         /**
151          * Draw a triangle
152          *
153          * @param x0 first coordinate
154          * @param y0 first coordinate
155          * @param x1 second coordinate
156          * @param y1 second coordinate
157          * @param x2 third coordinate
158          * @param y2 third coordinate
159          * @param color selected color
160          */
161         void drawTriangle (int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
162
163         /**
164          * Draw a circle
165          *
166          * @param x center of circule on X scale
167          * @param y center of circule on Y scale
168          */
169         void drawCircle (int16_t x, int16_t y, int16_t r, uint16_t color);
170
171         /**
172          * Set cursor for text message
173          *
174          * @param x axis on horizontal scale
175          * @param y axis on vertical scale
176          */
177         void setCursor (int16_t x, int16_t y);
178
179         /**
180          * Set text color for the message
181          *
182          * @param textColor font color
183          * @param textBGColor background color
184          */
185         void setTextColor (uint16_t textColor, uint16_t textBGColor);
186
187         /**
188          * Set the size of the font
189          *
190          * @param size font size
191          */
192         void setTextSize (uint8_t size);
193
194         /**
195          * Wrap printed message.
196          *
197          * @param wrap true (0x1) or false (0x0)
198          */
199         void setTextWrap (uint8_t wrap);
200
201         int m_height; /**< Screen height */
202         int m_width; /**< Screen width */
203         int m_textSize; /**< Printed text size */
204         int m_textColor; /**< Printed text color */
205         int m_textBGColor; /**< Printed text background color */
206         int m_cursorX; /**< Cursor X coordinate */
207         int m_cursorY; /**< Cursor Y coordinate */
208         int m_wrap; /**< Wrapper flag (true or false) */
209
210         uint8_t * m_map; /**< Screens buffer */
211
212     protected:
213         const int16_t   WIDTH, HEIGHT;
214         const unsigned char * m_font;
215     };
216 }