Benny Andersen / microbitOLED

Dependencies:   microbit

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OLED.h Source File

OLED.h

00001 #include "MicroBit.h"
00002 #include <vector>
00003 #include "stdarg.h"
00004 #include "cppNorm.h"
00005 
00006 #ifndef microbitOLED_OLED
00007 #define microbitOLED_OLED
00008 
00009 #define SSD1306_CHARGEPUMP 0x8D
00010 #define SSD1306_COMSCANDEC 0xC8
00011 #define SSD1306_COMSCANINC 0xC0
00012 #define SSD1306_DISPLAYALLON 0xA5
00013 #define SSD1306_DISPLAYALLON_RESUME 0xA4
00014 #define SSD1306_DISPLAYON 0xAF
00015 #define SSD1306_DISPLAYOFF 0xAE
00016 #define SSD1306_MEMORYMODE 0x20
00017 #define SSD1306_NORMALDISPLAY 0xA6
00018 #define SSD1306_INVERTDISPLAY 0xA7
00019 #define SSD1306_SEGREMAP 0xA0
00020 #define SSD1306_SETCONTRAST 0x81
00021 #define SSD1306_SETCOLUMNADRESS 0x21
00022 #define SSD1306_SETVCOMDETECT 0xDB
00023 #define SSD1306_SETCOMPINS 0xDA
00024 #define SSD1306_SETDISPLAYOFFSET 0xD3
00025 #define SSD1306_SETHIGHCOLUMN 0x10
00026 #define SSD1306_SETLOWCOLUMN 0x00
00027 #define SSD1306_SETPAGEADRESS 0x22
00028 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
00029 #define SSD1306_SETMULTIPLEX 0xA8
00030 #define SSD1306_SETPRECHARGE 0xD9
00031 #define SSD1306_SETSTARTLINE 0x40
00032 
00033 /**
00034   * Access to bitfields under different names.
00035   * Access to bit 0-7, named  by0 or x, and bit 8-15, named by1 or y \n  
00036   * of 16 bit named that16. Can be consructed with either two \n  
00037   * uint8_t or a uint16_t
00038   */
00039 union uint16 {
00040     struct {
00041     uint8_t by0;
00042     uint8_t by1;
00043     };
00044     struct {
00045     uint8_t x;
00046     uint8_t y;
00047     };
00048     uint16_t that16;
00049     uint16(uint16_t all) : that16(all) {}
00050     uint16(uint8_t byte1,uint8_t  byte0) : by1(byte1),by0(byte0) {}
00051     };
00052 
00053 const unsigned char font[][5] = {
00054     {0x00,0x00,0x00,0x00,0x00},
00055     {0x3E,0x5B,0x4F,0x5B,0x3E},
00056     {0x3E,0x6B,0x4F,0x6B,0x3E},
00057     {0x1C,0x3E,0x7C,0x3E,0x1C},
00058     {0x18,0x3C,0x7E,0x3C,0x18},
00059     {0x1C,0x57,0x7D,0x57,0x1C},
00060     {0x1C,0x5E,0x7F,0x5E,0x1C},
00061     {0x00,0x18,0x3C,0x18,0x00},
00062     {0xFF,0xE7,0xC3,0xE7,0xFF},
00063     {0x00,0x18,0x24,0x18,0x00},
00064     {0xFF,0xE7,0xDB,0xE7,0xFF},
00065     {0x30,0x48,0x3A,0x06,0x0E},
00066     {0x26,0x29,0x79,0x29,0x26},
00067     {0x40,0x7F,0x05,0x05,0x07},
00068     {0x40,0x7F,0x05,0x25,0x3F},
00069     {0x5A,0x3C,0xE7,0x3C,0x5A},
00070     {0x7F,0x3E,0x1C,0x1C,0x08},
00071     {0x08,0x1C,0x1C,0x3E,0x7F},
00072     {0x14,0x22,0x7F,0x22,0x14},
00073     {0x5F,0x5F,0x00,0x5F,0x5F},
00074     {0x06,0x09,0x7F,0x01,0x7F},
00075     {0x00,0x66,0x89,0x95,0x6A},
00076     {0x60,0x60,0x60,0x60,0x60},
00077     {0x94,0xA2,0xFF,0xA2,0x94},
00078     {0x08,0x04,0x7E,0x04,0x08},
00079     {0x10,0x20,0x7E,0x20,0x10},
00080     {0x08,0x08,0x2A,0x1C,0x08},
00081     {0x08,0x1C,0x2A,0x08,0x08},
00082     {0x1E,0x10,0x10,0x10,0x10},
00083     {0x0C,0x1E,0x0C,0x1E,0x0C},
00084     {0x30,0x38,0x3E,0x38,0x30},
00085     {0x06,0x0E,0x3E,0x0E,0x06},
00086     {0x00,0x00,0x00,0x00,0x00},
00087     {0x00,0x00,0x5F,0x00,0x00},
00088     {0x00,0x07,0x00,0x07,0x00},
00089     {0x14,0x7F,0x14,0x7F,0x14},
00090     {0x24,0x2A,0x7F,0x2A,0x12},
00091     {0x23,0x13,0x08,0x64,0x62},
00092     {0x36,0x49,0x56,0x20,0x50},
00093     {0x00,0x08,0x07,0x03,0x00},
00094     {0x00,0x1C,0x22,0x41,0x00},
00095     {0x00,0x41,0x22,0x1C,0x00},
00096     {0x2A,0x1C,0x7F,0x1C,0x2A},
00097     {0x08,0x08,0x3E,0x08,0x08},
00098     {0x00,0x80,0x70,0x30,0x00},
00099     {0x08,0x08,0x08,0x08,0x08},
00100     {0x00,0x00,0x60,0x60,0x00},
00101     {0x20,0x10,0x08,0x04,0x02},
00102     {0x3E,0x51,0x49,0x45,0x3E},
00103     {0x00,0x42,0x7F,0x40,0x00},
00104     {0x72,0x49,0x49,0x49,0x46},
00105     {0x21,0x41,0x49,0x4D,0x33},
00106     {0x18,0x14,0x12,0x7F,0x10},
00107     {0x27,0x45,0x45,0x45,0x39},
00108     {0x3C,0x4A,0x49,0x49,0x31},
00109     {0x41,0x21,0x11,0x09,0x07},
00110     {0x36,0x49,0x49,0x49,0x36},
00111     {0x46,0x49,0x49,0x29,0x1E},
00112     {0x00,0x00,0x14,0x00,0x00},
00113     {0x00,0x40,0x34,0x00,0x00},
00114     {0x00,0x08,0x14,0x22,0x41},
00115     {0x14,0x14,0x14,0x14,0x14},
00116     {0x00,0x41,0x22,0x14,0x08},
00117     {0x02,0x01,0x59,0x09,0x06},
00118     {0x3E,0x41,0x5D,0x59,0x4E},
00119     {0x7C,0x12,0x11,0x12,0x7C},
00120     {0x7F,0x49,0x49,0x49,0x36},
00121     {0x3E,0x41,0x41,0x41,0x22},
00122     {0x7F,0x41,0x41,0x41,0x3E},
00123     {0x7F,0x49,0x49,0x49,0x41},
00124     {0x7F,0x09,0x09,0x09,0x01},
00125     {0x3E,0x41,0x41,0x51,0x73},
00126     {0x7F,0x08,0x08,0x08,0x7F},
00127     {0x00,0x41,0x7F,0x41,0x00},
00128     {0x20,0x40,0x41,0x3F,0x01},
00129     {0x7F,0x08,0x14,0x22,0x41},
00130     {0x7F,0x40,0x40,0x40,0x40},
00131     {0x7F,0x02,0x1C,0x02,0x7F},
00132     {0x7F,0x04,0x08,0x10,0x7F},
00133     {0x3E,0x41,0x41,0x41,0x3E},
00134     {0x7F,0x09,0x09,0x09,0x06},
00135     {0x3E,0x41,0x51,0x21,0x5E},
00136     {0x7F,0x09,0x19,0x29,0x46},
00137     {0x26,0x49,0x49,0x49,0x32},
00138     {0x03,0x01,0x7F,0x01,0x03},
00139     {0x3F,0x40,0x40,0x40,0x3F},
00140     {0x1F,0x20,0x40,0x20,0x1F},
00141     {0x3F,0x40,0x38,0x40,0x3F},
00142     {0x63,0x14,0x08,0x14,0x63},
00143     {0x03,0x04,0x78,0x04,0x03},
00144     {0x61,0x59,0x49,0x4D,0x43},
00145     {0x00,0x7F,0x41,0x41,0x41},
00146     {0x02,0x04,0x08,0x10,0x20},
00147     {0x00,0x41,0x41,0x41,0x7F},
00148     {0x04,0x02,0x01,0x02,0x04},
00149     {0x40,0x40,0x40,0x40,0x40},
00150     {0x00,0x03,0x07,0x08,0x00},
00151     {0x20,0x54,0x54,0x78,0x40},
00152     {0x7F,0x28,0x44,0x44,0x38},
00153     {0x38,0x44,0x44,0x44,0x28},
00154     {0x38,0x44,0x44,0x28,0x7F},
00155     {0x38,0x54,0x54,0x54,0x18},
00156     {0x00,0x08,0x7E,0x09,0x02},
00157     {0x18,0xA4,0xA4,0x9C,0x78},
00158     {0x7F,0x08,0x04,0x04,0x78},
00159     {0x00,0x44,0x7D,0x40,0x00},
00160     {0x20,0x40,0x40,0x3D,0x00},
00161     {0x7F,0x10,0x28,0x44,0x00},
00162     {0x00,0x41,0x7F,0x40,0x00},
00163     {0x7C,0x04,0x78,0x04,0x78},
00164     {0x7C,0x08,0x04,0x04,0x78},
00165     {0x38,0x44,0x44,0x44,0x38},
00166     {0xFC,0x18,0x24,0x24,0x18},
00167     {0x18,0x24,0x24,0x18,0xFC},
00168     {0x7C,0x08,0x04,0x04,0x08},
00169     {0x48,0x54,0x54,0x54,0x24},
00170     {0x04,0x04,0x3F,0x44,0x24},
00171     {0x3C,0x40,0x40,0x20,0x7C},
00172     {0x1C,0x20,0x40,0x20,0x1C},
00173     {0x3C,0x40,0x30,0x40,0x3C},
00174     {0x44,0x28,0x10,0x28,0x44},
00175     {0x4C,0x90,0x90,0x90,0x7C},
00176     {0x44,0x64,0x54,0x4C,0x44},
00177     {0x00,0x08,0x36,0x41,0x00},
00178     {0x00,0x00,0x77,0x00,0x00},
00179     {0x00,0x41,0x36,0x08,0x00},
00180     {0x02,0x01,0x02,0x04,0x02},
00181     {0x3C,0x26,0x23,0x26,0x3C},
00182     {0x1E,0xA1,0xA1,0x61,0x12},
00183     {0x3A,0x40,0x40,0x20,0x7A},
00184     {0x38,0x54,0x54,0x55,0x59},
00185     {0x21,0x55,0x55,0x79,0x41},
00186     {0x21,0x54,0x54,0x78,0x41},
00187     {0x21,0x55,0x54,0x78,0x40},
00188     {0x20,0x54,0x55,0x79,0x40},
00189     {0x0C,0x1E,0x52,0x72,0x12},
00190     {0x39,0x55,0x55,0x55,0x59},
00191     {0x39,0x54,0x54,0x54,0x59},
00192     {0x39,0x55,0x54,0x54,0x58},
00193     {0x00,0x00,0x45,0x7C,0x41},
00194     {0x00,0x02,0x45,0x7D,0x42},
00195     {0x00,0x01,0x45,0x7C,0x40},
00196     {0xF0,0x29,0x24,0x29,0xF0},
00197     {0xF0,0x28,0x25,0x28,0xF0},
00198     {0x7C,0x54,0x55,0x45,0x00},
00199     {0x20,0x54,0x54,0x7C,0x54},
00200     {0x7C,0x0A,0x09,0x7F,0x49},
00201     {0x32,0x49,0x49,0x49,0x32},
00202     {0x32,0x48,0x48,0x48,0x32},
00203     {0x32,0x4A,0x48,0x48,0x30},
00204     {0x3A,0x41,0x41,0x21,0x7A},
00205     {0x3A,0x42,0x40,0x20,0x78},
00206     {0x00,0x9D,0xA0,0xA0,0x7D},
00207     {0x39,0x44,0x44,0x44,0x39},
00208     {0x3D,0x40,0x40,0x40,0x3D},
00209     {0x3C,0x24,0xFF,0x24,0x24},
00210     {0x48,0x7E,0x49,0x43,0x66},
00211     {0x2B,0x2F,0xFC,0x2F,0x2B},
00212     {0xFF,0x09,0x29,0xF6,0x20},
00213     {0xC0,0x88,0x7E,0x09,0x03},
00214     {0x20,0x54,0x54,0x79,0x41},
00215     {0x00,0x00,0x44,0x7D,0x41},
00216     {0x30,0x48,0x48,0x4A,0x32},
00217     {0x38,0x40,0x40,0x22,0x7A},
00218     {0x00,0x7A,0x0A,0x0A,0x72},
00219     {0x7D,0x0D,0x19,0x31,0x7D},
00220     {0x26,0x29,0x29,0x2F,0x28},
00221     {0x26,0x29,0x29,0x29,0x26},
00222     {0x30,0x48,0x4D,0x40,0x20},
00223     {0x38,0x08,0x08,0x08,0x08},
00224     {0x08,0x08,0x08,0x08,0x38},
00225     {0x2F,0x10,0xC8,0xAC,0xBA},
00226     {0x2F,0x10,0x28,0x34,0xFA},
00227     {0x00,0x00,0x7B,0x00,0x00},
00228     {0x08,0x14,0x2A,0x14,0x22},
00229     {0x22,0x14,0x2A,0x14,0x08},
00230     {0xAA,0x00,0x55,0x00,0xAA},
00231     {0xAA,0x55,0xAA,0x55,0xAA},
00232     {0x00,0x00,0x00,0xFF,0x00},
00233     {0x10,0x10,0x10,0xFF,0x00},
00234     {0x14,0x14,0x14,0xFF,0x00},
00235     {0x10,0x10,0xFF,0x00,0xFF},
00236     {0x10,0x10,0xF0,0x10,0xF0},
00237     {0x14,0x14,0x14,0xFC,0x00},
00238     {0x14,0x14,0xF7,0x00,0xFF},
00239     {0x00,0x00,0xFF,0x00,0xFF},
00240     {0x14,0x14,0xF4,0x04,0xFC},
00241     {0x14,0x14,0x17,0x10,0x1F},
00242     {0x10,0x10,0x1F,0x10,0x1F},
00243     {0x14,0x14,0x14,0x1F,0x00},
00244     {0x10,0x10,0x10,0xF0,0x00},
00245     {0x00,0x00,0x00,0x1F,0x10},
00246     {0x10,0x10,0x10,0x1F,0x10},
00247     {0x10,0x10,0x10,0xF0,0x10},
00248     {0x00,0x00,0x00,0xFF,0x10},
00249     {0x10,0x10,0x10,0x10,0x10},
00250     {0x10,0x10,0x10,0xFF,0x10},
00251     {0x00,0x00,0x00,0xFF,0x14},
00252     {0x00,0x00,0xFF,0x00,0xFF},
00253     {0x00,0x00,0x1F,0x10,0x17},
00254     {0x00,0x00,0xFC,0x04,0xF4},
00255     {0x14,0x14,0x17,0x10,0x17},
00256     {0x14,0x14,0xF4,0x04,0xF4},
00257     {0x00,0x00,0xFF,0x00,0xF7},
00258     {0x14,0x14,0x14,0x14,0x14},
00259     {0x14,0x14,0xF7,0x00,0xF7},
00260     {0x14,0x14,0x14,0x17,0x14},
00261     {0x10,0x10,0x1F,0x10,0x1F},
00262     {0x14,0x14,0x14,0xF4,0x14},
00263     {0x10,0x10,0xF0,0x10,0xF0},
00264     {0x00,0x00,0x1F,0x10,0x1F},
00265     {0x00,0x00,0x00,0x1F,0x14},
00266     {0x00,0x00,0x00,0xFC,0x14},
00267     {0x00,0x00,0xF0,0x10,0xF0},
00268     {0x10,0x10,0xFF,0x10,0xFF},
00269     {0x14,0x14,0x14,0xFF,0x14},
00270     {0x10,0x10,0x10,0x1F,0x00},
00271     {0x00,0x00,0x00,0xF0,0x10},
00272     {0xFF,0xFF,0xFF,0xFF,0xFF},
00273     {0xF0,0xF0,0xF0,0xF0,0xF0},
00274     {0xFF,0xFF,0xFF,0x00,0x00},
00275     {0x00,0x00,0x00,0xFF,0xFF},
00276     {0x0F,0x0F,0x0F,0x0F,0x0F},
00277     {0x38,0x44,0x44,0x38,0x44},
00278     {0x7C,0x2A,0x2A,0x3E,0x14},
00279     {0x7E,0x02,0x02,0x06,0x06},
00280     {0x02,0x7E,0x02,0x7E,0x02},
00281     {0x63,0x55,0x49,0x41,0x63},
00282     {0x38,0x44,0x44,0x3C,0x04},
00283     {0x40,0x7E,0x20,0x1E,0x20},
00284     {0x06,0x02,0x7E,0x02,0x02},
00285     {0x99,0xA5,0xE7,0xA5,0x99},
00286     {0x1C,0x2A,0x49,0x2A,0x1C},
00287     {0x4C,0x72,0x01,0x72,0x4C},
00288     {0x30,0x4A,0x4D,0x4D,0x30},
00289     {0x30,0x48,0x78,0x48,0x30},
00290     {0xBC,0x62,0x5A,0x46,0x3D},
00291     {0x3E,0x49,0x49,0x49,0x00},
00292     {0x7E,0x01,0x01,0x01,0x7E},
00293     {0x2A,0x2A,0x2A,0x2A,0x2A},
00294     {0x44,0x44,0x5F,0x44,0x44},
00295     {0x40,0x51,0x4A,0x44,0x40},
00296     {0x40,0x44,0x4A,0x51,0x40},
00297     {0x00,0x00,0xFF,0x01,0x03},
00298     {0xE0,0x80,0xFF,0x00,0x00},
00299     {0x08,0x08,0x6B,0x6B,0x08},
00300     {0x36,0x12,0x36,0x24,0x36},
00301     {0x06,0x0F,0x09,0x0F,0x06},
00302     {0x00,0x00,0x18,0x18,0x00},
00303     {0x00,0x00,0x10,0x10,0x00},
00304     {0x30,0x40,0xFF,0x01,0x01},
00305     {0x00,0x1F,0x01,0x01,0x1E},
00306     {0x00,0x19,0x1D,0x17,0x12},
00307     {0x00,0x3C,0x3C,0x3C,0x3C}, 
00308     {0x00,0x00,0x00,0x00,0x00}};
00309 
00310 /**
00311   * Representation of a 128x64 pixels, SSD1306 compatibel OLED display.
00312   */ 
00313 class OLED {
00314     
00315     static const uint8_t chipAdress = 0x78; 
00316     static const uint8_t xOffset = 0;
00317     static const uint8_t yOffset = 0;
00318     
00319     const uint8_t printf_textSize=0x3f;
00320     char printf_text[0x40];
00321     
00322     unsigned char textArea[22][9];
00323     uint8_t charX;
00324     uint8_t charY;
00325     uint8_t displayWidth;
00326     uint8_t displayHeight;
00327     uint16_t screenSize;
00328     bool loadStarted;
00329     float loadPercent;
00330     bool pendingNewline;
00331     
00332     MicroBitI2C i2c;
00333     
00334     void init(uint8_t width, uint8_t height);
00335     void command(uint8_t cmd);
00336     void setTextArea(uint8_t chr, bool setLine8);
00337     
00338     
00339 public:
00340 
00341     /**
00342       * ssd1306 compatible device of 128x64 pixels OLED display.
00343       * Init() must be called after construction. \n 
00344       * Works for some untraced circumstances not in fibers
00345       */
00346     OLED();
00347 
00348     /**
00349       * Initialisere display and clears it. 
00350       * A buffer representing charaters are filled with space characters.
00351       */ 
00352     void init();
00353     
00354     /**
00355       * Clears the display.
00356       */
00357     void clear();
00358 
00359     /** 
00360       * Makes a newline by incrementing charY and assigning xOffset to charX \n 
00361       * if charY becomes 8 the display screen is scrolled one line and charY \n 
00362       * is decremented to 7. pendingNewline=false is done at last.
00363       */
00364     void newLine();
00365     
00366     /**
00367       * Put a char on display. 
00368       * The font represent display characters as 6x8 pixels areas (6 horizontal). \n 
00369       * The font does actual contain 5 colums - the six'd makes characters \n 
00370       * seperation. Character values 0, 0x20, and 0xff are bitpatterns with \n
00371       * all null pixels (like space)
00372       *
00373       * @param x horizontal pixelwise position, range [0,displayWidth - 6]
00374       * @param y vertical position, range [0,7]
00375       * @param chr character to put on display
00376       */
00377     void drawChar(uint8_t x, uint8_t y, uint8_t chr );
00378     
00379   /** 
00380     * Writes a string at charX, charY position. Afterwards are \n 
00381     * charX, charY set to the next starting point for writting to display \n 
00382     * \\n and \\r is processed - \\n includes \\r  functionality. \n 
00383     *
00384     * @param str to be displayed at (charX, charY).   
00385     */
00386     void puts(string str);
00387     
00388     /**
00389       * printf - the old classic. \\n and \\r is processed. \n 
00390       * \\n includes \\r  functionality. \n    
00391       * This function is the main reason to that OLED not works in fibers. \n
00392       */
00393     uint8_t printf(const char * frmt, ...);   
00394     
00395     /**
00396      *  Scroll the display one line
00397      */
00398     void scroll();
00399     
00400 };
00401 #endif