Test for LinkSprite Y201 JPEG camera
Dependencies: mbed MODSERIAL Y201 mbed-rtos
main.cpp@1:60a7ddc83644, 2012-06-29 (annotated)
- Committer:
- ashleymills
- Date:
- Fri Jun 29 20:38:07 2012 +0000
- Revision:
- 1:60a7ddc83644
- Parent:
- 0:d927d630369a
- Child:
- 2:cf38a12918af
This works, and saves a picture to LocalFileSystem
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:d927d630369a | 1 | #include "mbed.h" |
ashleymills | 0:d927d630369a | 2 | #include "rtos.h" |
ashleymills | 0:d927d630369a | 3 | #include "Y201.h" |
ashleymills | 0:d927d630369a | 4 | |
ashleymills | 0:d927d630369a | 5 | const int Y201::resetSeq [4] = {0x56,0x00,0x26,0x00}; |
ashleymills | 0:d927d630369a | 6 | const int Y201::resetSeqAck [4] = {0x76,0x00,0x26,0x00}; |
ashleymills | 0:d927d630369a | 7 | const int Y201::takePicSeq [5] = {0x56,0x00,0x36,0x01,0x00}; |
ashleymills | 0:d927d630369a | 8 | const int Y201::takePicSeqAck [5] = {0x76,0x00,0x36,0x00,0x00}; |
ashleymills | 0:d927d630369a | 9 | const int Y201::set160x120 [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x22}; |
ashleymills | 0:d927d630369a | 10 | const int Y201::set320x240 [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x11}; |
ashleymills | 0:d927d630369a | 11 | const int Y201::set640x480 [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x00}; |
ashleymills | 0:d927d630369a | 12 | const int Y201::setSizeAck [5] = {0x76,0x00,0x31,0x00,0x00}; |
ashleymills | 0:d927d630369a | 13 | const int Y201::readFileSize [5] = {0x56,0x00,0x34,0x01,0x00}; |
ashleymills | 0:d927d630369a | 14 | const int Y201::readFileSizeAck[7] = {0x76,0x00,0x34,0x00,0x04,0x00,0x00}; |
ashleymills | 0:d927d630369a | 15 | const int Y201::readFileHead [8] = {0x56,0x00,0x32,0x0C,0x00,0x0A,0x00,0x00}; |
ashleymills | 0:d927d630369a | 16 | const int Y201::readFileAck [5] = {0x76,0x00,0x32,0x00,0x00}; |
ashleymills | 0:d927d630369a | 17 | |
ashleymills | 0:d927d630369a | 18 | DigitalOut led1(LED1); |
ashleymills | 0:d927d630369a | 19 | |
ashleymills | 0:d927d630369a | 20 | extern "C" void HardFault_Handler() { |
ashleymills | 0:d927d630369a | 21 | error("Hard Fault!\n"); |
ashleymills | 0:d927d630369a | 22 | } |
ashleymills | 0:d927d630369a | 23 | |
ashleymills | 1:60a7ddc83644 | 24 | |
ashleymills | 0:d927d630369a | 25 | Serial pc(USBTX, USBRX); |
ashleymills | 0:d927d630369a | 26 | |
ashleymills | 0:d927d630369a | 27 | |
ashleymills | 0:d927d630369a | 28 | void test(void const*) { |
ashleymills | 1:60a7ddc83644 | 29 | LocalFileSystem fs("fs"); |
ashleymills | 1:60a7ddc83644 | 30 | FILE *fp = fopen("/fs/picture.jpg","w"); |
ashleymills | 0:d927d630369a | 31 | pc.baud(115200); |
ashleymills | 0:d927d630369a | 32 | pc.printf("RESET V^V^V^V^V^V^V^V^V^V^V RESET\r\n"); |
ashleymills | 0:d927d630369a | 33 | |
ashleymills | 0:d927d630369a | 34 | // open camera |
ashleymills | 0:d927d630369a | 35 | Y201 camera(p13,p14); |
ashleymills | 0:d927d630369a | 36 | |
ashleymills | 0:d927d630369a | 37 | // set image size |
ashleymills | 0:d927d630369a | 38 | if(camera.setImageSize(Y201::e640x480)) { |
ashleymills | 0:d927d630369a | 39 | printf("Set image size\r\n"); |
ashleymills | 0:d927d630369a | 40 | } else { |
ashleymills | 0:d927d630369a | 41 | printf("Error setting image size!\r\n"); |
ashleymills | 0:d927d630369a | 42 | } |
ashleymills | 0:d927d630369a | 43 | |
ashleymills | 0:d927d630369a | 44 | // reset camera |
ashleymills | 0:d927d630369a | 45 | if(camera.reset()) { |
ashleymills | 0:d927d630369a | 46 | printf("Camera reset successfull\r\n"); |
ashleymills | 0:d927d630369a | 47 | } else { |
ashleymills | 0:d927d630369a | 48 | printf("Error resetting camera\r\n"); |
ashleymills | 0:d927d630369a | 49 | } |
ashleymills | 0:d927d630369a | 50 | |
ashleymills | 0:d927d630369a | 51 | // take a picture |
ashleymills | 0:d927d630369a | 52 | if(camera.takePicture()) { |
ashleymills | 0:d927d630369a | 53 | printf("Took picture!\r\n"); |
ashleymills | 0:d927d630369a | 54 | } else { |
ashleymills | 0:d927d630369a | 55 | printf("Take picture failed!\r\n"); |
ashleymills | 0:d927d630369a | 56 | } |
ashleymills | 0:d927d630369a | 57 | |
ashleymills | 0:d927d630369a | 58 | // read file size |
ashleymills | 0:d927d630369a | 59 | int fileSize = 0; |
ashleymills | 0:d927d630369a | 60 | if(camera.readImageSize(&fileSize)) { |
ashleymills | 0:d927d630369a | 61 | printf("Filesize: %d\r\n",fileSize); |
ashleymills | 0:d927d630369a | 62 | } else { |
ashleymills | 0:d927d630369a | 63 | printf("Error getting file size\r\n"); |
ashleymills | 0:d927d630369a | 64 | } |
ashleymills | 0:d927d630369a | 65 | |
ashleymills | 0:d927d630369a | 66 | |
ashleymills | 0:d927d630369a | 67 | // IMAGE UPLOAD |
ashleymills | 0:d927d630369a | 68 | |
ashleymills | 0:d927d630369a | 69 | int bytesRead = 0; |
ashleymills | 0:d927d630369a | 70 | int chunkSize = 256; |
ashleymills | 0:d927d630369a | 71 | |
ashleymills | 0:d927d630369a | 72 | uint8_t *readBuffer = (uint8_t*)malloc(chunkSize*sizeof(uint8_t)); |
ashleymills | 0:d927d630369a | 73 | while(bytesRead<fileSize) { |
ashleymills | 0:d927d630369a | 74 | // read the image |
ashleymills | 0:d927d630369a | 75 | size_t w; |
ashleymills | 0:d927d630369a | 76 | if(w=camera.readImage(bytesRead,chunkSize,readBuffer)) { |
ashleymills | 0:d927d630369a | 77 | |
ashleymills | 0:d927d630369a | 78 | } else { |
ashleymills | 0:d927d630369a | 79 | printf("Error in file read\r\n"); |
ashleymills | 0:d927d630369a | 80 | } |
ashleymills | 0:d927d630369a | 81 | bytesRead += chunkSize; |
ashleymills | 0:d927d630369a | 82 | printf("%d..",bytesRead,w); |
ashleymills | 0:d927d630369a | 83 | if(bytesRead<fileSize) { |
ashleymills | 1:60a7ddc83644 | 84 | size_t w = fwrite(readBuffer,chunkSize,1,fp); |
ashleymills | 0:d927d630369a | 85 | } else { |
ashleymills | 1:60a7ddc83644 | 86 | size_t w = fwrite(readBuffer,(chunkSize-(bytesRead-fileSize)),1,fp); |
ashleymills | 0:d927d630369a | 87 | } |
ashleymills | 0:d927d630369a | 88 | |
ashleymills | 0:d927d630369a | 89 | camera.trash(); |
ashleymills | 0:d927d630369a | 90 | |
ashleymills | 0:d927d630369a | 91 | } |
ashleymills | 0:d927d630369a | 92 | |
ashleymills | 0:d927d630369a | 93 | printf("\r\n"); |
ashleymills | 0:d927d630369a | 94 | fclose(fp); |
ashleymills | 0:d927d630369a | 95 | printf("loop exit\r\n"); |
ashleymills | 0:d927d630369a | 96 | printf("filesize: %d\r\n",fileSize); |
ashleymills | 0:d927d630369a | 97 | } |
ashleymills | 0:d927d630369a | 98 | |
ashleymills | 0:d927d630369a | 99 | void tick() { |
ashleymills | 0:d927d630369a | 100 | led1 = !led1; |
ashleymills | 0:d927d630369a | 101 | } |
ashleymills | 0:d927d630369a | 102 | |
ashleymills | 0:d927d630369a | 103 | int main() { |
ashleymills | 0:d927d630369a | 104 | Ticker t; |
ashleymills | 0:d927d630369a | 105 | t.attach(tick, 1); |
ashleymills | 0:d927d630369a | 106 | |
ashleymills | 0:d927d630369a | 107 | Thread testTask(test, NULL, osPriorityNormal, 1024 * 4); |
ashleymills | 0:d927d630369a | 108 | //Thread testTask(test); |
ashleymills | 0:d927d630369a | 109 | while(1); |
ashleymills | 0:d927d630369a | 110 | |
ashleymills | 0:d927d630369a | 111 | return 0; |
ashleymills | 0:d927d630369a | 112 | } |