Tedd OKANO / Mbed 2 deprecated MARY_CAMERA_Hello

Dependencies:   MARY_CAMERA NokiaLCD mbed

main.cpp

Committer:
okano
Date:
2014-02-14
Revision:
1:ce27bc7b44d4
Parent:
0:1062142e5718
Child:
2:2e03fc4f485b

File content as of revision 1:ce27bc7b44d4:

/** A sample app of MARMEX_OB OLED screen drawing library
 *
 *  @author  Tedd
 *  @version 0.5
 *  @date    07-Apr-2011
 *
 *  Released under the MIT License: http://mbed.org/license/mit
 *
 *  MARMEX_OB_oled OLED screen drawing library for mbed
 *  This code has been written based on sample code and advises
 *    from Ochiai-san (Marutsu-Elec). Thank you!
 *
 *  To build this code, "NokiaLCD" and "mbed" libraries are needed to be imported in a project.
 *     NokiaLCD library :  http://mbed.org/users/simon/libraries/NokiaLCD/
 */


#include "mbed.h"
#include "MARMEX_OB_oled.h"


#define PARAM_NUM       99
#define CAM_I2C_ADDR    0x42

const char param[2][PARAM_NUM] = {
    {
        0x01,0x02,0x03,0x0c,0x0e,0x0f,0x11,0x12,0x15,0x16,0x17,0x18,0x19,0x1a,0x1e,0x21,0x22,
        0x29,0x32,0x33,0x34,0x35,0x37,0x38,0x39,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x41,
        0x43,0x44,0x45,0x46,0x47,0x48,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,
        0x54,0x56,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
        0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x8d,0x8e,0x8f,0x90,
        0x91,0x96,0x96,0x97,0x98,0x99,0x9a,0x9a,0x9b,0x9c,0x9d,0x9e,0xa2,0xa4,0xb0,
        0xb1,0xb2,0xb3,0xb8,0xc8,0xc9
    },
    {
        0x40,0x60,0x02,0x0c,0x61,0x4b,0x81,0x04 ,0x00,0x02,0x39,0x03,0x03,0x7b,0x37,0x02,0x91,
        0x07,0x80,0x0b,0x11,0x0b,0x1d,0x71,0x2a,0x12,0x78,0xc3,0x11,0x00,0xd0,0x08,0x38,
        0x0a,0xf0,0x34,0x58,0x28,0x3a,0x09,0x00,0x40,0x20,0x80,0x80,0x00,0x22,0x5e,
        0x80,0x40,0x9e,0x88,0x88,0x44,0x67,0x49,0x0e,0x00,0x40,0x0a,0x0a,0x55,0x11,
        0x9f,0x3a,0x35,0x11,0xf1,0x10,0x05,0xe1,0x01,0x04,0x01,0x4f,0x00,0x00,0x00,
        0x00,0x00,0x00,0x30,0x20,0x30,0x00,0x84,0x29,0x03,0x4c,0x3f,0x52,0x88,0x84,
        0x0c,0x0e,0x82,0x0a,0xf0,0x60
    }
};


MARMEX_OB_oled   oled1( p5, p7,  p20, p16, p15 ); // mosi, sclk, cs, rst, power_control     -- maple-mini-type-b-slot1


SPI         spi( p5, p6, p7 );
DigitalOut  cs2( p22 );
DigitalOut  vsync( p9 );
DigitalOut  camera_reset( p26 );

void init( void )
{
    I2C     i2c( p28, p27 );

    vsync   = 0;
    cs2 = 1;
    camera_reset = 0;
    wait_ms( 100 );
    camera_reset = 1;
    wait_ms( 100 );


    for ( int i = 0; i < PARAM_NUM; i++ ) {
        i2c.start();
        i2c.write( CAM_I2C_ADDR );
        i2c.write( param[ 0 ][ i ] );
        i2c.write( param[ 1 ][ i ] );
        i2c.stop();
        wait_ms( 20 );
    }
    
    
    spi.format(8);
    spi.frequency( 1000000 );
    vsync   = 0;

}

int send_spi2( char data )
{
    int     tmp;

    cs2 = 0;
    tmp = spi.write( data );
    cs2 = 1;

    return ( tmp );
}

#define     PREPARE     0x1
#define     DONE        0x0

