lora's drivers taken out of examples repo to make a smaller binary
Dependencies: libxDot-mbed5
src/main.cpp@1:80bc4501abc5, 2019-12-08 (annotated)
- Committer:
- candre97
- Date:
- Sun Dec 08 01:56:17 2019 +0000
- Revision:
- 1:80bc4501abc5
- Parent:
- 0:973a5bbb2a17
working on lower levels of radio driver. Still have a binary that is much too large. ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
candre97 | 1:80bc4501abc5 | 1 | /* mbed Microcontroller Library |
candre97 | 1:80bc4501abc5 | 2 | * Copyright (c) 2018 ARM Limited |
candre97 | 1:80bc4501abc5 | 3 | * SPDX-License-Identifier: Apache-2.0 |
candre97 | 1:80bc4501abc5 | 4 | */ |
candre97 | 1:80bc4501abc5 | 5 | #include <stdio.h> |
candre97 | 1:80bc4501abc5 | 6 | #include <LoRaRadio.h> |
candre97 | 1:80bc4501abc5 | 7 | #include "dot_util.h" |
candre97 | 1:80bc4501abc5 | 8 | #include "RadioEvent.h" |
guoxsharon | 0:973a5bbb2a17 | 9 | |
candre97 | 1:80bc4501abc5 | 10 | // Application helper |
guoxsharon | 0:973a5bbb2a17 | 11 | #include "mbed.h" |
candre97 | 1:80bc4501abc5 | 12 | #include <AnalogIn.h> |
candre97 | 1:80bc4501abc5 | 13 | #include <DigitalOut.h> |
candre97 | 1:80bc4501abc5 | 14 | |
candre97 | 1:80bc4501abc5 | 15 | static std::string network_name = "LAleakers"; |
candre97 | 1:80bc4501abc5 | 16 | static std::string network_passphrase = "stephisbroke"; |
candre97 | 1:80bc4501abc5 | 17 | static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 }; |
candre97 | 1:80bc4501abc5 | 18 | static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B }; |
candre97 | 1:80bc4501abc5 | 19 | static uint8_t frequency_sub_band = 7; |
candre97 | 1:80bc4501abc5 | 20 | static lora::NetworkType network_type = lora::PUBLIC_LORAWAN; |
candre97 | 1:80bc4501abc5 | 21 | static uint8_t join_delay = 5; |
candre97 | 1:80bc4501abc5 | 22 | static uint8_t ack = 0; |
candre97 | 1:80bc4501abc5 | 23 | static bool adr = true; |
candre97 | 1:80bc4501abc5 | 24 | |
candre97 | 1:80bc4501abc5 | 25 | mDot* dot = NULL; |
candre97 | 1:80bc4501abc5 | 26 | lora::ChannelPlan* plan = new lora::ChannelPlan_US915(); |
candre97 | 1:80bc4501abc5 | 27 | |
candre97 | 1:80bc4501abc5 | 28 | I2C i2c(I2C_SDA, I2C_SCL); |
guoxsharon | 0:973a5bbb2a17 | 29 | |
candre97 | 1:80bc4501abc5 | 30 | // PIN DEFINITIONS |
candre97 | 1:80bc4501abc5 | 31 | DigitalOut vcc(GPIO0); |
candre97 | 1:80bc4501abc5 | 32 | AnalogIn mic(PB_0); |
candre97 | 1:80bc4501abc5 | 33 | |
candre97 | 1:80bc4501abc5 | 34 | // To sleep, 'wait' should be replaced by 'ThisThread::sleep_for' (C++) or 'thread_sleep_for' (C). If you wish to wait (without sleeping), call 'wait_us'. 'wait_us' is safe to call from ISR context. [since mbed-os-5.14] [-Wdeprecated-declarations] in "main.cpp", Line: 59, Col: 9 |
candre97 | 1:80bc4501abc5 | 35 | |
candre97 | 1:80bc4501abc5 | 36 | int main() { |
candre97 | 1:80bc4501abc5 | 37 | i2c.frequency(400000); |
candre97 | 1:80bc4501abc5 | 38 | assert(plan); |
candre97 | 1:80bc4501abc5 | 39 | RadioEvent events; |
candre97 | 1:80bc4501abc5 | 40 | dot = mDot::getInstance(plan); |
candre97 | 1:80bc4501abc5 | 41 | assert(dot); |
candre97 | 1:80bc4501abc5 | 42 | |
candre97 | 1:80bc4501abc5 | 43 | // attach the custom events handler |
candre97 | 1:80bc4501abc5 | 44 | dot->setEvents(&events); |
guoxsharon | 0:973a5bbb2a17 | 45 | |
candre97 | 1:80bc4501abc5 | 46 | if (!dot->getStandbyFlag()) { |
candre97 | 1:80bc4501abc5 | 47 | logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
candre97 | 1:80bc4501abc5 | 48 | |
candre97 | 1:80bc4501abc5 | 49 | // start from a well-known state |
candre97 | 1:80bc4501abc5 | 50 | logInfo("defaulting Dot configuration"); |
candre97 | 1:80bc4501abc5 | 51 | dot->resetConfig(); |
candre97 | 1:80bc4501abc5 | 52 | dot->resetNetworkSession(); |
candre97 | 1:80bc4501abc5 | 53 | |
candre97 | 1:80bc4501abc5 | 54 | // make sure library logging is turned on |
candre97 | 1:80bc4501abc5 | 55 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
candre97 | 1:80bc4501abc5 | 56 | |
candre97 | 1:80bc4501abc5 | 57 | // update configuration if necessary |
candre97 | 1:80bc4501abc5 | 58 | if (dot->getJoinMode() != mDot::OTA) { |
candre97 | 1:80bc4501abc5 | 59 | logInfo("changing network join mode to OTA"); |
candre97 | 1:80bc4501abc5 | 60 | if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) { |
candre97 | 1:80bc4501abc5 | 61 | logError("failed to set network join mode to OTA"); |
candre97 | 1:80bc4501abc5 | 62 | } |
guoxsharon | 0:973a5bbb2a17 | 63 | } |
candre97 | 1:80bc4501abc5 | 64 | // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY |
candre97 | 1:80bc4501abc5 | 65 | // only one method or the other should be used! |
candre97 | 1:80bc4501abc5 | 66 | // network ID = crc64(network name) |
candre97 | 1:80bc4501abc5 | 67 | // network KEY = cmac(network passphrase) |
candre97 | 1:80bc4501abc5 | 68 | update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, network_type, ack); |
candre97 | 1:80bc4501abc5 | 69 | //update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack); |
guoxsharon | 0:973a5bbb2a17 | 70 | |
candre97 | 1:80bc4501abc5 | 71 | // configure network link checks |
candre97 | 1:80bc4501abc5 | 72 | // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots |
candre97 | 1:80bc4501abc5 | 73 | // check the link every count packets |
candre97 | 1:80bc4501abc5 | 74 | // declare the Dot disconnected after threshold failed link checks |
candre97 | 1:80bc4501abc5 | 75 | // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row |
candre97 | 1:80bc4501abc5 | 76 | update_network_link_check_config(3, 5); |
guoxsharon | 0:973a5bbb2a17 | 77 | |
candre97 | 1:80bc4501abc5 | 78 | // enable or disable Adaptive Data Rate |
candre97 | 1:80bc4501abc5 | 79 | dot->setAdr(adr); |
candre97 | 1:80bc4501abc5 | 80 | |
candre97 | 1:80bc4501abc5 | 81 | // Configure the join delay |
candre97 | 1:80bc4501abc5 | 82 | dot->setJoinDelay(join_delay); |
candre97 | 1:80bc4501abc5 | 83 | |
candre97 | 1:80bc4501abc5 | 84 | // save changes to configuration |
candre97 | 1:80bc4501abc5 | 85 | logInfo("saving configuration"); |
candre97 | 1:80bc4501abc5 | 86 | if (!dot->saveConfig()) { |
candre97 | 1:80bc4501abc5 | 87 | logError("failed to save configuration"); |
candre97 | 1:80bc4501abc5 | 88 | } |
guoxsharon | 0:973a5bbb2a17 | 89 | |
candre97 | 1:80bc4501abc5 | 90 | // display configuration |
candre97 | 1:80bc4501abc5 | 91 | display_config(); |
candre97 | 1:80bc4501abc5 | 92 | } else { |
candre97 | 1:80bc4501abc5 | 93 | // restore the saved session if the dot woke from deepsleep mode |
candre97 | 1:80bc4501abc5 | 94 | // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep |
candre97 | 1:80bc4501abc5 | 95 | logInfo("restoring network session from NVM"); |
candre97 | 1:80bc4501abc5 | 96 | dot->restoreNetworkSession(); |
candre97 | 1:80bc4501abc5 | 97 | } |
candre97 | 1:80bc4501abc5 | 98 | // join network if not joined |
candre97 | 1:80bc4501abc5 | 99 | if (!dot->getNetworkJoinStatus()) { |
candre97 | 1:80bc4501abc5 | 100 | join_network(); |
candre97 | 1:80bc4501abc5 | 101 | } |
candre97 | 1:80bc4501abc5 | 102 | |
candre97 | 1:80bc4501abc5 | 103 | /* |
candre97 | 1:80bc4501abc5 | 104 | tests: |
candre97 | 1:80bc4501abc5 | 105 | 1. Check received transmissions/second from this loop |
candre97 | 1:80bc4501abc5 | 106 | 2. make the packet size a lot bigger |
candre97 | 1:80bc4501abc5 | 107 | 3. |
candre97 | 1:80bc4501abc5 | 108 | */ |
candre97 | 1:80bc4501abc5 | 109 | // get some data in send vector |
candre97 | 1:80bc4501abc5 | 110 | std::vector<uint8_t> tx_data; |
candre97 | 1:80bc4501abc5 | 111 | tx_data.clear(); |
candre97 | 1:80bc4501abc5 | 112 | snprintf(arr, 5, "%u", mic.read_u16()); |
candre97 | 1:80bc4501abc5 | 113 | for(int i = 0; i < 5; i++) { |
candre97 | 1:80bc4501abc5 | 114 | tx_data.push_back(arr[i]); |
candre97 | 1:80bc4501abc5 | 115 | } |
candre97 | 1:80bc4501abc5 | 116 | |
candre97 | 1:80bc4501abc5 | 117 | while(1) { |
candre97 | 1:80bc4501abc5 | 118 | send_data(tx_data); |
guoxsharon | 0:973a5bbb2a17 | 119 | } |
guoxsharon | 0:973a5bbb2a17 | 120 | } |