A compilation of some hardware sensors and their shared programming interfaces.

Committer:
mgottscho
Date:
Wed Mar 19 00:35:31 2014 +0000
Revision:
1:15396cab58d1
Parent:
0:8d34cc2ff388
Updated for most recent UtilityLib.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mgottscho 0:8d34cc2ff388 1 /* ADCSensor.cpp
mgottscho 0:8d34cc2ff388 2 * Tested with mbed board: FRDM-KL46Z
mgottscho 0:8d34cc2ff388 3 * Author: Mark Gottscho
mgottscho 0:8d34cc2ff388 4 * mgottscho@ucla.edu
mgottscho 0:8d34cc2ff388 5 */
mgottscho 0:8d34cc2ff388 6
mgottscho 0:8d34cc2ff388 7 #include "mbed.h"
mgottscho 0:8d34cc2ff388 8 #include "ADCSensor.h"
mgottscho 0:8d34cc2ff388 9 #include "PeriodicSensor.h"
mgottscho 0:8d34cc2ff388 10
mgottscho 0:8d34cc2ff388 11 ADCSensor::ADCSensor(PinName adc_pin) :
mgottscho 0:8d34cc2ff388 12 PeriodicSensor(0.005), //default max sampling rate of 200Hz
mgottscho 0:8d34cc2ff388 13 __adc(adc_pin),
mgottscho 0:8d34cc2ff388 14 __data(0)
mgottscho 0:8d34cc2ff388 15 {
mgottscho 0:8d34cc2ff388 16 }
mgottscho 0:8d34cc2ff388 17
mgottscho 0:8d34cc2ff388 18 ADCSensor::~ADCSensor() { }
mgottscho 0:8d34cc2ff388 19
mgottscho 0:8d34cc2ff388 20 uint16_t ADCSensor::getSample(bool sampleNow) {
mgottscho 0:8d34cc2ff388 21 __disable_irq();
mgottscho 0:8d34cc2ff388 22 if (sampleNow) {
mgottscho 0:8d34cc2ff388 23 __data = __adc.read_u16();
mgottscho 0:8d34cc2ff388 24 }
mgottscho 0:8d34cc2ff388 25
mgottscho 0:8d34cc2ff388 26 __dataReady = false;
mgottscho 0:8d34cc2ff388 27 __enable_irq();
mgottscho 0:8d34cc2ff388 28
mgottscho 0:8d34cc2ff388 29 return __data;
mgottscho 0:8d34cc2ff388 30 }
mgottscho 0:8d34cc2ff388 31
mgottscho 0:8d34cc2ff388 32 float ADCSensor::getSampleFloat(bool sampleNow) {
mgottscho 0:8d34cc2ff388 33 return getSample(sampleNow) * ADC_DIV;
mgottscho 0:8d34cc2ff388 34 }
mgottscho 0:8d34cc2ff388 35
mgottscho 0:8d34cc2ff388 36 void ADCSensor::__sample_data_ISR() {
mgottscho 0:8d34cc2ff388 37 getSample(true);
mgottscho 0:8d34cc2ff388 38 __dataReady = true;
mgottscho 0:8d34cc2ff388 39 }