add examples

This commit is contained in:
houaimin 2022-10-08 22:43:22 +08:00
parent e17329b953
commit 732f0776ab
13 changed files with 328 additions and 18 deletions

View File

@ -0,0 +1,37 @@
#include "KC868.h"
KC868 kc868(&Serial2); //create kc868 object
void setup() {
Serial.begin(115200);
delay(2000);
kc868.begin(115200);//begin kc868 with baud 115200
boolean ret = kc868.writeSwitch(2,1); //open relay 2
Serial.printf("open relay 2 ");
if(ret)
Serial.println("success!");
else
Serial.println("fail.");
delay(2000);
ret = kc868.writeSwitch(2,0); //close relay 2
Serial.printf("close relay 2 ");
if(ret)
Serial.println("success!");
else
Serial.println("fail.");
}
void loop(){
kc868.poll();
}

View File

@ -0,0 +1,37 @@
#include "KC868.h"
KC868 kc868(&Serial2); //create kc868 object
void setup() {
Serial.begin(115200);
delay(2000);
kc868.begin(115200);//begin kc868 with baud 115200
boolean ret = kc868.writeSwitchAll(98304); //open relay 16 and relay 17 ,hex value is 0x00018000 , decimal value is 98304
Serial.printf("open relay 16 and 17 ");
if(ret)
Serial.println("success!");
else
Serial.println("fail.");
delay(2000);
ret = kc868.writeSwitchAll(0); //close all relay (contain relay 16 and relay 17)
Serial.printf("close all relay ");
if(ret)
Serial.println("success!");
else
Serial.println("fail.");
}
void loop(){
kc868.poll();
}

View File

@ -0,0 +1,28 @@
#include "KC868.h"
KC868 kc868(&Serial2); //create kc868 object
void setup() {
Serial.begin(115200);
delay(2000);
kc868.begin(115200);//begin kc868 with baud 115200
boolean ret = kc868.writeSwitchAllON(); //open all relay
Serial.printf("open all relay ");
if(ret)
Serial.println("success!");
else
Serial.println("fail.");
}
void loop(){
kc868.poll();
}

View File

@ -0,0 +1,28 @@
#include "KC868.h"
KC868 kc868(&Serial2); //create kc868 object
void setup() {
Serial.begin(115200);
delay(2000);
kc868.begin(115200);//begin kc868 with baud 115200
boolean ret = kc868.writeSwitchAllOFF(); //close all relay
Serial.printf("close all relay ");
if(ret)
Serial.println("success!");
else
Serial.println("fail.");
}
void loop(){
kc868.poll();
}

View File

@ -0,0 +1,24 @@
#include "KC868.h"
KC868 kc868(&Serial2); //create kc868 object
void setup() {
Serial.begin(115200);
delay(2000);
kc868.begin(115200);//begin kc868 with baud 115200
uint8_t ret = kc868.readSwitch(15); //read relay 15
Serial.printf("relay 15 state is %d.\n",ret); //ret=1 relay 15 on, ret=0 relay 15 off
}
void loop(){
kc868.poll();
}

View File

@ -0,0 +1,23 @@
#include "KC868.h"
KC868 kc868(&Serial2); //create kc868 object
void setup() {
Serial.begin(115200);
delay(2000);
kc868.begin(115200);//begin kc868 with baud 115200
uint32_t ret = kc868.readSwitchAll(); //read all relay state
Serial.printf("relay state is 0x%08x.\n",ret); //32 bit value,the highest bit is the state of relay 32, and the lowest bit is the state of relay 1
}
void loop(){
kc868.poll();
}

View File

@ -0,0 +1,24 @@
#include "KC868.h"
KC868 kc868(&Serial2); //create kc868 object
void setup() {
Serial.begin(115200);
delay(2000);
kc868.begin(115200);//begin kc868 with baud 115200
uint8_t ret = kc868.readSensor(5); //read sensor 5 state,
Serial.printf("sensor 5 state is %d.\n",ret); //ret=1,the sensor is tirggered, ret=0,the sensor is not tirggered
}
void loop(){
kc868.poll();
}

