upm/src/st7735/gfx.cxx

153 lines
3.8 KiB
C++
Raw Normal View History

/*
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
* Copyright (c) 2014 Intel Corporation.
*
* 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.
*/
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include "gfx.h"
using namespace upm;
GFX::GFX (int width, int height) {
m_height = height;
m_width = width;
}
GFX::~GFX () {
}
maa_result_t
GFX::setPixel (int x, int y, uint16_t color) {
if((x < 0) ||(x >= m_width) || (y < 0) || (y >= m_height)) {
return MAA_ERROR_UNSPECIFIED;
}
int index = ((y * m_width) + x) * sizeof(uint16_t);
m_map[index] = (uint8_t) (color >> 8);
m_map[++index] = (uint8_t)(color);
return MAA_SUCCESS;
}
void
GFX::fillScreen (uint16_t color) {
fillRect(0, 0, m_width, m_height, color);
}
void
GFX::fillRect (int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
for (int16_t i=x; i<x+w; i++) {
drawFastVLine(i, y, h, color);
}
}
void
GFX::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) {
drawLine(x, y, x, y+h-1, color);
}
void
GFX::drawLine (int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) {
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
swap(x0, y0);
swap(x1, y1);
}
if (x0 > x1) {
swap(x0, x1);
swap(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs (y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
for (; x0 <= x1; x0++) {
if (steep) {
setPixel(y0, x0, color);
} else {
setPixel(x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}
void
GFX::drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
drawLine(x0, y0, x1, y1, color);
drawLine(x1, y1, x2, y2, color);
drawLine(x2, y2, x0, y0, color);
}
void
GFX::drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
setPixel(x0 , y0+r, color);
setPixel(x0 , y0-r, color);
setPixel(x0+r, y0 , color);
setPixel(x0-r, y0 , color);
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
setPixel(x0 + x, y0 + y, color);
setPixel(x0 - x, y0 + y, color);
setPixel(x0 + x, y0 - y, color);
setPixel(x0 - x, y0 - y, color);
setPixel(x0 + y, y0 + x, color);
setPixel(x0 - y, y0 + x, color);
setPixel(x0 + y, y0 - x, color);
setPixel(x0 - y, y0 - x, color);
}
}