Extend error types and handling in image.c

webcam_dev
bob 2018-09-24 09:39:08 +10:00
rodzic ffd2035296
commit f6c633953b
4 zmienionych plików z 66 dodań i 42 usunięć

Wyświetl plik

@ -127,11 +127,19 @@ void usb_cmd_printPicture(BaseSequentialStream *chp, int argc, char *argv[])
(void)argc;
(void)argv;
// Take picture
uint32_t size_sampled = takePicture(usb_buffer, sizeof(usb_buffer), RES_QVGA, false);
/*
* Take picture.
* Status is returned in msg.
* MSG_OK = capture success.
* MSG_RESET = no cmaera found
* MSG_TIMEOUT = capture failed.
*/
uint32_t size_sampled;
msg_t msg = takePicture(usb_buffer, sizeof(usb_buffer), RES_QVGA,
&size_sampled, false);
// Transmit image via USB
if(size_sampled)
if(size_sampled && msg == MSG_OK)
{
bool start_detected = false;
for(uint32_t i=0; i<size_sampled; i++)

Wyświetl plik

@ -51,6 +51,7 @@ bool initSD(void)
TRACE_DEBUG("SD > Connect");
if(mmcConnect(&MMCD1)) {
TRACE_ERROR("SD > SD card connection error");
sdInitialized = false;
} else {
TRACE_INFO("SD > SD card connection OK");
sdInitialized = true;

Wyświetl plik

@ -1160,9 +1160,10 @@ static bool analyze_image(const uint8_t *image, uint32_t image_len) {
*
*/
uint32_t takePicture(uint8_t* buffer, uint32_t size,
resolution_t res, bool enableJpegValidation) {
uint32_t size_sampled = 0;
resolution_t res, uint32_t *size_sampled,
bool enableJpegValidation) {
//*size_sampled = 0;
msg_t result;
// Initialize mutex
if(!camera_mtx_init)
chMtxObjectInit(&camera_mtx);
@ -1176,15 +1177,8 @@ uint32_t takePicture(uint8_t* buffer, uint32_t size,
if(camInitialized || OV5640_isAvailable()) { // OV5640 available
TRACE_INFO("IMG > OV5640 found");
// Init camera
/* if(!camInitialized) {
OV5640_init();
camInitialized = true;
}*/
uint8_t cntr = 5;
bool jpegValid = false;
//bool jpegValid = false;
do {
// Switch on and init camera
if(!camInitialized) {
@ -1193,29 +1187,34 @@ uint32_t takePicture(uint8_t* buffer, uint32_t size,
}
// Sample data from pseudo DCMI through DMA into RAM
size_sampled = OV5640_Snapshot2RAM(buffer, size, res);
if(size_sampled == 0) {
// Switch off camera
*size_sampled = OV5640_Snapshot2RAM(buffer, size, res);
if(*size_sampled == 0) {
/* Failed to capture. Switch off camera. */
OV5640_deinit();
camInitialized = false;
chThdSleep(TIME_MS2I(10));
result = MSG_TIMEOUT;
continue;
}
// Validate JPEG image
if(enableJpegValidation)
{
if(enableJpegValidation) {
TRACE_INFO("CAM > Validate integrity of JPEG");
jpegValid = analyze_image(buffer, size);
TRACE_INFO("CAM > JPEG image %s", jpegValid ? "valid" : "invalid");
} else {
jpegValid = true;
bool jpegValid = analyze_image(buffer, size);
TRACE_INFO("CAM > JPEG image %s", jpegValid ? "valid"
: "invalid");
if(!jpegValid) {
result = MSG_TIMEOUT;
continue;
}
}
} while(!jpegValid && cntr--);
result = MSG_OK;
break;
} while(cntr--);
} else { // Camera not found
//camInitialized = false;
result = MSG_RESET;
TRACE_ERROR("IMG > No camera found");
}
// Switch off camera
@ -1227,7 +1226,7 @@ uint32_t takePicture(uint8_t* buffer, uint32_t size,
TRACE_INFO("IMG > Unlock camera");
chMtxUnlock(&camera_mtx);
return size_sampled;
return result;
}
/**
@ -1275,24 +1274,39 @@ THD_FUNCTION(imgThread, arg) {
continue;
}
/* Take picture. */
uint32_t size_sampled = takePicture(buffer, conf->buf_size,
conf->res, true);
/*
* Take picture.
* Status is returned in msg.
* MSG_OK = capture success. Size captured is updated.
* MSG_RESET = no camera found
* MSG_TIMEOUT = capture failed.
*/
uint32_t size_sampled;
msg_t msg = takePicture(buffer, conf->buf_size, conf->res,
&size_sampled, true);
/* Nothing captured? */
if(size_sampled == 0) {
TRACE_INFO("IMG > Encode/Transmit SSDV (camera error) ID=%d",
my_image_id);
if(!send_image_packets(noCameraFound, sizeof(noCameraFound),
conf, (uint8_t)(my_image_id))) {
TRACE_ERROR("IMG > Error in encoding dummy image %d"
" - discarded", my_image_id);
}
if(msg != MSG_OK) {
/* Return the buffer to the heap. */
chHeapFree(buffer);
/* Allow time for other threads. */
chThdSleep(TIME_MS2I(10));
/* Try again at next run time. */
time = waitForTrigger(time, conf->svc_conf.cycle);
if(msg == MSG_RESET) {
TRACE_INFO("IMG > Encode/Transmit SSDV (camera error) ID=%d",
my_image_id);
if(!send_image_packets(noCameraFound, sizeof(noCameraFound),
conf, (uint8_t)(my_image_id))) {
TRACE_ERROR("IMG > Error in encoding dummy image %d"
" - discarded", my_image_id);
}
}
/*
* Re-check again in 1 minute or at cycle time if > 1 minute.
* Don't make this short or the IO queue will be filled.
*/
/* Try again at next run time (which may be immediately). */
time = waitForTrigger(time, conf->svc_conf.cycle > TIME_S2I(60)
? conf->svc_conf.cycle
: TIME_S2I(60));
continue;
}

Wyświetl plik

@ -18,7 +18,8 @@ extern bool reject_pri;
extern bool reject_sec;
void start_image_thread(img_app_conf_t *conf, const char *name);
uint32_t takePicture(uint8_t* buffer, uint32_t size, resolution_t resolution, bool enableJpegValidation);
uint32_t takePicture(uint8_t* buffer, uint32_t size, resolution_t resolution,
uint32_t *size_sampled, bool enableJpegValidation);
extern mutex_t camera_mtx;
extern uint32_t gimage_id;