rotary.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 "task/task.h"
  12. #include <stdint.h>
  13. #include <stdlib.h>
  14. #include "user_interface.h"
  15. #include "driver/rotary.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. ETSTimer 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_isfunction(L, arg_number)) {
  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. luaL_pcallx(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. #include "pm/swtimer.h"
  111. // Lua: setup(id, phase_a, phase_b [, press])
  112. static int lrotary_setup( lua_State* L )
  113. {
  114. unsigned int id;
  115. id = luaL_checkinteger( L, 1 );
  116. MOD_CHECK_ID( rotary, id );
  117. if (rotary_close(id)) {
  118. return luaL_error( L, "Unable to close switch." );
  119. }
  120. callback_free(L, id, ROTARY_ALL);
  121. if (!data[id]) {
  122. data[id] = (DATA *) calloc(1, sizeof(DATA));
  123. if (!data[id]) {
  124. return -1;
  125. }
  126. }
  127. DATA *d = data[id];
  128. memset(d, 0, sizeof(*d));
  129. d->id = id;
  130. os_timer_setfn(&d->timer, lrotary_timer_done, (void *) d);
  131. SWTIMER_REG_CB(lrotary_timer_done, SWTIMER_RESUME);
  132. //lrotary_timer_done checks time elapsed since last event
  133. //My guess: Since proper functionality relies on some variables to be reset via timer callback and state would be invalid anyway.
  134. //It is probably best to resume this timer so it can reset it's state variables
  135. int i;
  136. for (i = 0; i < CALLBACK_COUNT; i++) {
  137. d->callback[i] = LUA_NOREF;
  138. }
  139. d->click_delay_us = CLICK_DELAY_US;
  140. d->longpress_delay_us = LONGPRESS_DELAY_US;
  141. int phase_a = luaL_checkinteger(L, 2);
  142. luaL_argcheck(L, platform_gpio_exists(phase_a) && phase_a > 0, 2, "Invalid pin");
  143. int phase_b = luaL_checkinteger(L, 3);
  144. luaL_argcheck(L, platform_gpio_exists(phase_b) && phase_b > 0, 3, "Invalid pin");
  145. int press;
  146. if (lua_gettop(L) >= 4) {
  147. press = luaL_checkinteger(L, 4);
  148. luaL_argcheck(L, platform_gpio_exists(press) && press > 0, 4, "Invalid pin");
  149. } else {
  150. press = -1;
  151. }
  152. if (lua_gettop(L) >= 5) {
  153. d->longpress_delay_us = 1000 * luaL_checkinteger(L, 5);
  154. luaL_argcheck(L, d->longpress_delay_us > 0, 5, "Invalid timeout");
  155. }
  156. if (lua_gettop(L) >= 6) {
  157. d->click_delay_us = 1000 * luaL_checkinteger(L, 6);
  158. luaL_argcheck(L, d->click_delay_us > 0, 6, "Invalid timeout");
  159. }
  160. if (rotary_setup(id, phase_a, phase_b, press, tasknumber)) {
  161. return luaL_error(L, "Unable to setup rotary switch.");
  162. }
  163. return 0;
  164. }
  165. // Lua: close( id )
  166. static int lrotary_close( lua_State* L )
  167. {
  168. unsigned int id;
  169. id = luaL_checkinteger( L, 1 );
  170. MOD_CHECK_ID( rotary, id );
  171. callback_free(L, id, ROTARY_ALL);
  172. DATA *d = data[id];
  173. if (d) {
  174. data[id] = NULL;
  175. free(d);
  176. }
  177. if (rotary_close( id )) {
  178. return luaL_error( L, "Unable to close switch." );
  179. }
  180. return 0;
  181. }
  182. // Lua: on( id, mask[, cb] )
  183. static int lrotary_on( lua_State* L )
  184. {
  185. unsigned int id;
  186. id = luaL_checkinteger( L, 1 );
  187. MOD_CHECK_ID( rotary, id );
  188. int mask = luaL_checkinteger(L, 2);
  189. if (lua_gettop(L) >= 3) {
  190. if (callback_set(L, id, mask, 3)) {
  191. return luaL_error( L, "Unable to set callback." );
  192. }
  193. } else {
  194. callback_free(L, id, mask);
  195. }
  196. return 0;
  197. }
  198. // Lua: getpos( id ) -> pos, PRESS/RELEASE
  199. static int lrotary_getpos( lua_State* L )
  200. {
  201. unsigned int id;
  202. id = luaL_checkinteger( L, 1 );
  203. MOD_CHECK_ID( rotary, id );
  204. int pos = rotary_getpos(id);
  205. if (pos == -1) {
  206. return 0;
  207. }
  208. lua_pushnumber(L, (pos << 1) >> 1);
  209. lua_pushnumber(L, (pos & 0x80000000) ? MASK(PRESS) : MASK(RELEASE));
  210. return 2;
  211. }
  212. // Returns TRUE if there maybe/is more stuff to do
  213. static bool lrotary_dequeue_single(lua_State* L, DATA *d)
  214. {
  215. bool something_pending = FALSE;
  216. if (d) {
  217. // This chnnel is open
  218. rotary_event_t result;
  219. if (rotary_getevent(d->id, &result)) {
  220. int pos = result.pos;
  221. lrotary_check_timer(d, result.time_us, 0);
  222. if (pos != d->lastpos) {
  223. // We have something to enqueue
  224. if ((pos ^ d->lastpos) & 0x7fffffff) {
  225. // Some turning has happened
  226. callback_call(L, d, ROTARY_TURN_INDEX, (pos << 1) >> 1, result.time_us);
  227. }
  228. if ((pos ^ d->lastpos) & 0x80000000) {
  229. // pressing or releasing has happened
  230. callback_call(L, d, (pos & 0x80000000) ? ROTARY_PRESS_INDEX : ROTARY_RELEASE_INDEX, (pos << 1) >> 1, result.time_us);
  231. if (pos & 0x80000000) {
  232. // Press
  233. if (d->last_recent_event_was_release && result.time_us - d->last_event_time < d->click_delay_us) {
  234. d->possible_dbl_click = 1;
  235. }
  236. d->last_recent_event_was_press = 1;
  237. d->last_recent_event_was_release = 0;
  238. } else {
  239. // Release
  240. d->last_recent_event_was_press = 0;
  241. if (d->possible_dbl_click) {
  242. callback_call(L, d, ROTARY_DBLCLICK_INDEX, (pos << 1) >> 1, result.time_us);
  243. d->possible_dbl_click = 0;
  244. // Do this to suppress the CLICK event
  245. d->last_recent_event_was_release = 0;
  246. } else {
  247. d->last_recent_event_was_release = 1;
  248. }
  249. }
  250. d->last_event_time = result.time_us;
  251. }
  252. d->lastpos = pos;
  253. }
  254. something_pending = rotary_has_queued_event(d->id);
  255. }
  256. lrotary_check_timer(d, system_get_time(), 1);
  257. }
  258. return something_pending;
  259. }
  260. static void lrotary_timer_done(void *param)
  261. {
  262. DATA *d = (DATA *) param;
  263. d->timer_running = 0;
  264. lrotary_check_timer(d, system_get_time(), 1);
  265. }
  266. static void lrotary_check_timer(DATA *d, uint32_t time_us, bool dotimer)
  267. {
  268. uint32_t delay = time_us - d->last_event_time;
  269. if (d->timer_running) {
  270. os_timer_disarm(&d->timer);
  271. d->timer_running = 0;
  272. }
  273. int timeout = -1;
  274. if (d->last_recent_event_was_press) {
  275. if (delay > d->longpress_delay_us) {
  276. callback_call(lua_getstate(), d, ROTARY_LONGPRESS_INDEX, (d->lastpos << 1) >> 1, d->last_event_time + d->longpress_delay_us);
  277. d->last_recent_event_was_press = 0;
  278. } else {
  279. timeout = (d->longpress_delay_us - delay) / 1000;
  280. }
  281. }
  282. if (d->last_recent_event_was_release) {
  283. if (delay > d->click_delay_us) {
  284. callback_call(lua_getstate(), d, ROTARY_CLICK_INDEX, (d->lastpos << 1) >> 1, d->last_event_time + d->click_delay_us);
  285. d->last_recent_event_was_release = 0;
  286. } else {
  287. timeout = (d->click_delay_us - delay) / 1000;
  288. }
  289. }
  290. if (dotimer && timeout >= 0) {
  291. d->timer_running = 1;
  292. os_timer_arm(&d->timer, timeout + 1, 0);
  293. }
  294. }
  295. static void lrotary_task(os_param_t param, uint8_t prio)
  296. {
  297. (void) param;
  298. (void) prio;
  299. uint8_t *task_queue_ptr = (uint8_t*) param;
  300. if (task_queue_ptr) {
  301. // Signal that new events may need another task post
  302. *task_queue_ptr = 0;
  303. }
  304. int id;
  305. bool need_to_post = FALSE;
  306. lua_State *L = lua_getstate();
  307. for (id = 0; id < ROTARY_CHANNEL_COUNT; id++) {
  308. DATA *d = data[id];
  309. if (d) {
  310. if (lrotary_dequeue_single(L, d)) {
  311. need_to_post = TRUE;
  312. }
  313. }
  314. }
  315. if (need_to_post) {
  316. // If there is pending stuff, queue another task
  317. task_post_medium(tasknumber, 0);
  318. }
  319. }
  320. static int rotary_open(lua_State *L)
  321. {
  322. tasknumber = task_get_id(lrotary_task);
  323. return 0;
  324. }
  325. // Module function map
  326. LROT_BEGIN(rotary, NULL, 0)
  327. LROT_FUNCENTRY( setup, lrotary_setup )
  328. LROT_FUNCENTRY( close, lrotary_close )
  329. LROT_FUNCENTRY( on, lrotary_on )
  330. LROT_FUNCENTRY( getpos, lrotary_getpos )
  331. LROT_NUMENTRY( TURN, MASK(TURN) )
  332. LROT_NUMENTRY( PRESS, MASK(PRESS) )
  333. LROT_NUMENTRY( RELEASE, MASK(RELEASE) )
  334. LROT_NUMENTRY( LONGPRESS, MASK(LONGPRESS) )
  335. LROT_NUMENTRY( CLICK, MASK(CLICK) )
  336. LROT_NUMENTRY( DBLCLICK, MASK(DBLCLICK) )
  337. LROT_NUMENTRY( ALL, ROTARY_ALL )
  338. LROT_END(rotary, NULL, 0)
  339. NODEMCU_MODULE(ROTARY, "rotary", rotary, rotary_open);