void transfer_control( int flag )
{
    send_spi2( 0x03 );
    send_spi2( flag );
    send_spi2( 0x84 );

    while ( (send_spi2( 0x84 ) ^ flag) & 0x1 )
        ;
        
    vsync   = flag ? 0 : 1;
}

void open_transfer( void )
{
    transfer_control( PREPARE );
}

void close_transfer( void )
{
    transfer_control( DONE );
}


void set_address( int address )
{
    send_spi2( 0x00 );
    send_spi2( (address >> 0) & 0xFF );
    send_spi2( 0x01 );
    send_spi2( (address >> 8) & 0xFF );
    send_spi2( 0x02 );
    send_spi2( (address >> 16) & 0xFF );
}

#define     PIXEL_PER_LINE  176
#define     BYTE_PER_PIXEL  2
#define     BYTE_PER_LINE   (PIXEL_PER_LINE * BYTE_PER_PIXEL)


void transfer_a_line( short *p, int line_number, int x_offset, int n_of_pixels )
{
    set_address( line_number * BYTE_PER_LINE + x_offset * BYTE_PER_PIXEL);

    for( int x = 0; x < n_of_pixels; x++ )
        *p++    = (send_spi2( 0xA8 ) << 8) | send_spi2( 0xA8 );
}


#define X_OFFSET    24
#define Y_OFFSET    8

int main()
{
    short   buf[ 128 ];

    oled1.cls();

    short   p[3][ 128 ]    = { { 0xF800 }, {0x07E0}, { 0x001F } };

    for ( int i = 0; i < 128; i++ )
        p[ 0 ][ i ]   = 0xF800;
    for ( int i = 0; i < 128; i++ )
        p[ 1 ][ i ]   = 0x07E0;
    for ( int i = 0; i < 128; i++ )
        p[ 2 ][ i ]   = 0x001F;

    int count   = 0;
#if 0
    while ( 1 ) {
        for ( int i = 0; i < 128; i++ ) {
            oled1.blit565( 0, i, 128, 1, p[ count % 3 ] );
        }
        count++;
        
        if ( count == 3 )
            break;
    }
#endif

    init();

    while ( 1 ) {

        open_transfer();
        printf( "transfer ready\r\n" );

        for ( int line = 0 + Y_OFFSET; line < 128 + Y_OFFSET; line++  ) {
            transfer_a_line( buf, line, X_OFFSET, 128 );
           
            oled1.blit565( 0, line, 128, 1, buf );
        }

        close_transfer();
        printf( "loop done\r\n" );

    }

#if 0
    oled1.background( 0x000000 );
    oled1.cls();

    int colorbar_width  = MARMEX_OB_oled::WIDTH / 8;

    for ( int i = 0; i < 8; i++ )
        oled1.fill( colorbar_width * i, 0, colorbar_width, MARMEX_OB_oled::HEIGHT, ((i & 0x4) ? 0xFF0000 : 0x000000) | ((i & 0x2) ? 0x00FF00 : 0x000000) | ((i & 0x1) ? 0x0000FF : 0x000000) );

    oled1.fill(  50,  50,  64,  64, 0xCCCCCC );;

    oled1.locate( 0, 3 );
    oled1.printf( "Hello World!" );
    oled1.locate( 0, 4 );
    oled1.printf( "SPI = %s", MERMEX_OB_SPI_MODE_STR );

    for (int i = 0; i < MARMEX_OB_oled::WIDTH; i++ ) {
        oled1.pixel( i, 80 + sin( (float)i / 5.0 ) * 10, 0x000000 );
    }

    short   p[3][ 128 ]    = { { 0xF800 }, {0x07E0}, { 0x001F } };

    for ( int i = 0; i < 128; i++ )
        p[ 0 ][ i ]   = 0xF800;
    for ( int i = 0; i < 128; i++ )
        p[ 1 ][ i ]   = 0x07E0;
    for ( int i = 0; i < 128; i++ )
        p[ 2 ][ i ]   = 0x001F;

    int count   = 0;
    while ( 1 ) {
        for ( int i = 0; i < 128; i++ ) {
            oled1.blit565( 0, i, 128, 1, p[ count % 3 ] );
        }
        count++;
    }
#endif
}