Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2021
165
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Modification of Example: ArduCAM_Mini_5MP_Plus_Multi_Capture2SD
  2. #include "memorysaver.h"
  3. #include <ArduCAM.h>
  4. #include <SPI.h>
  5. #include <Wire.h>
  6. //This demo can only work on OV5640_MINI_5MP_PLUS or OV5642_MINI_5MP_PLUS platform.
  7. #if !(defined(OV5640_MINI_5MP_PLUS) || defined(OV5642_MINI_5MP_PLUS))
  8. #error Please select the hardware platform and camera module in the ../libraries/ArduCAM/memorysaver.h file
  9. #endif
  10.  
  11. #define FRAMES_NUM 0x00
  12. // set pin 7 as the slave select for the digital port for ArduCam:
  13. const int CS = 7;
  14. #define BYTE_PACKET_SIZE 256
  15.  
  16. bool is_header = false;
  17. int total_time = 0;
  18. #if defined(OV5640_MINI_5MP_PLUS)
  19. ArduCAM myCAM(OV5640, CS);
  20. #else
  21. ArduCAM myCAM(OV5642, CS);
  22. #endif
  23.  
  24. uint8_t read_fifo_burst(ArduCAM myCAM);
  25.  
  26. void dump_buffer(byte buffer[], int index);
  27.  
  28. int add_to_buffer(byte buffer[], int index, byte elem);
  29.  
  30. void setup() {
  31.   // put your setup code here, to run once:
  32.   uint8_t vid, pid;
  33.   uint8_t temp;
  34. #if defined(__SAM3X8E__)
  35.   Wire1.begin();
  36. #else
  37.   Wire.begin();
  38. #endif
  39.   Serial.begin(115200);
  40.   Serial.println(F("ArduCAM Start!"));
  41.  
  42.   // set the CS as an output:
  43.   pinMode(CS, OUTPUT);
  44.   digitalWrite(CS, HIGH);
  45.  
  46.   // initialize SPI:
  47.   SPI.begin();
  48.   //Reset the CPLD
  49.   myCAM.write_reg(0x07, 0x80);
  50.   delay(100);
  51.   myCAM.write_reg(0x07, 0x00);
  52.   delay(100);
  53.  
  54.   while (1) {
  55.     //Check if the ArduCAM SPI bus is OK
  56.     myCAM.write_reg(ARDUCHIP_TEST1, 0x55);
  57.     temp = myCAM.read_reg(ARDUCHIP_TEST1);
  58.     if (temp != 0x55) {
  59.       Serial.println(F("SPI interface Error!"));
  60.       delay(1000);
  61.       continue;
  62.     } else {
  63.       Serial.println(F("SPI interface OK."));
  64.       break;
  65.     }
  66.   }
  67. #if defined(OV5640_MINI_5MP_PLUS)
  68.   while (1) {
  69.     //Check if the camera module type is OV5640
  70.     myCAM.rdSensorReg16_8(OV5640_CHIPID_HIGH, &vid);
  71.     myCAM.rdSensorReg16_8(OV5640_CHIPID_LOW, &pid);
  72.     if ((vid != 0x56) || (pid != 0x40)) {
  73.       Serial.println(F("Can't find OV5640 module!"));
  74.       delay(1000);
  75.       continue;
  76.     } else {
  77.       Serial.println(F("OV5640 detected."));
  78.       break;
  79.     }
  80.   }
  81. #else
  82.   while (1) {
  83.     //Check if the camera module type is OV5642
  84.     myCAM.rdSensorReg16_8(OV5642_CHIPID_HIGH, &vid);
  85.     myCAM.rdSensorReg16_8(OV5642_CHIPID_LOW, &pid);
  86.     if ((vid != 0x56) || (pid != 0x42)) {
  87.       Serial.println(F("Can't find OV5642 module!"));
  88.       delay(1000);
  89.       continue;
  90.     } else {
  91.       Serial.println(F("OV5642 detected."));
  92.       break;
  93.     }
  94.   }
  95. #endif
  96.   //Change to JPEG capture mode and initialize the OV5640 module
  97.   myCAM.set_format(JPEG);
  98.   myCAM.InitCAM();
  99.   myCAM.set_bit(ARDUCHIP_TIM, VSYNC_LEVEL_MASK);
  100.   myCAM.clear_fifo_flag();
  101.   myCAM.write_reg(ARDUCHIP_FRAMES, FRAMES_NUM);
  102. }
  103.  
  104. void loop() {
  105.  
  106.   // put your main code here, to run repeatedly:
  107.   myCAM.flush_fifo();
  108.   myCAM.clear_fifo_flag();
  109.  
  110.   //ETCG Notes -- Change Resolution in set_JPEG_size
  111. #if defined(OV5640_MINI_5MP_PLUS)
  112.   myCAM.OV5640_set_JPEG_size(OV5640_2592x1944);
  113.   delay(1000);
  114. #else
  115.   myCAM.OV5642_set_JPEG_size(OV5642_320x240);
  116.   delay(1000);
  117. #endif
  118.   if (Serial.available() && Serial.read() == 65) {
  119.     //Start capture
  120.     myCAM.start_capture();
  121.     Serial.println(F("start capture."));
  122.     total_time = millis();
  123.     while (!myCAM.get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK))
  124.       ;
  125.     Serial.println(F("CAM Capture Done."));
  126.     total_time = millis() - total_time;
  127.     Serial.print(F("capture total_time used (in miliseconds):"));
  128.     Serial.println(total_time, DEC);
  129.     total_time = millis();
  130.     read_fifo_burst(myCAM);
  131.     total_time = millis() - total_time;
  132.     Serial.print(F("save capture total_time used (in miliseconds):"));
  133.     Serial.println(total_time, DEC);
  134.     //Clear the capture done flag
  135.     myCAM.clear_fifo_flag();
  136.     delay(5000);
  137.   } else {
  138.     Serial.println("Waiting for command (A to start capture)!");
  139.     delay(1000);
  140.   }
  141. }
  142.  
  143. uint8_t read_fifo_burst(ArduCAM myCAM) {
  144.   uint8_t currByte = 0;
  145.   uint8_t prevByte = 0;
  146.   uint32_t length = myCAM.read_fifo_length();
  147.   static int i = 0;
  148.   byte buffer[BYTE_PACKET_SIZE];
  149.  
  150.   Serial.print("The FIFO length is: ");
  151.   Serial.println(length, DEC);
  152.  
  153.   if (length >= MAX_FIFO_SIZE) {
  154.     Serial.println("Over size image.");
  155.     return 0;
  156.   } else if (length == 0) {
  157.     Serial.println("Empty image");
  158.     return 0;
  159.   } else {
  160.     Serial.println("Begin image processing");
  161.  
  162.     myCAM.CS_LOW();
  163.     myCAM.set_fifo_burst();
  164.  
  165.     while (length--) {
  166.       prevByte = currByte;
  167.       currByte = SPI.transfer(0x00);
  168.       Serial.println(currByte, HEX);      
  169.       if ((currByte == 0xD9) && (prevByte == 0xFF)) {
  170.         // buffer[i++] = currByte;
  171.         i = add_to_buffer(buffer, i, currByte);
  172.         myCAM.CS_HIGH();
  173.         dump_buffer(buffer, i);
  174.  
  175.         Serial.println("End read");
  176.         is_header = false;
  177.         myCAM.CS_LOW();
  178.         myCAM.set_fifo_burst();
  179.         i = 0;
  180.         myCAM.CS_HIGH();
  181.         return 1;
  182.       } else if ((currByte == 0xD8) && (prevByte == 0xFF)) {
  183.         Serial.println("Found header");
  184.  
  185.         is_header = true;
  186.         myCAM.CS_HIGH();
  187.         myCAM.CS_LOW();
  188.         myCAM.set_fifo_burst();
  189.         // buffer[i++] = prevByte;
  190.         // buffer[i++] = currByte;
  191.         i = add_to_buffer(buffer, i, prevByte);
  192.         i = add_to_buffer(buffer, i, currByte);
  193.       } else if (is_header) {
  194.         myCAM.CS_HIGH();
  195.         // buffer[i++] = currByte;
  196.         i = add_to_buffer(buffer, i, currByte);
  197.         myCAM.CS_LOW();
  198.         myCAM.set_fifo_burst();
  199.       }
  200.     }
  201.     return 0;
  202.   }
  203. }
  204.  
  205. void dump_buffer(byte buffer[], int index) {
  206.   for (int i = 0; i < index; i++) {
  207.     Serial.println(buffer[i], HEX);
  208.   }
  209. }
  210.  
  211. int add_to_buffer(byte buffer[], int index, byte elem) {
  212.   if (index < BYTE_PACKET_SIZE) {
  213.     buffer[index] = elem;
  214.     return index + 1;
  215.   } else {
  216.     dump_buffer(buffer, BYTE_PACKET_SIZE);
  217.     buffer[0] = elem;
  218.     return 1;
  219.   }
  220. }
Advertisement
RAW Paste Data Copied
Advertisement