Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*****************************************************************************
- // Copyright (C) 2016 Jose Marcelino - Metavurt Ltd
- //
- // Project: RM186 LoRa/TTN gateway for Cypress solar beacons
- //*****************************************************************************/
- //******************************************************************************
- // Definitions
- //******************************************************************************
- #include "RM1xx-defs.h"
- #include "lib/ble.sblib"
- //******************************************************************************
- // Register Error Handler as early as possible
- //******************************************************************************
- sub HandlerOnErr()
- print "\n OnErr - ";GetLastError();"\n"
- endsub
- onerror next HandlerOnErr
- //******************************************************************************
- // Global Variable Declarations
- //******************************************************************************
- dim ok$,er$,pr$
- dim urts //will be <0 if uart parser suspended
- dim uuid_hex$, uuid$
- dim status
- dim linkCheckLoop
- //******************************************************************************
- // Initialisse Global Variable
- //******************************************************************************
- ok$ = "\nOK"
- er$ = "\nERROR "
- pr$ = "\r\n>"
- // Set this to the "beacon" UUID we're looking for. Here we use the default from Cypress
- uuid_hex$ = "0005000100001000800000805F9B0131"
- uuid$ = StrDehexize$(uuid_hex$)
- #define LORA_CONNECTED ((1)<<1)
- #define LORA_WAITING_FOR_ACK ((1)<<2)
- //==============================================================================
- function HndlrAdvRpt()
- dim periphAddr$, advData$, nDiscarded, nRssi, result, lorarc, mac$
- //Read all cached advert reports
- do
- result = BleScanGetAdvReport(periphAddr$, advData$, nDiscarded, nRssi)
- mac$ = StrHexize$(periphAddr$)
- lorarc = LORAMACTxData(1, mac$, 0)
- until result != 0
- endfunc 1
- '//==============================================================================
- '// This handler is called when there is a LoRa TX Complete event
- '//==============================================================================
- function HandlerLoRaTxComp() as integer
- dim result
- print "LoRa TX Complete Event\n"
- if linkCheckLoop>0 then
- result = LORAMACLinkCheck()
- endif
- status = status&(!LORA_WAITING_FOR_ACK)
- endfunc 1
- '//==============================================================================
- '// This handler is called when there is a LoRa RX Complete event
- '//==============================================================================
- function HandlerLoRaRxComp() as integer
- print "LoRa RX Complete Event\n"
- endfunc 1
- '//==============================================================================
- '// This handler is called when the LoRa Join procedure starts
- '//==============================================================================
- function HandlerLoRaJoining() as integer
- print "Attempting to join the LoRa network\n"
- endfunc 1
- '//==============================================================================
- '// This handler is called when the LoRa Join Process completes successfully
- '//==============================================================================
- function HandlerLoRaJoined() as integer
- print "Joined the LoRa network\n"
- status = status+LORA_CONNECTED
- //if status == (LORA_CONNECTED + BLE_CONNECTED) then
- // rc=startDemo()
- // endif
- endfunc 1
- //==============================================================================
- sub UartRsp(rsp as integer)
- if rsp == 0 then
- print ok$;pr$
- elseif rsp > 0 then
- print er$;integer.h' rsp;pr$
- endif
- urts = rsp
- endsub
- //==============================================================================
- function Startup() as INTEGER
- dim result
- status = 0
- linkCheckLoop = 0
- // Reset the LoRa radio
- result = LORAMACReset()
- // Set scan type to passive
- result = BleScanConfig(2, 0)
- // Start scanning for BLE devices
- result=BleScanStart(0,0)
- if status != (LORA_CONNECTED) then
- // Join the LoRa network
- result = LORAMACJoin(LORAMAC_JOIN_BY_REQUEST)
- endif
- endfunc result
- //==============================================================================
- // Event handlers
- //==============================================================================
- OnEvent EVBLE_ADV_REPORT CALL HndlrAdvRpt
- OnEvent EVLORAMACTXCOMPLETE call HandlerLoRaTxComp
- OnEvent EVLORAMACRXCOMPLETE call HandlerLoRaRxComp
- OnEvent EVLORAMACJOINING call HandlerLoRaJoining
- OnEvent EVLORAMACJOINED call HandlerLoRaJoined
- //==============================================================================
- // Main block
- //==============================================================================
- dim result
- result = Startup()
- //Send an OK response
- if result == 0 then
- UartRsp(0)
- else
- UartRsp(1)
- endif
- //------------------------------------------------------------------------------
- // Wait for a synchronous event.
- // An application can have multiple <WaitEvent> statements
- //------------------------------------------------------------------------------
- WaitEvent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement