Test for STM32F4
Fork of RGB_OLED_SSD1331 by
Diff: src/SGL.cpp
- Revision:
- 10:ef7440718431
- Parent:
- 8:ff74bd4d94d6
- Child:
- 11:162aa3e801df
--- a/src/SGL.cpp Tue Nov 17 19:45:22 2015 +0000 +++ b/src/SGL.cpp Tue Nov 17 21:20:37 2015 +0000 @@ -29,23 +29,28 @@ */ #include <stdlib.h> #include "SGL.h" +#include "SimpleFont.h" //--------------------------------------------------------------------------------------- -SGL::SGL(uint8_t width, uint8_t height) -{ - _width = width; - _height = height; -} +SGL::SGL(uint8_t width, uint8_t height): + _width(width), _height(height), + _currentFont(0), + _fontWidth(0), + _fontHeight(0), + _fontStart(0), + _fontStop(0) +{} //--------------------------------------------------------------------------------------- void SGL::drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint16_t color) { uint8_t x = x1-x0; uint8_t y = y1-y0; - uint8_t dx = abs(x), sx = x0<x1 ? 1 : -1; + uint8_t dx = abs(x), sx = x0<x1 ? 1 : -1; uint8_t dy = -abs(y), sy = y0<y1 ? 1 : -1; uint8_t err = dx+dy, e2; - for (;;){ + for (;;) + { drawPixel(x0, y0,color); e2 = 2*err; if (e2 >= dy) { @@ -63,7 +68,8 @@ void SGL::drawVLine(uint8_t x, uint8_t y, uint8_t length,uint16_t color) { uint8_t y1 = MIN(y+length,_height-1); - for(uint8_t i = y; i < y1; ++i){ + for(uint8_t i = y; i < y1; ++i) + { drawPixel(x, i, color); } } @@ -72,7 +78,8 @@ void SGL::drawHLine(uint8_t x, uint8_t y, uint8_t length, uint16_t color) { uint8_t x1 = MIN(x+length,_width-1); - for(uint8_t i = x; i < x1; ++i){ + for(uint8_t i = x; i < x1; ++i) + { drawPixel(i, y, color); } } @@ -89,8 +96,10 @@ //--------------------------------------------------------------------------------------- void SGL::fillRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint16_t color) { - for(uint8_t i = 0; i < height; ++i){ - for(uint8_t j = 0; j < width; ++j){ + for(uint8_t i = 0; i < height; ++i) + { + for(uint8_t j = 0; j < width; ++j) + { drawPixel( x+j, y+i, color); } } @@ -100,13 +109,15 @@ void SGL::drawCircle(uint8_t poX, uint8_t poY, uint8_t r, uint16_t color) { int x = -r, y = 0, err = 2-2*r, e2; - do{ + do + { drawPixel(poX-x, poY+y,color); drawPixel(poX+x, poY+y,color); drawPixel(poX+x, poY-y,color); drawPixel(poX-x, poY-y,color); e2 = err; - if(e2 <= y) { + if(e2 <= y) + { err += ++y*2+1; if(-x == y && e2 <= x) e2 = 0; } @@ -118,11 +129,13 @@ void SGL::fillCircle(uint8_t poX, uint8_t poY, uint8_t r, uint16_t color) { int x = -r, y = 0, err = 2-2*r, e2; - do{ + do + { drawVLine(poX-x, poY-y, 2*y, color); drawVLine(poX+x, poY-y, 2*y, color); e2 = err; - if(e2 <= y){ + if(e2 <= y) + { err += ++y*2+1; if(-x == y && e2 <= x) e2 = 0; } @@ -164,7 +177,8 @@ else last = y1-1; - for(y=y0; y<=last; ++y) { + for(y = y0; y <= last; ++y) + { a = x0 + sa / dy01; b = x0 + sb / dy02; sa += dx01; @@ -175,7 +189,8 @@ sa = dx12 * (y - y1); sb = dx02 * (y - y0); - for(; y<=y2; ++y) { + for(; y<=y2; ++y) + { a = x1 + sa / dy12; b = x0 + sb / dy02; sa += dx12; @@ -186,31 +201,41 @@ } //--------------------------------------------------------------------------------------- -void SGL::drawChar(uint8_t ascii, uint8_t x, uint8_t y, uint8_t size, uint16_t color) +void SGL::drawChar(uint8_t ascii, uint8_t x, uint8_t y, float zoom, uint16_t color) { - if((ascii<32)||(ascii>=127)){ + if(!_currentFont && !*_currentFont) + return; + + if((ascii < _fontStart)||(ascii > _fontStop)){ return; } - for (uint8_t i = 0; i < FONT_X; ++i ) { - uint8_t temp = simpleFont[ascii-0x20][i]; - uint8_t inrun = 0; + for(uint8_t i = 0; i < _fontWidth; ++i ) + { + uint8_t temp = simpleFont[ascii-0x20][i]; + uint8_t inrun = 0; uint8_t runlen = 0; uint8_t endrun = 0; - for(uint8_t f = 0; f < FONT_Y; f++){ - if((temp>>f)&0x01){ + for(uint8_t f = 0; f < _fontHeight; ++f) + { + if((temp>>f)&0x01) + { if (inrun) runlen += 1; - else { - inrun = 1; + else + { + inrun = 1; runlen = 1; } - } else if (inrun) { + } + else if (inrun) + { endrun = 1; - inrun = 0; + inrun = 0; } - if (f == FONT_Y - 1 && inrun) { + if (f == _fontHeight - 1 && inrun) + { endrun = 1; // need the +1 b/c we this code is normally // only triggered when f == FONT_Y, due to the @@ -218,8 +243,9 @@ f += 1; } - if (endrun) { - fillRect(x+i*size, y+(f-runlen)*size, size, runlen*size, color); + if (endrun) + { + fillRect(x+i*zoom, y+(f-runlen)*zoom, zoom, runlen*zoom, color); inrun = 0; runlen = 0; endrun = 0; @@ -229,14 +255,19 @@ } //--------------------------------------------------------------------------------------- -void SGL::drawString(char *string, uint8_t x, uint8_t y, uint8_t size, uint16_t color) +void SGL::drawString(const char *string, uint8_t x, uint8_t y, float zoom, uint16_t color, uint8_t fontSpace) { - while(*string){ - drawChar(*string, x, y, size, color); + if(!_currentFont && !*_currentFont) + return; + + while(*string) + { + drawChar(*string, x, y, zoom, color); *string++; - x += FONT_SPACE*size; - if(x >= _width-1){ - y += FONT_Y*size; + x += fontSpace*zoom; + if(x >= _width-1) + { + y += _fontHeight*zoom; x = 0; } } @@ -263,4 +294,17 @@ void SGL::fillScreen(uint16_t color) { fillRect(0, 0, _width, _height, color); +} + +//--------------------------------------------------------------------------------------- +void SGL::setFont(FontType font, uint8_t width, uint8_t height, uint8_t start, uint8_t stop) +{ + if(!font && !*font) + return; + + _currentFont= font; + _fontWidth = width; + _fontHeight = height; + _fontStart = start; + _fontStop = stop; } \ No newline at end of file