Nicolas Borla / Mbed OS ROME2_Robot_Firmware
Committer:
boro
Date:
Mon Mar 16 13:12:31 2020 +0000
Revision:
0:4beb2ea291ec
a

Who changed what in which revision?

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