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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SSD1306_SPI.h Source File

SSD1306_SPI.h

00001 /* NeatGUI Library
00002  * Copyright (c) 2013 Neil Thiessen
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef SSD1306_SPI_H
00018 #define SSD1306_SPI_H
00019 
00020 #include "mbed.h"
00021 #include "Display.h"
00022 
00023 /** SSD1306_SPI class.
00024  *  Used for controlling an SSD1306-based OLED display connected to SPI.
00025  */
00026 class SSD1306_SPI : public Display
00027 {
00028 public:
00029 
00030     /** Create an SSD1306 object connected to the specified SPI pins with the specified /CS and DC pins
00031      *
00032      * @param mosi The SPI data out pin.
00033      * @param miso The SPI data in pin.
00034      * @param sclk The SPI clock pin.
00035      * @param sclk The SPI chip select pin.
00036      * @param sclk The data/command pin.
00037      */
00038     SSD1306_SPI(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName dc);
00039 
00040     /** Probe for the SSD1306 and initialize it if present
00041      *
00042      * @returns
00043      *   'true' if the device exists on the bus,
00044      *   'false' if the device doesn't exist on the bus.
00045      */
00046     virtual bool open();
00047 
00048     /** Send the buffer to the SSD1306
00049      */
00050     virtual void flush();
00051 
00052     /** Get the current state of the SSD1306
00053      *
00054      * @returns The current state as a Display::State enum.
00055      */
00056     virtual Display::State state();
00057 
00058     /** Set the state of the SSD1306
00059      *
00060      * @param mode The new state as a Display::State enum.
00061      */
00062     virtual void state(State s);
00063 
00064     //void display();
00065 
00066     /** Draw a single pixel at the specified coordinates
00067     *
00068     * @param x The X coordinate.
00069     * @param y The Y coordinate.
00070     * @param c The color of the pixel as a 32-bit ARGB value.
00071     */
00072     virtual void drawPixel(int x, int y, unsigned int c);
00073 
00074 private:
00075     //Commands
00076     enum Command {
00077         CMD_SETCONTRAST                             = 0x81,
00078         CMD_DISPLAYALLON_RESUME                     = 0xA4,
00079         CMD_DISPLAYALLON                            = 0xA5,
00080         CMD_NORMALDISPLAY                           = 0xA6,
00081         CMD_INVERTDISPLAY                           = 0xA7,
00082         CMD_DISPLAYOFF                              = 0xAE,
00083         CMD_DISPLAYON                               = 0xAF,
00084         CMD_SETDISPLAYOFFSET                        = 0xD3,
00085         CMD_SETCOMPINS                              = 0xDA,
00086         CMD_SETVCOMDETECT                           = 0xDB,
00087         CMD_SETDISPLAYCLOCKDIV                      = 0xD5,
00088         CMD_SETPRECHARGE                            = 0xD9,
00089         CMD_SETMULTIPLEX                            = 0xA8,
00090         CMD_SETLOWCOLUMN                            = 0x00,
00091         CMD_SETHIGHCOLUMN                           = 0x10,
00092         CMD_SETSTARTLINE                            = 0x40,
00093         CMD_MEMORYMODE                              = 0x20,
00094         CMD_COMSCANINC                              = 0xC0,
00095         CMD_COMSCANDEC                              = 0xC8,
00096         CMD_SEGREMAP                                = 0xA0,
00097         CMD_CHARGEPUMP                              = 0x8D,
00098         CMD_CHARGEPUMPON                            = 0x14,
00099         CMD_CHARGEPUMPOFF                           = 0x10,
00100         CMD_ACTIVATE_SCROLL                         = 0x2F,
00101         CMD_DEACTIVATE_SCROLL                       = 0x2E,
00102         CMD_SET_VERTICAL_SCROLL_AREA                = 0xA3,
00103         CMD_RIGHT_HORIZONTAL_SCROLL                 = 0x26,
00104         CMD_LEFT_HORIZONTAL_SCROLL                  = 0x27,
00105         CMD_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL    = 0x29,
00106         CMD_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL     = 0x2A
00107     };
00108 
00109     //SPI interface variables
00110     SPI m_SPI;
00111     DigitalOut m_CS;
00112     DigitalOut m_DC;
00113 
00114     //Back buffer
00115     char m_Buffer[1024];
00116 
00117     //Command and data helpers
00118     void writeCommand(char command);
00119     void writeData(char data);
00120 };
00121 
00122 #endif