rotary.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Module for interfacing with cheap rotary switches that
  3. * are much used in the automtive industry as the cntrols for
  4. * CD players and the like.
  5. *
  6. * Philip Gladstone, N1DQ
  7. */
  8. #ifdef __ESP8266__
  9. #include "module.h"
  10. #include "lauxlib.h"
  11. #include "platform.h"
  12. #include "c_types.h"
  13. #include "user_interface.h"
  14. #include "driver/rotary.h"
  15. #include <stdlib.h>
  16. #define MASK(x) (1 << ROTARY_ ## x ## _INDEX)
  17. #define ROTARY_PRESS_INDEX 0
  18. #define ROTARY_LONGPRESS_INDEX 1
  19. #define ROTARY_RELEASE_INDEX 2
  20. #define ROTARY_TURN_INDEX 3
  21. #define ROTARY_CLICK_INDEX 4
  22. #define ROTARY_DBLCLICK_INDEX 5
  23. #define ROTARY_ALL 0x3f
  24. #define LONGPRESS_DELAY_US 500000
  25. #define CLICK_DELAY_US 500000
  26. #define CALLBACK_COUNT 6
  27. #ifdef LUA_USE_MODULES_ROTARY
  28. #if !defined(GPIO_INTERRUPT_ENABLE) || !defined(GPIO_INTERRUPT_HOOK_ENABLE)
  29. #error Must have GPIO_INTERRUPT and GPIO_INTERRUPT_HOOK if using ROTARY module
  30. #endif
  31. #endif
  32. typedef struct {
  33. int lastpos;
  34. int last_recent_event_was_press : 1;
  35. int last_recent_event_was_release : 1;
  36. int timer_running : 1;
  37. int possible_dbl_click : 1;
  38. uint8_t id;
  39. int click_delay_us;
  40. int longpress_delay_us;
  41. uint32_t last_event_time;
  42. int callback[CALLBACK_COUNT];
  43. os_timer_t timer;
  44. } DATA;
  45. static DATA *data[ROTARY_CHANNEL_COUNT];
  46. static task_handle_t tasknumber;
  47. static void lrotary_timer_done(void *param);
  48. static void lrotary_check_timer(DATA *d, uint32_t time_us, bool dotimer);
  49. static void callback_free_one(lua_State *L, int *cb_ptr)
  50. {
  51. if (*cb_ptr != LUA_NOREF) {
  52. luaL_unref(L, LUA_REGISTRYINDEX, *cb_ptr);
  53. *cb_ptr = LUA_NOREF;
  54. }
  55. }
  56. static void callback_free(lua_State* L, unsigned int id, int mask)
  57. {
  58. DATA *d = data[id];
  59. if (d) {
  60. int i;
  61. for (i = 0; i < CALLBACK_COUNT; i++) {
  62. if (mask & (1 << i)) {
  63. callback_free_one(L, &d->callback[i]);
  64. }
  65. }
  66. }
  67. }
  68. static int callback_setOne(lua_State* L, int *cb_ptr, int arg_number)
  69. {
  70. if (lua_type(L, arg_number) == LUA_TFUNCTION || lua_type(L, arg_number) == LUA_TLIGHTFUNCTION) {
  71. lua_pushvalue(L, arg_number); // copy argument (func) to the top of stack
  72. callback_free_one(L, cb_ptr);
  73. *cb_ptr = luaL_ref(L, LUA_REGISTRYINDEX);
  74. return 0;
  75. }
  76. return -1;
  77. }
  78. static int callback_set(lua_State* L, int id, int mask, int arg_number)
  79. {
  80. DATA *d = data[id];
  81. int result = 0;
  82. int i;
  83. for (i = 0; i < CALLBACK_COUNT; i++) {
  84. if (mask & (1 << i)) {
  85. result |= callback_setOne(L, &d->callback[i], arg_number);
  86. }
  87. }
  88. return result;
  89. }
  90. static void callback_callOne(lua_State* L, int cb, int mask, int arg, uint32_t time)
  91. {
  92. if (cb != LUA_NOREF) {
  93. lua_rawgeti(L, LUA_REGISTRYINDEX, cb);
  94. lua_pushinteger(L, mask);
  95. lua_pushinteger(L, arg);
  96. lua_pushinteger(L, time);
  97. lua_call(L, 3, 0);
  98. }
  99. }
  100. static void callback_call(lua_State* L, DATA *d, int cbnum, int arg, uint32_t time)
  101. {
  102. if (d) {
  103. callback_callOne(L, d->callback[cbnum], 1 << cbnum, arg, time);
  104. }
  105. }
  106. int platform_rotary_exists( unsigned int id )
  107. {
  108. return (id < ROTARY_CHANNEL_COUNT);
  109. }
  110. // Lua: setup(id, phase_a, phase_b [, press])
  111. static int lrotary_setup( lua_State* L )
  112. {
  113. unsigned int id;
  114. id = luaL_checkinteger( L, 1 );
  115. MOD_CHECK_ID( rotary, id );
  116. if (rotary_close(id)) {
  117. return luaL_error( L, "Unable to close switch." );
  118. }
  119. callback_free(L, id, ROTARY_ALL);
  120. if (!data[id]) {
  121. data[id] = (DATA *) zalloc(sizeof(DATA));
  122. if (!data[id]) {
  123. return -1;
  124. }
  125. }
  126. DATA *d = data[id];
  127. memset(d, 0, sizeof(*d));
  128. os_timer_setfn(&d->timer, lrotary_timer_done, (void *) d);
  129. int i;
  130. for (i = 0; i < CALLBACK_COUNT; i++) {
  131. d->callback[i] = LUA_NOREF;
  132. }
  133. d->click_delay_us = CLICK_DELAY_US;
  134. d->longpress_delay_us = LONGPRESS_DELAY_US;
  135. int phase_a = luaL_checkinteger(L, 2);
  136. luaL_argcheck(L, platform_gpio_exists(phase_a) && phase_a > 0, 2, "Invalid pin");
  137. int phase_b = luaL_checkinteger(L, 3);
  138. luaL_argcheck(L, platform_gpio_exists(phase_b) && phase_b > 0, 3, "Invalid pin");
  139. int press;
  140. if (lua_gettop(L) >= 4) {
  141. press = luaL_checkinteger(L, 4);
  142. luaL_argcheck(L, platform_gpio_exists(press) && press > 0, 4, "Invalid pin");
  143. } else {
  144. press = -1;
  145. }
  146. if (lua_gettop(L) >= 5) {
  147. d->longpress_delay_us = 1000 * luaL_checkinteger(L, 5);
  148. luaL_argcheck(L, d->longpress_delay_us > 0, 5, "Invalid timeout");
  149. }
  150. if (lua_gettop(L) >= 6) {
  151. d->click_delay_us = 1000 * luaL_checkinteger(L, 6);
  152. luaL_argcheck(L, d->click_delay_us > 0, 6, "Invalid timeout");
  153. }
  154. if (rotary_setup(id, phase_a, phase_b, press, tasknumber)) {
  155. return luaL_error(L, "Unable to setup rotary switch.");
  156. }
  157. return 0;
  158. }
  159. // Lua: close( id )
  160. static int lrotary_close( lua_State* L )
  161. {
  162. unsigned int id;
  163. id = luaL_checkinteger( L, 1 );
  164. MOD_CHECK_ID( rotary, id );
  165. callback_free(L, id, ROTARY_ALL);
  166. DATA *d = data[id];
  167. if (d) {
  168. data[id] = NULL;
  169. free(d);
  170. }
  171. if (rotary_close( id )) {
  172. return luaL_error( L, "Unable to close switch." );
  173. }
  174. return 0;
  175. }
  176. // Lua: on( id, mask[, cb] )
  177. static int lrotary_on( lua_State* L )
  178. {
  179. unsigned int id;
  180. id = luaL_checkinteger( L, 1 );
  181. MOD_CHECK_ID( rotary, id );
  182. int mask = luaL_checkinteger(L, 2);
  183. if (lua_gettop(L) >= 3) {
  184. if (callback_set(L, id, mask, 3)) {
  185. return luaL_error( L, "Unable to set callback." );
  186. }
  187. } else {
  188. callback_free(L, id, mask);
  189. }
  190. return 0;
  191. }
  192. // Lua: getpos( id ) -> pos, PRESS/RELEASE
  193. static int lrotary_getpos( lua_State* L )
  194. {
  195. unsigned int id;
  196. id = luaL_checkinteger( L, 1 );
  197. MOD_CHECK_ID( rotary, id );
  198. int pos = rotary_getpos(id);
  199. if (pos == -1) {
  200. return 0;
  201. }
  202. lua_pushnumber(L, (pos << 1) >> 1);
  203. lua_pushnumber(L, (pos & 0x80000000) ? MASK(PRESS) : MASK(RELEASE));
  204. return 2;
  205. }
  206. // Returns TRUE if there maybe/is more stuff to do
  207. static bool lrotary_dequeue_single(lua_State* L, DATA *d)
  208. {
  209. bool something_pending = FALSE;
  210. if (d) {
  211. // This chnnel is open
  212. rotary_event_t result;
  213. if (rotary_getevent(d->id, &result)) {
  214. int pos = result.pos;
  215. lrotary_check_timer(d, result.time_us, 0);
  216. if (pos != d->lastpos) {
  217. // We have something to enqueue
  218. if ((pos ^ d->lastpos) & 0x7fffffff) {
  219. // Some turning has happened
  220. callback_call(L, d, ROTARY_TURN_INDEX, (pos << 1) >> 1, result.time_us);
  221. }
  222. if ((pos ^ d->lastpos) & 0x80000000) {
  223. // pressing or releasing has happened
  224. callback_call(L, d, (pos & 0x80000000) ? ROTARY_PRESS_INDEX : ROTARY_RELEASE_INDEX, (pos << 1) >> 1, result.time_us);
  225. if (pos & 0x80000000) {
  226. // Press
  227. if (d->last_recent_event_was_release && result.time_us - d->last_event_time < d->click_delay_us) {
  228. d->possible_dbl_click = 1;
  229. }
  230. d->last_recent_event_was_press = 1;
  231. d->last_recent_event_was_release = 0;
  232. } else {
  233. // Release
  234. d->last_recent_event_was_press = 0;
  235. if (d->possible_dbl_click) {
  236. callback_call(L, d, ROTARY_DBLCLICK_INDEX, (pos << 1) >> 1, result.time_us);
  237. d->possible_dbl_click = 0;
  238. // Do this to suppress the CLICK event
  239. d->last_recent_event_was_release = 0;
  240. } else {
  241. d->last_recent_event_was_release = 1;
  242. }
  243. }
  244. d->last_event_time = result.time_us;
  245. }
  246. d->lastpos = pos;
  247. }
  248. something_pending = rotary_has_queued_event(d->id);
  249. }
  250. lrotary_check_timer(d, system_get_time(), 1);
  251. }
  252. return something_pending;
  253. }
  254. static void lrotary_timer_done(void *param)
  255. {
  256. DATA *d = (DATA *) param;
  257. d->timer_running = 0;
  258. lrotary_check_timer(d, system_get_time(), 1);
  259. }
  260. static void lrotary_check_timer(DATA *d, uint32_t time_us, bool dotimer)
  261. {
  262. uint32_t delay = time_us - d->last_event_time;
  263. if (d->timer_running) {
  264. os_timer_disarm(&d->timer);
  265. d->timer_running = 0;
  266. }
  267. int timeout = -1;
  268. if (d->last_recent_event_was_press) {
  269. if (delay > d->longpress_delay_us) {
  270. callback_call(lua_getstate(), d, ROTARY_LONGPRESS_INDEX, (d->lastpos << 1) >> 1, d->last_event_time + d->longpress_delay_us);
  271. d->last_recent_event_was_press = 0;
  272. } else {
  273. timeout = (d->longpress_delay_us - delay) / 1000;
  274. }
  275. }
  276. if (d->last_recent_event_was_release) {
  277. if (delay > d->click_delay_us) {
  278. callback_call(lua_getstate(), d, ROTARY_CLICK_INDEX, (d->lastpos << 1) >> 1, d->last_event_time + d->click_delay_us);
  279. d->last_recent_event_was_release = 0;
  280. } else {
  281. timeout = (d->click_delay_us - delay) / 1000;
  282. }
  283. }
  284. if (dotimer && timeout >= 0) {
  285. d->timer_running = 1;
  286. os_timer_arm(&d->timer, timeout + 1, 0);
  287. }
  288. }
  289. static void lrotary_task(task_param_t param, task_prio_t prio)
  290. {
  291. (void) param;
  292. (void) prio;
  293. uint8_t *task_queue_ptr = (uint8_t*) param;
  294. if (task_queue_ptr) {
  295. // Signal that new events may need another task post
  296. *task_queue_ptr = 0;
  297. }
  298. int id;
  299. bool need_to_post = FALSE;
  300. lua_State *L = lua_getstate();
  301. for (id = 0; id < ROTARY_CHANNEL_COUNT; id++) {
  302. DATA *d = data[id];
  303. if (d) {
  304. if (lrotary_dequeue_single(L, d)) {
  305. need_to_post = TRUE;
  306. }
  307. }
  308. }
  309. if (need_to_post) {
  310. // If there is pending stuff, queue another task
  311. task_post_medium(tasknumber, 0);
  312. }
  313. }
  314. static int rotary_open(lua_State *L)
  315. {
  316. tasknumber = task_get_id(lrotary_task);
  317. return 0;
  318. }
  319. // Module function map
  320. static const LUA_REG_TYPE rotary_map[] = {
  321. { LSTRKEY( "setup" ), LFUNCVAL( lrotary_setup ) },
  322. { LSTRKEY( "close" ), LFUNCVAL( lrotary_close ) },
  323. { LSTRKEY( "on" ), LFUNCVAL( lrotary_on ) },
  324. { LSTRKEY( "getpos" ), LFUNCVAL( lrotary_getpos) },
  325. { LSTRKEY( "TURN" ), LNUMVAL( MASK(TURN) ) },
  326. { LSTRKEY( "PRESS" ), LNUMVAL( MASK(PRESS) ) },
  327. { LSTRKEY( "RELEASE" ), LNUMVAL( MASK(RELEASE) ) },
  328. { LSTRKEY( "LONGPRESS" ),LNUMVAL( MASK(LONGPRESS) ) },
  329. { LSTRKEY( "CLICK" ), LNUMVAL( MASK(CLICK) ) },
  330. { LSTRKEY( "DBLCLICK" ), LNUMVAL( MASK(DBLCLICK)) },
  331. { LSTRKEY( "ALL" ), LNUMVAL( ROTARY_ALL ) },
  332. { LNILKEY, LNILVAL }
  333. };
  334. NODEMCU_MODULE(ROTARY, "rotary", rotary_map, rotary_open);
  335. #endif