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_I2C.h"
neilt6 1:f7003ec66a51 18
neilt6 1:f7003ec66a51 19 SSD1306_I2C::SSD1306_I2C(PinName sda, PinName scl, Address addr) : Display(128, 64), m_I2C(sda, scl), m_ADDR((int)addr)
neilt6 1:f7003ec66a51 20 {
neilt6 1:f7003ec66a51 21 //Set the I2C frequency to 400kHz
neilt6 1:f7003ec66a51 22 m_I2C.frequency(400000);
neilt6 1:f7003ec66a51 23 }
neilt6 1:f7003ec66a51 24
neilt6 1:f7003ec66a51 25 bool SSD1306_I2C::open()
neilt6 1:f7003ec66a51 26 {
neilt6 1:f7003ec66a51 27 //Probe for the SSD1306 using a Zero Length Transfer
neilt6 1:f7003ec66a51 28 if (!m_I2C.write(m_ADDR, NULL, 0)) {
neilt6 1:f7003ec66a51 29 //Init sequence for 128x64 OLED module
neilt6 1:f7003ec66a51 30 writeCommand(CMD_DISPLAYOFF);
neilt6 1:f7003ec66a51 31 writeCommand(CMD_SETDISPLAYCLOCKDIV);
neilt6 1:f7003ec66a51 32 writeCommand(0x80);
neilt6 1:f7003ec66a51 33 writeCommand(CMD_SETMULTIPLEX);
neilt6 1:f7003ec66a51 34 writeCommand(0x3F);
neilt6 1:f7003ec66a51 35 writeCommand(CMD_SETDISPLAYOFFSET);
neilt6 1:f7003ec66a51 36 writeCommand(0x0);
neilt6 1:f7003ec66a51 37 writeCommand(CMD_SETSTARTLINE | 0x0);
neilt6 1:f7003ec66a51 38 writeCommand(CMD_CHARGEPUMP);
neilt6 1:f7003ec66a51 39 writeCommand(CMD_CHARGEPUMPON);
neilt6 1:f7003ec66a51 40 writeCommand(CMD_MEMORYMODE);
neilt6 1:f7003ec66a51 41 writeCommand(0x00);
neilt6 1:f7003ec66a51 42 writeCommand(CMD_SEGREMAP | 0x1);
neilt6 1:f7003ec66a51 43 writeCommand(CMD_COMSCANDEC);
neilt6 1:f7003ec66a51 44 writeCommand(CMD_SETCOMPINS);
neilt6 1:f7003ec66a51 45 writeCommand(0x12);
neilt6 1:f7003ec66a51 46 writeCommand(CMD_SETCONTRAST);
neilt6 1:f7003ec66a51 47 writeCommand(0xCF);
neilt6 1:f7003ec66a51 48 writeCommand(CMD_SETPRECHARGE);
neilt6 1:f7003ec66a51 49 writeCommand(0xF1);
neilt6 1:f7003ec66a51 50 writeCommand(CMD_SETVCOMDETECT);
neilt6 1:f7003ec66a51 51 writeCommand(0x40);
neilt6 1:f7003ec66a51 52 writeCommand(CMD_DISPLAYALLON_RESUME);
neilt6 1:f7003ec66a51 53 writeCommand(CMD_NORMALDISPLAY);
neilt6 1:f7003ec66a51 54
neilt6 1:f7003ec66a51 55 //Return success
neilt6 1:f7003ec66a51 56 return true;
neilt6 1:f7003ec66a51 57 } else {
neilt6 1:f7003ec66a51 58 //Return failure
neilt6 1:f7003ec66a51 59 return false;
neilt6 1:f7003ec66a51 60 }
neilt6 1:f7003ec66a51 61 }
neilt6 1:f7003ec66a51 62
neilt6 2:bbfc18022ee5 63 void SSD1306_I2C::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 //Make sure the first byte in the buffer is the control byte
neilt6 1:f7003ec66a51 71 m_Buffer[0] = CONTROL_DATA;
neilt6 1:f7003ec66a51 72
neilt6 1:f7003ec66a51 73 //Write the buffer
neilt6 1:f7003ec66a51 74 m_I2C.write(m_ADDR, m_Buffer, 1025);
neilt6 1:f7003ec66a51 75 }
neilt6 1:f7003ec66a51 76
neilt6 1:f7003ec66a51 77 Display::State SSD1306_I2C::state()
neilt6 1:f7003ec66a51 78 {
neilt6 1:f7003ec66a51 79 //Return the base class's state
neilt6 1:f7003ec66a51 80 return Display::state();
neilt6 1:f7003ec66a51 81 }
neilt6 1:f7003ec66a51 82
neilt6 1:f7003ec66a51 83 void SSD1306_I2C::state(State s)
neilt6 1:f7003ec66a51 84 {
neilt6 1:f7003ec66a51 85 //Check what the requested state is
neilt6 1:f7003ec66a51 86 if (s == Display::DISPLAY_ON) {
neilt6 1:f7003ec66a51 87 //Turn the display on
neilt6 1:f7003ec66a51 88 writeCommand(CMD_DISPLAYON);
neilt6 1:f7003ec66a51 89 } else if (s == Display::DISPLAY_OFF) {
neilt6 1:f7003ec66a51 90 //Turn the display off
neilt6 1:f7003ec66a51 91 writeCommand(CMD_DISPLAYOFF);
neilt6 1:f7003ec66a51 92 }
neilt6 1:f7003ec66a51 93
neilt6 1:f7003ec66a51 94 //Update the base class
neilt6 1:f7003ec66a51 95 Display::state(s);
neilt6 1:f7003ec66a51 96 }
neilt6 1:f7003ec66a51 97
neilt6 1:f7003ec66a51 98 void SSD1306_I2C::drawPixel(int x, int y, unsigned int c)
neilt6 1:f7003ec66a51 99 {
neilt6 1:f7003ec66a51 100 //Range check the pixel
neilt6 1:f7003ec66a51 101 if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
neilt6 1:f7003ec66a51 102 return;
neilt6 1:f7003ec66a51 103
neilt6 1:f7003ec66a51 104 //Make sure the color is fully opaque
neilt6 1:f7003ec66a51 105 if ((c >> 24) != 255)
neilt6 1:f7003ec66a51 106 return;
neilt6 1:f7003ec66a51 107
neilt6 1:f7003ec66a51 108 //Determine the pixel byte index
neilt6 1:f7003ec66a51 109 unsigned short byteIndex = x + (y / 8) * width();
neilt6 1:f7003ec66a51 110
neilt6 1:f7003ec66a51 111 //HACK: Fix the whole 1025 byte i2c buffer thing
neilt6 1:f7003ec66a51 112 byteIndex++;
neilt6 1:f7003ec66a51 113
neilt6 1:f7003ec66a51 114 //Set or clear the pixel
neilt6 1:f7003ec66a51 115 if ((c & 0x00FFFFFF) > 0)
neilt6 1:f7003ec66a51 116 m_Buffer[byteIndex] |= (1 << (y % 8));
neilt6 1:f7003ec66a51 117 else
neilt6 1:f7003ec66a51 118 m_Buffer[byteIndex] &= ~(1 << (y % 8));
neilt6 1:f7003ec66a51 119 }
neilt6 1:f7003ec66a51 120
neilt6 1:f7003ec66a51 121 void SSD1306_I2C::writeCommand(char command)
neilt6 1:f7003ec66a51 122 {
neilt6 1:f7003ec66a51 123 //Create a temporary buffer
neilt6 1:f7003ec66a51 124 char buff[2];
neilt6 1:f7003ec66a51 125
neilt6 1:f7003ec66a51 126 //Load the control byte and 8-bit command
neilt6 1:f7003ec66a51 127 buff[0] = CONTROL_COMMAND;
neilt6 1:f7003ec66a51 128 buff[1] = command;
neilt6 1:f7003ec66a51 129
neilt6 1:f7003ec66a51 130 //Write the command
neilt6 1:f7003ec66a51 131 m_I2C.write(m_ADDR, buff, 2);
neilt6 1:f7003ec66a51 132 }
neilt6 1:f7003ec66a51 133
neilt6 1:f7003ec66a51 134 void SSD1306_I2C::writeData(char data)
neilt6 1:f7003ec66a51 135 {
neilt6 1:f7003ec66a51 136 //Create a temporary buffer
neilt6 1:f7003ec66a51 137 char buff[2];
neilt6 1:f7003ec66a51 138
neilt6 1:f7003ec66a51 139 //Load the control byte and 8-bit data
neilt6 1:f7003ec66a51 140 buff[0] = CONTROL_DATA;
neilt6 1:f7003ec66a51 141 buff[1] = data;
neilt6 1:f7003ec66a51 142
neilt6 1:f7003ec66a51 143 //Write the data
neilt6 1:f7003ec66a51 144 m_I2C.write(m_ADDR, buff, 2);
neilt6 1:f7003ec66a51 145 }