Skip to content

Instantly share code, notes, and snippets.

@emmanuel-nwogu
Created March 29, 2023 23:02
Show Gist options
  • Save emmanuel-nwogu/3b3ec6ffc38ed7f2df76b49822b9d696 to your computer and use it in GitHub Desktop.
Save emmanuel-nwogu/3b3ec6ffc38ed7f2df76b49822b9d696 to your computer and use it in GitHub Desktop.
Continuously save images taken with an OV5642 connected to a Pi 4B to a text file
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "ArduCAM.h"
#include "sccb_bus.h"
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <netdb.h>
unsigned char is_first_time_printing_to_file = 1;
void print_image_data();
unsigned int length_cam1;
int main(int argc, char *argv[])
{
pioInit();
ArduCAM_CS_init( CAM_CS1, -1, -1, -1 ); // init the cs
sccb_bus_init();
spiInit(4000000, 0); //8MHZ
Arducam_bus_detect( CAM_CS1, -1, -1, -1 );
resetFirmware( CAM_CS1, -1, -1, -1 ); //reset the firmware
ArduCAM_Init(sensor_model);
// OV5642_640x480, OV5642_1024x768, OV5642_1280x960, OV5642_1600x1200, OV5642_2048x1536, OV5642_2592x1944
unsigned char jpeg_size = OV5642_640x480;
OV5642_set_JPEG_size(jpeg_size);
while (1) {
singleCapture(CAM_CS1);
print_image_data();
sleep(1);
}
return 0;
}
void print_image_data() {
printf("Writing image data to file.\n");
// open modes: https://www.ibm.com/docs/en/zos/2.4.0?topic=functions-fopen-open-file
FILE *f = NULL;
char filename[] = "image.txt";
if (is_first_time_printing_to_file == 1) {
f = fopen(filename, "wb");
is_first_time_printing_to_file = 0;
} else {
f = fopen(filename, "ab");
}
size_t written = fwrite(readbuf, sizeof(unsigned char), length * sizeof(unsigned char), f);
if (written == 0) {
printf("Error during writing to file !\n");
} else {
printf("Successful write!\n");
}
fclose(f);
}
@emmanuel-nwogu
Copy link
Author

I didn't comment the full list of resolutions at line 31 but they're easy to find lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment