Hello World example program for the OV528 Camera Module

Dependencies:   CameraOV528

main.cpp

Committer:
Jenny Plunkett
Date:
2017-12-19
Revision:
7:fcf88b299a5c
Parent:
6:9f5ecac1118f

File content as of revision 7:fcf88b299a5c:

#include "mbed.h"
#include "CameraOV528.h"
#include "FATFileSystem.h"
#include "SDBlockDevice.h"
#include <stdio.h>

CameraOV528 camera(D1, D0); // DX, RX
SDBlockDevice bd(PTE3, PTE1, PTE2, PTE4); // MOSI, MISO, SCLK, CS
FATFileSystem fs("fs");
EventQueue queue(32 * EVENTS_EVENT_SIZE);
Thread t;
DigitalOut green_led(LED_GREEN);
DigitalOut red_led(LED_RED);
InterruptIn btn(SW2);
int picture_count;

int format_fs(){
    int error = 0;
    printf("Welcome to the filesystem example.\r\n"
           "Formatting a FAT, RAM-backed filesystem. ");
    error = FATFileSystem::format(&bd);
    if (error)
      printf("Failure. %d\r\n", error);
    else
      printf("done.\r\n");
    return error;
}

int mount_fs() {
    printf("Mounting the filesystem on \"/fs\". ");
    int error = fs.mount(&bd);
    if (error)
      printf("Failure. %d\r\n", error);
    else
      printf("done.\r\n");
    return error;
}

void init_fs() {
  if (mount_fs()) {
      format_fs();
      mount_fs();
  }
}

void swap_led() {
    green_led = !green_led;
    red_led = !red_led;
}

void take_and_store_photo() {
    // Change LED to Red (for take_and_store_photo duration)
    swap_led();

    // Take and store the picture
    if(camera.take_picture() == 0) {
        printf("Picture taken.\r\n");

        uint32_t size = camera.get_picture_size();
        uint8_t* data_buff = (uint8_t*)malloc(size);
        uint32_t bytes_read = camera.read_picture_data(data_buff, size);
        char filename[30];
        sprintf(filename, "/fs/img%d.jpg", picture_count++);
        printf("Opening a new file, %s.\r\n", filename);

        FILE* fd = fopen(filename, "wb");
        fwrite(data_buff, sizeof(uint8_t), bytes_read, fd);
        fclose(fd);

        printf("Picture saved.\r\n\n");

        free(data_buff);
    }
    else {
        printf("Failed to take photo.\r\n\n");
    }

    // Change LED to Green
    swap_led();
}

void rise_handler(void) {
    // Add the take_and_store_photo event to the queue
    queue.call(take_and_store_photo);
}

int main() {
    // Initialize LED to Green
    red_led = !red_led;

    // Initialize picture count to zero
    picture_count = 0;

    // Initialize FATFileSystem and Camera
    init_fs();
    camera.powerup();

    // Start the event queue's dispatch thread
    t.start(callback(&queue, &EventQueue::dispatch_forever));

    // On button press (rise), execute rise_handler in the context of thread 't'
    btn.rise(queue.event(rise_handler));
}