Fast AnalogIn module which asks for a single non blocking reading and causes and interrupt when done.
NbAnalogIn.h@3:d4f99bc10643, 2017-04-04 (annotated)
- Committer:
- dontknowhow
- Date:
- Tue Apr 04 22:35:34 2017 +0000
- Revision:
- 3:d4f99bc10643
- Parent:
- 2:336af413f75c
set interrupt priority;
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:058d32b78e5d | 17 | /** |
dontknowhow | 0:058d32b78e5d | 18 | * Create a NbAnalogIn object, sets up ADC |
dontknowhow | 2:336af413f75c | 19 | * |
dontknowhow | 2:336af413f75c | 20 | * @param pin AnalogIn pin to connect to |
dontknowhow | 0:058d32b78e5d | 21 | */ |
dontknowhow | 3:d4f99bc10643 | 22 | NbAnalogIn( PinName pin ); |
dontknowhow | 3:d4f99bc10643 | 23 | |
dontknowhow | 3:d4f99bc10643 | 24 | /** |
dontknowhow | 3:d4f99bc10643 | 25 | * Set interrupt options |
dontknowhow | 3:d4f99bc10643 | 26 | * |
dontknowhow | 3:d4f99bc10643 | 27 | * @param irqfunc - callback function executed at the end of interrupts |
dontknowhow | 3:d4f99bc10643 | 28 | * @param priority - set interrupt priority level. -1 (default) leaves them unchanged |
dontknowhow | 3:d4f99bc10643 | 29 | */ |
dontknowhow | 3:d4f99bc10643 | 30 | void setInterrupt(void (*irqfunc)() = 0, int prio = -1); |
dontknowhow | 0:058d32b78e5d | 31 | |
dontknowhow | 0:058d32b78e5d | 32 | /** |
dontknowhow | 0:058d32b78e5d | 33 | * does a single blocking read and returns a 12 bit output |
dontknowhow | 0:058d32b78e5d | 34 | */ |
dontknowhow | 2:336af413f75c | 35 | int readBl(); |
dontknowhow | 2:336af413f75c | 36 | |
dontknowhow | 2:336af413f75c | 37 | /** |
dontknowhow | 2:336af413f75c | 38 | * starts a conversion and sets the interrupt to fire when ADC is finished |
dontknowhow | 2:336af413f75c | 39 | * the result will be put into the internal buffer |
dontknowhow | 2:336af413f75c | 40 | * |
dontknowhow | 2:336af413f75c | 41 | * @param wait - If true, wait for current adc conversion to finish, |
dontknowhow | 2:336af413f75c | 42 | * if false (default), stop the current conversion replace it |
dontknowhow | 2:336af413f75c | 43 | */ |
dontknowhow | 2:336af413f75c | 44 | void triggerConv(bool wait = false); |
dontknowhow | 0:058d32b78e5d | 45 | |
dontknowhow | 0:058d32b78e5d | 46 | /** |
dontknowhow | 2:336af413f75c | 47 | * checks if the buffer has new results that can be read |
dontknowhow | 2:336af413f75c | 48 | */ |
dontknowhow | 2:336af413f75c | 49 | bool readable(); |
dontknowhow | 2:336af413f75c | 50 | |
dontknowhow | 2:336af413f75c | 51 | /** |
dontknowhow | 2:336af413f75c | 52 | * returns the next value from the buffer |
dontknowhow | 0:058d32b78e5d | 53 | */ |
dontknowhow | 2:336af413f75c | 54 | int read(); |
dontknowhow | 2:336af413f75c | 55 | |
dontknowhow | 2:336af413f75c | 56 | /** An operator shorthand for read() |
dontknowhow | 2:336af413f75c | 57 | */ |
dontknowhow | 2:336af413f75c | 58 | operator int() { |
dontknowhow | 2:336af413f75c | 59 | return read(); |
dontknowhow | 2:336af413f75c | 60 | } |
dontknowhow | 1:2666729acca1 | 61 | |
dontknowhow | 0:058d32b78e5d | 62 | |
dontknowhow | 0:058d32b78e5d | 63 | private: |
dontknowhow | 0:058d32b78e5d | 64 | |
dontknowhow | 1:2666729acca1 | 65 | int buffer[ADC_BUF_SIZE]; |
dontknowhow | 1:2666729acca1 | 66 | int write_pos; // next position to be written to |
dontknowhow | 1:2666729acca1 | 67 | int read_pos; // next position to be read from |
dontknowhow | 1:2666729acca1 | 68 | |
dontknowhow | 2:336af413f75c | 69 | static volatile bool converting; |
dontknowhow | 1:2666729acca1 | 70 | |
dontknowhow | 2:336af413f75c | 71 | uint8_t channel; // channel of current object (ADC0-7) |
dontknowhow | 2:336af413f75c | 72 | void handler( int adc_result ); |
dontknowhow | 2:336af413f75c | 73 | |
dontknowhow | 2:336af413f75c | 74 | static void irq(); |
dontknowhow | 2:336af413f75c | 75 | static NbAnalogIn* handlers[ADC_CHANNELS]; |
dontknowhow | 1:2666729acca1 | 76 | |
dontknowhow | 3:d4f99bc10643 | 77 | // custom interrupt handler which, if set, is called after the internal |
dontknowhow | 3:d4f99bc10643 | 78 | // interrupt handling |
dontknowhow | 3:d4f99bc10643 | 79 | void (* cirq)(); |
dontknowhow | 1:2666729acca1 | 80 | |
dontknowhow | 0:058d32b78e5d | 81 | }; |
dontknowhow | 1:2666729acca1 | 82 | |
dontknowhow | 1:2666729acca1 | 83 | |
dontknowhow | 0:058d32b78e5d | 84 | #endif |