Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions src/DFRobot_C4001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ DFRobot_C4001::~DFRobot_C4001(){}
sSensorStatus_t DFRobot_C4001::getStatus(void)
{
sSensorStatus_t data;
uint8_t temp[100] = {0};
uint8_t temp[DFROBOT_C4001_BUFFER_SIZE] = {0};
uint8_t len = 0;
if(uartI2CFlag == I2C_FLAG){
uint8_t temp = 0;
Expand All @@ -26,11 +26,11 @@ sSensorStatus_t DFRobot_C4001::getStatus(void)
data.initStatus = (temp&0x80) >> 7;
}else{
sAllData_t allData;
readReg(0, temp, 100);
readReg(0, temp, DFROBOT_C4001_BUFFER_SIZE);
writeReg(0, (uint8_t *)START_SENSOR, strlen(START_SENSOR));
while(len == 0){
delay(1000);
len = readReg(0, temp, 100);
len = readReg(0, temp, DFROBOT_C4001_BUFFER_SIZE);
allData = anaysisData(temp ,len);
}
data.workStatus = allData.sta.workStatus;
Expand All @@ -53,9 +53,9 @@ bool DFRobot_C4001::motionDetection(void)
static bool old = false;
uint8_t status = 0;
uint8_t len = 0;
uint8_t temp[100] = {0};
uint8_t temp[DFROBOT_C4001_BUFFER_SIZE] = {0};
sAllData_t data;
len = readReg(0, temp, 100);
len = readReg(0, temp, DFROBOT_C4001_BUFFER_SIZE);
data = anaysisData(temp ,len);
if(data.exist){
old = (bool)status;
Expand Down Expand Up @@ -176,8 +176,8 @@ uint8_t DFRobot_C4001::getTrigSensitivity(void)
return temp;
}else{
sResponseData_t responseData;
uint8_t temp[100] = {0};
readReg(0, temp, 100);
uint8_t temp[DFROBOT_C4001_BUFFER_SIZE] = {0};
readReg(0, temp, DFROBOT_C4001_BUFFER_SIZE);
String data = "getSensitivity";
responseData = wRCMD(data, (uint8_t)1);
if(responseData.status){
Expand Down Expand Up @@ -213,8 +213,8 @@ uint8_t DFRobot_C4001::getKeepSensitivity(void)
return temp;
}else{
sResponseData_t responseData;
uint8_t temp[100] = {0};
readReg(0, temp, 100);
uint8_t temp[DFROBOT_C4001_BUFFER_SIZE] = {0};
readReg(0, temp, DFROBOT_C4001_BUFFER_SIZE);
String data = "getSensitivity";
responseData = wRCMD(data, (uint8_t)1);
if(responseData.status){
Expand Down Expand Up @@ -389,9 +389,9 @@ uint8_t DFRobot_C4001::getTargetNumber(void)
return _buffer.number;
}else{
uint8_t len = 0;
uint8_t temp[100] = {0};
uint8_t temp[DFROBOT_C4001_BUFFER_SIZE] = {0};
sAllData_t data;
len = readReg(0, temp, 100);
len = readReg(0, temp, DFROBOT_C4001_BUFFER_SIZE);
data = anaysisData(temp ,len);
if(data.target.number != 0){
flash_number = 0;
Expand Down Expand Up @@ -672,20 +672,18 @@ sAllData_t DFRobot_C4001::anaysisData(uint8_t * data, uint8_t len)
allData.sta.workStatus = 1;
allData.sta.initStatus = 1;
char *token;
char *parts[10]; // Let's say there are at most 10 parts
char *parts[10] = {0}; // Let's say there are at most 10 parts
int index = 0; // Used to track the number of parts stored
token = strtok((char*)(data+location), ",");
while (token != NULL) {
parts[index] = token; // Stores partial Pointers in an array
if(index++ > 8){
break;
}
while (token != NULL && index < 10) {
parts[index++] = token; // Stores partial Pointers in an array
token = strtok(NULL, ","); // Continue to extract the next section
}
allData.target.number = atoi(parts[1]);
allData.target.range = atof(parts[3]) * 100;
allData.target.speed = atof(parts[4]) * 100;
allData.target.energy = atof(parts[5]);
// Defensive: check we have enough fields and they are not NULL
allData.target.number = (index > 1 && parts[1]) ? atoi(parts[1]) : 0;
allData.target.range = (index > 3 && parts[3]) ? atof(parts[3]) * 100 : 0;
allData.target.speed = (index > 4 && parts[4]) ? atof(parts[4]) * 100 : 0;
allData.target.energy = (index > 5 && parts[5]) ? atof(parts[5]) : 0;
}else{
}
return allData;
Expand All @@ -694,12 +692,12 @@ sAllData_t DFRobot_C4001::anaysisData(uint8_t * data, uint8_t len)
sResponseData_t DFRobot_C4001::wRCMD(String cmd1, uint8_t count)
{
uint8_t len = 0;
uint8_t temp[200] = {0};
uint8_t temp[DFROBOT_C4001_BUFFER_SIZE] = {0};
sResponseData_t responseData;
sensorStop();
writeReg(0, (uint8_t *)cmd1.c_str(), cmd1.length());
delay(100);
len = readReg(0, temp, 200);
len = readReg(0, temp, DFROBOT_C4001_BUFFER_SIZE);
responseData = anaysisResponse(temp, len, count);
delay(100);
writeReg(0, (uint8_t *)START_SENSOR, strlen(START_SENSOR));
Expand All @@ -726,20 +724,20 @@ void DFRobot_C4001::writeCMD(String cmd1 , String cmd2, uint8_t count)
bool DFRobot_C4001::sensorStop(void)
{
uint8_t len = 0;
uint8_t temp[200] = {0};
uint8_t temp[DFROBOT_C4001_BUFFER_SIZE] = {0};
writeReg(0, (uint8_t *)STOP_SENSOR, strlen(STOP_SENSOR));
delay(1000);
len = readReg(0, temp, 200);
len = readReg(0, temp, DFROBOT_C4001_BUFFER_SIZE);
while(1){
if(len != 0){
if (strstr((const char *)temp, "sensorStop") != NULL) {
return true;
}
}
memset(temp, 0, 200);
memset(temp, 0, DFROBOT_C4001_BUFFER_SIZE);
delay(400);
writeReg(0, (uint8_t *)STOP_SENSOR, strlen(STOP_SENSOR));
len = readReg(0, temp, 200);
len = readReg(0, temp, DFROBOT_C4001_BUFFER_SIZE);

}
}
Expand All @@ -751,9 +749,9 @@ DFRobot_C4001_I2C::DFRobot_C4001_I2C(TwoWire *pWire, uint8_t addr)
uartI2CFlag = I2C_FLAG;
}

bool DFRobot_C4001_I2C::begin()
bool DFRobot_C4001_I2C::begin(int sda /*=2*/, int scl/*=14*/)
{
_pWire->begin();
_pWire->begin(sda, scl);
_pWire->beginTransmission(_I2C_addr);
if(_pWire->endTransmission() == 0){
return true;
Expand Down Expand Up @@ -808,7 +806,7 @@ int16_t DFRobot_C4001_I2C::readReg(uint8_t reg, uint8_t *data, uint8_t len)
bool DFRobot_C4001_UART::begin()
{
#ifdef ESP32
_serial->begin(this->_baud, SERIAL_8N1, _txpin, _rxpin);
_serial->begin(this->_baud, SERIAL_8N1, _rxpin, _txpin);
#elif defined(ARDUINO_AVR_UNO) || defined(ESP8266)
_serial->begin(this->_baud);
delay(1000);
Expand Down
4 changes: 3 additions & 1 deletion src/DFRobot_C4001.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class DFRobot_C4001{
#define REG_E_TRIG_RANGE_L 0x29
#define REG_E_TRIG_RANGE_H 0x2A

#define DFROBOT_C4001_BUFFER_SIZE 240

#define REG_RESULT_OBJ_MUN 0x10
#define REG_RESULT_RANGE_L 0x11
#define REG_RESULT_RANGE_H 0x12
Expand Down Expand Up @@ -420,7 +422,7 @@ class DFRobot_C4001{
class DFRobot_C4001_I2C:public DFRobot_C4001{
public:
DFRobot_C4001_I2C(TwoWire *pWire=&Wire, uint8_t addr = DEVICE_ADDR_0);
bool begin(void);
bool begin(int sda = 2, int scl = 14);
protected:
virtual void writeReg(uint8_t reg, uint8_t *data, uint8_t len);
virtual int16_t readReg(uint8_t reg, uint8_t *data, uint8_t len);
Expand Down