PortIn.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_PORTIN_H
  17. #define MBED_PORTIN_H
  18. #include "platform/platform.h"
  19. #if defined (DEVICE_PORTIN) || defined(DOXYGEN_ONLY)
  20. #include "hal/port_api.h"
  21. #include "platform/mbed_critical.h"
  22. namespace mbed {
  23. /** \addtogroup drivers */
  24. /** A multiple pin digital input
  25. *
  26. * @note Synchronization level: Interrupt safe
  27. *
  28. * Example:
  29. * @code
  30. * // Switch on an LED if any of mbed pins 21-26 is high
  31. *
  32. * #include "mbed.h"
  33. *
  34. * PortIn p(Port2, 0x0000003F); // p21-p26
  35. * DigitalOut ind(LED4);
  36. *
  37. * int main() {
  38. * while(1) {
  39. * int pins = p.read();
  40. * if(pins) {
  41. * ind = 1;
  42. * } else {
  43. * ind = 0;
  44. * }
  45. * }
  46. * }
  47. * @endcode
  48. * @ingroup drivers
  49. */
  50. class PortIn {
  51. public:
  52. /** Create an PortIn, connected to the specified port
  53. *
  54. * @param port Port to connect to (Port0-Port5)
  55. * @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
  56. */
  57. PortIn(PortName port, int mask = 0xFFFFFFFF)
  58. {
  59. core_util_critical_section_enter();
  60. port_init(&_port, port, mask, PIN_INPUT);
  61. core_util_critical_section_exit();
  62. }
  63. /** Read the value currently output on the port
  64. *
  65. * @returns
  66. * An integer with each bit corresponding to associated port pin setting
  67. */
  68. int read()
  69. {
  70. return port_read(&_port);
  71. }
  72. /** Set the input pin mode
  73. *
  74. * @param mode PullUp, PullDown, PullNone, OpenDrain
  75. */
  76. void mode(PinMode mode)
  77. {
  78. core_util_critical_section_enter();
  79. port_mode(&_port, mode);
  80. core_util_critical_section_exit();
  81. }
  82. /** A shorthand for read()
  83. */
  84. operator int()
  85. {
  86. return read();
  87. }
  88. private:
  89. port_t _port;
  90. };
  91. } // namespace mbed
  92. #endif
  93. #endif