diff --git a/lib/SG112A/SG112A.cpp b/lib/SG112A/SG112A.cpp index 8ff3199..bd23734 100644 --- a/lib/SG112A/SG112A.cpp +++ b/lib/SG112A/SG112A.cpp @@ -1,6 +1,33 @@ +/* + SG112A.cpp - SG112A Sensor Library + Copyright (c) 2020-2021, Stefan Brand + All rights reserved. + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + 3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + #include #include "SG112A.h" +// Constructor - Inititalize Hardware UART SG112A::SG112A(void) { Serial.begin(9600); Serial.setTimeout(READ_TIMEOUT); @@ -20,6 +47,7 @@ void SG112A::getSensorData(lora_data &loradata) { } } +// Write a Command to the Sensor void SG112A::write(byte cmd) { uint8_t _cmd[6] = {0xAA, 0x55, cmd, 0x00, 0x00, 0x00}; uint16_t crc = crc16(_cmd, 4); @@ -30,26 +58,37 @@ void SG112A::write(byte cmd) { Serial.flush(); } +// Read a Sensor Response uint8_t SG112A::read() { + // Number of returned Bytes uint8_t ret = 0; + // Clear Internal Buffer zeroBuffer(); + + // Read Available Bytes if (Serial.available() > 0) { ret = Serial.readBytes(buffer, SER_BUF_LEN); } - if (buffer[0] != 0xBB && buffer[1] != 0x66) - ret = 0; - - // TODO: Do CRC Check Here + // Check Sync Bytes + if (buffer[0] != 0xBB || buffer[1] != 0x66) + return 0; + + // Check CRC of the Returned Messages + uint16_t crc = crc16(buffer, ret-2); + if (buffer[ret-1] != (uint8_t)(crc >> 8) || buffer[ret-2] != (uint8_t)(crc & 0xFF)) + return 0; return ret; } +// Fill the Internal Buffer with Zeroes void SG112A::zeroBuffer() { for (int i=0; i < SER_BUF_LEN; i++) buffer[i] = 0x00; } +// Calculate 16Bit CRC of Messages and Commands uint16_t SG112A::crc16(uint8_t *cmd, int len){ uint16_t ret = 0xffff; uint16_t polynomial = 0xa001; @@ -71,4 +110,4 @@ uint16_t SG112A::crc16(uint8_t *cmd, int len){ } } return ret; -} +} \ No newline at end of file diff --git a/lib/SG112A/SG112A.h b/lib/SG112A/SG112A.h index df42d90..3db0677 100644 --- a/lib/SG112A/SG112A.h +++ b/lib/SG112A/SG112A.h @@ -1,17 +1,44 @@ +/* + SG112A.h - SG112A Sensor Library + Copyright (c) 2020-2021, Stefan Brand + All rights reserved. + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + 3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + #ifndef SG112A_H #define SG112A_H +// Data Structure for the LoRa Packet struct lora_data { uint8_t bat; int16_t ppm; } __attribute__ ((packed)); -#define READ_TIMEOUT 500 -#define SER_BUF_LEN 16 +#define READ_TIMEOUT 500 // Timeout for Serial Communication +#define SER_BUF_LEN 16 // Length of the Internal Serial Message Buffer -#define CMD_GET_VER 0x10 -#define CMD_GET_SER 0x12 -#define CMD_GET_PPM 0x14 +#define CMD_GET_VER 0x10 // Get Sensor Version +#define CMD_GET_SER 0x12 // Get Sensor Serial +#define CMD_GET_PPM 0x14 // Get Current PPM Reading class SG112A { private: