displaying on SSD1306, 128x64 pixels OLED
Diff: OLED.cpp
- Revision:
- 6:c69f08f464b5
- Parent:
- 4:19da6ea94042
- Child:
- 8:5972683a7190
--- a/OLED.cpp Tue Feb 18 15:40:28 2020 +0000 +++ b/OLED.cpp Sun Feb 23 01:57:35 2020 +0000 @@ -2,7 +2,6 @@ #include "MicroBit.h" #include "MicroBitI2C.h" -#include "mathExt.h" #include "OLEDGlobals.h" #include "OLED.h" #include "cppNorm.h" @@ -135,181 +134,29 @@ pendingNewline=false; } -void OLED::write(string str) { - if (pendingNewline) - newLine(); +void OLED::puts(string str) { for (uint16_t i = 0; i < str.length(); i++) { - if (charX > displayWidth - 6) { + if (str.charAt(i) == '\r') charX = xOffset; - charY++; + else if (str.charAt(i) == '\n') { + if (pendingNewline) + newLine(); + pendingNewline=true; + } else { + if (pendingNewline || charX > displayWidth - 6) + newLine(); + drawChar(charX, charY, (uint8_t)str.charAt(i)); + charX += 6; } - drawChar(charX, charY, (uint8_t)str.charAt(i)); - charX += 6; } } -void OLED::crwrite(string str) { - charX=xOffset; - write(str); -} - -void OLED::writeln(string str) { - write(str); - pendingNewline=true; -} - -void OLED::write(float number) { - write(ftos(number)); -} - -void OLED::crwrite(float number) { - charX=xOffset; - write(ftos(number)); -} - -void OLED::writeln(float number) { - write(number); - pendingNewline=true; - -} - -/* dont works -void OLED::drawShape(vector<xyPair> & pixels) { - uint8_t x1 = displayWidth; - uint8_t y1 = displayHeight * 8; - uint8_t x2 = 0; - uint8_t y2 = 0; - for (int8_t i = 0; i < pixels.size(); i++) { - if (pixels[i].x < x1) - x1 = pixels[i].x; - if (pixels[i].x > x2) - x2 = pixels[i].x; - if (pixels[i].y < y1) - y1 = pixels[i].y; - if (pixels[i].y > y2) - y2 = pixels[i].y; - } - uint8_t page1 = y1 / 8; - uint8_t page2 = y2 / 8; - char line[2]; - line[0] = 0x40; - for (uint8_t x = x1; x <= x2; x++) { - for (uint8_t page = page1; page <= page2; page++) { - line[1] = 0x00; - for (uint8_t i = 0; i < pixels.size(); i++) - if (pixels[i].x == x) - if (pixels[i].y / 8 == page) - line[1] |= 1 << (pixels[i].y % 8); - if (line[1] != 0x00) { - command(SSD1306_SETCOLUMNADRESS); - command(x); - command(x + 1); - command(SSD1306_SETPAGEADRESS); - command(page); - command(page + 1); - //line[1] |= pins.i2cReadBuffer(chipAdress, 2)[1] - i2c.write(chipAdress, line, 2, false); - } - } // for page - } // for x +uint8_t OLED::printf(const char * fmt, ...) { + va_list args; + va_start(args, fmt); + uint8_t len = min(vsnprintf(printf_text, printf_textSize, fmt, args), printf_textSize); + va_end(args); + printf_text[len] = '\0'; + puts(printf_text); + return len; } - -void OLED::drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) { - vector<xyPair> pixels; - uint8_t kx, ky, c, i; - uint8_t targetX = x1; - uint8_t targetY = y1; - x1 -= x0; kx = 0; - if (x1 > 0) - kx = +1; - if (x1 < 0) { - kx = -1; - x1 = -x1; - } - x1++; - y1 -= y0; ky = 0; - if (y1 > 0) - ky = +1; - if (y1 < 0) { - ky = -1; - y1 = -y1; - } - y1++; - if (x1 >= y1) { - c = x1; - for (i = 0; i < x1; i++ , x0 += kx) { - pixels.push_back(xyPair(x0, y0)); - c -= y1; - if (c <= 0) { - if (i != x1 - 1) - pixels.push_back(xyPair(x0 + kx, y0)); - c += x1; y0 += ky; - if (i != x1 - 1) - pixels.push_back(xyPair(x0, y0)); - } - if (pixels.size() > 20) { - drawShape(pixels); - pixels.clear(); - drawLine(x0, y0, targetX, targetY); - return; - } - } - } else { - c = y1; - for (i = 0; i < y1; i++ , y0 += ky) { - pixels.push_back(xyPair(x0, y0)); - c -= x1; - if (c <= 0) { - if (i != y1 - 1) - pixels.push_back(xyPair(x0, y0 + ky)); - c += y1; - x0 += kx; - if (i != y1 - 1) - pixels.push_back(xyPair(x0, y0)); - } - if (pixels.size() > 20) { - drawShape(pixels); - pixels.clear(); - drawLine(x0, y0, targetX, targetY); - return; - } - } - } - drawShape(pixels); -} -void OLED::drawRectangle(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) { - drawLine(x0, y0, x1, y0); - drawLine(x0, y1, x1, y1); - drawLine(x0, y0, x0, y1); - drawLine(x1, y0, x1, y1); - } */ - -void OLED::testOLED(MicroBit & uBit) { - writeln("the quick brown fox jumped over the lazy dog?"); - uBit.sleep(2000); - clear(); - writeln("THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG!"); - uBit.sleep(2000); - clear(); - write("Your magic number is "); - write(23 * 3); - writeln("!"); - writeln(1.4); - writeln(12); - uBit.sleep(2000); - writeln(2.1); - uBit.sleep(2000); - writeln(3); - /* - basic.pause(1000) - for (let i = 0; i < 100; i++) { - oled.drawLoading(i) - } - basic.pause(1000) - oled.clear() - oled.drawRectangle(10, 10, 60, 60) - oled.drawLine(0, 0, 128, 64) - oled.drawLine(0, 64, 128, 0) - */ -} -