Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //*****************************************************************************
  2. //              Copyright (C) 2016 Jose Marcelino - Metavurt Ltd
  3. //
  4. // Project:     RM186 LoRa/TTN gateway for Cypress solar beacons
  5. //*****************************************************************************/
  6.  
  7.  
  8. //******************************************************************************
  9. // Definitions
  10. //******************************************************************************
  11. #include "RM1xx-defs.h"
  12. #include "lib/ble.sblib"
  13.  
  14. //******************************************************************************
  15. // Register Error Handler as early as possible
  16. //******************************************************************************
  17. sub HandlerOnErr()
  18.   print "\n OnErr - ";GetLastError();"\n"
  19. endsub
  20. onerror next HandlerOnErr
  21.  
  22. //******************************************************************************
  23. // Global Variable Declarations
  24. //******************************************************************************
  25.  
  26. dim ok$,er$,pr$
  27. dim urts                    //will be <0 if uart parser suspended
  28. dim uuid_hex$, uuid$
  29. dim status
  30. dim linkCheckLoop
  31.  
  32. //******************************************************************************
  33. // Initialisse Global Variable
  34. //******************************************************************************
  35.  
  36. ok$    = "\nOK"
  37. er$    = "\nERROR "
  38. pr$    = "\r\n>"
  39.  
  40. // Set this to the "beacon" UUID we're looking for. Here we use the default from Cypress
  41. uuid_hex$ = "0005000100001000800000805F9B0131"
  42.  
  43.  
  44. uuid$ = StrDehexize$(uuid_hex$)
  45.  
  46. #define LORA_CONNECTED                      ((1)<<1)
  47. #define LORA_WAITING_FOR_ACK                ((1)<<2)
  48.  
  49. //==============================================================================
  50. function HndlrAdvRpt()
  51.     dim periphAddr$, advData$, nDiscarded, nRssi, result, lorarc, mac$
  52.     //Read all cached advert reports
  53.     do
  54.         result = BleScanGetAdvReport(periphAddr$, advData$, nDiscarded, nRssi)
  55.         mac$ = StrHexize$(periphAddr$)
  56.         lorarc = LORAMACTxData(1, mac$, 0)
  57.     until result != 0
  58. endfunc 1
  59.  
  60. '//==============================================================================
  61. '// This handler is called when there is a LoRa TX Complete event
  62. '//==============================================================================
  63. function HandlerLoRaTxComp() as integer
  64.     dim result
  65.    
  66.     print "LoRa TX Complete Event\n"
  67.     if linkCheckLoop>0 then
  68.         result = LORAMACLinkCheck()
  69.     endif
  70.     status = status&(!LORA_WAITING_FOR_ACK)
  71. endfunc 1
  72.  
  73. '//==============================================================================
  74. '// This handler is called when there is a LoRa RX Complete event
  75. '//==============================================================================
  76. function HandlerLoRaRxComp() as integer
  77.     print "LoRa RX Complete Event\n"
  78. endfunc 1
  79.  
  80.  
  81. '//==============================================================================
  82. '// This handler is called when the LoRa Join procedure starts
  83. '//==============================================================================
  84. function HandlerLoRaJoining() as integer
  85.     print "Attempting to join the LoRa network\n"
  86. endfunc 1
  87.  
  88. '//==============================================================================
  89. '// This handler is called when the LoRa Join Process completes successfully
  90. '//==============================================================================
  91. function HandlerLoRaJoined() as integer
  92.   print "Joined the LoRa network\n"
  93.   status = status+LORA_CONNECTED
  94.  
  95.   //if status == (LORA_CONNECTED + BLE_CONNECTED) then
  96.   // rc=startDemo()
  97.   // endif
  98. endfunc 1
  99.  
  100.  
  101.  
  102.  
  103.  
  104. //==============================================================================
  105. sub UartRsp(rsp as integer)
  106.     if rsp == 0 then
  107.         print ok$;pr$
  108.     elseif rsp > 0 then
  109.         print er$;integer.h' rsp;pr$
  110.     endif
  111.     urts = rsp
  112. endsub
  113.  
  114. //==============================================================================
  115. function Startup() as INTEGER
  116.     dim result
  117.     status = 0
  118.    
  119.     linkCheckLoop = 0
  120.  
  121.     // Reset the LoRa radio
  122.     result = LORAMACReset()
  123.    
  124.     // Set scan type to passive
  125.     result = BleScanConfig(2, 0)
  126.  
  127.     // Start scanning for BLE devices
  128.     result=BleScanStart(0,0)
  129.    
  130.     if status != (LORA_CONNECTED) then
  131.       // Join the LoRa network
  132.       result = LORAMACJoin(LORAMAC_JOIN_BY_REQUEST)  
  133.     endif
  134.  
  135. endfunc result
  136.  
  137. //==============================================================================
  138. // Event handlers
  139. //==============================================================================
  140.  
  141. OnEvent EVBLE_ADV_REPORT CALL HndlrAdvRpt
  142.  
  143. OnEvent  EVLORAMACTXCOMPLETE      call HandlerLoRaTxComp
  144. OnEvent  EVLORAMACRXCOMPLETE      call HandlerLoRaRxComp
  145. OnEvent  EVLORAMACJOINING         call HandlerLoRaJoining
  146. OnEvent  EVLORAMACJOINED          call HandlerLoRaJoined
  147.  
  148. //==============================================================================
  149. // Main block
  150. //==============================================================================
  151. dim result
  152.  
  153. result = Startup()
  154.  
  155. //Send an OK response
  156. if result == 0 then
  157.     UartRsp(0)
  158. else
  159.     UartRsp(1)
  160. endif
  161.  
  162. //------------------------------------------------------------------------------
  163. // Wait for a synchronous event.
  164. // An application can have multiple <WaitEvent> statements
  165. //------------------------------------------------------------------------------
  166. WaitEvent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement