Advertisement
Guest User

Untitled

a guest
Oct 27th, 2014
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.45 KB | None | 0 0
  1. #include <avr/pgmspace.h>
  2. #include "Arduino.h"
  3.  
  4. String content = "";
  5. char character;
  6. int motorPinR = 2;
  7.  
  8. //Camera definitions
  9. #define VC0706_PROTOCOL_SIGN            0x56
  10. #define VC0706_SERIAL_NUMBER            0x00
  11. #define VC0706_COMMAND_RESET            0x26
  12. #define VC0706_COMMAND_GEN_VERSION      0x11
  13. #define VC0706_COMMAND_TV_OUT_CTRL      0x44
  14. #define VC0706_COMMAND_OSD_ADD_CHAR     0x45
  15. #define VC0706_COMMAND_DOWNSIZE_SIZE        0x53
  16. #define VC0706_COMMAND_READ_FBUF        0x32
  17. #define FBUF_CURRENT_FRAME          0
  18. #define FBUF_NEXT_FRAME             0
  19. #define VC0706_COMMAND_FBUF_CTRL        0x36
  20. #define VC0706_COMMAND_COMM_MOTION_CTRL     0x37
  21. #define VC0706_COMMAND_COMM_MOTION_DETECTED 0x39
  22. #define VC0706_COMMAND_POWER_SAVE_CTRL      0x3E
  23. #define VC0706_COMMAND_COLOR_CTRL       0x3C
  24. #define VC0706_COMMAND_MOTION_CTRL      0x42
  25. #define VC0706_COMMAND_WRITE_DATA       0x31
  26. #define VC0706_COMMAND_GET_FBUF_LEN     0x34
  27. #define READ_DATA_BLOCK_NO          56
  28. unsigned char   tx_counter;
  29. unsigned char   tx_vcbuffer[20];
  30. bool        tx_ready;
  31. bool        rx_ready;
  32. unsigned char   rx_counter;
  33. unsigned char   VC0706_rx_buffer[80];
  34. uint32_t    frame_length=0;
  35. uint32_t    vc_frame_address =0;
  36. uint32_t    last_data_length=0;
  37.  
  38.  
  39. void setup()
  40. {
  41.   Serial.begin(115200);
  42.   //VC0706_compression_ratio(66);
  43.   //delay(100);
  44.   //pinMode(motorPinR,OUTPUT);
  45. }
  46. void loop()
  47. {
  48.   if(Serial.available() > 0) Serial.read();
  49.   {
  50.     Serial.println("Welcome to Onix_One");
  51.     delay(1000);
  52.     Serial.println("");
  53.     capture_photo();
  54.   }
  55. }
  56.  
  57. void VC0706_read_frame_buffer(unsigned long buffer_address, unsigned long buffer_length)
  58. {
  59.  
  60.   tx_vcbuffer[0]=VC0706_PROTOCOL_SIGN;
  61.   tx_vcbuffer[1]=VC0706_SERIAL_NUMBER;
  62.   tx_vcbuffer[2]=VC0706_COMMAND_READ_FBUF;
  63.   tx_vcbuffer[3]=0x0c;
  64.   tx_vcbuffer[4]=FBUF_CURRENT_FRAME;
  65.   tx_vcbuffer[5]=0x0a;      // 0x0a=data transfer by MCU mode; 0x0f=data transfer by SPI interface
  66.   tx_vcbuffer[6]=buffer_address>>24;            //starting address
  67.   tx_vcbuffer[7]=buffer_address>>16;           
  68.   tx_vcbuffer[8]=buffer_address>>8;        
  69.   tx_vcbuffer[9]=buffer_address&0x0ff;         
  70.  
  71.   tx_vcbuffer[10]=buffer_length>>24;        // data length
  72.   tx_vcbuffer[11]=buffer_length>>16;
  73.   tx_vcbuffer[12]=buffer_length>>8;    
  74.   tx_vcbuffer[13]=buffer_length&0x0ff;
  75.   tx_vcbuffer[14]=0x00;     // delay time
  76.   tx_vcbuffer[15]=0x0a;
  77.  
  78.  
  79.   tx_counter=16;
  80.  
  81.   buffer_send();
  82. }
  83.  
  84. void VC0706_frame_control(byte frame_control)
  85. {
  86.   if(frame_control>3)frame_control=3;
  87.   tx_vcbuffer[0]=VC0706_PROTOCOL_SIGN;
  88.   tx_vcbuffer[1]=VC0706_SERIAL_NUMBER;
  89.   tx_vcbuffer[2]=VC0706_COMMAND_FBUF_CTRL;
  90.   tx_vcbuffer[3]=0x01;
  91.   tx_vcbuffer[4]=frame_control;
  92.   tx_counter=5;
  93.  
  94.   buffer_send();
  95. }
  96.  
  97. void VC0706_get_framebuffer_length(byte fbuf_type)
  98. {
  99.   if(fbuf_type>1)fbuf_type=1;
  100.   tx_vcbuffer[0]=VC0706_PROTOCOL_SIGN;
  101.   tx_vcbuffer[1]=VC0706_SERIAL_NUMBER;
  102.   tx_vcbuffer[2]=VC0706_COMMAND_GET_FBUF_LEN;
  103.   tx_vcbuffer[3]=0x01;
  104.   tx_vcbuffer[4]=fbuf_type;
  105.   tx_counter=5;
  106.  
  107.   buffer_send();
  108. }
  109.  
  110. void VC0706_compression_ratio(int ratio)
  111. {
  112.   if(ratio>63)ratio=63;
  113.   if(ratio<13)ratio=13;
  114.   int vc_comp_ratio=(ratio-13)*4+53;
  115.   tx_vcbuffer[0]=VC0706_PROTOCOL_SIGN;
  116.   tx_vcbuffer[1]=VC0706_SERIAL_NUMBER;
  117.   tx_vcbuffer[2]=VC0706_COMMAND_WRITE_DATA;
  118.   tx_vcbuffer[3]=0x05;
  119.   tx_vcbuffer[4]=01;        //chip register
  120.   tx_vcbuffer[5]=0x01;  //data num ready to write
  121.   tx_vcbuffer[6]=0x12;  //register address
  122.   tx_vcbuffer[7]=0x04;
  123.   tx_vcbuffer[8]=vc_comp_ratio; //data
  124.  
  125.   tx_counter=8;
  126.  
  127.   buffer_send();
  128. }
  129.  
  130. void buffer_send()
  131. {
  132.   int i=0;
  133.  
  134.   for (i=0;i<tx_counter;i++)
  135.     Serial.write(tx_vcbuffer[i]);
  136.  
  137.   tx_ready=true;
  138. }
  139.  
  140.  
  141. void buffer_read()
  142. {
  143.   bool validity=true;
  144.  
  145.   if (rx_ready)         // if something unread in buffer, just quit
  146.     return;
  147.  
  148.   rx_counter=0;
  149.   VC0706_rx_buffer[0]=0;
  150.   while (Serial.available() > 0)
  151.   {
  152.     VC0706_rx_buffer[rx_counter++]= Serial.read();
  153.     //delay(1);
  154.   }
  155.  
  156.   if (VC0706_rx_buffer[0]!=0x76)
  157.     validity=false;
  158.   if (VC0706_rx_buffer[1]!=VC0706_SERIAL_NUMBER)
  159.     validity=false;
  160.  
  161.   if (validity) rx_ready=true;
  162.  
  163.  
  164. }
  165.  
  166.  
  167. void capture_photo(){  
  168.  
  169.   VC0706_compression_ratio(63);
  170.   delay(100);
  171.  
  172.   VC0706_frame_control(3);
  173.   delay(10);
  174.  
  175.   VC0706_frame_control(0);
  176.   delay(10);
  177.   rx_ready=false;
  178.   rx_counter=0;
  179.  
  180.   Serial.end();         // clear all rx buffer
  181.   delay(5);
  182.  
  183.   Serial.begin(115200);
  184.  
  185.   //get frame buffer length
  186.   VC0706_get_framebuffer_length(0);
  187.   delay(10);
  188.   buffer_read();
  189.  
  190.   //while(1){};
  191.  
  192.   // store frame buffer length for coming reading
  193.   frame_length=(VC0706_rx_buffer[5]<<8)+VC0706_rx_buffer[6];
  194.   frame_length=frame_length<<16;
  195.   frame_length=frame_length+(0x0ff00&(VC0706_rx_buffer[7]<<8))+VC0706_rx_buffer[8];
  196.  
  197.   vc_frame_address =READ_DATA_BLOCK_NO;
  198.  
  199.   while(vc_frame_address<frame_length){
  200.     VC0706_read_frame_buffer(vc_frame_address-READ_DATA_BLOCK_NO, READ_DATA_BLOCK_NO);
  201.     delay(9);
  202.  
  203.     //get the data with length=READ_DATA_BLOCK_NObytes
  204.     rx_ready=false;
  205.     rx_counter=0;
  206.     buffer_read();
  207.  
  208.     for(int i = 5; i < 75;i++)
  209.     Serial.print(VC0706_rx_buffer[i],HEX);
  210.  
  211.     //read next READ_DATA_BLOCK_NO bytes from frame buffer
  212.     vc_frame_address=vc_frame_address+READ_DATA_BLOCK_NO;
  213.  
  214.   }
  215.  
  216.   // get the last data
  217.   vc_frame_address=vc_frame_address-READ_DATA_BLOCK_NO;
  218.  
  219.   last_data_length=frame_length-vc_frame_address;
  220.  
  221.  
  222.   VC0706_read_frame_buffer(vc_frame_address,last_data_length);
  223.   delay(9);
  224.   //get the data
  225.   rx_ready=false;
  226.   rx_counter=0;
  227.   buffer_read();
  228.   for(int i = 5; i < 75;i++)
  229.   Serial.print(VC0706_rx_buffer[i],HEX);
  230.  
  231.   Serial.flush();
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement