Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
LCD_ST7735/Canvas.h
- Committer:
- taylorza
- Date:
- 2015-01-28
- Revision:
- 0:d85c449aca6d
File content as of revision 0:d85c449aca6d:
#include "Base.h" #ifndef __CANVAS_H__ #define __CANVAS_H__ template <typename T> class Canvas { public: Canvas(T *pSurface) { _pSurface = pSurface; } inline void clear() { _pSurface->clear(); } inline void setPixel(int x, int y, uint16_t color) { _pSurface->setPixel(x, y, color); } void drawLine(int x1, int y1, int x2, int y2, uint16_t color) { int dx = abs(x2 - x1); int dy = abs(y2 - y1); if (dx == 0) { if (y1 > y2) swap(y1, y2); drawVertLine(x1, y1, y2, color); return; } else if(dy == 0) { if (x1 > x2) swap(x1, x2); drawHorizLine(x1, y1, x2, color); return; } int sx = (x1 < x2) ? 1 : -1; int sy = (y1 < y2) ? 1 : -1; int err = dx - dy; while(x1 != x2 || y1 != y2) { setPixel(x1, y1, color); int e2 = err << 1; if (e2 > -dy) { err -= dy; x1 += sx; } if (e2 < dx) { err += dx; y1 += sy; } } setPixel(x2, y2, color); } void drawRect(int x1, int y1, int x2, int y2, uint16_t color) { if (x1 > x2) swap(x1, x2); if (y1 > y2) swap(y1, y2); drawHorizLine(x1, y1, x2, color); drawHorizLine(x1, y2, x2, color); drawVertLine(x1, y1, y2, color); drawVertLine(x2, y1, y2, color); } void fillRect(int x1, int y1, int x2, int y2, uint16_t color) { if (x1 > x2) swap(x1, x2); if (y1 > y2) swap(y1, y2); for (int x = x1; x <= x2; ++x) { _pSurface->fastVLine(y1, y2, x, color); } } void drawBitmap(int x, int y, T &bmp, int srcX, int srcY, int srcWidth, int srcHeight, bool transparent) { _pSurface->drawBitmap(x, y, bmp, srcX, srcY, srcWidth, srcHeight, transparent); } private: void drawHorizLine(int x1, int y1, int x2, uint16_t color) { for(int x = x1; x <= x2; ++x) setPixel(x, y1, color); } void drawVertLine(int x1, int y1, int y2, uint16_t color) { for(int y = y1; y <= y2; ++y) setPixel(x1, y, color); } private: T *_pSurface; }; #endif // __CANVAS_H__