Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Io_moon by
Adafruit_SSD1306.h
00001 /********************************************************************* 00002 This is a library for our Monochrome OLEDs based on SSD1306 drivers 00003 00004 Pick one up today in the adafruit shop! 00005 ------> http://www.adafruit.com/category/63_98 00006 00007 These displays use SPI to communicate, 4 or 5 pins are required to 00008 interface 00009 00010 Adafruit invests time and resources providing this open source code, 00011 please support Adafruit and open-source hardware by purchasing 00012 products from Adafruit! 00013 00014 Written by Limor Fried/Ladyada for Adafruit Industries. 00015 BSD license, check license.txt for more information 00016 All text above, and the splash screen must be included in any redistribution 00017 *********************************************************************/ 00018 00019 /* 00020 * Modified by Neal Horman 7/14/2012 for use in mbed 00021 */ 00022 00023 #ifndef _ADAFRUIT_SSD1306_H_ 00024 #define _ADAFRUIT_SSD1306_H_ 00025 00026 #include "mbed.h" 00027 #include "Adafruit_GFX.h" 00028 #if 0 00029 #include <vector> 00030 #include <algorithm> 00031 #endif 00032 // A DigitalOut sub-class that provides a constructed default state 00033 class DigitalOut2 : public DigitalOut 00034 { 00035 public: 00036 DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { 00037 write(active); 00038 }; 00039 DigitalOut2& operator= (int value) { 00040 write(value); 00041 return *this; 00042 }; 00043 DigitalOut2& operator= (DigitalOut2& rhs) { 00044 write(rhs.read()); 00045 return *this; 00046 }; 00047 operator int() { 00048 return read(); 00049 }; 00050 }; 00051 00052 #define SSD1306_EXTERNALVCC 0x1 00053 #define SSD1306_SWITCHCAPVCC 0x2 00054 #ifdef ADAFRUIT_SSD1306_CPP 00055 uint8_t buffer[1024]; 00056 #else 00057 extern uint8_t buffer[1024]; 00058 #endif 00059 /** The pure base class for the SSD1306 display driver. 00060 * 00061 * You should derive from this for a new transport interface type, 00062 * such as the SPI and I2C drivers. 00063 */ 00064 class Adafruit_SSD1306 : public Adafruit_GFX 00065 { 00066 public: 00067 Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128) 00068 : Adafruit_GFX(rawWidth,rawHeight) 00069 , rst(RST,false) { 00070 //buffer.resize(rawHeight * rawWidth / 8); 00071 }; 00072 00073 void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC); 00074 00075 // These must be implemented in the derived transport driver 00076 virtual void command(uint8_t c) = 0; 00077 virtual void data(uint8_t c) = 0; 00078 virtual void drawPixel(int16_t x, int16_t y, uint16_t color); 00079 00080 /// Clear the display buffer 00081 void clearDisplay(void); 00082 virtual void invertDisplay(bool i); 00083 00084 /// Cause the display to be updated with the buffer content. 00085 void display(); 00086 /// Fill the buffer with the AdaFruit splash screen. 00087 virtual void splash(); 00088 00089 protected: 00090 virtual void sendDisplayBuffer() = 0; 00091 DigitalOut2 rst; 00092 00093 // the memory buffer for the LCD 00094 //std::vector<uint8_t> buffer; 00095 }; 00096 00097 #if 0 00098 /** This is the SPI SSD1306 display driver transport class 00099 * 00100 */ 00101 class Adafruit_SSD1306_Spi : public Adafruit_SSD1306 00102 { 00103 public: 00104 /** Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimentions 00105 * 00106 * Required parameters 00107 * @param spi - a reference to an initialized SPI object 00108 * @param DC (Data/Command) pin name 00109 * @param RST (Reset) pin name 00110 * @param CS (Chip Select) pin name 00111 * 00112 * Optional parameters 00113 * @param rawHeight - the vertical number of pixels for the display, defaults to 32 00114 * @param rawWidth - the horizonal number of pixels for the display, defaults to 128 00115 */ 00116 Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128) 00117 : Adafruit_SSD1306(RST, rawHieght, rawWidth) 00118 , cs(CS,true) 00119 , dc(DC,false) 00120 , mspi(spi) { 00121 begin(); 00122 splash(); 00123 display(); 00124 }; 00125 00126 virtual void command(uint8_t c) { 00127 cs = 1; 00128 dc = 0; 00129 cs = 0; 00130 mspi.write(c); 00131 cs = 1; 00132 }; 00133 00134 virtual void data(uint8_t c) { 00135 cs = 1; 00136 dc = 1; 00137 cs = 0; 00138 mspi.write(c); 00139 cs = 1; 00140 }; 00141 00142 protected: 00143 virtual void sendDisplayBuffer() { 00144 cs = 1; 00145 dc = 1; 00146 cs = 0; 00147 00148 for(uint16_t i=0, q=buffer.size(); i<q; i++) 00149 mspi.write(buffer[i]); 00150 00151 if(height() == 32) { 00152 for(uint16_t i=0, q=buffer.size(); i<q; i++) 00153 mspi.write(0); 00154 } 00155 00156 cs = 1; 00157 }; 00158 00159 DigitalOut2 cs, dc; 00160 SPI &mspi; 00161 }; 00162 #endif 00163 00164 /** This is the I2C SSD1306 display driver transport class 00165 * 00166 */ 00167 class Adafruit_SSD1306_I2c : public Adafruit_SSD1306 00168 { 00169 public: 00170 #define SSD_I2C_ADDRESS 0x78 00171 /** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions 00172 * 00173 * Required parameters 00174 * @param i2c - A reference to an initialized I2C object 00175 * @param RST - The Reset pin name 00176 * 00177 * Optional parameters 00178 * @param i2cAddress - The i2c address of the display 00179 * @param rawHeight - The vertical number of pixels for the display, defaults to 32 00180 * @param rawWidth - The horizonal number of pixels for the display, defaults to 128 00181 */ 00182 Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128) 00183 : Adafruit_SSD1306(RST, rawHeight, rawWidth) 00184 , mi2c(i2c) 00185 , mi2cAddress(i2cAddress) { 00186 begin(); 00187 splash(); 00188 display(); 00189 }; 00190 00191 virtual void command(uint8_t c) { 00192 char buff[2]; 00193 buff[0] = 0; // Command Mode 00194 buff[1] = c; 00195 mi2c.write(mi2cAddress, buff, sizeof(buff)); 00196 } 00197 00198 virtual void data(uint8_t c) { 00199 char buff[2]; 00200 buff[0] = 0x40; // Data Mode 00201 buff[1] = c; 00202 mi2c.write(mi2cAddress, buff, sizeof(buff)); 00203 }; 00204 00205 protected: 00206 virtual void sendDisplayBuffer() { 00207 char buff[17]; 00208 buff[0] = 0x40; // Data Mode 00209 #if 0 00210 // send display buffer in 16 byte chunks 00211 for(uint16_t i=0, q=buffer.size(); i<q; i+=16 ) { 00212 uint8_t x ; 00213 00214 // TODO - this will segfault if buffer.size() % 16 != 0 00215 for(x=1; x<sizeof(buff); x++) 00216 buff[x] = buffer[i+x-1]; 00217 mi2c.write(mi2cAddress, buff, sizeof(buff)); 00218 } 00219 #else 00220 for(uint16_t i=0; i<1024; i+=16 ) { 00221 uint8_t x ; 00222 for(x=1; x<sizeof(buff); x++) 00223 buff[x] = buffer[i+x-1]; 00224 mi2c.write(mi2cAddress, buff, sizeof(buff)); 00225 } 00226 #endif 00227 }; 00228 00229 I2C &mi2c; 00230 uint8_t mi2cAddress; 00231 }; 00232 00233 #endif
Generated on Tue Jul 19 2022 01:01:49 by
1.7.2
