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

Committer:
dontknowhow
Date:
Mon Apr 03 10:59:17 2017 +0000
Revision:
2:336af413f75c
Parent:
1:2666729acca1
Child:
3:d4f99bc10643
Multiple channels, custom interrupt;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dontknowhow 0:058d32b78e5d 1 #include "mbed.h"
dontknowhow 2:336af413f75c 2 #include "pinmap.h"
dontknowhow 0:058d32b78e5d 3
dontknowhow 0:058d32b78e5d 4 #ifndef NBANALOGIN_H
dontknowhow 0:058d32b78e5d 5 #define NBANALOGIN_H
dontknowhow 0:058d32b78e5d 6
dontknowhow 1:2666729acca1 7 #define ADC_BUF_SIZE 100
dontknowhow 2:336af413f75c 8 #define ADC_CHANNELS 8
dontknowhow 1:2666729acca1 9
dontknowhow 0:058d32b78e5d 10 /** Library for non blocking ADC operation
dontknowhow 0:058d32b78e5d 11 */
dontknowhow 0:058d32b78e5d 12
dontknowhow 0:058d32b78e5d 13 class NbAnalogIn{
dontknowhow 0:058d32b78e5d 14 public:
dontknowhow 1:2666729acca1 15
dontknowhow 1:2666729acca1 16
dontknowhow 1:2666729acca1 17
dontknowhow 0:058d32b78e5d 18 /**
dontknowhow 0:058d32b78e5d 19 * Create a NbAnalogIn object, sets up ADC
dontknowhow 2:336af413f75c 20 *
dontknowhow 2:336af413f75c 21 * @param pin AnalogIn pin to connect to
dontknowhow 0:058d32b78e5d 22 */
dontknowhow 2:336af413f75c 23 NbAnalogIn( PinName pin, void (*irqfunc)() = 0 );
dontknowhow 0:058d32b78e5d 24
dontknowhow 0:058d32b78e5d 25 /**
dontknowhow 0:058d32b78e5d 26 * does a single blocking read and returns a 12 bit output
dontknowhow 0:058d32b78e5d 27 */
dontknowhow 2:336af413f75c 28 int readBl();
dontknowhow 2:336af413f75c 29
dontknowhow 2:336af413f75c 30 /**
dontknowhow 2:336af413f75c 31 * starts a conversion and sets the interrupt to fire when ADC is finished
dontknowhow 2:336af413f75c 32 * the result will be put into the internal buffer
dontknowhow 2:336af413f75c 33 *
dontknowhow 2:336af413f75c 34 * @param wait - If true, wait for current adc conversion to finish,
dontknowhow 2:336af413f75c 35 * if false (default), stop the current conversion replace it
dontknowhow 2:336af413f75c 36 */
dontknowhow 2:336af413f75c 37 void triggerConv(bool wait = false);
dontknowhow 0:058d32b78e5d 38
dontknowhow 0:058d32b78e5d 39 /**
dontknowhow 2:336af413f75c 40 * checks if the buffer has new results that can be read
dontknowhow 2:336af413f75c 41 */
dontknowhow 2:336af413f75c 42 bool readable();
dontknowhow 2:336af413f75c 43
dontknowhow 2:336af413f75c 44 /**
dontknowhow 2:336af413f75c 45 * returns the next value from the buffer
dontknowhow 0:058d32b78e5d 46 */
dontknowhow 2:336af413f75c 47 int read();
dontknowhow 2:336af413f75c 48
dontknowhow 2:336af413f75c 49 /** An operator shorthand for read()
dontknowhow 2:336af413f75c 50 */
dontknowhow 2:336af413f75c 51 operator int() {
dontknowhow 2:336af413f75c 52 return read();
dontknowhow 2:336af413f75c 53 }
dontknowhow 1:2666729acca1 54
dontknowhow 1:2666729acca1 55
dontknowhow 2:336af413f75c 56 // custom interrupt handler which, if set, is called after the internal
dontknowhow 2:336af413f75c 57 // interrupt handling
dontknowhow 2:336af413f75c 58 void (* cirq)();
dontknowhow 1:2666729acca1 59
dontknowhow 0:058d32b78e5d 60
dontknowhow 0:058d32b78e5d 61 private:
dontknowhow 0:058d32b78e5d 62
dontknowhow 1:2666729acca1 63 int buffer[ADC_BUF_SIZE];
dontknowhow 1:2666729acca1 64 int write_pos; // next position to be written to
dontknowhow 1:2666729acca1 65 int read_pos; // next position to be read from
dontknowhow 1:2666729acca1 66
dontknowhow 2:336af413f75c 67 static volatile bool converting;
dontknowhow 1:2666729acca1 68
dontknowhow 2:336af413f75c 69 uint8_t channel; // channel of current object (ADC0-7)
dontknowhow 2:336af413f75c 70 void handler( int adc_result );
dontknowhow 2:336af413f75c 71
dontknowhow 2:336af413f75c 72 static void irq();
dontknowhow 2:336af413f75c 73 static NbAnalogIn* handlers[ADC_CHANNELS];
dontknowhow 1:2666729acca1 74
dontknowhow 1:2666729acca1 75
dontknowhow 1:2666729acca1 76
dontknowhow 0:058d32b78e5d 77 };
dontknowhow 1:2666729acca1 78
dontknowhow 1:2666729acca1 79
dontknowhow 0:058d32b78e5d 80 #endif