mDot EVB to M2X demo.
Dependencies: DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B libmDot mbed-rtos mbed-src
Fork of MTDOT-EVBDemo by
main.cpp@8:5b33eee8e2a5, 2015-07-21 (annotated)
- Committer:
- jamescmaki
- Date:
- Tue Jul 21 14:57:52 2015 +0000
- Revision:
- 8:5b33eee8e2a5
- Parent:
- 7:c9d37522b975
- Child:
- 9:46ce96e9954f
New tx_intervals [120,60,30,10] with 30 as the default.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
falingtrea | 0:bdd16076aaa5 | 1 | /** |
falingtrea | 0:bdd16076aaa5 | 2 | * @file main.cpp |
falingtrea | 0:bdd16076aaa5 | 3 | * @brief Main application for mDot-EVB demo |
falingtrea | 0:bdd16076aaa5 | 4 | * @author Tim Barr MultiTech Systems Inc. |
falingtrea | 3:68e974f5f532 | 5 | * @version 1.03 |
falingtrea | 0:bdd16076aaa5 | 6 | * @see |
falingtrea | 0:bdd16076aaa5 | 7 | * |
falingtrea | 0:bdd16076aaa5 | 8 | * Copyright (c) 2015 |
falingtrea | 0:bdd16076aaa5 | 9 | * |
falingtrea | 0:bdd16076aaa5 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
falingtrea | 0:bdd16076aaa5 | 11 | * you may not use this file except in compliance with the License. |
falingtrea | 0:bdd16076aaa5 | 12 | * You may obtain a copy of the License at |
falingtrea | 0:bdd16076aaa5 | 13 | * |
falingtrea | 0:bdd16076aaa5 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
falingtrea | 0:bdd16076aaa5 | 15 | * |
falingtrea | 0:bdd16076aaa5 | 16 | * Unless required by applicable law or agreed to in writing, software |
falingtrea | 0:bdd16076aaa5 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
falingtrea | 0:bdd16076aaa5 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
falingtrea | 0:bdd16076aaa5 | 19 | * See the License for the specific language governing permissions and |
falingtrea | 0:bdd16076aaa5 | 20 | * limitations under the License. |
falingtrea | 1:ac9595d0f0e7 | 21 | * |
falingtrea | 1:ac9595d0f0e7 | 22 | * 1.01 TAB 7/6/15 Removed NULL pointer from evbAmbientLight creation call. |
falingtrea | 2:75adc72aa6a0 | 23 | * |
falingtrea | 3:68e974f5f532 | 24 | * 1.02 TAB 7/8/15 Send logo to LCD before attempting connection to LoRa network. Added |
jamescmaki | 5:72d4d95e1d42 | 25 | * information on setting up for public LoRa network. Moved SW setup to |
jamescmaki | 5:72d4d95e1d42 | 26 | * beginning of main. Removed printf call from ISR functions. Added |
jamescmaki | 5:72d4d95e1d42 | 27 | * additional checks for exit_program. |
falingtrea | 3:68e974f5f532 | 28 | * |
falingtrea | 3:68e974f5f532 | 29 | * 1.03 TAB 7/15/15 Added threads for push button switch debounce. |
falingtrea | 3:68e974f5f532 | 30 | * |
jamescmaki | 5:72d4d95e1d42 | 31 | * 1.04 JCM 7/16/15 Updated application for AT&T M2X Demo |
jamescmaki | 5:72d4d95e1d42 | 32 | * |
jamescmaki | 5:72d4d95e1d42 | 33 | * 1.05 JCM 7/20/15 Integrate Senet Public network demo |
jamescmaki | 4:71e411fbd9ff | 34 | * |
jamescmaki | 7:c9d37522b975 | 35 | * 1.06 JCM 7/20/15 Clean up code, send message using clock |
jamescmaki | 7:c9d37522b975 | 36 | * |
falingtrea | 0:bdd16076aaa5 | 37 | */ |
falingtrea | 0:bdd16076aaa5 | 38 | |
falingtrea | 0:bdd16076aaa5 | 39 | #include "mbed.h" |
falingtrea | 0:bdd16076aaa5 | 40 | #include "MMA845x.h" |
falingtrea | 0:bdd16076aaa5 | 41 | #include "MPL3115A2.h" |
falingtrea | 0:bdd16076aaa5 | 42 | #include "ISL29011.h" |
falingtrea | 0:bdd16076aaa5 | 43 | #include "NCP5623B.h" |
falingtrea | 0:bdd16076aaa5 | 44 | #include "DOGS102.h" |
falingtrea | 0:bdd16076aaa5 | 45 | #include "font_6x8.h" |
falingtrea | 0:bdd16076aaa5 | 46 | #include "MultiTech_Logo.h" |
falingtrea | 0:bdd16076aaa5 | 47 | #include "mDot.h" |
falingtrea | 0:bdd16076aaa5 | 48 | #include "rtos.h" |
falingtrea | 0:bdd16076aaa5 | 49 | #include <string> |
falingtrea | 0:bdd16076aaa5 | 50 | #include <vector> |
falingtrea | 0:bdd16076aaa5 | 51 | |
jamescmaki | 5:72d4d95e1d42 | 52 | /* |
jamescmaki | 5:72d4d95e1d42 | 53 | * General settings |
jamescmaki | 5:72d4d95e1d42 | 54 | */ |
jamescmaki | 5:72d4d95e1d42 | 55 | const bool public_net = false; |
jamescmaki | 5:72d4d95e1d42 | 56 | const bool senet_demo = false; |
jamescmaki | 5:72d4d95e1d42 | 57 | static uint8_t config_frequency_sub_band = 5; |
jamescmaki | 4:71e411fbd9ff | 58 | |
jamescmaki | 4:71e411fbd9ff | 59 | /* |
jamescmaki | 4:71e411fbd9ff | 60 | * Conduit AEP settings |
jamescmaki | 4:71e411fbd9ff | 61 | * |
jamescmaki | 4:71e411fbd9ff | 62 | * AEP settings can be found at: https://<ip-address>/lora_network.html |
jamescmaki | 4:71e411fbd9ff | 63 | */ |
jamescmaki | 4:71e411fbd9ff | 64 | static std::string config_network_name = "testtest"; |
jamescmaki | 4:71e411fbd9ff | 65 | static std::string config_network_pass = "memememe"; |
jamescmaki | 5:72d4d95e1d42 | 66 | |
jamescmaki | 5:72d4d95e1d42 | 67 | /* |
jamescmaki | 5:72d4d95e1d42 | 68 | * Public Network settings |
jamescmaki | 5:72d4d95e1d42 | 69 | */ |
jamescmaki | 5:72d4d95e1d42 | 70 | static uint8_t app_id[8] = {0x00,0x25,0x0C,0x00,0x00,0x01,0x00,0x01}; |
jamescmaki | 5:72d4d95e1d42 | 71 | std::vector<uint8_t> config_app_id(app_id, app_id + sizeof(app_id)/sizeof(uint8_t)); |
jamescmaki | 5:72d4d95e1d42 | 72 | static uint8_t app_key[16] = {0xB4,0xAD,0x1A,0x25,0x69,0x7F,0xF6,0x8E,0xD3,0x4B,0x83,0xC4,0xB6,0xC0,0xF2,0x3C}; |
jamescmaki | 5:72d4d95e1d42 | 73 | std::vector<uint8_t> config_app_key(app_key, app_key + sizeof(app_key)/sizeof(uint8_t)); |
jamescmaki | 4:71e411fbd9ff | 74 | |
jamescmaki | 4:71e411fbd9ff | 75 | /* |
jamescmaki | 4:71e411fbd9ff | 76 | * M2X settings |
jamescmaki | 4:71e411fbd9ff | 77 | * |
jamescmaki | 4:71e411fbd9ff | 78 | * M2X settings can be found at: https://m2x.att.com/devices |
jamescmaki | 4:71e411fbd9ff | 79 | * Once a device is added you will see DEVICE ID and PRIMARY API KEY fields |
jamescmaki | 4:71e411fbd9ff | 80 | * for the device. |
jamescmaki | 4:71e411fbd9ff | 81 | */ |
jamescmaki | 5:72d4d95e1d42 | 82 | const char *m2x_device = "f397d9c6c74cb1e2a1be5ef7731dc8af"; |
jamescmaki | 5:72d4d95e1d42 | 83 | const char *m2x_key = "5899c8b045531d3634efa65096ee3ede"; |
jamescmaki | 4:71e411fbd9ff | 84 | |
jamescmaki | 4:71e411fbd9ff | 85 | |
falingtrea | 0:bdd16076aaa5 | 86 | enum LED1_COLOR { |
falingtrea | 0:bdd16076aaa5 | 87 | RED = 0, |
falingtrea | 0:bdd16076aaa5 | 88 | GREEN = 1 |
falingtrea | 0:bdd16076aaa5 | 89 | }; |
falingtrea | 0:bdd16076aaa5 | 90 | |
falingtrea | 0:bdd16076aaa5 | 91 | /* |
falingtrea | 0:bdd16076aaa5 | 92 | * union for converting from 32-bit to 4 8-bit values |
falingtrea | 0:bdd16076aaa5 | 93 | */ |
falingtrea | 0:bdd16076aaa5 | 94 | union convert32 { |
jamescmaki | 5:72d4d95e1d42 | 95 | int32_t f_s; // convert from signed 32 bit int |
jamescmaki | 5:72d4d95e1d42 | 96 | uint32_t f_u; // convert from unsigned 32 bit int |
jamescmaki | 5:72d4d95e1d42 | 97 | uint8_t t_u[4]; // convert to 8 bit unsigned array |
falingtrea | 0:bdd16076aaa5 | 98 | }; |
falingtrea | 0:bdd16076aaa5 | 99 | |
falingtrea | 0:bdd16076aaa5 | 100 | /* |
falingtrea | 0:bdd16076aaa5 | 101 | * union for converting from 16- bit to 2 8-bit values |
falingtrea | 0:bdd16076aaa5 | 102 | */ |
falingtrea | 0:bdd16076aaa5 | 103 | union convert16 { |
jamescmaki | 5:72d4d95e1d42 | 104 | int16_t f_s; // convert from signed 16 bit int |
jamescmaki | 5:72d4d95e1d42 | 105 | uint16_t f_u; // convert from unsigned 16 bit int |
jamescmaki | 5:72d4d95e1d42 | 106 | uint8_t t_u[2]; // convert to 8 bit unsigned array |
falingtrea | 0:bdd16076aaa5 | 107 | }; |
falingtrea | 0:bdd16076aaa5 | 108 | |
jamescmaki | 5:72d4d95e1d42 | 109 | //DigitalIn mDot02(PA_2); // GPIO/UART_TX |
jamescmaki | 5:72d4d95e1d42 | 110 | //DigitalOut mDot03(PA_3); // GPIO/UART_RX |
jamescmaki | 5:72d4d95e1d42 | 111 | //DigitalIn mDot04(PA_6); // GPIO/SPI_MISO |
jamescmaki | 5:72d4d95e1d42 | 112 | //DigitalIn mDot06(PA_8); // GPIO/I2C_SCL |
jamescmaki | 5:72d4d95e1d42 | 113 | //DigitalIn mDot07(PC_9); // GPIO/I2C_SDA |
falingtrea | 0:bdd16076aaa5 | 114 | |
jamescmaki | 5:72d4d95e1d42 | 115 | InterruptIn mDot08(PA_12); // GPIO/USB PB S1 on EVB |
jamescmaki | 5:72d4d95e1d42 | 116 | InterruptIn mDot09(PA_11); // GPIO/USB PB S2 on EVB |
falingtrea | 0:bdd16076aaa5 | 117 | |
jamescmaki | 5:72d4d95e1d42 | 118 | //DigitalIn mDot11(PA_7); // GPIO/SPI_MOSI |
falingtrea | 0:bdd16076aaa5 | 119 | |
jamescmaki | 5:72d4d95e1d42 | 120 | InterruptIn mDot12(PA_0); // GPIO/UART_CTS PRESSURE_INT2 on EVB |
jamescmaki | 5:72d4d95e1d42 | 121 | DigitalOut mDot13(PC_13,1); // GPIO LCD_C/D |
jamescmaki | 5:72d4d95e1d42 | 122 | InterruptIn mDot15(PC_1); // GPIO LIGHT_PROX_INT on EVB |
jamescmaki | 5:72d4d95e1d42 | 123 | InterruptIn mDot16(PA_1); // GPIO/UART_RTS ACCEL_INT2 on EVB |
jamescmaki | 5:72d4d95e1d42 | 124 | DigitalOut mDot17(PA_4,1); // GPIO/SPI_NCS LCD_CS on EVB |
falingtrea | 0:bdd16076aaa5 | 125 | |
jamescmaki | 5:72d4d95e1d42 | 126 | //DigitalIn mDot18(PA_5); // GPIO/SPI_SCK |
falingtrea | 0:bdd16076aaa5 | 127 | |
falingtrea | 0:bdd16076aaa5 | 128 | //DigitalInOut mDot19(PB_0,PIN_INPUT,PullNone,0); // GPIO PushPull LED Low=Red High=Green set MODE=INPUT to turn off |
jamescmaki | 5:72d4d95e1d42 | 129 | AnalogIn mDot20(PB_1); // GPIO Current Sense Analog in on EVB |
falingtrea | 0:bdd16076aaa5 | 130 | |
jamescmaki | 5:72d4d95e1d42 | 131 | Serial debugUART(PA_9, PA_10); // mDot debug UART |
falingtrea | 0:bdd16076aaa5 | 132 | |
jamescmaki | 5:72d4d95e1d42 | 133 | //Serial mDotUART(PA_2, PA_3); // mDot external UART mDot02 and mDot03 |
falingtrea | 0:bdd16076aaa5 | 134 | |
jamescmaki | 5:72d4d95e1d42 | 135 | I2C mDoti2c(PC_9,PA_8); // mDot External I2C mDot6 and mDot7 |
falingtrea | 0:bdd16076aaa5 | 136 | |
jamescmaki | 5:72d4d95e1d42 | 137 | SPI mDotspi(PA_7,PA_6,PA_5); // mDot external SPI mDot11, mDot4, and mDot18 |
falingtrea | 0:bdd16076aaa5 | 138 | |
falingtrea | 2:75adc72aa6a0 | 139 | |
jamescmaki | 7:c9d37522b975 | 140 | uint8_t result; |
jamescmaki | 8:5b33eee8e2a5 | 141 | uint8_t tx_interval = 30; |
falingtrea | 0:bdd16076aaa5 | 142 | char data; |
falingtrea | 0:bdd16076aaa5 | 143 | unsigned char test; |
jamescmaki | 4:71e411fbd9ff | 144 | char txtstr[64]; |
jamescmaki | 7:c9d37522b975 | 145 | int32_t num_whole; |
jamescmaki | 7:c9d37522b975 | 146 | int32_t mdot_ret; |
falingtrea | 0:bdd16076aaa5 | 147 | uint32_t pressure; |
falingtrea | 0:bdd16076aaa5 | 148 | int16_t num_frac; |
falingtrea | 0:bdd16076aaa5 | 149 | |
jamescmaki | 5:72d4d95e1d42 | 150 | uint8_t position_value; // 00 unknown, 01 is flat, 02 is vertical |
jamescmaki | 5:72d4d95e1d42 | 151 | |
falingtrea | 0:bdd16076aaa5 | 152 | bool exit_program = false; |
falingtrea | 0:bdd16076aaa5 | 153 | |
falingtrea | 0:bdd16076aaa5 | 154 | MMA845x_DATA accel_data; |
falingtrea | 0:bdd16076aaa5 | 155 | MPL3115A2_DATA baro_data; |
falingtrea | 0:bdd16076aaa5 | 156 | uint16_t lux_data; |
jamescmaki | 7:c9d37522b975 | 157 | MMA845x *evbAccel; |
jamescmaki | 7:c9d37522b975 | 158 | MPL3115A2 *evbBaro; |
jamescmaki | 7:c9d37522b975 | 159 | ISL29011 *evbAmbLight; |
jamescmaki | 7:c9d37522b975 | 160 | NCP5623B *evbBackLight; |
jamescmaki | 7:c9d37522b975 | 161 | DOGS102 *evbLCD; |
jamescmaki | 7:c9d37522b975 | 162 | mDot *mdot_radio; |
falingtrea | 0:bdd16076aaa5 | 163 | |
falingtrea | 0:bdd16076aaa5 | 164 | convert32 convertl; |
falingtrea | 0:bdd16076aaa5 | 165 | convert16 converts; |
falingtrea | 0:bdd16076aaa5 | 166 | |
falingtrea | 3:68e974f5f532 | 167 | // flags for pushbutton debounce code |
falingtrea | 3:68e974f5f532 | 168 | bool pb1_low = false; |
falingtrea | 3:68e974f5f532 | 169 | bool pb2_low = false; |
falingtrea | 3:68e974f5f532 | 170 | |
falingtrea | 0:bdd16076aaa5 | 171 | void pb1ISR(void); |
falingtrea | 0:bdd16076aaa5 | 172 | void pb2ISR(void); |
falingtrea | 3:68e974f5f532 | 173 | void pb1_debounce(void const *args); |
falingtrea | 3:68e974f5f532 | 174 | void pb2_debounce(void const *args); |
falingtrea | 3:68e974f5f532 | 175 | Thread* thread_3; |
falingtrea | 0:bdd16076aaa5 | 176 | |
jamescmaki | 7:c9d37522b975 | 177 | void log_error(mDot *dot, const char *msg, int32_t retval); |
falingtrea | 0:bdd16076aaa5 | 178 | |
jamescmaki | 7:c9d37522b975 | 179 | void config_pkt_xmit(void const *args); |
falingtrea | 0:bdd16076aaa5 | 180 | |
falingtrea | 0:bdd16076aaa5 | 181 | int main() |
falingtrea | 0:bdd16076aaa5 | 182 | { |
jamescmaki | 4:71e411fbd9ff | 183 | std::vector<uint8_t> lora_pl; |
falingtrea | 0:bdd16076aaa5 | 184 | std::vector<uint8_t> mdot_EUI; |
jamescmaki | 7:c9d37522b975 | 185 | int count; |
jamescmaki | 7:c9d37522b975 | 186 | int loops; |
jamescmaki | 7:c9d37522b975 | 187 | int i; |
falingtrea | 0:bdd16076aaa5 | 188 | |
jamescmaki | 4:71e411fbd9ff | 189 | debugUART.baud(115200); |
falingtrea | 0:bdd16076aaa5 | 190 | // mDotUART.baud(9600); // mdot UART unused but available on external connector |
falingtrea | 0:bdd16076aaa5 | 191 | |
jamescmaki | 5:72d4d95e1d42 | 192 | Thread thread_1(pb1_debounce); // threads for de-bouncing pushbutton switches |
falingtrea | 3:68e974f5f532 | 193 | Thread thread_2(pb2_debounce); |
falingtrea | 3:68e974f5f532 | 194 | |
jamescmaki | 5:72d4d95e1d42 | 195 | thread_3 = new Thread(config_pkt_xmit); // start thread that sends LoRa packet when SW2 pressed |
falingtrea | 2:75adc72aa6a0 | 196 | |
jamescmaki | 7:c9d37522b975 | 197 | evbAccel = new MMA845x(mDoti2c, MMA845x::SA0_VSS); // setup Accelerometer |
jamescmaki | 5:72d4d95e1d42 | 198 | evbBaro = new MPL3115A2(mDoti2c); // setup Barometric sensor |
jamescmaki | 5:72d4d95e1d42 | 199 | evbAmbLight = new ISL29011(mDoti2c); // Setup Ambient Light Sensor |
jamescmaki | 5:72d4d95e1d42 | 200 | evbBackLight = new NCP5623B(mDoti2c); // setup backlight and LED 2 driver chip |
jamescmaki | 5:72d4d95e1d42 | 201 | evbLCD = new DOGS102(mDotspi, mDot17, mDot13); // setup LCD |
falingtrea | 0:bdd16076aaa5 | 202 | |
falingtrea | 2:75adc72aa6a0 | 203 | /* |
falingtrea | 2:75adc72aa6a0 | 204 | * Setup SW1 as program stop function |
falingtrea | 2:75adc72aa6a0 | 205 | */ |
falingtrea | 2:75adc72aa6a0 | 206 | mDot08.disable_irq(); |
falingtrea | 2:75adc72aa6a0 | 207 | mDot08.fall(&pb1ISR); |
falingtrea | 2:75adc72aa6a0 | 208 | |
falingtrea | 2:75adc72aa6a0 | 209 | /* |
falingtrea | 2:75adc72aa6a0 | 210 | * need to call this function after rise or fall because rise/fall sets |
falingtrea | 2:75adc72aa6a0 | 211 | * mode to PullNone |
falingtrea | 2:75adc72aa6a0 | 212 | */ |
falingtrea | 2:75adc72aa6a0 | 213 | mDot08.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 214 | |
falingtrea | 2:75adc72aa6a0 | 215 | mDot08.enable_irq(); |
falingtrea | 2:75adc72aa6a0 | 216 | |
falingtrea | 2:75adc72aa6a0 | 217 | /* |
falingtrea | 2:75adc72aa6a0 | 218 | * Setup SW2 as packet time change |
falingtrea | 2:75adc72aa6a0 | 219 | */ |
falingtrea | 2:75adc72aa6a0 | 220 | mDot09.disable_irq(); |
falingtrea | 2:75adc72aa6a0 | 221 | mDot09.fall(&pb2ISR); |
falingtrea | 2:75adc72aa6a0 | 222 | |
falingtrea | 2:75adc72aa6a0 | 223 | /* |
falingtrea | 2:75adc72aa6a0 | 224 | * need to call this function after rise or fall because rise/fall sets |
falingtrea | 2:75adc72aa6a0 | 225 | * mode to PullNone |
falingtrea | 2:75adc72aa6a0 | 226 | */ |
falingtrea | 2:75adc72aa6a0 | 227 | mDot09.mode(PullUp); |
jamescmaki | 7:c9d37522b975 | 228 | |
falingtrea | 2:75adc72aa6a0 | 229 | mDot09.enable_irq(); |
falingtrea | 2:75adc72aa6a0 | 230 | |
falingtrea | 2:75adc72aa6a0 | 231 | /* |
falingtrea | 2:75adc72aa6a0 | 232 | * Setting other InterruptIn pins with Pull Ups |
falingtrea | 2:75adc72aa6a0 | 233 | */ |
falingtrea | 2:75adc72aa6a0 | 234 | mDot12.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 235 | mDot15.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 236 | mDot16.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 237 | |
jamescmaki | 7:c9d37522b975 | 238 | printf("font table address %p\n\r", &font_6x8); |
jamescmaki | 7:c9d37522b975 | 239 | printf("bitmap address %p\n\r", &MultiTech_Logo); |
falingtrea | 2:75adc72aa6a0 | 240 | |
jamescmaki | 7:c9d37522b975 | 241 | // Setup and display logo on LCD |
falingtrea | 2:75adc72aa6a0 | 242 | evbLCD->startUpdate(); |
falingtrea | 2:75adc72aa6a0 | 243 | |
jamescmaki | 7:c9d37522b975 | 244 | evbLCD->writeBitmap(0, 0, MultiTech_Logo); |
falingtrea | 2:75adc72aa6a0 | 245 | |
falingtrea | 2:75adc72aa6a0 | 246 | sprintf(txtstr,"MTDOT"); |
jamescmaki | 7:c9d37522b975 | 247 | evbLCD->writeText(24, 3, font_6x8, txtstr, strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 248 | sprintf(txtstr,"Evaluation"); |
jamescmaki | 7:c9d37522b975 | 249 | evbLCD->writeText(24, 4, font_6x8, txtstr, strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 250 | sprintf(txtstr,"Board"); |
jamescmaki | 7:c9d37522b975 | 251 | evbLCD->writeText(24, 5, font_6x8, txtstr, strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 252 | |
falingtrea | 2:75adc72aa6a0 | 253 | evbLCD->endUpdate(); |
falingtrea | 2:75adc72aa6a0 | 254 | |
jamescmaki | 7:c9d37522b975 | 255 | printf("\n\r setup mdot\n\r"); |
falingtrea | 0:bdd16076aaa5 | 256 | |
falingtrea | 0:bdd16076aaa5 | 257 | // get a mDot handle |
falingtrea | 0:bdd16076aaa5 | 258 | mdot_radio = mDot::getInstance(); |
falingtrea | 0:bdd16076aaa5 | 259 | |
falingtrea | 0:bdd16076aaa5 | 260 | if (mdot_radio) { |
falingtrea | 0:bdd16076aaa5 | 261 | // reset to default config so we know what state we're in |
falingtrea | 0:bdd16076aaa5 | 262 | mdot_radio->resetConfig(); |
falingtrea | 0:bdd16076aaa5 | 263 | |
falingtrea | 0:bdd16076aaa5 | 264 | // Setting up LED1 as activity LED |
falingtrea | 0:bdd16076aaa5 | 265 | mdot_radio->setActivityLedPin(PB_0); |
falingtrea | 0:bdd16076aaa5 | 266 | mdot_radio->setActivityLedEnable(true); |
falingtrea | 0:bdd16076aaa5 | 267 | |
falingtrea | 0:bdd16076aaa5 | 268 | // Read node ID |
falingtrea | 0:bdd16076aaa5 | 269 | mdot_EUI = mdot_radio->getDeviceId(); |
falingtrea | 0:bdd16076aaa5 | 270 | printf("mDot EUI = "); |
jamescmaki | 7:c9d37522b975 | 271 | for (i = 0; i < mdot_EUI.size(); i++) { |
falingtrea | 0:bdd16076aaa5 | 272 | printf("%02x ", mdot_EUI[i]); |
falingtrea | 0:bdd16076aaa5 | 273 | } |
falingtrea | 0:bdd16076aaa5 | 274 | printf("\n\r"); |
falingtrea | 0:bdd16076aaa5 | 275 | |
jamescmaki | 5:72d4d95e1d42 | 276 | printf("setting Network Mode to %s\r\n", public_net ? "public" : "private"); |
jamescmaki | 5:72d4d95e1d42 | 277 | if ((mdot_ret = mdot_radio->setPublicNetwork(public_net)) != mDot::MDOT_OK) { |
jamescmaki | 5:72d4d95e1d42 | 278 | log_error(mdot_radio, "failed to set Network Mode", mdot_ret); |
jamescmaki | 5:72d4d95e1d42 | 279 | } |
falingtrea | 2:75adc72aa6a0 | 280 | |
jamescmaki | 5:72d4d95e1d42 | 281 | /* |
jamescmaki | 5:72d4d95e1d42 | 282 | * Frequency sub-band is valid for NAM only and for Private networks should be set to a value |
jamescmaki | 5:72d4d95e1d42 | 283 | * between 1-8 that matches the the LoRa gateway setting. Public networks use sub-band 0 only. |
jamescmaki | 5:72d4d95e1d42 | 284 | * This function can be commented out for EU networks |
jamescmaki | 5:72d4d95e1d42 | 285 | */ |
falingtrea | 0:bdd16076aaa5 | 286 | printf("setting frequency sub band\r\n"); |
falingtrea | 0:bdd16076aaa5 | 287 | if ((mdot_ret = mdot_radio->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 288 | log_error(mdot_radio, "failed to set frequency sub band", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 289 | } |
falingtrea | 0:bdd16076aaa5 | 290 | |
falingtrea | 0:bdd16076aaa5 | 291 | printf("setting network name\r\n"); |
jamescmaki | 5:72d4d95e1d42 | 292 | if (public_net) { |
jamescmaki | 5:72d4d95e1d42 | 293 | if ((mdot_ret = mdot_radio->setNetworkId(config_app_id)) != mDot::MDOT_OK) { |
jamescmaki | 5:72d4d95e1d42 | 294 | log_error(mdot_radio, "failed to set network name", mdot_ret); |
jamescmaki | 5:72d4d95e1d42 | 295 | } |
jamescmaki | 5:72d4d95e1d42 | 296 | } else { |
jamescmaki | 5:72d4d95e1d42 | 297 | if ((mdot_ret = mdot_radio->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
jamescmaki | 5:72d4d95e1d42 | 298 | log_error(mdot_radio, "failed to set network name", mdot_ret); |
jamescmaki | 5:72d4d95e1d42 | 299 | } |
falingtrea | 0:bdd16076aaa5 | 300 | } |
falingtrea | 0:bdd16076aaa5 | 301 | |
falingtrea | 0:bdd16076aaa5 | 302 | printf("setting network password\r\n"); |
jamescmaki | 5:72d4d95e1d42 | 303 | if (public_net) { |
jamescmaki | 5:72d4d95e1d42 | 304 | if ((mdot_ret = mdot_radio->setNetworkKey(config_app_key)) != mDot::MDOT_OK) { |
jamescmaki | 5:72d4d95e1d42 | 305 | log_error(mdot_radio, "failed to set network key", mdot_ret); |
jamescmaki | 5:72d4d95e1d42 | 306 | } |
jamescmaki | 5:72d4d95e1d42 | 307 | } else { |
jamescmaki | 5:72d4d95e1d42 | 308 | if ((mdot_ret = mdot_radio->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
jamescmaki | 5:72d4d95e1d42 | 309 | log_error(mdot_radio, "failed to set network pass phrase", mdot_ret); |
jamescmaki | 5:72d4d95e1d42 | 310 | } |
falingtrea | 0:bdd16076aaa5 | 311 | } |
falingtrea | 0:bdd16076aaa5 | 312 | |
falingtrea | 0:bdd16076aaa5 | 313 | // attempt to join the network |
falingtrea | 0:bdd16076aaa5 | 314 | printf("joining network\r\n"); |
falingtrea | 2:75adc72aa6a0 | 315 | while (((mdot_ret = mdot_radio->joinNetwork()) != mDot::MDOT_OK) && (!exit_program)) { |
falingtrea | 0:bdd16076aaa5 | 316 | log_error(mdot_radio,"failed to join network:", mdot_ret); |
jamescmaki | 7:c9d37522b975 | 317 | if (mdot_radio->getFrequencyBand() == mDot::FB_868) { |
jamescmaki | 5:72d4d95e1d42 | 318 | mdot_ret = mdot_radio->getNextTxMs(); |
jamescmaki | 5:72d4d95e1d42 | 319 | } else { |
jamescmaki | 5:72d4d95e1d42 | 320 | mdot_ret = 0; |
jamescmaki | 7:c9d37522b975 | 321 | } |
jamescmaki | 7:c9d37522b975 | 322 | printf("delay = %lu\n\r", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 323 | osDelay(mdot_ret + 1); |
falingtrea | 0:bdd16076aaa5 | 324 | } |
falingtrea | 2:75adc72aa6a0 | 325 | |
falingtrea | 2:75adc72aa6a0 | 326 | /* |
falingtrea | 2:75adc72aa6a0 | 327 | * Check for PB1 press during network join attempt |
falingtrea | 2:75adc72aa6a0 | 328 | */ |
falingtrea | 2:75adc72aa6a0 | 329 | if (exit_program) { |
falingtrea | 2:75adc72aa6a0 | 330 | printf("Exiting program\n\r"); |
falingtrea | 2:75adc72aa6a0 | 331 | evbLCD->clearBuffer(); |
falingtrea | 2:75adc72aa6a0 | 332 | sprintf(txtstr,"Exiting Program"); |
jamescmaki | 7:c9d37522b975 | 333 | evbLCD->writeText(0, 4, font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 334 | exit(1); |
falingtrea | 2:75adc72aa6a0 | 335 | } |
falingtrea | 0:bdd16076aaa5 | 336 | } else { |
falingtrea | 0:bdd16076aaa5 | 337 | printf("radio setup failed\n\r"); |
falingtrea | 0:bdd16076aaa5 | 338 | //exit(1); |
falingtrea | 0:bdd16076aaa5 | 339 | } |
falingtrea | 0:bdd16076aaa5 | 340 | |
falingtrea | 0:bdd16076aaa5 | 341 | osDelay(200); |
jamescmaki | 7:c9d37522b975 | 342 | evbBackLight->setPWM(NCP5623B::LED_3, 16); // enable LED2 on EVB and set to 50% PWM |
falingtrea | 0:bdd16076aaa5 | 343 | |
falingtrea | 2:75adc72aa6a0 | 344 | // sets LED2 to 50% max current |
falingtrea | 0:bdd16076aaa5 | 345 | evbBackLight->setLEDCurrent(16); |
falingtrea | 0:bdd16076aaa5 | 346 | |
falingtrea | 0:bdd16076aaa5 | 347 | printf("Start of Test\n\r"); |
falingtrea | 0:bdd16076aaa5 | 348 | |
jamescmaki | 5:72d4d95e1d42 | 349 | osDelay (500); // allows other threads to process |
falingtrea | 2:75adc72aa6a0 | 350 | printf("shutdown LED:\n\r"); |
falingtrea | 2:75adc72aa6a0 | 351 | evbBackLight->shutdown(); |
falingtrea | 0:bdd16076aaa5 | 352 | |
jamescmaki | 5:72d4d95e1d42 | 353 | osDelay (500); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 354 | printf("Turn on LED2\n\r"); |
falingtrea | 0:bdd16076aaa5 | 355 | evbBackLight->setLEDCurrent(16); |
falingtrea | 0:bdd16076aaa5 | 356 | |
falingtrea | 0:bdd16076aaa5 | 357 | data = evbAccel->getWhoAmI(); |
falingtrea | 0:bdd16076aaa5 | 358 | printf("Accelerometer who_am_i value = %x \n\r", data); |
falingtrea | 0:bdd16076aaa5 | 359 | |
falingtrea | 0:bdd16076aaa5 | 360 | result = evbAccel->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 361 | printf("status byte = %x \n\r", result); |
falingtrea | 0:bdd16076aaa5 | 362 | |
falingtrea | 0:bdd16076aaa5 | 363 | printf("Barometer who_am_i check = %s \n\r", evbBaro->testWhoAmI() ? "TRUE" : "FALSE"); |
falingtrea | 0:bdd16076aaa5 | 364 | |
falingtrea | 0:bdd16076aaa5 | 365 | result = evbBaro->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 366 | printf("status byte = %x \n\r", result); |
falingtrea | 0:bdd16076aaa5 | 367 | |
falingtrea | 0:bdd16076aaa5 | 368 | /* |
falingtrea | 0:bdd16076aaa5 | 369 | * Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz |
falingtrea | 0:bdd16076aaa5 | 370 | * normal oversample mode, High pass filter off |
falingtrea | 0:bdd16076aaa5 | 371 | */ |
jamescmaki | 7:c9d37522b975 | 372 | evbAccel->setCommonParameters(MMA845x::RANGE_8g, MMA845x::RES_MAX,MMA845x::LN_OFF, |
jamescmaki | 7:c9d37522b975 | 373 | MMA845x::DR_1_56,MMA845x::OS_NORMAL,MMA845x::HPF_OFF); |
falingtrea | 0:bdd16076aaa5 | 374 | |
falingtrea | 0:bdd16076aaa5 | 375 | /* |
falingtrea | 0:bdd16076aaa5 | 376 | * Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition. |
falingtrea | 0:bdd16076aaa5 | 377 | * and a sample taken every second when in active mode |
falingtrea | 0:bdd16076aaa5 | 378 | */ |
falingtrea | 0:bdd16076aaa5 | 379 | evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, |
falingtrea | 0:bdd16076aaa5 | 380 | MPL3115A2::AT_1); |
falingtrea | 0:bdd16076aaa5 | 381 | /* |
falingtrea | 0:bdd16076aaa5 | 382 | * Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution, |
falingtrea | 0:bdd16076aaa5 | 383 | * and 16000 lux range |
falingtrea | 0:bdd16076aaa5 | 384 | */ |
falingtrea | 0:bdd16076aaa5 | 385 | |
falingtrea | 0:bdd16076aaa5 | 386 | evbAmbLight->setMode(ISL29011::ALS_CONT); |
falingtrea | 0:bdd16076aaa5 | 387 | evbAmbLight->setResolution(ISL29011::ADC_16BIT); |
falingtrea | 0:bdd16076aaa5 | 388 | evbAmbLight->setRange(ISL29011::RNG_16000); |
falingtrea | 0:bdd16076aaa5 | 389 | |
falingtrea | 0:bdd16076aaa5 | 390 | /* |
falingtrea | 0:bdd16076aaa5 | 391 | * Set the accelerometer for active mode |
falingtrea | 0:bdd16076aaa5 | 392 | */ |
falingtrea | 0:bdd16076aaa5 | 393 | evbAccel->activeMode(); |
falingtrea | 0:bdd16076aaa5 | 394 | |
falingtrea | 0:bdd16076aaa5 | 395 | /* |
falingtrea | 0:bdd16076aaa5 | 396 | * Clear the min-max registers in the Barometric Sensor |
falingtrea | 0:bdd16076aaa5 | 397 | */ |
falingtrea | 0:bdd16076aaa5 | 398 | evbBaro->clearMinMaxRegs(); |
falingtrea | 0:bdd16076aaa5 | 399 | |
falingtrea | 0:bdd16076aaa5 | 400 | evbBackLight->setLEDCurrent(0); |
falingtrea | 0:bdd16076aaa5 | 401 | |
falingtrea | 0:bdd16076aaa5 | 402 | /* |
falingtrea | 2:75adc72aa6a0 | 403 | * Check for PB1 press during network join attempt |
falingtrea | 2:75adc72aa6a0 | 404 | */ |
falingtrea | 2:75adc72aa6a0 | 405 | if (exit_program) { |
falingtrea | 2:75adc72aa6a0 | 406 | printf("Exiting program\n\r"); |
falingtrea | 2:75adc72aa6a0 | 407 | evbLCD->clearBuffer(); |
falingtrea | 2:75adc72aa6a0 | 408 | sprintf(txtstr,"Exiting Program"); |
jamescmaki | 7:c9d37522b975 | 409 | evbLCD->writeText(0, 4, font_6x8, txtstr, strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 410 | exit(1); |
falingtrea | 2:75adc72aa6a0 | 411 | } |
falingtrea | 2:75adc72aa6a0 | 412 | |
jamescmaki | 5:72d4d95e1d42 | 413 | if (!senet_demo) { |
jamescmaki | 5:72d4d95e1d42 | 414 | lora_pl.clear(); |
jamescmaki | 5:72d4d95e1d42 | 415 | lora_pl.push_back(19); |
jamescmaki | 5:72d4d95e1d42 | 416 | lora_pl.push_back(strlen(m2x_device)); |
jamescmaki | 5:72d4d95e1d42 | 417 | lora_pl.insert(lora_pl.end(), m2x_device, m2x_device + strlen(m2x_device)); |
jamescmaki | 5:72d4d95e1d42 | 418 | while ((mdot_ret = mdot_radio->send(lora_pl)) != mDot::MDOT_OK) { |
jamescmaki | 5:72d4d95e1d42 | 419 | log_error(mdot_radio, "Failed to register m2x_device", mdot_ret); |
jamescmaki | 5:72d4d95e1d42 | 420 | osDelay(2000); |
jamescmaki | 5:72d4d95e1d42 | 421 | } |
jamescmaki | 4:71e411fbd9ff | 422 | |
jamescmaki | 5:72d4d95e1d42 | 423 | lora_pl.clear(); |
jamescmaki | 5:72d4d95e1d42 | 424 | lora_pl.push_back(20); |
jamescmaki | 5:72d4d95e1d42 | 425 | lora_pl.push_back(strlen(m2x_key)); |
jamescmaki | 5:72d4d95e1d42 | 426 | lora_pl.insert(lora_pl.end(), m2x_key, m2x_key + strlen(m2x_key)); |
jamescmaki | 5:72d4d95e1d42 | 427 | while ((mdot_ret = mdot_radio->send(lora_pl)) != mDot::MDOT_OK) { |
jamescmaki | 5:72d4d95e1d42 | 428 | log_error(mdot_radio, "Failed to register m2x_key", mdot_ret); |
jamescmaki | 5:72d4d95e1d42 | 429 | osDelay(2000); |
jamescmaki | 5:72d4d95e1d42 | 430 | } |
jamescmaki | 4:71e411fbd9ff | 431 | } |
jamescmaki | 4:71e411fbd9ff | 432 | |
jamescmaki | 7:c9d37522b975 | 433 | loops = 0; |
jamescmaki | 7:c9d37522b975 | 434 | time_t tx_next = time(NULL) + tx_interval; |
jamescmaki | 7:c9d37522b975 | 435 | |
jamescmaki | 7:c9d37522b975 | 436 | /* |
jamescmaki | 7:c9d37522b975 | 437 | * Main data acquisition loop |
jamescmaki | 7:c9d37522b975 | 438 | */ |
jamescmaki | 7:c9d37522b975 | 439 | while (!exit_program) { |
jamescmaki | 7:c9d37522b975 | 440 | osDelay(200); |
jamescmaki | 7:c9d37522b975 | 441 | |
falingtrea | 0:bdd16076aaa5 | 442 | evbLCD->startUpdate(); |
falingtrea | 0:bdd16076aaa5 | 443 | evbLCD->clearBuffer(); |
falingtrea | 0:bdd16076aaa5 | 444 | |
falingtrea | 0:bdd16076aaa5 | 445 | /* |
falingtrea | 0:bdd16076aaa5 | 446 | * Test Accelerometer XYZ data ready bit to see if acquisition complete |
falingtrea | 0:bdd16076aaa5 | 447 | */ |
jamescmaki | 7:c9d37522b975 | 448 | count = 1; |
falingtrea | 0:bdd16076aaa5 | 449 | do { |
jamescmaki | 5:72d4d95e1d42 | 450 | osDelay(100); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 451 | result = evbAccel->getStatus(); |
jamescmaki | 7:c9d37522b975 | 452 | if ((count++ % 10) == 0) { |
jamescmaki | 7:c9d37522b975 | 453 | printf("waiting on accelerometer reading\r\n"); |
jamescmaki | 7:c9d37522b975 | 454 | } |
jamescmaki | 7:c9d37522b975 | 455 | } while ((result & MMA845x::XYZDR) == 0); |
falingtrea | 0:bdd16076aaa5 | 456 | |
falingtrea | 0:bdd16076aaa5 | 457 | /* |
falingtrea | 0:bdd16076aaa5 | 458 | * Retrieve and print out accelerometer data |
falingtrea | 0:bdd16076aaa5 | 459 | */ |
falingtrea | 0:bdd16076aaa5 | 460 | accel_data = evbAccel->getXYZ(); |
falingtrea | 0:bdd16076aaa5 | 461 | |
falingtrea | 0:bdd16076aaa5 | 462 | sprintf(txtstr,"Accelerometer"); |
jamescmaki | 7:c9d37522b975 | 463 | evbLCD->writeText(0, 0, font_6x8, txtstr, strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 464 | sprintf(txtstr, "x = %d", accel_data._x); |
jamescmaki | 7:c9d37522b975 | 465 | evbLCD->writeText(20, 1, font_6x8, txtstr, strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 466 | sprintf(txtstr, "y = %d", accel_data._y); |
jamescmaki | 7:c9d37522b975 | 467 | evbLCD->writeText(20, 2, font_6x8, txtstr, strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 468 | sprintf(txtstr, "z = %d", accel_data._z ); |
jamescmaki | 7:c9d37522b975 | 469 | evbLCD->writeText(20, 3, font_6x8, txtstr, strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 470 | |
jamescmaki | 5:72d4d95e1d42 | 471 | // convert to simple position value for use in send/recv |
jamescmaki | 5:72d4d95e1d42 | 472 | if (accel_data._x > 500 && accel_data._z < 500) { |
jamescmaki | 5:72d4d95e1d42 | 473 | position_value = 0x02; |
jamescmaki | 5:72d4d95e1d42 | 474 | } else if (accel_data._x < 500 && accel_data._z > 500) { |
jamescmaki | 5:72d4d95e1d42 | 475 | position_value = 0x01; |
jamescmaki | 5:72d4d95e1d42 | 476 | } else { |
jamescmaki | 5:72d4d95e1d42 | 477 | position_value= 0x00; |
jamescmaki | 5:72d4d95e1d42 | 478 | } |
jamescmaki | 5:72d4d95e1d42 | 479 | |
falingtrea | 0:bdd16076aaa5 | 480 | /* |
falingtrea | 0:bdd16076aaa5 | 481 | * Trigger a Pressure reading |
falingtrea | 0:bdd16076aaa5 | 482 | */ |
falingtrea | 0:bdd16076aaa5 | 483 | evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, |
falingtrea | 0:bdd16076aaa5 | 484 | MPL3115A2::AT_1); |
falingtrea | 0:bdd16076aaa5 | 485 | evbBaro->triggerOneShot(); |
falingtrea | 0:bdd16076aaa5 | 486 | |
falingtrea | 0:bdd16076aaa5 | 487 | /* |
falingtrea | 0:bdd16076aaa5 | 488 | * Test barometer device status to see if acquisition is complete |
falingtrea | 0:bdd16076aaa5 | 489 | */ |
jamescmaki | 7:c9d37522b975 | 490 | count = 1; |
falingtrea | 0:bdd16076aaa5 | 491 | do { |
jamescmaki | 5:72d4d95e1d42 | 492 | osDelay(100); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 493 | result = evbBaro->getStatus(); |
jamescmaki | 7:c9d37522b975 | 494 | if ((count++ % 10) == 0) { |
jamescmaki | 7:c9d37522b975 | 495 | printf("waiting on barometer reading\r\n"); |
jamescmaki | 7:c9d37522b975 | 496 | } |
jamescmaki | 7:c9d37522b975 | 497 | } while ((result & MPL3115A2::PTDR) == 0); |
falingtrea | 0:bdd16076aaa5 | 498 | |
falingtrea | 0:bdd16076aaa5 | 499 | /* |
falingtrea | 0:bdd16076aaa5 | 500 | * Retrieve and print out barometric pressure |
falingtrea | 0:bdd16076aaa5 | 501 | */ |
falingtrea | 0:bdd16076aaa5 | 502 | pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value |
jamescmaki | 5:72d4d95e1d42 | 503 | num_whole = pressure >> 2; // 18 bit integer significant |
jamescmaki | 5:72d4d95e1d42 | 504 | num_frac = (pressure & 0x3) * 25; // 2 bit fractional 0.25 per bit |
falingtrea | 0:bdd16076aaa5 | 505 | sprintf(txtstr,"Press=%ld.%02d Pa", num_whole, num_frac); |
jamescmaki | 7:c9d37522b975 | 506 | evbLCD->writeText(0, 4, font_6x8, txtstr, strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 507 | |
falingtrea | 0:bdd16076aaa5 | 508 | /* |
jamescmaki | 7:c9d37522b975 | 509 | * Trigger an Altitude reading |
falingtrea | 0:bdd16076aaa5 | 510 | */ |
falingtrea | 0:bdd16076aaa5 | 511 | evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16, |
falingtrea | 0:bdd16076aaa5 | 512 | MPL3115A2::AT_1); |
falingtrea | 0:bdd16076aaa5 | 513 | evbBaro->triggerOneShot(); |
falingtrea | 0:bdd16076aaa5 | 514 | |
falingtrea | 0:bdd16076aaa5 | 515 | /* |
falingtrea | 0:bdd16076aaa5 | 516 | * Test barometer device status to see if acquisition is complete |
falingtrea | 0:bdd16076aaa5 | 517 | */ |
jamescmaki | 7:c9d37522b975 | 518 | count = 1; |
falingtrea | 0:bdd16076aaa5 | 519 | do { |
jamescmaki | 5:72d4d95e1d42 | 520 | osDelay(100); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 521 | result = evbBaro->getStatus(); |
jamescmaki | 7:c9d37522b975 | 522 | if ((count++ % 10) == 0) { |
jamescmaki | 7:c9d37522b975 | 523 | printf("waiting on temperature reading\r\n"); |
jamescmaki | 7:c9d37522b975 | 524 | } |
jamescmaki | 7:c9d37522b975 | 525 | } while ((result & MPL3115A2::PTDR) == 0); |
falingtrea | 0:bdd16076aaa5 | 526 | |
falingtrea | 0:bdd16076aaa5 | 527 | /* |
falingtrea | 0:bdd16076aaa5 | 528 | * Retrieve and print out altitude and temperature |
falingtrea | 0:bdd16076aaa5 | 529 | */ |
falingtrea | 0:bdd16076aaa5 | 530 | baro_data = evbBaro->getAllData(false); |
jamescmaki | 5:72d4d95e1d42 | 531 | baro_data._baro /= 4096; // convert 32 bit signed to 20 bit signed value |
jamescmaki | 5:72d4d95e1d42 | 532 | num_whole = baro_data._baro / 16; // 18 bit signed significant integer |
jamescmaki | 5:72d4d95e1d42 | 533 | num_frac = (baro_data._baro & 0xF) * 625 / 100; // 4 bit fractional .0625 per bit |
jamescmaki | 7:c9d37522b975 | 534 | sprintf(txtstr, "Alti=%ld.%03d m", num_whole, num_frac); |
falingtrea | 0:bdd16076aaa5 | 535 | evbLCD->writeText(0,5,font_6x8,txtstr,strlen(txtstr)); |
jamescmaki | 5:72d4d95e1d42 | 536 | num_whole = baro_data._temp / 16; // 8 bit signed significant integer |
jamescmaki | 5:72d4d95e1d42 | 537 | num_frac = (baro_data._temp & 0x0F) * 625 / 10; // 4 bit fractional .0625 per bit |
jamescmaki | 7:c9d37522b975 | 538 | sprintf(txtstr, "Temp=%ld.%03d C", num_whole, num_frac); |
jamescmaki | 7:c9d37522b975 | 539 | evbLCD->writeText(0, 6, font_6x8, txtstr, strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 540 | |
falingtrea | 0:bdd16076aaa5 | 541 | /* |
falingtrea | 0:bdd16076aaa5 | 542 | * retrieve and print out Ambient Light level |
falingtrea | 0:bdd16076aaa5 | 543 | */ |
falingtrea | 0:bdd16076aaa5 | 544 | lux_data = evbAmbLight->getData(); |
jamescmaki | 5:72d4d95e1d42 | 545 | num_whole = lux_data * 24 / 100; // 16000 lux full scale .24 lux per bit |
falingtrea | 0:bdd16076aaa5 | 546 | num_frac = lux_data * 24 % 100; |
jamescmaki | 4:71e411fbd9ff | 547 | sprintf(txtstr, "Light=%ld.%02d lux", num_whole, num_frac); |
jamescmaki | 7:c9d37522b975 | 548 | evbLCD->writeText(0, 7, font_6x8, txtstr, strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 549 | |
falingtrea | 0:bdd16076aaa5 | 550 | evbLCD->endUpdate(); |
falingtrea | 0:bdd16076aaa5 | 551 | |
jamescmaki | 7:c9d37522b975 | 552 | if (time(NULL) > tx_next) { |
jamescmaki | 7:c9d37522b975 | 553 | tx_next = time(NULL) + tx_interval; |
jamescmaki | 7:c9d37522b975 | 554 | |
jamescmaki | 4:71e411fbd9ff | 555 | lora_pl.clear(); |
jamescmaki | 5:72d4d95e1d42 | 556 | |
jamescmaki | 5:72d4d95e1d42 | 557 | if (senet_demo) { |
jamescmaki | 5:72d4d95e1d42 | 558 | // Position Value |
jamescmaki | 5:72d4d95e1d42 | 559 | lora_pl.push_back(0); |
jamescmaki | 5:72d4d95e1d42 | 560 | lora_pl.push_back(position_value); |
jamescmaki | 5:72d4d95e1d42 | 561 | } else { |
jamescmaki | 5:72d4d95e1d42 | 562 | // Current Acceleration 3-Axis Value |
jamescmaki | 5:72d4d95e1d42 | 563 | lora_pl.push_back(14); |
jamescmaki | 5:72d4d95e1d42 | 564 | lora_pl.push_back(6); |
jamescmaki | 5:72d4d95e1d42 | 565 | converts.f_s = accel_data._x; |
jamescmaki | 5:72d4d95e1d42 | 566 | lora_pl.push_back(converts.t_u[1]); |
jamescmaki | 5:72d4d95e1d42 | 567 | lora_pl.push_back(converts.t_u[0]); |
jamescmaki | 5:72d4d95e1d42 | 568 | converts.f_s = accel_data._y; |
jamescmaki | 5:72d4d95e1d42 | 569 | lora_pl.push_back(converts.t_u[1]); |
jamescmaki | 5:72d4d95e1d42 | 570 | lora_pl.push_back(converts.t_u[0]); |
jamescmaki | 5:72d4d95e1d42 | 571 | converts.f_s = accel_data._z; |
jamescmaki | 5:72d4d95e1d42 | 572 | lora_pl.push_back(converts.t_u[1]); |
jamescmaki | 5:72d4d95e1d42 | 573 | lora_pl.push_back(converts.t_u[0]); |
jamescmaki | 5:72d4d95e1d42 | 574 | // Current Pressure Value |
jamescmaki | 5:72d4d95e1d42 | 575 | lora_pl.push_back(8); |
jamescmaki | 5:72d4d95e1d42 | 576 | lora_pl.push_back(3); |
jamescmaki | 5:72d4d95e1d42 | 577 | convertl.f_u = pressure; |
jamescmaki | 5:72d4d95e1d42 | 578 | lora_pl.push_back(convertl.t_u[2]); |
jamescmaki | 5:72d4d95e1d42 | 579 | lora_pl.push_back(convertl.t_u[1]); |
jamescmaki | 5:72d4d95e1d42 | 580 | lora_pl.push_back(convertl.t_u[0]); |
jamescmaki | 5:72d4d95e1d42 | 581 | // Current Ambient Light Value |
jamescmaki | 5:72d4d95e1d42 | 582 | lora_pl.push_back(5); |
jamescmaki | 5:72d4d95e1d42 | 583 | lora_pl.push_back(2); |
jamescmaki | 5:72d4d95e1d42 | 584 | converts.f_u = lux_data; |
jamescmaki | 5:72d4d95e1d42 | 585 | lora_pl.push_back(converts.t_u[1]); |
jamescmaki | 5:72d4d95e1d42 | 586 | lora_pl.push_back(converts.t_u[0]); |
jamescmaki | 5:72d4d95e1d42 | 587 | // Current Temperature Value |
jamescmaki | 5:72d4d95e1d42 | 588 | lora_pl.push_back(11); |
jamescmaki | 5:72d4d95e1d42 | 589 | lora_pl.push_back(2); |
jamescmaki | 5:72d4d95e1d42 | 590 | converts.f_s = baro_data._temp; |
jamescmaki | 5:72d4d95e1d42 | 591 | lora_pl.push_back(converts.t_u[1]); |
jamescmaki | 5:72d4d95e1d42 | 592 | lora_pl.push_back(converts.t_u[0]); |
jamescmaki | 5:72d4d95e1d42 | 593 | } |
falingtrea | 0:bdd16076aaa5 | 594 | |
jamescmaki | 6:c212427700af | 595 | printf("Sending LoRa message, length: %d\r\n", lora_pl.size()); |
jamescmaki | 4:71e411fbd9ff | 596 | if ((mdot_ret = mdot_radio->send(lora_pl)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 597 | log_error(mdot_radio, "failed to send", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 598 | } else { |
falingtrea | 0:bdd16076aaa5 | 599 | printf("successfully sent data to gateway\r\n"); |
jamescmaki | 5:72d4d95e1d42 | 600 | |
jamescmaki | 5:72d4d95e1d42 | 601 | if (senet_demo) { |
jamescmaki | 5:72d4d95e1d42 | 602 | lora_pl.clear(); |
jamescmaki | 5:72d4d95e1d42 | 603 | if ((mdot_ret = mdot_radio->recv(lora_pl)) != mDot::MDOT_OK) { |
jamescmaki | 5:72d4d95e1d42 | 604 | log_error(mdot_radio, "failed to recv:", mdot_ret); |
jamescmaki | 5:72d4d95e1d42 | 605 | } else { |
jamescmaki | 5:72d4d95e1d42 | 606 | printf("recv data: "); |
jamescmaki | 5:72d4d95e1d42 | 607 | for(int i = 0;i < lora_pl.size();i++) { |
jamescmaki | 5:72d4d95e1d42 | 608 | printf("%02X", lora_pl[i]); |
jamescmaki | 5:72d4d95e1d42 | 609 | } |
jamescmaki | 5:72d4d95e1d42 | 610 | printf("\r\n"); |
jamescmaki | 5:72d4d95e1d42 | 611 | if(lora_pl[0] == position_value) { |
jamescmaki | 5:72d4d95e1d42 | 612 | evbBackLight->setLEDCurrent(16); |
jamescmaki | 5:72d4d95e1d42 | 613 | } else { |
jamescmaki | 5:72d4d95e1d42 | 614 | evbBackLight->setLEDCurrent(0); |
jamescmaki | 5:72d4d95e1d42 | 615 | } |
jamescmaki | 5:72d4d95e1d42 | 616 | } |
jamescmaki | 5:72d4d95e1d42 | 617 | } |
falingtrea | 0:bdd16076aaa5 | 618 | } |
falingtrea | 0:bdd16076aaa5 | 619 | } |
jamescmaki | 7:c9d37522b975 | 620 | |
jamescmaki | 7:c9d37522b975 | 621 | printf("finished iteration %d\n\r", loops++); |
jamescmaki | 7:c9d37522b975 | 622 | } |
falingtrea | 0:bdd16076aaa5 | 623 | |
falingtrea | 0:bdd16076aaa5 | 624 | evbBaro->triggerOneShot(); |
falingtrea | 0:bdd16076aaa5 | 625 | |
falingtrea | 0:bdd16076aaa5 | 626 | do { |
jamescmaki | 5:72d4d95e1d42 | 627 | osDelay(200); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 628 | result = evbBaro->getStatus(); |
jamescmaki | 7:c9d37522b975 | 629 | } while ((result & MPL3115A2::PTDR) == 0); |
falingtrea | 0:bdd16076aaa5 | 630 | |
falingtrea | 0:bdd16076aaa5 | 631 | baro_data = evbBaro->getAllData(true); |
falingtrea | 0:bdd16076aaa5 | 632 | printf ("minBaro=%ld maxBaro=%ld minTemp=%d maxTemp=%d\n\r", baro_data._minbaro, baro_data._maxbaro, |
falingtrea | 0:bdd16076aaa5 | 633 | baro_data._mintemp, baro_data._maxtemp); |
falingtrea | 0:bdd16076aaa5 | 634 | |
falingtrea | 0:bdd16076aaa5 | 635 | printf("End of Test\n\r"); |
falingtrea | 0:bdd16076aaa5 | 636 | |
falingtrea | 2:75adc72aa6a0 | 637 | evbLCD->clearBuffer(); |
falingtrea | 2:75adc72aa6a0 | 638 | sprintf(txtstr,"Exiting Program"); |
jamescmaki | 7:c9d37522b975 | 639 | evbLCD->writeText(0, 4, font_6x8, txtstr, strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 640 | } |
falingtrea | 0:bdd16076aaa5 | 641 | |
falingtrea | 0:bdd16076aaa5 | 642 | /* |
falingtrea | 3:68e974f5f532 | 643 | * Sets pb1_low flag. Slag is cleared in pb1_debounce thread |
falingtrea | 0:bdd16076aaa5 | 644 | */ |
falingtrea | 0:bdd16076aaa5 | 645 | void pb1ISR(void) |
falingtrea | 0:bdd16076aaa5 | 646 | { |
jamescmaki | 5:72d4d95e1d42 | 647 | if (!pb1_low) |
jamescmaki | 5:72d4d95e1d42 | 648 | pb1_low = true; |
falingtrea | 3:68e974f5f532 | 649 | } |
falingtrea | 0:bdd16076aaa5 | 650 | |
falingtrea | 3:68e974f5f532 | 651 | /* |
falingtrea | 3:68e974f5f532 | 652 | * Debounces pb1. Also exits program if pushbutton 1 is pressed |
falingtrea | 3:68e974f5f532 | 653 | */ |
falingtrea | 3:68e974f5f532 | 654 | void pb1_debounce(void const *args) |
falingtrea | 3:68e974f5f532 | 655 | { |
falingtrea | 3:68e974f5f532 | 656 | static uint8_t count = 0; |
falingtrea | 3:68e974f5f532 | 657 | |
falingtrea | 3:68e974f5f532 | 658 | while (true) { |
jamescmaki | 5:72d4d95e1d42 | 659 | if (pb1_low && (mDot08 == 0)) { |
jamescmaki | 5:72d4d95e1d42 | 660 | count++; |
jamescmaki | 5:72d4d95e1d42 | 661 | } else { |
jamescmaki | 5:72d4d95e1d42 | 662 | count = 0; |
jamescmaki | 5:72d4d95e1d42 | 663 | pb1_low = false; |
jamescmaki | 5:72d4d95e1d42 | 664 | } |
falingtrea | 3:68e974f5f532 | 665 | |
jamescmaki | 7:c9d37522b975 | 666 | if (count == 5) { |
jamescmaki | 7:c9d37522b975 | 667 | printf("pb1_debounce: setting exit_program\r\n"); |
jamescmaki | 5:72d4d95e1d42 | 668 | exit_program = true; |
jamescmaki | 7:c9d37522b975 | 669 | } |
jamescmaki | 5:72d4d95e1d42 | 670 | |
falingtrea | 3:68e974f5f532 | 671 | Thread::wait(5); |
falingtrea | 3:68e974f5f532 | 672 | } |
falingtrea | 0:bdd16076aaa5 | 673 | } |
falingtrea | 0:bdd16076aaa5 | 674 | |
falingtrea | 0:bdd16076aaa5 | 675 | /* |
falingtrea | 3:68e974f5f532 | 676 | * Sets pb2_low flag. Flag is cleared in pb2_debounce thread |
falingtrea | 0:bdd16076aaa5 | 677 | */ |
falingtrea | 0:bdd16076aaa5 | 678 | void pb2ISR(void) |
falingtrea | 0:bdd16076aaa5 | 679 | { |
jamescmaki | 5:72d4d95e1d42 | 680 | if (!pb2_low) |
jamescmaki | 5:72d4d95e1d42 | 681 | pb2_low = true; |
falingtrea | 3:68e974f5f532 | 682 | } |
falingtrea | 0:bdd16076aaa5 | 683 | |
falingtrea | 3:68e974f5f532 | 684 | /* |
falingtrea | 3:68e974f5f532 | 685 | * Debounces pb2. Also changes packet transmit time to every other, |
falingtrea | 3:68e974f5f532 | 686 | * every fifth, or every tenth sample when SW2 pushed |
falingtrea | 3:68e974f5f532 | 687 | * Also triggers a thread to transmit a configuration packet |
falingtrea | 3:68e974f5f532 | 688 | */ |
falingtrea | 3:68e974f5f532 | 689 | void pb2_debounce(void const *args) |
falingtrea | 3:68e974f5f532 | 690 | { |
falingtrea | 3:68e974f5f532 | 691 | static uint8_t count = 0; |
falingtrea | 3:68e974f5f532 | 692 | |
falingtrea | 3:68e974f5f532 | 693 | while (true) { |
jamescmaki | 5:72d4d95e1d42 | 694 | if (pb2_low && (mDot09 == 0)) { |
jamescmaki | 5:72d4d95e1d42 | 695 | count++; |
jamescmaki | 5:72d4d95e1d42 | 696 | } else { |
jamescmaki | 5:72d4d95e1d42 | 697 | count = 0; |
jamescmaki | 5:72d4d95e1d42 | 698 | pb2_low = false; |
falingtrea | 3:68e974f5f532 | 699 | } |
falingtrea | 0:bdd16076aaa5 | 700 | |
jamescmaki | 5:72d4d95e1d42 | 701 | if (count == 5) { |
jamescmaki | 8:5b33eee8e2a5 | 702 | if (tx_interval > 60) { |
jamescmaki | 8:5b33eee8e2a5 | 703 | tx_interval = 60; |
jamescmaki | 8:5b33eee8e2a5 | 704 | } else if (tx_interval > 30) { |
jamescmaki | 8:5b33eee8e2a5 | 705 | tx_interval = 30; |
jamescmaki | 8:5b33eee8e2a5 | 706 | } else if (tx_interval > 10) { |
jamescmaki | 8:5b33eee8e2a5 | 707 | tx_interval = 10; |
jamescmaki | 7:c9d37522b975 | 708 | } else { |
jamescmaki | 8:5b33eee8e2a5 | 709 | tx_interval = 120; |
jamescmaki | 7:c9d37522b975 | 710 | } |
falingtrea | 3:68e974f5f532 | 711 | |
jamescmaki | 5:72d4d95e1d42 | 712 | thread_3->signal_set(0x10); // signal config_pkt_xmit to send packet |
jamescmaki | 5:72d4d95e1d42 | 713 | } |
jamescmaki | 5:72d4d95e1d42 | 714 | |
falingtrea | 3:68e974f5f532 | 715 | Thread::wait(5); |
jamescmaki | 5:72d4d95e1d42 | 716 | } |
falingtrea | 3:68e974f5f532 | 717 | } |
falingtrea | 0:bdd16076aaa5 | 718 | |
falingtrea | 2:75adc72aa6a0 | 719 | /* |
falingtrea | 2:75adc72aa6a0 | 720 | * Function that print clear text verion of mDot errors |
falingtrea | 2:75adc72aa6a0 | 721 | */ |
jamescmaki | 7:c9d37522b975 | 722 | void log_error(mDot *dot, const char *msg, int32_t retval) |
falingtrea | 0:bdd16076aaa5 | 723 | { |
falingtrea | 0:bdd16076aaa5 | 724 | printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str()); |
falingtrea | 0:bdd16076aaa5 | 725 | } |
falingtrea | 0:bdd16076aaa5 | 726 | |
falingtrea | 2:75adc72aa6a0 | 727 | /* |
falingtrea | 2:75adc72aa6a0 | 728 | * Thread that is triggered by SW2 ISR. Sends a packet to the LoRa server with the new Packet Transmission time setting |
falingtrea | 2:75adc72aa6a0 | 729 | */ |
jamescmaki | 7:c9d37522b975 | 730 | void config_pkt_xmit(void const *args) |
falingtrea | 0:bdd16076aaa5 | 731 | { |
jamescmaki | 4:71e411fbd9ff | 732 | std::vector<uint8_t> lora_pl; |
falingtrea | 0:bdd16076aaa5 | 733 | |
falingtrea | 0:bdd16076aaa5 | 734 | while (true) { |
jamescmaki | 5:72d4d95e1d42 | 735 | Thread::signal_wait(0x10); // wait for pb2ISR to signal send |
jamescmaki | 5:72d4d95e1d42 | 736 | |
jamescmaki | 4:71e411fbd9ff | 737 | lora_pl.clear(); |
jamescmaki | 7:c9d37522b975 | 738 | // Send packet transmission interval |
jamescmaki | 7:c9d37522b975 | 739 | lora_pl.push_back(15); |
jamescmaki | 4:71e411fbd9ff | 740 | lora_pl.push_back(1); |
jamescmaki | 7:c9d37522b975 | 741 | lora_pl.push_back(tx_interval); |
falingtrea | 0:bdd16076aaa5 | 742 | |
jamescmaki | 4:71e411fbd9ff | 743 | if ((mdot_ret = mdot_radio->send(lora_pl)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 744 | log_error(mdot_radio, "failed to send config data", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 745 | } else { |
falingtrea | 0:bdd16076aaa5 | 746 | printf("sent config data to gateway\r\n"); |
falingtrea | 0:bdd16076aaa5 | 747 | } |
falingtrea | 0:bdd16076aaa5 | 748 | } |
jamescmaki | 5:72d4d95e1d42 | 749 | } |