Fast AnalogIn module which asks for a single non blocking reading and causes and interrupt when done.

Revision:
2:336af413f75c
Parent:
1:2666729acca1
Child:
3:d4f99bc10643
--- a/NbAnalogIn.h	Sun Apr 02 11:16:24 2017 +0000
+++ b/NbAnalogIn.h	Mon Apr 03 10:59:17 2017 +0000
@@ -1,9 +1,11 @@
 #include "mbed.h"
+#include "pinmap.h"
 
 #ifndef NBANALOGIN_H
 #define NBANALOGIN_H
 
 #define ADC_BUF_SIZE 100
+#define ADC_CHANNELS 8
 
 /** Library for non blocking ADC operation
   */
@@ -15,28 +17,46 @@
     
     /**
     * Create a NbAnalogIn object, sets up ADC
+    *
+    * @param pin AnalogIn pin to connect to
     */
-    NbAnalogIn();
+    NbAnalogIn( PinName pin, void (*irqfunc)() = 0 );
     
     /**
     * does a single blocking read and returns a 12 bit output
     */
-    unsigned int read();
+    int readBl();
+    
+    /**
+    * starts a conversion and sets the interrupt to fire when ADC is finished
+    * the result will be put into the internal buffer
+    *
+    * @param wait - If true, wait for current adc conversion to finish, 
+    *               if false (default), stop the current conversion replace it
+    */
+    void triggerConv(bool wait = false);    
     
     /**
-    * sets the interrupt to fire when ADC is finished
+    * checks if the buffer has new results that can be read
+    */
+    bool readable();
+    
+    /**
+    * returns the next value from the buffer
     */
-    void triggerConv();
+    int read();
+    
+    /** An operator shorthand for read()
+    */
+    operator int() {
+        return read();
+    }
     
     
-    
-    bool readable();
+    // custom interrupt handler which, if set, is called after the internal
+    // interrupt handling
+    void (* cirq)(); 
     
-    int readNb();
-    
-    static void irq();
-    static NbAnalogIn* handlers[2]; 
-    void handler( int adc_result );
         
 private:
     
@@ -44,7 +64,13 @@
     int write_pos;      // next position to be written to
     int read_pos;       // next position to be read from
     
+    static volatile bool converting;
     
+    uint8_t channel;    // channel of current object (ADC0-7)
+    void handler( int adc_result );
+    
+    static void irq();
+    static NbAnalogIn* handlers[ADC_CHANNELS];