Test for STM32F4
Fork of RGB_OLED_SSD1331 by
Diff: src/SGL.cpp
- Revision:
- 8:ff74bd4d94d6
- Parent:
- 4:1707ca53e7d5
- Child:
- 10:ef7440718431
--- a/src/SGL.cpp Sun Nov 15 23:07:48 2015 +0000 +++ b/src/SGL.cpp Tue Nov 17 08:43:15 2015 +0000 @@ -30,12 +30,14 @@ #include <stdlib.h> #include "SGL.h" -SGL::SGL(uint16_t width, uint16_t height) +//--------------------------------------------------------------------------------------- +SGL::SGL(uint8_t width, uint8_t height) { - _width = width; + _width = width; _height = height; } +//--------------------------------------------------------------------------------------- void SGL::drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint16_t color) { uint8_t x = x1-x0; @@ -57,40 +59,45 @@ } } -void SGL::drawVerticalLine(uint16_t x, uint16_t y, uint16_t height,uint16_t color) +//--------------------------------------------------------------------------------------- +void SGL::drawVLine(uint8_t x, uint8_t y, uint8_t length,uint16_t color) { - uint16_t y1 = MIN(y+height,_height-1); - for(int16_t i = y; i < y1; i++){ - drawPixel(x,i,color); + uint8_t y1 = MIN(y+length,_height-1); + for(uint8_t i = y; i < y1; ++i){ + drawPixel(x, i, color); } } -void SGL::drawHorizontalLine(uint16_t x, uint16_t y, uint16_t width, uint16_t color) +//--------------------------------------------------------------------------------------- +void SGL::drawHLine(uint8_t x, uint8_t y, uint8_t length, uint16_t color) { - uint16_t x1 = MIN(x+width,_width-1); - for(int16_t i = x; i < x1; i++){ - drawPixel(i,y,color); + uint8_t x1 = MIN(x+length,_width-1); + for(uint8_t i = x; i < x1; ++i){ + drawPixel(i, y, color); } } -void SGL::drawRectangle(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t color) +//--------------------------------------------------------------------------------------- +void SGL::drawRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint16_t color) { - drawHorizontalLine(x, y, width, color); - drawHorizontalLine(x, y+height, width, color); - drawVerticalLine(x, y, height, color); - drawVerticalLine(x+width, y, height, color); + drawHLine(x, y, width, color); + drawHLine(x, y+height, width, color); + drawVLine(x, y, height, color); + drawVLine(x+width, y, height, color); } -void SGL::fillRectangle(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t color) +//--------------------------------------------------------------------------------------- +void SGL::fillRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint16_t color) { - for(uint16_t i = 0; i < height; i++){ - for(uint16_t j = 0; j < width; j++){ - drawPixel(x+j,y+i,color); + for(uint8_t i = 0; i < height; ++i){ + for(uint8_t j = 0; j < width; ++j){ + drawPixel( x+j, y+i, color); } } } -void SGL::drawCircle(uint16_t poX, uint16_t poY, uint16_t r, uint16_t color) +//--------------------------------------------------------------------------------------- +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{ @@ -107,12 +114,13 @@ } while(x <= 0); } -void SGL::fillCircle(uint16_t poX, uint16_t poY, uint16_t r, uint16_t color) +//--------------------------------------------------------------------------------------- +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{ - drawVerticalLine(poX-x, poY-y, 2*y, color); - drawVerticalLine(poX+x, poY-y, 2*y, color); + drawVLine(poX-x, poY-y, 2*y, color); + drawVLine(poX+x, poY-y, 2*y, color); e2 = err; if(e2 <= y){ err += ++y*2+1; @@ -122,16 +130,18 @@ }while(x <= 0); } -void SGL::drawTraingle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,uint16_t x2, uint16_t y2, uint16_t color) +//--------------------------------------------------------------------------------------- +void SGL::drawTraingle(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint16_t color) { drawLine(x0, y0, x1, y1,color); drawLine(x1, y1, x2, y2,color); drawLine(x2, y2, x0, y0,color); } -void SGL::fillTraingle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,uint16_t x2, uint16_t y2, uint16_t color) +//--------------------------------------------------------------------------------------- +void SGL::fillTraingle(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint16_t color) { - uint16_t a, b, y, last; + uint8_t a, b, y, last; if(y0 > y1){ swap(&y0, &y1); swap(&x0, &x1); } if(y1 > y2){ swap(&y2, &y1); swap(&x2, &x1); } @@ -140,7 +150,7 @@ if(y0 == y2){ x0 = MIN(x0,x1)<x2?MIN(x0,x1):x2; x2 = MAX(x0,x1)>x2?MAX(x0,x1):x2; - drawHorizontalLine(x0, y0, x2-x0, color); + drawHLine(x0, y0, x2-x0, color); return; } @@ -149,43 +159,46 @@ dx12 = x2 - x1, dy12 = y2 - y1; int16_t sa = 0, sb = 0; - if(y1 == y2) last = y1; - else last = y1-1; + if(y1 == y2) + last = y1; + 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; sb += dx02; if(a > b) swap(&a,&b); - drawHorizontalLine(a, y, b-a+1, color); + drawHLine(a, y, b-a+1, color); } 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; sb += dx02; if(a > b) swap(&a,&b); - drawHorizontalLine(a, y, b-a+1, color); + drawHLine(a, y, b-a+1, color); } } -void SGL::drawChar(uint8_t ascii, uint16_t x, uint16_t y, uint16_t size, uint16_t color) +//--------------------------------------------------------------------------------------- +void SGL::drawChar(uint8_t ascii, uint8_t x, uint8_t y, uint8_t size, uint16_t color) { if((ascii<32)||(ascii>=127)){ return; } - for (int8_t i = 0; i < FONT_X; i++ ) { - int8_t temp = simpleFont[ascii-0x20][i]; - int8_t inrun = 0; - int8_t runlen = 0; - int8_t endrun = 0; + for (uint8_t i = 0; i < FONT_X; ++i ) { + uint8_t temp = simpleFont[ascii-0x20][i]; + uint8_t inrun = 0; + uint8_t runlen = 0; + uint8_t endrun = 0; - for(int8_t f = 0; f < FONT_Y; f++){ + for(uint8_t f = 0; f < FONT_Y; f++){ if((temp>>f)&0x01){ if (inrun) runlen += 1; else { @@ -206,7 +219,7 @@ } if (endrun) { - fillRectangle(x+i*size, y+(f-runlen)*size, size, runlen*size, color); + fillRect(x+i*size, y+(f-runlen)*size, size, runlen*size, color); inrun = 0; runlen = 0; endrun = 0; @@ -215,7 +228,8 @@ } } -void SGL::drawString(char *string, uint16_t x, uint16_t y, uint16_t size, uint16_t color) +//--------------------------------------------------------------------------------------- +void SGL::drawString(char *string, uint8_t x, uint8_t y, uint8_t size, uint16_t color) { while(*string){ drawChar(*string, x, y, size, color); @@ -228,19 +242,25 @@ } } -void SGL::drawBitMap(uint16_t x, uint16_t y, const uint8_t *bitmap, uint16_t width, int16_t height, uint16_t color) +//--------------------------------------------------------------------------------------- +void SGL::drawBitMap(uint8_t x, uint8_t y, const uint8_t *bitmap, uint8_t width, uint8_t height, uint16_t color) { - uint16_t i, j, byteWidth = (width + 7) / 8; - for(j = 0; j < height; j++){ - for(i = 0; i < width; i++ ) { - if( *(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7)) ) { + uint8_t byteWidth = (width + 7) / 8; + + for(uint8_t j = 0; j < height; ++j) + { + for(uint8_t i = 0; i < width; ++i ) + { + if( *(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7)) ) + { drawPixel(x+i, y+j, color); } } } } +//--------------------------------------------------------------------------------------- void SGL::fillScreen(uint16_t color) { - fillRectangle(0, 0, _width, _height, color); + fillRect(0, 0, _width, _height, color); } \ No newline at end of file