Hello World example program for the OV528 Camera Module

Dependencies:   CameraOV528

Revision:
6:9f5ecac1118f
Parent:
4:327a3341b5f3
--- a/main.cpp	Fri Jul 07 20:45:17 2017 +0000
+++ b/main.cpp	Fri Jul 07 21:55:18 2017 +0000
@@ -7,11 +7,14 @@
 CameraOV528 camera(D1, D0); // DX, RX
 SDBlockDevice bd(PTE3, PTE1, PTE2, PTE4); // MOSI, MISO, SCLK, CS
 FATFileSystem fs("fs");
-DigitalOut led(LED_GREEN);
-DigitalIn btn(SW2);
+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 format_fs(){
     int error = 0;
     printf("Welcome to the filesystem example.\r\n"
            "Formatting a FAT, RAM-backed filesystem. ");
@@ -34,19 +37,22 @@
 }
 
 void init_fs() {
-    if (mount_fs()) {
-        format_fs();
-        mount_fs();
-    }
+  if (mount_fs()) {
+      format_fs();
+      mount_fs();
+  }
 }
 
-void blink() {
-    led = !led;
+void swap_led() {
+    green_led = !green_led;
+    red_led = !red_led;
 }
 
 void take_and_store_photo() {
-    blink();
+    // 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");
 
@@ -55,7 +61,6 @@
         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");
@@ -69,15 +74,30 @@
     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();
-    while (true) {
-      if (!btn.read()) {
-        take_and_store_photo();
-      }
-    }
+
+    // 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));
 }