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 
31 #include <mraa.hpp>
32 
33 #define swap(a, b) { int16_t t = a; a = b; b = t; }
34 
35 namespace upm {
36 
37 /**
38  * @brief GFX helper class
39  *
40  * This file is used by the screen.
41  */
42 class GFX {
43     public:
44         /**
45          * Instantiates a GFX object
46          *
47          * @param width Screen width
48          * @param height Screen height
49          * @param screenBuffer Pointer to the screen buffer
50          * @param font Pointer to the font map
51          */
52         GFX (int width, int height, uint8_t * screenBuffer, const unsigned char * font);
53 
54         /**
55          * GFX object destructor
56          */
57         ~GFX ();
58 
59         /**
60          * Sets the window address
61          *
62          * @param x0 First coordinate
63          * @param y0 First coordinate
64          * @param x1 Second coordinate
65          * @param y1 Second coordinate
66          */
67         virtual void setAddrWindow (uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) = 0;
68 
69         /**
70          * Sends a pixel color (RGB) to the chip. Must be implemented by the
71          * inherited class.
72          *
73          * @param x Axis on the horizontal scale
74          * @param y Axis on the vertical scale
75          * @param color RGB value
76          */
77         virtual void drawPixel (int16_t x, int16_t y, uint16_t color) = 0;
78 
79         /**
80          * Copies the buffer to the chip via the SPI.
81          */
82         virtual void refresh () = 0;
83 
84         /**
85          *
86          *
87          * @param x Axis on the horizontal scale
88          * @param y Axis on the vertical scale
89          * @param data Character to write
90          * @param color Character color
91          * @param bg Character background color
92          * @param size Size of the font
93          */
94         void drawChar (int16_t x, int16_t y, uint8_t data, uint16_t color, uint16_t bg, uint8_t size);
95 
96         /**
97          * Prints a message on the screen
98          *
99          * @param msg Message to print
100          */
101         void print (std::string msg);
102 
103         /**
104          * Prints a message on the screen
105          *
106          * @param x Axis on the horizontal scale
107          * @param y Axis on the vertical scale
108          * @param color Pixel color
109          */
110         mraa::Result setPixel (int x, int y, uint16_t color);
111 
112         /**
113          * Fills the screen with a selected color
114          *
115          * @param color Selected color
116          */
117         void fillScreen (uint16_t color);
118 
119         /**
120          * Fills a rectangle with a selected color
121          *
122          * @param x Axis on the horizontal scale (top-left corner)
123          * @param y Axis on the vertical scale (top-left corner)
124          * @param w Distanse from x
125          * @param h Distanse from y
126          * @param color Selected color
127          */
128         void fillRect (int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
129 
130         /**
131          * Draws a line on the vertical scale
132          *
133          * @param x Axis on the horizontal scale
134          * @param y Axis on the vertical scale
135          * @param h Distanse from y
136          * @param color Selected color
137          */
138         void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
139 
140         /**
141          * Draws a line from coordinate C0 to coordinate C1
142          *
143          * @param x0 First coordinate
144          * @param y0 First coordinate
145          * @param x1 Second coordinate
146          * @param y1 Second coordinate
147          * @param color selected color
148          */
149         void drawLine (int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
150 
151         /**
152          * Draws a triangle
153          *
154          * @param x0 First coordinate
155          * @param y0 First coordinate
156          * @param x1 Second coordinate
157          * @param y1 Second coordinate
158          * @param x2 Third coordinate
159          * @param y2 Third coordinate
160          * @param color Selected color
161          */
162         void drawTriangle (int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
163 
164         /**
165          * Draws a circle
166          *
167          * @param x Center of the circle on the horizontal scale
168          * @param y Center of the circle on the vertical scale
169          * @param r Radius of the circle
170          * @param color Color of the circle
171          */
172         void drawCircle (int16_t x, int16_t y, int16_t r, uint16_t color);
173 
174         /**
175          * Sets the cursor for a text message
176          *
177          * @param x Axis on the horizontal scale
178          * @param y Axis on the vertical scale
179          */
180         void setCursor (int16_t x, int16_t y);
181 
182         /**
183          * Sets a text color for a message
184          *
185          * @param textColor Font color
186          * @param textBGColor Background color
187          */
188         void setTextColor (uint16_t textColor, uint16_t textBGColor);
189 
190         /**
191          * Sets the size of the font
192          *
193          * @param size Font size
194          */
195         void setTextSize (uint8_t size);
196 
197         /**
198          * Wraps a printed message
199          *
200          * @param wrap True (0x1) or false (0x0)
201          */
202         void setTextWrap (uint8_t wrap);
203 
204         int m_height; /**< Screen height */
205         int m_width; /**< Screen width */
206         int m_textSize; /**< Printed text size */
207         int m_textColor; /**< Printed text color */
208         int m_textBGColor; /**< Printed text background color */
209         int m_cursorX; /**< Cursor X coordinate */
210         int m_cursorY; /**< Cursor Y coordinate */
211         int m_wrap; /**< Wrapper flag (true or false) */
212 
213         uint8_t * m_map; /**< Screens buffer */
214 
215     protected:
216         const int16_t   WIDTH, HEIGHT;
217         const unsigned char * m_font;
218     };
219 }
220