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
25 #include <iostream>
26 #include <unistd.h>
27 #include <stdlib.h>
28
29 #include "gfx.h"
30
31 using namespace upm;
32
GFX(int width,int height,uint8_t * screenBuffer,const unsigned char * font)33 GFX::GFX (int width, int height, uint8_t * screenBuffer, const unsigned char * font) : WIDTH(width), HEIGHT(height) {
34 m_height = height;
35 m_width = width;
36 m_font = font;
37 m_map = screenBuffer;
38 }
39
~GFX()40 GFX::~GFX () {
41 }
42
43 mraa::Result
setPixel(int x,int y,uint16_t color)44 GFX::setPixel (int x, int y, uint16_t color) {
45 if((x < 0) ||(x >= m_width) || (y < 0) || (y >= m_height)) {
46 return mraa::ERROR_UNSPECIFIED;
47 }
48
49 int index = ((y * m_width) + x) * sizeof(uint16_t);
50 m_map[index] = (uint8_t) (color >> 8);
51 m_map[++index] = (uint8_t)(color);
52
53 return mraa::SUCCESS;
54 }
55
56 void
fillScreen(uint16_t color)57 GFX::fillScreen (uint16_t color) {
58 fillRect(0, 0, m_width, m_height, color);
59 }
60
61 void
fillRect(int16_t x,int16_t y,int16_t w,int16_t h,uint16_t color)62 GFX::fillRect (int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
63 for (int16_t i=x; i<x+w; i++) {
64 drawFastVLine(i, y, h, color);
65 }
66 }
67
68 void
drawFastVLine(int16_t x,int16_t y,int16_t h,uint16_t color)69 GFX::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) {
70 drawLine(x, y, x, y+h-1, color);
71 }
72
73 void
drawLine(int16_t x0,int16_t y0,int16_t x1,int16_t y1,uint16_t color)74 GFX::drawLine (int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) {
75 int16_t steep = abs(y1 - y0) > abs(x1 - x0);
76
77 if (steep) {
78 swap(x0, y0);
79 swap(x1, y1);
80 }
81
82 if (x0 > x1) {
83 swap(x0, x1);
84 swap(y0, y1);
85 }
86
87 int16_t dx, dy;
88 dx = x1 - x0;
89 dy = abs (y1 - y0);
90
91 int16_t err = dx / 2;
92 int16_t ystep;
93
94 if (y0 < y1) {
95 ystep = 1;
96 } else {
97 ystep = -1;
98 }
99
100 for (; x0 <= x1; x0++) {
101 if (steep) {
102 setPixel(y0, x0, color);
103 } else {
104 setPixel(x0, y0, color);
105 }
106 err -= dy;
107 if (err < 0) {
108 y0 += ystep;
109 err += dx;
110 }
111 }
112 }
113
114 void
drawTriangle(int16_t x0,int16_t y0,int16_t x1,int16_t y1,int16_t x2,int16_t y2,uint16_t color)115 GFX::drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
116 drawLine(x0, y0, x1, y1, color);
117 drawLine(x1, y1, x2, y2, color);
118 drawLine(x2, y2, x0, y0, color);
119 }
120
121 void
drawCircle(int16_t x0,int16_t y0,int16_t r,uint16_t color)122 GFX::drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color) {
123 int16_t f = 1 - r;
124 int16_t ddF_x = 1;
125 int16_t ddF_y = -2 * r;
126 int16_t x = 0;
127 int16_t y = r;
128
129 setPixel(x0 , y0+r, color);
130 setPixel(x0 , y0-r, color);
131 setPixel(x0+r, y0 , color);
132 setPixel(x0-r, y0 , color);
133
134 while (x<y) {
135 if (f >= 0) {
136 y--;
137 ddF_y += 2;
138 f += ddF_y;
139 }
140 x++;
141
142 ddF_x += 2;
143 f += ddF_x;
144
145 setPixel(x0 + x, y0 + y, color);
146 setPixel(x0 - x, y0 + y, color);
147 setPixel(x0 + x, y0 - y, color);
148 setPixel(x0 - x, y0 - y, color);
149 setPixel(x0 + y, y0 + x, color);
150 setPixel(x0 - y, y0 + x, color);
151 setPixel(x0 + y, y0 - x, color);
152 setPixel(x0 - y, y0 - x, color);
153 }
154 }
155
156 void
setCursor(int16_t x,int16_t y)157 GFX::setCursor (int16_t x, int16_t y) {
158 m_cursorX = x;
159 m_cursorY = y;
160 }
161
162 void
setTextColor(uint16_t textColor,uint16_t textBGColor)163 GFX::setTextColor (uint16_t textColor, uint16_t textBGColor) {
164 m_textColor = textColor;
165 m_textBGColor = textBGColor;
166 }
167
168 void
setTextSize(uint8_t size)169 GFX::setTextSize (uint8_t size) {
170 m_textSize = (size > 0) ? size : 1;
171 }
172
173 void
setTextWrap(uint8_t wrap)174 GFX::setTextWrap (uint8_t wrap) {
175 m_wrap = wrap;
176 }
177
178 void
drawChar(int16_t x,int16_t y,uint8_t data,uint16_t color,uint16_t bg,uint8_t size)179 GFX::drawChar (int16_t x, int16_t y, uint8_t data, uint16_t color, uint16_t bg, uint8_t size) {
180 if( (x >= m_width) || // Clip right
181 (y >= m_height) || // Clip bottom
182 ((x + 6 * size - 1) < 0) || // Clip left
183 ((y + 8 * size - 1) < 0)) // Clip top
184 return;
185
186 for (int8_t i=0; i<6; i++ ) {
187 uint8_t line;
188 if (i == 5) {
189 line = 0x0;
190 } else {
191 line = *(m_font+(data * 5)+i);
192 for (int8_t j = 0; j<8; j++) {
193 if (line & 0x1) {
194 if (size == 1) // default size
195 setPixel (x+i, y+j, color);
196 else { // big size
197 fillRect (x+(i*size), y+(j*size), size, size, color);
198 }
199 } else if (bg != color) {
200 if (size == 1) // default size
201 setPixel (x+i, y+j, bg);
202 else { // big size
203 fillRect (x+i*size, y+j*size, size, size, bg);
204 }
205 }
206 line >>= 1;
207 }
208 }
209 }
210 }
211
212 void
print(std::string msg)213 GFX::print (std::string msg) {
214 int len = msg.length();
215
216 for (int idx = 0; idx < len; idx++) {
217 if (msg[idx] == '\n') {
218 m_cursorY += m_textSize * 8;
219 m_cursorX = 0;
220 } else if (msg[idx] == '\r') {
221 // skip em
222 } else {
223 drawChar(m_cursorX, m_cursorY, msg[idx], m_textColor, m_textBGColor, m_textSize);
224 m_cursorX += m_textSize * 6;
225 if (m_wrap && (m_textColor > (m_width - m_textSize * 6))) {
226 m_cursorY += m_textSize * 8;
227 m_cursorX = 0;
228 }
229 }
230 }
231 }
232