displaying on SSD1306, 128x64 pixels OLED

Dependencies:   microbit

OLED.h

Committer:
bvirk
Date:
2020-02-23
Revision:
6:c69f08f464b5
Parent:
4:19da6ea94042
Child:
8:5972683a7190

File content as of revision 6:c69f08f464b5:

#include "MicroBit.h"
#include <vector>
#include "stdarg.h"
#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 );
    
    /** 
      * Writes a string at charX, charY position. Afterwards are
      * charX, charY set to the new starting point for writting
      * \n and \r is processed - \n includes \r  functionality
      *
      * @param str to be displayed at (charX, charY).   
      */
    void puts(string str);
    
    /**
      * printf - the old clasic but don't use \n or \r in format string
      *  \n and \r is processed - \n includes \r  functionality
      */
    uint8_t printf(const char * frmt, ...);   
    
    /**
      * Scroll display one line up. An empty line will arise at bottom.
      */
    void scroll();
    
};
#endif