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

NbAnalogIn.h

Committer:
dontknowhow
Date:
2017-04-03
Revision:
2:336af413f75c
Parent:
1:2666729acca1
Child:
3:d4f99bc10643

File content as of revision 2:336af413f75c:

#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
  */
  
class NbAnalogIn{
public:

    
    
    /**
    * Create a NbAnalogIn object, sets up ADC
    *
    * @param pin AnalogIn pin to connect to
    */
    NbAnalogIn( PinName pin, void (*irqfunc)() = 0 );
    
    /**
    * does a single blocking read and returns a 12 bit output
    */
    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);    
    
    /**
    * checks if the buffer has new results that can be read
    */
    bool readable();
    
    /**
    * returns the next value from the buffer
    */
    int read();
    
    /** An operator shorthand for read()
    */
    operator int() {
        return read();
    }
    
    
    // custom interrupt handler which, if set, is called after the internal
    // interrupt handling
    void (* cirq)(); 
    
        
private:
    
    int buffer[ADC_BUF_SIZE];
    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]; 
    
    
    
};


#endif