AnalogIn.h 3.0 KB

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