This Program is for MAPLE board with OLED and GPS/RTC module. OLED module(OB) : 128 x 128 pixels, 4K color GPS/RTC module(GB) : UP501 These module can buy Marutyu (http://www.marutsu.co.jp)
MARMEX_OB_oled.h@0:58e40a872950, 2011-06-05 (annotated)
- Committer:
- y_notsu
- Date:
- Sun Jun 05 15:10:48 2011 +0000
- Revision:
- 0:58e40a872950
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
y_notsu | 0:58e40a872950 | 1 | /** MARMEX_OB OLED screen drawing library |
y_notsu | 0:58e40a872950 | 2 | * |
y_notsu | 0:58e40a872950 | 3 | * @class MARMEX_OB_oled |
y_notsu | 0:58e40a872950 | 4 | * @author tedd |
y_notsu | 0:58e40a872950 | 5 | * @version 0.51 |
y_notsu | 0:58e40a872950 | 6 | * @date 08-Apr-2011 |
y_notsu | 0:58e40a872950 | 7 | * |
y_notsu | 0:58e40a872950 | 8 | * Released under the MIT License: http://mbed.org/license/mit |
y_notsu | 0:58e40a872950 | 9 | * |
y_notsu | 0:58e40a872950 | 10 | * MARMEX_OB_oled OLED screen drawing library for mbed |
y_notsu | 0:58e40a872950 | 11 | * This code has been written based on sample code and advises |
y_notsu | 0:58e40a872950 | 12 | * from Ochiai-san (Marutsu-Elec). Thank you! |
y_notsu | 0:58e40a872950 | 13 | * |
y_notsu | 0:58e40a872950 | 14 | * SPI mode: |
y_notsu | 0:58e40a872950 | 15 | * 9bit or 8bit SPI mode can be selected by disabling/enabling "#define MARMEX_OB_SPI_8BIT_MODE". |
y_notsu | 0:58e40a872950 | 16 | * See source code in this (MARMEX_OB_oled.h) file. |
y_notsu | 0:58e40a872950 | 17 | */ |
y_notsu | 0:58e40a872950 | 18 | |
y_notsu | 0:58e40a872950 | 19 | #ifndef MBED_MARMEX_OB_OLED |
y_notsu | 0:58e40a872950 | 20 | #define MBED_MARMEX_OB_OLED |
y_notsu | 0:58e40a872950 | 21 | |
y_notsu | 0:58e40a872950 | 22 | #include "mbed.h" |
y_notsu | 0:58e40a872950 | 23 | #include "NokiaLCD.h" |
y_notsu | 0:58e40a872950 | 24 | |
y_notsu | 0:58e40a872950 | 25 | /** @def MARMEX_OB_SPI_8BIT_MODE |
y_notsu | 0:58e40a872950 | 26 | * |
y_notsu | 0:58e40a872950 | 27 | * MARMEX_OB_oled_oled OLED screen SPI access length setting |
y_notsu | 0:58e40a872950 | 28 | * Enabling "MARMEX_OB_SPI_8BIT_MODE" makes 9bit SPI access by 8bit * 2 times. |
y_notsu | 0:58e40a872950 | 29 | * This may be useful if other 8bit access SPI device on same SPI bus. |
y_notsu | 0:58e40a872950 | 30 | * |
y_notsu | 0:58e40a872950 | 31 | * If disabled (just coment out the "#define MARMEX_OB_SPI_8BIT_MODE"), SPI access willbe done by 9 bit format. |
y_notsu | 0:58e40a872950 | 32 | */ |
y_notsu | 0:58e40a872950 | 33 | #define MARMEX_OB_SPI_8BIT_MODE |
y_notsu | 0:58e40a872950 | 34 | |
y_notsu | 0:58e40a872950 | 35 | /** MARMEX_OB_oled OLED screen drawing class |
y_notsu | 0:58e40a872950 | 36 | * |
y_notsu | 0:58e40a872950 | 37 | * This is a driver code for MARMEX_OB_oled board OLED screen. |
y_notsu | 0:58e40a872950 | 38 | * This class inherits NokiaLCD class of mbed.org. |
y_notsu | 0:58e40a872950 | 39 | * To use this class, import the NokiaLCD class from here.. |
y_notsu | 0:58e40a872950 | 40 | * http://mbed.org/users/simon/libraries/NokiaLCD/ |
y_notsu | 0:58e40a872950 | 41 | * |
y_notsu | 0:58e40a872950 | 42 | * Example: |
y_notsu | 0:58e40a872950 | 43 | * @code |
y_notsu | 0:58e40a872950 | 44 | * #include "mbed.h" |
y_notsu | 0:58e40a872950 | 45 | * #include "MARMEX_OB_oled.h" |
y_notsu | 0:58e40a872950 | 46 | * |
y_notsu | 0:58e40a872950 | 47 | * // oled1 is for MARMEX_OB_oled board on MAPLE slot 1 |
y_notsu | 0:58e40a872950 | 48 | * // oled1 is for MARMEX_OB_oled board on MAPLE slot 2 |
y_notsu | 0:58e40a872950 | 49 | * |
y_notsu | 0:58e40a872950 | 50 | * MARMEX_OB_oled oled1( p5, p7, p8, p30, p11 ); // mosi, sclk, cs, rst, power_control |
y_notsu | 0:58e40a872950 | 51 | * //MARMEX_OB_oled oled2( p5, p7, p26, p21, p17 ); // mosi, sclk, cs, rst, power_control |
y_notsu | 0:58e40a872950 | 52 | * |
y_notsu | 0:58e40a872950 | 53 | * |
y_notsu | 0:58e40a872950 | 54 | * int main() { |
y_notsu | 0:58e40a872950 | 55 | * oled1.background( 0x000000 ); |
y_notsu | 0:58e40a872950 | 56 | * oled1.cls(); |
y_notsu | 0:58e40a872950 | 57 | * |
y_notsu | 0:58e40a872950 | 58 | * int colorbar_width = MARMEX_OB_oled::WIDTH / 8; |
y_notsu | 0:58e40a872950 | 59 | * |
y_notsu | 0:58e40a872950 | 60 | * for ( int i = 0; i < 8; i++ ) |
y_notsu | 0:58e40a872950 | 61 | * oled1.fill( colorbar_width * i, 0, colorbar_width, MARMEX_OB_oled::HEIGHT, ((i & 0x4) ? 0xFF0000 : 0x000000) | ((i & 0x2) ? 0x00FF00 : 0x000000) | ((i & 0x1) ? 0x0000FF : 0x000000) ); |
y_notsu | 0:58e40a872950 | 62 | * |
y_notsu | 0:58e40a872950 | 63 | * oled1.fill( 50, 50, 64, 64, 0xCCCCCC );; |
y_notsu | 0:58e40a872950 | 64 | * |
y_notsu | 0:58e40a872950 | 65 | * oled1.locate( 0, 3 ); |
y_notsu | 0:58e40a872950 | 66 | * oled1.printf( "Hello World!" ); |
y_notsu | 0:58e40a872950 | 67 | * oled1.locate( 0, 4 ); |
y_notsu | 0:58e40a872950 | 68 | * oled1.printf( "SPI = %s", MERMEX_OB_SPI_MODE_STR ); |
y_notsu | 0:58e40a872950 | 69 | * |
y_notsu | 0:58e40a872950 | 70 | * for (int i = 0; i < MARMEX_OB_oled::WIDTH; i++ ) { |
y_notsu | 0:58e40a872950 | 71 | * oled1.pixel( i, 80 + sin( (float)i / 5.0 ) * 10, 0x000000 ); |
y_notsu | 0:58e40a872950 | 72 | * } |
y_notsu | 0:58e40a872950 | 73 | * } * @endcode |
y_notsu | 0:58e40a872950 | 74 | */ |
y_notsu | 0:58e40a872950 | 75 | |
y_notsu | 0:58e40a872950 | 76 | class MARMEX_OB_oled : public NokiaLCD { |
y_notsu | 0:58e40a872950 | 77 | |
y_notsu | 0:58e40a872950 | 78 | public: |
y_notsu | 0:58e40a872950 | 79 | |
y_notsu | 0:58e40a872950 | 80 | /** General parameters for MARMEX_OB_oled */ |
y_notsu | 0:58e40a872950 | 81 | enum { |
y_notsu | 0:58e40a872950 | 82 | ROWS = 15, /**< # of rows (lines) for displaying characters */ |
y_notsu | 0:58e40a872950 | 83 | COLS = 16, /**< # of columns (width) for displaying characters */ |
y_notsu | 0:58e40a872950 | 84 | WIDTH = 128, /**< screen width [pixels] */ |
y_notsu | 0:58e40a872950 | 85 | HEIGHT = 128, /**< screen height [pixels] */ |
y_notsu | 0:58e40a872950 | 86 | SPI_FREQUENCY = 20000000 /**< SPI (sclk) SPI_FREQUENCY */ |
y_notsu | 0:58e40a872950 | 87 | }; |
y_notsu | 0:58e40a872950 | 88 | |
y_notsu | 0:58e40a872950 | 89 | /** Constants for power() function */ |
y_notsu | 0:58e40a872950 | 90 | enum { |
y_notsu | 0:58e40a872950 | 91 | OFF = 0, /**< : to turning-OFF */ |
y_notsu | 0:58e40a872950 | 92 | ON /**< : to turning-ON */ |
y_notsu | 0:58e40a872950 | 93 | }; |
y_notsu | 0:58e40a872950 | 94 | |
y_notsu | 0:58e40a872950 | 95 | /** Create a MARMEX_OB_oled object connected to specified SPI and DigitalOut pins |
y_notsu | 0:58e40a872950 | 96 | * |
y_notsu | 0:58e40a872950 | 97 | * @param mosi SPI-MOSI pin (for MAPLE board, use p5) |
y_notsu | 0:58e40a872950 | 98 | * @param sclk SPI-SCLK pin (for MAPLE board, use p8) |
y_notsu | 0:58e40a872950 | 99 | * @param cs chip select signal (for MAPLE board, use p8(slot1), p26(slot2)) |
y_notsu | 0:58e40a872950 | 100 | * @param rst reset signal (for MAPLE board, use p30(slot1), p21(slot2)) |
y_notsu | 0:58e40a872950 | 101 | * @param power_pin backlight power control signal (for MAPLE board, use p11(slot1), p17(slot2)) |
y_notsu | 0:58e40a872950 | 102 | * |
y_notsu | 0:58e40a872950 | 103 | * Example of MARMEX_OB_oled on MAPLE board: |
y_notsu | 0:58e40a872950 | 104 | * @code |
y_notsu | 0:58e40a872950 | 105 | * #include "mbed.h" |
y_notsu | 0:58e40a872950 | 106 | * #include "MARMEX_OB_oled.h" |
y_notsu | 0:58e40a872950 | 107 | * |
y_notsu | 0:58e40a872950 | 108 | * MARMEX_OB_oled oled_on_maple_slot1( p5, p7, p8, p30, p11 ); // mosi, sclk, cs, rst, power_control |
y_notsu | 0:58e40a872950 | 109 | * MARMEX_OB_oled oled_on_maple_slot2( p5, p7, p26, p21, p17 ); // mosi, sclk, cs, rst, power_control |
y_notsu | 0:58e40a872950 | 110 | * ... |
y_notsu | 0:58e40a872950 | 111 | * .. |
y_notsu | 0:58e40a872950 | 112 | * @endcode |
y_notsu | 0:58e40a872950 | 113 | */ |
y_notsu | 0:58e40a872950 | 114 | |
y_notsu | 0:58e40a872950 | 115 | MARMEX_OB_oled( PinName mosi, PinName sclk, PinName cs, PinName rst, PinName power_pin ) : NokiaLCD( mosi, sclk, cs, rst, NokiaLCD::LCD6100 ), _power_pin( power_pin ) { |
y_notsu | 0:58e40a872950 | 116 | power( ON ); |
y_notsu | 0:58e40a872950 | 117 | reset(); |
y_notsu | 0:58e40a872950 | 118 | } |
y_notsu | 0:58e40a872950 | 119 | |
y_notsu | 0:58e40a872950 | 120 | #if DOXYGEN_ONLY |
y_notsu | 0:58e40a872950 | 121 | /** Write a character to the LCD |
y_notsu | 0:58e40a872950 | 122 | * |
y_notsu | 0:58e40a872950 | 123 | * @param c The character to write to the display |
y_notsu | 0:58e40a872950 | 124 | */ |
y_notsu | 0:58e40a872950 | 125 | int putc( int c ); |
y_notsu | 0:58e40a872950 | 126 | |
y_notsu | 0:58e40a872950 | 127 | /** Write a formated string to the LCD |
y_notsu | 0:58e40a872950 | 128 | * |
y_notsu | 0:58e40a872950 | 129 | * @param format A printf-style format string, followed by the |
y_notsu | 0:58e40a872950 | 130 | * variables to use in formating the string. |
y_notsu | 0:58e40a872950 | 131 | * |
y_notsu | 0:58e40a872950 | 132 | * !!! 16th character in the string will be disappeared |
y_notsu | 0:58e40a872950 | 133 | * !!! This problem is due to difference of screen size NokiaLCD library and its internal mechanism... |
y_notsu | 0:58e40a872950 | 134 | */ |
y_notsu | 0:58e40a872950 | 135 | int printf( const char* format, ... ); |
y_notsu | 0:58e40a872950 | 136 | |
y_notsu | 0:58e40a872950 | 137 | /** Set the foreground colour |
y_notsu | 0:58e40a872950 | 138 | * |
y_notsu | 0:58e40a872950 | 139 | * @param c 24-bit colour |
y_notsu | 0:58e40a872950 | 140 | */ |
y_notsu | 0:58e40a872950 | 141 | void foreground(int c); |
y_notsu | 0:58e40a872950 | 142 | |
y_notsu | 0:58e40a872950 | 143 | /** Set the background colour |
y_notsu | 0:58e40a872950 | 144 | * |
y_notsu | 0:58e40a872950 | 145 | * @param c 24-bit colour |
y_notsu | 0:58e40a872950 | 146 | */ |
y_notsu | 0:58e40a872950 | 147 | void background(int c); |
y_notsu | 0:58e40a872950 | 148 | |
y_notsu | 0:58e40a872950 | 149 | #endif |
y_notsu | 0:58e40a872950 | 150 | |
y_notsu | 0:58e40a872950 | 151 | /** reset MARMEX_OB_oled |
y_notsu | 0:58e40a872950 | 152 | * |
y_notsu | 0:58e40a872950 | 153 | * Executes hardware reset and initialize. |
y_notsu | 0:58e40a872950 | 154 | * See MARMEX_OB_oled manual for the initialization sequence and values |
y_notsu | 0:58e40a872950 | 155 | * For gamma correction table, using math function to make the code simple |
y_notsu | 0:58e40a872950 | 156 | */ |
y_notsu | 0:58e40a872950 | 157 | |
y_notsu | 0:58e40a872950 | 158 | void reset( void ) { |
y_notsu | 0:58e40a872950 | 159 | |
y_notsu | 0:58e40a872950 | 160 | #define GAMMA_LUT_SIZE 63 |
y_notsu | 0:58e40a872950 | 161 | unsigned char gamma_LUT[ GAMMA_LUT_SIZE ]; |
y_notsu | 0:58e40a872950 | 162 | |
y_notsu | 0:58e40a872950 | 163 | for ( int i = 0; i < GAMMA_LUT_SIZE; i++ ) |
y_notsu | 0:58e40a872950 | 164 | gamma_LUT[ i ] = (unsigned char)(powf( ((float)i / 62.0), (1.0 / 0.58) ) * 178.0 + 2.0); |
y_notsu | 0:58e40a872950 | 165 | |
y_notsu | 0:58e40a872950 | 166 | // setup the SPI interface and bring display out of reset |
y_notsu | 0:58e40a872950 | 167 | _cs = 1; |
y_notsu | 0:58e40a872950 | 168 | _rst = 0; |
y_notsu | 0:58e40a872950 | 169 | #ifdef MARMEX_OB_SPI_8BIT_MODE |
y_notsu | 0:58e40a872950 | 170 | _spi.format( 8 ); |
y_notsu | 0:58e40a872950 | 171 | #else |
y_notsu | 0:58e40a872950 | 172 | _spi.format( 9 ); |
y_notsu | 0:58e40a872950 | 173 | #endif |
y_notsu | 0:58e40a872950 | 174 | |
y_notsu | 0:58e40a872950 | 175 | _spi.frequency( SPI_FREQUENCY ); |
y_notsu | 0:58e40a872950 | 176 | wait_ms( 1 ); |
y_notsu | 0:58e40a872950 | 177 | _rst = 1; |
y_notsu | 0:58e40a872950 | 178 | wait_ms( 1 ); |
y_notsu | 0:58e40a872950 | 179 | |
y_notsu | 0:58e40a872950 | 180 | _cs = 0; |
y_notsu | 0:58e40a872950 | 181 | |
y_notsu | 0:58e40a872950 | 182 | command( SET_DISPLAY_MODE_ALL_OFF ); |
y_notsu | 0:58e40a872950 | 183 | command( SET_COMMAND_LOCK ); |
y_notsu | 0:58e40a872950 | 184 | data( 0x12 ); |
y_notsu | 0:58e40a872950 | 185 | |
y_notsu | 0:58e40a872950 | 186 | command( SET_COMMAND_LOCK ); |
y_notsu | 0:58e40a872950 | 187 | data( 0xb1 ); |
y_notsu | 0:58e40a872950 | 188 | |
y_notsu | 0:58e40a872950 | 189 | command( SET_SLEEP_MODE_ON ); |
y_notsu | 0:58e40a872950 | 190 | |
y_notsu | 0:58e40a872950 | 191 | command( FRONT_CLOCK_DRIVER_OSC_FREQ ); |
y_notsu | 0:58e40a872950 | 192 | data( 0xF1 ); |
y_notsu | 0:58e40a872950 | 193 | |
y_notsu | 0:58e40a872950 | 194 | command( SET_MUX_RATIO ); |
y_notsu | 0:58e40a872950 | 195 | data( 0x7F ); |
y_notsu | 0:58e40a872950 | 196 | |
y_notsu | 0:58e40a872950 | 197 | command( SET_DISPAY_OFFSET ); |
y_notsu | 0:58e40a872950 | 198 | data( 0x00 ); |
y_notsu | 0:58e40a872950 | 199 | |
y_notsu | 0:58e40a872950 | 200 | command( SET_DISPAY_START_LINE ); |
y_notsu | 0:58e40a872950 | 201 | data( 0x00 ); |
y_notsu | 0:58e40a872950 | 202 | |
y_notsu | 0:58e40a872950 | 203 | command( SET_REMAP_COLOR_DEPTH ); |
y_notsu | 0:58e40a872950 | 204 | data( 0x74 ); |
y_notsu | 0:58e40a872950 | 205 | |
y_notsu | 0:58e40a872950 | 206 | command( SET_GPIO ); |
y_notsu | 0:58e40a872950 | 207 | data( 0x00); |
y_notsu | 0:58e40a872950 | 208 | |
y_notsu | 0:58e40a872950 | 209 | command( FUNCTION_SELECTION ); |
y_notsu | 0:58e40a872950 | 210 | data( 0x01 ); |
y_notsu | 0:58e40a872950 | 211 | |
y_notsu | 0:58e40a872950 | 212 | command( SET_SEGMENT_LOW_VOLTAGE ); |
y_notsu | 0:58e40a872950 | 213 | data( 0xA0 ); |
y_notsu | 0:58e40a872950 | 214 | data( 0xB5 ); |
y_notsu | 0:58e40a872950 | 215 | data( 0x55 ); |
y_notsu | 0:58e40a872950 | 216 | |
y_notsu | 0:58e40a872950 | 217 | command( SET_CONTRAST_CURRENT_FOR_COLOR_ABC ); |
y_notsu | 0:58e40a872950 | 218 | data( 0xC8 ); |
y_notsu | 0:58e40a872950 | 219 | data( 0x80 ); |
y_notsu | 0:58e40a872950 | 220 | data( 0xC8 ); |
y_notsu | 0:58e40a872950 | 221 | |
y_notsu | 0:58e40a872950 | 222 | command( MASTER_CONTRAST_CURRENT_CONTROL ); |
y_notsu | 0:58e40a872950 | 223 | data( 0x0F ); |
y_notsu | 0:58e40a872950 | 224 | |
y_notsu | 0:58e40a872950 | 225 | command( LOOKUP_TABLE_FOR_GRAYSCALE_PULSE_WIDTH ); |
y_notsu | 0:58e40a872950 | 226 | for ( int i = 0; i < GAMMA_LUT_SIZE; i++ ) |
y_notsu | 0:58e40a872950 | 227 | data( gamma_LUT[ i ] ); |
y_notsu | 0:58e40a872950 | 228 | |
y_notsu | 0:58e40a872950 | 229 | command( SET_RESET_PRECHARGE_PERIOD ); |
y_notsu | 0:58e40a872950 | 230 | data( 0x32 ); |
y_notsu | 0:58e40a872950 | 231 | |
y_notsu | 0:58e40a872950 | 232 | command( ENHANCE_DRIVING_SCHEME_CAPABILITY ); |
y_notsu | 0:58e40a872950 | 233 | data( 0x04 ); |
y_notsu | 0:58e40a872950 | 234 | data( 0x00 ); |
y_notsu | 0:58e40a872950 | 235 | data( 0x00 ); |
y_notsu | 0:58e40a872950 | 236 | |
y_notsu | 0:58e40a872950 | 237 | command( SET_PRECHARGE_VOLTAGE ); |
y_notsu | 0:58e40a872950 | 238 | data( 0x17 ); |
y_notsu | 0:58e40a872950 | 239 | |
y_notsu | 0:58e40a872950 | 240 | command( SET_SECOND_PRECHARGE_VOLTAGE ); |
y_notsu | 0:58e40a872950 | 241 | data( 0x01 ); |
y_notsu | 0:58e40a872950 | 242 | |
y_notsu | 0:58e40a872950 | 243 | command( SET_VCOMH_VOLTAGE ); |
y_notsu | 0:58e40a872950 | 244 | data( 0x05 ); |
y_notsu | 0:58e40a872950 | 245 | |
y_notsu | 0:58e40a872950 | 246 | command( SET_DISPLAY_MODE_RESET ); |
y_notsu | 0:58e40a872950 | 247 | |
y_notsu | 0:58e40a872950 | 248 | #if 0 |
y_notsu | 0:58e40a872950 | 249 | command( SET_COLUMN_ADDRESS ); |
y_notsu | 0:58e40a872950 | 250 | data( 0x00 ); |
y_notsu | 0:58e40a872950 | 251 | data( 0x7F ); |
y_notsu | 0:58e40a872950 | 252 | |
y_notsu | 0:58e40a872950 | 253 | command( SET_ROW_ADDRESS ); |
y_notsu | 0:58e40a872950 | 254 | data( 0x00 ); |
y_notsu | 0:58e40a872950 | 255 | data( 0x7F); |
y_notsu | 0:58e40a872950 | 256 | |
y_notsu | 0:58e40a872950 | 257 | command( WRITE_RAM_COMMAND ); |
y_notsu | 0:58e40a872950 | 258 | for ( int i = 0; i < WIDTH * HEIGHT; i++ ) |
y_notsu | 0:58e40a872950 | 259 | data( 0x00 ); |
y_notsu | 0:58e40a872950 | 260 | #endif |
y_notsu | 0:58e40a872950 | 261 | _cs = 1; |
y_notsu | 0:58e40a872950 | 262 | |
y_notsu | 0:58e40a872950 | 263 | cls(); |
y_notsu | 0:58e40a872950 | 264 | wait_ms( 200 ); |
y_notsu | 0:58e40a872950 | 265 | |
y_notsu | 0:58e40a872950 | 266 | command( SET_SLEEP_MODE_OFF ); |
y_notsu | 0:58e40a872950 | 267 | } |
y_notsu | 0:58e40a872950 | 268 | |
y_notsu | 0:58e40a872950 | 269 | /** Clear the screen and locate to 0,0 */ |
y_notsu | 0:58e40a872950 | 270 | |
y_notsu | 0:58e40a872950 | 271 | void cls( void ) { |
y_notsu | 0:58e40a872950 | 272 | fill( 0, 0, WIDTH , HEIGHT, _background ); |
y_notsu | 0:58e40a872950 | 273 | _row = 0; |
y_notsu | 0:58e40a872950 | 274 | _column = 0; |
y_notsu | 0:58e40a872950 | 275 | } |
y_notsu | 0:58e40a872950 | 276 | |
y_notsu | 0:58e40a872950 | 277 | /** Set a pixel on te screen |
y_notsu | 0:58e40a872950 | 278 | * |
y_notsu | 0:58e40a872950 | 279 | * @param x horizontal position from left |
y_notsu | 0:58e40a872950 | 280 | * @param y vertical position from top |
y_notsu | 0:58e40a872950 | 281 | * @param colour 24-bit colour in format 0x00RRGGBB |
y_notsu | 0:58e40a872950 | 282 | */ |
y_notsu | 0:58e40a872950 | 283 | |
y_notsu | 0:58e40a872950 | 284 | virtual void pixel( int x, int y, int colour ) { |
y_notsu | 0:58e40a872950 | 285 | _cs = 0; |
y_notsu | 0:58e40a872950 | 286 | _window( x, y, 1, 1 ); |
y_notsu | 0:58e40a872950 | 287 | _putp( colour ); |
y_notsu | 0:58e40a872950 | 288 | _cs = 1; |
y_notsu | 0:58e40a872950 | 289 | } |
y_notsu | 0:58e40a872950 | 290 | |
y_notsu | 0:58e40a872950 | 291 | /** Fill an area of the screen |
y_notsu | 0:58e40a872950 | 292 | * |
y_notsu | 0:58e40a872950 | 293 | * @param x horizontal position from left |
y_notsu | 0:58e40a872950 | 294 | * @param y vertical position from top |
y_notsu | 0:58e40a872950 | 295 | * @param width width in pixels |
y_notsu | 0:58e40a872950 | 296 | * @param height height in pixels |
y_notsu | 0:58e40a872950 | 297 | * @param colour 24-bit colour in format 0x00RRGGBB |
y_notsu | 0:58e40a872950 | 298 | */ |
y_notsu | 0:58e40a872950 | 299 | |
y_notsu | 0:58e40a872950 | 300 | void fill( int x, int y, int width, int height, int colour ) { |
y_notsu | 0:58e40a872950 | 301 | _cs = 0; |
y_notsu | 0:58e40a872950 | 302 | _window( x, y, width, height ); |
y_notsu | 0:58e40a872950 | 303 | |
y_notsu | 0:58e40a872950 | 304 | for (int i = 0; i < width * height; i++ ) { |
y_notsu | 0:58e40a872950 | 305 | _putp( colour ); |
y_notsu | 0:58e40a872950 | 306 | } |
y_notsu | 0:58e40a872950 | 307 | |
y_notsu | 0:58e40a872950 | 308 | _window( 0, 0, WIDTH, HEIGHT ); |
y_notsu | 0:58e40a872950 | 309 | _cs = 1; |
y_notsu | 0:58e40a872950 | 310 | } |
y_notsu | 0:58e40a872950 | 311 | |
y_notsu | 0:58e40a872950 | 312 | void blit( int x, int y, int width, int height, const int* colour ) { |
y_notsu | 0:58e40a872950 | 313 | _cs = 0; |
y_notsu | 0:58e40a872950 | 314 | _window( x, y, width, height ); |
y_notsu | 0:58e40a872950 | 315 | |
y_notsu | 0:58e40a872950 | 316 | for (int i = 0; i < width * height; i++ ) { |
y_notsu | 0:58e40a872950 | 317 | _putp( colour[i] ); |
y_notsu | 0:58e40a872950 | 318 | } |
y_notsu | 0:58e40a872950 | 319 | _window( 0, 0, WIDTH, HEIGHT ); |
y_notsu | 0:58e40a872950 | 320 | _cs = 1; |
y_notsu | 0:58e40a872950 | 321 | } |
y_notsu | 0:58e40a872950 | 322 | |
y_notsu | 0:58e40a872950 | 323 | void bitblit( int x, int y, int width, int height, const char* bitstream ) { |
y_notsu | 0:58e40a872950 | 324 | _cs = 0; |
y_notsu | 0:58e40a872950 | 325 | _window( x, y, width, height ); |
y_notsu | 0:58e40a872950 | 326 | |
y_notsu | 0:58e40a872950 | 327 | for (int i = 0; i < height * width; i++ ) { |
y_notsu | 0:58e40a872950 | 328 | int byte = i / 8; |
y_notsu | 0:58e40a872950 | 329 | int bit = i % 8; |
y_notsu | 0:58e40a872950 | 330 | int colour = ((bitstream[ byte ] << bit) & 0x80) ? _foreground : _background; |
y_notsu | 0:58e40a872950 | 331 | _putp( colour ); |
y_notsu | 0:58e40a872950 | 332 | } |
y_notsu | 0:58e40a872950 | 333 | _window( 0, 0, _width, _height ); |
y_notsu | 0:58e40a872950 | 334 | _cs = 1; |
y_notsu | 0:58e40a872950 | 335 | } |
y_notsu | 0:58e40a872950 | 336 | |
y_notsu | 0:58e40a872950 | 337 | /** Screen width |
y_notsu | 0:58e40a872950 | 338 | * |
y_notsu | 0:58e40a872950 | 339 | * @return screen width [pixel] |
y_notsu | 0:58e40a872950 | 340 | */ |
y_notsu | 0:58e40a872950 | 341 | int width() { |
y_notsu | 0:58e40a872950 | 342 | return WIDTH; |
y_notsu | 0:58e40a872950 | 343 | } |
y_notsu | 0:58e40a872950 | 344 | |
y_notsu | 0:58e40a872950 | 345 | /** Screen height |
y_notsu | 0:58e40a872950 | 346 | * |
y_notsu | 0:58e40a872950 | 347 | * @return screen height [pixel] |
y_notsu | 0:58e40a872950 | 348 | */ |
y_notsu | 0:58e40a872950 | 349 | int height() { |
y_notsu | 0:58e40a872950 | 350 | return HEIGHT; |
y_notsu | 0:58e40a872950 | 351 | } |
y_notsu | 0:58e40a872950 | 352 | /** Columns |
y_notsu | 0:58e40a872950 | 353 | * |
y_notsu | 0:58e40a872950 | 354 | * @return screen columns |
y_notsu | 0:58e40a872950 | 355 | */ |
y_notsu | 0:58e40a872950 | 356 | int columns() { |
y_notsu | 0:58e40a872950 | 357 | return COLS; |
y_notsu | 0:58e40a872950 | 358 | } |
y_notsu | 0:58e40a872950 | 359 | |
y_notsu | 0:58e40a872950 | 360 | /** Rows |
y_notsu | 0:58e40a872950 | 361 | * |
y_notsu | 0:58e40a872950 | 362 | * @return screen rows |
y_notsu | 0:58e40a872950 | 363 | */ |
y_notsu | 0:58e40a872950 | 364 | int rows() { |
y_notsu | 0:58e40a872950 | 365 | return ROWS; |
y_notsu | 0:58e40a872950 | 366 | } |
y_notsu | 0:58e40a872950 | 367 | |
y_notsu | 0:58e40a872950 | 368 | /** Power switch for OLED backlight |
y_notsu | 0:58e40a872950 | 369 | * |
y_notsu | 0:58e40a872950 | 370 | * @param sw argument can be MARMEX_OB_oled::ON or MARMEX_OB_oled::OFF |
y_notsu | 0:58e40a872950 | 371 | */ |
y_notsu | 0:58e40a872950 | 372 | |
y_notsu | 0:58e40a872950 | 373 | void power( unsigned char sw ) { |
y_notsu | 0:58e40a872950 | 374 | _power_pin = sw; |
y_notsu | 0:58e40a872950 | 375 | } |
y_notsu | 0:58e40a872950 | 376 | |
y_notsu | 0:58e40a872950 | 377 | private: |
y_notsu | 0:58e40a872950 | 378 | /** Command list for the OLED controller */ |
y_notsu | 0:58e40a872950 | 379 | enum { |
y_notsu | 0:58e40a872950 | 380 | SET_DISPLAY_MODE_ALL_OFF = 0xA4, |
y_notsu | 0:58e40a872950 | 381 | SET_COMMAND_LOCK = 0xFD, |
y_notsu | 0:58e40a872950 | 382 | SET_SLEEP_MODE_ON = 0xAE, |
y_notsu | 0:58e40a872950 | 383 | FRONT_CLOCK_DRIVER_OSC_FREQ = 0xB3, |
y_notsu | 0:58e40a872950 | 384 | SET_MUX_RATIO = 0xCA, |
y_notsu | 0:58e40a872950 | 385 | SET_DISPAY_OFFSET = 0xA2, |
y_notsu | 0:58e40a872950 | 386 | SET_DISPAY_START_LINE = 0xA1, |
y_notsu | 0:58e40a872950 | 387 | SET_REMAP_COLOR_DEPTH = 0xA0, |
y_notsu | 0:58e40a872950 | 388 | SET_GPIO = 0xB5, |
y_notsu | 0:58e40a872950 | 389 | FUNCTION_SELECTION = 0xAB, |
y_notsu | 0:58e40a872950 | 390 | SET_SEGMENT_LOW_VOLTAGE = 0xB4, |
y_notsu | 0:58e40a872950 | 391 | SET_CONTRAST_CURRENT_FOR_COLOR_ABC = 0xC1, |
y_notsu | 0:58e40a872950 | 392 | MASTER_CONTRAST_CURRENT_CONTROL = 0xC7, |
y_notsu | 0:58e40a872950 | 393 | LOOKUP_TABLE_FOR_GRAYSCALE_PULSE_WIDTH = 0xB8, |
y_notsu | 0:58e40a872950 | 394 | SET_RESET_PRECHARGE_PERIOD = 0xB1, |
y_notsu | 0:58e40a872950 | 395 | ENHANCE_DRIVING_SCHEME_CAPABILITY = 0xB2, |
y_notsu | 0:58e40a872950 | 396 | SET_PRECHARGE_VOLTAGE = 0xBB, |
y_notsu | 0:58e40a872950 | 397 | SET_SECOND_PRECHARGE_VOLTAGE = 0xB6, |
y_notsu | 0:58e40a872950 | 398 | SET_VCOMH_VOLTAGE = 0xBE, |
y_notsu | 0:58e40a872950 | 399 | SET_DISPLAY_MODE_RESET = 0xA6, |
y_notsu | 0:58e40a872950 | 400 | SET_COLUMN_ADDRESS = 0x15, |
y_notsu | 0:58e40a872950 | 401 | SET_ROW_ADDRESS = 0x75, |
y_notsu | 0:58e40a872950 | 402 | WRITE_RAM_COMMAND = 0x5C, |
y_notsu | 0:58e40a872950 | 403 | SET_SLEEP_MODE_OFF = 0xAF |
y_notsu | 0:58e40a872950 | 404 | }; |
y_notsu | 0:58e40a872950 | 405 | |
y_notsu | 0:58e40a872950 | 406 | #ifdef MARMEX_OB_SPI_8BIT_MODE |
y_notsu | 0:58e40a872950 | 407 | void command( int value ) { |
y_notsu | 0:58e40a872950 | 408 | int tmp = value & 0x00ff; |
y_notsu | 0:58e40a872950 | 409 | _cs = 0; |
y_notsu | 0:58e40a872950 | 410 | _spi.write( tmp >> 1 ); |
y_notsu | 0:58e40a872950 | 411 | _spi.write( tmp << 7 ); |
y_notsu | 0:58e40a872950 | 412 | _cs = 1; |
y_notsu | 0:58e40a872950 | 413 | } |
y_notsu | 0:58e40a872950 | 414 | |
y_notsu | 0:58e40a872950 | 415 | void data( int value ) { |
y_notsu | 0:58e40a872950 | 416 | int tmp = value & 0x00ff; |
y_notsu | 0:58e40a872950 | 417 | tmp |= 0x0100; |
y_notsu | 0:58e40a872950 | 418 | _cs = 0; |
y_notsu | 0:58e40a872950 | 419 | _spi.write( tmp >> 1 ); |
y_notsu | 0:58e40a872950 | 420 | _spi.write( tmp << 7 ); |
y_notsu | 0:58e40a872950 | 421 | _cs = 1; |
y_notsu | 0:58e40a872950 | 422 | } |
y_notsu | 0:58e40a872950 | 423 | #else |
y_notsu | 0:58e40a872950 | 424 | void command( int value ) { |
y_notsu | 0:58e40a872950 | 425 | _cs = 0; |
y_notsu | 0:58e40a872950 | 426 | _spi.write( value & 0xFF ); |
y_notsu | 0:58e40a872950 | 427 | _cs = 1; |
y_notsu | 0:58e40a872950 | 428 | } |
y_notsu | 0:58e40a872950 | 429 | |
y_notsu | 0:58e40a872950 | 430 | void data(int value) { |
y_notsu | 0:58e40a872950 | 431 | _cs = 0; |
y_notsu | 0:58e40a872950 | 432 | _spi.write( value | 0x100 ); |
y_notsu | 0:58e40a872950 | 433 | _cs = 1; |
y_notsu | 0:58e40a872950 | 434 | } |
y_notsu | 0:58e40a872950 | 435 | #endif |
y_notsu | 0:58e40a872950 | 436 | |
y_notsu | 0:58e40a872950 | 437 | virtual void _window( int x, int y, int width, int height ) { |
y_notsu | 0:58e40a872950 | 438 | int x1 = x + 0; |
y_notsu | 0:58e40a872950 | 439 | int y1 = y + 0; |
y_notsu | 0:58e40a872950 | 440 | int x2 = x1 + width - 1; |
y_notsu | 0:58e40a872950 | 441 | int y2 = y1 + height - 1; |
y_notsu | 0:58e40a872950 | 442 | |
y_notsu | 0:58e40a872950 | 443 | command( SET_COLUMN_ADDRESS ); |
y_notsu | 0:58e40a872950 | 444 | data( x1 ); |
y_notsu | 0:58e40a872950 | 445 | data( x2 ); |
y_notsu | 0:58e40a872950 | 446 | command( SET_ROW_ADDRESS ); |
y_notsu | 0:58e40a872950 | 447 | data( y1 ); |
y_notsu | 0:58e40a872950 | 448 | data( y2 ); |
y_notsu | 0:58e40a872950 | 449 | command( WRITE_RAM_COMMAND ); |
y_notsu | 0:58e40a872950 | 450 | } |
y_notsu | 0:58e40a872950 | 451 | |
y_notsu | 0:58e40a872950 | 452 | void window( int x, int y, int width, int height ) { |
y_notsu | 0:58e40a872950 | 453 | _cs = 0; |
y_notsu | 0:58e40a872950 | 454 | _window( x, y, width, height ); |
y_notsu | 0:58e40a872950 | 455 | _cs = 1; |
y_notsu | 0:58e40a872950 | 456 | } |
y_notsu | 0:58e40a872950 | 457 | |
y_notsu | 0:58e40a872950 | 458 | virtual void _putp( int colour ) { |
y_notsu | 0:58e40a872950 | 459 | int cnv = 0; |
y_notsu | 0:58e40a872950 | 460 | |
y_notsu | 0:58e40a872950 | 461 | cnv = (colour >> 8) & 0xf800; |
y_notsu | 0:58e40a872950 | 462 | cnv |= (colour >> 5) & 0x07e0; |
y_notsu | 0:58e40a872950 | 463 | cnv |= (colour >> 3) & 0x001f; |
y_notsu | 0:58e40a872950 | 464 | |
y_notsu | 0:58e40a872950 | 465 | data( cnv >> 8); |
y_notsu | 0:58e40a872950 | 466 | data( cnv ); |
y_notsu | 0:58e40a872950 | 467 | } |
y_notsu | 0:58e40a872950 | 468 | |
y_notsu | 0:58e40a872950 | 469 | DigitalOut _power_pin; |
y_notsu | 0:58e40a872950 | 470 | } |
y_notsu | 0:58e40a872950 | 471 | ; |
y_notsu | 0:58e40a872950 | 472 | |
y_notsu | 0:58e40a872950 | 473 | #ifdef MARMEX_OB_SPI_8BIT_MODE |
y_notsu | 0:58e40a872950 | 474 | #define MERMEX_OB_SPI_MODE_STR "8bit mode" |
y_notsu | 0:58e40a872950 | 475 | #else |
y_notsu | 0:58e40a872950 | 476 | #define MERMEX_OB_SPI_MODE_STR "9bit mode" |
y_notsu | 0:58e40a872950 | 477 | #endif |
y_notsu | 0:58e40a872950 | 478 | #endif // MBED_MARMEX_OB_OLED |
y_notsu | 0:58e40a872950 | 479 | |
y_notsu | 0:58e40a872950 | 480 | /* |
y_notsu | 0:58e40a872950 | 481 | * history: |
y_notsu | 0:58e40a872950 | 482 | * 0.5 (2011-Apr-07) : initial published version |
y_notsu | 0:58e40a872950 | 483 | * 0.51 (2011-Apr-08) : a. "virtual" had been added on "_putp()" function definition to surpress warning when compiling (is this correct way?) |
y_notsu | 0:58e40a872950 | 484 | * b. sample code (for Doxygen) is changed from new "main.cpp (ver 0.51)" |
y_notsu | 0:58e40a872950 | 485 | */ |