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.

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?

UserRevisionLine numberNew 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 #include "SSD1306_SPI.h"
neilt6 1:f7003ec66a51 18
neilt6 1:f7003ec66a51 19 SSD1306_SPI::SSD1306_SPI(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName dc) : Display(128, 64), m_SPI(mosi, miso, sclk), m_CS(cs), m_DC(dc)
neilt6 1:f7003ec66a51 20 {
neilt6 1:f7003ec66a51 21 //Deselect the display
neilt6 1:f7003ec66a51 22 m_CS = 1;
neilt6 1:f7003ec66a51 23
neilt6 1:f7003ec66a51 24 //Set the SPI format to 8 bit data, high steady state clock, second edge capture
neilt6 1:f7003ec66a51 25 m_SPI.format(8, 3);
neilt6 1:f7003ec66a51 26
neilt6 1:f7003ec66a51 27 //Set the SPI frequency to 10MHz
neilt6 1:f7003ec66a51 28 m_SPI.frequency(10000000);
neilt6 1:f7003ec66a51 29 }
neilt6 1:f7003ec66a51 30
neilt6 1:f7003ec66a51 31 bool SSD1306_SPI::open()
neilt6 1:f7003ec66a51 32 {
neilt6 1:f7003ec66a51 33 //Init sequence for 128x64 OLED module
neilt6 1:f7003ec66a51 34 writeCommand(CMD_DISPLAYOFF);
neilt6 1:f7003ec66a51 35 writeCommand(CMD_SETDISPLAYCLOCKDIV);
neilt6 1:f7003ec66a51 36 writeCommand(0x80);
neilt6 1:f7003ec66a51 37 writeCommand(CMD_SETMULTIPLEX);
neilt6 1:f7003ec66a51 38 writeCommand(0x3F);
neilt6 1:f7003ec66a51 39 writeCommand(CMD_SETDISPLAYOFFSET);
neilt6 1:f7003ec66a51 40 writeCommand(0x0);
neilt6 1:f7003ec66a51 41 writeCommand(CMD_SETSTARTLINE | 0x0);
neilt6 1:f7003ec66a51 42 writeCommand(CMD_CHARGEPUMP);
neilt6 1:f7003ec66a51 43 writeCommand(CMD_CHARGEPUMPON);
neilt6 1:f7003ec66a51 44 writeCommand(CMD_MEMORYMODE);
neilt6 1:f7003ec66a51 45 writeCommand(0x00);
neilt6 1:f7003ec66a51 46 writeCommand(CMD_SEGREMAP | 0x1);
neilt6 1:f7003ec66a51 47 writeCommand(CMD_COMSCANDEC);
neilt6 1:f7003ec66a51 48 writeCommand(CMD_SETCOMPINS);
neilt6 1:f7003ec66a51 49 writeCommand(0x12);
neilt6 1:f7003ec66a51 50 writeCommand(CMD_SETCONTRAST);
neilt6 1:f7003ec66a51 51 writeCommand(0xCF);
neilt6 1:f7003ec66a51 52 writeCommand(CMD_SETPRECHARGE);
neilt6 1:f7003ec66a51 53 writeCommand(0xF1);
neilt6 1:f7003ec66a51 54 writeCommand(CMD_SETVCOMDETECT);
neilt6 1:f7003ec66a51 55 writeCommand(0x40);
neilt6 1:f7003ec66a51 56 writeCommand(CMD_DISPLAYALLON_RESUME);
neilt6 1:f7003ec66a51 57 writeCommand(CMD_NORMALDISPLAY);
neilt6 1:f7003ec66a51 58
neilt6 1:f7003ec66a51 59 //Return success
neilt6 1:f7003ec66a51 60 return true;
neilt6 1:f7003ec66a51 61 }
neilt6 1:f7003ec66a51 62
neilt6 2:bbfc18022ee5 63 void SSD1306_SPI::flush()
neilt6 1:f7003ec66a51 64 {
neilt6 1:f7003ec66a51 65 //Select low col 0, hi col 0, line 0
neilt6 1:f7003ec66a51 66 writeCommand(CMD_SETLOWCOLUMN | 0x0);
neilt6 1:f7003ec66a51 67 writeCommand(CMD_SETHIGHCOLUMN | 0x0);
neilt6 1:f7003ec66a51 68 writeCommand(CMD_SETSTARTLINE | 0x0);
neilt6 1:f7003ec66a51 69
neilt6 1:f7003ec66a51 70 //Set DC to data and select the chip
neilt6 1:f7003ec66a51 71 m_DC = 1;
neilt6 1:f7003ec66a51 72 m_CS = 0;
neilt6 1:f7003ec66a51 73
neilt6 1:f7003ec66a51 74 //Write the entire buffer at once
neilt6 1:f7003ec66a51 75 for (int i = 0; i < 1024; i++)
neilt6 1:f7003ec66a51 76 m_SPI.write(m_Buffer[i]);
neilt6 1:f7003ec66a51 77
neilt6 1:f7003ec66a51 78 //Deselect the chip
neilt6 1:f7003ec66a51 79 m_CS = 1;
neilt6 1:f7003ec66a51 80 }
neilt6 1:f7003ec66a51 81
neilt6 1:f7003ec66a51 82 Display::State SSD1306_SPI::state()
neilt6 1:f7003ec66a51 83 {
neilt6 1:f7003ec66a51 84 //Return the base class's state
neilt6 1:f7003ec66a51 85 return Display::state();
neilt6 1:f7003ec66a51 86 }
neilt6 1:f7003ec66a51 87
neilt6 1:f7003ec66a51 88 void SSD1306_SPI::state(State s)
neilt6 1:f7003ec66a51 89 {
neilt6 1:f7003ec66a51 90 //Check what the requested state is
neilt6 1:f7003ec66a51 91 if (s == Display::DISPLAY_ON) {
neilt6 1:f7003ec66a51 92 //Turn the display on
neilt6 1:f7003ec66a51 93 writeCommand(CMD_DISPLAYON);
neilt6 1:f7003ec66a51 94 } else if (s == Display::DISPLAY_OFF) {
neilt6 1:f7003ec66a51 95 //Turn the display off
neilt6 1:f7003ec66a51 96 writeCommand(CMD_DISPLAYOFF);
neilt6 1:f7003ec66a51 97 }
neilt6 1:f7003ec66a51 98
neilt6 1:f7003ec66a51 99 //Update the base class
neilt6 1:f7003ec66a51 100 Display::state(s);
neilt6 1:f7003ec66a51 101 }
neilt6 1:f7003ec66a51 102
neilt6 1:f7003ec66a51 103 void SSD1306_SPI::drawPixel(int x, int y, unsigned int c)
neilt6 1:f7003ec66a51 104 {
neilt6 1:f7003ec66a51 105 //Range check the pixel
neilt6 1:f7003ec66a51 106 if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
neilt6 1:f7003ec66a51 107 return;
neilt6 1:f7003ec66a51 108
neilt6 1:f7003ec66a51 109 //Make sure the color is fully opaque
neilt6 1:f7003ec66a51 110 if ((c >> 24) != 255)
neilt6 1:f7003ec66a51 111 return;
neilt6 1:f7003ec66a51 112
neilt6 1:f7003ec66a51 113 //Determine the pixel byte index
neilt6 1:f7003ec66a51 114 unsigned short byteIndex = x + (y / 8) * width();
neilt6 1:f7003ec66a51 115
neilt6 1:f7003ec66a51 116 //Set or clear the pixel
neilt6 1:f7003ec66a51 117 if ((c & 0x00FFFFFF) > 0)
neilt6 1:f7003ec66a51 118 m_Buffer[byteIndex] |= (1 << (y % 8));
neilt6 1:f7003ec66a51 119 else
neilt6 1:f7003ec66a51 120 m_Buffer[byteIndex] &= ~(1 << (y % 8));
neilt6 1:f7003ec66a51 121 }
neilt6 1:f7003ec66a51 122
neilt6 1:f7003ec66a51 123 void SSD1306_SPI::writeCommand(char command)
neilt6 1:f7003ec66a51 124 {
neilt6 1:f7003ec66a51 125 //Set DC to command and select the display
neilt6 1:f7003ec66a51 126 m_DC = 0;
neilt6 1:f7003ec66a51 127 m_CS = 0;
neilt6 1:f7003ec66a51 128
neilt6 1:f7003ec66a51 129 //Write the command byte
neilt6 1:f7003ec66a51 130 m_SPI.write(command);
neilt6 1:f7003ec66a51 131
neilt6 1:f7003ec66a51 132 //Deselect the display
neilt6 1:f7003ec66a51 133 m_CS = 1;
neilt6 1:f7003ec66a51 134 }
neilt6 1:f7003ec66a51 135
neilt6 1:f7003ec66a51 136 void SSD1306_SPI::writeData(char data)
neilt6 1:f7003ec66a51 137 {
neilt6 1:f7003ec66a51 138 //Set DC to data and select the display
neilt6 1:f7003ec66a51 139 m_DC = 1;
neilt6 1:f7003ec66a51 140 m_CS = 0;
neilt6 1:f7003ec66a51 141
neilt6 1:f7003ec66a51 142 //Write the data byte
neilt6 1:f7003ec66a51 143 m_SPI.write(data);
neilt6 1:f7003ec66a51 144
neilt6 1:f7003ec66a51 145 //Deselect the display
neilt6 1:f7003ec66a51 146 m_CS = 1;
neilt6 1:f7003ec66a51 147 }