#include "lualib.h"
#include "lauxlib.h"
#include "platform.h"
#include "gpio.h"
#include "auxmods.h"
#include "lrotable.h"
//#include "driver/easygpio.h"
//static Ping_Data pingA;
#define defPulseLen 185
#define defProtocol 1
#define defRepeat 10
#define defBits 24
// Number of maximum High/Low changes per packet. ESP8266 should able to handle more
// We can handle up to (unsigned long) => 32 bit * 2 H/L changes per bit + 2 for sync
#define RCSWITCH_MAX_CHANGES 67
#define PROTOCOL3_SYNC_FACTOR 71
#define PROTOCOL3_0_HIGH_CYCLES 4
#define PROTOCOL3_0_LOW_CYCLES 11
#define PROTOCOL3_1_HIGH_CYCLES 9
#define PROTOCOL3_1_LOW_CYCLES 6
static unsigned int timings[RCSWITCH_MAX_CHANGES];
//int nReceiverInterrupt;
//int nTransmitterPin;
//int nPulseLength;
//int nRepeatTransmit;
//char nProtocol;
static int nReceiveTolerance = 60;
static unsigned long nReceivedValue = 0;
static unsigned int nReceivedBitlength = 0;
static unsigned int nReceivedDelay = 0;
static unsigned int nReceivedProtocol = 1;
static void ICACHE_FLASH_ATTR transmit(int pin, int pulseLen, int nHighPulses, int nLowPulses) {
platform_gpio_write(pin, 1);
os_delay_us(pulseLen*nHighPulses);
platform_gpio_write(pin, 0);
os_delay_us(pulseLen*nLowPulses);
}
//rc.send(4,267715,24,185,1,10) --GPIO, code, bits, pulselen, protocol, repeat
static int ICACHE_FLASH_ATTR rc_send(lua_State* L) {
const uint8_t pin = luaL_checkinteger(L, 1);
platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
//platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_PULLUP);
//platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_PULLDOWN);
platform_gpio_write(pin, 0);
long code = luaL_checklong(L, 2);
//const uint8_t bits = luaL_checkinteger(L, 3);
uint8_t bits = luaL_checkinteger(L, 3);
const uint8_t pulseLen = luaL_checkinteger(L, 4);
const uint8_t Protocol = luaL_checkinteger(L, 5);
const uint8_t repeat = luaL_checkinteger(L, 6);
NODE_ERR("pulseLen:%d\n",pulseLen);
NODE_ERR("Protocol:%d\n",Protocol);
NODE_ERR("repeat:%d\n",repeat);
NODE_ERR("send:");
int c,k,nRepeat;
bits = bits-1;
for (c = bits; c >= 0; c--)
{
k = code >> c;
if (k & 1)
NODE_ERR("1");
else
NODE_ERR("0");
}
NODE_ERR("\n");
for (nRepeat=0; nRepeat<repeat; nRepeat++) {
for (c = bits; c >= 0; c--)
{
k = code >> c;
if (k & 1){
//send1
if(Protocol==1){
transmit(pin,pulseLen,3,1);
}else if(Protocol==2){
transmit(pin,pulseLen,2,1);
}else if(Protocol==3){
transmit(pin,pulseLen,9,6);
}
}
else{
//send0
if(Protocol==1){
transmit(pin,pulseLen,1,3);
}else if(Protocol==2){
transmit(pin,pulseLen,1,2);
}else if(Protocol==3){
transmit(pin,pulseLen,4,11);
}
}
}
//sendSync();
if(Protocol==1){
transmit(pin,pulseLen,1,31);
}else if(Protocol==2){
transmit(pin,pulseLen,1,10);
}else if(Protocol==3){
transmit(pin,pulseLen,1,71);
}
}
return 1;
}
//Learn 433MHz codes
void printCode(){
NODE_ERR("Protocol:%d\n",nReceivedProtocol);
NODE_ERR("pulseLen:%d\n",nReceivedDelay); //??
NODE_ERR("Bitlength:%d\n",nReceivedBitlength);
NODE_ERR("Code:%d\n",nReceivedValue);
//NODE_ERR("repeat:%d\n",repeat);
}
bool receiveProtocol1(unsigned int changeCount){
unsigned long code = 0;
unsigned long delay = timings[0] / 31;
unsigned long delayTolerance = delay * nReceiveTolerance * 0.01;
int i = 1;
for (i; i<changeCount ; i=i+2) {
if (timings[i] > delay-delayTolerance && timings[i] < delay+delayTolerance && timings[i+1] > delay*3-delayTolerance && timings[i+1] < delay*3+delayTolerance) {
code = code << 1;
} else if (timings[i] > delay*3-delayTolerance && timings[i] < delay*3+delayTolerance && timings[i+1] > delay-delayTolerance && timings[i+1] < delay+delayTolerance) {
code+=1;
code = code << 1;
} else {
// Failed
i = changeCount;
code = 0;
}
}
code = code >> 1;
if (changeCount > 6) { // ignore < 4bit values as there are no devices sending 4bit values => noise
nReceivedValue = code;
nReceivedBitlength = changeCount / 2;
nReceivedDelay = delay;
nReceivedProtocol = 1;
}
if (code == 0){
return false;
}else if (code != 0){
//NODE_ERR("Protocol:1");
printCode();
return true;
}
}
bool receiveProtocol2(unsigned int changeCount){
unsigned long code = 0;
unsigned long delay = timings[0] / 10;
unsigned long delayTolerance = delay * nReceiveTolerance * 0.01;
int i = 1;
for (i; i<changeCount ; i=i+2) {
if (timings[i] > delay-delayTolerance && timings[i] < delay+delayTolerance && timings[i+1] > delay*2-delayTolerance && timings[i+1] < delay*2+delayTolerance) {
code = code << 1;
} else if (timings[i] > delay*2-delayTolerance && timings[i] < delay*2+delayTolerance && timings[i+1] > delay-delayTolerance && timings[i+1] < delay+delayTolerance) {
code+=1;
code = code << 1;
} else {
// Failed
i = changeCount;
code = 0;
}
}
code = code >> 1;
if (changeCount > 6) { // ignore < 4bit values as there are no devices sending 4bit values => noise
nReceivedValue = code;
nReceivedBitlength = changeCount / 2;
nReceivedDelay = delay;
nReceivedProtocol = 2;
}
if (code == 0){
return false;
}else if (code != 0){
//NODE_ERR("Protocol:2");
printCode();
return true;
}
}
bool receiveProtocol3(unsigned int changeCount){
unsigned long code = 0;
unsigned long delay = timings[0] / PROTOCOL3_SYNC_FACTOR;
unsigned long delayTolerance = delay * nReceiveTolerance * 0.01;
int i = 1;
for ( i; i<changeCount ; i=i+2) {
if (timings[i] > delay*PROTOCOL3_0_HIGH_CYCLES - delayTolerance
&& timings[i] < delay*PROTOCOL3_0_HIGH_CYCLES + delayTolerance
&& timings[i+1] > delay*PROTOCOL3_0_LOW_CYCLES - delayTolerance
&& timings[i+1] < delay*PROTOCOL3_0_LOW_CYCLES + delayTolerance) {
code = code << 1;
} else if (timings[i] > delay*PROTOCOL3_1_HIGH_CYCLES - delayTolerance
&& timings[i] < delay*PROTOCOL3_1_HIGH_CYCLES + delayTolerance
&& timings[i+1] > delay*PROTOCOL3_1_LOW_CYCLES - delayTolerance
&& timings[i+1] < delay*PROTOCOL3_1_LOW_CYCLES + delayTolerance) {
code+=1;
code = code << 1;
} else {
// Failed
i = changeCount;
code = 0;
}
}
code = code >> 1;
if (changeCount > 6) { // ignore < 4bit values as there are no devices sending 4bit values => noise
nReceivedValue = code;
nReceivedBitlength = changeCount / 2;
nReceivedDelay = delay;
nReceivedProtocol = 3;
}
if (code == 0){
return false;
}else if (code != 0){
//NODE_ERR("Protocol:3");
printCode();
return true;
}
}
LOCAL void intr_handler(){
static unsigned int duration;
static unsigned int changeCount;
static unsigned long lastTime;
static unsigned int repeatCount;
long time = system_get_time();
duration = time - lastTime;
if (duration > 5000 && duration > timings[0] - 200 && duration < timings[0] + 200) {
repeatCount++;
changeCount--;
if (repeatCount == 2) {
if (receiveProtocol1(changeCount) == false){
if (receiveProtocol2(changeCount) == false){
if (receiveProtocol3(changeCount) == false){
//failed
NODE_ERR("Can not find code.\n");
}
}
}
repeatCount = 0;
}
changeCount = 0;
} else if (duration > 5000) {
changeCount = 0;
}
if (changeCount >= RCSWITCH_MAX_CHANGES) {
changeCount = 0;
repeatCount = 0;
}
timings[changeCount++] = duration;
lastTime = time;
//NODE_ERR("time:%d\n",time);
}
/*
static int ICACHE_FLASH_ATTR rc_learn(lua_State* L) {
const uint8_t pin = luaL_checkinteger(L, 1);
//PIN_PULLUP_EN(PERIPHS_IO_MUX_M
评论0