Naze32 clone with Frysky receiver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DigitalInOut.h 3.2KB

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_DIGITALINOUT_H
  17. #define MBED_DIGITALINOUT_H
  18. #include "platform.h"
  19. #include "gpio_api.h"
  20. namespace mbed {
  21. /** A digital input/output, used for setting or reading a bi-directional pin
  22. */
  23. class DigitalInOut {
  24. public:
  25. /** Create a DigitalInOut connected to the specified pin
  26. *
  27. * @param pin DigitalInOut pin to connect to
  28. */
  29. DigitalInOut(PinName pin) : gpio() {
  30. gpio_init_in(&gpio, pin);
  31. }
  32. /** Create a DigitalInOut connected to the specified pin
  33. *
  34. * @param pin DigitalInOut pin to connect to
  35. * @param direction the initial direction of the pin
  36. * @param mode the initial mode of the pin
  37. * @param value the initial value of the pin if is an output
  38. */
  39. DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio() {
  40. gpio_init_inout(&gpio, pin, direction, mode, value);
  41. }
  42. /** Set the output, specified as 0 or 1 (int)
  43. *
  44. * @param value An integer specifying the pin output value,
  45. * 0 for logical 0, 1 (or any other non-zero value) for logical 1
  46. */
  47. void write(int value) {
  48. gpio_write(&gpio, value);
  49. }
  50. /** Return the output setting, represented as 0 or 1 (int)
  51. *
  52. * @returns
  53. * an integer representing the output setting of the pin if it is an output,
  54. * or read the input if set as an input
  55. */
  56. int read() {
  57. return gpio_read(&gpio);
  58. }
  59. /** Set as an output
  60. */
  61. void output() {
  62. gpio_dir(&gpio, PIN_OUTPUT);
  63. }
  64. /** Set as an input
  65. */
  66. void input() {
  67. gpio_dir(&gpio, PIN_INPUT);
  68. }
  69. /** Set the input pin mode
  70. *
  71. * @param mode PullUp, PullDown, PullNone, OpenDrain
  72. */
  73. void mode(PinMode pull) {
  74. gpio_mode(&gpio, pull);
  75. }
  76. /** Return the output setting, represented as 0 or 1 (int)
  77. *
  78. * @returns
  79. * Non zero value if pin is connected to uc GPIO
  80. * 0 if gpio object was initialized with NC
  81. */
  82. int is_connected() {
  83. return gpio_is_connected(&gpio);
  84. }
  85. #ifdef MBED_OPERATORS
  86. /** A shorthand for write()
  87. */
  88. DigitalInOut& operator= (int value) {
  89. write(value);
  90. return *this;
  91. }
  92. DigitalInOut& operator= (DigitalInOut& rhs) {
  93. write(rhs.read());
  94. return *this;
  95. }
  96. /** A shorthand for read()
  97. */
  98. operator int() {
  99. return read();
  100. }
  101. #endif
  102. protected:
  103. gpio_t gpio;
  104. };
  105. } // namespace mbed
  106. #endif