arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

Adafruit_SSD1306.h (8040B)


      1 /*!
      2  * @file Adafruit_SSD1306.h
      3  *
      4  * This is part of for Adafruit's SSD1306 library for monochrome
      5  * OLED displays: http://www.adafruit.com/category/63_98
      6  *
      7  * These displays use I2C or SPI to communicate. I2C requires 2 pins
      8  * (SCL+SDA) and optionally a RESET pin. SPI requires 4 pins (MOSI, SCK,
      9  * select, data/command) and optionally a reset pin. Hardware SPI or
     10  * 'bitbang' software SPI are both supported.
     11  *
     12  * Adafruit invests time and resources providing this open source code,
     13  * please support Adafruit and open-source hardware by purchasing
     14  * products from Adafruit!
     15  *
     16  * Written by Limor Fried/Ladyada for Adafruit Industries, with
     17  * contributions from the open source community.
     18  *
     19  * BSD license, all text above, and the splash screen header file,
     20  * must be included in any redistribution.
     21  *
     22  */
     23 
     24 #ifndef _Adafruit_SSD1306_H_
     25 #define _Adafruit_SSD1306_H_
     26 
     27 // ONE of the following three lines must be #defined:
     28 //#define SSD1306_128_64 ///< DEPRECTAED: old way to specify 128x64 screen
     29 #define SSD1306_128_32 ///< DEPRECATED: old way to specify 128x32 screen
     30 //#define SSD1306_96_16  ///< DEPRECATED: old way to specify 96x16 screen
     31 // This establishes the screen dimensions in old Adafruit_SSD1306 sketches
     32 // (NEW CODE SHOULD IGNORE THIS, USE THE CONSTRUCTORS THAT ACCEPT WIDTH
     33 // AND HEIGHT ARGUMENTS).
     34 
     35 #if defined(ARDUINO_STM32_FEATHER)
     36 typedef class HardwareSPI SPIClass;
     37 #endif
     38 
     39 #include <Adafruit_GFX.h>
     40 #include <SPI.h>
     41 #include <Wire.h>
     42 
     43 #if defined(__AVR__)
     44 typedef volatile uint8_t PortReg;
     45 typedef uint8_t PortMask;
     46 #define HAVE_PORTREG
     47 #elif defined(__SAM3X8E__)
     48 typedef volatile RwReg PortReg;
     49 typedef uint32_t PortMask;
     50 #define HAVE_PORTREG
     51 #elif (defined(__arm__) || defined(ARDUINO_FEATHER52)) &&                      \
     52     !defined(ARDUINO_ARCH_MBED)
     53 typedef volatile uint32_t PortReg;
     54 typedef uint32_t PortMask;
     55 #define HAVE_PORTREG
     56 #endif
     57 
     58 /// The following "raw" color names are kept for backwards client compatability
     59 /// They can be disabled by predefining this macro before including the Adafruit
     60 /// header client code will then need to be modified to use the scoped enum
     61 /// values directly
     62 #ifndef NO_ADAFRUIT_SSD1306_COLOR_COMPATIBILITY
     63 #define BLACK SSD1306_BLACK     ///< Draw 'off' pixels
     64 #define WHITE SSD1306_WHITE     ///< Draw 'on' pixels
     65 #define INVERSE SSD1306_INVERSE ///< Invert pixels
     66 #endif
     67 /// fit into the SSD1306_ naming scheme
     68 #define SSD1306_BLACK 0   ///< Draw 'off' pixels
     69 #define SSD1306_WHITE 1   ///< Draw 'on' pixels
     70 #define SSD1306_INVERSE 2 ///< Invert pixels
     71 
     72 #define SSD1306_MEMORYMODE 0x20          ///< See datasheet
     73 #define SSD1306_COLUMNADDR 0x21          ///< See datasheet
     74 #define SSD1306_PAGEADDR 0x22            ///< See datasheet
     75 #define SSD1306_SETCONTRAST 0x81         ///< See datasheet
     76 #define SSD1306_CHARGEPUMP 0x8D          ///< See datasheet
     77 #define SSD1306_SEGREMAP 0xA0            ///< See datasheet
     78 #define SSD1306_DISPLAYALLON_RESUME 0xA4 ///< See datasheet
     79 #define SSD1306_DISPLAYALLON 0xA5        ///< Not currently used
     80 #define SSD1306_NORMALDISPLAY 0xA6       ///< See datasheet
     81 #define SSD1306_INVERTDISPLAY 0xA7       ///< See datasheet
     82 #define SSD1306_SETMULTIPLEX 0xA8        ///< See datasheet
     83 #define SSD1306_DISPLAYOFF 0xAE          ///< See datasheet
     84 #define SSD1306_DISPLAYON 0xAF           ///< See datasheet
     85 #define SSD1306_COMSCANINC 0xC0          ///< Not currently used
     86 #define SSD1306_COMSCANDEC 0xC8          ///< See datasheet
     87 #define SSD1306_SETDISPLAYOFFSET 0xD3    ///< See datasheet
     88 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5  ///< See datasheet
     89 #define SSD1306_SETPRECHARGE 0xD9        ///< See datasheet
     90 #define SSD1306_SETCOMPINS 0xDA          ///< See datasheet
     91 #define SSD1306_SETVCOMDETECT 0xDB       ///< See datasheet
     92 
     93 #define SSD1306_SETLOWCOLUMN 0x00  ///< Not currently used
     94 #define SSD1306_SETHIGHCOLUMN 0x10 ///< Not currently used
     95 #define SSD1306_SETSTARTLINE 0x40  ///< See datasheet
     96 
     97 #define SSD1306_EXTERNALVCC 0x01  ///< External display voltage source
     98 #define SSD1306_SWITCHCAPVCC 0x02 ///< Gen. display voltage from 3.3V
     99 
    100 #define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26              ///< Init rt scroll
    101 #define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27               ///< Init left scroll
    102 #define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29 ///< Init diag scroll
    103 #define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A  ///< Init diag scroll
    104 #define SSD1306_DEACTIVATE_SCROLL 0x2E                    ///< Stop scroll
    105 #define SSD1306_ACTIVATE_SCROLL 0x2F                      ///< Start scroll
    106 #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3             ///< Set scroll range
    107 
    108 // Deprecated size stuff for backwards compatibility with old sketches
    109 #if defined SSD1306_128_64
    110 #define SSD1306_LCDWIDTH 128 ///< DEPRECATED: width w/SSD1306_128_64 defined
    111 #define SSD1306_LCDHEIGHT 64 ///< DEPRECATED: height w/SSD1306_128_64 defined
    112 #endif
    113 #if defined SSD1306_128_32
    114 #define SSD1306_LCDWIDTH 128 ///< DEPRECATED: width w/SSD1306_128_32 defined
    115 #define SSD1306_LCDHEIGHT 32 ///< DEPRECATED: height w/SSD1306_128_32 defined
    116 #endif
    117 #if defined SSD1306_96_16
    118 #define SSD1306_LCDWIDTH 96  ///< DEPRECATED: width w/SSD1306_96_16 defined
    119 #define SSD1306_LCDHEIGHT 16 ///< DEPRECATED: height w/SSD1306_96_16 defined
    120 #endif
    121 
    122 /*!
    123     @brief  Class that stores state and functions for interacting with
    124             SSD1306 OLED displays.
    125 */
    126 class Adafruit_SSD1306 : public Adafruit_GFX {
    127 public:
    128   // NEW CONSTRUCTORS -- recommended for new projects
    129   Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi = &Wire,
    130                    int8_t rst_pin = -1, uint32_t clkDuring = 400000UL,
    131                    uint32_t clkAfter = 100000UL);
    132   Adafruit_SSD1306(uint8_t w, uint8_t h, int8_t mosi_pin, int8_t sclk_pin,
    133                    int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
    134   Adafruit_SSD1306(uint8_t w, uint8_t h, SPIClass *spi, int8_t dc_pin,
    135                    int8_t rst_pin, int8_t cs_pin, uint32_t bitrate = 8000000UL);
    136 
    137   // DEPRECATED CONSTRUCTORS - for back compatibility, avoid in new projects
    138   Adafruit_SSD1306(int8_t mosi_pin, int8_t sclk_pin, int8_t dc_pin,
    139                    int8_t rst_pin, int8_t cs_pin);
    140   Adafruit_SSD1306(int8_t dc_pin, int8_t rst_pin, int8_t cs_pin);
    141   Adafruit_SSD1306(int8_t rst_pin = -1);
    142 
    143   ~Adafruit_SSD1306(void);
    144 
    145   bool begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC, uint8_t i2caddr = 0,
    146              bool reset = true, bool periphBegin = true);
    147   void display(void);
    148   void clearDisplay(void);
    149   void invertDisplay(bool i);
    150   void dim(bool dim);
    151   void drawPixel(int16_t x, int16_t y, uint16_t color);
    152   virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
    153   virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
    154   void startscrollright(uint8_t start, uint8_t stop);
    155   void startscrollleft(uint8_t start, uint8_t stop);
    156   void startscrolldiagright(uint8_t start, uint8_t stop);
    157   void startscrolldiagleft(uint8_t start, uint8_t stop);
    158   void stopscroll(void);
    159   void ssd1306_command(uint8_t c);
    160   bool getPixel(int16_t x, int16_t y);
    161   uint8_t *getBuffer(void);
    162 
    163 private:
    164   inline void SPIwrite(uint8_t d) __attribute__((always_inline));
    165   void drawFastHLineInternal(int16_t x, int16_t y, int16_t w, uint16_t color);
    166   void drawFastVLineInternal(int16_t x, int16_t y, int16_t h, uint16_t color);
    167   void ssd1306_command1(uint8_t c);
    168   void ssd1306_commandList(const uint8_t *c, uint8_t n);
    169 
    170   SPIClass *spi;
    171   TwoWire *wire;
    172   uint8_t *buffer;
    173   int8_t i2caddr, vccstate, page_end;
    174   int8_t mosiPin, clkPin, dcPin, csPin, rstPin;
    175 #ifdef HAVE_PORTREG
    176   PortReg *mosiPort, *clkPort, *dcPort, *csPort;
    177   PortMask mosiPinMask, clkPinMask, dcPinMask, csPinMask;
    178 #endif
    179 #if ARDUINO >= 157
    180   uint32_t wireClk;    // Wire speed for SSD1306 transfers
    181   uint32_t restoreClk; // Wire speed following SSD1306 transfers
    182 #endif
    183   uint8_t contrast; // normal contrast setting for this device
    184 #if defined(SPI_HAS_TRANSACTION)
    185 protected:
    186   // Allow sub-class to change
    187   SPISettings spiSettings;
    188 #endif
    189 };
    190 
    191 #endif // _Adafruit_SSD1306_H_