SPKDisplay - A mbed display class and processing imaging tools for 128x64 OLEDs using the SSD1305 driver, connected via SPI.

Dependents:   SPK-DVIMXR SPK-DMXer

Committer:
tobyspark
Date:
Tue Dec 10 15:48:54 2013 +0000
Revision:
5:0d518115e76c
Parent:
4:a675a19c16f0
fontByteMaker fix

Who changed what in which revision?

UserRevisionLine numberNew 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