View File

@ -0,0 +1,24 @@
#include "KC868.h"
KC868 kc868(&Serial2); //create kc868 object
void setup() {
Serial.begin(115200);
delay(2000);
kc868.begin(115200);//begin kc868 with baud 115200
uint8_t ret = kc868.readSensorAll(); //read all sensor state,
Serial.printf("sensor state is 0x%02x.\n",ret); //8 bit value,the 6st bit is the state of sensor 6, and the lowest bit is the state of sensor 1
//Which bit is 1, which bit is triggered.
}
void loop(){
kc868.poll();
}

View File

@ -0,0 +1,29 @@
#include "KC868.h"
KC868 kc868(&Serial2); //create kc868 object
void SwitchUpdate(uint8_t idx, uint8_t state)
{
Serial.printf("Switch %d : %d\n",idx,state);//state ,0:switch off , 1: switch on
}
void setup() {
Serial.begin(115200);
delay(2000);
kc868.begin(115200);//begin kc868 with baud 115200
kc868.setSwitchChangeHook(SwitchUpdate);//set switch change hook
}
void loop(){
kc868.poll();
}

View File

@ -0,0 +1,29 @@
#include "KC868.h"
KC868 kc868(&Serial2); //create kc868 object
void SensorUpdate(uint8_t idx, uint8_t state)
{
Serial.printf("Sensor %d : %d\n",idx,state); //state ,0:sensor triggered , 1:sensor not triggered
}
void setup() {
Serial.begin(115200);
delay(2000);
kc868.begin(115200);//begin kc868 with baud 115200
kc868.setSensorChangeHook(SensorUpdate);//set sensor change hook
}
void loop(){
kc868.poll();
}

View File

@ -1,7 +1,7 @@
#include "KC868.h" #include "KC868.h"
KC868 kc868(&Serial2,115200);//create a new kc868 object, arg 1: serial object point , arg 2: baudrate KC868 kc868(&Serial2);//create a new kc868 object, arg 1: serial object point , arg 2: baudrate
void SwitchUpdate(uint8_t idx, uint8_t state) void SwitchUpdate(uint8_t idx, uint8_t state)
@ -21,7 +21,7 @@ void setup() {
delay(3000); delay(3000);
kc868.open();//open the kc868 first kc868.begin(115200);//open the kc868 first
kc868.setReadMode(0);//mode, 0 :default, Query directly; 1: Query all switch state every 2s kc868.setReadMode(0);//mode, 0 :default, Query directly; 1: Query all switch state every 2s
@ -32,13 +32,17 @@ void setup() {
Serial.println("Write Switch 1 on."); Serial.println("Write Switch 1 on.");
kc868.writeSwitch(1,1); kc868.writeSwitch(1,1);
int ret = kc868.readSwitch(1);
Serial.printf("The state of Switch 1 is %d .\n",ret);
Serial.println("Delay 1 second."); Serial.println("Delay 1 second.");
delay(1000); delay(1000);
Serial.println("Write Switch 1 off."); Serial.println("Write Switch 1 off.");
kc868.writeSwitch(1,0); kc868.writeSwitch(1,0);
//kc868.readSwitchAll();// ret = kc868.readSwitch(1);
Serial.printf("The state of Switch 1 is %d .\n",ret);
} }

View File

