A class library for OLED on MARMEX_OB board. MARY拡張のMARMEX_OB基板のOLEDをmbed+MAPLE基板で動かすためのライブラリです.このサンプルではMAPLEに用意されているMARMEXスロットの1番に搭載したOLEDを制御するようになっています.詳細はCode&APIからソースやAPI詳細をご覧ください

Dependencies:   mbed NokiaLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MARMEX_OB_oled.h Source File

MARMEX_OB_oled.h

00001 /** MARMEX_OB OLED screen drawing library
00002  *
00003  *  @class   MARMEX_OB_oled
00004  *  @author  tedd
00005  *  @version 0.51
00006  *  @date    08-Apr-2011
00007  *
00008  *  Released under the MIT License: http://mbed.org/license/mit
00009  *
00010  *  MARMEX_OB_oled OLED screen drawing library for mbed
00011  *  This code has been written based on sample code and advises
00012  *    from Ochiai-san (Marutsu-Elec). Thank you!
00013  *
00014  *  SPI mode:
00015  *    9bit or 8bit SPI mode can be selected by disabling/enabling "#define MARMEX_OB_SPI_8BIT_MODE".
00016  *    See source code in this (MARMEX_OB_oled.h) file.
00017  */
00018 
00019 #ifndef     MBED_MARMEX_OB_OLED
00020 #define     MBED_MARMEX_OB_OLED
00021 
00022 #include    "mbed.h"
00023 #include    "NokiaLCD.h"
00024 
00025 /** @def MARMEX_OB_SPI_8BIT_MODE
00026  *
00027  *  MARMEX_OB_oled_oled OLED screen SPI access length setting
00028  *  Enabling "MARMEX_OB_SPI_8BIT_MODE" makes 9bit SPI access by 8bit * 2 times.
00029  *  This may be useful if other 8bit access SPI device on same SPI bus.
00030  *
00031  *  If disabled (just coment out the "#define MARMEX_OB_SPI_8BIT_MODE"), SPI access willbe done by 9 bit format.
00032  */
00033 //#define MARMEX_OB_SPI_8BIT_MODE
00034 
00035 /** MARMEX_OB_oled OLED screen drawing class
00036  *
00037  *  This is a driver code for MARMEX_OB_oled board OLED screen.
00038  *  This class inherits NokiaLCD class of mbed.org.
00039  *  To use this class, import the NokiaLCD class from here..
00040  *   http://mbed.org/users/simon/libraries/NokiaLCD/
00041  *
00042  *  Example:
00043  *  @code
00044  *  #include "mbed.h"
00045  *  #include "MARMEX_OB_oled.h"
00046  *  
00047  *  //  oled1 is for MARMEX_OB_oled board on MAPLE slot 1
00048  *  //  oled1 is for MARMEX_OB_oled board on MAPLE slot 2
00049  *  
00050  *  MARMEX_OB_oled   oled1( p5, p7,  p8, p30, p11 ); // mosi, sclk, cs, rst, power_control
00051  *  //MARMEX_OB_oled   oled2( p5, p7, p26, p21, p17 ); // mosi, sclk, cs, rst, power_control
00052  *  
00053  *  
00054  *  int main() {
00055  *      oled1.background( 0x000000 );
00056  *      oled1.cls();
00057  *     
00058  *      int colorbar_width  = MARMEX_OB_oled::WIDTH / 8;
00059  *  
00060  *      for ( int i = 0; i < 8; i++ )
00061  *          oled1.fill( colorbar_width * i, 0, colorbar_width, MARMEX_OB_oled::HEIGHT, ((i & 0x4) ? 0xFF0000 : 0x000000) | ((i & 0x2) ? 0x00FF00 : 0x000000) | ((i & 0x1) ? 0x0000FF : 0x000000) );
00062  *  
00063  *      oled1.fill(  50,  50,  64,  64, 0xCCCCCC );;
00064  *  
00065  *      oled1.locate( 0, 3 );
00066  *      oled1.printf( "Hello World!" );
00067  *      oled1.locate( 0, 4 );
00068  *      oled1.printf( "SPI = %s", MERMEX_OB_SPI_MODE_STR );
00069  *  
00070  *      for (int i = 0; i < MARMEX_OB_oled::WIDTH; i++ ) {
00071  *          oled1.pixel( i, 80 + sin( (float)i / 5.0 ) * 10, 0x000000 );
00072  *      }
00073  *  } *  @endcode
00074  */
00075 
00076 class MARMEX_OB_oled : public NokiaLCD {
00077 
00078 public:
00079 
00080     /** General parameters for MARMEX_OB_oled */
00081     enum  {
00082         ROWS          = 15,         /**< # of rows (lines) for displaying characters  */
00083         COLS          = 16,         /**< # of columns (width) for displaying characters  */
00084         WIDTH         = 128,        /**< screen width [pixels]  */
00085         HEIGHT        = 128,        /**< screen height [pixels]  */
00086         SPI_FREQUENCY = 20000000    /**< SPI (sclk) SPI_FREQUENCY  */
00087     };
00088 
00089     /** Constants for power() function */
00090     enum  {
00091         OFF   = 0,  /**< : to turning-OFF  */
00092         ON          /**< : to turning-ON   */
00093     };
00094 
00095     /** Create a MARMEX_OB_oled object connected to specified SPI and DigitalOut pins
00096      *
00097      *  @param mosi SPI-MOSI pin (for MAPLE board, use p5)
00098      *  @param sclk SPI-SCLK pin (for MAPLE board, use p8)
00099      *  @param cs   chip select signal (for MAPLE board, use p8(slot1), p26(slot2))
00100      *  @param rst  reset signal (for MAPLE board, use p30(slot1), p21(slot2))
00101      *  @param power_pin backlight power control signal (for MAPLE board, use p11(slot1), p17(slot2))
00102      *
00103      *  Example of MARMEX_OB_oled on MAPLE board:
00104      *  @code
00105      *  #include "mbed.h"
00106      *  #include "MARMEX_OB_oled.h"
00107      *
00108      *  MARMEX_OB_oled   oled_on_maple_slot1( p5, p7,  p8, p30, p11 ); // mosi, sclk, cs, rst, power_control
00109      *  MARMEX_OB_oled   oled_on_maple_slot2( p5, p7, p26, p21, p17 ); // mosi, sclk, cs, rst, power_control
00110      *  ...
00111      *  ..
00112      *  @endcode
00113      */
00114 
00115     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 ) {
00116         power( ON );
00117         reset();
00118     }
00119 
00120 #if DOXYGEN_ONLY
00121     /** Write a character to the LCD
00122      *
00123      * @param c The character to write to the display
00124      */
00125     int putc( int c );
00126 
00127     /** Write a formated string to the LCD
00128      *
00129      * @param format A printf-style format string, followed by the
00130      *               variables to use in formating the string.
00131      *
00132      *  !!! 16th character in the string will be disappeared
00133      *  !!! This problem is due to difference of screen size NokiaLCD library and its internal mechanism...
00134      */
00135     int printf( const char* format, ... );
00136 
00137     /** Set the foreground colour
00138      *
00139      * @param c 24-bit colour
00140      */
00141     void foreground(int c);
00142 
00143     /** Set the background colour
00144      *
00145      * @param c 24-bit colour
00146      */
00147     void background(int c);
00148 
00149 #endif
00150 
00151     /** reset MARMEX_OB_oled
00152      *
00153      *  Executes hardware reset and initialize.
00154      *  See MARMEX_OB_oled manual for the initialization sequence and values
00155      *  For gamma correction table, using math function to make the code simple
00156      */
00157 
00158     void reset( void ) {
00159 
00160 #define GAMMA_LUT_SIZE 63
00161         unsigned char    gamma_LUT[ GAMMA_LUT_SIZE ];
00162 
00163         for ( int i = 0; i < GAMMA_LUT_SIZE; i++ )
00164             gamma_LUT[ i ]  = (unsigned char)(powf( ((float)i / 62.0), (1.0 / 0.58) ) * 178.0 + 2.0);
00165 
00166         // setup the SPI interface and bring display out of reset
00167         _cs = 1;
00168         _rst = 0;
00169 #ifdef MARMEX_OB_SPI_8BIT_MODE
00170         _spi.format( 8 );
00171 #else
00172         _spi.format( 9 );
00173 #endif
00174 
00175         _spi.frequency( SPI_FREQUENCY );
00176         wait_ms( 1 );
00177         _rst = 1;
00178         wait_ms( 1 );
00179 
00180         _cs = 0;
00181 
00182         command( SET_DISPLAY_MODE_ALL_OFF );
00183         command( SET_COMMAND_LOCK );
00184         data( 0x12 );
00185 
00186         command( SET_COMMAND_LOCK );
00187         data( 0xb1 );
00188 
00189         command( SET_SLEEP_MODE_ON );
00190 
00191         command( FRONT_CLOCK_DRIVER_OSC_FREQ );
00192         data( 0xF1 );
00193 
00194         command( SET_MUX_RATIO );
00195         data( 0x7F );
00196 
00197         command( SET_DISPAY_OFFSET );
00198         data( 0x00 );
00199 
00200         command( SET_DISPAY_START_LINE );
00201         data( 0x00 );
00202 
00203         command( SET_REMAP_COLOR_DEPTH );
00204         data( 0x74 );
00205 
00206         command( SET_GPIO );
00207         data( 0x00);
00208 
00209         command( FUNCTION_SELECTION );
00210         data( 0x01 );
00211 
00212         command( SET_SEGMENT_LOW_VOLTAGE );
00213         data( 0xA0 );
00214         data( 0xB5 );
00215         data( 0x55 );
00216 
00217         command( SET_CONTRAST_CURRENT_FOR_COLOR_ABC );
00218         data( 0xC8 );
00219         data( 0x80 );
00220         data( 0xC8 );
00221 
00222         command( MASTER_CONTRAST_CURRENT_CONTROL );
00223         data( 0x0F );
00224 
00225         command( LOOKUP_TABLE_FOR_GRAYSCALE_PULSE_WIDTH );
00226         for ( int i = 0; i < GAMMA_LUT_SIZE; i++ )
00227             data(  gamma_LUT[ i ] );
00228 
00229         command( SET_RESET_PRECHARGE_PERIOD );
00230         data( 0x32 );
00231 
00232         command( ENHANCE_DRIVING_SCHEME_CAPABILITY );
00233         data( 0x04 );
00234         data( 0x00 );
00235         data( 0x00 );
00236 
00237         command( SET_PRECHARGE_VOLTAGE );
00238         data( 0x17 );
00239 
00240         command( SET_SECOND_PRECHARGE_VOLTAGE );
00241         data( 0x01 );
00242 
00243         command( SET_VCOMH_VOLTAGE );
00244         data( 0x05 );
00245 
00246         command( SET_DISPLAY_MODE_RESET );
00247 
00248 #if 0
00249         command( SET_COLUMN_ADDRESS );
00250         data( 0x00 );
00251         data( 0x7F );
00252 
00253         command( SET_ROW_ADDRESS );
00254         data( 0x00 );
00255         data( 0x7F);
00256 
00257         command( WRITE_RAM_COMMAND );
00258         for ( int i = 0; i < WIDTH * HEIGHT; i++ )
00259             data( 0x00 );
00260 #endif
00261         _cs = 1;
00262 
00263         cls();
00264         wait_ms( 200 );
00265 
00266         command( SET_SLEEP_MODE_OFF );
00267     }
00268 
00269     /** Clear the screen and locate to 0,0 */
00270 
00271     void cls( void ) {
00272         fill( 0, 0, WIDTH , HEIGHT, _background );
00273         _row = 0;
00274         _column = 0;
00275     }
00276 
00277     /** Set a pixel on te screen
00278      *
00279      * @param x horizontal position from left
00280      * @param y vertical position from top
00281      * @param colour 24-bit colour in format 0x00RRGGBB
00282      */
00283 
00284     virtual void pixel( int x, int y, int colour ) {
00285         _cs = 0;
00286         _window( x, y, 1, 1 );
00287         _putp( colour );
00288         _cs = 1;
00289     }
00290 
00291     /** Fill an area of the screen
00292      *
00293      * @param x horizontal position from left
00294      * @param y vertical position from top
00295      * @param width width in pixels
00296      * @param height height in pixels
00297      * @param colour 24-bit colour in format 0x00RRGGBB
00298      */
00299 
00300     void fill( int x, int y, int width, int height, int colour ) {
00301         _cs = 0;
00302         _window( x, y, width, height );
00303 
00304         for (int i = 0; i < width * height; i++ ) {
00305             _putp( colour );
00306         }
00307 
00308         _window( 0, 0, WIDTH, HEIGHT );
00309         _cs = 1;
00310     }
00311 
00312     void blit( int x, int y, int width, int height, const int* colour ) {
00313         _cs = 0;
00314         _window( x, y, width, height );
00315 
00316         for (int i = 0; i < width * height; i++ ) {
00317             _putp( colour[i] );
00318         }
00319         _window( 0, 0, WIDTH, HEIGHT );
00320         _cs = 1;
00321     }
00322 
00323     void bitblit( int x, int y, int width, int height, const char* bitstream ) {
00324         _cs = 0;
00325         _window( x, y, width, height );
00326 
00327         for (int i = 0; i < height * width; i++ ) {
00328             int byte = i / 8;
00329             int bit = i % 8;
00330             int colour = ((bitstream[ byte ] << bit) & 0x80) ? _foreground : _background;
00331             _putp( colour );
00332         }
00333         _window( 0, 0, _width, _height );
00334         _cs = 1;
00335     }
00336 
00337     /** Screen width
00338      *
00339      *  @return screen width [pixel]
00340      */
00341     int width() {
00342         return WIDTH;
00343     }
00344 
00345     /** Screen height
00346      *
00347      *  @return screen height [pixel]
00348      */
00349     int height() {
00350         return HEIGHT;
00351     }
00352     /** Columns
00353      *
00354      *  @return screen columns
00355      */
00356     int columns() {
00357         return COLS;
00358     }
00359 
00360     /** Rows
00361      *
00362      *  @return screen rows
00363      */
00364     int rows() {
00365         return ROWS;
00366     }
00367 
00368     /** Power switch for OLED backlight
00369      *
00370      * @param sw argument can be MARMEX_OB_oled::ON or MARMEX_OB_oled::OFF
00371      */
00372 
00373     void power( unsigned char sw ) {
00374         _power_pin  = sw;
00375     }
00376 
00377 private:
00378     /** Command list for the OLED controller */
00379     enum {
00380         SET_DISPLAY_MODE_ALL_OFF                = 0xA4,
00381         SET_COMMAND_LOCK                        = 0xFD,
00382         SET_SLEEP_MODE_ON                       = 0xAE,
00383         FRONT_CLOCK_DRIVER_OSC_FREQ             = 0xB3,
00384         SET_MUX_RATIO                           = 0xCA,
00385         SET_DISPAY_OFFSET                       = 0xA2,
00386         SET_DISPAY_START_LINE                   = 0xA1,
00387         SET_REMAP_COLOR_DEPTH                   = 0xA0,
00388         SET_GPIO                                = 0xB5,
00389         FUNCTION_SELECTION                      = 0xAB,
00390         SET_SEGMENT_LOW_VOLTAGE                 = 0xB4,
00391         SET_CONTRAST_CURRENT_FOR_COLOR_ABC      = 0xC1,
00392         MASTER_CONTRAST_CURRENT_CONTROL         = 0xC7,
00393         LOOKUP_TABLE_FOR_GRAYSCALE_PULSE_WIDTH  = 0xB8,
00394         SET_RESET_PRECHARGE_PERIOD              = 0xB1,
00395         ENHANCE_DRIVING_SCHEME_CAPABILITY       = 0xB2,
00396         SET_PRECHARGE_VOLTAGE                   = 0xBB,
00397         SET_SECOND_PRECHARGE_VOLTAGE            = 0xB6,
00398         SET_VCOMH_VOLTAGE                       = 0xBE,
00399         SET_DISPLAY_MODE_RESET                  = 0xA6,
00400         SET_COLUMN_ADDRESS                      = 0x15,
00401         SET_ROW_ADDRESS                         = 0x75,
00402         WRITE_RAM_COMMAND                       = 0x5C,
00403         SET_SLEEP_MODE_OFF                      = 0xAF
00404     };
00405 
00406 #ifdef MARMEX_OB_SPI_8BIT_MODE
00407     void command( int value ) {
00408         int tmp = value & 0x00ff;
00409         _cs = 0;
00410         _spi.write( tmp >> 1 );
00411         _spi.write( tmp << 7 );
00412         _cs = 1;
00413     }
00414 
00415     void data( int value ) {
00416         int tmp  = value & 0x00ff;
00417         tmp |= 0x0100;
00418         _cs = 0;
00419         _spi.write( tmp >> 1 );
00420         _spi.write( tmp << 7 );
00421         _cs = 1;
00422     }
00423 #else
00424     void command( int value ) {
00425         _cs = 0;
00426         _spi.write( value & 0xFF );
00427         _cs = 1;
00428     }
00429 
00430     void data(int value) {
00431         _cs = 0;
00432         _spi.write( value | 0x100 );
00433         _cs = 1;
00434     }
00435 #endif
00436 
00437     virtual void _window( int x, int y, int width, int height ) {
00438         int x1 = x + 0;
00439         int y1 = y + 0;
00440         int x2 = x1 + width - 1;
00441         int y2 = y1 + height - 1;
00442 
00443         command( SET_COLUMN_ADDRESS );
00444         data( x1 );
00445         data( x2 );
00446         command( SET_ROW_ADDRESS );
00447         data( y1 );
00448         data( y2 );
00449         command( WRITE_RAM_COMMAND );
00450     }
00451 
00452     void window( int x, int y, int width, int height ) {
00453         _cs = 0;
00454         _window( x, y, width, height );
00455         _cs = 1;
00456     }
00457 
00458     virtual void _putp( int colour ) {
00459         int cnv = 0;
00460 
00461         cnv  = (colour >> 8) & 0xf800;
00462         cnv |= (colour >> 5) & 0x07e0;
00463         cnv |= (colour >> 3) & 0x001f;
00464 
00465         data( cnv >> 8);
00466         data( cnv );
00467     }
00468 
00469     DigitalOut     _power_pin;
00470 }
00471 ;
00472 
00473 #ifdef  MARMEX_OB_SPI_8BIT_MODE
00474 #define MERMEX_OB_SPI_MODE_STR  "8bit mode"
00475 #else
00476 #define MERMEX_OB_SPI_MODE_STR  "9bit mode"
00477 #endif
00478 #endif  //  MBED_MARMEX_OB_OLED
00479 
00480 /*
00481  *  history:
00482  *      0.5  (2011-Apr-07) :  initial published version
00483  *      0.51 (2011-Apr-08) :  a. "virtual" had been added on "_putp()" function definition to surpress warning when compiling (is this correct way?)
00484  *                            b. sample code (for Doxygen) is changed from new "main.cpp (ver 0.51)" 
00485  */