A compilation of some hardware sensors and their shared programming interfaces.
TouchSensor.cpp@1:15396cab58d1, 2014-03-19 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
mgottscho | 0:8d34cc2ff388 | 1 | /* TouchSensor.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 "TouchSensor.h" |
mgottscho | 0:8d34cc2ff388 | 9 | #include "PeriodicSensor.h" |
mgottscho | 0:8d34cc2ff388 | 10 | |
mgottscho | 0:8d34cc2ff388 | 11 | TouchSensor::TouchSensor() : |
mgottscho | 0:8d34cc2ff388 | 12 | PeriodicSensor(0.005), //default min sampling rate of 200 Hz |
mgottscho | 0:8d34cc2ff388 | 13 | __sensor(), |
mgottscho | 0:8d34cc2ff388 | 14 | __distance(0), |
mgottscho | 0:8d34cc2ff388 | 15 | __percentage(0) |
mgottscho | 0:8d34cc2ff388 | 16 | { |
mgottscho | 0:8d34cc2ff388 | 17 | } |
mgottscho | 0:8d34cc2ff388 | 18 | |
mgottscho | 0:8d34cc2ff388 | 19 | TouchSensor::~TouchSensor() { } |
mgottscho | 0:8d34cc2ff388 | 20 | |
mgottscho | 0:8d34cc2ff388 | 21 | float TouchSensor::getPercentage(bool sampleNow) { |
mgottscho | 0:8d34cc2ff388 | 22 | __disable_irq(); |
mgottscho | 0:8d34cc2ff388 | 23 | if (sampleNow) { |
mgottscho | 0:8d34cc2ff388 | 24 | __percentage = __sensor.readPercentage(); |
mgottscho | 0:8d34cc2ff388 | 25 | } |
mgottscho | 0:8d34cc2ff388 | 26 | |
mgottscho | 0:8d34cc2ff388 | 27 | __dataReady = false; |
mgottscho | 0:8d34cc2ff388 | 28 | __enable_irq(); |
mgottscho | 0:8d34cc2ff388 | 29 | |
mgottscho | 0:8d34cc2ff388 | 30 | return __percentage; |
mgottscho | 0:8d34cc2ff388 | 31 | } |
mgottscho | 0:8d34cc2ff388 | 32 | |
mgottscho | 0:8d34cc2ff388 | 33 | uint8_t TouchSensor::getDistance(bool sampleNow) { |
mgottscho | 0:8d34cc2ff388 | 34 | __disable_irq(); |
mgottscho | 0:8d34cc2ff388 | 35 | if (sampleNow) { |
mgottscho | 0:8d34cc2ff388 | 36 | __distance = __sensor.readDistance(); |
mgottscho | 0:8d34cc2ff388 | 37 | } |
mgottscho | 0:8d34cc2ff388 | 38 | |
mgottscho | 0:8d34cc2ff388 | 39 | __dataReady = false; |
mgottscho | 0:8d34cc2ff388 | 40 | __enable_irq(); |
mgottscho | 0:8d34cc2ff388 | 41 | |
mgottscho | 0:8d34cc2ff388 | 42 | return __distance; |
mgottscho | 0:8d34cc2ff388 | 43 | } |
mgottscho | 0:8d34cc2ff388 | 44 | |
mgottscho | 0:8d34cc2ff388 | 45 | void TouchSensor::__sample_data_ISR() { |
mgottscho | 0:8d34cc2ff388 | 46 | getDistance(true); |
mgottscho | 0:8d34cc2ff388 | 47 | getPercentage(true); |
mgottscho | 0:8d34cc2ff388 | 48 | __dataReady = true; |
mgottscho | 0:8d34cc2ff388 | 49 | } |