Bracciale Slave
Adafruit_SSD1306.h@19:e5d8d6e7fac5, 2019-02-25 (annotated)
- Committer:
- gandhi4
- Date:
- Mon Feb 25 21:14:16 2019 +0000
- Revision:
- 19:e5d8d6e7fac5
- Parent:
- 18:a267f00528be
a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nkhorman | 0:c3dcd4c4983a | 1 | #ifndef _ADAFRUIT_SSD1306_H_ |
nkhorman | 0:c3dcd4c4983a | 2 | #define _ADAFRUIT_SSD1306_H_ |
nkhorman | 0:c3dcd4c4983a | 3 | |
nkhorman | 0:c3dcd4c4983a | 4 | #include "mbed.h" |
nkhorman | 0:c3dcd4c4983a | 5 | #include "Adafruit_GFX.h" |
nkhorman | 0:c3dcd4c4983a | 6 | |
nkhorman | 9:ddb97c9850a2 | 7 | #include <vector> |
nkhorman | 9:ddb97c9850a2 | 8 | #include <algorithm> |
nkhorman | 0:c3dcd4c4983a | 9 | |
nkhorman | 0:c3dcd4c4983a | 10 | #define SSD1306_EXTERNALVCC 0x1 |
nkhorman | 0:c3dcd4c4983a | 11 | #define SSD1306_SWITCHCAPVCC 0x2 |
nkhorman | 0:c3dcd4c4983a | 12 | |
nkhorman | 0:c3dcd4c4983a | 13 | class Adafruit_SSD1306 : public Adafruit_GFX |
nkhorman | 0:c3dcd4c4983a | 14 | { |
nkhorman | 9:ddb97c9850a2 | 15 | public: |
switches | 18:a267f00528be | 16 | Adafruit_SSD1306(uint8_t rawHeight = 32, uint8_t rawWidth = 128) |
nkhorman | 9:ddb97c9850a2 | 17 | : Adafruit_GFX(rawWidth,rawHeight) |
nkhorman | 11:86909e6db3c8 | 18 | { |
nkhorman | 11:86909e6db3c8 | 19 | buffer.resize(rawHeight * rawWidth / 8); |
nkhorman | 11:86909e6db3c8 | 20 | }; |
nkhorman | 9:ddb97c9850a2 | 21 | |
nkhorman | 9:ddb97c9850a2 | 22 | void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC); |
nkhorman | 11:86909e6db3c8 | 23 | |
nkhorman | 11:86909e6db3c8 | 24 | // These must be implemented in the derived transport driver |
nkhorman | 9:ddb97c9850a2 | 25 | virtual void command(uint8_t c) = 0; |
nkhorman | 9:ddb97c9850a2 | 26 | virtual void data(uint8_t c) = 0; |
nkhorman | 11:86909e6db3c8 | 27 | virtual void drawPixel(int16_t x, int16_t y, uint16_t color); |
nkhorman | 11:86909e6db3c8 | 28 | |
nkhorman | 11:86909e6db3c8 | 29 | /// Clear the display buffer |
nkhorman | 9:ddb97c9850a2 | 30 | void clearDisplay(void); |
nkhorman | 9:ddb97c9850a2 | 31 | virtual void invertDisplay(bool i); |
nkhorman | 11:86909e6db3c8 | 32 | |
nkhorman | 11:86909e6db3c8 | 33 | /// Cause the display to be updated with the buffer content. |
nkhorman | 9:ddb97c9850a2 | 34 | void display(); |
nkhorman | 9:ddb97c9850a2 | 35 | |
nkhorman | 9:ddb97c9850a2 | 36 | protected: |
nkhorman | 9:ddb97c9850a2 | 37 | virtual void sendDisplayBuffer() = 0; |
nkhorman | 9:ddb97c9850a2 | 38 | |
nkhorman | 9:ddb97c9850a2 | 39 | // the memory buffer for the LCD |
nkhorman | 9:ddb97c9850a2 | 40 | std::vector<uint8_t> buffer; |
nkhorman | 9:ddb97c9850a2 | 41 | }; |
nkhorman | 9:ddb97c9850a2 | 42 | |
nkhorman | 11:86909e6db3c8 | 43 | |
gandhi4 | 19:e5d8d6e7fac5 | 44 | // This is the I2C SSD1306 display driver transport class |
gandhi4 | 19:e5d8d6e7fac5 | 45 | |
nkhorman | 9:ddb97c9850a2 | 46 | class Adafruit_SSD1306_I2c : public Adafruit_SSD1306 |
nkhorman | 9:ddb97c9850a2 | 47 | { |
nkhorman | 9:ddb97c9850a2 | 48 | public: |
nkhorman | 9:ddb97c9850a2 | 49 | #define SSD_I2C_ADDRESS 0x78 |
nkhorman | 11:86909e6db3c8 | 50 | /** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions |
nkhorman | 11:86909e6db3c8 | 51 | * |
nkhorman | 11:86909e6db3c8 | 52 | * Required parameters |
nkhorman | 11:86909e6db3c8 | 53 | * @param i2c - A reference to an initialized I2C object |
nkhorman | 11:86909e6db3c8 | 54 | * @param RST - The Reset pin name |
nkhorman | 11:86909e6db3c8 | 55 | * |
nkhorman | 11:86909e6db3c8 | 56 | * Optional parameters |
nkhorman | 11:86909e6db3c8 | 57 | * @param i2cAddress - The i2c address of the display |
nkhorman | 11:86909e6db3c8 | 58 | * @param rawHeight - The vertical number of pixels for the display, defaults to 32 |
nkhorman | 11:86909e6db3c8 | 59 | * @param rawWidth - The horizonal number of pixels for the display, defaults to 128 |
nkhorman | 11:86909e6db3c8 | 60 | */ |
switches | 18:a267f00528be | 61 | Adafruit_SSD1306_I2c(I2C &i2c, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128) |
switches | 18:a267f00528be | 62 | : Adafruit_SSD1306(rawHeight, rawWidth) |
nkhorman | 9:ddb97c9850a2 | 63 | , mi2c(i2c) |
nkhorman | 9:ddb97c9850a2 | 64 | , mi2cAddress(i2cAddress) |
nkhorman | 9:ddb97c9850a2 | 65 | { |
nkhorman | 9:ddb97c9850a2 | 66 | begin(); |
nkhorman | 9:ddb97c9850a2 | 67 | display(); |
nkhorman | 9:ddb97c9850a2 | 68 | }; |
nkhorman | 9:ddb97c9850a2 | 69 | |
nkhorman | 9:ddb97c9850a2 | 70 | virtual void command(uint8_t c) |
nkhorman | 9:ddb97c9850a2 | 71 | { |
nkhorman | 9:ddb97c9850a2 | 72 | char buff[2]; |
nkhorman | 9:ddb97c9850a2 | 73 | buff[0] = 0; // Command Mode |
nkhorman | 9:ddb97c9850a2 | 74 | buff[1] = c; |
nkhorman | 9:ddb97c9850a2 | 75 | mi2c.write(mi2cAddress, buff, sizeof(buff)); |
nkhorman | 9:ddb97c9850a2 | 76 | } |
nkhorman | 9:ddb97c9850a2 | 77 | |
nkhorman | 9:ddb97c9850a2 | 78 | virtual void data(uint8_t c) |
nkhorman | 9:ddb97c9850a2 | 79 | { |
nkhorman | 9:ddb97c9850a2 | 80 | char buff[2]; |
nkhorman | 9:ddb97c9850a2 | 81 | buff[0] = 0x40; // Data Mode |
nkhorman | 9:ddb97c9850a2 | 82 | buff[1] = c; |
nkhorman | 9:ddb97c9850a2 | 83 | mi2c.write(mi2cAddress, buff, sizeof(buff)); |
nkhorman | 9:ddb97c9850a2 | 84 | }; |
nkhorman | 9:ddb97c9850a2 | 85 | |
nkhorman | 9:ddb97c9850a2 | 86 | protected: |
nkhorman | 9:ddb97c9850a2 | 87 | virtual void sendDisplayBuffer() |
nkhorman | 9:ddb97c9850a2 | 88 | { |
nkhorman | 9:ddb97c9850a2 | 89 | char buff[17]; |
nkhorman | 9:ddb97c9850a2 | 90 | buff[0] = 0x40; // Data Mode |
nkhorman | 9:ddb97c9850a2 | 91 | |
nkhorman | 9:ddb97c9850a2 | 92 | // send display buffer in 16 byte chunks |
nkhorman | 9:ddb97c9850a2 | 93 | for(uint16_t i=0, q=buffer.size(); i<q; i+=16 ) |
nkhorman | 9:ddb97c9850a2 | 94 | { uint8_t x ; |
nkhorman | 9:ddb97c9850a2 | 95 | |
nkhorman | 9:ddb97c9850a2 | 96 | // TODO - this will segfault if buffer.size() % 16 != 0 |
nkhorman | 9:ddb97c9850a2 | 97 | for(x=1; x<sizeof(buff); x++) |
JojoS | 15:77feec1c0684 | 98 | buff[x] = buffer[i+x-1]; |
nkhorman | 9:ddb97c9850a2 | 99 | mi2c.write(mi2cAddress, buff, sizeof(buff)); |
nkhorman | 9:ddb97c9850a2 | 100 | } |
nkhorman | 9:ddb97c9850a2 | 101 | }; |
nkhorman | 9:ddb97c9850a2 | 102 | |
nkhorman | 9:ddb97c9850a2 | 103 | I2C &mi2c; |
nkhorman | 9:ddb97c9850a2 | 104 | uint8_t mi2cAddress; |
nkhorman | 0:c3dcd4c4983a | 105 | }; |
nkhorman | 0:c3dcd4c4983a | 106 | |
nkhorman | 0:c3dcd4c4983a | 107 | #endif |