CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Mon Nov 05 09:52:17 2018 -0700
Revision:
3:c547dba5d39b
Parent:
0:aa13e1c335cd
debug

Who changed what in which revision?

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