rc.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "module.h"
  2. #include "lauxlib.h"
  3. #include "platform.h"
  4. #include "rom.h"
  5. //#include "driver/easygpio.h"
  6. //static Ping_Data pingA;
  7. #define defPulseLen 185
  8. #define defProtocol 1
  9. #define defRepeat 10
  10. #define defBits 24
  11. static void ICACHE_FLASH_ATTR transmit(int pin, int pulseLen, int nHighPulses, int nLowPulses) {
  12. platform_gpio_write(pin, 1);
  13. os_delay_us(pulseLen*nHighPulses);
  14. platform_gpio_write(pin, 0);
  15. os_delay_us(pulseLen*nLowPulses);
  16. }
  17. //rc.send(4,267715,24,185,1,10) --GPIO, code, bits, pulselen, protocol, repeat
  18. static int ICACHE_FLASH_ATTR rc_send(lua_State* L) {
  19. const uint8_t pin = luaL_checkinteger(L, 1);
  20. platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
  21. //platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_PULLUP);
  22. //platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_PULLDOWN);
  23. platform_gpio_write(pin, 0);
  24. long code = luaL_checklong(L, 2);
  25. //const uint8_t bits = luaL_checkinteger(L, 3);
  26. uint8_t bits = luaL_checkinteger(L, 3);
  27. const uint8_t pulseLen = luaL_checkinteger(L, 4);
  28. const uint8_t Protocol = luaL_checkinteger(L, 5);
  29. const uint8_t repeat = luaL_checkinteger(L, 6);
  30. NODE_ERR("pulseLen:%d\n",pulseLen);
  31. NODE_ERR("Protocol:%d\n",Protocol);
  32. NODE_ERR("repeat:%d\n",repeat);
  33. NODE_ERR("send:");
  34. int c,k,nRepeat;
  35. bits = bits-1;
  36. for (c = bits; c >= 0; c--)
  37. {
  38. k = code >> c;
  39. if (k & 1)
  40. NODE_ERR("1");
  41. else
  42. NODE_ERR("0");
  43. }
  44. NODE_ERR("\n");
  45. for (nRepeat=0; nRepeat<repeat; nRepeat++) {
  46. for (c = bits; c >= 0; c--)
  47. {
  48. k = code >> c;
  49. if (k & 1){
  50. //send1
  51. if(Protocol==1){
  52. transmit(pin,pulseLen,3,1);
  53. }else if(Protocol==2){
  54. transmit(pin,pulseLen,2,1);
  55. }else if(Protocol==3){
  56. transmit(pin,pulseLen,9,6);
  57. }
  58. }
  59. else{
  60. //send0
  61. if(Protocol==1){
  62. transmit(pin,pulseLen,1,3);
  63. }else if(Protocol==2){
  64. transmit(pin,pulseLen,1,2);
  65. }else if(Protocol==3){
  66. transmit(pin,pulseLen,4,11);
  67. }
  68. }
  69. }
  70. //sendSync();
  71. if(Protocol==1){
  72. transmit(pin,pulseLen,1,31);
  73. }else if(Protocol==2){
  74. transmit(pin,pulseLen,1,10);
  75. }else if(Protocol==3){
  76. transmit(pin,pulseLen,1,71);
  77. }
  78. }
  79. return 1;
  80. }
  81. // Module function map
  82. static const LUA_REG_TYPE rc_map[] = {
  83. { LSTRKEY( "send" ), LFUNCVAL( rc_send )},
  84. { LNILKEY, LNILVAL}
  85. };
  86. int luaopen_rc(lua_State *L) {
  87. // TODO: Make sure that the GPIO system is initialized
  88. return 0;
  89. }
  90. NODEMCU_MODULE(RC, "rc", rc_map, luaopen_rc);