somfy.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // ***************************************************************************
  2. // Somfy module for ESP8266 with NodeMCU
  3. //
  4. // Written by Lukas Voborsky, @voborsky
  5. // based on https://github.com/Nickduino/Somfy_Remote
  6. // Somfy protocol description: https://pushstack.wordpress.com/somfy-rts-protocol/
  7. // and discussion: https://forum.arduino.cc/index.php?topic=208346.0
  8. //
  9. // MIT license, http://opensource.org/licenses/MIT
  10. // ***************************************************************************
  11. //#define NODE_DEBUG
  12. #include <stdint.h>
  13. #include "os_type.h"
  14. #include "osapi.h"
  15. #include "sections.h"
  16. #include "module.h"
  17. #include "lauxlib.h"
  18. #include "lmem.h"
  19. #include "platform.h"
  20. #include "task/task.h"
  21. #include "hw_timer.h"
  22. #include "user_interface.h"
  23. #define SYMBOL 640 // symbol width in microseconds
  24. #define SOMFY_UP 0x2
  25. #define SOMFY_STOP 0x1
  26. #define SOMFY_DOWN 0x4
  27. #define SOMFY_PROG 0x8
  28. #define DIRECT_WRITE_LOW(pin) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 0))
  29. #define DIRECT_WRITE_HIGH(pin) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 1))
  30. static const os_param_t TIMER_OWNER = 0x736f6d66; // "somf"
  31. static task_handle_t done_taskid;
  32. static uint8_t pin;
  33. static uint8_t frame[7];
  34. static uint8_t sync;
  35. static uint8_t repeat;
  36. //static uint32_t delay[10] = {9415, 89565, 4*SYMBOL, 4*SYMBOL, 4*SYMBOL, 4550, SYMBOL, SYMBOL, SYMBOL, 30415}; // in us
  37. // the `delay` array of constants must be in RAM as it is accessed from the timer interrupt
  38. static const RAM_CONST_SECTION_ATTR uint32_t delay[10] = {US_TO_RTC_TIMER_TICKS(9415), US_TO_RTC_TIMER_TICKS(89565), US_TO_RTC_TIMER_TICKS(4*SYMBOL), US_TO_RTC_TIMER_TICKS(4*SYMBOL), US_TO_RTC_TIMER_TICKS(4*SYMBOL), US_TO_RTC_TIMER_TICKS(4550), US_TO_RTC_TIMER_TICKS(SYMBOL), US_TO_RTC_TIMER_TICKS(SYMBOL), US_TO_RTC_TIMER_TICKS(SYMBOL), US_TO_RTC_TIMER_TICKS(30415)}; // in ticks (no need to recalculate)
  39. static uint8_t repeatindex;
  40. static uint8_t signalindex;
  41. static uint8_t subindex;
  42. static uint8_t bitcondition;
  43. int lua_done_ref; // callback when transmission is done
  44. void buildFrame(uint8_t *frame, uint64_t remote, uint8_t button, uint16_t code) {
  45. // NODE_DBG("remote: %x\n", remote);
  46. // NODE_DBG("button: %x\n", button);
  47. // NODE_DBG("rolling code: %x\n", code);
  48. frame[0] = 0xA7; // Encryption key. Doesn't matter much
  49. frame[1] = button << 4; // Which button did you press? The 4 LSB will be the checksum
  50. frame[2] = code >> 8; // Rolling code (big endian)
  51. frame[3] = code; // Rolling code
  52. frame[4] = remote >> 16; // Remote address
  53. frame[5] = remote >> 8; // Remote address
  54. frame[6] = remote; // Remote address
  55. // frame[7] = 0x80;
  56. // frame[8] = 0x0;
  57. // frame[9] = 0x0;
  58. // NODE_DBG("Frame:\t\t\t%02x %02x %02x %02x %02x %02x %02x\n", frame[0], frame[1], frame[2], frame[3], frame[4], frame[5], frame[6]);
  59. // Checksum calculation: a XOR of all the nibbles
  60. uint8_t checksum = 0;
  61. for(uint8_t i = 0; i < 7; i++) {
  62. checksum = checksum ^ frame[i] ^ (frame[i] >> 4);
  63. }
  64. checksum &= 0b1111; // We keep the last 4 bits only
  65. //Checksum integration
  66. frame[1] |= checksum; // If a XOR of all the nibbles is equal to 0, the blinds will consider the checksum ok.
  67. // NODE_DBG("With checksum:\t%02x %02x %02x %02x %02x %02x %02x\n", frame[0], frame[1], frame[2], frame[3], frame[4], frame[5], frame[6]);
  68. // Obfuscation: a XOR of all the uint8_ts
  69. for(uint8_t i = 1; i < 7; i++) {
  70. frame[i] ^= frame[i-1];
  71. }
  72. // NODE_DBG("Obfuscated:\t\t%02x %02x %02x %02x %02x %02x %02x\n", frame[0], frame[1], frame[2], frame[3], frame[4], frame[5], frame[6]);
  73. }
  74. static void somfy_transmissionDone (task_param_t arg)
  75. {
  76. lua_State *L = lua_getstate();
  77. lua_rawgeti (L, LUA_REGISTRYINDEX, lua_done_ref);
  78. luaL_unref (L, LUA_REGISTRYINDEX, lua_done_ref);
  79. lua_done_ref = LUA_NOREF;
  80. luaL_pcallx (L, 0, 0);
  81. }
  82. static void ICACHE_RAM_ATTR sendCommand(os_param_t p) {
  83. (void) p;
  84. // NODE_DBG("%d\t%d\n", signalindex, subindex);
  85. switch (signalindex) {
  86. case 0:
  87. subindex = 0;
  88. if(sync == 2) { // Only with the first frame.
  89. //Wake-up pulse & Silence
  90. DIRECT_WRITE_HIGH(pin);
  91. signalindex++;
  92. // delayMicroseconds(9415);
  93. break;
  94. } else {
  95. signalindex++; signalindex++; //no break means: go directly to step 3
  96. }
  97. case 1:
  98. //Wake-up pulse & Silence
  99. DIRECT_WRITE_LOW(pin);
  100. signalindex++;
  101. // delayMicroseconds(89565);
  102. break;
  103. case 2:
  104. signalindex++;
  105. // no break means go directly to step 3
  106. // a "useless" step to allow repeating the hardware sync w/o the silence after wake-up pulse
  107. case 3:
  108. // Hardware sync: two sync for the first frame, seven for the following ones.
  109. DIRECT_WRITE_HIGH(pin);
  110. signalindex++;
  111. // delayMicroseconds(4*SYMBOL);
  112. break;
  113. case 4:
  114. DIRECT_WRITE_LOW(pin);
  115. subindex++;
  116. if (subindex < sync) {signalindex--;} else {signalindex++;}
  117. // delayMicroseconds(4*SYMBOL);
  118. break;
  119. case 5:
  120. // Software sync
  121. DIRECT_WRITE_HIGH(pin);
  122. signalindex++;
  123. // delayMicroseconds(4550);
  124. break;
  125. case 6:
  126. DIRECT_WRITE_LOW(pin);
  127. signalindex++;
  128. subindex=0;
  129. // delayMicroseconds(SYMBOL);
  130. break;
  131. case 7:
  132. //Data: bits are sent one by one, starting with the MSB.
  133. bitcondition = ((frame[subindex/8] >> (7 - (subindex%8))) & 1) == 1;
  134. if(bitcondition) {
  135. DIRECT_WRITE_LOW(pin);
  136. }
  137. else {
  138. DIRECT_WRITE_HIGH(pin);
  139. }
  140. signalindex++;
  141. // delayMicroseconds(SYMBOL);
  142. break;
  143. case 8:
  144. //Data: bits are sent one by one, starting with the MSB.
  145. if(bitcondition) {
  146. DIRECT_WRITE_HIGH(pin);
  147. }
  148. else {
  149. DIRECT_WRITE_LOW(pin);
  150. }
  151. if (subindex<56) {
  152. subindex++;
  153. signalindex--;
  154. }
  155. else {
  156. signalindex++;
  157. }
  158. // delayMicroseconds(SYMBOL);
  159. break;
  160. case 9:
  161. DIRECT_WRITE_LOW(pin);
  162. signalindex++;
  163. // delayMicroseconds(30415); // Inter-frame silence
  164. break;
  165. case 10:
  166. repeatindex++;
  167. if (repeatindex<repeat) {
  168. DIRECT_WRITE_HIGH(pin); //start repeat from step 3, but don't wait as after step 1
  169. signalindex=4; subindex=0; sync=7;
  170. } else {
  171. platform_hw_timer_close(TIMER_OWNER);
  172. if (lua_done_ref != LUA_NOREF) {
  173. task_post_low (done_taskid, (task_param_t)0);
  174. }
  175. }
  176. break;
  177. }
  178. if (signalindex<10) {
  179. platform_hw_timer_arm_ticks(TIMER_OWNER, delay[signalindex-1]);
  180. }
  181. }
  182. static int somfy_lua_sendcommand(lua_State* L) { // pin, remote, command, rolling_code, num_repeat, callback
  183. if (!lua_isnumber(L, 4)) {
  184. return luaL_error(L, "wrong arg range");
  185. }
  186. pin = luaL_checkinteger(L, 1);
  187. uint64_t remote = luaL_checkinteger(L, 2);
  188. uint8_t cmd = luaL_checkinteger(L, 3);
  189. uint16_t code = luaL_checkinteger(L, 4);
  190. repeat=luaL_optint( L, 5, 2 );
  191. luaL_argcheck(L, platform_gpio_exists(pin), 1, "Invalid pin");
  192. luaL_unref(L, LUA_REGISTRYINDEX, lua_done_ref);
  193. if (!lua_isnoneornil(L, 6)) {
  194. lua_pushvalue(L, 6);
  195. lua_done_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  196. } else {
  197. lua_done_ref = LUA_NOREF;
  198. }
  199. MOD_CHECK_ID(gpio, pin);
  200. platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_PULLUP);
  201. buildFrame(frame, remote, cmd, code);
  202. if (!platform_hw_timer_init(TIMER_OWNER, FRC1_SOURCE, TRUE)) {
  203. // Failed to init the timer
  204. luaL_error(L, "Unable to initialize timer");
  205. }
  206. platform_hw_timer_set_func(TIMER_OWNER, sendCommand, 0);
  207. sync=2;
  208. signalindex=0; repeatindex=0;
  209. sendCommand(0);
  210. return 0;
  211. }
  212. LROT_BEGIN(somfy, NULL, 0)
  213. LROT_NUMENTRY( UP, SOMFY_UP )
  214. LROT_NUMENTRY( DOWN, SOMFY_DOWN )
  215. LROT_NUMENTRY( PROG, SOMFY_PROG )
  216. LROT_NUMENTRY( STOP, SOMFY_STOP )
  217. LROT_FUNCENTRY( sendcommand, somfy_lua_sendcommand )
  218. LROT_END(somfy, NULL, 0)
  219. int luaopen_somfy( lua_State *L ) {
  220. done_taskid = task_get_id((task_callback_t) somfy_transmissionDone);
  221. return 0;
  222. }
  223. NODEMCU_MODULE(SOMFY, "somfy", somfy, luaopen_somfy);