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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. //Read a number of words from startAddress. Store into Data array.
  20. //Returns 0 if successful, -1 if error
  21. int MLX90640_I2CRead(uint8_t _deviceAddress, unsigned int startAddress, unsigned int nWordsRead, uint16_t *data)
  22. {
  23. //Caller passes number of 'unsigned ints to read', increase this to 'bytes to read'
  24. uint16_t bytesRemaining = nWordsRead * 2;
  25. //It doesn't look like sequential read works. Do we need to re-issue the address command each time?
  26. uint16_t dataSpot = 0; //Start at beginning of array
  27. //Setup a series of chunked I2C_BUFFER_LENGTH byte reads
  28. while (bytesRemaining > 0)
  29. {
  30. Wire.beginTransmission(_deviceAddress);
  31. Wire.write(startAddress >> 8); //MSB
  32. Wire.write(startAddress & 0xFF); //LSB
  33. Wire.endTransmission(false)
  34. uint16_t numberOfBytesToRead = bytesRemaining;
  35. if (numberOfBytesToRead > I2C_BUFFER_LENGTH) numberOfBytesToRead = I2C_BUFFER_LENGTH;
  36. Wire.requestFrom((uint8_t)_deviceAddress, numberOfBytesToRead);
  37. if (Wire.available())
  38. {
  39. for (uint16_t x = 0 ; x < numberOfBytesToRead / 2; x++)
  40. {
  41. //Store data into array
  42. data[dataSpot] = Wire.read() << 8; //MSB
  43. data[dataSpot] |= Wire.read(); //LSB
  44. dataSpot++;
  45. }
  46. }
  47. bytesRemaining -= numberOfBytesToRead;
  48. startAddress += numberOfBytesToRead / 2;
  49. }
  50. return (0); //Success
  51. }
  52. //Set I2C Freq, in kHz
  53. //MLX90640_I2CFreqSet(1000) sets frequency to 1MHz
  54. void MLX90640_I2CFreqSet(int freq)
  55. {
  56. //i2c.frequency(1000 * freq);
  57. Wire.setClock((long)1000 * freq);
  58. }
  59. //Write two bytes to a two byte address
  60. int MLX90640_I2CWrite(uint8_t _deviceAddress, unsigned int writeAddress, uint16_t data)
  61. {
  62. Wire.beginTransmission((uint8_t)_deviceAddress);
  63. Wire.write(writeAddress >> 8); //MSB
  64. Wire.write(writeAddress & 0xFF); //LSB
  65. Wire.write(data >> 8); //MSB
  66. Wire.write(data & 0xFF); //LSB
  67. if (Wire.endTransmission() != 0)
  68. {
  69. //Sensor did not ACK
  70. Serial.println("Error: Sensor did not ack");
  71. return (-1);
  72. }
  73. uint16_t dataCheck;
  74. MLX90640_I2CRead(_deviceAddress, writeAddress, 1, &dataCheck);
  75. if (dataCheck != data)
  76. {
  77. //Serial.println("The write request didn't stick");
  78. return -2;
  79. }
  80. return (0); //Success
  81. }