mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* mbed Microcontroller Library
elessair 0:f269e3021894 2 * Copyright (c) 2006-2013 ARM Limited
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 5 * you may not use this file except in compliance with the License.
elessair 0:f269e3021894 6 * You may obtain a copy of the License at
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 9 *
elessair 0:f269e3021894 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 11 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 13 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 14 * limitations under the License.
elessair 0:f269e3021894 15 */
elessair 0:f269e3021894 16 #ifndef MBED_ANALOGIN_H
elessair 0:f269e3021894 17 #define MBED_ANALOGIN_H
elessair 0:f269e3021894 18
elessair 0:f269e3021894 19 #include "platform/platform.h"
elessair 0:f269e3021894 20
elessair 0:f269e3021894 21 #if DEVICE_ANALOGIN
elessair 0:f269e3021894 22
elessair 0:f269e3021894 23 #include "hal/analogin_api.h"
elessair 0:f269e3021894 24 #include "platform/SingletonPtr.h"
elessair 0:f269e3021894 25 #include "platform/PlatformMutex.h"
elessair 0:f269e3021894 26
elessair 0:f269e3021894 27 namespace mbed {
elessair 0:f269e3021894 28 /** \addtogroup drivers */
elessair 0:f269e3021894 29 /** @{*/
elessair 0:f269e3021894 30
elessair 0:f269e3021894 31 /** An analog input, used for reading the voltage on a pin
elessair 0:f269e3021894 32 *
elessair 0:f269e3021894 33 * @Note Synchronization level: Thread safe
elessair 0:f269e3021894 34 *
elessair 0:f269e3021894 35 * Example:
elessair 0:f269e3021894 36 * @code
elessair 0:f269e3021894 37 * // Print messages when the AnalogIn is greater than 50%
elessair 0:f269e3021894 38 *
elessair 0:f269e3021894 39 * #include "mbed.h"
elessair 0:f269e3021894 40 *
elessair 0:f269e3021894 41 * AnalogIn temperature(p20);
elessair 0:f269e3021894 42 *
elessair 0:f269e3021894 43 * int main() {
elessair 0:f269e3021894 44 * while(1) {
elessair 0:f269e3021894 45 * if(temperature > 0.5) {
elessair 0:f269e3021894 46 * printf("Too hot! (%f)", temperature.read());
elessair 0:f269e3021894 47 * }
elessair 0:f269e3021894 48 * }
elessair 0:f269e3021894 49 * }
elessair 0:f269e3021894 50 * @endcode
elessair 0:f269e3021894 51 */
elessair 0:f269e3021894 52
elessair 0:f269e3021894 53
elessair 0:f269e3021894 54 class BatteryLevel {
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 public:
elessair 0:f269e3021894 57 BatteryLevel() {
elessair 0:f269e3021894 58 battery_init(&_adc);
elessair 0:f269e3021894 59 }
elessair 0:f269e3021894 60
elessair 0:f269e3021894 61 unsigned short read_battery_u16() {
elessair 0:f269e3021894 62 unsigned short ret = battery_read_u16(&_adc);
elessair 0:f269e3021894 63 return ret;
elessair 0:f269e3021894 64 }
elessair 0:f269e3021894 65 virtual ~BatteryLevel() {
elessair 0:f269e3021894 66 // Do nothing
elessair 0:f269e3021894 67 }
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 protected:
elessair 0:f269e3021894 70
elessair 0:f269e3021894 71 analogin_t _adc;
elessair 0:f269e3021894 72 static SingletonPtr<PlatformMutex> _mutex;
elessair 0:f269e3021894 73 };
elessair 0:f269e3021894 74
elessair 0:f269e3021894 75
elessair 0:f269e3021894 76 class AnalogIn {
elessair 0:f269e3021894 77
elessair 0:f269e3021894 78 public:
elessair 0:f269e3021894 79
elessair 0:f269e3021894 80 /** Create an AnalogIn, connected to the specified pin
elessair 0:f269e3021894 81 *
elessair 0:f269e3021894 82 * @param pin AnalogIn pin to connect to
elessair 0:f269e3021894 83 * @param name (optional) A string to identify the object
elessair 0:f269e3021894 84 */
elessair 0:f269e3021894 85 AnalogIn(PinName pin) {
elessair 0:f269e3021894 86 lock();
elessair 0:f269e3021894 87 analogin_init(&_adc, pin);
elessair 0:f269e3021894 88 unlock();
elessair 0:f269e3021894 89 }
elessair 0:f269e3021894 90 /** Read the input voltage, represented as a float in the range [0.0, 1.0]
elessair 0:f269e3021894 91 *
elessair 0:f269e3021894 92 * @returns A floating-point value representing the current input voltage, measured as a percentage
elessair 0:f269e3021894 93 */
elessair 0:f269e3021894 94 float read() {
elessair 0:f269e3021894 95 lock();
elessair 0:f269e3021894 96 float ret = analogin_read(&_adc);
elessair 0:f269e3021894 97 unlock();
elessair 0:f269e3021894 98 return ret;
elessair 0:f269e3021894 99 }
elessair 0:f269e3021894 100
elessair 0:f269e3021894 101 /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
elessair 0:f269e3021894 102 *
elessair 0:f269e3021894 103 * @returns
elessair 0:f269e3021894 104 * 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
elessair 0:f269e3021894 105 */
elessair 0:f269e3021894 106 unsigned short read_u16() {
elessair 0:f269e3021894 107 lock();
elessair 0:f269e3021894 108 unsigned short ret = analogin_read_u16(&_adc);
elessair 0:f269e3021894 109 unlock();
elessair 0:f269e3021894 110 return ret;
elessair 0:f269e3021894 111 }
elessair 0:f269e3021894 112
elessair 0:f269e3021894 113 /** An operator shorthand for read()
elessair 0:f269e3021894 114 *
elessair 0:f269e3021894 115 * The float() operator can be used as a shorthand for read() to simplify common code sequences
elessair 0:f269e3021894 116 *
elessair 0:f269e3021894 117 * Example:
elessair 0:f269e3021894 118 * @code
elessair 0:f269e3021894 119 * float x = volume.read();
elessair 0:f269e3021894 120 * float x = volume;
elessair 0:f269e3021894 121 *
elessair 0:f269e3021894 122 * if(volume.read() > 0.25) { ... }
elessair 0:f269e3021894 123 * if(volume > 0.25) { ... }
elessair 0:f269e3021894 124 * @endcode
elessair 0:f269e3021894 125 */
elessair 0:f269e3021894 126 operator float() {
elessair 0:f269e3021894 127 // Underlying call is thread safe
elessair 0:f269e3021894 128 return read();
elessair 0:f269e3021894 129 }
elessair 0:f269e3021894 130
elessair 0:f269e3021894 131 virtual ~AnalogIn() {
elessair 0:f269e3021894 132 // Do nothing
elessair 0:f269e3021894 133 }
elessair 0:f269e3021894 134
elessair 0:f269e3021894 135 protected:
elessair 0:f269e3021894 136
elessair 0:f269e3021894 137 virtual void lock() {
elessair 0:f269e3021894 138 _mutex->lock();
elessair 0:f269e3021894 139 }
elessair 0:f269e3021894 140
elessair 0:f269e3021894 141 virtual void unlock() {
elessair 0:f269e3021894 142 _mutex->unlock();
elessair 0:f269e3021894 143 }
elessair 0:f269e3021894 144
elessair 0:f269e3021894 145 analogin_t _adc;
elessair 0:f269e3021894 146 static SingletonPtr<PlatformMutex> _mutex;
elessair 0:f269e3021894 147 };
elessair 0:f269e3021894 148
elessair 0:f269e3021894 149 } // namespace mbed
elessair 0:f269e3021894 150
elessair 0:f269e3021894 151 #endif
elessair 0:f269e3021894 152
elessair 0:f269e3021894 153 #endif
elessair 0:f269e3021894 154
elessair 0:f269e3021894 155 /** @}*/