Arduino style GUI

Committer:
jonebuckman
Date:
Wed Feb 27 22:34:06 2019 +0000
Revision:
4:d353b314d244
Parent:
0:90962b684403
Updated writeCommand and writeData.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jonebuckman 0:90962b684403 1 /* NeatGUI Library
jonebuckman 0:90962b684403 2 * Copyright (c) 2013 Neil Thiessen
jonebuckman 0:90962b684403 3 *
jonebuckman 0:90962b684403 4 * Licensed under the Apache License, Version 2.0 (the "License");
jonebuckman 0:90962b684403 5 * you may not use this file except in compliance with the License.
jonebuckman 0:90962b684403 6 * You may obtain a copy of the License at
jonebuckman 0:90962b684403 7 *
jonebuckman 0:90962b684403 8 * http://www.apache.org/licenses/LICENSE-2.0
jonebuckman 0:90962b684403 9 *
jonebuckman 0:90962b684403 10 * Unless required by applicable law or agreed to in writing, software
jonebuckman 0:90962b684403 11 * distributed under the License is distributed on an "AS IS" BASIS,
jonebuckman 0:90962b684403 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jonebuckman 0:90962b684403 13 * See the License for the specific language governing permissions and
jonebuckman 0:90962b684403 14 * limitations under the License.
jonebuckman 0:90962b684403 15 */
jonebuckman 0:90962b684403 16
jonebuckman 0:90962b684403 17 #include "SSD1306_SPI.h"
jonebuckman 0:90962b684403 18
jonebuckman 0:90962b684403 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)
jonebuckman 0:90962b684403 20 {
jonebuckman 0:90962b684403 21 //Deselect the display
jonebuckman 0:90962b684403 22 m_CS = 1;
jonebuckman 0:90962b684403 23
jonebuckman 0:90962b684403 24 //Set the SPI format to 8 bit data, high steady state clock, second edge capture
jonebuckman 0:90962b684403 25 m_SPI.format(8, 3);
jonebuckman 0:90962b684403 26
jonebuckman 0:90962b684403 27 //Set the SPI frequency to 10MHz
jonebuckman 0:90962b684403 28 m_SPI.frequency(10000000);
jonebuckman 0:90962b684403 29 }
jonebuckman 0:90962b684403 30
jonebuckman 0:90962b684403 31 bool SSD1306_SPI::open()
jonebuckman 0:90962b684403 32 {
jonebuckman 0:90962b684403 33 //Init sequence for 128x64 OLED module
jonebuckman 0:90962b684403 34 writeCommand(CMD_DISPLAYOFF);
jonebuckman 0:90962b684403 35 writeCommand(CMD_SETDISPLAYCLOCKDIV);
jonebuckman 0:90962b684403 36 writeCommand(0x80);
jonebuckman 0:90962b684403 37 writeCommand(CMD_SETMULTIPLEX);
jonebuckman 0:90962b684403 38 writeCommand(0x3F);
jonebuckman 0:90962b684403 39 writeCommand(CMD_SETDISPLAYOFFSET);
jonebuckman 0:90962b684403 40 writeCommand(0x0);
jonebuckman 0:90962b684403 41 writeCommand(CMD_SETSTARTLINE | 0x0);
jonebuckman 0:90962b684403 42 writeCommand(CMD_CHARGEPUMP);
jonebuckman 0:90962b684403 43 writeCommand(CMD_CHARGEPUMPON);
jonebuckman 0:90962b684403 44 writeCommand(CMD_MEMORYMODE);
jonebuckman 0:90962b684403 45 writeCommand(0x00);
jonebuckman 0:90962b684403 46 writeCommand(CMD_SEGREMAP | 0x1);
jonebuckman 0:90962b684403 47 writeCommand(CMD_COMSCANDEC);
jonebuckman 0:90962b684403 48 writeCommand(CMD_SETCOMPINS);
jonebuckman 0:90962b684403 49 writeCommand(0x12);
jonebuckman 0:90962b684403 50 writeCommand(CMD_SETCONTRAST);
jonebuckman 0:90962b684403 51 writeCommand(0xCF);
jonebuckman 0:90962b684403 52 writeCommand(CMD_SETPRECHARGE);
jonebuckman 0:90962b684403 53 writeCommand(0xF1);
jonebuckman 0:90962b684403 54 writeCommand(CMD_SETVCOMDETECT);
jonebuckman 0:90962b684403 55 writeCommand(0x40);
jonebuckman 0:90962b684403 56 writeCommand(CMD_DISPLAYALLON_RESUME);
jonebuckman 0:90962b684403 57 writeCommand(CMD_NORMALDISPLAY);
jonebuckman 0:90962b684403 58
jonebuckman 0:90962b684403 59 //Return success
jonebuckman 0:90962b684403 60 return true;
jonebuckman 0:90962b684403 61 }
jonebuckman 0:90962b684403 62
jonebuckman 0:90962b684403 63 void SSD1306_SPI::flush()
jonebuckman 0:90962b684403 64 {
jonebuckman 0:90962b684403 65 //Select low col 0, hi col 0, line 0
jonebuckman 0:90962b684403 66 writeCommand(CMD_SETLOWCOLUMN | 0x0);
jonebuckman 0:90962b684403 67 writeCommand(CMD_SETHIGHCOLUMN | 0x0);
jonebuckman 0:90962b684403 68 writeCommand(CMD_SETSTARTLINE | 0x0);
jonebuckman 0:90962b684403 69
jonebuckman 0:90962b684403 70 //Set DC to data and select the chip
jonebuckman 0:90962b684403 71 m_DC = 1;
jonebuckman 0:90962b684403 72 m_CS = 0;
jonebuckman 0:90962b684403 73
jonebuckman 0:90962b684403 74 //Write the entire buffer at once
jonebuckman 0:90962b684403 75 for (int i = 0; i < 1024; i++)
jonebuckman 0:90962b684403 76 m_SPI.write(m_Buffer[i]);
jonebuckman 0:90962b684403 77
jonebuckman 0:90962b684403 78 //Deselect the chip
jonebuckman 0:90962b684403 79 m_CS = 1;
jonebuckman 0:90962b684403 80 }
jonebuckman 0:90962b684403 81
jonebuckman 0:90962b684403 82 Display::State SSD1306_SPI::state()
jonebuckman 0:90962b684403 83 {
jonebuckman 0:90962b684403 84 //Return the base class's state
jonebuckman 0:90962b684403 85 return Display::state();
jonebuckman 0:90962b684403 86 }
jonebuckman 0:90962b684403 87
jonebuckman 0:90962b684403 88 void SSD1306_SPI::state(State s)
jonebuckman 0:90962b684403 89 {
jonebuckman 0:90962b684403 90 //Check what the requested state is
jonebuckman 0:90962b684403 91 if (s == Display::DISPLAY_ON) {
jonebuckman 0:90962b684403 92 //Turn the display on
jonebuckman 0:90962b684403 93 writeCommand(CMD_DISPLAYON);
jonebuckman 0:90962b684403 94 } else if (s == Display::DISPLAY_OFF) {
jonebuckman 0:90962b684403 95 //Turn the display off
jonebuckman 0:90962b684403 96 writeCommand(CMD_DISPLAYOFF);
jonebuckman 0:90962b684403 97 }
jonebuckman 0:90962b684403 98
jonebuckman 0:90962b684403 99 //Update the base class
jonebuckman 0:90962b684403 100 Display::state(s);
jonebuckman 0:90962b684403 101 }
jonebuckman 0:90962b684403 102
jonebuckman 0:90962b684403 103 void SSD1306_SPI::drawPixel(int x, int y, unsigned int c)
jonebuckman 0:90962b684403 104 {
jonebuckman 0:90962b684403 105 //Range check the pixel
jonebuckman 0:90962b684403 106 if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
jonebuckman 0:90962b684403 107 return;
jonebuckman 0:90962b684403 108
jonebuckman 0:90962b684403 109 //Make sure the color is fully opaque
jonebuckman 0:90962b684403 110 if ((c >> 24) != 255)
jonebuckman 0:90962b684403 111 return;
jonebuckman 0:90962b684403 112
jonebuckman 0:90962b684403 113 //Determine the pixel byte index
jonebuckman 0:90962b684403 114 unsigned short byteIndex = x + (y / 8) * width();
jonebuckman 0:90962b684403 115
jonebuckman 0:90962b684403 116 //Set or clear the pixel
jonebuckman 0:90962b684403 117 if ((c & 0x00FFFFFF) > 0)
jonebuckman 0:90962b684403 118 m_Buffer[byteIndex] |= (1 << (y % 8));
jonebuckman 0:90962b684403 119 else
jonebuckman 0:90962b684403 120 m_Buffer[byteIndex] &= ~(1 << (y % 8));
jonebuckman 0:90962b684403 121 }
jonebuckman 0:90962b684403 122
jonebuckman 0:90962b684403 123 void SSD1306_SPI::writeCommand(char command)
jonebuckman 0:90962b684403 124 {
jonebuckman 0:90962b684403 125 //Set DC to command and select the display
jonebuckman 0:90962b684403 126 m_DC = 0;
jonebuckman 0:90962b684403 127 m_CS = 0;
jonebuckman 0:90962b684403 128
jonebuckman 0:90962b684403 129 //Write the command byte
jonebuckman 0:90962b684403 130 m_SPI.write(command);
jonebuckman 0:90962b684403 131
jonebuckman 0:90962b684403 132 //Deselect the display
jonebuckman 0:90962b684403 133 m_CS = 1;
jonebuckman 0:90962b684403 134 }
jonebuckman 0:90962b684403 135
jonebuckman 0:90962b684403 136 void SSD1306_SPI::writeData(char data)
jonebuckman 0:90962b684403 137 {
jonebuckman 0:90962b684403 138 //Set DC to data and select the display
jonebuckman 0:90962b684403 139 m_DC = 1;
jonebuckman 0:90962b684403 140 m_CS = 0;
jonebuckman 0:90962b684403 141
jonebuckman 0:90962b684403 142 //Write the data byte
jonebuckman 0:90962b684403 143 m_SPI.write(data);
jonebuckman 0:90962b684403 144
jonebuckman 0:90962b684403 145 //Deselect the display
jonebuckman 0:90962b684403 146 m_CS = 1;
jonebuckman 0:90962b684403 147 }