diff --git a/.cproject b/.cproject index 682b26d..51685f7 100644 --- a/.cproject +++ b/.cproject @@ -27,8 +27,8 @@ - + @@ -42,8 +42,8 @@ - + @@ -57,8 +57,8 @@ - + @@ -84,4 +84,5 @@ + diff --git a/.project b/.project index 6437264..1aa4d96 100644 --- a/.project +++ b/.project @@ -66,6 +66,11 @@ 2 ECLIPSE_HOME/arduinoPlugin/libraries/Ethernet/2.0.0 + + libraries/PubSubClient + 2 + ECLIPSE_HOME/arduinoPlugin/libraries/PubSubClient/2.7.0 + libraries/SPI 2 diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index d6beb5d..1414163 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -4,7 +4,7 @@ - + diff --git a/.settings/org.eclipse.cdt.core.prefs b/.settings/org.eclipse.cdt.core.prefs index 2d298b7..120447a 100644 --- a/.settings/org.eclipse.cdt.core.prefs +++ b/.settings/org.eclipse.cdt.core.prefs @@ -487,7 +487,7 @@ environment/project/io.sloeber.core.toolChain.release.662097424/JANTJE.SELECTED. environment/project/io.sloeber.core.toolChain.release.662097424/JANTJE.SELECTED.PLATFORM/value=${eclipse_home}/arduinoPlugin/packages/arduino/hardware/avr/1.8.2 environment/project/io.sloeber.core.toolChain.release.662097424/JANTJE.SIZE.SWITCH/delimiter=; environment/project/io.sloeber.core.toolChain.release.662097424/JANTJE.SIZE.SWITCH/operation=replace -environment/project/io.sloeber.core.toolChain.release.662097424/JANTJE.SIZE.SWITCH/value=${A.RECIPE.SIZE.PATTERN} +environment/project/io.sloeber.core.toolChain.release.662097424/JANTJE.SIZE.SWITCH/value=${A.ALT_SIZE_COMMAND} environment/project/io.sloeber.core.toolChain.release.662097424/JANTJE.UPLOAD/delimiter=; environment/project/io.sloeber.core.toolChain.release.662097424/JANTJE.UPLOAD/operation=replace environment/project/io.sloeber.core.toolChain.release.662097424/JANTJE.UPLOAD/value=Default diff --git a/I2Cdev/I2Cdev.cpp b/I2Cdev/I2Cdev.cpp index 5525624..e845f3b 100644 --- a/I2Cdev/I2Cdev.cpp +++ b/I2Cdev/I2Cdev.cpp @@ -20,35 +20,35 @@ // 2011-07-28 - initial release /* ============================================ -I2Cdev device library code is placed under the MIT license -Copyright (c) 2013 Jeff Rowberg - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -=============================================== -*/ + I2Cdev device library code is placed under the MIT license + Copyright (c) 2013 Jeff Rowberg + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + =============================================== + */ #include "I2Cdev.h" #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE - #ifdef I2CDEV_IMPLEMENTATION_WARNINGS - #if ARDUINO < 100 +#ifdef I2CDEV_IMPLEMENTATION_WARNINGS +#if ARDUINO < 100 #warning Using outdated Arduino IDE with Wire library is functionally limiting. #warning Arduino IDE v1.6.5+ with I2Cdev Fastwire implementation is recommended. #warning This I2Cdev implementation does not support: @@ -61,12 +61,12 @@ THE SOFTWARE. #warning - Repeated starts conditions #warning - Timeout detection (some Wire requests block forever) #elif ARDUINO > 100 - /*#warning Using current Arduino IDE with Wire library is functionally limiting. - #warning Arduino IDE v1.6.5+ with I2CDEV_BUILTIN_FASTWIRE implementation is recommended. - #warning This I2Cdev implementation does not support: - #warning - Timeout detection (some Wire requests block forever)*/ - #endif - #endif +/*#warning Using current Arduino IDE with Wire library is functionally limiting. + #warning Arduino IDE v1.6.5+ with I2CDEV_BUILTIN_FASTWIRE implementation is recommended. + #warning This I2Cdev implementation does not support: + #warning - Timeout detection (some Wire requests block forever)*/ +#endif +#endif #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE @@ -105,11 +105,12 @@ I2Cdev::I2Cdev() { * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Status of read operation (true = success) */ -int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout) { - uint8_t b; - uint8_t count = readByte(devAddr, regAddr, &b, timeout); - *data = b & (1 << bitNum); - return count; +int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, + uint8_t *data, uint16_t timeout) { + uint8_t b; + uint8_t count = readByte(devAddr, regAddr, &b, timeout); + *data = b & (1 << bitNum); + return count; } /** Read a single bit from a 16-bit device register. @@ -120,11 +121,12 @@ int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Status of read operation (true = success) */ -int8_t I2Cdev::readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout) { - uint16_t b; - uint8_t count = readWord(devAddr, regAddr, &b, timeout); - *data = b & (1 << bitNum); - return count; +int8_t I2Cdev::readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, + uint16_t *data, uint16_t timeout) { + uint16_t b; + uint8_t count = readWord(devAddr, regAddr, &b, timeout); + *data = b & (1 << bitNum); + return count; } /** Read multiple bits from an 8-bit device register. @@ -136,20 +138,21 @@ int8_t I2Cdev::readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Status of read operation (true = success) */ -int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout) { - // 01101001 read byte - // 76543210 bit numbers - // xxx args: bitStart=4, length=3 - // 010 masked - // -> 010 shifted - uint8_t count, b; - if ((count = readByte(devAddr, regAddr, &b, timeout)) != 0) { - uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); - b &= mask; - b >>= (bitStart - length + 1); - *data = b; - } - return count; +int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, + uint8_t length, uint8_t *data, uint16_t timeout) { + // 01101001 read byte + // 76543210 bit numbers + // xxx args: bitStart=4, length=3 + // 010 masked + // -> 010 shifted + uint8_t count, b; + if ((count = readByte(devAddr, regAddr, &b, timeout)) != 0) { + uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); + b &= mask; + b >>= (bitStart - length + 1); + *data = b; + } + return count; } /** Read multiple bits from a 16-bit device register. @@ -161,21 +164,22 @@ int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Status of read operation (1 = success, 0 = failure, -1 = timeout) */ -int8_t I2Cdev::readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout) { - // 1101011001101001 read byte - // fedcba9876543210 bit numbers - // xxx args: bitStart=12, length=3 - // 010 masked - // -> 010 shifted - uint8_t count; - uint16_t w; - if ((count = readWord(devAddr, regAddr, &w, timeout)) != 0) { - uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1); - w &= mask; - w >>= (bitStart - length + 1); - *data = w; - } - return count; +int8_t I2Cdev::readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, + uint8_t length, uint16_t *data, uint16_t timeout) { + // 1101011001101001 read byte + // fedcba9876543210 bit numbers + // xxx args: bitStart=12, length=3 + // 010 masked + // -> 010 shifted + uint8_t count; + uint16_t w; + if ((count = readWord(devAddr, regAddr, &w, timeout)) != 0) { + uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1); + w &= mask; + w >>= (bitStart - length + 1); + *data = w; + } + return count; } /** Read single byte from an 8-bit device register. @@ -185,8 +189,9 @@ int8_t I2Cdev::readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uin * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Status of read operation (true = success) */ -int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout) { - return readBytes(devAddr, regAddr, 1, data, timeout); +int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, + uint16_t timeout) { + return readBytes(devAddr, regAddr, 1, data, timeout); } /** Read single word from a 16-bit device register. @@ -196,8 +201,9 @@ int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_ * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Status of read operation (true = success) */ -int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout) { - return readWords(devAddr, regAddr, 1, data, timeout); +int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, + uint16_t timeout) { + return readWords(devAddr, regAddr, 1, data, timeout); } /** Read multiple bytes from an 8-bit device register. @@ -208,8 +214,9 @@ int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Number of bytes read (-1 indicates failure) */ -int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout) { - #ifdef I2CDEV_SERIAL_DEBUG +int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, + uint8_t *data, uint16_t timeout) { +#ifdef I2CDEV_SERIAL_DEBUG Serial.print("I2C (0x"); Serial.print(devAddr, HEX); Serial.print(") reading "); @@ -219,12 +226,12 @@ int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8 Serial.print("..."); #endif - int8_t count = 0; - uint32_t t1 = millis(); + int8_t count = 0; + uint32_t t1 = millis(); - #if (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE) +#if (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE) - #if (ARDUINO < 100) +#if (ARDUINO < 100) // Arduino v00xx (before v1.0), Wire library // I2C/TWI subsystem uses internal buffer that breaks with large data requests @@ -272,30 +279,31 @@ int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8 Wire.endTransmission(); } #elif (ARDUINO > 100) - // Arduino v1.0.1+, Wire library - // Adds official support for repeated start condition, yay! - - // I2C/TWI subsystem uses internal buffer that breaks with large data requests - // so if user requests more than BUFFER_LENGTH bytes, we have to do it in - // smaller chunks instead of all at once - for (uint8_t k = 0; k < length; k += min((int)length, BUFFER_LENGTH)) { - Wire.beginTransmission(devAddr); - Wire.write(regAddr); - Wire.endTransmission(); - Wire.beginTransmission(devAddr); - Wire.requestFrom(devAddr, (uint8_t)min(length - k, BUFFER_LENGTH)); - - for (; Wire.available() && (timeout == 0 || millis() - t1 < timeout); count++) { - data[count] = Wire.read(); - #ifdef I2CDEV_SERIAL_DEBUG + // Arduino v1.0.1+, Wire library + // Adds official support for repeated start condition, yay! + + // I2C/TWI subsystem uses internal buffer that breaks with large data requests + // so if user requests more than BUFFER_LENGTH bytes, we have to do it in + // smaller chunks instead of all at once + for (uint8_t k = 0; k < length; k += min((int )length, BUFFER_LENGTH)) { + Wire.beginTransmission(devAddr); + Wire.write(regAddr); + Wire.endTransmission(); + Wire.beginTransmission(devAddr); + Wire.requestFrom(devAddr, (uint8_t) min(length - k, BUFFER_LENGTH)); + + for (; Wire.available() && (timeout == 0 || millis() - t1 < timeout); + count++) { + data[count] = Wire.read(); +#ifdef I2CDEV_SERIAL_DEBUG Serial.print(data[count], HEX); if (count + 1 < length) Serial.print(" "); #endif - } - } - #endif + } + } +#endif - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) +#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) // Fastwire library // no loop required for fastwire @@ -308,16 +316,17 @@ int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8 #endif - // check for timeout - if (timeout > 0 && millis() - t1 >= timeout && count < length) count = -1; // timeout + // check for timeout + if (timeout > 0 && millis() - t1 >= timeout && count < length) + count = -1; // timeout - #ifdef I2CDEV_SERIAL_DEBUG +#ifdef I2CDEV_SERIAL_DEBUG Serial.print(". Done ("); Serial.print(count, DEC); Serial.println(" read)."); #endif - return count; + return count; } /** Read multiple words from a 16-bit device register. @@ -328,8 +337,9 @@ int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Number of words read (-1 indicates failure) */ -int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout) { - #ifdef I2CDEV_SERIAL_DEBUG +int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, + uint16_t *data, uint16_t timeout) { +#ifdef I2CDEV_SERIAL_DEBUG Serial.print("I2C (0x"); Serial.print(devAddr, HEX); Serial.print(") reading "); @@ -339,12 +349,12 @@ int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint1 Serial.print("..."); #endif - int8_t count = 0; - uint32_t t1 = millis(); + int8_t count = 0; + uint32_t t1 = millis(); #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE - #if (ARDUINO < 100) +#if (ARDUINO < 100) // Arduino v00xx (before v1.0), Wire library // I2C/TWI subsystem uses internal buffer that breaks with large data requests @@ -410,41 +420,43 @@ int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint1 Wire.endTransmission(); } #elif (ARDUINO > 100) - // Arduino v1.0.1+, Wire library - // Adds official support for repeated start condition, yay! - - // I2C/TWI subsystem uses internal buffer that breaks with large data requests - // so if user requests more than BUFFER_LENGTH bytes, we have to do it in - // smaller chunks instead of all at once - for (uint8_t k = 0; k < length * 2; k += min(length * 2, BUFFER_LENGTH)) { - Wire.beginTransmission(devAddr); - Wire.write(regAddr); - Wire.endTransmission(); - Wire.beginTransmission(devAddr); - Wire.requestFrom(devAddr, (uint8_t)(length * 2)); // length=words, this wants bytes - - bool msb = true; // starts with MSB, then LSB - for (; Wire.available() && count < length && (timeout == 0 || millis() - t1 < timeout);) { - if (msb) { - // first byte is bits 15-8 (MSb=15) - data[count] = Wire.read() << 8; - } else { - // second byte is bits 7-0 (LSb=0) - data[count] |= Wire.read(); - #ifdef I2CDEV_SERIAL_DEBUG + // Arduino v1.0.1+, Wire library + // Adds official support for repeated start condition, yay! + + // I2C/TWI subsystem uses internal buffer that breaks with large data requests + // so if user requests more than BUFFER_LENGTH bytes, we have to do it in + // smaller chunks instead of all at once + for (uint8_t k = 0; k < length * 2; k += min(length * 2, BUFFER_LENGTH)) { + Wire.beginTransmission(devAddr); + Wire.write(regAddr); + Wire.endTransmission(); + Wire.beginTransmission(devAddr); + Wire.requestFrom(devAddr, (uint8_t) (length * 2)); // length=words, this wants bytes + + bool msb = true; // starts with MSB, then LSB + for (; + Wire.available() && count < length + && (timeout == 0 || millis() - t1 < timeout);) { + if (msb) { + // first byte is bits 15-8 (MSb=15) + data[count] = Wire.read() << 8; + } else { + // second byte is bits 7-0 (LSb=0) + data[count] |= Wire.read(); +#ifdef I2CDEV_SERIAL_DEBUG Serial.print(data[count], HEX); if (count + 1 < length) Serial.print(" "); #endif - count++; - } - msb = !msb; - } - - Wire.endTransmission(); - } - #endif + count++; + } + msb = !msb; + } + + Wire.endTransmission(); + } +#endif - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) +#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) // Fastwire library // no loop required for fastwire @@ -461,15 +473,16 @@ int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint1 #endif - if (timeout > 0 && millis() - t1 >= timeout && count < length) count = -1; // timeout + if (timeout > 0 && millis() - t1 >= timeout && count < length) + count = -1; // timeout - #ifdef I2CDEV_SERIAL_DEBUG +#ifdef I2CDEV_SERIAL_DEBUG Serial.print(". Done ("); Serial.print(count, DEC); Serial.println(" read)."); #endif - - return count; + + return count; } /** write a single bit in an 8-bit device register. @@ -479,11 +492,12 @@ int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint1 * @param value New bit value to write * @return Status of operation (true = success) */ -bool I2Cdev::writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data) { - uint8_t b; - readByte(devAddr, regAddr, &b); - b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum)); - return writeByte(devAddr, regAddr, b); +bool I2Cdev::writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, + uint8_t data) { + uint8_t b; + readByte(devAddr, regAddr, &b); + b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum)); + return writeByte(devAddr, regAddr, b); } /** write a single bit in a 16-bit device register. @@ -493,11 +507,12 @@ bool I2Cdev::writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t * @param value New bit value to write * @return Status of operation (true = success) */ -bool I2Cdev::writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data) { - uint16_t w; - readWord(devAddr, regAddr, &w); - w = (data != 0) ? (w | (1 << bitNum)) : (w & ~(1 << bitNum)); - return writeWord(devAddr, regAddr, w); +bool I2Cdev::writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, + uint16_t data) { + uint16_t w; + readWord(devAddr, regAddr, &w); + w = (data != 0) ? (w | (1 << bitNum)) : (w & ~(1 << bitNum)); + return writeWord(devAddr, regAddr, w); } /** Write multiple bits in an 8-bit device register. @@ -508,25 +523,26 @@ bool I2Cdev::writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_ * @param data Right-aligned value to write * @return Status of operation (true = success) */ -bool I2Cdev::writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data) { - // 010 value to write - // 76543210 bit numbers - // xxx args: bitStart=4, length=3 - // 00011100 mask byte - // 10101111 original value (sample) - // 10100011 original & ~mask - // 10101011 masked | value - uint8_t b; - if (readByte(devAddr, regAddr, &b) != 0) { - uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); - data <<= (bitStart - length + 1); // shift data into correct position - data &= mask; // zero all non-important bits in data - b &= ~(mask); // zero all important bits in existing byte - b |= data; // combine data with existing byte - return writeByte(devAddr, regAddr, b); - } else { - return false; - } +bool I2Cdev::writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, + uint8_t length, uint8_t data) { + // 010 value to write + // 76543210 bit numbers + // xxx args: bitStart=4, length=3 + // 00011100 mask byte + // 10101111 original value (sample) + // 10100011 original & ~mask + // 10101011 masked | value + uint8_t b; + if (readByte(devAddr, regAddr, &b) != 0) { + uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); + data <<= (bitStart - length + 1); // shift data into correct position + data &= mask; // zero all non-important bits in data + b &= ~(mask); // zero all important bits in existing byte + b |= data; // combine data with existing byte + return writeByte(devAddr, regAddr, b); + } else { + return false; + } } /** Write multiple bits in a 16-bit device register. @@ -537,25 +553,26 @@ bool I2Cdev::writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8 * @param data Right-aligned value to write * @return Status of operation (true = success) */ -bool I2Cdev::writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data) { - // 010 value to write - // fedcba9876543210 bit numbers - // xxx args: bitStart=12, length=3 - // 0001110000000000 mask word - // 1010111110010110 original value (sample) - // 1010001110010110 original & ~mask - // 1010101110010110 masked | value - uint16_t w; - if (readWord(devAddr, regAddr, &w) != 0) { - uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1); - data <<= (bitStart - length + 1); // shift data into correct position - data &= mask; // zero all non-important bits in data - w &= ~(mask); // zero all important bits in existing word - w |= data; // combine data with existing word - return writeWord(devAddr, regAddr, w); - } else { - return false; - } +bool I2Cdev::writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, + uint8_t length, uint16_t data) { + // 010 value to write + // fedcba9876543210 bit numbers + // xxx args: bitStart=12, length=3 + // 0001110000000000 mask word + // 1010111110010110 original value (sample) + // 1010001110010110 original & ~mask + // 1010101110010110 masked | value + uint16_t w; + if (readWord(devAddr, regAddr, &w) != 0) { + uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1); + data <<= (bitStart - length + 1); // shift data into correct position + data &= mask; // zero all non-important bits in data + w &= ~(mask); // zero all important bits in existing word + w |= data; // combine data with existing word + return writeWord(devAddr, regAddr, w); + } else { + return false; + } } /** Write single byte to an 8-bit device register. @@ -565,7 +582,7 @@ bool I2Cdev::writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint * @return Status of operation (true = success) */ bool I2Cdev::writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data) { - return writeBytes(devAddr, regAddr, 1, &data); + return writeBytes(devAddr, regAddr, 1, &data); } /** Write single word to a 16-bit device register. @@ -575,7 +592,7 @@ bool I2Cdev::writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data) { * @return Status of operation (true = success) */ bool I2Cdev::writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data) { - return writeWords(devAddr, regAddr, 1, &data); + return writeWords(devAddr, regAddr, 1, &data); } /** Write multiple bytes to an 8-bit device register. @@ -585,8 +602,9 @@ bool I2Cdev::writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data) { * @param data Buffer to copy new data from * @return Status of operation (true = success) */ -bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data) { - #ifdef I2CDEV_SERIAL_DEBUG +bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, + uint8_t *data) { +#ifdef I2CDEV_SERIAL_DEBUG Serial.print("I2C (0x"); Serial.print(devAddr, HEX); Serial.print(") writing "); @@ -595,48 +613,48 @@ bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_ Serial.print(regAddr, HEX); Serial.print("..."); #endif - uint8_t status = 0; - #if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE) + uint8_t status = 0; +#if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE) Wire.beginTransmission(devAddr); Wire.send((uint8_t) regAddr); // send address #elif ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100) \ || (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE && ARDUINO >= 100) \ || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE) - Wire.beginTransmission(devAddr); - Wire.write((uint8_t) regAddr); // send address - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) + Wire.beginTransmission(devAddr); + Wire.write((uint8_t) regAddr); // send address +#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) Fastwire::beginTransmission(devAddr); Fastwire::write(regAddr); #endif - for (uint8_t i = 0; i < length; i++) { - #ifdef I2CDEV_SERIAL_DEBUG + for (uint8_t i = 0; i < length; i++) { +#ifdef I2CDEV_SERIAL_DEBUG Serial.print(data[i], HEX); if (i + 1 < length) Serial.print(" "); #endif - #if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE) +#if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE) Wire.send((uint8_t) data[i]); #elif ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100) \ || (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE && ARDUINO >= 100) \ || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE) - Wire.write((uint8_t) data[i]); - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) + Wire.write((uint8_t) data[i]); +#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) Fastwire::write((uint8_t) data[i]); #endif - } - #if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE) + } +#if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE) Wire.endTransmission(); #elif ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100) \ || (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE && ARDUINO >= 100) \ || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE) - status = Wire.endTransmission(); - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) + status = Wire.endTransmission(); +#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) Fastwire::stop(); //status = Fastwire::endTransmission(); #endif - #ifdef I2CDEV_SERIAL_DEBUG +#ifdef I2CDEV_SERIAL_DEBUG Serial.println(". Done."); #endif - return status == 0; + return status == 0; } /** Write multiple words to a 16-bit device register. @@ -646,8 +664,9 @@ bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_ * @param data Buffer to copy new data from * @return Status of operation (true = success) */ -bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t* data) { - #ifdef I2CDEV_SERIAL_DEBUG +bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, + uint16_t *data) { +#ifdef I2CDEV_SERIAL_DEBUG Serial.print("I2C (0x"); Serial.print(devAddr, HEX); Serial.print(") writing "); @@ -656,52 +675,52 @@ bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16 Serial.print(regAddr, HEX); Serial.print("..."); #endif - uint8_t status = 0; - #if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE) + uint8_t status = 0; +#if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE) Wire.beginTransmission(devAddr); Wire.send(regAddr); // send address #elif ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100) \ || (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE && ARDUINO >= 100) \ || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE) - Wire.beginTransmission(devAddr); - Wire.write(regAddr); // send address - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) + Wire.beginTransmission(devAddr); + Wire.write(regAddr); // send address +#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) Fastwire::beginTransmission(devAddr); Fastwire::write(regAddr); #endif - for (uint8_t i = 0; i < length; i++) { - #ifdef I2CDEV_SERIAL_DEBUG + for (uint8_t i = 0; i < length; i++) { +#ifdef I2CDEV_SERIAL_DEBUG Serial.print(data[i], HEX); if (i + 1 < length) Serial.print(" "); #endif - #if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE) +#if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE) Wire.send((uint8_t)(data[i] >> 8)); // send MSB Wire.send((uint8_t)data[i]); // send LSB #elif ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100) \ || (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE && ARDUINO >= 100) \ || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE) - Wire.write((uint8_t)(data[i] >> 8)); // send MSB - Wire.write((uint8_t)data[i]); // send LSB - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) + Wire.write((uint8_t) (data[i] >> 8)); // send MSB + Wire.write((uint8_t) data[i]); // send LSB +#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) Fastwire::write((uint8_t)(data[i] >> 8)); // send MSB status = Fastwire::write((uint8_t)data[i]); // send LSB if (status != 0) break; #endif - } - #if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE) + } +#if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE) Wire.endTransmission(); #elif ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100) \ || (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE && ARDUINO >= 100) \ || I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE) - status = Wire.endTransmission(); - #elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) + status = Wire.endTransmission(); +#elif (I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE) Fastwire::stop(); //status = Fastwire::endTransmission(); #endif - #ifdef I2CDEV_SERIAL_DEBUG +#ifdef I2CDEV_SERIAL_DEBUG Serial.println(". Done."); #endif - return status == 0; + return status == 0; } /** Default timeout value for read operations. diff --git a/I2Cdev/I2Cdev.h b/I2Cdev/I2Cdev.h index 60c3195..42e3f86 100644 --- a/I2Cdev/I2Cdev.h +++ b/I2Cdev/I2Cdev.h @@ -22,28 +22,28 @@ // 2011-07-28 - initial release /* ============================================ -I2Cdev device library code is placed under the MIT license -Copyright (c) 2013 Jeff Rowberg - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -=============================================== -*/ + I2Cdev device library code is placed under the MIT license + Copyright (c) 2013 Jeff Rowberg + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + =============================================== + */ #ifndef _I2CDEV_H_ #define _I2CDEV_H_ @@ -67,7 +67,7 @@ THE SOFTWARE. // ----------------------------------------------------------------------------- #define I2CDEV_ARDUINO_WIRE 1 // Wire object from Arduino #define I2CDEV_BUILTIN_NBWIRE 2 // Tweaked Wire object from Gene Knight's NBWire project - // ^^^ NBWire implementation is still buggy w/some interrupts! +// ^^^ NBWire implementation is still buggy w/some interrupts! #define I2CDEV_BUILTIN_FASTWIRE 3 // FastWire object from Francesco Ferrara's project #define I2CDEV_I2CMASTER_LIBRARY 4 // I2C object from DSSCircuits I2C-Master Library at https://github.com/DSSCircuits/I2C-Master-Library #define I2CDEV_BUILTIN_SBWIRE 5 // I2C object from Shuning (Steve) Bian's SBWire Library at https://github.com/freespace/SBWire @@ -79,21 +79,21 @@ THE SOFTWARE. //#define I2CDEV_SERIAL_DEBUG #ifdef ARDUINO - #if ARDUINO < 100 +#if ARDUINO < 100 #include "WProgram.h" #else - #include "Arduino.h" - #endif - #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE - #include - #endif - #if I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE +#include "Arduino.h" +#endif +#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE +#include +#endif +#if I2CDEV_IMPLEMENTATION == I2CDEV_TEENSY_3X_WIRE #include #endif - #if I2CDEV_IMPLEMENTATION == I2CDEV_I2CMASTER_LIBRARY +#if I2CDEV_IMPLEMENTATION == I2CDEV_I2CMASTER_LIBRARY #include #endif - #if I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE +#if I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_SBWIRE #include "SBWire.h" #endif #endif @@ -103,33 +103,48 @@ THE SOFTWARE. #define ARDUINO 101 #endif - // 1000ms default read timeout (modify with "I2Cdev::readTimeout = [ms];") #define I2CDEV_DEFAULT_READ_TIMEOUT 1000 class I2Cdev { - public: - I2Cdev(); - - static int8_t readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout); - static int8_t readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout); - static int8_t readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout); - static int8_t readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout); - static int8_t readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout); - static int8_t readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout); - static int8_t readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout); - static int8_t readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout); - - static bool writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data); - static bool writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data); - static bool writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data); - static bool writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data); - static bool writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data); - static bool writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data); - static bool writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data); - static bool writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data); - - static uint16_t readTimeout; +public: + I2Cdev(); + + static int8_t readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, + uint8_t *data, uint16_t timeout = I2Cdev::readTimeout); + static int8_t readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, + uint16_t *data, uint16_t timeout = I2Cdev::readTimeout); + static int8_t readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, + uint8_t length, uint8_t *data, uint16_t timeout = + I2Cdev::readTimeout); + static int8_t readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, + uint8_t length, uint16_t *data, uint16_t timeout = + I2Cdev::readTimeout); + static int8_t readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, + uint16_t timeout = I2Cdev::readTimeout); + static int8_t readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, + uint16_t timeout = I2Cdev::readTimeout); + static int8_t readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, + uint8_t *data, uint16_t timeout = I2Cdev::readTimeout); + static int8_t readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, + uint16_t *data, uint16_t timeout = I2Cdev::readTimeout); + + static bool writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, + uint8_t data); + static bool writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, + uint16_t data); + static bool writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, + uint8_t length, uint8_t data); + static bool writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, + uint8_t length, uint16_t data); + static bool writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data); + static bool writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data); + static bool writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, + uint8_t *data); + static bool writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, + uint16_t *data); + + static uint16_t readTimeout; }; #if I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE diff --git a/IAQ2000+DHT11.ino b/IAQ2000+DHT11.ino index 912009f..8e8542c 100644 --- a/IAQ2000+DHT11.ino +++ b/IAQ2000+DHT11.ino @@ -4,9 +4,9 @@ #include #include "I2Cdev.h" #include "IAQ2000.h" +#include #include - - +#include #define dht_apin 5 @@ -14,6 +14,8 @@ IAQ2000 iaq; DHT dht(dht_apin, DHT11); +// DNS +DNSClient dnClient; uint16_t airQuality; int val = 0; @@ -40,6 +42,11 @@ char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // Start der UDP-Instanz EthernetUDP Udp; +//MQTT +const char *MQTT_BROKER = "loxberry"; +IPAddress mqttbroker(192, 168, 1, 127); +EthernetClient ethClient; +PubSubClient MQTTclient(ethClient); // Initialize the Ethernet server library // with the IP address and port you want to use @@ -48,182 +55,222 @@ EthernetServer server(80); void setup() { - Wire.begin(); - Serial.begin(9600); - //serset w5100 - Ethernet.begin(mac); - if (!Ethernet.localIP()) { - Serial.println("dhcp failed"); - Serial.println(Ethernet.linkStatus()); - } - else { - Serial.print("IP is "); - Serial.println(Ethernet.localIP()); - } - Udp.begin(localPort); // Start UDP - SPI.begin(); - sendUDP("Raumluft - aktiv"); // send UDP Ready - - - - // start the Ethernet connection and the server: - server.begin(); - Serial.print("server is at "); - Serial.println(Ethernet.localIP()); - - // initialize device - Serial.println("Initialisierung des Sensor..."); - iaq.initialize(); - - // verify connection - Serial.println("Verbindung zum Sensor testen..."); - Serial.println(iaq.testConnection() ? "iAQ-2000 erfolgreich verbunden" : "iAQ-2000 Fehler bei der Verbindung"); - - - dht.begin(); //DHT11 Sensor starten - - //Serial leeren - clearAll(); - - Serial.println("Warte auf UDP-Befehl"); + Wire.begin(); + Serial.begin(9600); + //serset w5100 + Ethernet.begin(mac); + if (!Ethernet.localIP()) { + Serial.println("dhcp failed"); + Serial.println(Ethernet.linkStatus()); + } else { + Serial.print("IP is "); + Serial.println(Ethernet.localIP()); + } + Udp.begin(localPort); // Start UDP + SPI.begin(); + sendUDP("Raumluft - aktiv"); // send UDP Ready + + // start the Ethernet connection and the server: + server.begin(); + Serial.print("server is at "); + Serial.println(Ethernet.localIP()); + + // initialize device + Serial.println("Initialisierung des Sensor..."); + iaq.initialize(); + + Serial.println( + iaq.testConnection() ? + "iAQ-2000 erfolgreich" : + "iAQ-2000 Fehler"); + + dht.begin(); //DHT11 Sensor starten + + //Serial leeren + clearAll(); + +// //DNS Client starten +// dnClient.begin(Ethernet.dnsServerIP()); +// Serial.print("dns server is at "); +// Serial.println(Ethernet.dnsServerIP()); +// if (dnClient.getHostByName(MQTT_BROKER, mqttbroker, 10) == 1) { +// Serial.print(F("loxberry = ")); +// Serial.println(mqttbroker); +// } else { +// Serial.println("dns failed "); +// Serial.println(dnClient.getHostByName(MQTT_BROKER, mqttbroker, 10)); +// mqttbroker.fromString("192.168.1.127"); +// Serial.println(mqttbroker); +// } + + // MQTT + MQTTclient.setServer(mqttbroker, 1883); + //MQTTclient.setCallback(MQTTcallback); + + //Serial.println("setup done"); } void loop() { - String aq = String (airQuality); - String temp = String (Temperatur, 2); - String hum = String (Humidity, 2); - - dht.read(); //read data from pin 5 (DHT11) - - airQuality = iaq.getIaqpred(); - Temperatur = dht.readTemperature(); - Humidity = dht.readHumidity(); - - - - // schaut on ein UDP Befehl empfangen wurde - checkUDP(); - - if (!strcmp(packetBuffer, "000")) - { - Serial.print("CO2 = "); - Serial.print(airQuality); - Serial.print(" "); - Serial.println("[ppm]"); - - - - // Wert wird auf 3000ppm begrnezt - if (airQuality > 3000) - { - aq = "3000"; - } - else - { - aq = airQuality; - } - sendUDP(aq); - } - - if (!strcmp(packetBuffer, "001")) - { - sendUDP (temp); - } - - - if (!strcmp(packetBuffer, "002")) - { - sendUDP (hum); - } - clearAll(); - // listen for incoming clients - EthernetClient client = server.available(); - if (client) - { - Serial.println("new client"); - // an http request ends with a blank line - boolean currentLineIsBlank = true; - while (client.connected()) { - if (client.available()) { - char c = client.read(); - //Serial.write(c); - // if you've gotten to the end of the line (received a newline - // character) and the line is blank, the http request has ended, - // so you can send a reply - if (c == '\n' && currentLineIsBlank) { - // send a standard http response header - client.println("HTTP/1.1 200 OK"); - client.println("Content-Type: text/html"); - client.println("Connection: close"); - client.println(); - client.println(""); - client.println(""); - // add a meta refresh tag, so the browser pulls again every 5 seconds: - client.println(""); - // output the value of each analog input pin - - client.println("Current Humidity = "); //Prints information within qoutation - client.println(hum); //Prints the Humidity read from the DHT11 on PIN 5 - client.println("% "); - client.println("Temperature = "); - client.println(temp); //Prints the temperature read from the DHT11 on PIN 5 - client.println("C "); - client.println("CO2 = "); - client.println(aq); - client.println(" "); - client.println("[ppm]"); - client.println(""); - break; - } - if (c == '\n') { - // you're starting a new line - currentLineIsBlank = true; - } - else if (c != '\r') { - // you've gotten a character on the current line - currentLineIsBlank = false; - } - } - } - // give the web browser time to receive the data - delay(1); - // close the connection: - client.stop(); - Serial.println("client disonnected"); - - } + String aq = String(airQuality); + String temp = String(Temperatur, 2); + String hum = String(Humidity, 2); + char buff[20]; + + Serial.println("LOOP10"); + dht.read(); //read data from pin 5 (DHT11) + Serial.println("LOOP20"); + airQuality = iaq.getIaqpred(); + Temperatur = dht.readTemperature(); + Humidity = dht.readHumidity(); + Serial.println("LOOP30"); + //MQTT + if (!MQTTclient.connected()) { + MQTTclient.connect("Arduino", "loxberry", "OSVL0AMqISFXgr5g"); + Serial.print("MQTT Client state:"); + Serial.println(MQTTclient.state()); + // Abonieren von Nachrichten mit dem angegebenen Topic + //MQTTclient.subscribe("/Stall/arduino1/#"); + Serial.println("connected to MQTT"); + } + temp.toCharArray(buff, temp.length()); + MQTTclient.publish("Arduino/Stall/Temperatur", buff); + aq.toCharArray(buff, aq.length()+1); + MQTTclient.publish("Arduino/Stall/CO2", buff); + Serial.println("CO2 buff"); + Serial.println(buff); + Serial.println(aq); + hum.toCharArray(buff, hum.length()); + MQTTclient.publish("Arduino/Stall/Humidity", buff); + snprintf(buff, 19, "%ld", millis()); + Serial.println("Publish message: "); + Serial.println(buff); + MQTTclient.publish("Arduino/Stall/data/Alive", buff); +Serial.println("MQTT done"); +// MQTTclient.loop(); // Schleife für MQTT + + // schaut on ein UDP Befehl empfangen wurde + checkUDP(); + + if (!strcmp(packetBuffer, "000")) { + Serial.print("CO2 = "); + Serial.print(airQuality); + Serial.print(" "); + Serial.println("[ppm]"); + + // Wert wird auf 3000ppm begrnezt + if (airQuality > 3000) { + aq = "3000"; + } else { + aq = airQuality; + } + sendUDP(aq); + } + + if (!strcmp(packetBuffer, "001")) { + sendUDP(temp); + } + + if (!strcmp(packetBuffer, "002")) { + sendUDP(hum); + } + clearAll(); + Serial.println("UDP done"); + + // listen for incoming clients + EthernetClient client = server.available(); + if (client) { + Serial.println("new client"); + // an http request ends with a blank line + boolean currentLineIsBlank = true; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + //Serial.write(c); + // if you've gotten to the end of the line (received a newline + // character) and the line is blank, the http request has ended, + // so you can send a reply + if (c == '\n' && currentLineIsBlank) { + // send a standard http response header + client.println("Current Humidity = "); //Prints information within qoutation + client.println(hum); //Prints the Humidity read from the DHT11 on PIN 5 + client.println("% "); + client.println("Temperature = "); + client.println(temp); //Prints the temperature read from the DHT11 on PIN 5 + client.println("C "); + client.println("CO2 = "); + client.println(aq); + client.println(" "); + client.println("[ppm]"); + break; + } + if (c == '\n') { + // you're starting a new line + currentLineIsBlank = true; + } else if (c != '\r') { + // you've gotten a character on the current line + currentLineIsBlank = false; + } + } + } + // give the web browser time to receive the data + delay(1); + // close the connection: + client.stop(); + Serial.println("client disonnected"); + + } + Serial.println("HTTP done"); } //// Module //// // Serial-Speicher loeschen void clearAll() { - // Paket-Buffer leeren - for (int i = 0; i < UDP_TX_PACKET_MAX_SIZE; i++) packetBuffer[i] = (char)0; + // Paket-Buffer leeren + for (int i = 0; i < UDP_TX_PACKET_MAX_SIZE; i++) + packetBuffer[i] = (char) 0; } // empfangene UDP-Befehle auswerten -void checkUDP() -{ - // pruefen ob Daten vorhanden sind - int packetSize = Udp.parsePacket(); - if (packetSize) - { - Udp.read(packetBuffer, packetSize); - Serial.print("Packet Content: "); - Serial.println(packetBuffer); - RecipientIP=Udp.remoteIP(); - Serial.print("Remote IP: "); - Serial.println(RecipientIP); - } - delay(10); +void checkUDP() { + // pruefen ob Daten vorhanden sind + int packetSize = Udp.parsePacket(); + if (packetSize) { + Udp.read(packetBuffer, packetSize); + Serial.print("Packet Content: "); + Serial.println(packetBuffer); + RecipientIP = Udp.remoteIP(); + Serial.print("Remote IP: "); + Serial.println(RecipientIP); + } + delay(10); } // UDP-Befehl senden -void sendUDP(String text) -{ - Udp.beginPacket(RecipientIP, RecipientPort); - Udp.print(text); - Udp.endPacket(); - delay(10); +void sendUDP(String text) { + Udp.beginPacket(RecipientIP, RecipientPort); + Udp.print(text); + Udp.endPacket(); + delay(10); } + +//void MQTTcallback(char *topic, byte *payload, unsigned int length) { +// Serial.print("Received message ["); +// Serial.print(topic); +// Serial.print("] "); +// char msg[length + 1]; +// for (unsigned int i = 0; i < length; i++) { +// Serial.print((char) payload[i]); +// msg[i] = (char) payload[i]; +// } +// Serial.println("keine Ahnung"); +// +// msg[length] = '\0'; +// Serial.println(msg); +// +// if (strcmp(msg, "on") == 0) { +// digitalWrite(13, HIGH); +// } else if (strcmp(msg, "off") == 0) { +// digitalWrite(13, LOW); +// } +//} diff --git a/IAQ2000/IAQ2000.cpp b/IAQ2000/IAQ2000.cpp index 3430eb7..62733e7 100644 --- a/IAQ2000/IAQ2000.cpp +++ b/IAQ2000/IAQ2000.cpp @@ -10,8 +10,8 @@ // 2012-11-08 - added TVoc and Status /* ============================================ -I2Cdev device library code is placed under the MIT license -Copyright (c) 2012 Peteris Skorovs, Jeff Rowberg + I2Cdev device library code is placed under the MIT license + Copyright (c) 2012 Peteris Skorovs, Jeff Rowberg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -39,7 +39,7 @@ Copyright (c) 2012 Peteris Skorovs, Jeff Rowberg * @see IAQ2000_DEFAULT_ADDRESS */ IAQ2000::IAQ2000() { - devAddr = IAQ2000_DEFAULT_ADDRESS; + devAddr = IAQ2000_DEFAULT_ADDRESS; } /** Specific address constructor. @@ -48,14 +48,14 @@ IAQ2000::IAQ2000() { * @see IAQ2000_ADDRESS */ IAQ2000::IAQ2000(uint8_t address) { - devAddr = address; + devAddr = address; } /** Power on and prepare for general usage. */ void IAQ2000::initialize() { - // Nothing is required, but - // the method should exist anyway. + // Nothing is required, but + // the method should exist anyway. } /** Very primitive method o verify the I2C connection. @@ -63,37 +63,34 @@ void IAQ2000::initialize() { * @return True if connection is valid, false otherwise */ bool IAQ2000::testConnection() { - if (getIaqpred() >= 450) { - return true; - } - else { - return false; - } + if (getIaqpred() >= 450) { + return true; + } else { + return false; + } } /** Read iAQ-2000 indoor air quality sensor. * @return Predicted CO2 concentration based on human induced volatile organic compounds (VOC) detection (in ppm VOC + CO2 equivalents) */ uint16_t IAQ2000::getIaqpred() { - // read bytes from the DATA0 AND DATA1 registers and bit-shifting them into a 16-bit value - readAllBytes(devAddr, 2, buffer); - return ((buffer[0] << 8) | buffer[1]); + // read bytes from the DATA0 AND DATA1 registers and bit-shifting them into a 16-bit value + readAllBytes(devAddr, 2, buffer); + return ((buffer[0] << 8) | buffer[1]); } uint8_t IAQ2000::getIaqstatus() { - // read bytes from the DATA2 register - readAllBytes(devAddr, 3, buffer); - return (buffer[2]); + // read bytes from the DATA2 register + readAllBytes(devAddr, 3, buffer); + return (buffer[2]); } uint16_t IAQ2000::getIaqtvoc() { - // read bytes from the DATA7 AND DATA8 registers and bit-shifting them into a 16-bit value - readAllBytes(devAddr, 9, buffer); - return ((buffer[7] << 8) | buffer[8]); + // read bytes from the DATA7 AND DATA8 registers and bit-shifting them into a 16-bit value + readAllBytes(devAddr, 9, buffer); + return ((buffer[7] << 8) | buffer[8]); } - - /** Read bytes from a slave device. * This is a "stripped-down" version of the standard Jeff Rowberg's I2Cdev::readBytes method * intended to provide compatibility with iAQ-2000, @@ -104,7 +101,8 @@ uint16_t IAQ2000::getIaqtvoc() { * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) * @return Number of bytes read (0 indicates failure) */ -int8_t IAQ2000::readAllBytes(uint8_t devAddr, uint8_t length, uint8_t *data, uint16_t timeout) { +int8_t IAQ2000::readAllBytes(uint8_t devAddr, uint8_t length, uint8_t *data, + uint16_t timeout) { #ifdef I2CDEV_SERIAL_DEBUG Serial.print("I2C (0x"); Serial.print(devAddr, HEX); @@ -112,30 +110,32 @@ int8_t IAQ2000::readAllBytes(uint8_t devAddr, uint8_t length, uint8_t *data, uin Serial.print(length, DEC); Serial.print(" bytes..."); #endif - - int8_t count = 0; - Wire.requestFrom(devAddr, length); - - uint32_t t1 = millis(); - for (; Wire.available() && (timeout == 0 || millis() - t1 < timeout); count++) { + int8_t count = 0; + + Wire.requestFrom(devAddr, length); + + uint32_t t1 = millis(); + for (; Wire.available() && (timeout == 0 || millis() - t1 < timeout); + count++) { #if ((I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO < 100) || I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_NBWIRE) data[count] = Wire.receive(); #elif (I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE && ARDUINO >= 100) - data[count] = Wire.read(); + data[count] = Wire.read(); #endif #ifdef I2CDEV_SERIAL_DEBUG Serial.print(data[count], HEX); if (count + 1 < length) Serial.print(" "); #endif - } - if (timeout > 0 && millis() - t1 >= timeout && count < length) count = -1; // timeout - + } + if (timeout > 0 && millis() - t1 >= timeout && count < length) + count = -1; // timeout + #ifdef I2CDEV_SERIAL_DEBUG Serial.print(". Done ("); Serial.print(count, DEC); Serial.println(" read)."); #endif - - return count; + + return count; } diff --git a/IAQ2000/IAQ2000.h b/IAQ2000/IAQ2000.h index 05a8a0b..c8422fb 100644 --- a/IAQ2000/IAQ2000.h +++ b/IAQ2000/IAQ2000.h @@ -10,28 +10,28 @@ // 2015-11-08 - added TVoc and Status /* ============================================ -I2Cdev device library code is placed under the MIT license -Copyright (c) 2012 Peteris Skorovs, Jeff Rowberg + I2Cdev device library code is placed under the MIT license + Copyright (c) 2012 Peteris Skorovs, Jeff Rowberg -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -=============================================== -*/ + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + =============================================== + */ #ifndef _IAQ2000_H_ #define _IAQ2000_H_ @@ -45,20 +45,21 @@ THE SOFTWARE. #define IAQ2000_RA_DATA2 0x01 class IAQ2000 { - public: - IAQ2000(); - IAQ2000(uint8_t address); - void initialize(); +public: + IAQ2000(); + IAQ2000(uint8_t address); + void initialize(); bool testConnection(); uint16_t getIaqtvoc(); - uint16_t getIaqpred(); + uint16_t getIaqpred(); uint8_t getIaqstatus(); - private: - uint8_t devAddr; - uint8_t buffer[8]; - - int8_t readAllBytes(uint8_t devAddr, uint8_t length, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout); +private: + uint8_t devAddr; + uint8_t buffer[8]; + + int8_t readAllBytes(uint8_t devAddr, uint8_t length, uint8_t *data, + uint16_t timeout = I2Cdev::readTimeout); }; #endif /* _IAQ200_H_ */ diff --git a/IAQ2000/examples/IAQ2000/IAQ2000.ino b/IAQ2000/examples/IAQ2000/IAQ2000.ino index 96dcf4f..e48f7b1 100644 --- a/IAQ2000/examples/IAQ2000/IAQ2000.ino +++ b/IAQ2000/examples/IAQ2000/IAQ2000.ino @@ -8,28 +8,28 @@ // 2015-11-08 - added TVoc and Status /* ============================================ - IAQ2000 device library code is placed under the MIT license - Copyright (c) 2012 Peteris Skorovs, Jeff Rowberg - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - =============================================== -*/ + IAQ2000 device library code is placed under the MIT license + Copyright (c) 2012 Peteris Skorovs, Jeff Rowberg + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + =============================================== + */ // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation // is used in I2Cdev.h @@ -40,7 +40,6 @@ #include "I2Cdev.h" #include "IAQ2000.h" - // class default I2C address is 0x5A // specific I2C addresses may be passed as a parameter here // but this device only supports one I2C address (0x5A) @@ -56,107 +55,100 @@ uint8_t oldiAQstatus = 0; uint16_t oldairTvoc = 0; uint8_t result = 0; - #define LED_PIN 13 bool blinkState = false; - void setup() { - // configure LED pin for output - pinMode(LED_PIN, OUTPUT); - // join I2C bus (I2Cdev library doesn't do this automatically) - Wire.begin(); - - // initialize serial communication - // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but - // it's really up to you depending on your project) - Serial.begin(38400); - delay(1000); - Serial.println("Initializing Serial..."); - delay(1000); - - while (!Serial) { - ; // wait for serial port to connect - } - - startTime = millis(); - - // initialize device - // Serial.println("Initializing I2C devices..."); - - - iaq.initialize(); - if (!iaq.testConnection()) { - blinkState = !blinkState; - digitalWrite(LED_PIN, blinkState); - delay(500); - } + // configure LED pin for output + pinMode(LED_PIN, OUTPUT); + // join I2C bus (I2Cdev library doesn't do this automatically) + Wire.begin(); + + // initialize serial communication + // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but + // it's really up to you depending on your project) + Serial.begin(38400); + delay(1000); + Serial.println("Initializing Serial..."); + delay(1000); + + while (!Serial) { + ; // wait for serial port to connect + } + + startTime = millis(); + + // initialize device + // Serial.println("Initializing I2C devices..."); + + iaq.initialize(); + if (!iaq.testConnection()) { + blinkState = !blinkState; + digitalWrite(LED_PIN, blinkState); + delay(500); + } } void loop() { - // display predicted CO2 concentration based on human induced volatile organic compounds (VOC) detection (in ppm VOC + CO2 equivalents) - // during the first 6 hours of continuous operation (burn-in) the module will display 450ppm - // the successful burn-in is saved to the EEPROM, the run-in time after restart is 15min - Serial.print("CO2 = "); - Serial.print(airQuality); - Serial.print(" "); - Serial.print("[ppm] 450-2000"); - - // blink LED to indicate activity - blinkState = !blinkState; - digitalWrite(LED_PIN, blinkState); - - // wait five seconds - delay(5000); - - // read seansor - iAQstatus = iaq.getIaqstatus(); - - // display predicted CO2 concentration based on human induced volatile organic compounds (VOC) detection (in ppm VOC + CO2 equivalents) - // during the first 6 hours of continuous operation (burn-in) the module will display 450ppm - // the successful burn-in is saved to the EEPROM, the run-in time after restart is 15min - Serial.print(" -- Status = "); - if (iAQstatus == 0) - { - Serial.print("OK"); - } - if (iAQstatus == 1) - { - Serial.print("BUSY"); - } - if (iAQstatus == 16) - { - Serial.print("WARUMUP"); - } - if (iAQstatus == 128) - { - Serial.print("ERROR"); - } - - // blink LED to indicate activity - blinkState = !blinkState; - digitalWrite(LED_PIN, blinkState); - - // wait five seconds - delay(5000); - - // read seansor - airTvoc = iaq.getIaqtvoc(); - - // display predicted CO2 concentration based on human induced volatile organic compounds (VOC) detection (in ppm VOC + CO2 equivalents) - // during the first 6 hours of continuous operation (burn-in) the module will display 450ppm - // the successful burn-in is saved to the EEPROM, the run-in time after restart is 15min - Serial.print(" -- TVoc = "); - Serial.print(airTvoc); - Serial.print(" "); - Serial.println("[ppb] 125-600"); - - // blink LED to indicate activity - blinkState = !blinkState; - digitalWrite(LED_PIN, blinkState); - - // wait five seconds - delay(5000); + // display predicted CO2 concentration based on human induced volatile organic compounds (VOC) detection (in ppm VOC + CO2 equivalents) + // during the first 6 hours of continuous operation (burn-in) the module will display 450ppm + // the successful burn-in is saved to the EEPROM, the run-in time after restart is 15min + Serial.print("CO2 = "); + Serial.print(airQuality); + Serial.print(" "); + Serial.print("[ppm] 450-2000"); + + // blink LED to indicate activity + blinkState = !blinkState; + digitalWrite(LED_PIN, blinkState); + + // wait five seconds + delay(5000); + + // read seansor + iAQstatus = iaq.getIaqstatus(); + + // display predicted CO2 concentration based on human induced volatile organic compounds (VOC) detection (in ppm VOC + CO2 equivalents) + // during the first 6 hours of continuous operation (burn-in) the module will display 450ppm + // the successful burn-in is saved to the EEPROM, the run-in time after restart is 15min + Serial.print(" -- Status = "); + if (iAQstatus == 0) { + Serial.print("OK"); + } + if (iAQstatus == 1) { + Serial.print("BUSY"); + } + if (iAQstatus == 16) { + Serial.print("WARUMUP"); + } + if (iAQstatus == 128) { + Serial.print("ERROR"); + } + + // blink LED to indicate activity + blinkState = !blinkState; + digitalWrite(LED_PIN, blinkState); + + // wait five seconds + delay(5000); + + // read seansor + airTvoc = iaq.getIaqtvoc(); + + // display predicted CO2 concentration based on human induced volatile organic compounds (VOC) detection (in ppm VOC + CO2 equivalents) + // during the first 6 hours of continuous operation (burn-in) the module will display 450ppm + // the successful burn-in is saved to the EEPROM, the run-in time after restart is 15min + Serial.print(" -- TVoc = "); + Serial.print(airTvoc); + Serial.print(" "); + Serial.println("[ppb] 125-600"); + + // blink LED to indicate activity + blinkState = !blinkState; + digitalWrite(LED_PIN, blinkState); + + // wait five seconds + delay(5000); } diff --git a/spec.d b/spec.d index 3dfb1df..3f556a2 100644 --- a/spec.d +++ b/spec.d @@ -1,2 +1,2 @@ spec.o: \ - C:/Users/andy/Documents/sloeber-workspace/.metadata/.plugins/org.eclipse.cdt.managedbuilder.core/spec.C + C:/Users/agr/Documents/sloeber-workspace/.metadata/.plugins/org.eclipse.cdt.managedbuilder.core/spec.C