Naze32 clone with Frysky receiver
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AnalogIn.h 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.h"
  19. #if DEVICE_ANALOGIN
  20. #include "analogin_api.h"
  21. namespace mbed {
  22. /** An analog input, used for reading the voltage on a pin
  23. *
  24. * Example:
  25. * @code
  26. * // Print messages when the AnalogIn is greater than 50%
  27. *
  28. * #include "mbed.h"
  29. *
  30. * AnalogIn temperature(p20);
  31. *
  32. * int main() {
  33. * while(1) {
  34. * if(temperature > 0.5) {
  35. * printf("Too hot! (%f)", temperature.read());
  36. * }
  37. * }
  38. * }
  39. * @endcode
  40. */
  41. class AnalogIn {
  42. public:
  43. /** Create an AnalogIn, connected to the specified pin
  44. *
  45. * @param pin AnalogIn pin to connect to
  46. * @param name (optional) A string to identify the object
  47. */
  48. AnalogIn(PinName pin) {
  49. analogin_init(&_adc, pin);
  50. }
  51. /** Read the input voltage, represented as a float in the range [0.0, 1.0]
  52. *
  53. * @returns A floating-point value representing the current input voltage, measured as a percentage
  54. */
  55. float read() {
  56. return analogin_read(&_adc);
  57. }
  58. /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
  59. *
  60. * @returns
  61. * 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
  62. */
  63. unsigned short read_u16() {
  64. return analogin_read_u16(&_adc);
  65. }
  66. #ifdef MBED_OPERATORS
  67. /** An operator shorthand for read()
  68. *
  69. * The float() operator can be used as a shorthand for read() to simplify common code sequences
  70. *
  71. * Example:
  72. * @code
  73. * float x = volume.read();
  74. * float x = volume;
  75. *
  76. * if(volume.read() > 0.25) { ... }
  77. * if(volume > 0.25) { ... }
  78. * @endcode
  79. */
  80. operator float() {
  81. return read();
  82. }
  83. #endif
  84. protected:
  85. analogin_t _adc;
  86. };
  87. } // namespace mbed
  88. #endif
  89. #endif