2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2015-06-12 00:05:18 +02:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
2019-07-23 21:46:49 +02:00
|
|
|
#include "user_interface.h"
|
2016-01-20 09:37:03 +01:00
|
|
|
#include "rom.h"
|
2019-07-23 21:46:49 +02:00
|
|
|
|
2015-06-12 00:05:18 +02:00
|
|
|
//#include "driver/easygpio.h"
|
|
|
|
//static Ping_Data pingA;
|
|
|
|
#define defPulseLen 185
|
|
|
|
#define defProtocol 1
|
|
|
|
#define defRepeat 10
|
|
|
|
#define defBits 24
|
2015-06-27 20:42:39 +02:00
|
|
|
static void ICACHE_FLASH_ATTR transmit(int pin, int pulseLen, int nHighPulses, int nLowPulses) {
|
2015-06-12 00:05:18 +02:00
|
|
|
platform_gpio_write(pin, 1);
|
|
|
|
os_delay_us(pulseLen*nHighPulses);
|
|
|
|
platform_gpio_write(pin, 0);
|
|
|
|
os_delay_us(pulseLen*nLowPulses);
|
|
|
|
}
|
2015-06-27 20:42:39 +02:00
|
|
|
//rc.send(4,267715,24,185,1,10) --GPIO, code, bits, pulselen, protocol, repeat
|
2015-06-12 00:05:18 +02:00
|
|
|
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;
|
|
|
|
}
|
2015-12-12 04:27:31 +01:00
|
|
|
|
2015-12-13 03:29:37 +01:00
|
|
|
// Module function map
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_BEGIN(rc)
|
|
|
|
LROT_FUNCENTRY( send, rc_send )
|
|
|
|
LROT_END( rc, NULL, 0 )
|
|
|
|
|
2015-06-12 00:05:18 +02:00
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
int luaopen_rc(lua_State *L) {
|
2015-06-12 00:05:18 +02:00
|
|
|
// TODO: Make sure that the GPIO system is initialized
|
2015-12-12 04:27:31 +01:00
|
|
|
return 0;
|
2015-06-12 00:05:18 +02:00
|
|
|
}
|
2015-12-16 06:04:58 +01:00
|
|
|
|
2019-05-08 13:08:20 +02:00
|
|
|
NODEMCU_MODULE(RC, "rc", rc, luaopen_rc);
|