Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

142 řádky
4.8KB

  1. // I2Cdev library collection - iAQ-2000 I2C device class
  2. // Based on AppliedSensor iAQ-2000 Interface Description, Version PA1, 2009
  3. // 2012-04-01 by Peteris Skorovs <pskorovs@gmail.com>
  4. //
  5. // This I2C device library is using (and submitted as a part of) Jeff Rowberg's I2Cdevlib library,
  6. // which should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
  7. //
  8. // Changelog:
  9. // 2012-04-01 - initial release
  10. // 2012-11-08 - added TVoc and Status
  11. /* ============================================
  12. I2Cdev device library code is placed under the MIT license
  13. Copyright (c) 2012 Peteris Skorovs, Jeff Rowberg
  14. Permission is hereby granted, free of charge, to any person obtaining a copy
  15. of this software and associated documentation files (the "Software"), to deal
  16. in the Software without restriction, including without limitation the rights
  17. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. copies of the Software, and to permit persons to whom the Software is
  19. furnished to do so, subject to the following conditions:
  20. The above copyright notice and this permission notice shall be included in
  21. all copies or substantial portions of the Software.
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. THE SOFTWARE.
  29. ===============================================
  30. */
  31. #include "IAQ2000.h"
  32. /** Default constructor, uses default I2C address.
  33. * @see IAQ2000_DEFAULT_ADDRESS
  34. */
  35. IAQ2000::IAQ2000() {
  36. devAddr = IAQ2000_DEFAULT_ADDRESS;
  37. }
  38. /** Specific address constructor.
  39. * @param address I2C address
  40. * @see IAQ2000_DEFAULT_ADDRESS
  41. * @see IAQ2000_ADDRESS
  42. */
  43. IAQ2000::IAQ2000(uint8_t address) {
  44. devAddr = address;
  45. }
  46. /** Power on and prepare for general usage.
  47. */
  48. void IAQ2000::initialize() {
  49. // Nothing is required, but
  50. // the method should exist anyway.
  51. }
  52. /** Very primitive method o verify the I2C connection.
  53. * Make sure the device is connected and responds as expected.
  54. * @return True if connection is valid, false otherwise
  55. */
  56. bool IAQ2000::testConnection() {
  57. if (getIaqpred() >= 450) {
  58. return true;
  59. } else {
  60. return false;
  61. }
  62. }
  63. /** Read iAQ-2000 indoor air quality sensor.
  64. * @return Predicted CO2 concentration based on human induced volatile organic compounds (VOC) detection (in ppm VOC + CO2 equivalents)
  65. */
  66. uint16_t IAQ2000::getIaqpred() {
  67. // read bytes from the DATA0 AND DATA1 registers and bit-shifting them into a 16-bit value
  68. readAllBytes(devAddr, 2, buffer);
  69. return ((buffer[0] << 8) | buffer[1]);
  70. }
  71. uint8_t IAQ2000::getIaqstatus() {
  72. // read bytes from the DATA2 register
  73. readAllBytes(devAddr, 3, buffer);
  74. return (buffer[2]);
  75. }
  76. uint16_t IAQ2000::getIaqtvoc() {
  77. // read bytes from the DATA7 AND DATA8 registers and bit-shifting them into a 16-bit value
  78. readAllBytes(devAddr, 9, buffer);
  79. return ((buffer[7] << 8) | buffer[8]);
  80. }
  81. /** Read bytes from a slave device.
  82. * This is a "stripped-down" version of the standard Jeff Rowberg's I2Cdev::readBytes method
  83. * intended to provide compatibility with iAQ-2000,
  84. * which apparently does not support setting of an address pointer to indicate from which position is to start read from.
  85. * @param devAddr Address of the slave device to read bytes from
  86. * @param length Number of bytes to read
  87. * @param data Buffer to store read data in
  88. * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
  89. * @return Number of bytes read (0 indicates failure)
  90. */
  91. int8_t IAQ2000::readAllBytes(uint8_t devAddr, uint8_t length, uint8_t *data,
  92. uint16_t timeout) {
  93. #ifdef I2CDEV_SERIAL_DEBUG
  94. Serial.print("I2C (0x");
  95. Serial.print(devAddr, HEX);
  96. Serial.print(") reading ");
  97. Serial.print(length, DEC);
  98. Serial.print(" bytes...");
  99. #endif
  100. int8_t count = 0;
  101. Wire.requestFrom(devAddr, length);
  102. uint32_t t1 = millis();
  103. for (; Wire.available() && (timeout == 0 || millis() - t1 < timeout);
  104. count++) {
  105. #if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE)
  106. data[count] = Wire.receive();
  107. #elif (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100)
  108. data[count] = Wire.read();
  109. #endif
  110. #ifdef I2CDEV_SERIAL_DEBUG
  111. Serial.print(data[count], HEX);
  112. if (count + 1 < length) Serial.print(" ");
  113. #endif
  114. }
  115. if (timeout > 0 && millis() - t1 >= timeout && count < length)
  116. count = -1; // timeout
  117. #ifdef I2CDEV_SERIAL_DEBUG
  118. Serial.print(". Done (");
  119. Serial.print(count, DEC);
  120. Serial.println(" read).");
  121. #endif
  122. return count;
  123. }