Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
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.

Wire.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. TwoWire.h - TWI/I2C library for Arduino & Wiring
  3. Copyright (c) 2006 Nicholas Zambetti. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
  16. Modified December 2014 by Ivan Grokhotkov (ivan@esp8266.com) - esp8266 support
  17. Modified April 2015 by Hrsto Gochkov (ficeto@ficeto.com) - alternative esp8266 support
  18. Modified October 2016 by Marcelo Aquino <marceloaqno@gmail.org> for Raspberry Pi
  19. */
  20. #include "Wire.h"
  21. #include <stdlib.h>
  22. #include <pthread.h>
  23. #include "bcm2835.h"
  24. #include "log.h"
  25. static pthread_mutex_t i2cMutex = PTHREAD_MUTEX_INITIALIZER;
  26. uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
  27. uint8_t TwoWire::rxBufferIndex = 0;
  28. uint8_t TwoWire::rxBufferLength = 0;
  29. uint8_t TwoWire::txAddress = 0;
  30. uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
  31. uint8_t TwoWire::txBufferIndex = 0;
  32. uint8_t TwoWire::txBufferLength = 0;
  33. uint8_t TwoWire::transmitting = 0;
  34. void TwoWire::begin()
  35. {
  36. if (!bcm2835_i2c_begin()) {
  37. logError("You need root privilege to use I2C.\n");
  38. exit(1);
  39. }
  40. }
  41. void TwoWire::begin(uint8_t address)
  42. {
  43. begin();
  44. bcm2835_i2c_setSlaveAddress(address);
  45. }
  46. void TwoWire::begin(int address)
  47. {
  48. begin(static_cast<uint8_t>(address));
  49. }
  50. void TwoWire::end()
  51. {
  52. bcm2835_i2c_end();
  53. }
  54. void TwoWire::setClock(uint32_t clock)
  55. {
  56. bcm2835_i2c_set_baudrate(clock);
  57. }
  58. void TwoWire::beginTransmission(uint8_t address)
  59. {
  60. pthread_mutex_lock(&i2cMutex);
  61. // indicate that we are transmitting
  62. transmitting = 1;
  63. // set address of targeted slave
  64. txAddress = address;
  65. // reset tx buffer iterator vars
  66. txBufferIndex = 0;
  67. txBufferLength = 0;
  68. }
  69. void TwoWire::beginTransmission(int address)
  70. {
  71. beginTransmission(static_cast<uint8_t>(address));
  72. }
  73. uint8_t TwoWire::endTransmission(void)
  74. {
  75. // transmit buffer
  76. bcm2835_i2c_setSlaveAddress(txAddress);
  77. uint8_t ret = bcm2835_i2c_write(reinterpret_cast<const char *>(txBuffer), txBufferLength);
  78. // reset tx buffer iterator vars
  79. txBufferIndex = 0;
  80. txBufferLength = 0;
  81. // indicate that we are done transmitting
  82. transmitting = 0;
  83. pthread_mutex_unlock(&i2cMutex);
  84. if (ret == BCM2835_I2C_REASON_OK) {
  85. return 0; // success
  86. } else if (ret == BCM2835_I2C_REASON_ERROR_NACK) {
  87. return 3; // error: data send, nack received
  88. }
  89. return 4; // other error
  90. }
  91. size_t TwoWire::requestFrom(uint8_t address, size_t quantity)
  92. {
  93. // clamp to buffer length
  94. if (quantity > BUFFER_LENGTH) {
  95. quantity = BUFFER_LENGTH;
  96. }
  97. rxBufferIndex = 0;
  98. rxBufferLength = 0;
  99. bcm2835_i2c_setSlaveAddress(address);
  100. uint8_t ret = bcm2835_i2c_read(reinterpret_cast<char *>(rxBuffer), quantity);
  101. if (ret == BCM2835_I2C_REASON_OK) {
  102. rxBufferLength = quantity;
  103. }
  104. return rxBufferLength;
  105. }
  106. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
  107. {
  108. return requestFrom(address, static_cast<size_t>(quantity));
  109. }
  110. uint8_t TwoWire::requestFrom(int address, int quantity)
  111. {
  112. return requestFrom(static_cast<uint8_t>(address), static_cast<size_t>(quantity));
  113. }
  114. size_t TwoWire::write(uint8_t data)
  115. {
  116. if (transmitting) {
  117. // in master transmitter mode
  118. // don't bother if buffer is full
  119. if (txBufferLength >= BUFFER_LENGTH) {
  120. setWriteError();
  121. return 0;
  122. }
  123. // put byte in tx buffer
  124. txBuffer[txBufferIndex] = data;
  125. ++txBufferIndex;
  126. // update amount in buffer
  127. txBufferLength = txBufferIndex;
  128. return 1;
  129. } else {
  130. return write(&data, 1);
  131. }
  132. }
  133. size_t TwoWire::write(const uint8_t *data, size_t quantity)
  134. {
  135. if (transmitting) {
  136. // in master transmitter mode
  137. for (size_t i = 0; i < quantity; ++i) {
  138. write(data[i]);
  139. }
  140. } else {
  141. uint8_t rc = bcm2835_i2c_write(reinterpret_cast<const char *>(data), quantity);
  142. if (rc != BCM2835_I2C_REASON_OK) {
  143. return 0;
  144. }
  145. }
  146. return quantity;
  147. }
  148. int TwoWire::available()
  149. {
  150. return rxBufferLength - rxBufferIndex;
  151. }
  152. int TwoWire::read()
  153. {
  154. int value = -1;
  155. if (rxBufferIndex < rxBufferLength) {
  156. value = rxBuffer[rxBufferIndex];
  157. ++rxBufferIndex;
  158. }
  159. return value;
  160. }
  161. int TwoWire::peek()
  162. {
  163. if (rxBufferIndex < rxBufferLength) {
  164. return rxBuffer[rxBufferIndex];
  165. }
  166. return -1;
  167. }
  168. void TwoWire::flush()
  169. {
  170. rxBufferIndex = 0;
  171. rxBufferLength = 0;
  172. txBufferIndex = 0;
  173. txBufferLength = 0;
  174. }
  175. TwoWire Wire = TwoWire();