DigitalOut.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_DIGITALOUT_H
  17. #define MBED_DIGITALOUT_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 output, used for setting the state of a pin
  24. *
  25. * @note Synchronization level: Interrupt safe
  26. *
  27. * Example:
  28. * @code
  29. * // Toggle a LED
  30. * #include "mbed.h"
  31. *
  32. * DigitalOut led(LED1);
  33. *
  34. * int main() {
  35. * while(1) {
  36. * led = !led;
  37. * wait(0.2);
  38. * }
  39. * }
  40. * @endcode
  41. * @ingroup drivers
  42. */
  43. class DigitalOut {
  44. public:
  45. /** Create a DigitalOut connected to the specified pin
  46. *
  47. * @param pin DigitalOut pin to connect to
  48. */
  49. DigitalOut(PinName pin) : gpio()
  50. {
  51. // No lock needed in the constructor
  52. gpio_init_out(&gpio, pin);
  53. }
  54. /** Create a DigitalOut connected to the specified pin
  55. *
  56. * @param pin DigitalOut pin to connect to
  57. * @param value the initial pin value
  58. */
  59. DigitalOut(PinName pin, int value) : gpio()
  60. {
  61. // No lock needed in the constructor
  62. gpio_init_out_ex(&gpio, pin, value);
  63. }
  64. /** Set the output, specified as 0 or 1 (int)
  65. *
  66. * @param value An integer specifying the pin output value,
  67. * 0 for logical 0, 1 (or any other non-zero value) for logical 1
  68. */
  69. void write(int value)
  70. {
  71. // Thread safe / atomic HAL call
  72. gpio_write(&gpio, value);
  73. }
  74. /** Return the output setting, represented as 0 or 1 (int)
  75. *
  76. * @returns
  77. * an integer representing the output setting of the pin,
  78. * 0 for logical 0, 1 for logical 1
  79. */
  80. int read()
  81. {
  82. // Thread safe / atomic HAL call
  83. return gpio_read(&gpio);
  84. }
  85. /** Return the output setting, represented as 0 or 1 (int)
  86. *
  87. * @returns
  88. * Non zero value if pin is connected to uc GPIO
  89. * 0 if gpio object was initialized with NC
  90. */
  91. int is_connected()
  92. {
  93. // Thread safe / atomic HAL call
  94. return gpio_is_connected(&gpio);
  95. }
  96. /** A shorthand for write()
  97. * \sa DigitalOut::write()
  98. */
  99. DigitalOut &operator= (int value)
  100. {
  101. // Underlying write is thread safe
  102. write(value);
  103. return *this;
  104. }
  105. /** A shorthand for write()
  106. * \sa DigitalOut::write()
  107. */
  108. DigitalOut &operator= (DigitalOut &rhs)
  109. {
  110. core_util_critical_section_enter();
  111. write(rhs.read());
  112. core_util_critical_section_exit();
  113. return *this;
  114. }
  115. /** A shorthand for read()
  116. * \sa DigitalOut::read()
  117. */
  118. operator int()
  119. {
  120. // Underlying call is thread safe
  121. return read();
  122. }
  123. protected:
  124. gpio_t gpio;
  125. };
  126. } // namespace mbed
  127. #endif