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

Dependencies:   mbed BLE_API nRF51822

Committer:
f3d
Date:
Wed Jun 24 11:37:11 2020 +0000
Revision:
0:8fa1c7356f71
Initial commit for CJMCU-8223 offering an LED service and an accelerometer service.  Also includes an Oled display

Who changed what in which revision?

UserRevisionLine numberNew contents of line
f3d 0:8fa1c7356f71 1 // oled.h
f3d 0:8fa1c7356f71 2 // This class is for the SSD1306 OLED
f3d 0:8fa1c7356f71 3 //
f3d 0:8fa1c7356f71 4 #include <mbed.h>
f3d 0:8fa1c7356f71 5 #include "font5x7.h"
f3d 0:8fa1c7356f71 6 I2C i2c(P0_30, P0_0); // SDA is on P0_30, SCL is on P0_0
f3d 0:8fa1c7356f71 7 class oled
f3d 0:8fa1c7356f71 8 {
f3d 0:8fa1c7356f71 9
f3d 0:8fa1c7356f71 10 public:
f3d 0:8fa1c7356f71 11 oled() {};
f3d 0:8fa1c7356f71 12 void clear()
f3d 0:8fa1c7356f71 13 {
f3d 0:8fa1c7356f71 14 for (int i = 0; i < 8; i++)
f3d 0:8fa1c7356f71 15 {
f3d 0:8fa1c7356f71 16 clearOLEDLine(i);
f3d 0:8fa1c7356f71 17 }
f3d 0:8fa1c7356f71 18 }
f3d 0:8fa1c7356f71 19 void begin()
f3d 0:8fa1c7356f71 20 {
f3d 0:8fa1c7356f71 21 resetOLED();
f3d 0:8fa1c7356f71 22 clear();
f3d 0:8fa1c7356f71 23 }
f3d 0:8fa1c7356f71 24 void print(int Col, int Row, const char *Text)
f3d 0:8fa1c7356f71 25 {
f3d 0:8fa1c7356f71 26 // write the supplied text (up to 25 bytes) to the given display line
f3d 0:8fa1c7356f71 27 int i;
f3d 0:8fa1c7356f71 28 uint8_t RowData[128];
f3d 0:8fa1c7356f71 29 for (i = 0; i < 128; i++)
f3d 0:8fa1c7356f71 30 RowData[i] = 0;
f3d 0:8fa1c7356f71 31 while (*Text) {
f3d 0:8fa1c7356f71 32 for (i = 0; i < FONT_WIDTH; i++)
f3d 0:8fa1c7356f71 33 {
f3d 0:8fa1c7356f71 34 RowData[Col * FONT_WIDTH + i] = Font5x7[FONT_WIDTH * ((*Text) - 32) + i];
f3d 0:8fa1c7356f71 35 }
f3d 0:8fa1c7356f71 36 Col++;
f3d 0:8fa1c7356f71 37 Text++;
f3d 0:8fa1c7356f71 38 if (Col > 24)
f3d 0:8fa1c7356f71 39 break; // Can't print past end of the screen
f3d 0:8fa1c7356f71 40 }
f3d 0:8fa1c7356f71 41 writeOLEDLine(Row, RowData);
f3d 0:8fa1c7356f71 42 }
f3d 0:8fa1c7356f71 43 void print(int Col, int Row, int32_t Value)
f3d 0:8fa1c7356f71 44 {
f3d 0:8fa1c7356f71 45 char Text[20];
f3d 0:8fa1c7356f71 46 int2Text(Text,Value);
f3d 0:8fa1c7356f71 47 print(Col,Row,Text);
f3d 0:8fa1c7356f71 48 }
f3d 0:8fa1c7356f71 49
f3d 0:8fa1c7356f71 50 private:
f3d 0:8fa1c7356f71 51 void writeOLEDLine(int LineNumber, uint8_t *Values)
f3d 0:8fa1c7356f71 52 {
f3d 0:8fa1c7356f71 53 // Writes the set of values to the given line number
f3d 0:8fa1c7356f71 54 writeOLEDRegister(0x00, 0x21);
f3d 0:8fa1c7356f71 55 writeOLEDRegister(0x00, 0);
f3d 0:8fa1c7356f71 56 writeOLEDRegister(0x00, 127);
f3d 0:8fa1c7356f71 57 writeOLEDRegister(0x00, 0x22);
f3d 0:8fa1c7356f71 58 writeOLEDRegister(0x00, LineNumber); // Page address
f3d 0:8fa1c7356f71 59 writeOLEDRegister(0x00, 7);
f3d 0:8fa1c7356f71 60 writeOLEDBytes(0x40, 128, Values);
f3d 0:8fa1c7356f71 61 }
f3d 0:8fa1c7356f71 62 uint8_t writeOLEDRegister(uint8_t RegNum, uint8_t Value)
f3d 0:8fa1c7356f71 63 {
f3d 0:8fa1c7356f71 64 char TXData[2];
f3d 0:8fa1c7356f71 65 TXData[0] = RegNum;
f3d 0:8fa1c7356f71 66 TXData[1] = Value;
f3d 0:8fa1c7356f71 67 return i2c.write(0x3c << 1,(const char *)TXData,2);
f3d 0:8fa1c7356f71 68 }
f3d 0:8fa1c7356f71 69 void resetOLED()
f3d 0:8fa1c7356f71 70 {
f3d 0:8fa1c7356f71 71 // Reset sequence got from https://github.com/adafruit/Adafruit_SSD1306/blob/master/Adafruit_SSD1306.cpp
f3d 0:8fa1c7356f71 72 writeOLEDRegister(0x00, 0xae);
f3d 0:8fa1c7356f71 73 writeOLEDRegister(0x00, 0xd5);
f3d 0:8fa1c7356f71 74 writeOLEDRegister(0x00, 0x80);
f3d 0:8fa1c7356f71 75 writeOLEDRegister(0x00, 0xa8);
f3d 0:8fa1c7356f71 76 writeOLEDRegister(0x00, 63);
f3d 0:8fa1c7356f71 77 writeOLEDRegister(0x00, 0xd3);
f3d 0:8fa1c7356f71 78 writeOLEDRegister(0x00, 0);
f3d 0:8fa1c7356f71 79 writeOLEDRegister(0x00, 0x40);
f3d 0:8fa1c7356f71 80 writeOLEDRegister(0x00, 0x8d);
f3d 0:8fa1c7356f71 81 writeOLEDRegister(0x00, 0x14);
f3d 0:8fa1c7356f71 82 writeOLEDRegister(0x00, 0x20);
f3d 0:8fa1c7356f71 83 writeOLEDRegister(0x00, 0x00);
f3d 0:8fa1c7356f71 84 writeOLEDRegister(0x00, 0xa1);
f3d 0:8fa1c7356f71 85 writeOLEDRegister(0x00, 0xc8);
f3d 0:8fa1c7356f71 86
f3d 0:8fa1c7356f71 87 writeOLEDRegister(0x00, 0xda);
f3d 0:8fa1c7356f71 88 writeOLEDRegister(0x00, 0x12);
f3d 0:8fa1c7356f71 89 writeOLEDRegister(0x00, 0x81);
f3d 0:8fa1c7356f71 90 writeOLEDRegister(0x00, 0xcf);
f3d 0:8fa1c7356f71 91
f3d 0:8fa1c7356f71 92 writeOLEDRegister(0x00, 0xd9);
f3d 0:8fa1c7356f71 93 writeOLEDRegister(0x00, 0xf1);
f3d 0:8fa1c7356f71 94 writeOLEDRegister(0x00, 0xdb);
f3d 0:8fa1c7356f71 95 writeOLEDRegister(0x00, 0x40);
f3d 0:8fa1c7356f71 96 writeOLEDRegister(0x00, 0xa4);
f3d 0:8fa1c7356f71 97 writeOLEDRegister(0x00, 0xa6);
f3d 0:8fa1c7356f71 98 writeOLEDRegister(0x00, 0x2e);
f3d 0:8fa1c7356f71 99 writeOLEDRegister(0x00, 0xaf);
f3d 0:8fa1c7356f71 100 }
f3d 0:8fa1c7356f71 101 void clearOLEDLine(int LineNumber)
f3d 0:8fa1c7356f71 102 {
f3d 0:8fa1c7356f71 103 // Clears the given line (range 0 to 7) on the display
f3d 0:8fa1c7356f71 104 // A line is 8 pixels high
f3d 0:8fa1c7356f71 105 writeOLEDRegister(0x00, 0x21);
f3d 0:8fa1c7356f71 106 writeOLEDRegister(0x00, 0);
f3d 0:8fa1c7356f71 107 writeOLEDRegister(0x00, 127);
f3d 0:8fa1c7356f71 108 writeOLEDRegister(0x00, 0x22);
f3d 0:8fa1c7356f71 109 writeOLEDRegister(0x00, LineNumber); // Page address
f3d 0:8fa1c7356f71 110 writeOLEDRegister(0x00, 7);
f3d 0:8fa1c7356f71 111 fillOLEDBytes(0x40, 128, 0x00);
f3d 0:8fa1c7356f71 112 }
f3d 0:8fa1c7356f71 113
f3d 0:8fa1c7356f71 114
f3d 0:8fa1c7356f71 115 uint8_t fillOLEDBytes(uint8_t RegNum, uint8_t Count, uint8_t Value)
f3d 0:8fa1c7356f71 116 {
f3d 0:8fa1c7356f71 117 // Repeatedly writes the given Value to the OLED memory - useful
f3d 0:8fa1c7356f71 118 // for clearing the display
f3d 0:8fa1c7356f71 119 uint8_t TXData[Count+1];
f3d 0:8fa1c7356f71 120 int i;
f3d 0:8fa1c7356f71 121 TXData[0] = RegNum;
f3d 0:8fa1c7356f71 122 for (i = 0; i < Count; i++)
f3d 0:8fa1c7356f71 123 {
f3d 0:8fa1c7356f71 124 TXData[i + 1] = Value;
f3d 0:8fa1c7356f71 125 }
f3d 0:8fa1c7356f71 126 return i2c.write(0x3c << 1,(const char *)TXData,Count+1);
f3d 0:8fa1c7356f71 127 }
f3d 0:8fa1c7356f71 128 uint8_t writeOLEDBytes(uint8_t RegNum, uint8_t Count, uint8_t *Values)
f3d 0:8fa1c7356f71 129 {
f3d 0:8fa1c7356f71 130 // Writes the array of up to 128 bytes to the OLED display
f3d 0:8fa1c7356f71 131 if (Count > 128)
f3d 0:8fa1c7356f71 132 return -1;
f3d 0:8fa1c7356f71 133 uint8_t TXData[Count+1];
f3d 0:8fa1c7356f71 134 int i;
f3d 0:8fa1c7356f71 135 TXData[0] = RegNum;
f3d 0:8fa1c7356f71 136 for (i = 0; i < Count; i++)
f3d 0:8fa1c7356f71 137 {
f3d 0:8fa1c7356f71 138 TXData[i + 1] = Values[i];
f3d 0:8fa1c7356f71 139 }
f3d 0:8fa1c7356f71 140 return i2c.write(0x3c << 1,(const char *)TXData,Count+1);
f3d 0:8fa1c7356f71 141 }
f3d 0:8fa1c7356f71 142
f3d 0:8fa1c7356f71 143
f3d 0:8fa1c7356f71 144 void int2Text(char *Text, int32_t Value)
f3d 0:8fa1c7356f71 145 {
f3d 0:8fa1c7356f71 146 int index;
f3d 0:8fa1c7356f71 147 Text[11]=0;
f3d 0:8fa1c7356f71 148 if (Value < 0)
f3d 0:8fa1c7356f71 149 {
f3d 0:8fa1c7356f71 150 Text[0]='-';
f3d 0:8fa1c7356f71 151 Value = -Value;
f3d 0:8fa1c7356f71 152 }
f3d 0:8fa1c7356f71 153 else
f3d 0:8fa1c7356f71 154 {
f3d 0:8fa1c7356f71 155 Text[0] = '+';
f3d 0:8fa1c7356f71 156 }
f3d 0:8fa1c7356f71 157 for (index = 0; index < 10;index++)
f3d 0:8fa1c7356f71 158 {
f3d 0:8fa1c7356f71 159 Text[10-index]=(Value % 10) + '0';
f3d 0:8fa1c7356f71 160 Value = Value / 10;
f3d 0:8fa1c7356f71 161 }
f3d 0:8fa1c7356f71 162 }
f3d 0:8fa1c7356f71 163 void int2Hex(char *Hex, uint32_t Value)
f3d 0:8fa1c7356f71 164 {
f3d 0:8fa1c7356f71 165 int temp;
f3d 0:8fa1c7356f71 166 int index;
f3d 0:8fa1c7356f71 167 Hex[8]=0;
f3d 0:8fa1c7356f71 168 for (index = 0; index < 8;index++)
f3d 0:8fa1c7356f71 169 {
f3d 0:8fa1c7356f71 170 temp = Value % 16;
f3d 0:8fa1c7356f71 171 if (temp < 10)
f3d 0:8fa1c7356f71 172 temp = temp + '0';
f3d 0:8fa1c7356f71 173 else
f3d 0:8fa1c7356f71 174 temp = temp + 'A' - 10;
f3d 0:8fa1c7356f71 175 Hex[7-index]=temp;
f3d 0:8fa1c7356f71 176 Value = Value / 16;
f3d 0:8fa1c7356f71 177 }
f3d 0:8fa1c7356f71 178 }
f3d 0:8fa1c7356f71 179 };