BusIn.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include "drivers/BusIn.h"
  17. #include "platform/mbed_assert.h"
  18. namespace mbed {
  19. BusIn::BusIn(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15)
  20. {
  21. PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
  22. // No lock needed in the constructor
  23. _nc_mask = 0;
  24. for (int i = 0; i < 16; i++) {
  25. _pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0;
  26. if (pins[i] != NC) {
  27. _nc_mask |= (1 << i);
  28. }
  29. }
  30. }
  31. BusIn::BusIn(PinName pins[16])
  32. {
  33. // No lock needed in the constructor
  34. _nc_mask = 0;
  35. for (int i = 0; i < 16; i++) {
  36. _pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0;
  37. if (pins[i] != NC) {
  38. _nc_mask |= (1 << i);
  39. }
  40. }
  41. }
  42. BusIn::~BusIn()
  43. {
  44. // No lock needed in the destructor
  45. for (int i = 0; i < 16; i++) {
  46. if (_pin[i] != 0) {
  47. delete _pin[i];
  48. }
  49. }
  50. }
  51. int BusIn::read()
  52. {
  53. int v = 0;
  54. lock();
  55. for (int i = 0; i < 16; i++) {
  56. if (_pin[i] != 0) {
  57. v |= _pin[i]->read() << i;
  58. }
  59. }
  60. unlock();
  61. return v;
  62. }
  63. void BusIn::mode(PinMode pull)
  64. {
  65. lock();
  66. for (int i = 0; i < 16; i++) {
  67. if (_pin[i] != 0) {
  68. _pin[i]->mode(pull);
  69. }
  70. }
  71. unlock();
  72. }
  73. void BusIn::lock()
  74. {
  75. _mutex.lock();
  76. }
  77. void BusIn::unlock()
  78. {
  79. _mutex.unlock();
  80. }
  81. BusIn::operator int()
  82. {
  83. // Underlying read is thread safe
  84. return read();
  85. }
  86. DigitalIn &BusIn::operator[](int index)
  87. {
  88. // No lock needed since _pin is not modified outside the constructor
  89. MBED_ASSERT(index >= 0 && index <= 16);
  90. MBED_ASSERT(_pin[index]);
  91. return *_pin[index];
  92. }
  93. } // namespace mbed