SPKDisplay - A mbed display class and processing imaging tools for 128x64 OLEDs using the SSD1305 driver, connected via SPI. This variant drives a Unigraphics oled on the same driver but in page mode. Includes a simple 5x7 ascii font
Fork of spk_oled_ssd1305 by
OLED driver and demo for Univision oled display 128x64.
Modified Toby Harris' code to run page mode addressing in 4 wire SPI on the Univision UG-2864HSWEG01 128X64 unit - often found for sale on ebay. That's where I got mine anyhow.
Wiring this was fairly straightforward but you really do have to ground all the unused pins EXCEPT for number 8 to get it to work.
Here's the layout - I made a little carrier using a 2x8 connector and a couple of headers mounted on a small piece of stripboard. You can see all the wire links that are used to ground the unused pins. It was pretty awkward setting the display to run in 4 wire spi - had to desolder and resolder three smd o ohm links on the back o fhte pcb - ended up using a microscope to see them.
Here is the output of the demo code.
In page mode the display is effectively divided up into 8 horizontal strips, each 8 pixels high so lends itself nicley to displaying text in a 5x7 font. I've included the 5x7 lcd font in the code below.
Code
Import programunivision_oled
driver demo for univision oled dispaly
#include "mbed.h" #include "4spi_oled_ssd1305.h" /* re writtten to cope with teh ssd1306 driven oled UG-2864HSWEG01 from univision wiring for test circuit OLED pins -FUNCTION-------MBED PINns 1 ----3.3V--- Vout (p40) 2 ----0.0V--- GND (p1) 3 ----------- GND (p1) 4 ----------- GND (p1) 5 ----------- GND (p1) 6 ----------- GND (p1) 7 ----------- GND (p1) 8 ----------- N/C 9 ----D1---- mosi (p5) 10 ---D0------sck(p7) 11 ----------- GND (p1) 12 ----------- GND (p1) 13 ---DC------ p10 14 ---RES----- p9 15 ---CS------ p8 */ DigitalOut myled(LED1); // Create object and load font //SPKDisplay( mosiPin, clkPin, csPin, dcPin, resPin, Serial *debugSerial) SPKDisplay screen(p5, p7, p8, p10, p9); int main() { char msg[22]; sprintf(msg," "); // 01234567890abcdefghij int i=0; while(1==1) { myled=1; // nice logo output screen.welcome(); wait(3); wait(2); for(i=0; i<5; i++) { myled=0; screen.inverse(); wait(1); screen.normal(); myled=1; wait(1); } screen.fontdemo(); myled=0; wait(5); screen.clearBuffer(); screen.sendBuffer(); myled=1; wait(0.5); myled=0; wait(0.5); for(i=0; i<8; i++) { sprintf(msg,"HELLO WORLD %d",i); screen.textToBuffer(msg, i); screen.sendBuffer(); screen.clearBuffer(); myled=1; wait(1); } } }
spk_oled_ssd1305.h@4:a675a19c16f0, 2012-10-28 (annotated)
- Committer:
- tobyspark
- Date:
- Sun Oct 28 19:53:49 2012 +0000
- Revision:
- 4:a675a19c16f0
- Parent:
- 3:ade83210ecf6
void characterToBuffer(char character, int x, int row)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tobyspark | 0:76bb084fa033 | 1 | // OLED display using SSD1305 driver |
tobyspark | 3:ade83210ecf6 | 2 | // A library by *spark audio-visual |
tobyspark | 3:ade83210ecf6 | 3 | |
tobyspark | 3:ade83210ecf6 | 4 | /* Copyright (c) 2011 Toby Harris, MIT License |
tobyspark | 3:ade83210ecf6 | 5 | * |
tobyspark | 3:ade83210ecf6 | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
tobyspark | 3:ade83210ecf6 | 7 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
tobyspark | 3:ade83210ecf6 | 8 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
tobyspark | 3:ade83210ecf6 | 9 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
tobyspark | 3:ade83210ecf6 | 10 | * furnished to do so, subject to the following conditions: |
tobyspark | 3:ade83210ecf6 | 11 | * |
tobyspark | 3:ade83210ecf6 | 12 | * The above copyright notice and this permission notice shall be included in all copies or |
tobyspark | 3:ade83210ecf6 | 13 | * substantial portions of the Software. |
tobyspark | 3:ade83210ecf6 | 14 | * |
tobyspark | 3:ade83210ecf6 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
tobyspark | 3:ade83210ecf6 | 16 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
tobyspark | 3:ade83210ecf6 | 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
tobyspark | 3:ade83210ecf6 | 18 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
tobyspark | 3:ade83210ecf6 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
tobyspark | 3:ade83210ecf6 | 20 | */ |
tobyspark | 0:76bb084fa033 | 21 | |
tobyspark | 0:76bb084fa033 | 22 | #ifndef SPK_OLED_SSD1305_h |
tobyspark | 0:76bb084fa033 | 23 | #define SPK_OLED_SSD1305_h |
tobyspark | 0:76bb084fa033 | 24 | |
tobyspark | 0:76bb084fa033 | 25 | #include "mbed.h" |
tobyspark | 0:76bb084fa033 | 26 | #include <string> |
tobyspark | 0:76bb084fa033 | 27 | |
tobyspark | 0:76bb084fa033 | 28 | #define bufferCount 1056 |
tobyspark | 0:76bb084fa033 | 29 | #define bufferWidth 132 |
tobyspark | 0:76bb084fa033 | 30 | #define pixelWidth 128 |
tobyspark | 0:76bb084fa033 | 31 | #define pixelHeight 64 |
tobyspark | 0:76bb084fa033 | 32 | #define pixInPage 8 |
tobyspark | 0:76bb084fa033 | 33 | #define pageCount 8 |
tobyspark | 0:76bb084fa033 | 34 | |
tobyspark | 2:8187d69071f8 | 35 | /** Display class for 128x64 OLEDs using the SSD1305 driver, connected via SPI |
tobyspark | 2:8187d69071f8 | 36 | * |
tobyspark | 1:dd3faa2ab1dd | 37 | * Display ie. DENSITRON - DD-12864YO-3A |
tobyspark | 0:76bb084fa033 | 38 | * |
tobyspark | 1:dd3faa2ab1dd | 39 | * This is a ground-up, minimal library. Further functionality as and when its needed or anybody wants to contribute. |
tobyspark | 0:76bb084fa033 | 40 | * |
tobyspark | 0:76bb084fa033 | 41 | * This library includes two processing sketches to create a font and full-screen image in the required byte representations. |
tobyspark | 2:8187d69071f8 | 42 | * Without creating your font and any images, all this library will do is blank the screen and draw horizontal lines. But at least you'll know its working! |
tobyspark | 0:76bb084fa033 | 43 | * |
tobyspark | 0:76bb084fa033 | 44 | * Terminology: |
tobyspark | 0:76bb084fa033 | 45 | * 'rows' are 8 pixel high rows across the display, 0 being the topmost and 7 the bottom. |
tobyspark | 2:8187d69071f8 | 46 | * 'lines' are single pixel lines, origin top left. |
tobyspark | 0:76bb084fa033 | 47 | * |
tobyspark | 0:76bb084fa033 | 48 | * Example: |
tobyspark | 0:76bb084fa033 | 49 | * @code |
tobyspark | 1:dd3faa2ab1dd | 50 | * // Create object and load font |
tobyspark | 0:76bb084fa033 | 51 | * SPKDisplay screen(p5, p7, p8, p10, p9) |
tobyspark | 1:dd3faa2ab1dd | 52 | * screen.fontStartCharacter = &myStartChar; |
tobyspark | 1:dd3faa2ab1dd | 53 | * screen.fontEndCharacter = &myEndChar; |
tobyspark | 1:dd3faa2ab1dd | 54 | * screen.fontCharacters = myFontArray; |
tobyspark | 1:dd3faa2ab1dd | 55 | * // Draw |
tobyspark | 1:dd3faa2ab1dd | 56 | * screen.imageToBuffer(myImageByteArray); |
tobyspark | 0:76bb084fa033 | 57 | * screen.textToBuffer("*spark OLED SSD1305",0); |
tobyspark | 0:76bb084fa033 | 58 | * screen.textToBuffer("v01",1); |
tobyspark | 0:76bb084fa033 | 59 | * screen.sendBuffer |
tobyspark | 0:76bb084fa033 | 60 | * @endcode |
tobyspark | 0:76bb084fa033 | 61 | */ |
tobyspark | 0:76bb084fa033 | 62 | class SPKDisplay |
tobyspark | 0:76bb084fa033 | 63 | { |
tobyspark | 0:76bb084fa033 | 64 | public: |
tobyspark | 0:76bb084fa033 | 65 | /** Create a display object connected via SPI |
tobyspark | 0:76bb084fa033 | 66 | * |
tobyspark | 0:76bb084fa033 | 67 | * @param mosi SPI MOSI |
tobyspark | 0:76bb084fa033 | 68 | * @param clk SPI SCK |
tobyspark | 0:76bb084fa033 | 69 | * @param cs Chip Select - a digital out pin |
tobyspark | 0:76bb084fa033 | 70 | * @param dc Data/Command - a digital out pin |
tobyspark | 0:76bb084fa033 | 71 | * @param res Reset - a digital out pin |
tobyspark | 0:76bb084fa033 | 72 | * @param debugSerial An optional serial object to log to |
tobyspark | 0:76bb084fa033 | 73 | */ |
tobyspark | 0:76bb084fa033 | 74 | SPKDisplay(PinName mosi, PinName clk, PinName cs, PinName dc, PinName res, Serial *debugSerial = NULL); |
tobyspark | 0:76bb084fa033 | 75 | |
tobyspark | 1:dd3faa2ab1dd | 76 | /** Font - Assign the ASCII value of the character at the start of the implemented range */ |
tobyspark | 1:dd3faa2ab1dd | 77 | const int *fontStartCharacter; |
tobyspark | 1:dd3faa2ab1dd | 78 | |
tobyspark | 1:dd3faa2ab1dd | 79 | /** Font - Assign the ASCII value of the character at the end of the implemented range */ |
tobyspark | 1:dd3faa2ab1dd | 80 | const int *fontEndCharacter; |
tobyspark | 1:dd3faa2ab1dd | 81 | |
tobyspark | 1:dd3faa2ab1dd | 82 | /** Font - Assign the font, an array of 8x8px characters |
tobyspark | 1:dd3faa2ab1dd | 83 | * |
tobyspark | 1:dd3faa2ab1dd | 84 | * @note The processing sketch spk_oled_fontByteMaker--processing takes characterCount*8px x 8px images and creates the code to declare the font array needed by this method |
tobyspark | 1:dd3faa2ab1dd | 85 | */ |
tobyspark | 1:dd3faa2ab1dd | 86 | uint8_t const **fontCharacters; |
tobyspark | 1:dd3faa2ab1dd | 87 | |
tobyspark | 0:76bb084fa033 | 88 | /** Completely clear the object's display representation */ |
tobyspark | 0:76bb084fa033 | 89 | void clearBuffer(); |
tobyspark | 0:76bb084fa033 | 90 | |
tobyspark | 0:76bb084fa033 | 91 | /** Clear a row of the object's display representation |
tobyspark | 0:76bb084fa033 | 92 | * |
tobyspark | 0:76bb084fa033 | 93 | * @param row The row to clear. |
tobyspark | 0:76bb084fa033 | 94 | */ |
tobyspark | 0:76bb084fa033 | 95 | void clearBufferRow(int row); |
tobyspark | 0:76bb084fa033 | 96 | |
tobyspark | 1:dd3faa2ab1dd | 97 | /** Replace the object\s display representation with the contents of image |
tobyspark | 1:dd3faa2ab1dd | 98 | * |
tobyspark | 1:dd3faa2ab1dd | 99 | * @param image An array of 1056 bytes representing an image. |
tobyspark | 1:dd3faa2ab1dd | 100 | * @note The processing sketch spk_oled_screenByteMaker--processing takes 132x64 images and creates the code to declare such arrays |
tobyspark | 1:dd3faa2ab1dd | 101 | */ |
tobyspark | 1:dd3faa2ab1dd | 102 | void imageToBuffer(const uint8_t* image); |
tobyspark | 0:76bb084fa033 | 103 | |
tobyspark | 0:76bb084fa033 | 104 | /** Draw a horizontal line in the object's display representation |
tobyspark | 0:76bb084fa033 | 105 | * |
tobyspark | 0:76bb084fa033 | 106 | * @param y The y position of the line to draw |
tobyspark | 0:76bb084fa033 | 107 | */ |
tobyspark | 0:76bb084fa033 | 108 | void horizLineToBuffer(int y); |
tobyspark | 0:76bb084fa033 | 109 | |
tobyspark | 0:76bb084fa033 | 110 | /** Write a line of text in the object's display representation |
tobyspark | 1:dd3faa2ab1dd | 111 | * Requires the font to have been set |
tobyspark | 0:76bb084fa033 | 112 | * |
tobyspark | 0:76bb084fa033 | 113 | * @param message The text to write. The text will be truncated if longer than the screen's width. |
tobyspark | 0:76bb084fa033 | 114 | * @param row The row in which to write the text |
tobyspark | 0:76bb084fa033 | 115 | */ |
tobyspark | 0:76bb084fa033 | 116 | void textToBuffer(std::string message, int row); |
tobyspark | 0:76bb084fa033 | 117 | |
tobyspark | 4:a675a19c16f0 | 118 | /** Write a single character in the object's display representation |
tobyspark | 4:a675a19c16f0 | 119 | * Requires the font to have been set |
tobyspark | 4:a675a19c16f0 | 120 | * |
tobyspark | 4:a675a19c16f0 | 121 | * @param character The character to write. |
tobyspark | 4:a675a19c16f0 | 122 | * @param x The x position to draw the character |
tobyspark | 4:a675a19c16f0 | 123 | * @param row The row in which to write the character |
tobyspark | 4:a675a19c16f0 | 124 | */ |
tobyspark | 4:a675a19c16f0 | 125 | void characterToBuffer(char character, int x, int row); |
tobyspark | 4:a675a19c16f0 | 126 | |
tobyspark | 0:76bb084fa033 | 127 | /** Send the object's display representation to the OLED |
tobyspark | 0:76bb084fa033 | 128 | * |
tobyspark | 0:76bb084fa033 | 129 | * You can safely call this once per main loop, it will only transmit the buffer contents if there has been an update |
tobyspark | 0:76bb084fa033 | 130 | */ |
tobyspark | 0:76bb084fa033 | 131 | void sendBuffer(); |
tobyspark | 0:76bb084fa033 | 132 | |
tobyspark | 0:76bb084fa033 | 133 | private: |
tobyspark | 0:76bb084fa033 | 134 | SPI *spi; |
tobyspark | 0:76bb084fa033 | 135 | DigitalOut *cs; |
tobyspark | 0:76bb084fa033 | 136 | DigitalOut *dc; |
tobyspark | 0:76bb084fa033 | 137 | DigitalOut *res; |
tobyspark | 0:76bb084fa033 | 138 | |
tobyspark | 0:76bb084fa033 | 139 | Serial *debug; |
tobyspark | 0:76bb084fa033 | 140 | uint8_t buffer[bufferCount]; |
tobyspark | 0:76bb084fa033 | 141 | |
tobyspark | 0:76bb084fa033 | 142 | bool bufferHasChanged; |
tobyspark | 0:76bb084fa033 | 143 | |
tobyspark | 0:76bb084fa033 | 144 | void setup(); |
tobyspark | 0:76bb084fa033 | 145 | }; |
tobyspark | 0:76bb084fa033 | 146 | |
tobyspark | 0:76bb084fa033 | 147 | #endif |