example for using the rgb_color_sensor class

Dependencies:   mbed rgb_sensor

Revision:
3:3649eaf4f954
Parent:
0:d034f4b3b23c
Child:
4:fbbc729f4152
--- a/main.cpp	Wed Jun 25 15:07:06 2014 +0000
+++ b/main.cpp	Fri Jun 27 11:37:25 2014 +0000
@@ -23,37 +23,65 @@
 #include "rgb_sensor.h"
 
 #define COUNT(x) (sizeof(x)/sizeof(x[0]))
+#define RGB_VALUES 500
+
+/* serial console */
+static uint32_t g_buffer_pos;
+static TRGB g_buffer[RGB_VALUES];
+static Serial console(USBTX, USBRX);
+
+static bool rgb_callback(const TRGB &color)
+{
+    if(g_buffer_pos>=RGB_VALUES)
+        return false;
+
+    g_buffer[g_buffer_pos++] = color;
+    return true;
+}
+
+static void rgb_print(const TRGB &color)
+{
+    int i;
+
+    console.printf("\t[");
+    for(i=0; i<COUNT(color.data); i++)
+        console.printf("%s%04i", i?",":"", color.data[i] / RGB_OVERSAMPLING);
+    console.printf("]\n\r");
+}
 
 int main() {
     TRGB color;
-    int i, seq;
 
-    /* serial console */
-    Serial console(USBTX, USBRX);
     console.baud(115200);
 
     /* R,G,B pins and ADC for light dependent resistor */
     RGB_Sensor rgb(p23,p24,p25,p20);
 
+    /* detect a single RGB value to demo synchronous API */
+    rgb.capture(color);
+    rgb_print(color);
+
     /* needed for time measurement */
     Timer timer;
 
-    seq = 0;
+    g_buffer_pos = 0;
+
     while(1) {
 
         /* start four channel RGB conversion */
         timer.reset();
         timer.start();
 
-        /* detect RGB value */
-        rgb.capture(color);
+        g_buffer_pos = 0;
+        rgb.capture(rgb_callback);
+
+        rgb.wait();
 
         /* stop time measurement */
         timer.stop();
 
-        console.printf("%06i", seq++);
-        for(i=0; i<COUNT(color.data); i++)
-            console.printf(",%04i", color.data[i] / RGB_OVERSAMPLING);
-        console.printf(",%i\n\r", timer.read_us());
+        console.printf("Captured %i values per %i ms\n\r", g_buffer_pos, timer.read_ms());
+
+        delay_ms(250);
     }
 }
\ No newline at end of file