Tedd OKANO / MARY_CAMERA
Committer:
okano
Date:
Sat Mar 22 01:50:50 2014 +0000
Revision:
29:4432c5282c7b
Parent:
28:b5d5a2f7f0d0
test code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 4:cb0ef3fd89c9 1 #ifndef MBED_MARY_CAMERA
okano 4:cb0ef3fd89c9 2 #define MBED_MARY_CAMERA
okano 0:f4584dba3bac 3
okano 21:db9c0690c0a2 4 #define SPI_FREQUENCY 6000000
okano 0:f4584dba3bac 5
okano 4:cb0ef3fd89c9 6 /** MARY_CAMERA class
okano 4:cb0ef3fd89c9 7 *
okano 4:cb0ef3fd89c9 8 * MARY_CAMERA driver library
okano 4:cb0ef3fd89c9 9 * This driver has been made to get camera data from MARY CAMERA.
okano 4:cb0ef3fd89c9 10 *
okano 4:cb0ef3fd89c9 11 * Example:
okano 4:cb0ef3fd89c9 12 * @code
okano 13:210f4bbd0cd6 13 *
okano 4:cb0ef3fd89c9 14 * #include "mbed.h"
okano 4:cb0ef3fd89c9 15 * #include "MARMEX_OB_oled.h"
okano 4:cb0ef3fd89c9 16 * #include "MARY_CAMERA.h"
okano 13:210f4bbd0cd6 17 *
okano 4:cb0ef3fd89c9 18 * MARMEX_OB_oled oled1( p5, p7, p20, p16, p15 ); // mosi, sclk, cs, rst, power_control -- maple-mini-type-b-slot1
okano 4:cb0ef3fd89c9 19 * MARY_CAMERA camera( p5, p6, p7, p22, p26, p28, p27 ); // mosi, miso, sclk, cs, reset, I2C_SDA, I2C_SCL
okano 4:cb0ef3fd89c9 20 * BusOut led( LED3, LED4 );
okano 13:210f4bbd0cd6 21 *
okano 4:cb0ef3fd89c9 22 * #define X_OFFSET ((MARY_CAMERA::PIXEL_PER_LINE - MARMEX_OB_oled::WIDTH ) / 2)
okano 4:cb0ef3fd89c9 23 * #define Y_OFFSET ((MARY_CAMERA::LINE_PER_FRAME - MARMEX_OB_oled::HEIGHT) / 2)
okano 13:210f4bbd0cd6 24 *
okano 4:cb0ef3fd89c9 25 * int main()
okano 4:cb0ef3fd89c9 26 * {
okano 4:cb0ef3fd89c9 27 * led = 0x3;
okano 13:210f4bbd0cd6 28 *
okano 4:cb0ef3fd89c9 29 * oled1.cls();
okano 13:210f4bbd0cd6 30 *
okano 4:cb0ef3fd89c9 31 * short buf[ MARMEX_OB_oled::WIDTH ];
okano 13:210f4bbd0cd6 32 *
okano 4:cb0ef3fd89c9 33 * while ( 1 ) {
okano 13:210f4bbd0cd6 34 *
okano 4:cb0ef3fd89c9 35 * led = 0x1;
okano 4:cb0ef3fd89c9 36 * camera.open_transfer();
okano 13:210f4bbd0cd6 37 *
okano 4:cb0ef3fd89c9 38 * for ( int line = 0; line < 128; line++ ) {
okano 4:cb0ef3fd89c9 39 * camera.transfer_a_line( buf, line + Y_OFFSET, X_OFFSET, 128 );
okano 4:cb0ef3fd89c9 40 * oled1.blit565( 0, line, 128, 1, buf );
okano 4:cb0ef3fd89c9 41 * }
okano 13:210f4bbd0cd6 42 *
okano 4:cb0ef3fd89c9 43 * camera.close_transfer();
okano 4:cb0ef3fd89c9 44 * led = 0x2;
okano 4:cb0ef3fd89c9 45 * }
okano 4:cb0ef3fd89c9 46 * }
okano 4:cb0ef3fd89c9 47 * @endcode
okano 4:cb0ef3fd89c9 48 */
okano 13:210f4bbd0cd6 49
okano 13:210f4bbd0cd6 50
okano 19:1d07d6d762a9 51 #define DEFAULT_PICTURE_SIZE QCIF
okano 19:1d07d6d762a9 52 //#define DEFAULT_PICTURE_SIZE VGA
okano 19:1d07d6d762a9 53 //#define DEFAULT_PICTURE_SIZE QVGA
okano 19:1d07d6d762a9 54 //#define DEFAULT_PICTURE_SIZE QQVGA
okano 17:756fb618c0ed 55
okano 0:f4584dba3bac 56 class MARY_CAMERA
okano 0:f4584dba3bac 57 {
okano 0:f4584dba3bac 58 public:
okano 19:1d07d6d762a9 59 typedef enum {
okano 22:1a923c255be6 60 QCIF = 1, /**< QCIF */
okano 19:1d07d6d762a9 61 VGA, /**< VGA */
okano 19:1d07d6d762a9 62 QVGA, /**< QVGA */
okano 19:1d07d6d762a9 63 QQVGA, /**< QQVGA */
okano 22:1a923c255be6 64 } CameraResolution;
okano 0:f4584dba3bac 65
okano 20:fa4a54e25fc4 66 typedef enum {
okano 20:fa4a54e25fc4 67 OFF = 0, /**< ON */
okano 20:fa4a54e25fc4 68 ON, /**< OFF */
okano 20:fa4a54e25fc4 69 } SwitchState;
okano 20:fa4a54e25fc4 70
okano 8:23d14d5254d2 71 /** General parameters for MARY_CAMERA */
okano 4:cb0ef3fd89c9 72 enum {
okano 19:1d07d6d762a9 73 BYTE_PER_PIXEL = 2, /**< bytes per pixel */
okano 17:756fb618c0ed 74
okano 19:1d07d6d762a9 75 QCIF_PIXEL_PER_LINE = 176, /**< pixels in a line */
okano 19:1d07d6d762a9 76 QCIF_LINE_PER_FRAME = 144, /**< pixels in a line */
okano 19:1d07d6d762a9 77 VGA_PIXEL_PER_LINE = 640, /**< pixels in a line */
okano 19:1d07d6d762a9 78 VGA_LINE_PER_FRAME = 480, /**< pixels in a line */
okano 4:cb0ef3fd89c9 79 };
okano 4:cb0ef3fd89c9 80
okano 8:23d14d5254d2 81 /** General parameters for MARMEX_OB_oled */
okano 8:23d14d5254d2 82 enum {
okano 8:23d14d5254d2 83 NO_ERROR = 0 /**< zero */
okano 8:23d14d5254d2 84 };
okano 8:23d14d5254d2 85
okano 4:cb0ef3fd89c9 86 /** Create a MARY_CAMERA instance connected to specified SPI, DigitalOut and I2C pins
okano 0:f4584dba3bac 87 *
okano 4:cb0ef3fd89c9 88 * @param SPI_mosi SPI-bus MOSI pin
okano 4:cb0ef3fd89c9 89 * @param SPI_miso SPI-bus MISO pin
okano 4:cb0ef3fd89c9 90 * @param SPI_sclk SPI-bus SCLK pin
okano 4:cb0ef3fd89c9 91 * @param SPI_cs SPI-bus Chip Select pin
okano 4:cb0ef3fd89c9 92 * @param cam_reset Camera reset pin
okano 4:cb0ef3fd89c9 93 * @param I2C_sda I2C-bus SDA pin
okano 4:cb0ef3fd89c9 94 * @param I2C_scl I2C-bus SCL pin
okano 0:f4584dba3bac 95 */
okano 0:f4584dba3bac 96
okano 0:f4584dba3bac 97 MARY_CAMERA(
okano 4:cb0ef3fd89c9 98 PinName SPI_mosi,
okano 4:cb0ef3fd89c9 99 PinName SPI_miso,
okano 4:cb0ef3fd89c9 100 PinName SPI_sck,
okano 4:cb0ef3fd89c9 101 PinName SPI_cs,
okano 0:f4584dba3bac 102 PinName cam_reset,
okano 4:cb0ef3fd89c9 103 PinName I2C_sda,
okano 4:cb0ef3fd89c9 104 PinName I2C_scl
okano 0:f4584dba3bac 105 );
okano 4:cb0ef3fd89c9 106
okano 4:cb0ef3fd89c9 107 /** Initialization
okano 4:cb0ef3fd89c9 108 *
okano 4:cb0ef3fd89c9 109 * Performs MARY_CAMERA reset and initializations
okano 13:210f4bbd0cd6 110 * This function is called from MARY_CAMERA constoructor. So user don't have to call in the user code.
okano 4:cb0ef3fd89c9 111 *
okano 13:210f4bbd0cd6 112 * This function takes about 2 seconds because there is 99 times I2C access with 20ms interval.
okano 22:1a923c255be6 113 *
okano 22:1a923c255be6 114 * @param res select camera resolution : QCIF(default), VGA, QVGA or QQVGA
okano 22:1a923c255be6 115 */
okano 29:4432c5282c7b 116 int init( CameraResolution res = QCIF );
okano 19:1d07d6d762a9 117
okano 20:fa4a54e25fc4 118 /** Color bar ON/OFF
okano 20:fa4a54e25fc4 119 *
okano 20:fa4a54e25fc4 120 * Set colorbar ON/OFF
okano 20:fa4a54e25fc4 121 *
okano 22:1a923c255be6 122 * @param sw turn-ON or -OFF colorbar : ON or OFF
okano 20:fa4a54e25fc4 123 */
okano 20:fa4a54e25fc4 124 void colorbar( SwitchState sw );
okano 20:fa4a54e25fc4 125
okano 19:1d07d6d762a9 126 /** Get holizontal size
okano 19:1d07d6d762a9 127 *
okano 19:1d07d6d762a9 128 * Returns image horizontal size (pixels)
okano 19:1d07d6d762a9 129 *
okano 19:1d07d6d762a9 130 * @return holizontal size
okano 19:1d07d6d762a9 131 */
okano 19:1d07d6d762a9 132 int horizontal_size( void );
okano 19:1d07d6d762a9 133
okano 19:1d07d6d762a9 134 /** Get vertical size
okano 19:1d07d6d762a9 135 *
okano 19:1d07d6d762a9 136 * Returns image vertical size (pixels)
okano 19:1d07d6d762a9 137 *
okano 19:1d07d6d762a9 138 * @return vertical size
okano 19:1d07d6d762a9 139 */
okano 19:1d07d6d762a9 140 int vertical_size( void );
okano 4:cb0ef3fd89c9 141
okano 8:23d14d5254d2 142 /** Check camera availability
okano 8:23d14d5254d2 143 *
okano 8:23d14d5254d2 144 * Returns last I2C access result
okano 8:23d14d5254d2 145 * This returns non-zero value id the camera initialization failed
okano 8:23d14d5254d2 146 *
okano 13:210f4bbd0cd6 147 * This function takes about 2 seconds because there is 99 times I2C access with 20ms interval.
okano 11:61a025e8ab68 148 *
okano 11:61a025e8ab68 149 * @return error code in init function (I2C API return value)
okano 8:23d14d5254d2 150 */
okano 8:23d14d5254d2 151 int ready( void );
okano 8:23d14d5254d2 152
okano 4:cb0ef3fd89c9 153 /** Open transfer
okano 4:cb0ef3fd89c9 154 *
okano 13:210f4bbd0cd6 155 * Let the MARY_CAMERA get ready to transfer the data.
okano 13:210f4bbd0cd6 156 * When this function is called, the camera will stop updating buffer at end of frame.
okano 8:23d14d5254d2 157 *
okano 8:23d14d5254d2 158 * @return error code in init function (I2C API return value)
okano 4:cb0ef3fd89c9 159 */
okano 0:f4584dba3bac 160 void open_transfer( void );
okano 4:cb0ef3fd89c9 161
okano 4:cb0ef3fd89c9 162 /** Close transfer
okano 4:cb0ef3fd89c9 163 *
okano 13:210f4bbd0cd6 164 * Letting the MARY_CAMERA to know the data transfer done.
okano 4:cb0ef3fd89c9 165 * This function should be called when the data transfer done to resume the buffer update by camera
okano 4:cb0ef3fd89c9 166 */
okano 0:f4584dba3bac 167 void close_transfer( void );
okano 13:210f4bbd0cd6 168
okano 4:cb0ef3fd89c9 169 /** Transfer a line
okano 4:cb0ef3fd89c9 170 *
okano 4:cb0ef3fd89c9 171 * Reads 1 line data from MARY_CAMERA
okano 4:cb0ef3fd89c9 172 * This function should be called when the data transfer done to resume the buffer update by camera
okano 13:210f4bbd0cd6 173 *
okano 4:cb0ef3fd89c9 174 * @param *p pointer to array of short
okano 4:cb0ef3fd89c9 175 * @param line_number to select which line want to read
okano 4:cb0ef3fd89c9 176 * @param x_offset holizontal offset (from left) to start the read
okano 4:cb0ef3fd89c9 177 * @param n_of_pixels pixels to be read
okano 4:cb0ef3fd89c9 178 */
okano 0:f4584dba3bac 179 void transfer_a_line( short *p, int line_number, int x_offset, int n_of_pixels );
okano 0:f4584dba3bac 180
okano 0:f4584dba3bac 181 private:
okano 11:61a025e8ab68 182 int send_spi( char data );
okano 11:61a025e8ab68 183 void write_register( char reg, char value );
okano 11:61a025e8ab68 184 int read_register( char reg );
okano 11:61a025e8ab68 185 void set_address( int address );
okano 0:f4584dba3bac 186
okano 4:cb0ef3fd89c9 187 SPI _spi;
okano 4:cb0ef3fd89c9 188 DigitalOut _cs;
okano 4:cb0ef3fd89c9 189 DigitalOut _reset;
okano 4:cb0ef3fd89c9 190 I2C _i2c;
okano 8:23d14d5254d2 191 int _error_state;
okano 19:1d07d6d762a9 192 int _horizontal_size;
okano 19:1d07d6d762a9 193 int _vertical_size;
okano 0:f4584dba3bac 194 };
okano 0:f4584dba3bac 195
okano 0:f4584dba3bac 196 #endif // MBED_MARY_CAMERA