SSD1306 LCD 96x16 model
Dependents: testSoftware8_alles_display
Fork of ssd1306_library by
Diff: ssd1306.cpp
- Revision:
- 3:bb6fba3e84ff
- Parent:
- 2:7f1160c1a741
- Child:
- 4:35757c8b7625
--- a/ssd1306.cpp Thu Oct 26 16:07:21 2017 +0000 +++ b/ssd1306.cpp Wed Nov 01 11:04:20 2017 +0000 @@ -1,3 +1,14 @@ +/* +* ssd1306.cpp +* +* Created on: 20 oct. 2017 +* Author: Miguel Angel Rodriguez Jodar +* +* Based upon Adafruit library. +* GPL licensed. +* +*/ + #include "ssd1306.h" #include "mbed.h" @@ -210,7 +221,7 @@ }; -SSD1306::SSD1306 (PinName sda, PinName scl) +SSD1306::SSD1306 (PinName sda, PinName scl, char ssd1306_addr) { idxfb = 0; fb = new char[1024]; @@ -220,21 +231,25 @@ } bus = new I2C (sda, scl); - ssd1306_i2c_addr = scan(); - if (ssd1306_i2c_addr != 0x78 && ssd1306_i2c_addr != 0x7A) { - printf ("SSD1306: slave not found!. "); - if (ssd1306_i2c_addr != 0) - printf ("Found I2C slave at 0x%02.2X\r\n", ssd1306_i2c_addr); - else - printf ("\r\n"); - for (;;) { } - } + ssd1306_i2c_addr = ssd1306_addr; + do_not_delete_bus = 0; #ifdef SSD1306_DEBUG printf ("SSD1306 debug: fb = 0x%08.8X\r\n", fb); printf ("SSD1306 debug: I2C addr = 0x%02.2X\r\n", ssd1306_i2c_addr); #endif +} - +SSD1306::SSD1306 (I2C &busi2c, char ssd1306_addr) +{ + bus = &busi2c; + idxfb = 0; + fb = new char[1024]; + if (!fb) { + printf ("SSD1306: Framebuffer allocation failed!\r\n"); + for (;;) { } + } + ssd1306_i2c_addr = ssd1306_addr; + do_not_delete_bus = 1; } void SSD1306::speed (I2CSpeed spd) @@ -295,9 +310,17 @@ int SSD1306::init (void) { - static char comando[] = {0x80, SSD1306_DISPLAYOFF, 0x80, SSD1306_CHARGEPUMP, 0x80, 0x14, 0x80, SSD1306_MEMORYMODE, 0x80, 0x00, 0x80, SSD1306_SEGREMAP | 0x1, - 0x80, SSD1306_COMSCANDEC, 0x80, SSD1306_SETCONTRAST, 0x80, 0x7F, 0x00, SSD1306_DISPLAYON - }; + static const char comando[] = {0x80, SSD1306_DISPLAYOFF, + 0x80, SSD1306_CHARGEPUMP, + 0x80, 0x14, + 0x80, SSD1306_MEMORYMODE, + 0x80, 0x00, + 0x80, SSD1306_SEGREMAP | 0x1, + 0x80, SSD1306_COMSCANDEC, + 0x80, SSD1306_SETCONTRAST, + 0x80, 0x7F, + 0x00, SSD1306_DISPLAYON + }; return bus->write (ssd1306_i2c_addr, comando, sizeof comando); } @@ -341,6 +364,19 @@ while (*s) putchar (*s++, refresh); } +void SSD1306::printf (char *fmt,...) +{ + char *s = new char[129]; + va_list args; + + va_start (args, fmt); + vsnprintf (s, 128, fmt, args); + puts (s, false); + delete[] s; + va_end (args); +} + + void SSD1306::display(void) { int i; @@ -385,7 +421,7 @@ { x = x % 128; y = y % 64; - + switch (modo) { case Normal: fb[(y/8)*128+x] |= (1<<(y%8)); @@ -401,6 +437,17 @@ display(); } +bool SSD1306::point (char x, char y) +{ + x = x % 128; + y = y % 64; + + if ( fb[(y/8)*128+x] & (1<<(y%8)) ) + return true; + else + return false; +} + void SSD1306::line (char x0, char y0, char x1, char y1, PlotStyle modo, bool refresh) { int dx = abs (x1 - x0), sx = x0 < x1 ? 1 : -1; @@ -424,3 +471,39 @@ if (refresh) display(); } + +void SSD1306::circle (char x0, char y0, char r, PlotStyle modo, bool refresh) +{ + int x = r-1; + int y = 0; + int dx = 1; + int dy = 1; + int err = dx - 2*r; + + while (x >= y) + { + plot (x0 + x, y0 + y, modo); + plot (x0 + y, y0 + x, modo); + plot (x0 - y, y0 + x, modo); + plot (x0 - x, y0 + y, modo); + plot (x0 - x, y0 - y, modo); + plot (x0 - y, y0 - x, modo); + plot (x0 + y, y0 - x, modo); + plot (x0 + x, y0 - y, modo); + + if (err <= 0) + { + y++; + err += dy; + dy += 2; + } + if (err > 0) + { + x--; + dx += 2; + err += dx - 2*r; + } + } + if (refresh) + display(); +}