Initial code.

This commit is contained in:
Mentalflow 2022-09-07 22:54:45 +08:00
parent 1206b2e43f
commit 9008e00cc6
Signed by: Mentalflow
GPG Key ID: 5AE68D4401A2EE71
7 changed files with 343 additions and 0 deletions

BIN
examples/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,30 @@
#include "DLLN3X.h"
DLLN3X dlln33; //Instantiating DLLN3X module
void zigbee_call_back(unsigned char orig_port,
unsigned char dest_port, unsigned short addr,
unsigned char data[], int length) {
switch (orig_port) {
case 0x82:
{
/* Do everything you want to do */
Serial.println("Hello from port 0x82!");
break;
default:
break;
}
}
}
void setup() {
dlln33.init(&Serial1);
dlln33.setcallback(zigbee_call_back);
Serial.printf("DLLN33 addr is 0x%X\n", dlln33.read_addr());
dlln33.send(0x81, 0x82, dlln33.read_addr(), (uint8_t *)"Hello to port 0x81!", 21);
}
void loop() {
dlln33.loop();
}

37
keywords.txt Normal file
View File

@ -0,0 +1,37 @@
#######################################
# Syntax Coloring Map For DLLN3X
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
DLLN3X KEYWORD1
zigbee_frame KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
_clear KEYWORD2
_pack KEYWORD2
_depack KEYWORD2
_callback KEYWORD2
init KEYWORD2
recv KEYWORD2
send KEYWORD2
rled_blink KEYWORD2
send_msg KEYWORD2
loop KEYWORD2
setcallback KEYWORD2
read_addr KEYWORD2
#######################################
# Instances (KEYWORD2)
#######################################
#######################################
# Constants (LITERAL1)
#######################################

10
library.properties Normal file
View File

@ -0,0 +1,10 @@
name= DLLN3X ZigBee Mesh Module Library
version= 1.0.0
author= Duke Liu <mentalflow@ourdocs.cn>
maintainer= Duke Liu <mentalflow@ourdocs.cn>
sentence= This library allows you to use DLLN3X ZigBee mesh module very easily.
paragraph= This library currently allows basic send and receive operations using the DLLN3X module, with more features to come.
category= Communication
architectures= *
includes= DLLN3X.h
license= MIT

BIN
src/.DS_Store vendored Normal file

Binary file not shown.

205
src/DLLN3X.cpp Normal file
View File

