Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2021
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  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_2592x1944);
  116. delay(1000);
  117. #endif
  118.  
  119. //ETCG Notes -- Check if Time Lapse has been long enough and if So Take Picture
  120. if (Serial.available() && Serial.read() == 65) {
  121. //Start capture
  122. myCAM.start_capture();
  123. Serial.println(F("start capture."));
  124. total_time = millis();
  125. while (!myCAM.get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK))
  126. ;
  127. Serial.println(F("CAM Capture Done."));
  128. total_time = millis() - total_time;
  129. Serial.print(F("capture total_time used (in miliseconds):"));
  130. Serial.println(total_time, DEC);
  131. total_time = millis();
  132. read_fifo_burst(myCAM);
  133. total_time = millis() - total_time;
  134. Serial.print(F("save capture total_time used (in miliseconds):"));
  135. Serial.println(total_time, DEC);
  136. //Clear the capture done flag
  137. myCAM.clear_fifo_flag();
  138. delay(5000);
  139. } else {
  140. Serial.println("Waiting for command (A to start capture)!");
  141. delay(1000);
  142. }
  143. }
  144.  
  145. uint8_t read_fifo_burst(ArduCAM myCAM) {
  146. uint8_t currByte = 0;
  147. uint8_t prevByte = 0;
  148. uint32_t length = myCAM.read_fifo_length();
  149. static int i = 0;
  150. byte buffer[BYTE_PACKET_SIZE];
  151.  
  152. Serial.print("The FIFO length is: ");
  153. Serial.println(length, DEC);
  154.  
  155. if (length >= MAX_FIFO_SIZE) {
  156. Serial.println("Over size image.");
  157. return 0;
  158. } else if (length == 0) {
  159. Serial.println("Empty image");
  160. return 0;
  161. } else {
  162. Serial.println("Begin image processing");
  163.  
  164. myCAM.CS_LOW();
  165. myCAM.set_fifo_burst();
  166.  
  167. while (length--) {
  168. prevByte = currByte;
  169. currByte = SPI.transfer(0x00);
  170. Serial.println(currByte, HEX);
  171. if ((currByte == 0xD9) && (prevByte == 0xFF)) {
  172. // buffer[i++] = currByte;
  173. i = add_to_buffer(buffer, i, currByte);
  174. myCAM.CS_HIGH();
  175. dump_buffer(buffer, i);
  176.  
  177. Serial.println("End read");
  178. is_header = false;
  179. myCAM.CS_LOW();
  180. myCAM.set_fifo_burst();
  181. i = 0;
  182. myCAM.CS_HIGH();
  183. return 1;
  184. } else if ((currByte == 0xD8) && (prevByte == 0xFF)) {
  185. Serial.println("Found header");
  186.  
  187. is_header = true;
  188. myCAM.CS_HIGH();
  189. myCAM.CS_LOW();
  190. myCAM.set_fifo_burst();
  191. // buffer[i++] = prevByte;
  192. // buffer[i++] = currByte;
  193. i = add_to_buffer(buffer, i, prevByte);
  194. i = add_to_buffer(buffer, i, currByte);
  195. } else if (is_header) {
  196. myCAM.CS_HIGH();
  197. // buffer[i++] = currByte;
  198. i = add_to_buffer(buffer, i, currByte);
  199. myCAM.CS_LOW();
  200. myCAM.set_fifo_burst();
  201. }
  202. }
  203. return 0;
  204. }
  205. }
  206.  
  207. void dump_buffer(byte buffer[], int index) {
  208. for (int i = 0; i < index; i++) {
  209. Serial.println(buffer[i], HEX);
  210. }
  211. }
  212.  
  213. int add_to_buffer(byte buffer[], int index, byte elem) {
  214. if (index < BYTE_PACKET_SIZE) {
  215. buffer[index] = elem;
  216. return index + 1;
  217. } else {
  218. dump_buffer(buffer, BYTE_PACKET_SIZE);
  219. buffer[0] = elem;
  220. return 1;
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement