BusIn.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_BUSIN_H
  17. #define MBED_BUSIN_H
  18. #include "platform/platform.h"
  19. #include "drivers/DigitalIn.h"
  20. #include "platform/PlatformMutex.h"
  21. #include "platform/NonCopyable.h"
  22. namespace mbed {
  23. /** \addtogroup drivers */
  24. /** A digital input bus, used for reading the state of a collection of pins
  25. *
  26. * @note Synchronization level: Thread safe
  27. * @ingroup drivers
  28. */
  29. class BusIn : private NonCopyable<BusIn> {
  30. public:
  31. /* Group: Configuration Methods */
  32. /** Create an BusIn, connected to the specified pins
  33. *
  34. * @param p0 DigitalIn pin to connect to bus bit
  35. * @param p1 DigitalIn pin to connect to bus bit
  36. * @param p2 DigitalIn pin to connect to bus bit
  37. * @param p3 DigitalIn pin to connect to bus bit
  38. * @param p4 DigitalIn pin to connect to bus bit
  39. * @param p5 DigitalIn pin to connect to bus bit
  40. * @param p6 DigitalIn pin to connect to bus bit
  41. * @param p7 DigitalIn pin to connect to bus bit
  42. * @param p8 DigitalIn pin to connect to bus bit
  43. * @param p9 DigitalIn pin to connect to bus bit
  44. * @param p10 DigitalIn pin to connect to bus bit
  45. * @param p11 DigitalIn pin to connect to bus bit
  46. * @param p12 DigitalIn pin to connect to bus bit
  47. * @param p13 DigitalIn pin to connect to bus bit
  48. * @param p14 DigitalIn pin to connect to bus bit
  49. * @param p15 DigitalIn pin to connect to bus bit
  50. *
  51. * @note
  52. * It is only required to specify as many pin variables as is required
  53. * for the bus; the rest will default to NC (not connected)
  54. */
  55. BusIn(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC,
  56. PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = NC,
  57. PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC,
  58. PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC);
  59. /** Create an BusIn, connected to the specified pins
  60. *
  61. * @param pins An array of pins to connect to bus bit
  62. */
  63. BusIn(PinName pins[16]);
  64. virtual ~BusIn();
  65. /** Read the value of the input bus
  66. *
  67. * @returns
  68. * An integer with each bit corresponding to the value read from the associated DigitalIn pin
  69. */
  70. int read();
  71. /** Set the input pin mode
  72. *
  73. * @param pull PullUp, PullDown, PullNone
  74. */
  75. void mode(PinMode pull);
  76. /** Binary mask of bus pins connected to actual pins (not NC pins)
  77. * If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1
  78. *
  79. * @returns
  80. * Binary mask of connected pins
  81. */
  82. int mask()
  83. {
  84. // No lock needed since _nc_mask is not modified outside the constructor
  85. return _nc_mask;
  86. }
  87. /** A shorthand for read()
  88. * \sa DigitalIn::read()
  89. */
  90. operator int();
  91. /** Access to particular bit in random-iterator fashion
  92. * @param index Position of bit
  93. */
  94. DigitalIn &operator[](int index);
  95. protected:
  96. DigitalIn *_pin[16];
  97. /* Mask of bus's NC pins
  98. * If bit[n] is set to 1 - pin is connected
  99. * if bit[n] is cleared - pin is not connected (NC)
  100. */
  101. int _nc_mask;
  102. PlatformMutex _mutex;
  103. private:
  104. virtual void lock();
  105. virtual void unlock();
  106. };
  107. } // namespace mbed
  108. #endif