Test for LinkSprite Y201 JPEG camera

Dependencies:   mbed MODSERIAL Y201 mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "Y201.h"
00004 
00005 const int Y201::resetSeq       [4] = {0x56,0x00,0x26,0x00};
00006 const int Y201::resetSeqAck    [4] = {0x76,0x00,0x26,0x00};
00007 const int Y201::takePicSeq     [5] = {0x56,0x00,0x36,0x01,0x00};
00008 const int Y201::takePicSeqAck  [5] = {0x76,0x00,0x36,0x00,0x00};
00009 const int Y201::set160x120     [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x22};
00010 const int Y201::set320x240     [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x11};
00011 const int Y201::set640x480     [9] = {0x56,0x00,0x31,0x05,0x04,0x01,0x00,0x19,0x00};
00012 const int Y201::setSizeAck     [5] = {0x76,0x00,0x31,0x00,0x00};
00013 const int Y201::readFileSize   [5] = {0x56,0x00,0x34,0x01,0x00};
00014 const int Y201::readFileSizeAck[7] = {0x76,0x00,0x34,0x00,0x04,0x00,0x00};
00015 const int Y201::readFileHead   [8] = {0x56,0x00,0x32,0x0C,0x00,0x0A,0x00,0x00};
00016 const int Y201::readFileAck    [5] = {0x76,0x00,0x32,0x00,0x00};
00017 
00018 DigitalOut led1(LED1);
00019 
00020 extern "C" void HardFault_Handler() {
00021   error("Hard Fault!\n");
00022 }
00023 
00024 
00025 Serial pc(USBTX, USBRX);
00026 
00027 
00028 void test(void const*) {
00029   LocalFileSystem fs("fs");
00030   FILE *fp = fopen("/fs/picture.jpg","w");
00031   pc.baud(115200);
00032   pc.printf("RESET V^V^V^V^V^V^V^V^V^V^V RESET\r\n");
00033   
00034   // open camera
00035   Y201 camera(p28,p27);
00036   
00037   // set image size
00038   if(camera.setImageSize(Y201::e640x480)) {
00039     printf("Set image size\r\n");
00040   } else {
00041     printf("Error setting image size!\r\n");
00042   }
00043   
00044   // reset camera
00045   if(camera.reset()) {
00046     printf("Camera reset successfull\r\n");
00047   } else {
00048     printf("Error resetting camera\r\n");
00049   }
00050   
00051   // take a picture  
00052   if(camera.takePicture()) {
00053     printf("Took picture!\r\n");
00054   } else {
00055     printf("Take picture failed!\r\n");
00056   }
00057   
00058   // read file size
00059   int fileSize = 0;
00060   if(camera.readImageSize(&fileSize)) {
00061     printf("Filesize: %d\r\n",fileSize);
00062   } else {
00063     printf("Error getting file size\r\n");
00064   }
00065   
00066   NVIC_SetPriority(UART2_IRQn, 0);
00067 
00068   // IMAGE UPLOAD
00069    
00070   int bytesRead = 0;
00071   int chunkSize = 512;
00072 
00073   uint8_t *readBuffer = (uint8_t*)malloc(chunkSize*sizeof(uint8_t));
00074   while(bytesRead<fileSize) {  
00075     // read the image
00076     size_t w;
00077     if(w=camera.readImage(bytesRead,chunkSize,readBuffer)) {
00078         
00079     } else {
00080         printf("Error in file read\r\n");
00081         //Dump packet
00082         for(int i = 0; i < chunkSize; i++)
00083         {
00084           if(!(i % 16))
00085           {
00086             printf("\n");
00087           }
00088           printf("%02x ", readBuffer[i]);
00089         }
00090     }
00091     bytesRead += chunkSize;
00092     //Thread::wait(1000);
00093     printf("%d..\n",bytesRead);
00094     if(bytesRead<fileSize) {
00095         size_t w = fwrite(readBuffer,chunkSize,1,fp);
00096     } else {
00097         size_t w = fwrite(readBuffer,(chunkSize-(bytesRead-fileSize)),1,fp);
00098     }
00099     
00100     camera.trash();
00101     
00102   }
00103   
00104   printf("\r\n");
00105   fclose(fp);
00106   printf("loop exit\r\n");
00107   printf("filesize: %d\r\n",fileSize);  
00108 }
00109 
00110 void tick() {
00111   led1 = !led1;
00112 }
00113 
00114 int main() {
00115  // Ticker t;
00116  // t.attach(tick, 1);
00117 
00118   Thread testTask(test, NULL, osPriorityNormal, 1024 * 4);
00119   //Thread testTask(test);
00120   while(1){ Thread::wait(1000); }
00121 
00122   return 0;
00123 }