luat_lib_lora.c 18 KB

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