@ -0,0 +1,205 @@
#include "DLLN3X.h"
DLLN3X::DLLN3X(){}
DLLN3X::~DLLN3X() {}
void DLLN3X::init(HardwareSerial *DSerial)
{
_DSerial = DSerial;
_DSerial->begin(115200);
rled_blink();
_clear();
read_addr();
}
void DLLN3X::_clear()
{
while (_DSerial->available())
_DSerial->read();
}
int DLLN3X::_pack(unsigned char buf[], unsigned char data[], int length)
{
int count = 0;
for (int i = 0; i < length; i++)
{
if (data[i] == 0xFE)
{
buf[count++] = data[i];
buf[count++] = 0xFC;
}
else if (data[i] == 0xFF)
{
buf[count++] = 0xFE;
buf[count++] = 0xFD;
}
else
{
buf[count++] = data[i];
}
}
return count;
}
int DLLN3X::_depack(unsigned char buf[], unsigned char data[], int length)
{
int count = 0;
for (int i = 0; i < length; i++)
{
if (buf[i] != 0xFE)
{
data[count++] = buf[i];
}
else if (buf[i + 1] == 0xFD)
{
data[count++] = 0xFF;
i++;
}
else if (buf[i + 1] == 0xFC)
{
data[count++] = 0xFE;
i++;
}
}
return count;
}
void DLLN3X::recv(zigbee_frame *frame)
{
_recv_lock = true;
_DSerial->readBytesUntil(0xFF, (char *)frame, 64);
_DSerial->read();
_recv_lock = false;
}
bool DLLN3X::recv(unsigned char *orig_port,
unsigned char *dest_port, unsigned short *addr,
unsigned char data[], int *length)
{
_recv_lock = true;
unsigned char buf[200] = "";
unsigned char head = 0, tail = 0;
head = _DSerial->read();
if (head == 0xFE)
{
*length = _DSerial->read();
*length -= 4;
*orig_port = _DSerial->read();
*dest_port = _DSerial->read();
*addr = _DSerial->read();
*addr += _DSerial->read() << 8;
int count = 0;
for (int i = 0; i < *length; i++)
{
buf[count++] = _DSerial->read();
if (buf[count-1]==0xFE)
{
buf[count++]=_DSerial->read();
}
}
*length = count;
_DSerial->read(); // read pkg tail
*length = _depack(buf, data, *length);
_recv_lock = false;
return true;
}
else
{
_recv_lock = false;
return false;
}
}
bool DLLN3X::send(unsigned char orig_port,
unsigned char dest_port, unsigned short addr,
unsigned char data[], int length)
{
if (orig_port<0x80)
return false;
unsigned char send_buf[208] = {0xFE}, buf[200] = "";
unsigned char head = 0, buf_length = 0;
buf_length = _pack(buf, data, length);
send_buf[1] = length+4;
send_buf[2] = orig_port;
send_buf[3] = dest_port;
send_buf[4] = addr & 0xFF;
send_buf[5] = (addr & 0xFF00) >> 8;
memcpy(send_buf + 6, buf, buf_length);
send_buf[6 + buf_length] = 0xFF;
return _DSerial->write(send_buf, buf_length + 7);
}
void DLLN3X::rled_blink(unsigned char orig_port, unsigned short addr, unsigned char time)
{
uint8_t gap = 5;
for (int i = 0; i < time; i += gap)
{
if (i%2!=0)
send(orig_port, 0x20, addr, &gap, 1);
else
delay(gap*100);
}
}
unsigned short DLLN3X::read_addr()
{
if (self_addr!=0)
return self_addr;
unsigned char orig_port = 0xFF, dest_port = 0xFF;
unsigned char arg = 0x01;
unsigned short addr = 0xFFFF;
unsigned char data[60] = "";
int length = 0;
if (!_online)
{
_clear();
_online = true;
}
send(0x80, 0x21, 0x0000, &arg, 1);
recv(&orig_port, &dest_port, &addr, data, &length);
if (orig_port!=0x21||dest_port!=0x80||addr !=0x0000||length!=3||data[0]!=0x21)
{
return 0;
}
else
{
this->self_addr = data[1] + (data[2] << 8);
return this->self_addr;
}
}
bool DLLN3X::send_msg(unsigned char dest_port, unsigned short addr,
unsigned char data[], int length, unsigned char orig_port)
{
return send(orig_port, dest_port, addr, data, length);
}
void DLLN3X::loop()
{
unsigned char orig_port = 0xFF, dest_port = 0xFF;
unsigned short addr = 0xFFFF;
unsigned char data[200] = "";
int length = 0;
if (_DSerial->available()>0&&!_recv_lock)
{
recv(&orig_port,&dest_port,&addr,data,&length);
Serial.print("Message: ");
for (int i = 0; i < length;i++)
{
Serial.printf("%X ", data[i]);
}
Serial.printf("at port %X from %X:%X\n", dest_port, addr, orig_port);
if (_callback!=nullptr)
_callback(orig_port, dest_port, addr, data, length);
}
}
void DLLN3X::setcallback(void (*callback)(unsigned char orig_port,
unsigned char dest_port, unsigned short addr,
unsigned char data[], int length))
{
_callback = callback;
}

61
src/DLLN3X.h Normal file
View File

@ -0,0 +1,61 @@
#ifndef _DLLN3X_H_
#define _DLLN3X_H_
#include <Arduino.h>
typedef struct zigbee_frame
{
uint8_t length;
uint8_t src_port;
uint8_t dis_port;
uint8_t remote_addrL;
uint8_t remote_addrH;
uint8_t data[59];
}zigbee_frame;
#define new_zigbee_frame(num) \
struct \
{ \
uint8_t length; \
uint8_t src_port; \
uint8_t dis_port; \
uint8_t remote_addrL;\
uint8_t remote_addrH;\
uint8_t data[num]; \
}
class DLLN3X
{
private:
void _clear();
bool _online = false, _recv_lock = false;
int _pack(unsigned char buf[], unsigned char data[], int length);
int _depack(unsigned char buf[], unsigned char data[], int length);
HardwareSerial *_DSerial;
void (*_callback) (unsigned char orig_port,
unsigned char dest_port, unsigned short addr,
unsigned char data[], int length)=nullptr;
public:
DLLN3X();
~DLLN3X();
void init(HardwareSerial *DSerial = &Serial1);
void recv(zigbee_frame *frame);
bool recv(unsigned char *orig_port,
unsigned char *dest_port, unsigned short *addr,
unsigned char data[], int *length);
bool send(unsigned char orig_port,
unsigned char dest_port, unsigned short addr,
unsigned char data[], int length);
void rled_blink(unsigned char orig_port = 0x80,
unsigned short addr = 0x00, unsigned char time = 50);
bool send_msg(unsigned char dest_port, unsigned short addr,
unsigned char data[], int length, unsigned char orig_port = 0x80);
void loop();
void setcallback(void (*callback)(unsigned char orig_port,
unsigned char dest_port, unsigned short addr,
unsigned char data[], int length));
unsigned short read_addr();
unsigned short self_addr = 0x0000;
};
#endif