luat_lib_lora.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. @module lora2
  3. @summary lora2驱动模块(支持多挂)
  4. @version 1.0
  5. @date 2022.06.24
  6. @demo lora
  7. @tag LUAT_USE_LORA2
  8. */
  9. #include "luat_base.h"
  10. #include "luat_rtos.h"
  11. #include "luat_gpio.h"
  12. #include "luat_spi.h"
  13. #include "luat_mem.h"
  14. #include "sx126x/radio.h"
  15. #include "sx126x/sx126x.h"
  16. #include "sx126x/sx126x-board.h"
  17. #define LUAT_LOG_TAG "lora"
  18. #include "luat_log.h"
  19. typedef struct lora_rx_data
  20. {
  21. uint16_t size;
  22. int16_t rssi;
  23. int8_t snr;
  24. char buff[1];
  25. }lora_rx_data_t;
  26. enum{
  27. LORA_TX_DONE,
  28. LORA_RX_DONE,
  29. LORA_TX_TIMEOUT,
  30. LORA_RX_TIMEOUT,
  31. LORA_RX_ERROR,
  32. };
  33. #define LUAT_LORA_TYPE "LORA*"
  34. static lora_device_t * get_lora_device(lua_State *L){
  35. if (luaL_testudata(L, 1, LUAT_LORA_TYPE)){
  36. return ((lora_device_t *)luaL_checkudata(L, 1, LUAT_LORA_TYPE));
  37. }else{
  38. return ((lora_device_t *)lua_touserdata(L, 1));
  39. }
  40. }
  41. static int l_lora_handler(lua_State* L, void* ptr) {
  42. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  43. lora_device_t *lora_device =(lora_device_t *)msg->ptr;
  44. int event = msg->arg1;
  45. lua_geti(L, LUA_REGISTRYINDEX, lora_device->lora_cb);
  46. if (lua_isfunction(L, -1)) {
  47. lua_geti(L, LUA_REGISTRYINDEX, lora_device->lora_ref);
  48. switch (event){
  49. case LORA_TX_DONE:
  50. lua_pushstring(L, "tx_done");
  51. lua_call(L, 2, 0);
  52. break;
  53. case LORA_RX_DONE:
  54. lua_pushstring(L, "rx_done");
  55. lora_rx_data_t* rx_buff = (lora_rx_data_t*)msg->arg2;
  56. lua_pushlstring(L, (const char *)rx_buff->buff,rx_buff->size);
  57. lua_pushinteger(L, rx_buff->size);
  58. lua_pushinteger(L, rx_buff->rssi);
  59. lua_pushinteger(L, rx_buff->snr);
  60. lua_call(L, 6, 0);
  61. luat_heap_free(rx_buff);
  62. break;
  63. case LORA_TX_TIMEOUT:
  64. lua_pushstring(L, "tx_timeout");
  65. lua_call(L, 2, 0);
  66. break;
  67. case LORA_RX_TIMEOUT:
  68. lua_pushstring(L, "rx_timeout");
  69. lua_call(L, 2, 0);
  70. break;
  71. case LORA_RX_ERROR:
  72. lua_pushstring(L, "rx_error");
  73. lua_call(L, 2, 0);
  74. break;
  75. }
  76. }
  77. return 0;
  78. }
  79. static void OnTxDone( lora_device_t* lora_device ){
  80. rtos_msg_t msg = {0};
  81. msg.handler = l_lora_handler;
  82. msg.ptr = lora_device;
  83. msg.arg1 = LORA_TX_DONE;
  84. msg.arg2 = 0;
  85. luat_msgbus_put(&msg, 1);
  86. }
  87. static void OnRxDone( lora_device_t* lora_device,uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ){
  88. // LLOGD("RxDone size:%d rssi:%d snr:%d",size,rssi,snr);
  89. // LLOGD("RxDone payload: %.*s",size,payload);
  90. lora_rx_data_t* rx_buff = luat_heap_malloc(sizeof(lora_rx_data_t)+ size);
  91. rx_buff->size = size;
  92. rx_buff->rssi = rssi;
  93. rx_buff->snr = snr;
  94. memcpy( rx_buff->buff, payload, size );
  95. rtos_msg_t msg = {0};
  96. msg.handler = l_lora_handler;
  97. msg.ptr = lora_device;
  98. msg.arg1 = LORA_RX_DONE;
  99. msg.arg2 = rx_buff;
  100. luat_msgbus_put(&msg, 1);
  101. }
  102. static void OnTxTimeout( lora_device_t* lora_device ){
  103. rtos_msg_t msg = {0};
  104. msg.handler = l_lora_handler;
  105. msg.ptr = lora_device;
  106. msg.arg1 = LORA_TX_TIMEOUT;
  107. msg.arg2 = 0;
  108. luat_msgbus_put(&msg, 1);
  109. }
  110. static void OnRxTimeout( lora_device_t* lora_device ){
  111. rtos_msg_t msg = {0};
  112. msg.handler = l_lora_handler;
  113. msg.ptr = lora_device;
  114. msg.arg1 = LORA_RX_TIMEOUT;
  115. msg.arg2 = 0;
  116. luat_msgbus_put(&msg, 1);
  117. }
  118. static void OnRxError( lora_device_t* lora_device ){
  119. rtos_msg_t msg = {0};
  120. msg.handler = l_lora_handler;
  121. msg.ptr = lora_device;
  122. msg.arg1 = LORA_RX_ERROR;
  123. msg.arg2 = 0;
  124. luat_msgbus_put(&msg, 1);
  125. }
  126. #define META_SPI "SPI*"
  127. /*
  128. lora初始化
  129. @api lora2.init(ic, loraconfig,spiconfig)
  130. @string lora 型号,当前支持:<br>llcc68<br>sx1268
  131. @table lora配置参数,与具体设备有关
  132. @return userdata 若成功会返回lora对象,否则返回nil
  133. @usage
  134. spi_lora = spi.deviceSetup(spi_id,pin_cs,0,0,8,10*1000*1000,spi.MSB,1,0)
  135. lora_device = lora2.init("llcc68",{res = pin_reset,busy = pin_busy,dio1 = pin_dio1},spi_lora)
  136. */
  137. static int luat_lora_init(lua_State *L){
  138. size_t len = 0;
  139. const char* lora_ic = luaL_checklstring(L, 1, &len);
  140. if(strcmp("llcc68",lora_ic)== 0||strcmp("LLCC68",lora_ic)== 0||strcmp("sx1268",lora_ic)== 0||strcmp("SX1268",lora_ic)== 0){
  141. lora_device_t *lora_device = (lora_device_t *)lua_newuserdata(L, sizeof(lora_device_t));
  142. if (!lora_device){
  143. LLOGE("out of memory when malloc lora_device");
  144. return 0;
  145. }
  146. RadioEvents_t RadioEvents = {0};
  147. uint8_t id = 0,cs = 0,res = 0,busy = 0,dio1 = 0;
  148. lora_device->lora_init = true;
  149. if (lua_istable(L, 2)) {
  150. lua_pushstring(L, "id");
  151. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  152. id = luaL_checkinteger(L, -1);
  153. }
  154. lua_pop(L, 1);
  155. lua_pushstring(L, "cs");
  156. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  157. cs = luaL_checkinteger(L, -1);
  158. }
  159. lua_pop(L, 1);
  160. lua_pushstring(L, "res");
  161. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  162. res = luaL_checkinteger(L, -1);
  163. }
  164. lua_pop(L, 1);
  165. lua_pushstring(L, "busy");
  166. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  167. busy = luaL_checkinteger(L, -1);
  168. }
  169. lua_pop(L, 1);
  170. lua_pushstring(L, "dio1");
  171. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  172. dio1 = luaL_checkinteger(L, -1);
  173. }
  174. lua_pop(L, 1);
  175. lua_pushstring(L, "lora_init");
  176. if (LUA_TBOOLEAN == lua_gettable(L, 2)) {
  177. lora_device->lora_init = lua_toboolean(L, -1);
  178. }
  179. lua_pop(L, 1);
  180. }
  181. if (luaL_testudata(L, 3, META_SPI)){
  182. lora_device->lora_spi_id = 255;
  183. lora_device->lora_spi_device = (luat_spi_device_t*)lua_touserdata(L, 3);
  184. }else{
  185. lora_device->lora_spi_id = id;
  186. lora_device->lora_pin_cs = cs;
  187. luat_gpio_mode(lora_device->lora_pin_cs, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  188. }
  189. lora_device->lora_pin_rst = res;
  190. lora_device->lora_pin_busy = busy;
  191. lora_device->lora_pin_dio1 = dio1;
  192. luat_gpio_mode(lora_device->lora_pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_HIGH);
  193. luat_gpio_mode(lora_device->lora_pin_busy, Luat_GPIO_INPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  194. luat_gpio_mode(lora_device->lora_pin_dio1, Luat_GPIO_INPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
  195. RadioEvents.TxDone = OnTxDone;
  196. RadioEvents.RxDone = OnRxDone;
  197. RadioEvents.TxTimeout = OnTxTimeout;
  198. RadioEvents.RxTimeout = OnRxTimeout;
  199. RadioEvents.RxError = OnRxError;
  200. RadioEventsInit2(lora_device,&RadioEvents);
  201. if (lora_device->lora_init) Radio2.Init( lora_device,&RadioEvents );
  202. luat_rtos_timer_create(&lora_device->timer);
  203. luat_rtos_timer_start(lora_device->timer, 10, 1, Radio2.IrqProcess, lora_device);
  204. luaL_setmetatable(L, LUAT_LORA_TYPE);
  205. lua_pushvalue(L, -1);
  206. lora_device->lora_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  207. return 1;
  208. }
  209. else {
  210. LLOGE("no such ic %s", lora_ic);
  211. }
  212. return 0;
  213. }
  214. /*
  215. 设置频道频率
  216. @api lora_device:set_channel(freq)
  217. @number 频率
  218. @usage
  219. lora_device:set_channel(433000000)
  220. */
  221. static int luat_lora_set_channel(lua_State *L){
  222. lora_device_t * lora_device = get_lora_device(L);
  223. uint32_t freq = luaL_optinteger(L, 2,433000000);
  224. Radio2.SetChannel(lora_device,freq);
  225. return 0;
  226. }
  227. /*
  228. lora配置发送参数
  229. @api lora_device:set_txconfig(txconfig)
  230. @table lora发送配置参数,与具体设备有关
  231. @usage
  232. lora_device:set_txconfig(
  233. {
  234. mode=1,
  235. power=22,
  236. fdev=0,
  237. bandwidth=0,
  238. datarate=9,
  239. coderate=4,
  240. preambleLen=8,
  241. fixLen=false,
  242. crcOn=true,
  243. freqHopOn=0,
  244. hopPeriod=0,
  245. iqInverted=false,
  246. timeout=3000
  247. }
  248. )
  249. */
  250. static int luat_lora_set_txconfig(lua_State *L){
  251. uint8_t mode = 1,power = 0,fdev = 0,bandwidth = 0,datarate = 9,coderate = 4,preambleLen = 8,freqHopOn = 0,hopPeriod = 0;
  252. uint32_t timeout = 0;
  253. bool fixLen = false,crcOn = true,iqInverted = false,rateOptimize = false;
  254. lora_device_t * lora_device = get_lora_device(L);
  255. if (lua_istable(L, 2)) {
  256. lua_pushstring(L, "mode");
  257. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  258. mode = luaL_optinteger(L, -1,1);
  259. }
  260. lua_pop(L, 1);
  261. lua_pushstring(L, "power");
  262. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  263. power = luaL_optinteger(L, -1,22);
  264. }
  265. lua_pop(L, 1);
  266. lua_pushstring(L, "fdev");
  267. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  268. fdev = luaL_optinteger(L, -1,0);
  269. }
  270. lua_pop(L, 1);
  271. lua_pushstring(L, "bandwidth");
  272. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  273. bandwidth = luaL_optinteger(L, -1,0);
  274. }
  275. lua_pop(L, 1);
  276. lua_pushstring(L, "datarate");
  277. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  278. datarate = luaL_optinteger(L, -1,9);
  279. }
  280. lua_pop(L, 1);
  281. lua_pushstring(L, "coderate");
  282. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  283. coderate = luaL_optinteger(L, -1,4);
  284. }
  285. lua_pop(L, 1);
  286. lua_pushstring(L, "preambleLen");
  287. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  288. preambleLen = luaL_optinteger(L, -1,8);
  289. }
  290. lua_pop(L, 1);
  291. lua_pushstring(L, "fixLen");
  292. if (LUA_TBOOLEAN == lua_gettable(L, 2)) {
  293. fixLen = lua_toboolean(L, -1);
  294. }
  295. lua_pop(L, 1);
  296. lua_pushstring(L, "crcOn");
  297. if (LUA_TBOOLEAN == lua_gettable(L, 2)) {
  298. crcOn = lua_toboolean(L, -1);
  299. }
  300. lua_pop(L, 1);
  301. lua_pushstring(L, "freqHopOn");
  302. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  303. freqHopOn = luaL_optinteger(L, -1,0);
  304. }
  305. lua_pop(L, 1);
  306. lua_pushstring(L, "hopPeriod");
  307. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  308. hopPeriod = luaL_optinteger(L, -1,0);
  309. }
  310. lua_pop(L, 1);
  311. lua_pushstring(L, "iqInverted");
  312. if (LUA_TBOOLEAN == lua_gettable(L, 2)) {
  313. iqInverted = lua_toboolean(L, -1);
  314. }
  315. lua_pop(L, 1);
  316. lua_pushstring(L, "timeout");
  317. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  318. timeout = luaL_optinteger(L, -1,3000);
  319. }
  320. lua_pop(L, 1);
  321. lua_pushstring(L, "rateOptimize");
  322. if (LUA_TBOOLEAN == lua_gettable(L, 2)) {
  323. rateOptimize = lua_toboolean(L, -1);
  324. }
  325. lua_pop(L, 1);
  326. }
  327. Radio2.SetTxConfig( lora_device,mode, power, fdev, bandwidth,
  328. datarate, coderate,
  329. preambleLen, fixLen,
  330. crcOn, freqHopOn, hopPeriod, iqInverted, timeout ,rateOptimize);
  331. return 0;
  332. }
  333. /*
  334. lora配置接收参数
  335. @api lora_device:set_rxconfig(set_rxconfig)
  336. @table lora接收配置参数,与具体设备有关
  337. @usage
  338. lora_device:set_rxconfig(
  339. {
  340. mode=1,
  341. bandwidth=0,
  342. datarate=9,
  343. coderate=4,
  344. bandwidthAfc=0,
  345. preambleLen=8,
  346. symbTimeout=0,
  347. fixLen=false,
  348. payloadLen=0,
  349. crcOn=true,
  350. freqHopOn=0,
  351. hopPeriod=0,
  352. iqInverted=false,
  353. rxContinuous=false
  354. }
  355. )
  356. */
  357. static int luat_lora_set_rxconfig(lua_State *L){
  358. uint8_t mode = 1,bandwidth = 0,datarate = 9,coderate = 4,bandwidthAfc = 0,preambleLen = 8,symbTimeout = 0,payloadLen = 0,freqHopOn = 0,hopPeriod = 0;
  359. uint32_t frequency = 433000000,timeout = 0;
  360. bool fixLen = false,crcOn = true,iqInverted = false,rxContinuous = false,rateOptimize = false;
  361. lora_device_t * lora_device = get_lora_device(L);
  362. if (lua_istable(L, 2)) {
  363. lua_pushstring(L, "mode");
  364. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  365. mode = luaL_optinteger(L, -1,1);
  366. }
  367. lua_pop(L, 1);
  368. lua_pushstring(L, "bandwidth");
  369. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  370. bandwidth = luaL_optinteger(L, -1,0);
  371. }
  372. lua_pop(L, 1);
  373. lua_pushstring(L, "datarate");
  374. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  375. datarate = luaL_optinteger(L, -1,9);
  376. }
  377. lua_pop(L, 1);
  378. lua_pushstring(L, "coderate");
  379. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  380. coderate = luaL_optinteger(L, -1,4);
  381. }
  382. lua_pop(L, 1);
  383. lua_pushstring(L, "bandwidthAfc");
  384. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  385. bandwidthAfc = luaL_optinteger(L, -1,0);
  386. }
  387. lua_pop(L, 1);
  388. lua_pushstring(L, "preambleLen");
  389. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  390. preambleLen = luaL_optinteger(L, -1,8);
  391. }
  392. lua_pop(L, 1);
  393. lua_pushstring(L, "symbTimeout");
  394. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  395. symbTimeout = luaL_optinteger(L, -1,0);
  396. }
  397. lua_pop(L, 1);
  398. lua_pushstring(L, "fixLen");
  399. if (LUA_TBOOLEAN == lua_gettable(L, 2)) {
  400. fixLen = lua_toboolean(L, -1);
  401. }
  402. lua_pop(L, 1);
  403. lua_pushstring(L, "payloadLen");
  404. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  405. payloadLen = luaL_optinteger(L, -1,0);
  406. }
  407. lua_pop(L, 1);
  408. lua_pushstring(L, "crcOn");
  409. if (LUA_TBOOLEAN == lua_gettable(L, 2)) {
  410. crcOn = lua_toboolean(L, -1);
  411. }
  412. lua_pop(L, 1);
  413. lua_pushstring(L, "freqHopOn");
  414. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  415. freqHopOn = luaL_optinteger(L, -1,0);
  416. }
  417. lua_pop(L, 1);
  418. lua_pushstring(L, "hopPeriod");
  419. if (LUA_TNUMBER == lua_gettable(L, 2)) {
  420. hopPeriod = luaL_optinteger(L, -1,0);
  421. }
  422. lua_pop(L, 1);
  423. lua_pushstring(L, "iqInverted");
  424. if (LUA_TBOOLEAN == lua_gettable(L, 2)) {
  425. iqInverted = lua_toboolean(L, -1);
  426. }
  427. lua_pop(L, 1);
  428. lua_pushstring(L, "rxContinuous");
  429. if (LUA_TBOOLEAN == lua_gettable(L, 2)) {
  430. rxContinuous = lua_toboolean(L, -1);
  431. }
  432. lua_pop(L, 1);
  433. lua_pushstring(L, "rateOptimize");
  434. if (LUA_TBOOLEAN == lua_gettable(L, 2)) {
  435. rateOptimize = lua_toboolean(L, -1);
  436. }
  437. lua_pop(L, 1);
  438. }
  439. Radio2.SetRxConfig( lora_device,mode, bandwidth, datarate,
  440. coderate, bandwidthAfc, preambleLen,
  441. symbTimeout, fixLen,
  442. payloadLen, crcOn, freqHopOn, hopPeriod, iqInverted, rxContinuous ,rateOptimize);
  443. return 0;
  444. }
  445. /*
  446. 发数据
  447. @api lora_device:send(data)
  448. @string 写入的数据
  449. @usage
  450. lora_device:send("PING")
  451. */
  452. static int luat_lora_send(lua_State *L){
  453. lora_device_t * lora_device = get_lora_device(L);
  454. size_t len;
  455. const char* send_buff = luaL_checklstring(L, 2, &len);
  456. Radio2.Standby(lora_device);
  457. Radio2.Send( lora_device,send_buff, len);
  458. return 0;
  459. }
  460. /*
  461. 开启收数据
  462. @api lora_device:recv(timeout)
  463. @number 超时时间,默认1000 单位ms
  464. @usage
  465. sys.subscribe("LORA_RX_DONE", function(data, size)
  466. log.info("LORA_RX_DONE: ", data, size)
  467. lora_device:send("PING")
  468. end)
  469. lora_device:recv(1000)
  470. */
  471. static int luat_lora_recv(lua_State *L){
  472. lora_device_t * lora_device = get_lora_device(L);
  473. int rx_timeout = luaL_optinteger(L, 2, 1000);
  474. Radio2.Standby(lora_device);
  475. Radio2.Rx(lora_device,rx_timeout);
  476. return 0;
  477. }
  478. /*
  479. 设置进入模式(休眠,正常等)
  480. @api lora_device:mode(mode)
  481. @number 模式 正常模式:lora.STANDBY 休眠模式:lora.SLEEP 默认为正常模式
  482. @usage
  483. lora_device:mode(lora.STANDBY)
  484. */
  485. static int luat_lora_mode(lua_State *L){
  486. lora_device_t * lora_device = get_lora_device(L);
  487. int mode = luaL_optinteger(L, 2, 1);
  488. if (mode == 1){
  489. Radio2.Standby(lora_device);
  490. }else if (mode == 0){
  491. Radio2.Sleep(lora_device);
  492. }
  493. return 0;
  494. }
  495. /*
  496. 注册lora回调
  497. @api lora_device:on(cb)
  498. @function cb lora回调,参数包括lora_device, event, data, size
  499. @return nil 无返回值
  500. @usage
  501. lora_device:on(function(lora_device, event, data, size)
  502. log.info("lora", "event", event, lora_device, data, size)
  503. if event == "tx_done" then
  504. lora_device:recv(1000)
  505. elseif event == "rx_done" then
  506. lora_device:send("PING")
  507. elseif event == "tx_timeout" then
  508. elseif event == "rx_timeout" then
  509. lora_device:recv(1000)
  510. elseif event == "rx_error" then
  511. end
  512. end)
  513. --[[
  514. event可能出现的值有
  515. tx_done -- 发送完成
  516. rx_done -- 接收完成
  517. tx_timeout -- 发送超时
  518. rx_timeout -- 接收超时
  519. rx_error -- 接收错误
  520. ]]
  521. */
  522. static int luat_lora_on(lua_State *L){
  523. lora_device_t *lora_device = get_lora_device(L);
  524. if (lora_device->lora_cb != 0) {
  525. luaL_unref(L, LUA_REGISTRYINDEX, lora_device->lora_cb);
  526. lora_device->lora_cb = 0;
  527. }
  528. if (lua_isfunction(L, 2)) {
  529. lua_pushvalue(L, 2);
  530. lora_device->lora_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  531. }
  532. return 0;
  533. }
  534. static int _lora_struct_newindex(lua_State *L);
  535. void luat_lora_struct_init(lua_State *L) {
  536. luaL_newmetatable(L, LUAT_LORA_TYPE);
  537. lua_pushcfunction(L, _lora_struct_newindex);
  538. lua_setfield( L, -2, "__index" );
  539. lua_pop(L, 1);
  540. }
  541. #include "rotable2.h"
  542. static const rotable_Reg_t reg_lora[] =
  543. {
  544. { "init", ROREG_FUNC(luat_lora_init)},
  545. { "set_channel", ROREG_FUNC(luat_lora_set_channel)},
  546. { "set_txconfig",ROREG_FUNC(luat_lora_set_txconfig)},
  547. { "set_rxconfig",ROREG_FUNC(luat_lora_set_rxconfig)},
  548. { "on", ROREG_FUNC(luat_lora_on)},
  549. { "send", ROREG_FUNC(luat_lora_send)},
  550. { "recv", ROREG_FUNC(luat_lora_recv)},
  551. { "mode", ROREG_FUNC(luat_lora_mode)},
  552. //@const SLEEP number SLEEP模式
  553. { "SLEEP", ROREG_INT(0)},
  554. //@const STANDBY number STANDBY模式
  555. { "STANDBY", ROREG_INT(1)},
  556. { NULL, ROREG_INT(0)}
  557. };
  558. static int _lora_struct_newindex(lua_State *L) {
  559. const rotable_Reg_t* reg = reg_lora;
  560. const char* key = luaL_checkstring(L, 2);
  561. while (1) {
  562. if (reg->name == NULL)
  563. return 0;
  564. if (!strcmp(reg->name, key)) {
  565. lua_pushcfunction(L, reg->value.value.func);
  566. return 1;
  567. }
  568. reg ++;
  569. }
  570. }
  571. LUAMOD_API int luaopen_lora2( lua_State *L ) {
  572. luat_newlib2(L, reg_lora);
  573. luat_lora_struct_init(L);
  574. return 1;
  575. }