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.

MLX90640_I2C_Driver.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. @copyright (C) 2017 Melexis N.V.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <Arduino.h>
  14. #include <Wire.h>
  15. #include "MLX90640_I2C_Driver.h"
  16. void MLX90640_I2CInit()
  17. {
  18. }
  19. int MLX90640_I2CRead(uint8_t _deviceAddress, unsigned int startAddress, unsigned int nWordsRead, uint16_t *data)
  20. {
  21. // nWordsRead must be <= 32767
  22. Wire.beginTransmission(_deviceAddress);
  23. Wire.write(startAddress >> 8); //MSB
  24. Wire.write(startAddress & 0xFF); //LSB
  25. Wire.endTransmission(false);
  26. i2c_err_t error = Wire.readTransmission(_deviceAddress, (uint8_t*) data, nWordsRead*2);
  27. if(error != 0){//problems
  28. Serial.printf("Block read from sensor(0x%02X) at address=%d of %d uint16_t's failed=%d(%s)\n",
  29. _deviceAddress,startAddress,nWordsRead,error,Wire.getErrorText(error));
  30. }
  31. else { // reverse byte order, sensor Big Endian, ESP32 Little Endian
  32. for(auto a = 0; a<nWordsRead; a++){
  33. data[a] = ((data[a] & 0xff)<<8) | (( data[a]>>8)&0xff);
  34. }
  35. }
  36. return 0;
  37. }
  38. /*
  39. //Read a number of words from startAddress. Store into Data array.
  40. //Returns 0 if successful, -1 if error
  41. int MLX90640_I2CRead(uint8_t _deviceAddress, unsigned int startAddress, unsigned int nWordsRead, uint16_t *data)
  42. {
  43. //Caller passes number of 'unsigned ints to read', increase this to 'bytes to read'
  44. uint16_t bytesRemaining = nWordsRead * 2;
  45. //It doesn't look like sequential read works. Do we need to re-issue the address command each time?
  46. uint16_t dataSpot = 0; //Start at beginning of array
  47. //Setup a series of chunked I2C_BUFFER_LENGTH byte reads
  48. while (bytesRemaining > 0)
  49. {
  50. Wire.beginTransmission(_deviceAddress);
  51. Wire.write(startAddress >> 8); //MSB
  52. Wire.write(startAddress & 0xFF); //LSB
  53. if(Wire.endTransmission(false) != 7){
  54. Serial.println("Error: Sensor did not ack");
  55. return (0);//Sensor did not ACK
  56. }
  57. uint16_t numberOfBytesToRead = bytesRemaining;
  58. if (numberOfBytesToRead > I2C_BUFFER_LENGTH) numberOfBytesToRead = I2C_BUFFER_LENGTH;
  59. Wire.requestFrom((uint8_t)_deviceAddress, numberOfBytesToRead);
  60. if (Wire.available())
  61. {
  62. for (uint16_t x = 0 ; x < numberOfBytesToRead / 2; x++)
  63. {
  64. //Store data into array
  65. data[dataSpot] = Wire.read() << 8; //MSB
  66. data[dataSpot] |= Wire.read(); //LSB
  67. dataSpot++;
  68. }
  69. }
  70. bytesRemaining -= numberOfBytesToRead;
  71. startAddress += numberOfBytesToRead / 2;
  72. }
  73. return (0); //Success
  74. }
  75. */
  76. //Set I2C Freq, in kHz
  77. //MLX90640_I2CFreqSet(1000) sets frequency to 1MHz
  78. void MLX90640_I2CFreqSet(int freq)
  79. {
  80. //i2c.frequency(1000 * freq);
  81. Wire.setClock((long)1000 * freq);
  82. }
  83. //Write two bytes to a two byte address
  84. int MLX90640_I2CWrite(uint8_t _deviceAddress, unsigned int writeAddress, uint16_t data)
  85. {
  86. Wire.beginTransmission((uint8_t)_deviceAddress);
  87. Wire.write(writeAddress >> 8); //MSB
  88. Wire.write(writeAddress & 0xFF); //LSB
  89. Wire.write(data >> 8); //MSB
  90. Wire.write(data & 0xFF); //LSB
  91. if (Wire.endTransmission() != 0)
  92. {
  93. //Sensor did not ACK
  94. Serial.println("Error: Sensor did not ack");
  95. return (0);//Sensor did not ACK
  96. }
  97. uint16_t dataCheck;
  98. MLX90640_I2CRead(_deviceAddress, writeAddress, 1, &dataCheck);
  99. if (dataCheck != data)
  100. {
  101. //Serial.println("The write request didn't stick");
  102. return -2;
  103. }
  104. return (0); //Success
  105. }