rotary.c 9.8 KB

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