BTLE example for a CJMCU-8223 (NRF51822+Lis3dh). Also includes an OLED display. Further details on ioprog.com

Dependencies:   mbed BLE_API nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers oled.h Source File

oled.h

00001 // oled.h
00002 // This class is for the SSD1306 OLED
00003 // 
00004 #include <mbed.h>
00005 #include "font5x7.h"
00006 I2C i2c(P0_30, P0_0); // SDA is on P0_30, SCL is on P0_0
00007 class oled 
00008 {
00009 
00010 public: 
00011     oled() {};
00012     void clear()
00013     {
00014         for (int i = 0; i < 8; i++)
00015         {
00016             clearOLEDLine(i);
00017         }
00018     }
00019     void begin()
00020     {
00021         resetOLED();
00022         clear();
00023     }
00024     void print(int Col, int Row, const char *Text)
00025     {
00026         // write the supplied text (up to 25 bytes) to the given display line
00027         int i;
00028         uint8_t RowData[128];
00029         for (i = 0; i < 128; i++)
00030             RowData[i] = 0;
00031         while (*Text) {
00032             for (i = 0; i < FONT_WIDTH; i++)
00033             {
00034                 RowData[Col * FONT_WIDTH + i] = Font5x7[FONT_WIDTH * ((*Text) - 32) + i];
00035             }
00036             Col++;
00037             Text++;
00038             if (Col > 24)
00039                 break; // Can't print past end of the screen
00040         }
00041         writeOLEDLine(Row, RowData);
00042     }
00043     void print(int Col, int Row, int32_t Value)
00044     {
00045         char Text[20];
00046         int2Text(Text,Value);
00047         print(Col,Row,Text);
00048     }
00049    
00050 private:
00051     void writeOLEDLine(int LineNumber, uint8_t *Values)
00052     {
00053         // Writes the set of values to the given line number
00054         writeOLEDRegister(0x00, 0x21);
00055         writeOLEDRegister(0x00, 0);
00056         writeOLEDRegister(0x00, 127);
00057         writeOLEDRegister(0x00, 0x22);
00058         writeOLEDRegister(0x00, LineNumber); // Page address
00059         writeOLEDRegister(0x00, 7);
00060         writeOLEDBytes(0x40, 128, Values);
00061     }
00062     uint8_t writeOLEDRegister(uint8_t RegNum, uint8_t Value)
00063     {
00064         char TXData[2];
00065         TXData[0] = RegNum;
00066         TXData[1] = Value;
00067         return i2c.write(0x3c << 1,(const char *)TXData,2);    
00068     }
00069     void resetOLED()
00070     {
00071         // Reset sequence got from https://github.com/adafruit/Adafruit_SSD1306/blob/master/Adafruit_SSD1306.cpp
00072         writeOLEDRegister(0x00, 0xae);
00073         writeOLEDRegister(0x00, 0xd5);
00074         writeOLEDRegister(0x00, 0x80);
00075         writeOLEDRegister(0x00, 0xa8);
00076         writeOLEDRegister(0x00, 63);
00077         writeOLEDRegister(0x00, 0xd3);
00078         writeOLEDRegister(0x00, 0);
00079         writeOLEDRegister(0x00, 0x40);
00080         writeOLEDRegister(0x00, 0x8d);
00081         writeOLEDRegister(0x00, 0x14);
00082         writeOLEDRegister(0x00, 0x20);
00083         writeOLEDRegister(0x00, 0x00);
00084         writeOLEDRegister(0x00, 0xa1);
00085         writeOLEDRegister(0x00, 0xc8);
00086         
00087         writeOLEDRegister(0x00, 0xda);
00088         writeOLEDRegister(0x00, 0x12);
00089         writeOLEDRegister(0x00, 0x81);
00090         writeOLEDRegister(0x00, 0xcf);
00091         
00092         writeOLEDRegister(0x00, 0xd9);
00093         writeOLEDRegister(0x00, 0xf1);
00094         writeOLEDRegister(0x00, 0xdb);
00095         writeOLEDRegister(0x00, 0x40);
00096         writeOLEDRegister(0x00, 0xa4);
00097         writeOLEDRegister(0x00, 0xa6);
00098         writeOLEDRegister(0x00, 0x2e);
00099         writeOLEDRegister(0x00, 0xaf);
00100     }
00101     void clearOLEDLine(int LineNumber)
00102     {
00103         // Clears the given line (range 0 to 7) on the display
00104         // A line is 8 pixels high
00105         writeOLEDRegister(0x00, 0x21);
00106         writeOLEDRegister(0x00, 0);
00107         writeOLEDRegister(0x00, 127);
00108         writeOLEDRegister(0x00, 0x22);
00109         writeOLEDRegister(0x00, LineNumber); // Page address
00110         writeOLEDRegister(0x00, 7);
00111         fillOLEDBytes(0x40, 128, 0x00);
00112     }
00113     
00114     
00115     uint8_t fillOLEDBytes(uint8_t RegNum, uint8_t Count, uint8_t Value)
00116     {
00117         // Repeatedly writes the given Value to the OLED memory - useful
00118         // for clearing the display
00119         uint8_t TXData[Count+1];    
00120         int i;
00121         TXData[0] = RegNum;
00122         for (i = 0; i < Count; i++)
00123         {
00124             TXData[i + 1] = Value;
00125         }
00126         return i2c.write(0x3c << 1,(const char *)TXData,Count+1);    
00127     }
00128     uint8_t writeOLEDBytes(uint8_t RegNum, uint8_t Count, uint8_t *Values)
00129     {
00130         // Writes the array of up to 128 bytes to the OLED display
00131         if (Count > 128)
00132             return -1;
00133         uint8_t TXData[Count+1];        
00134         int i;
00135         TXData[0] = RegNum;
00136         for (i = 0; i < Count; i++)
00137         {
00138             TXData[i + 1] = Values[i];
00139         }
00140         return i2c.write(0x3c << 1,(const char *)TXData,Count+1);    
00141     }
00142 
00143     
00144     void int2Text(char *Text, int32_t Value)
00145     {
00146         int index;
00147         Text[11]=0;
00148         if (Value < 0)
00149         {
00150             Text[0]='-';
00151             Value = -Value;
00152         }
00153         else
00154         {
00155             Text[0] = '+';
00156         }
00157         for (index = 0; index < 10;index++)
00158         {
00159             Text[10-index]=(Value % 10) + '0';
00160             Value = Value / 10;
00161         }   
00162     }
00163     void int2Hex(char *Hex, uint32_t Value)
00164     {
00165         int temp;
00166         int index;
00167         Hex[8]=0;
00168         for (index = 0; index < 8;index++)
00169         {
00170             temp = Value % 16;
00171             if (temp < 10)
00172                 temp = temp + '0';
00173             else
00174                 temp = temp + 'A' - 10;
00175             Hex[7-index]=temp;
00176             Value = Value / 16;
00177         }
00178     }
00179 };