Class similar to AnalogIn that uses burst mode to run continious background conversions so when the input is read, the last value can immediatly be returned. This slightly modified version allows NC pins.

Dependents:   Pinscape_Controller

Fork of FastAnalogIn by Erik -

Revision:
2:9b61d0792927
Parent:
0:c2a7b899e6c7
Child:
3:a9b753c25073
--- a/FastAnalogIn.h	Sat May 11 08:56:22 2013 +0000
+++ b/FastAnalogIn.h	Sat Mar 08 15:44:57 2014 +0000
@@ -7,20 +7,60 @@
 #include "mbed.h"
 #include "pinmap.h"
 
-#ifndef TARGET_LPC1768
+#if !defined TARGET_LPC1768 && !defined TARGET_KL25Z && !defined TARGET_KL46Z && !defined TARGET_KL05Z
     #error "Target not supported"
 #endif
 
-/** A class that is similar to AnalogIn, only faster
-*
-* AnalogIn does a single conversion when you read a value (actually several conversions and it takes the median of that).
-* This library has all used ADC channels running automatically in the background, and if you read a value it will immediatly return the last converted value.
-*
-* You can use several FastAnalogIn objects per ADC pin, and several ADC pins at the same time. Using more ADC pins will decrease the conversion rate.
-* If you need to generally convert one pin very fast, but sometimes also need to do AD conversions on another pin, you can disable the ADC channel and still read its value.
-* Only now it will block until conversion is complete, so more like the regular AnalogIn library. Of course you can also disable an ADC channel and enable it later.
-*
-* It does not play nicely with regular AnalogIn objects, so use only this library or only AnalogIn. Also currently only LPC1768 is supported.
+ /** A class similar to AnalogIn, only faster, for LPC1768 and KLxx
+ *
+ * AnalogIn does a single conversion when you read a value (actually several conversions and it takes the median of that).
+ * This library runns the ADC conversion automatically in the background.
+ * When read is called, it immediatly returns the last sampled value.
+ * Using more ADC pins in continuous mode will decrease the conversion rate.
+ * If you need to sample one pin very fast and sometimes also need to do AD conversions on another pin,
+ * you can disable the continuous conversion on that ADC channel and still read its value.
+ * When continuous conversion is disabled, a read will block until the conversion is complete (much like the regular AnalogIn library does).
+ * Each ADC channel can be enabled/disabled separately.
+ *
+ * IMPORTANT NOTES
+ * ---------------
+ * - When used with KLxx processors, this library can coexist with the regular AnalogIn library.
+ * - When used with the LPC1768 processor, it does not play nicely with regular AnalogIn objects,
+ *   so either use this library or AnalogIn, not both at the same time!!
+ *
+ * Example for the KLxx processors:
+ * @code
+ * // Print messages when the AnalogIn is greater than 50%
+ *
+ * #include "mbed.h"
+ *
+ * FastAnalogIn temperature(PTC2); //Fast sampling on PTC2
+ * AnalogIn speed(PTB3);           //Normal sampling on PTB3
+ *
+ * int main() {
+ *     while(1) {
+ *         if(temperature > 0.5) {
+ *             printf("Too hot! (%f) at speed %f", temperature.read(), speed.read());
+ *         }
+ *     }
+ * }
+ * @endcode
+ * Example for the LPC1768 processor:
+ * @code
+ * // Print messages when the AnalogIn is greater than 50%
+ *
+ * #include "mbed.h"
+ *
+ * AnalogIn temperature(p20);
+ *
+ * int main() {
+ *     while(1) {
+ *         if(temperature > 0.5) {
+ *             printf("Too hot! (%f)", temperature.read());
+ *         }
+ *     }
+ * }
+ * @endcode
 */
 class FastAnalogIn {
 
@@ -32,7 +72,10 @@
      */
     FastAnalogIn( PinName pin, bool enabled = true );
     
-    ~FastAnalogIn( void );
+    ~FastAnalogIn( void )
+    {
+        disable();
+    }
     
     /** Enable the ADC channel
     *
@@ -58,7 +101,11 @@
     *
     * @param return Float with scaled converted value to 0.0-1.0
     */
-    float read( void );
+    float read( void )
+    {
+        unsigned short value = read_u16();
+        return (float)value/65535;
+    }
     
     /** An operator shorthand for read()
     */
@@ -68,16 +115,10 @@
 
     
 private:
-    static const PinMap PinMap_ADC[9];
     static int channel_usage[8];
-    
     bool running;    
     char ADCnumber;
     uint32_t *datareg;
-    
-    
-
-
 };
 
-#endif
\ No newline at end of file
+#endif