Test for LinkSprite Y201 JPEG camera
Dependencies: mbed MODSERIAL Y201 mbed-rtos
Diff: main.cpp
- Revision:
- 0:d927d630369a
- Child:
- 1:60a7ddc83644
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Jun 29 19:29:17 2012 +0000 @@ -0,0 +1,111 @@ +#include "mbed.h" +#include "rtos.h" +#include "Y201.h" + +const int Y201::resetSeq [4] = {0x56,0x00,0x26,0x00}; +const int Y201::resetSeqAck [4] = {0x76,0x00,0x26,0x00}; +const int Y201::takePicSeq [5] = {0x56,0x00,0x36,0x01,0x00}; +const int Y201::takePicSeqAck [5] = {0x76,0x00,0x36,0x00,0x00}; +const int Y201::set160x120 [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x22}; +const int Y201::set320x240 [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x11}; +const int Y201::set640x480 [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x00}; +const int Y201::setSizeAck [5] = {0x76,0x00,0x31,0x00,0x00}; +const int Y201::readFileSize [5] = {0x56,0x00,0x34,0x01,0x00}; +const int Y201::readFileSizeAck[7] = {0x76,0x00,0x34,0x00,0x04,0x00,0x00}; +const int Y201::readFileHead [8] = {0x56,0x00,0x32,0x0C,0x00,0x0A,0x00,0x00}; +const int Y201::readFileAck [5] = {0x76,0x00,0x32,0x00,0x00}; + +DigitalOut led1(LED1); + +extern "C" void HardFault_Handler() { + error("Hard Fault!\n"); +} + +LocalFileSystem fs("fs"); +FILE *fp = fopen("picture.jpg","w"); +Serial pc(USBTX, USBRX); + + +void test(void const*) { + pc.baud(115200); + pc.printf("RESET V^V^V^V^V^V^V^V^V^V^V RESET\r\n"); + + // open camera + Y201 camera(p13,p14); + + // set image size + if(camera.setImageSize(Y201::e640x480)) { + printf("Set image size\r\n"); + } else { + printf("Error setting image size!\r\n"); + } + + // reset camera + if(camera.reset()) { + printf("Camera reset successfull\r\n"); + } else { + printf("Error resetting camera\r\n"); + } + + // take a picture + if(camera.takePicture()) { + printf("Took picture!\r\n"); + } else { + printf("Take picture failed!\r\n"); + } + + // read file size + int fileSize = 0; + if(camera.readImageSize(&fileSize)) { + printf("Filesize: %d\r\n",fileSize); + } else { + printf("Error getting file size\r\n"); + } + + + // IMAGE UPLOAD + + int bytesRead = 0; + int chunkSize = 256; + + uint8_t *readBuffer = (uint8_t*)malloc(chunkSize*sizeof(uint8_t)); + while(bytesRead<fileSize) { + // read the image + size_t w; + if(w=camera.readImage(bytesRead,chunkSize,readBuffer)) { + + } else { + printf("Error in file read\r\n"); + } + bytesRead += chunkSize; + printf("%d..",bytesRead,w); + if(bytesRead<fileSize) { + //size_t w = fwrite(readBuffer,chunkSize,1,fp); + } else { + //size_t w = fwrite(readBuffer,(chunkSize-(bytesRead-fileSize)),1,fp); + } + + camera.trash(); + + } + + printf("\r\n"); + fclose(fp); + printf("loop exit\r\n"); + printf("filesize: %d\r\n",fileSize); +} + +void tick() { + led1 = !led1; +} + +int main() { + Ticker t; + t.attach(tick, 1); + + Thread testTask(test, NULL, osPriorityNormal, 1024 * 4); + //Thread testTask(test); + while(1); + + return 0; +}