![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
SmartMesh QSL for STM32F4 version
Fork of COG-AD4050_QSL by
main.cpp@0:8ca1e814a851, 2018-05-02 (annotated)
- Committer:
- APS_Lab
- Date:
- Wed May 02 09:26:10 2018 +0000
- Revision:
- 0:8ca1e814a851
- Child:
- 1:b909b8399252
version1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
APS_Lab | 0:8ca1e814a851 | 1 | #include "mbed.h" |
APS_Lab | 0:8ca1e814a851 | 2 | /* |
APS_Lab | 0:8ca1e814a851 | 3 | Copyright (c) 2016, Dust Networks. All rights reserved. |
APS_Lab | 0:8ca1e814a851 | 4 | |
APS_Lab | 0:8ca1e814a851 | 5 | SimplePublish example application for the mbed OS. |
APS_Lab | 0:8ca1e814a851 | 6 | |
APS_Lab | 0:8ca1e814a851 | 7 | \license See attached DN_LICENSE.txt. |
APS_Lab | 0:8ca1e814a851 | 8 | */ |
APS_Lab | 0:8ca1e814a851 | 9 | |
APS_Lab | 0:8ca1e814a851 | 10 | #include "mbed.h" |
APS_Lab | 0:8ca1e814a851 | 11 | #include "millis.h" |
APS_Lab | 0:8ca1e814a851 | 12 | #include "dn_qsl_api.h" // Only really need this include from QSL |
APS_Lab | 0:8ca1e814a851 | 13 | #include "dn_debug.h" // Included to borrow debug macros |
APS_Lab | 0:8ca1e814a851 | 14 | #include "dn_endianness.h" // Included to borrow array copying |
APS_Lab | 0:8ca1e814a851 | 15 | #include "dn_time.h" // Included to borrow sleep function |
APS_Lab | 0:8ca1e814a851 | 16 | #include "ADXL362.h" |
APS_Lab | 0:8ca1e814a851 | 17 | |
APS_Lab | 0:8ca1e814a851 | 18 | #define NETID 0 // Factory default value used if zero (1229) |
APS_Lab | 0:8ca1e814a851 | 19 | #define JOINKEY NULL // Factory default value used if NULL (44 55 53 54 4E 45 54 57 4F 52 4B 53 52 4F 43 4B) |
APS_Lab | 0:8ca1e814a851 | 20 | #define BANDWIDTH_MS 500 // Not changed if zero (default base bandwidth given by manager is 9 s) |
APS_Lab | 0:8ca1e814a851 | 21 | #define SRC_PORT 60000 // Default port used if zero (0xf0b8) |
APS_Lab | 0:8ca1e814a851 | 22 | #define DEST_PORT 0 // Default port used if zero (0xf0b8) |
APS_Lab | 0:8ca1e814a851 | 23 | #define DATA_PERIOD_MS 500 // Should be longer than (or equal to) bandwidth |
APS_Lab | 0:8ca1e814a851 | 24 | |
APS_Lab | 0:8ca1e814a851 | 25 | // We can use debug macros from dn_debug, as stdio defaults to this serial |
APS_Lab | 0:8ca1e814a851 | 26 | //Serial serialDebug(USBTX1, USBRX1); |
APS_Lab | 0:8ca1e814a851 | 27 | #define log_info serialDebug.printf |
APS_Lab | 0:8ca1e814a851 | 28 | |
APS_Lab | 0:8ca1e814a851 | 29 | // LED2 is the green LED on the NUCLEO-L053R8; might change for other boards |
APS_Lab | 0:8ca1e814a851 | 30 | DigitalOut myled(LED1); |
APS_Lab | 0:8ca1e814a851 | 31 | DigitalOut status(LED2); |
APS_Lab | 0:8ca1e814a851 | 32 | ADXL362 Acc(P1_07, P1_08, P1_06, P1_10); |
APS_Lab | 0:8ca1e814a851 | 33 | |
APS_Lab | 0:8ca1e814a851 | 34 | static uint16_t randomWalk(void); |
APS_Lab | 0:8ca1e814a851 | 35 | static void parsePayload(const uint8_t *payload, uint8_t size); |
APS_Lab | 0:8ca1e814a851 | 36 | |
APS_Lab | 0:8ca1e814a851 | 37 | void init_acc(void) |
APS_Lab | 0:8ca1e814a851 | 38 | { |
APS_Lab | 0:8ca1e814a851 | 39 | Acc.init_spi(); |
APS_Lab | 0:8ca1e814a851 | 40 | Acc.init_adxl362(); |
APS_Lab | 0:8ca1e814a851 | 41 | } |
APS_Lab | 0:8ca1e814a851 | 42 | |
APS_Lab | 0:8ca1e814a851 | 43 | int main() |
APS_Lab | 0:8ca1e814a851 | 44 | { |
APS_Lab | 0:8ca1e814a851 | 45 | uint8_t payload[4]; |
APS_Lab | 0:8ca1e814a851 | 46 | uint8_t inboxBuf[DN_DEFAULT_PAYLOAD_SIZE_LIMIT]; |
APS_Lab | 0:8ca1e814a851 | 47 | uint8_t bytesRead; |
APS_Lab | 0:8ca1e814a851 | 48 | uint8_t i,e; |
APS_Lab | 0:8ca1e814a851 | 49 | int8_t x0, y0, z0; |
APS_Lab | 0:8ca1e814a851 | 50 | |
APS_Lab | 0:8ca1e814a851 | 51 | myled=1; |
APS_Lab | 0:8ca1e814a851 | 52 | status=1; |
APS_Lab | 0:8ca1e814a851 | 53 | |
APS_Lab | 0:8ca1e814a851 | 54 | for(e=0;e<10;e++) |
APS_Lab | 0:8ca1e814a851 | 55 | { |
APS_Lab | 0:8ca1e814a851 | 56 | myled=!myled; |
APS_Lab | 0:8ca1e814a851 | 57 | status=!status; |
APS_Lab | 0:8ca1e814a851 | 58 | wait(0.5); |
APS_Lab | 0:8ca1e814a851 | 59 | } |
APS_Lab | 0:8ca1e814a851 | 60 | |
APS_Lab | 0:8ca1e814a851 | 61 | myled=1; |
APS_Lab | 0:8ca1e814a851 | 62 | status=1; |
APS_Lab | 0:8ca1e814a851 | 63 | |
APS_Lab | 0:8ca1e814a851 | 64 | init_acc(); |
APS_Lab | 0:8ca1e814a851 | 65 | // Configure SysTick for timing |
APS_Lab | 0:8ca1e814a851 | 66 | millisStart(); |
APS_Lab | 0:8ca1e814a851 | 67 | |
APS_Lab | 0:8ca1e814a851 | 68 | // Set PC debug serial baudrate |
APS_Lab | 0:8ca1e814a851 | 69 | //serialDebug.baud(115200); |
APS_Lab | 0:8ca1e814a851 | 70 | |
APS_Lab | 0:8ca1e814a851 | 71 | //log_info("Initializing..."); |
APS_Lab | 0:8ca1e814a851 | 72 | dn_qsl_init(); |
APS_Lab | 0:8ca1e814a851 | 73 | |
APS_Lab | 0:8ca1e814a851 | 74 | // Flash LED to indicate start-up complete |
APS_Lab | 0:8ca1e814a851 | 75 | for (i = 0; i < 10; i++) |
APS_Lab | 0:8ca1e814a851 | 76 | { |
APS_Lab | 0:8ca1e814a851 | 77 | myled = !myled; |
APS_Lab | 0:8ca1e814a851 | 78 | dn_sleep_ms(50); |
APS_Lab | 0:8ca1e814a851 | 79 | } |
APS_Lab | 0:8ca1e814a851 | 80 | myled=0; |
APS_Lab | 0:8ca1e814a851 | 81 | |
APS_Lab | 0:8ca1e814a851 | 82 | while(TRUE) { |
APS_Lab | 0:8ca1e814a851 | 83 | if (dn_qsl_isConnected()) |
APS_Lab | 0:8ca1e814a851 | 84 | { |
APS_Lab | 0:8ca1e814a851 | 85 | Acc.ACC_GetXYZ8(&x0, &y0, &z0); |
APS_Lab | 0:8ca1e814a851 | 86 | payload[0]=x0; |
APS_Lab | 0:8ca1e814a851 | 87 | payload[1]=y0; |
APS_Lab | 0:8ca1e814a851 | 88 | payload[2]=z0; |
APS_Lab | 0:8ca1e814a851 | 89 | //uint16_t val = randomWalk(); |
APS_Lab | 0:8ca1e814a851 | 90 | static uint8_t count = 0; |
APS_Lab | 0:8ca1e814a851 | 91 | myled = 1; // Turn off LED during send/read |
APS_Lab | 0:8ca1e814a851 | 92 | status = 0; |
APS_Lab | 0:8ca1e814a851 | 93 | //dn_write_uint16_t(payload, val); |
APS_Lab | 0:8ca1e814a851 | 94 | payload[3] = count; |
APS_Lab | 0:8ca1e814a851 | 95 | |
APS_Lab | 0:8ca1e814a851 | 96 | if (dn_qsl_send(payload, sizeof (payload), DEST_PORT)) |
APS_Lab | 0:8ca1e814a851 | 97 | { |
APS_Lab | 0:8ca1e814a851 | 98 | //log_info("Sent message nr %u: %u", count, val); |
APS_Lab | 0:8ca1e814a851 | 99 | count++; |
APS_Lab | 0:8ca1e814a851 | 100 | status=1; |
APS_Lab | 0:8ca1e814a851 | 101 | } else |
APS_Lab | 0:8ca1e814a851 | 102 | { |
APS_Lab | 0:8ca1e814a851 | 103 | //log_info("Send failed"); |
APS_Lab | 0:8ca1e814a851 | 104 | status=0; |
APS_Lab | 0:8ca1e814a851 | 105 | } |
APS_Lab | 0:8ca1e814a851 | 106 | |
APS_Lab | 0:8ca1e814a851 | 107 | do |
APS_Lab | 0:8ca1e814a851 | 108 | { |
APS_Lab | 0:8ca1e814a851 | 109 | bytesRead = dn_qsl_read(inboxBuf); |
APS_Lab | 0:8ca1e814a851 | 110 | parsePayload(inboxBuf, bytesRead); |
APS_Lab | 0:8ca1e814a851 | 111 | } while (bytesRead > 0); |
APS_Lab | 0:8ca1e814a851 | 112 | |
APS_Lab | 0:8ca1e814a851 | 113 | myled = 1; // Turn on LED |
APS_Lab | 0:8ca1e814a851 | 114 | dn_sleep_ms(DATA_PERIOD_MS); |
APS_Lab | 0:8ca1e814a851 | 115 | } else |
APS_Lab | 0:8ca1e814a851 | 116 | { |
APS_Lab | 0:8ca1e814a851 | 117 | //log_info("Connecting..."); |
APS_Lab | 0:8ca1e814a851 | 118 | myled = 1; // Not connected; turn off LED |
APS_Lab | 0:8ca1e814a851 | 119 | if (dn_qsl_connect(NETID, JOINKEY, SRC_PORT, BANDWIDTH_MS)) |
APS_Lab | 0:8ca1e814a851 | 120 | { |
APS_Lab | 0:8ca1e814a851 | 121 | myled = 0; // Connected; turn on LED |
APS_Lab | 0:8ca1e814a851 | 122 | status=1;//log_info("Connected to network"); |
APS_Lab | 0:8ca1e814a851 | 123 | } else |
APS_Lab | 0:8ca1e814a851 | 124 | { |
APS_Lab | 0:8ca1e814a851 | 125 | //log_info("Failed to connect"); |
APS_Lab | 0:8ca1e814a851 | 126 | } |
APS_Lab | 0:8ca1e814a851 | 127 | } |
APS_Lab | 0:8ca1e814a851 | 128 | status=0; |
APS_Lab | 0:8ca1e814a851 | 129 | wait(0.5); |
APS_Lab | 0:8ca1e814a851 | 130 | status=1; |
APS_Lab | 0:8ca1e814a851 | 131 | } |
APS_Lab | 0:8ca1e814a851 | 132 | } |
APS_Lab | 0:8ca1e814a851 | 133 | |
APS_Lab | 0:8ca1e814a851 | 134 | static uint16_t randomWalk(void) |
APS_Lab | 0:8ca1e814a851 | 135 | { |
APS_Lab | 0:8ca1e814a851 | 136 | static bool first = TRUE; |
APS_Lab | 0:8ca1e814a851 | 137 | static uint16_t lastValue = 0x7fff; // Start in middle of uint16 range |
APS_Lab | 0:8ca1e814a851 | 138 | const int powerLevel = 9001; |
APS_Lab | 0:8ca1e814a851 | 139 | |
APS_Lab | 0:8ca1e814a851 | 140 | // Seed random number generator on first call |
APS_Lab | 0:8ca1e814a851 | 141 | if (first) |
APS_Lab | 0:8ca1e814a851 | 142 | { |
APS_Lab | 0:8ca1e814a851 | 143 | first = FALSE; |
APS_Lab | 0:8ca1e814a851 | 144 | srand(dn_time_ms()); |
APS_Lab | 0:8ca1e814a851 | 145 | } |
APS_Lab | 0:8ca1e814a851 | 146 | |
APS_Lab | 0:8ca1e814a851 | 147 | // Random walk within +/- powerLevel |
APS_Lab | 0:8ca1e814a851 | 148 | lastValue += rand() / (RAND_MAX / (2*powerLevel) + 1) - powerLevel; |
APS_Lab | 0:8ca1e814a851 | 149 | return lastValue; |
APS_Lab | 0:8ca1e814a851 | 150 | } |
APS_Lab | 0:8ca1e814a851 | 151 | |
APS_Lab | 0:8ca1e814a851 | 152 | static void parsePayload(const uint8_t *payload, uint8_t size) |
APS_Lab | 0:8ca1e814a851 | 153 | { |
APS_Lab | 0:8ca1e814a851 | 154 | uint8_t i; |
APS_Lab | 0:8ca1e814a851 | 155 | char msg[size + 1]; |
APS_Lab | 0:8ca1e814a851 | 156 | |
APS_Lab | 0:8ca1e814a851 | 157 | if (size == 0) |
APS_Lab | 0:8ca1e814a851 | 158 | { |
APS_Lab | 0:8ca1e814a851 | 159 | // Nothing to parse |
APS_Lab | 0:8ca1e814a851 | 160 | return; |
APS_Lab | 0:8ca1e814a851 | 161 | } |
APS_Lab | 0:8ca1e814a851 | 162 | |
APS_Lab | 0:8ca1e814a851 | 163 | // Parse bytes individually as well as together as a string |
APS_Lab | 0:8ca1e814a851 | 164 | //log_info("Received downstream payload of %u bytes:", size); |
APS_Lab | 0:8ca1e814a851 | 165 | for (i = 0; i < size; i++) |
APS_Lab | 0:8ca1e814a851 | 166 | { |
APS_Lab | 0:8ca1e814a851 | 167 | msg[i] = payload[i]; |
APS_Lab | 0:8ca1e814a851 | 168 | //log_info("\tByte# %03u: %#.2x (%u)", i, payload[i], payload[i]); |
APS_Lab | 0:8ca1e814a851 | 169 | } |
APS_Lab | 0:8ca1e814a851 | 170 | msg[size] = '\0'; |
APS_Lab | 0:8ca1e814a851 | 171 | //log_info("\tMessage: %s", msg); |
APS_Lab | 0:8ca1e814a851 | 172 | } |