DigitalIn.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_DIGITALIN_H
  17. #define MBED_DIGITALIN_H
  18. #include "platform/platform.h"
  19. #include "hal/gpio_api.h"
  20. #include "platform/mbed_critical.h"
  21. namespace mbed {
  22. /** \addtogroup drivers */
  23. /** A digital input, used for reading the state of a pin
  24. *
  25. * @note Synchronization level: Interrupt safe
  26. *
  27. * Example:
  28. * @code
  29. * // Flash an LED while a DigitalIn is true
  30. *
  31. * #include "mbed.h"
  32. *
  33. * DigitalIn enable(p5);
  34. * DigitalOut led(LED1);
  35. *
  36. * int main() {
  37. * while(1) {
  38. * if(enable) {
  39. * led = !led;
  40. * }
  41. * wait(0.25);
  42. * }
  43. * }
  44. * @endcode
  45. * @ingroup drivers
  46. */
  47. class DigitalIn {
  48. public:
  49. /** Create a DigitalIn connected to the specified pin
  50. *
  51. * @param pin DigitalIn pin to connect to
  52. */
  53. DigitalIn(PinName pin) : gpio()
  54. {
  55. // No lock needed in the constructor
  56. gpio_init_in(&gpio, pin);
  57. }
  58. /** Create a DigitalIn connected to the specified pin
  59. *
  60. * @param pin DigitalIn pin to connect to
  61. * @param mode the initial mode of the pin
  62. */
  63. DigitalIn(PinName pin, PinMode mode) : gpio()
  64. {
  65. // No lock needed in the constructor
  66. gpio_init_in_ex(&gpio, pin, mode);
  67. }
  68. /** Read the input, represented as 0 or 1 (int)
  69. *
  70. * @returns
  71. * An integer representing the state of the input pin,
  72. * 0 for logical 0, 1 for logical 1
  73. */
  74. int read()
  75. {
  76. // Thread safe / atomic HAL call
  77. return gpio_read(&gpio);
  78. }
  79. /** Set the input pin mode
  80. *
  81. * @param pull PullUp, PullDown, PullNone, OpenDrain
  82. */
  83. void mode(PinMode pull)
  84. {
  85. core_util_critical_section_enter();
  86. gpio_mode(&gpio, pull);
  87. core_util_critical_section_exit();
  88. }
  89. /** Return the output setting, represented as 0 or 1 (int)
  90. *
  91. * @returns
  92. * Non zero value if pin is connected to uc GPIO
  93. * 0 if gpio object was initialized with NC
  94. */
  95. int is_connected()
  96. {
  97. // Thread safe / atomic HAL call
  98. return gpio_is_connected(&gpio);
  99. }
  100. /** An operator shorthand for read()
  101. * \sa DigitalIn::read()
  102. */
  103. operator int()
  104. {
  105. // Underlying read is thread safe
  106. return read();
  107. }
  108. protected:
  109. gpio_t gpio;
  110. };
  111. } // namespace mbed
  112. #endif