Discrete RGB colour sensor using a colour LED flashing at high speed and a monochrome LDR (light dependent resistor) for detecting the colour via ADC conversion. The library implements interrupt driven ADC conversion at high speed (370 RGB readings per second, 128 times oversampling per channelfor noise reduction). The detection can optionally run in background.

Dependents:   rgb_sensor_buffer discrete_rgb_color_sensor_example

Revision:
3:50e1ac3c56db
Parent:
2:4545984e62b6
Child:
4:0ffadc2caaf6
--- a/rgb_sensor.cpp	Fri Jun 27 07:21:51 2014 +0000
+++ b/rgb_sensor.cpp	Fri Jun 27 11:36:15 2014 +0000
@@ -145,9 +145,18 @@
 
                     default:
                         m_blue= !RGB_LED_ON;
-                        m_done = true;
+                        if(!m_callback)
+                            m_done = true;
+                        else
+                        {
+                            TRGB rgb;
+                            convert(rgb);
+                            m_done = !m_callback(rgb);
+                            m_rgb_channel = 0;
+                        }
                         /* stop data aquisition */
-                        LPC_ADC->ADCR &= ~(1UL<<16);
+                        if(m_done)
+                            LPC_ADC->ADCR &= ~(1UL<<16);
                 }                        
             }
         }
@@ -155,12 +164,13 @@
     LPC_ADC->ADSTAT = status;
 }
 
-bool RGB_Sensor::capture(void)
+bool RGB_Sensor::capture(TRGB_Callback callback)
 {
     /* ignore mis-configurations */
     if(m_adc_channel==(ADCName)NC)
         return false;
 
+    m_callback = callback;
     m_done = false;
     m_adc_count = m_rgb_channel = 0;
     memset((void*)&m_adc_aggregation, 0, sizeof(m_adc_aggregation));
@@ -182,17 +192,21 @@
     return true;
 }
 
-bool RGB_Sensor::capture(TRGB &rgb)
+void RGB_Sensor::convert(TRGB &rgb)
 {
-    if(!(capture() && wait()))
-        return false;
-
     /* correct "DC" offset by subdstracting
      * environment light
      */
     rgb.ch.red   = m_adc_aggregation[1] - m_adc_aggregation[0];   
     rgb.ch.green = m_adc_aggregation[2] - m_adc_aggregation[0];   
     rgb.ch.blue  = m_adc_aggregation[3] - m_adc_aggregation[0];
+} 
 
+bool RGB_Sensor::capture(TRGB &rgb)
+{
+    if(!(capture(NULL) && wait()))
+        return false;
+
+    convert(rgb);
     return true;  
 }