Test for STM32F4
Fork of RGB_OLED_SSD1331 by
src/SGL.cpp
- Committer:
- messi1
- Date:
- 2015-11-17
- Revision:
- 8:ff74bd4d94d6
- Parent:
- 4:1707ca53e7d5
- Child:
- 10:ef7440718431
File content as of revision 8:ff74bd4d94d6:
/* * SGL.cpp * A library for Seeed Graphical library * * Copyright (c) 2014 seeed technology inc. * Author : lawliet.zou(lawliet.zou@gmail.com) * Create Time : Jun 06, 2014 * Change Log : * * The MIT License (MIT) * * 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 <stdlib.h> #include "SGL.h" //--------------------------------------------------------------------------------------- SGL::SGL(uint8_t width, uint8_t height) { _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; uint8_t y = y1-y0; 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 (;;){ drawPixel(x0, y0,color); e2 = 2*err; if (e2 >= dy) { if (x0 == x1) break; err += dy; x0 += sx; } if (e2 <= dx) { if (y0 == y1) break; err += dx; y0 += sy; } } } //--------------------------------------------------------------------------------------- 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){ drawPixel(x, i, color); } } //--------------------------------------------------------------------------------------- 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){ drawPixel(i, y, color); } } //--------------------------------------------------------------------------------------- void SGL::drawRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint16_t 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::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){ drawPixel( x+j, y+i, 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{ 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) { err += ++y*2+1; if(-x == y && e2 <= x) e2 = 0; } if(e2 > x) err += ++x*2+1; } while(x <= 0); } //--------------------------------------------------------------------------------------- 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{ 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; if(-x == y && e2 <= x) e2 = 0; } if(e2 > x) err += ++x*2+1; }while(x <= 0); } //--------------------------------------------------------------------------------------- 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(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint16_t color) { uint8_t a, b, y, last; if(y0 > y1){ swap(&y0, &y1); swap(&x0, &x1); } if(y1 > y2){ swap(&y2, &y1); swap(&x2, &x1); } if(y0 > y1){ swap(&y1, &y0); swap(&x1, &x0); } if(y0 == y2){ x0 = MIN(x0,x1)<x2?MIN(x0,x1):x2; x2 = MAX(x0,x1)>x2?MAX(x0,x1):x2; drawHLine(x0, y0, x2-x0, color); return; } int16_t dx01 = x1 - x0, dy01 = y1 - y0, dx02 = x2 - x0, dy02 = y2 - y0, dx12 = x2 - x1, dy12 = y2 - y1; int16_t sa = 0, sb = 0; if(y1 == y2) last = y1; else last = y1-1; for(y=y0; y<=last; ++y) { a = x0 + sa / dy01; b = x0 + sb / dy02; sa += dx01; sb += dx02; if(a > b) swap(&a,&b); drawHLine(a, y, b-a+1, color); } sa = dx12 * (y - y1); sb = dx02 * (y - y0); for(; y<=y2; ++y) { a = x1 + sa / dy12; b = x0 + sb / dy02; sa += dx12; sb += dx02; if(a > b) swap(&a,&b); drawHLine(a, y, b-a+1, 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 (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(uint8_t f = 0; f < FONT_Y; f++){ if((temp>>f)&0x01){ if (inrun) runlen += 1; else { inrun = 1; runlen = 1; } } else if (inrun) { endrun = 1; inrun = 0; } if (f == FONT_Y - 1 && inrun) { endrun = 1; // need the +1 b/c we this code is normally // only triggered when f == FONT_Y, due to the // edge-triggered nature of this algorithm f += 1; } if (endrun) { fillRect(x+i*size, y+(f-runlen)*size, size, runlen*size, color); inrun = 0; runlen = 0; endrun = 0; } } } } //--------------------------------------------------------------------------------------- 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); *string++; x += FONT_SPACE*size; if(x >= _width-1){ y += FONT_Y*size; x = 0; } } } //--------------------------------------------------------------------------------------- void SGL::drawBitMap(uint8_t x, uint8_t y, const uint8_t *bitmap, uint8_t width, uint8_t height, uint16_t color) { 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) { fillRect(0, 0, _width, _height, color); }