displaying on SSD1306, 128x64 pixels OLED

Dependencies:   microbit

OLED.h

Committer:
bvirk
Date:
2020-02-18
Revision:
4:19da6ea94042
Parent:
3:f36427797fd7
Child:
6:c69f08f464b5

File content as of revision 4:19da6ea94042:

#include "MicroBit.h"
#include <vector>
#include "cppNorm.h"
#ifndef microbitOLED_OLED
#define microbitOLED_OLED

/**
  * Access to bit 0-7, named  by0 or x, and bit 8-15, named by1 or y 
  * of 16 bit named that16. Can be consructed with either two uint8_t or
  * a uint16_t
  */
union uint16 {
    struct {
    uint8_t by0;
    uint8_t by1;
    };
    struct {
    uint8_t x;
    uint8_t y;
    };
    uint16_t that16;
    uint16(uint16_t all) : that16(all) {}
    uint16(uint8_t byte1,uint8_t  byte0) : by1(byte1),by0(byte0) {}
    };

/**
  * Representation of a 128x64 pixels, SSD1306 compatibel OLED display
  */ 
class OLED {
    static const uint8_t chipAdress = 0x78; 
    static const uint8_t xOffset = 0;
    static const uint8_t yOffset = 0;
    
    unsigned char textArea[22][9];
    uint8_t charX;
    uint8_t charY;
    uint8_t displayWidth;
    uint8_t displayHeight;
    uint16_t screenSize;
    bool loadStarted;
    float loadPercent;
    bool pendingNewline;
    
    MicroBitI2C i2c;
    
    void init(uint8_t width, uint8_t height);
    void command(uint8_t cmd);
    void setTextArea(uint8_t chr, bool setLine8);
    
    
public:

    /**
      * ssd1306 compatible device of 128x64 pixels OLED display.
      * Init() must be called after construction.
      */
    OLED();

    /**
      * Initialisere display and clears it. A buffer representing charaters are 
      * filled with space characters
      */ 
    void init();
    
    /**
      * clears the display
      */
    void clear();

    /** 
      * Makes a newline by incrementing charY and assigning xOffset to charX
      * if charY becomes 8 the display screen is scrolled one line and charY
      * is decremented to 7. pendingNewline=false is done at last.
      */
    void newLine();
    
    /**
      * Put a char on display. A value in range [0x00,0xFF] i permitted -
      * 0,0xff and 0x20 are bitpatterns with all null pixels (like space)
      *
      * @param x horizontal pixelwise position, range [0,displayWidth - 6]
      * @param y vertical position, range [0,7]
      */
    void drawChar(uint8_t x, uint8_t y, uint8_t chr );
    
    /** 
      * Write a string at charX, charY position. Afterwards are
      * charX, charY set to the new starting point for writting
      *
      * @param str to be displayed at (charX, charY).   
      */
    void write(string str);
    
    /**
      * Write string at x posion 0 of charY. Afterwards are
      * charX, charY set to the new starting point for writting
      *
      * @param str to be displayed at (0, charY).
      */ 
    void crwrite(string str);
    
    /**
      * Write a string at charX, charY position. Afterwards the
      * pendingNewline state is set
      *
      * @param str to be displayed at (charX, charY).
      */ 
    void writeln(string str);

    /** 
      * Write a number at charX, charY position. Afterwards are
      * charX, charY set to the new starting point for writting
      *
      * @param number to be displayed at (charX, charY).   
      */
    void write(float number);
    
    /**
      * Write number at x posion 0 of charY. Afterwards are
      * charX, charY set to the new starting point for writting
      *
      * @param number to be displayed at (0, charY).
      */ 
    void crwrite(float number);

    /**
      * Write a string at charX, charY position. Afterwards the
      * pendingNewline state is set
      *
      * @param str to be displayed at (charX, charY).
      */ 
    void writeln(float number);
    
    /**
      * Scroll display one line up. An empty line will arise at bottom.
      */
    void scroll();
    
    void drawRectangle(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1);
    void drawShape(vector<uint16> & pixels);
    void drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1);
    void testOLED(MicroBit & uBit);
};
#endif