Jon Buckman / NeatGUIs
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SSD1353_SPI.cpp Source File

SSD1353_SPI.cpp

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 #include "SSD1353_SPI.h"
00018 
00019 SSD1353_SPI::SSD1353_SPI(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName dc) : Display(162, 132 ),  m_SPI(mosi, miso, sclk), m_CS(cs), m_DC(dc)
00020 {
00021     //Deselect the display
00022     m_CS = 1;
00023  
00024     //Set the SPI format to 8 bit data, high steady state clock, second edge capture
00025     m_SPI.format(8, 3);
00026  
00027     //Set the SPI frequency to 4MHz
00028     m_SPI.frequency(4000000);
00029 }
00030  
00031 bool SSD1353_SPI::open()
00032 {
00033     //Init sequence for 160x128 OLED module
00034     writeCommand(CMD_SOFTRESET);
00035     writeCommand(CMD_COMMANDLOCK);
00036     writeData(0x00);
00037     writeCommand(CMD_DISPLAYOFF);
00038     writeCommand(CMD_CLOCKDIV);
00039     writeCommand(0xF1);
00040     writeCommand(CMD_MUXRATIO);
00041     writeData(0x83);
00042     writeCommand(CMD_SETREMAP); // 0xA0
00043     writeData(0xB4);    // B4
00044     writeCommand(CMD_SETCOLUMN);
00045     writeData(0x00);
00046     writeData(0x9F);
00047     writeCommand(CMD_SETROW);
00048     writeData(0x00);
00049     writeData(0x7F);
00050     writeCommand(CMD_STARTLINE);
00051     writeData(0x0);
00052     writeCommand(CMD_DISPLAYOFFSET);
00053     writeData(0x0);
00054     writeCommand(CMD_VCOMH);
00055     writeData(0x3C);    // 2A
00056     writeCommand(CMD_NORMALDISPLAY);
00057     writeCommand(CMD_CONTRASTA);
00058     writeData(0xc8);    // C8
00059     writeCommand(CMD_CONTRASTB);
00060     writeData(0xe8);    // 80
00061     writeCommand(CMD_CONTRASTC);
00062     writeData(0xe8);    // C8
00063     writeCommand(CMD_MASTERCURRENT);
00064     writeData(0x0F);    // 0x0E
00065     writeCommand(CMD_PRECHARGESPEED2);
00066     writeData(0x02);
00067     writeCommand(CMD_PHASE12PERIOD);    // 0xB1
00068     writeData(0x21);    // 0x21
00069 
00070     //Return success
00071     return true;
00072 }
00073  
00074 void SSD1353_SPI::flush()
00075 {
00076     if (m_CacheIndex > 0) {
00077         //Set DC to data and select the display
00078         m_DC = 1;
00079         m_CS = 0;
00080  
00081         //Write the entire cache at once
00082         for (int i = 0; i < m_CacheIndex; i++)
00083             m_SPI.write(m_Cache[i]);
00084  
00085         //Deselect the display
00086         m_CS = 1;
00087  
00088         //Reset the cache index
00089         m_CacheIndex = 0;
00090     }
00091 }
00092  
00093 Display::State SSD1353_SPI::state()
00094 {
00095     //Return the base class's state
00096     return Display::state();
00097 }
00098  
00099 void SSD1353_SPI::state(State s)
00100 {
00101     //Check what the requested state is
00102     if (s == Display::DISPLAY_ON) {
00103         //Turn the display on
00104         writeCommand(CMD_DISPLAYON);
00105     } else if (s == Display::DISPLAY_OFF) {
00106         //Turn the display off
00107         writeCommand(CMD_DISPLAYOFF);
00108     }
00109  
00110     //Update the base class
00111     Display::state(s);
00112 }
00113 
00114 void SSD1353_SPI::masterCurrent(int current)
00115 {
00116     if(current >= 0 && current <= 15)
00117     {
00118         writeCommand(CMD_MASTERCURRENT);
00119         writeData(current);
00120     }
00121 }
00122 
00123 void SSD1353_SPI::drawPixel(int x, int y, unsigned int c)
00124 {
00125     //Range check the pixel
00126     if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
00127         return;
00128  
00129     //Only goto if the cursor is out of position
00130     if (x != m_CursX || y != m_CursY) {
00131         //First flush the cache
00132         flush();
00133  
00134         //Set column start and end address (also moves pointer)
00135         writeCommand(CMD_SETCOLUMN);
00136         writeData(x);
00137         writeData(width() - 1);
00138  
00139         //Set row start and end address (also moves pointer)
00140         writeCommand(CMD_SETROW);
00141         writeData(y);
00142         writeData(height() - 1);
00143  
00144         writeCommand(CMD_WRITERAM);
00145  
00146         //Update the cursor variables
00147         m_StartX = x;
00148         m_StartY = y;
00149         m_CursX = x;
00150         m_CursY = y;
00151     }
00152  
00153     //Check if the cache is full
00154     if (m_CacheIndex > 1024 - 4) {
00155         //Flush the cache
00156         flush();
00157     }
00158  
00159     //Add this pixel to the cache
00160     m_Cache[m_CacheIndex++] = c >> 18;
00161     m_Cache[m_CacheIndex++] = c >> 10;
00162     m_Cache[m_CacheIndex++] = c >> 2;
00163  
00164     //Increment the cursor with wrapping
00165     if (++m_CursX > width() - 1) {
00166         m_CursX = m_StartX;
00167  
00168         if (++m_CursY > height() - 1) {
00169             m_CursY = m_StartY;
00170         }
00171     }
00172 }
00173  
00174 void SSD1353_SPI::writeCommand(char command)
00175 {
00176     //Set DC to command and select the display
00177     m_DC = 0;
00178     m_CS = 0;
00179  
00180     //Write the command byte
00181     m_SPI.write(command);
00182  
00183     //Deselect the display
00184     m_CS = 1;
00185 }
00186  
00187 void SSD1353_SPI::writeData(char data)
00188 {
00189     //Set DC to data and select the display
00190     m_DC = 1;
00191     m_CS = 0;
00192  
00193     //Write the data byte
00194     m_SPI.write(data);
00195  
00196     //Deselect the display
00197     m_CS = 1;
00198 }