2021-02-01 17:29:05 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include "SG112A.h"
|
|
|
|
|
2021-02-02 20:12:31 +00:00
|
|
|
SG112A::SG112A(void) {
|
|
|
|
Serial.begin(9600);
|
|
|
|
Serial.setTimeout(READ_TIMEOUT);
|
2021-02-01 17:29:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 20:21:03 +00:00
|
|
|
void SG112A::getSensorData(lora_data &loradata) {
|
2021-02-02 20:12:31 +00:00
|
|
|
write(CMD_GET_PPM);
|
|
|
|
delay(50);
|
2021-02-03 15:23:45 +00:00
|
|
|
uint8_t readBytes = read();
|
|
|
|
|
|
|
|
if (readBytes > 0) {
|
|
|
|
switch(buffer[2]) {
|
2021-02-02 20:12:31 +00:00
|
|
|
case 0x15:
|
|
|
|
loradata.ppm = (buffer[5]*256) + buffer[4];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 20:21:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 20:12:31 +00:00
|
|
|
void SG112A::write(byte cmd) {
|
|
|
|
uint8_t _cmd[6] = {0xAA, 0x55, cmd, 0x00, 0x00, 0x00};
|
|
|
|
uint16_t crc = crc16(_cmd, 4);
|
|
|
|
_cmd[4] = (uint8_t)(crc & 0xFF);
|
|
|
|
_cmd[5] = (uint8_t)(crc >> 8);
|
|
|
|
while (Serial.available() > 0) Serial.read();
|
|
|
|
Serial.write(_cmd, 6);
|
|
|
|
Serial.flush();
|
2021-02-01 17:29:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 20:12:31 +00:00
|
|
|
uint8_t SG112A::read() {
|
|
|
|
uint8_t ret = 0;
|
|
|
|
zeroBuffer();
|
|
|
|
if (Serial.available() > 0) {
|
|
|
|
ret = Serial.readBytes(buffer, SER_BUF_LEN);
|
|
|
|
}
|
2021-02-01 17:29:05 +00:00
|
|
|
|
2021-02-02 20:12:31 +00:00
|
|
|
if (buffer[0] != 0xBB && buffer[1] != 0x66)
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
// TODO: Do CRC Check Here
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SG112A::zeroBuffer() {
|
|
|
|
for (int i=0; i < SER_BUF_LEN; i++)
|
|
|
|
buffer[i] = 0x00;
|
2021-02-01 17:29:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 20:12:31 +00:00
|
|
|
uint16_t SG112A::crc16(uint8_t *cmd, int len){
|
|
|
|
uint16_t ret = 0xffff;
|
|
|
|
uint16_t polynomial = 0xa001;
|
|
|
|
int shift = 0x0;
|
|
|
|
int i = 0;
|
|
|
|
for (i = len - 1; i >= 0 ; i-- ){
|
|
|
|
|
|
|
|
uint16_t code = ( uint16_t )( cmd [ len -1 - i ] & 0xff );
|
|
|
|
ret = ret^code;
|
|
|
|
shift = 0x0;
|
|
|
|
while ( shift <= 7 ){
|
|
|
|
if ( ret & 0x1 ) {
|
|
|
|
ret = ret >> 1;
|
|
|
|
ret = ret^polynomial ;
|
|
|
|
} else {
|
|
|
|
ret = ret >> 1;
|
|
|
|
}
|
|
|
|
shift++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2021-02-01 17:29:05 +00:00
|
|
|
}
|