displaying on SSD1306, 128x64 pixels OLED

Dependencies:   microbit

Revision:
3:f36427797fd7
Child:
4:19da6ea94042
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OLED.cpp	Sat Feb 15 23:05:29 2020 +0000
@@ -0,0 +1,166 @@
+
+#include "MicroBit.h"
+#include "MicroBitI2C.h"
+
+#include "mathExt.h"
+#include "OLEDGlobals.h"
+#include "OLED.h"
+ 
+OLED::OLED() : 
+     i2c(I2C_SDA0,I2C_SCL0)
+    ,charX(0)
+    ,charY(0)
+    ,displayWidth(128)
+    ,displayHeight(64 / 8)
+    ,screenSize(0) {
+    init(128, 64);
+    clear();
+}
+
+void OLED::command(uint8_t cmd) {
+    char buf[2];
+    buf[0] = '\0';
+    buf[1] = cmd;
+    i2c.write(chipAdress,buf,2);
+}
+
+void OLED::init(uint8_t width, uint8_t height) {
+    command(SSD1306_DISPLAYOFF);
+    command(SSD1306_SETDISPLAYCLOCKDIV);
+    command(0x80);                                  // the suggested ratio 0x80
+    command(SSD1306_SETMULTIPLEX);
+    command(0x3F);
+    command(SSD1306_SETDISPLAYOFFSET);
+    command(0x0);                                   // no offset
+    command(SSD1306_SETSTARTLINE | 0x0);            // line #0
+    command(SSD1306_CHARGEPUMP);
+    command(0x14);
+    command(SSD1306_MEMORYMODE);
+    command(0x00);                                  // 0x0 act like ks0108
+    command(SSD1306_SEGREMAP | 0x1);
+    command(SSD1306_COMSCANDEC);
+    command(SSD1306_SETCOMPINS);
+    command(0x12);
+    command(SSD1306_SETCONTRAST);
+    command(0xCF);
+    command(SSD1306_SETPRECHARGE);
+    command(0xF1);
+    command(SSD1306_SETVCOMDETECT);
+    command(0x40);
+    command(SSD1306_DISPLAYALLON_RESUME);
+    command(SSD1306_NORMALDISPLAY);
+    command(SSD1306_DISPLAYON);
+    displayWidth = width;
+    displayHeight = height / 8;
+    screenSize = displayWidth * displayHeight;
+    charX = xOffset;
+    charY = yOffset;
+    loadStarted = false;
+    loadPercent = 0;
+    clear();
+}
+
+void OLED::clear() {
+    loadStarted = false;
+    loadPercent = 0;
+    command(SSD1306_SETCOLUMNADRESS);
+    command(0x00);
+    command(displayWidth - 1);
+    command(SSD1306_SETPAGEADRESS);
+    command(0x00);
+    command(displayHeight - 1);
+    char data[17];
+    data[0] = 0x40; // Data Mode;
+    for (int8_t i = 1; i < 17; i++) 
+        data[i] = 0x00;
+    // send display buffer in 16 byte chunks;
+    for (int16_t  i = 0; i < screenSize; i += 16) { 
+        i2c.write(chipAdress, data, 17,false);
+    }
+    charX = xOffset;
+    charY = yOffset;
+}
+
+//void OLED::newLine() {
+//    charY++;charX = xOffset;
+//}
+
+void OLED::setCharTable(unsigned int chr, unsigned char *tegnbits) {
+    for (int8_t p=0;  p<5; p++) 
+        tegnbits[p] = font[chr][p];
+}
+
+void OLED::drawChar(uint8_t x, uint8_t y, uint8_t chr ) {
+    command(SSD1306_SETCOLUMNADRESS);
+    command(x);
+    command(x + 5);
+    command(SSD1306_SETPAGEADRESS);
+    command(y);
+    command(y + 1);
+    char line[2];
+    line[0] = 0x40;
+    for (int8_t i = 0; i < 6; i++) {
+        if (i == 5) 
+            line[1] = 0x00;
+        else 
+            line[1] = font[chr][i];
+        i2c.write(chipAdress, line, 2, false);
+    }
+}
+
+void OLED::write(ManagedString str) {
+    for (uint16_t i = 0; i < str.length(); i++) {
+        if (charX > displayWidth - 6) {
+            charY++;
+            charX = xOffset;
+            }
+
+        drawChar(charX, charY, (uint8_t)str.charAt(i));
+        charX += 6;
+    }
+}
+void OLED::writeln(ManagedString str) {
+    write(str);
+    charY++;charX = xOffset;
+
+}
+ 
+void OLED::write(float number) {
+    write(ftos(number));
+}
+
+void OLED::writeln(float number) {
+    write(number);
+    charY++;charX = xOffset;
+
+}
+
+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)
+    */
+}
+