Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MARY_CAMERA.h@25:8f6c2a094544, 2014-03-15 (annotated)
- Committer:
- okano
- Date:
- Sat Mar 15 12:00:49 2014 +0000
- Revision:
- 25:8f6c2a094544
- Parent:
- 22:1a923c255be6
test code
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:cb0ef3fd89c9 | 113 | */ |
okano | 22:1a923c255be6 | 114 | int init( void ); |
okano | 22:1a923c255be6 | 115 | |
okano | 22:1a923c255be6 | 116 | /** Resolution |
okano | 22:1a923c255be6 | 117 | * |
okano | 22:1a923c255be6 | 118 | * Change camera resolution |
okano | 22:1a923c255be6 | 119 | * |
okano | 22:1a923c255be6 | 120 | * @param res select camera resolution : QCIF(default), VGA, QVGA or QQVGA |
okano | 22:1a923c255be6 | 121 | */ |
okano | 22:1a923c255be6 | 122 | void resolution( CameraResolution res ); |
okano | 19:1d07d6d762a9 | 123 | |
okano | 20:fa4a54e25fc4 | 124 | /** Color bar ON/OFF |
okano | 20:fa4a54e25fc4 | 125 | * |
okano | 20:fa4a54e25fc4 | 126 | * Set colorbar ON/OFF |
okano | 20:fa4a54e25fc4 | 127 | * |
okano | 22:1a923c255be6 | 128 | * @param sw turn-ON or -OFF colorbar : ON or OFF |
okano | 20:fa4a54e25fc4 | 129 | */ |
okano | 20:fa4a54e25fc4 | 130 | void colorbar( SwitchState sw ); |
okano | 20:fa4a54e25fc4 | 131 | |
okano | 19:1d07d6d762a9 | 132 | /** Get holizontal size |
okano | 19:1d07d6d762a9 | 133 | * |
okano | 19:1d07d6d762a9 | 134 | * Returns image horizontal size (pixels) |
okano | 19:1d07d6d762a9 | 135 | * |
okano | 19:1d07d6d762a9 | 136 | * @return holizontal size |
okano | 19:1d07d6d762a9 | 137 | */ |
okano | 19:1d07d6d762a9 | 138 | int horizontal_size( void ); |
okano | 19:1d07d6d762a9 | 139 | |
okano | 19:1d07d6d762a9 | 140 | /** Get vertical size |
okano | 19:1d07d6d762a9 | 141 | * |
okano | 19:1d07d6d762a9 | 142 | * Returns image vertical size (pixels) |
okano | 19:1d07d6d762a9 | 143 | * |
okano | 19:1d07d6d762a9 | 144 | * @return vertical size |
okano | 19:1d07d6d762a9 | 145 | */ |
okano | 19:1d07d6d762a9 | 146 | int vertical_size( void ); |
okano | 4:cb0ef3fd89c9 | 147 | |
okano | 8:23d14d5254d2 | 148 | /** Check camera availability |
okano | 8:23d14d5254d2 | 149 | * |
okano | 8:23d14d5254d2 | 150 | * Returns last I2C access result |
okano | 8:23d14d5254d2 | 151 | * This returns non-zero value id the camera initialization failed |
okano | 8:23d14d5254d2 | 152 | * |
okano | 13:210f4bbd0cd6 | 153 | * This function takes about 2 seconds because there is 99 times I2C access with 20ms interval. |
okano | 11:61a025e8ab68 | 154 | * |
okano | 11:61a025e8ab68 | 155 | * @return error code in init function (I2C API return value) |
okano | 8:23d14d5254d2 | 156 | */ |
okano | 8:23d14d5254d2 | 157 | int ready( void ); |
okano | 8:23d14d5254d2 | 158 | |
okano | 4:cb0ef3fd89c9 | 159 | /** Open transfer |
okano | 4:cb0ef3fd89c9 | 160 | * |
okano | 13:210f4bbd0cd6 | 161 | * Let the MARY_CAMERA get ready to transfer the data. |
okano | 13:210f4bbd0cd6 | 162 | * When this function is called, the camera will stop updating buffer at end of frame. |
okano | 8:23d14d5254d2 | 163 | * |
okano | 8:23d14d5254d2 | 164 | * @return error code in init function (I2C API return value) |
okano | 4:cb0ef3fd89c9 | 165 | */ |
okano | 0:f4584dba3bac | 166 | void open_transfer( void ); |
okano | 4:cb0ef3fd89c9 | 167 | |
okano | 4:cb0ef3fd89c9 | 168 | /** Close transfer |
okano | 4:cb0ef3fd89c9 | 169 | * |
okano | 13:210f4bbd0cd6 | 170 | * Letting the MARY_CAMERA to know the data transfer done. |
okano | 4:cb0ef3fd89c9 | 171 | * This function should be called when the data transfer done to resume the buffer update by camera |
okano | 4:cb0ef3fd89c9 | 172 | */ |
okano | 0:f4584dba3bac | 173 | void close_transfer( void ); |
okano | 13:210f4bbd0cd6 | 174 | |
okano | 4:cb0ef3fd89c9 | 175 | /** Transfer a line |
okano | 4:cb0ef3fd89c9 | 176 | * |
okano | 4:cb0ef3fd89c9 | 177 | * Reads 1 line data from MARY_CAMERA |
okano | 4:cb0ef3fd89c9 | 178 | * This function should be called when the data transfer done to resume the buffer update by camera |
okano | 13:210f4bbd0cd6 | 179 | * |
okano | 4:cb0ef3fd89c9 | 180 | * @param *p pointer to array of short |
okano | 4:cb0ef3fd89c9 | 181 | * @param line_number to select which line want to read |
okano | 4:cb0ef3fd89c9 | 182 | * @param x_offset holizontal offset (from left) to start the read |
okano | 4:cb0ef3fd89c9 | 183 | * @param n_of_pixels pixels to be read |
okano | 4:cb0ef3fd89c9 | 184 | */ |
okano | 0:f4584dba3bac | 185 | void transfer_a_line( short *p, int line_number, int x_offset, int n_of_pixels ); |
okano | 0:f4584dba3bac | 186 | |
okano | 25:8f6c2a094544 | 187 | void test0( int v ); |
okano | 25:8f6c2a094544 | 188 | |
okano | 0:f4584dba3bac | 189 | private: |
okano | 11:61a025e8ab68 | 190 | int send_spi( char data ); |
okano | 11:61a025e8ab68 | 191 | void write_register( char reg, char value ); |
okano | 11:61a025e8ab68 | 192 | int read_register( char reg ); |
okano | 11:61a025e8ab68 | 193 | void set_address( int address ); |
okano | 0:f4584dba3bac | 194 | |
okano | 4:cb0ef3fd89c9 | 195 | SPI _spi; |
okano | 4:cb0ef3fd89c9 | 196 | DigitalOut _cs; |
okano | 4:cb0ef3fd89c9 | 197 | DigitalOut _reset; |
okano | 4:cb0ef3fd89c9 | 198 | I2C _i2c; |
okano | 8:23d14d5254d2 | 199 | int _error_state; |
okano | 19:1d07d6d762a9 | 200 | int _horizontal_size; |
okano | 19:1d07d6d762a9 | 201 | int _vertical_size; |
okano | 0:f4584dba3bac | 202 | }; |
okano | 0:f4584dba3bac | 203 | |
okano | 0:f4584dba3bac | 204 | #endif // MBED_MARY_CAMERA |