Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

split off a superclass

  • Loading branch information...
commit 80815ef32bdd2bb904bc2543cf8408b51f0fcdc2 1 parent e459ef2
@ladyada ladyada authored
View
454 Adafruit_GP9002.cpp
@@ -10,339 +10,227 @@ static uint8_t is_reversed = 0;
// a 5x7 font table
extern uint8_t PROGMEM font[];
+Adafruit_GP9002::Adafruit_GP9002(int8_t SCLK, int8_t MISO, int8_t MOSI,
+ int8_t CS, int8_t DC) {
+ _sclk = SCLK;
+ _miso = MISO;
+ _mosi = MOSI;
+ _cs = CS;
+ _dc = DC;
-void Adafruit_GP9002::drawbitmap(uint8_t x, uint8_t y,
- const uint8_t *bitmap, uint8_t w, uint8_t h,
- uint8_t color) {
- for (uint8_t j=0; j<h; j++) {
- for (uint8_t i=0; i<w; i++ ) {
- if (pgm_read_byte(bitmap + i + (j/8)*w) & _BV(j%8)) {
- setpixel(x+i, y+j, color);
- }
- }
- }
+ WIDTH = 128;
+ HEIGHT = 64;
}
-void Adafruit_GP9002::drawstring(uint8_t x, uint8_t line, char *c) {
+
+void Adafruit_GP9002::drawString(uint8_t x, uint8_t line, char *c) {
while (c[0] != 0) {
- drawchar(x, line, c[0]);
+ drawChar(x, line, c[0]);
c++;
x += 6; // 6 pixels wide
- if (x + 6 >= Adafruit_GP9002_LCDWIDTH) {
+ if (x + 6 >= this->WIDTH) {
x = 0; // ran out of this line
line++;
}
- if (line >= (Adafruit_GP9002_LCDHEIGHT/8))
+ if (line >= (this->HEIGHT/8))
return; // ran out of space :(
}
}
-void Adafruit_GP9002::drawchar(uint8_t x, uint8_t line, uint8_t c) {
- if ((line >= Adafruit_GP9002_LCDHEIGHT/8) || (x >= (Adafruit_GP9002_LCDWIDTH - 6)))
+void Adafruit_GP9002::drawChar(uint8_t x, uint8_t line, uint8_t c) {
+ if ((line >= this->HEIGHT/8) || (x >= (this->WIDTH - 6)))
return;
for (uint8_t i =0; i<5; i++ ) {
- buffer[x + (line*128) ] = pgm_read_byte(font+(c*5)+i);
+ //buffer[x + (line*128) ] = pgm_read_byte(font+(c*5)+i);
x++;
}
}
-// bresenham's algorithm - thx wikpedia
-void Adafruit_GP9002::drawline(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1,
- uint8_t color) {
- uint8_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);
+// updated high speed drawing!
+void Adafruit_GP9002::drawFastVLine(uint8_t x, uint8_t orig_y, uint8_t h, uint8_t color) {
+
+ while (orig_y % 8) {
+ drawPixel(x, orig_y, color);
+ orig_y++;
+ h--;
+ }
+
+ // calculate addr
+ uint16_t addr = 0;
+ addr = x*8;
+ addr += orig_y/8;
+
+ command(GP9002_ADDRINCR);
+ command(GP9002_ADDRL);
+ data(addr & 0xFF);
+ command(GP9002_ADDRH);
+ data(addr >> 8);
+ command(GP9002_DATAWRITE);
+ while (h >= 8) {
+ // draw 8 pixels at once!
+ if (color)
+ data(0xFF);
+ else
+ data(0x00);
+ h -= 8;
+ orig_y += 8;
}
+ command(GP9002_ADDRHELD);
- uint8_t dx, dy;
- dx = x1 - x0;
- dy = abs(y1 - y0);
-
- int8_t err = dx / 2;
- int8_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;
- }
+ while (h) {
+ drawPixel(x, orig_y, color);
+ orig_y++;
+ h--;
}
}
-// filled rectangle
-void Adafruit_GP9002::fillrect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
- uint8_t color) {
- // stupidest version - just pixels - but fast with internal buffer!
- for (uint8_t i=x; i<x+w; i++) {
- for (uint8_t j=y; j<y+h; j++) {
- setpixel(i, j, color);
- }
- }
-}
-// draw a rectangle
-void Adafruit_GP9002::drawrect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
- uint8_t color) {
- // stupidest version - just pixels - but fast with internal buffer!
- for (uint8_t i=x; i<x+w; i++) {
- setpixel(i, y, color);
- setpixel(i, y+h-1, color);
- }
- for (uint8_t i=y; i<y+h; i++) {
- setpixel(x, i, color);
- setpixel(x+w-1, i, color);
- }
-}
-// draw a circle outline
-void Adafruit_GP9002::drawcircle(uint8_t x0, uint8_t y0, uint8_t r,
- uint8_t color) {
- int8_t f = 1 - r;
- int8_t ddF_x = 1;
- int8_t ddF_y = -2 * r;
- int8_t x = 0;
- int8_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);
-
- }
-}
-void Adafruit_GP9002::fillcircle(uint8_t x0, uint8_t y0, uint8_t r,
- uint8_t color) {
- int8_t f = 1 - r;
- int8_t ddF_x = 1;
- int8_t ddF_y = -2 * r;
- int8_t x = 0;
- int8_t y = r;
-
- for (uint8_t i=y0-r; i<=y0+r; i++) {
- setpixel(x0, i, color);
- }
-
- while (x<y) {
- if (f >= 0) {
- y--;
- ddF_y += 2;
- f += ddF_y;
- }
- x++;
- ddF_x += 2;
- f += ddF_x;
-
- for (uint8_t i=y0-y; i<=y0+y; i++) {
- setpixel(x0+x, i, color);
- setpixel(x0-x, i, color);
- }
- for (uint8_t i=y0-x; i<=y0+x; i++) {
- setpixel(x0+y, i, color);
- setpixel(x0-y, i, color);
- }
- }
-}
// the most basic function, set a single pixel
-void Adafruit_GP9002::setpixel(uint8_t x, uint8_t y, uint8_t color) {
- if ((x >= Adafruit_GP9002_LCDWIDTH) || (y >= Adafruit_GP9002_LCDHEIGHT))
+void Adafruit_GP9002::drawPixel(uint8_t x, uint8_t y, uint8_t color) {
+ if ((x >= this->WIDTH) || (y >= this->HEIGHT))
return;
- // x is which column
- if (color == WHITE)
- buffer[x+ (y/8)*Adafruit_GP9002_LCDWIDTH] |= _BV((y%8));
- else
- buffer[x+ (y/8)*Adafruit_GP9002_LCDWIDTH] &= ~_BV((y%8));
-}
-
-void Adafruit_GP9002::ssd1306_init(uint8_t vccstate) {
- // set pin directions
- pinMode(sid, OUTPUT);
- pinMode(sclk, OUTPUT);
- pinMode(dc, OUTPUT);
- pinMode(rst, OUTPUT);
- pinMode(cs, OUTPUT);
-
- digitalWrite(rst, HIGH);
- // VDD (3.3V) goes high at start, lets just chill for a ms
- delay(1);
- // bring0xset low
- digitalWrite(rst, LOW);
- // wait 10ms
- delay(10);
- // bring out of reset
- digitalWrite(rst, HIGH);
- // turn on VCC (9V?)
-
- #if defined Adafruit_GP9002_128_32
- // Init sequence for 128x32 OLED module
- ssd1306_command(Adafruit_GP9002_DISPLAYOFF); // 0xAE
- ssd1306_command(Adafruit_GP9002_SETDISPLAYCLOCKDIV); // 0xD5
- ssd1306_command(0x80); // the suggested ratio 0x80
- ssd1306_command(Adafruit_GP9002_SETMULTIPLEX); // 0xA8
- ssd1306_command(0x1F);
- ssd1306_command(Adafruit_GP9002_SETDISPLAYOFFSET); // 0xD3
- ssd1306_command(0x0); // no offset
- ssd1306_command(Adafruit_GP9002_SETSTARTLINE | 0x0); // line #0
- ssd1306_command(Adafruit_GP9002_CHARGEPUMP); // 0x8D
- if (vccstate == Adafruit_GP9002_EXTERNALVCC)
- { ssd1306_command(0x10); }
- else
- { ssd1306_command(0x14); }
- ssd1306_command(Adafruit_GP9002_MEMORYMODE); // 0x20
- ssd1306_command(0x00); // 0x0 act like ks0108
- ssd1306_command(Adafruit_GP9002_SEGREMAP | 0x1);
- ssd1306_command(Adafruit_GP9002_COMSCANDEC);
- ssd1306_command(Adafruit_GP9002_SETCOMPINS); // 0xDA
- ssd1306_command(0x02);
- ssd1306_command(Adafruit_GP9002_SETCONTRAST); // 0x81
- ssd1306_command(0x8F);
- ssd1306_command(Adafruit_GP9002_SETPRECHARGE); // 0xd9
- if (vccstate == Adafruit_GP9002_EXTERNALVCC)
- { ssd1306_command(0x22); }
- else
- { ssd1306_command(0xF1); }
- ssd1306_command(Adafruit_GP9002_SETVCOMDETECT); // 0xDB
- ssd1306_command(0x40);
- ssd1306_command(Adafruit_GP9002_DISPLAYALLON_RESUME); // 0xA4
- ssd1306_command(Adafruit_GP9002_NORMALDISPLAY); // 0xA6
- #endif
-
- #if defined Adafruit_GP9002_128_64
- // Init sequence for 128x64 OLED module
- ssd1306_command(Adafruit_GP9002_DISPLAYOFF); // 0xAE
- ssd1306_command(Adafruit_GP9002_SETDISPLAYCLOCKDIV); // 0xD5
- ssd1306_command(0x80); // the suggested ratio 0x80
- ssd1306_command(Adafruit_GP9002_SETMULTIPLEX); // 0xA8
- ssd1306_command(0x3F);
- ssd1306_command(Adafruit_GP9002_SETDISPLAYOFFSET); // 0xD3
- ssd1306_command(0x0); // no offset
- ssd1306_command(Adafruit_GP9002_SETSTARTLINE | 0x0); // line #0
- ssd1306_command(Adafruit_GP9002_CHARGEPUMP); // 0x8D
- if (vccstate == Adafruit_GP9002_EXTERNALVCC)
- { ssd1306_command(0x10); }
- else
- { ssd1306_command(0x14); }
- ssd1306_command(Adafruit_GP9002_MEMORYMODE); // 0x20
- ssd1306_command(0x00); // 0x0 act like ks0108
- ssd1306_command(Adafruit_GP9002_SEGREMAP | 0x1);
- ssd1306_command(Adafruit_GP9002_COMSCANDEC);
- ssd1306_command(Adafruit_GP9002_SETCOMPINS); // 0xDA
- ssd1306_command(0x12);
- ssd1306_command(Adafruit_GP9002_SETCONTRAST); // 0x81
- if (vccstate == Adafruit_GP9002_EXTERNALVCC)
- { ssd1306_command(0x9F); }
- else
- { ssd1306_command(0xCF); }
- ssd1306_command(Adafruit_GP9002_SETPRECHARGE); // 0xd9
- if (vccstate == Adafruit_GP9002_EXTERNALVCC)
- { ssd1306_command(0x22); }
- else
- { ssd1306_command(0xF1); }
- ssd1306_command(Adafruit_GP9002_SETVCOMDETECT); // 0xDB
- ssd1306_command(0x40);
- ssd1306_command(Adafruit_GP9002_DISPLAYALLON_RESUME); // 0xA4
- ssd1306_command(Adafruit_GP9002_NORMALDISPLAY); // 0xA6
- #endif
+ uint8_t p;
- ssd1306_command(Adafruit_GP9002_DISPLAYON);//--turn on oled panel
-}
+ // calculate addr
+ uint16_t addr = 0;
+ addr = x*8;
+ addr += y/8;
-
-void Adafruit_GP9002::invert(uint8_t i) {
- if (i) {
- ssd1306_command(Adafruit_GP9002_INVERTDISPLAY);
- } else {
- ssd1306_command(Adafruit_GP9002_NORMALDISPLAY);
- }
+ // hold the address so we can read and then write
+ command(GP9002_ADDRHELD);
+
+ command(GP9002_ADDRL);
+ data(addr & 0xFF);
+ command(GP9002_ADDRH);
+ data(addr >> 8);
+
+ command(GP9002_DATAREAD);
+ data(0x0);
+ p = data(0x0);
+
+ p |= (1 << (7-(y % 8)));
+ command(GP9002_DATAWRITE);
+ data(p);
}
-inline void Adafruit_GP9002::spiwrite(uint8_t c) {
- shiftOut(sid, sclk, MSBFIRST, c);
+
+void Adafruit_GP9002::begin(void) {
+ // set pin directions
+ pinMode(_mosi, OUTPUT);
+ pinMode(_miso, INPUT);
+ pinMode(_sclk, OUTPUT);
+ pinMode(_dc, OUTPUT);
+ pinMode(_cs, OUTPUT);
-}
-void Adafruit_GP9002::ssd1306_command(uint8_t c) {
- digitalWrite(cs, HIGH);
- digitalWrite(dc, LOW);
- digitalWrite(cs, LOW);
- spiwrite(c);
- digitalWrite(cs, HIGH);
+ command(GP9002_DISPLAY);
+ data(GP9002_DISPLAY_MONOCHROME);
+ command(GP9002_OR);
+ command(GP9002_CLEARSCREEN);
+ command(GP9002_DISPLAY1ON);
}
-void Adafruit_GP9002::ssd1306_data(uint8_t c) {
- digitalWrite(cs, HIGH);
- digitalWrite(dc, HIGH);
- digitalWrite(cs, LOW);
- spiwrite(c);
- digitalWrite(cs, HIGH);
-}
-void Adafruit_GP9002::ssd1306_set_brightness(uint8_t val) {
+void Adafruit_GP9002::invert(uint8_t i) {
+ // command(GP9002_XOR);
+}
+
+uint8_t Adafruit_GP9002::spiTransfer(uint8_t d, uint8_t datacommand) {
+ uint8_t reply = 0;
+
+ volatile uint8_t *sclkportreg, *mosiportreg, *misoportreg;
+ uint8_t sclkpin, mosipin, misopin;
+
+ digitalWrite(_dc, datacommand);
+ digitalWrite(_cs, LOW);
+
+ sclkportreg = portOutputRegister(digitalPinToPort(_sclk));
+ sclkpin = digitalPinToBitMask(_sclk);
+ mosiportreg = portOutputRegister(digitalPinToPort(_mosi));
+ mosipin = digitalPinToBitMask(_mosi);
+ misoportreg = portInputRegister(digitalPinToPort(_miso));
+ misopin = digitalPinToBitMask(_miso);
+
+ for (uint8_t i=0; i<8; i++) {
+ //digitalWrite(_sclk, LOW);
+ *sclkportreg &= ~sclkpin;
+ if (d & _BV(i)) {
+ //digitalWrite(_mosi, HIGH);
+ *mosiportreg |= mosipin;
+ } else {
+ //digitalWrite(_mosi, LOW);
+ *mosiportreg &= ~mosipin;
+ }
+ //delayMicroseconds(10);
+ //digitalWrite(_sclk, HIGH);
+ *sclkportreg |= sclkpin;
+
+ if (digitalRead(_miso))
+ //if ((*misoportreg) & misopin)
+ reply |= _BV(i);
+ delayMicroseconds(5);
+ }
+ digitalWrite(_cs, HIGH);
+ return reply;
}
-void Adafruit_GP9002::display(void) {
- ssd1306_command(Adafruit_GP9002_SETLOWCOLUMN | 0x0); // low col = 0
- ssd1306_command(Adafruit_GP9002_SETHIGHCOLUMN | 0x0); // hi col = 0
- ssd1306_command(Adafruit_GP9002_SETSTARTLINE | 0x0); // line #0
+void Adafruit_GP9002::command(uint8_t d) {
+ volatile uint8_t *sclkportreg, *mosiportreg;
+ uint8_t sclkpin, mosipin;
- for (uint16_t i=0; i<(Adafruit_GP9002_LCDWIDTH*Adafruit_GP9002_LCDHEIGHT/8); i++) {
- ssd1306_data(buffer[i]);
- }
- // i wonder why we have to do this (check datasheet)
- if (Adafruit_GP9002_LCDHEIGHT == 32) {
- for (uint16_t i=0; i<(Adafruit_GP9002_LCDWIDTH*Adafruit_GP9002_LCDHEIGHT/8); i++) {
- ssd1306_data(0);
- }
- }
+ digitalWrite(_dc, HIGH);
+ digitalWrite(_cs, LOW);
+
+ /*
+ sclkportreg = portOutputRegister(digitalPinToPort(_sclk));
+ sclkpin = digitalPinToBitMask(_sclk);
+
+ mosiportreg = portOutputRegister(digitalPinToPort(_mosi));
+ mosipin = digitalPinToBitMask(_mosi);
+ */
+ for (uint8_t i=0; i<8; i++) {
+
+ digitalWrite(_sclk, LOW);
+ //*sclkportreg &= ~sclkpin;
+ if (d & _BV(i)) {
+ digitalWrite(_mosi, HIGH);
+ //*mosiportreg |= mosipin;
+ } else {
+ digitalWrite(_mosi, LOW);
+ //*mosiportreg &= ~mosipin;
+ }
+ digitalWrite(_sclk, HIGH);
+ //*sclkportreg |= sclkpin;
+ }
+ digitalWrite(_cs, HIGH);
+}
+
+uint8_t Adafruit_GP9002::data(uint8_t d) {
+ return spiTransfer(d, LOW);
+}
+
+void Adafruit_GP9002::setBrightness(uint8_t val) {
+
}
// clear everything
-void Adafruit_GP9002::clear(void) {
+void Adafruit_GP9002::clearDisplay(void) {
+ command(GP9002_CLEARSCREEN);
}
-void Adafruit_GP9002::clear_display(void) {
-
+void Adafruit_GP9002::displayOff(void) {
+ command(GP9002_DISPLAYSOFF);
}
+void Adafruit_GP9002::displayOn(void) {
+ command(GP9002_DISPLAY1ON);
+}
+
+
View
115 Adafruit_GP9002.h
@@ -1,87 +1,72 @@
+#ifndef _ADAFRUIT_GP9002_H
+#define _ADAFRUIT_GP9002_H
+
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
-#define swap(a, b) { uint8_t t = a; a = b; b = t; }
+#include "Adafruit_MonoGfx.h"
#define BLACK 0
#define WHITE 1
- #define GP9002_LCDWIDTH 128
- #define GP9002_LCDHEIGHT 64
-
-#define GP9002_SETCONTRAST 0x81
-#define GP9002_DISPLAYALLON_RESUME 0xA4
-#define GP9002_DISPLAYALLON 0xA5
-#define GP9002_NORMALDISPLAY 0xA6
-#define GP9002_INVERTDISPLAY 0xA7
-#define GP9002_DISPLAYOFF 0xAE
-#define GP9002_DISPLAYON 0xAF
-
-#define GP9002_SETDISPLAYOFFSET 0xD3
-#define GP9002_SETCOMPINS 0xDA
-
-#define GP9002_SETVCOMDETECT 0xDB
-
-#define GP9002_SETDISPLAYCLOCKDIV 0xD5
-#define GP9002_SETPRECHARGE 0xD9
-
-#define GP9002_SETMULTIPLEX 0xA8
-
-#define GP9002_SETLOWCOLUMN 0x00
-#define GP9002_SETHIGHCOLUMN 0x10
-
-#define GP9002_SETSTARTLINE 0x40
-
-#define GP9002_MEMORYMODE 0x20
-
-#define GP9002_COMSCANINC 0xC0
-#define GP9002_COMSCANDEC 0xC8
-
-#define GP9002_SEGREMAP 0xA0
-
-#define GP9002_CHARGEPUMP 0x8D
-
-#define GP9002_EXTERNALVCC 0x1
-#define GP9002_SWITCHCAPVCC 0x2
-
-class Adafruit_GP9002 {
+#define GP9002_DISPLAYSOFF 0x00
+#define GP9002_DISPLAY1ON 0x01
+#define GP9002_DISPLAY2ON 0x02
+#define GP9002_ADDRINCR 0x04
+#define GP9002_ADDRHELD 0x05
+#define GP9002_CLEARSCREEN 0x06
+#define GP9002_CONTROLPOWER 0x07
+#define GP9002_DATAWRITE 0x08
+#define GP9002_DATAREAD 0x09
+#define GP9002_LOWERADDR1 0x0A
+#define GP9002_HIGHERADDR1 0x0B
+#define GP9002_LOWERADDR2 0x0C
+#define GP9002_HIGHERADDR1 0x1D
+#define GP9002_ADDRL 0x0E
+#define GP9002_ADDRH 0x0F
+#define GP9002_OR 0x10
+#define GP9002_XOR 0x11
+#define GP9002_AND 0x12
+#define GP9002_BRIGHT 0x13
+#define GP9002_DISPLAY 0x14
+#define GP9002_DISPLAY_MONOCHROME 0x10
+#define GP9002_DISPLAY_GRAYSCALE 0x14
+#define GP9002_INTMODE 0x15
+#define GP9002_DRAWCHAR 0x20
+#define GP9002_CHARRAM 0x21
+#define GP9002_CHARSIZE 0x22
+#define GP9002_CHARBRIGHT 0x24
+
+
+class Adafruit_GP9002 : public Adafruit_MonoGfx {
public:
- Adafruit_GP9002(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS) :sid(SID), sclk(SCLK), dc(DC), rst(RST), cs(CS) {}
- Adafruit_GP9002(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST) :sid(SID), sclk(SCLK), dc(DC), rst(RST), cs(-1) {}
-
+ Adafruit_GP9002(int8_t SCLK, int8_t MISO, int8_t MOSI,
+ int8_t CS, int8_t DC);
- void init(uint8_t switchvcc);
+ // particular to this display
+ void begin(void);
+ uint8_t spiTransfer(uint8_t d, uint8_t datacommand);
void command(uint8_t c);
- void data(uint8_t c);
+ uint8_t data(uint8_t c);
void setBrightness(uint8_t val);
- void clearDisplay(void);
- void clear();
void invert(uint8_t i);
- void display();
- void setpixel(uint8_t x, uint8_t y, uint8_t color);
- void fillCircle(uint8_t x0, uint8_t y0, uint8_t r,
- uint8_t color);
- void drawCircle(uint8_t x0, uint8_t y0, uint8_t r,
- uint8_t color);
- void drawRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
- uint8_t color);
- void fillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
- uint8_t color);
- void drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1,
- uint8_t color);
+ void displayOn();
+ void displayOff();
+ void clearDisplay(void);
+
+ void drawPixel(uint8_t x, uint8_t y, uint8_t color);
+ void drawFastVLine(uint8_t x, uint8_t y, uint8_t h, uint8_t color);
+
void drawChar(uint8_t x, uint8_t line, uint8_t c);
void drawString(uint8_t x, uint8_t line, char *c);
- void drawBitmap(uint8_t x, uint8_t y,
- const uint8_t *bitmap, uint8_t w, uint8_t h,
- uint8_t color);
-
private:
- int8_t sid, sclk, dc, rst, cs;
+ int8_t _miso, _mosi, _sclk, _dc, _cs;
void spiwrite(uint8_t c);
-
};
+
+#endif
View
153 Adafruit_MonoGfx.cpp
@@ -0,0 +1,153 @@
+#include "Adafruit_MonoGfx.h"
+
+// draw a circle outline
+void Adafruit_MonoGfx::drawCircle(uint8_t x0, uint8_t y0, uint8_t r,
+ uint8_t color) {
+ int8_t f = 1 - r;
+ int8_t ddF_x = 1;
+ int8_t ddF_y = -2 * r;
+ int8_t x = 0;
+ int8_t y = r;
+
+ drawPixel(x0, y0+r, color);
+ drawPixel(x0, y0-r, color);
+ drawPixel(x0+r, y0, color);
+ drawPixel(x0-r, y0, color);
+
+ while (x<y) {
+ if (f >= 0) {
+ y--;
+ ddF_y += 2;
+ f += ddF_y;
+ }
+ x++;
+ ddF_x += 2;
+ f += ddF_x;
+
+ drawPixel(x0 + x, y0 + y, color);
+ drawPixel(x0 - x, y0 + y, color);
+ drawPixel(x0 + x, y0 - y, color);
+ drawPixel(x0 - x, y0 - y, color);
+
+ drawPixel(x0 + y, y0 + x, color);
+ drawPixel(x0 - y, y0 + x, color);
+ drawPixel(x0 + y, y0 - x, color);
+ drawPixel(x0 - y, y0 - x, color);
+
+ }
+}
+
+void Adafruit_MonoGfx::fillCircle(uint8_t x0, uint8_t y0, uint8_t r,
+ uint8_t color) {
+ int8_t f = 1 - r;
+ int8_t ddF_x = 1;
+ int8_t ddF_y = -2 * r;
+ int8_t x = 0;
+ int8_t y = r;
+
+ for (uint8_t i=y0-r; i<=y0+r; i++) {
+ drawPixel(x0, i, color);
+ }
+
+ while (x<y) {
+ if (f >= 0) {
+ y--;
+ ddF_y += 2;
+ f += ddF_y;
+ }
+ x++;
+ ddF_x += 2;
+ f += ddF_x;
+
+ for (uint8_t i=y0-y; i<=y0+y; i++) {
+ drawPixel(x0+x, i, color);
+ drawPixel(x0-x, i, color);
+ }
+ for (uint8_t i=y0-x; i<=y0+x; i++) {
+ drawPixel(x0+y, i, color);
+ drawPixel(x0-y, i, color);
+ }
+ }
+}
+
+// bresenham's algorithm - thx wikpedia
+void Adafruit_MonoGfx::drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1,
+ uint8_t color) {
+ uint8_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);
+ }
+
+ uint8_t dx, dy;
+ dx = x1 - x0;
+ dy = abs(y1 - y0);
+
+ int8_t err = dx / 2;
+ int8_t ystep;
+
+ if (y0 < y1) {
+ ystep = 1;
+ } else {
+ ystep = -1;}
+
+ for (; x0<x1; x0++) {
+ if (steep) {
+ drawPixel(y0, x0, color);
+ } else {
+ drawPixel(x0, y0, color);
+ }
+ err -= dy;
+ if (err < 0) {
+ y0 += ystep;
+ err += dx;
+ }
+ }
+}
+
+
+// draw a rectangle
+void Adafruit_MonoGfx::drawRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
+ uint8_t color) {
+ // very stupid version, but works!
+ for (uint8_t i=x; i<x+w; i++) {
+ drawPixel(i, y, color);
+ drawPixel(i, y+h-1, color);
+ }
+ for (uint8_t i=y; i<y+h; i++) {
+ drawPixel(x, i, color);
+ drawPixel(x+w-1, i, color);
+ }
+}
+
+void Adafruit_MonoGfx::drawFastVLine(uint8_t x, uint8_t y, uint8_t h, uint8_t color) {
+ // stupidest version
+ for (uint8_t j=y; j<y+h; j++) {
+ drawPixel(x, j, color);
+ }
+}
+
+void Adafruit_MonoGfx::fillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
+ uint8_t color) {
+ // stupidest version
+ for (uint8_t i=x; i<x+w; i++) {
+ drawFastVLine(i, y, h, color);
+ }
+}
+
+void Adafruit_MonoGfx::drawBitmap(uint8_t x, uint8_t y,
+ const uint8_t *bitmap, uint8_t w, uint8_t h,
+ uint8_t color) {
+ for (uint8_t j=0; j<h; j++) {
+ for (uint8_t i=0; i<w; i++ ) {
+ if (pgm_read_byte(bitmap + i + (j/8)*w) & _BV(j%8)) {
+ drawPixel(x+i, y+j, color);
+ }
+ }
+ }
+}
View
46 Adafruit_MonoGfx.h
@@ -0,0 +1,46 @@
+#ifndef _ADAFRUIT_MONOGFX_H
+#define _ADAFRUIT_MONOGFX_H
+
+#if ARDUINO >= 100
+ #include "Arduino.h"
+#else
+ #include "WProgram.h"
+#endif
+
+#define swap(a, b) { uint8_t t = a; a = b; b = t; }
+
+class Adafruit_MonoGfx {
+ public:
+ // this must be defined by the subclass
+ virtual void drawPixel(uint8_t x, uint8_t y, uint8_t color);
+
+ // these are 'generic' drawing functions, so we can share them!
+ void drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1,
+ uint8_t color);
+ virtual void drawFastVLine(uint8_t x, uint8_t y, uint8_t h, uint8_t color);
+ void drawRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
+ uint8_t color);
+ void fillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
+ uint8_t color);
+
+ void drawCircle(uint8_t x0, uint8_t y0, uint8_t r,
+ uint8_t color);
+ void fillCircle(uint8_t x0, uint8_t y0, uint8_t r,
+ uint8_t color);
+
+ void drawBitmap(uint8_t x, uint8_t y,
+ const uint8_t *bitmap, uint8_t w, uint8_t h,
+ uint8_t color);
+
+
+ // return the size of the display
+ uint8_t width() { return WIDTH; }
+ uint8_t height() { return HEIGHT; }
+
+ protected:
+ uint8_t WIDTH, HEIGHT;
+
+
+};
+
+#endif
Please sign in to comment.
Something went wrong with that request. Please try again.