A simple yet powerful library for controlling graphical displays. Multiple display controllers are supported using inheritance.
Dependents: mbed_rifletool Hexi_Bubble_Game Hexi_Catch-the-dot_Game Hexi_Acceleromagnetic_Synth
NOTE: This library is in beta right now. As far as I know, everything here works, but there are many features that are lacking so far. Most notably containers, button handling, and display drivers other than the SSD1306.
Drivers/SSD1306_SPI.h@3:a8f72d4864e6, 2014-05-27 (annotated)
- Committer:
- neilt6
- Date:
- Tue May 27 21:41:28 2014 +0000
- Revision:
- 3:a8f72d4864e6
- Parent:
- 2:bbfc18022ee5
Syntax improvements
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
neilt6 | 1:f7003ec66a51 | 1 | /* NeatGUI Library |
neilt6 | 1:f7003ec66a51 | 2 | * Copyright (c) 2013 Neil Thiessen |
neilt6 | 1:f7003ec66a51 | 3 | * |
neilt6 | 1:f7003ec66a51 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
neilt6 | 1:f7003ec66a51 | 5 | * you may not use this file except in compliance with the License. |
neilt6 | 1:f7003ec66a51 | 6 | * You may obtain a copy of the License at |
neilt6 | 1:f7003ec66a51 | 7 | * |
neilt6 | 1:f7003ec66a51 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
neilt6 | 1:f7003ec66a51 | 9 | * |
neilt6 | 1:f7003ec66a51 | 10 | * Unless required by applicable law or agreed to in writing, software |
neilt6 | 1:f7003ec66a51 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
neilt6 | 1:f7003ec66a51 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
neilt6 | 1:f7003ec66a51 | 13 | * See the License for the specific language governing permissions and |
neilt6 | 1:f7003ec66a51 | 14 | * limitations under the License. |
neilt6 | 1:f7003ec66a51 | 15 | */ |
neilt6 | 1:f7003ec66a51 | 16 | |
neilt6 | 1:f7003ec66a51 | 17 | #ifndef SSD1306_SPI_H |
neilt6 | 1:f7003ec66a51 | 18 | #define SSD1306_SPI_H |
neilt6 | 1:f7003ec66a51 | 19 | |
neilt6 | 1:f7003ec66a51 | 20 | #include "mbed.h" |
neilt6 | 1:f7003ec66a51 | 21 | #include "Display.h" |
neilt6 | 1:f7003ec66a51 | 22 | |
neilt6 | 1:f7003ec66a51 | 23 | /** SSD1306_SPI class. |
neilt6 | 1:f7003ec66a51 | 24 | * Used for controlling an SSD1306-based OLED display connected to SPI. |
neilt6 | 1:f7003ec66a51 | 25 | */ |
neilt6 | 1:f7003ec66a51 | 26 | class SSD1306_SPI : public Display |
neilt6 | 1:f7003ec66a51 | 27 | { |
neilt6 | 1:f7003ec66a51 | 28 | public: |
neilt6 | 1:f7003ec66a51 | 29 | |
neilt6 | 1:f7003ec66a51 | 30 | /** Create an SSD1306 object connected to the specified SPI pins with the specified /CS and DC pins |
neilt6 | 1:f7003ec66a51 | 31 | * |
neilt6 | 1:f7003ec66a51 | 32 | * @param mosi The SPI data out pin. |
neilt6 | 1:f7003ec66a51 | 33 | * @param miso The SPI data in pin. |
neilt6 | 1:f7003ec66a51 | 34 | * @param sclk The SPI clock pin. |
neilt6 | 1:f7003ec66a51 | 35 | * @param sclk The SPI chip select pin. |
neilt6 | 1:f7003ec66a51 | 36 | * @param sclk The data/command pin. |
neilt6 | 1:f7003ec66a51 | 37 | */ |
neilt6 | 1:f7003ec66a51 | 38 | SSD1306_SPI(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName dc); |
neilt6 | 1:f7003ec66a51 | 39 | |
neilt6 | 1:f7003ec66a51 | 40 | /** Probe for the SSD1306 and initialize it if present |
neilt6 | 1:f7003ec66a51 | 41 | * |
neilt6 | 1:f7003ec66a51 | 42 | * @returns |
neilt6 | 1:f7003ec66a51 | 43 | * 'true' if the device exists on the bus, |
neilt6 | 1:f7003ec66a51 | 44 | * 'false' if the device doesn't exist on the bus. |
neilt6 | 1:f7003ec66a51 | 45 | */ |
neilt6 | 1:f7003ec66a51 | 46 | virtual bool open(); |
neilt6 | 1:f7003ec66a51 | 47 | |
neilt6 | 1:f7003ec66a51 | 48 | /** Send the buffer to the SSD1306 |
neilt6 | 1:f7003ec66a51 | 49 | */ |
neilt6 | 2:bbfc18022ee5 | 50 | virtual void flush(); |
neilt6 | 1:f7003ec66a51 | 51 | |
neilt6 | 1:f7003ec66a51 | 52 | /** Get the current state of the SSD1306 |
neilt6 | 1:f7003ec66a51 | 53 | * |
neilt6 | 1:f7003ec66a51 | 54 | * @returns The current state as a Display::State enum. |
neilt6 | 1:f7003ec66a51 | 55 | */ |
neilt6 | 1:f7003ec66a51 | 56 | virtual Display::State state(); |
neilt6 | 1:f7003ec66a51 | 57 | |
neilt6 | 1:f7003ec66a51 | 58 | /** Set the state of the SSD1306 |
neilt6 | 1:f7003ec66a51 | 59 | * |
neilt6 | 1:f7003ec66a51 | 60 | * @param mode The new state as a Display::State enum. |
neilt6 | 1:f7003ec66a51 | 61 | */ |
neilt6 | 1:f7003ec66a51 | 62 | virtual void state(State s); |
neilt6 | 1:f7003ec66a51 | 63 | |
neilt6 | 1:f7003ec66a51 | 64 | //void display(); |
neilt6 | 1:f7003ec66a51 | 65 | |
neilt6 | 1:f7003ec66a51 | 66 | /** Draw a single pixel at the specified coordinates |
neilt6 | 1:f7003ec66a51 | 67 | * |
neilt6 | 1:f7003ec66a51 | 68 | * @param x The X coordinate. |
neilt6 | 1:f7003ec66a51 | 69 | * @param y The Y coordinate. |
neilt6 | 1:f7003ec66a51 | 70 | * @param c The color of the pixel as a 32-bit ARGB value. |
neilt6 | 1:f7003ec66a51 | 71 | */ |
neilt6 | 1:f7003ec66a51 | 72 | virtual void drawPixel(int x, int y, unsigned int c); |
neilt6 | 1:f7003ec66a51 | 73 | |
neilt6 | 1:f7003ec66a51 | 74 | private: |
neilt6 | 1:f7003ec66a51 | 75 | //Commands |
neilt6 | 1:f7003ec66a51 | 76 | enum Command { |
neilt6 | 1:f7003ec66a51 | 77 | CMD_SETCONTRAST = 0x81, |
neilt6 | 1:f7003ec66a51 | 78 | CMD_DISPLAYALLON_RESUME = 0xA4, |
neilt6 | 1:f7003ec66a51 | 79 | CMD_DISPLAYALLON = 0xA5, |
neilt6 | 1:f7003ec66a51 | 80 | CMD_NORMALDISPLAY = 0xA6, |
neilt6 | 1:f7003ec66a51 | 81 | CMD_INVERTDISPLAY = 0xA7, |
neilt6 | 1:f7003ec66a51 | 82 | CMD_DISPLAYOFF = 0xAE, |
neilt6 | 1:f7003ec66a51 | 83 | CMD_DISPLAYON = 0xAF, |
neilt6 | 1:f7003ec66a51 | 84 | CMD_SETDISPLAYOFFSET = 0xD3, |
neilt6 | 1:f7003ec66a51 | 85 | CMD_SETCOMPINS = 0xDA, |
neilt6 | 1:f7003ec66a51 | 86 | CMD_SETVCOMDETECT = 0xDB, |
neilt6 | 1:f7003ec66a51 | 87 | CMD_SETDISPLAYCLOCKDIV = 0xD5, |
neilt6 | 1:f7003ec66a51 | 88 | CMD_SETPRECHARGE = 0xD9, |
neilt6 | 1:f7003ec66a51 | 89 | CMD_SETMULTIPLEX = 0xA8, |
neilt6 | 1:f7003ec66a51 | 90 | CMD_SETLOWCOLUMN = 0x00, |
neilt6 | 1:f7003ec66a51 | 91 | CMD_SETHIGHCOLUMN = 0x10, |
neilt6 | 1:f7003ec66a51 | 92 | CMD_SETSTARTLINE = 0x40, |
neilt6 | 1:f7003ec66a51 | 93 | CMD_MEMORYMODE = 0x20, |
neilt6 | 1:f7003ec66a51 | 94 | CMD_COMSCANINC = 0xC0, |
neilt6 | 1:f7003ec66a51 | 95 | CMD_COMSCANDEC = 0xC8, |
neilt6 | 1:f7003ec66a51 | 96 | CMD_SEGREMAP = 0xA0, |
neilt6 | 1:f7003ec66a51 | 97 | CMD_CHARGEPUMP = 0x8D, |
neilt6 | 1:f7003ec66a51 | 98 | CMD_CHARGEPUMPON = 0x14, |
neilt6 | 1:f7003ec66a51 | 99 | CMD_CHARGEPUMPOFF = 0x10, |
neilt6 | 1:f7003ec66a51 | 100 | CMD_ACTIVATE_SCROLL = 0x2F, |
neilt6 | 1:f7003ec66a51 | 101 | CMD_DEACTIVATE_SCROLL = 0x2E, |
neilt6 | 1:f7003ec66a51 | 102 | CMD_SET_VERTICAL_SCROLL_AREA = 0xA3, |
neilt6 | 1:f7003ec66a51 | 103 | CMD_RIGHT_HORIZONTAL_SCROLL = 0x26, |
neilt6 | 1:f7003ec66a51 | 104 | CMD_LEFT_HORIZONTAL_SCROLL = 0x27, |
neilt6 | 1:f7003ec66a51 | 105 | CMD_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL = 0x29, |
neilt6 | 1:f7003ec66a51 | 106 | CMD_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = 0x2A |
neilt6 | 1:f7003ec66a51 | 107 | }; |
neilt6 | 1:f7003ec66a51 | 108 | |
neilt6 | 1:f7003ec66a51 | 109 | //SPI interface variables |
neilt6 | 1:f7003ec66a51 | 110 | SPI m_SPI; |
neilt6 | 1:f7003ec66a51 | 111 | DigitalOut m_CS; |
neilt6 | 1:f7003ec66a51 | 112 | DigitalOut m_DC; |
neilt6 | 1:f7003ec66a51 | 113 | |
neilt6 | 1:f7003ec66a51 | 114 | //Back buffer |
neilt6 | 1:f7003ec66a51 | 115 | char m_Buffer[1024]; |
neilt6 | 1:f7003ec66a51 | 116 | |
neilt6 | 1:f7003ec66a51 | 117 | //Command and data helpers |
neilt6 | 1:f7003ec66a51 | 118 | void writeCommand(char command); |
neilt6 | 1:f7003ec66a51 | 119 | void writeData(char data); |
neilt6 | 1:f7003ec66a51 | 120 | }; |
neilt6 | 1:f7003ec66a51 | 121 | |
neilt6 | 1:f7003ec66a51 | 122 | #endif |