@ -1,24 +1,24 @@
#include "KC868.h" #include "KC868.h"
KC868::KC868(HardwareSerial *hs, unsigned long baud) KC868::KC868(HardwareSerial *hs)
{ {
_type = dev_serial; _type = dev_serial;
_serial = hs; _serial = hs;
_baud = baud;
} }
boolean KC868::open() boolean KC868::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms, uint8_t rxfifo_full_thrhd)
{ {
if (_type == dev_serial) if (_type == dev_serial)
{ {
_serial->begin(_baud); _serial->begin(baud, config, rxPin, txPin, invert, timeout_ms, rxfifo_full_thrhd);
} }
return true; return true;
} }
boolean KC868::close() boolean KC868::end()
{ {
if (_type == dev_serial) if (_type == dev_serial)
{ {
@ -48,8 +48,6 @@ boolean KC868::readSwitch(int idx)
// example RELAY-READ-255,1,1,OK\0 // example RELAY-READ-255,1,1,OK\0
sprintf(tmp, "%s,%d,1,OK", READ_RELAY_CMD, idx); //check 'OK' ,otherwise return false sprintf(tmp, "%s,%d,1,OK", READ_RELAY_CMD, idx); //check 'OK' ,otherwise return false
ret = checkRet(tmp); ret = checkRet(tmp);
Serial.printf("---%s---",pkt.rxbuf);
Serial.printf("---%d---",ret);
if (ret) if (ret)
return true; return true;
else else
@ -324,13 +322,14 @@ boolean KC868::readSensor(int idx)
} }
value = atoi(cmd.p1); value = atoi(cmd.p1);
value = ~value;
value = (value>>(idx-1))&0x01; value = (value>>(idx-1))&0x01;
return value; return value;
} }
} }
} }
return 1; return 0;
} }
@ -368,6 +367,7 @@ uint8_t KC868::readSensorAll()
} }
value = atoi(cmd.p1); value = atoi(cmd.p1);
value = ~value;
return value; return value;
} }
} }
@ -381,6 +381,30 @@ void KC868::write(char *data, uint16_t len)
{ {
if (_type == dev_serial) if (_type == dev_serial)
{ {
bufferPos=0;
while (_serial->available())
{
uint8_t received = _serial->read();
// if((received=='\0')||(received=='\n'))
// break;
inputBuffer[bufferPos++] = received;
if (bufferPos >= PKT_MAX_LEN)
{
bufferPos = 0;
}
}
if(bufferPos>0)
{
//handle one line
handleData(inputBuffer, bufferPos);
memset(inputBuffer, 0, PKT_MAX_LEN);
bufferPos = 0;
}
memset(pkt.rxbuf, 0, PKT_MAX_LEN); memset(pkt.rxbuf, 0, PKT_MAX_LEN);
_serial->write(data, len); _serial->write(data, len);
} }
@ -589,7 +613,7 @@ void KC868::handleData(char *data, uint16_t len)
idx = atoi(cmd.p1); idx = atoi(cmd.p1);
if(SensorChangeHook!=NULL) if(SensorChangeHook!=NULL)
SensorChangeHook(idx,0);//alarm SensorChangeHook(idx,1);//alarm
} }
@ -606,7 +630,7 @@ void KC868::handleData(char *data, uint16_t len)
idx = atoi(cmd.p1); idx = atoi(cmd.p1);
if(SensorChangeHook!=NULL) if(SensorChangeHook!=NULL)
SensorChangeHook(idx,1);//Cancel alarm SensorChangeHook(idx,0);//Cancel alarm
} }

View File

@ -57,10 +57,9 @@ class KC868
_dev_type _type; _dev_type _type;
HardwareSerial *_serial; HardwareSerial *_serial;
int _baud;
uint32_t _SwitchCache;//save switch state ,32 bit uint32_t _SwitchCache;//save switch state ,32 bit
uint32_t _SwitchCache_last=0x00;//save last switch state ,32 bit uint32_t _SwitchCache_last=0x00;//save last switch state ,32 bit
uint8_t _SensorCache=0XFF;//save sensor input state uint8_t _SensorCache=0x00;//save sensor input state
uint8_t _mode; uint8_t _mode;
hw_timer_t* tim= NULL; hw_timer_t* tim= NULL;
@ -83,11 +82,11 @@ class KC868
_data_pkt pkt; _data_pkt pkt;
public: public:
KC868(HardwareSerial *hs, unsigned long baud); //serial KC868(HardwareSerial *hs); //serial
// KC868(WiFiClient client); // wificlient // KC868(WiFiClient client); // wificlient
boolean open(); boolean begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL, uint8_t rxfifo_full_thrhd = 112);
boolean close(); boolean end();
void poll(); void poll();
boolean readSwitch(int idx); boolean readSwitch(int idx);
boolean readSwitchCache(int idx); boolean readSwitchCache(int idx);