UDK + Shield

Dependencies:   DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B libmDot_1012-hotifx mbed-rtos mbed-src Senet_Packet X_NUCLEO_IKS01A1

Fork of MTDOT-EVBDemo_Senet by Dave Kjendal

Committer:
Shaun Nelson
Date:
Tue Aug 23 23:35:00 2016 -0400
Revision:
12:ad92bbb6312f
Parent:
10:658720d51610
Child:
14:07ff01da6bbf
Disabled UDK MEMS sensor shield free fall dection because the device appears to get interrupt bound

Who changed what in which revision?

UserRevisionLine numberNew 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
falingtrea 2:75adc72aa6a0 25 * information on setting up for public LoRa network. Moved SW setup to
falingtrea 2:75adc72aa6a0 26 * beginning of main. Removed printf call from ISR functions. Added
falingtrea 2:75adc72aa6a0 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 *
falingtrea 0:bdd16076aaa5 31 */
falingtrea 0:bdd16076aaa5 32
falingtrea 0:bdd16076aaa5 33 #include "mbed.h"
Shaun Nelson 10:658720d51610 34 #include "senet_packet.h"
Shaun Nelson 10:658720d51610 35
Shaun Nelson 10:658720d51610 36 // EVB Sensors
Shaun Nelson 10:658720d51610 37 #ifdef MTDOT_EVB
Shaun Nelson 12:ad92bbb6312f 38
falingtrea 0:bdd16076aaa5 39 #include "MMA845x.h"
falingtrea 0:bdd16076aaa5 40 #include "MPL3115A2.h"
falingtrea 0:bdd16076aaa5 41 #include "ISL29011.h"
falingtrea 0:bdd16076aaa5 42 #include "NCP5623B.h"
falingtrea 0:bdd16076aaa5 43 #include "DOGS102.h"
falingtrea 0:bdd16076aaa5 44 #include "font_6x8.h"
falingtrea 0:bdd16076aaa5 45 #include "MultiTech_Logo.h"
Shaun Nelson 12:ad92bbb6312f 46
Shaun Nelson 10:658720d51610 47 #elif defined(MTDOT_UDK)
Shaun Nelson 12:ad92bbb6312f 48
Shaun Nelson 10:658720d51610 49 #include "x_nucleo_iks01a1.h"
Shaun Nelson 12:ad92bbb6312f 50
Shaun Nelson 10:658720d51610 51 #endif
Shaun Nelson 10:658720d51610 52
falingtrea 0:bdd16076aaa5 53 #include "mDot.h"
falingtrea 0:bdd16076aaa5 54 #include "rtos.h"
falingtrea 0:bdd16076aaa5 55 #include <string>
falingtrea 0:bdd16076aaa5 56 #include <vector>
falingtrea 0:bdd16076aaa5 57
Shaun Nelson 12:ad92bbb6312f 58 /*
Shaun Nelson 12:ad92bbb6312f 59 * Board sensor data
falingtrea 0:bdd16076aaa5 60 */
Shaun Nelson 10:658720d51610 61 struct BoardSensorData
Shaun Nelson 10:658720d51610 62 {
Shaun Nelson 10:658720d51610 63 float temperature;
Shaun Nelson 10:658720d51610 64 float pressure;
Shaun Nelson 10:658720d51610 65 int32_t accel_x;
Shaun Nelson 10:658720d51610 66 int32_t accel_y;
Shaun Nelson 10:658720d51610 67 int32_t accel_z;
Shaun Nelson 10:658720d51610 68
Shaun Nelson 10:658720d51610 69 inline void init()
Shaun Nelson 10:658720d51610 70 {
Shaun Nelson 10:658720d51610 71 temperature= 0;
Shaun Nelson 10:658720d51610 72 pressure = 0;
Shaun Nelson 10:658720d51610 73 accel_x = 0;
Shaun Nelson 10:658720d51610 74 accel_y = 0;
Shaun Nelson 10:658720d51610 75 accel_z = 0;
Shaun Nelson 10:658720d51610 76 }
Shaun Nelson 10:658720d51610 77
Shaun Nelson 10:658720d51610 78 BoardSensorData() { init(); }
Shaun Nelson 10:658720d51610 79 };
Shaun Nelson 10:658720d51610 80
Shaun Nelson 12:ad92bbb6312f 81 #ifdef MDOT_EVB
falingtrea 0:bdd16076aaa5 82
Shaun Nelson 12:ad92bbb6312f 83 //DigitalIn mDot02(PA_2); // GPIO/UART_TX
Shaun Nelson 12:ad92bbb6312f 84 //DigitalOut mDot03(PA_3); // GPIO/UART_RX
Shaun Nelson 12:ad92bbb6312f 85 //DigitalIn mDot04(PA_6); // GPIO/SPI_MISO
Shaun Nelson 12:ad92bbb6312f 86 //DigitalIn mDot06(PA_8); // GPIO/I2C_SCL
Shaun Nelson 12:ad92bbb6312f 87 //DigitalIn mDot07(PC_9); // GPIO/I2C_SDA
falingtrea 0:bdd16076aaa5 88
Shaun Nelson 12:ad92bbb6312f 89 InterruptIn mDot08(PA_12); // GPIO/USB PB S1 on EVB
Shaun Nelson 12:ad92bbb6312f 90 InterruptIn mDot09(PA_11); // GPIO/USB PB S2 on EVB
Shaun Nelson 12:ad92bbb6312f 91
Shaun Nelson 12:ad92bbb6312f 92 //DigitalIn mDot11(PA_7); // GPIO/SPI_MOSI
falingtrea 0:bdd16076aaa5 93
Shaun Nelson 12:ad92bbb6312f 94 InterruptIn mDot12(PA_0); // GPIO/UART_CTS PRESSURE_INT2 on EVB
Shaun Nelson 12:ad92bbb6312f 95 DigitalOut mDot13(PC_13,1); // GPIO LCD_C/D
Shaun Nelson 12:ad92bbb6312f 96 InterruptIn mDot15(PC_1); // GPIO LIGHT_PROX_INT on EVB
Shaun Nelson 12:ad92bbb6312f 97 InterruptIn mDot16(PA_1); // GPIO/UART_RTS ACCEL_INT2 on EVB
Shaun Nelson 12:ad92bbb6312f 98 DigitalOut mDot17(PA_4,1); // GPIO/SPI_NCS LCD_CS on EVB
falingtrea 0:bdd16076aaa5 99
Shaun Nelson 12:ad92bbb6312f 100 //DigitalIn mDot18(PA_5); // GPIO/SPI_SCK
falingtrea 0:bdd16076aaa5 101
falingtrea 0:bdd16076aaa5 102 //DigitalInOut mDot19(PB_0,PIN_INPUT,PullNone,0); // GPIO PushPull LED Low=Red High=Green set MODE=INPUT to turn off
Shaun Nelson 12:ad92bbb6312f 103 AnalogIn mDot20(PB_1); // GPIO Current Sense Analog in on EVB
Shaun Nelson 12:ad92bbb6312f 104 Serial debugUART(PA_9, PA_10); // mDot debug UART
Shaun Nelson 12:ad92bbb6312f 105 //Serial mDotUART(PA_2, PA_3); // mDot external UART mDot02 and mDot03
Shaun Nelson 12:ad92bbb6312f 106 I2C mDoti2c(PC_9,PA_8); // mDot External I2C mDot6 and mDot7
falingtrea 0:bdd16076aaa5 107
Shaun Nelson 12:ad92bbb6312f 108 SPI mDotspi(PA_7,PA_6,PA_5); // mDot external SPI mDot11, mDot4, and mDot18
Shaun Nelson 10:658720d51610 109 #elif defined(MTDOT_UDK)
Shaun Nelson 10:658720d51610 110
Shaun Nelson 12:ad92bbb6312f 111 Serial debugUART(USBTX, USBRX); // mDot debug UART
Shaun Nelson 10:658720d51610 112
Shaun Nelson 10:658720d51610 113 #endif
falingtrea 0:bdd16076aaa5 114
Shaun Nelson 12:ad92bbb6312f 115 /*
Shaun Nelson 12:ad92bbb6312f 116 * LoRaWAN Configuration
falingtrea 2:75adc72aa6a0 117 */
Shaun Nelson 12:ad92bbb6312f 118 static uint8_t app_id[8] = {0x00,0x25,0x0C,0x00,0x00,0x01,0x00,0x01};
Shaun Nelson 12:ad92bbb6312f 119 static uint8_t app_key[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
falingtrea 0:bdd16076aaa5 120
Shaun Nelson 12:ad92bbb6312f 121 static std::vector<uint8_t> config_app_id(app_id,app_id+sizeof(app_id)/sizeof(uint8_t));
Shaun Nelson 12:ad92bbb6312f 122 static std::vector<uint8_t> config_app_key(app_key,app_key+sizeof(app_key)/sizeof(uint8_t));
Shaun Nelson 12:ad92bbb6312f 123 static uint8_t config_frequency_sub_band = 0;
Shaun Nelson 12:ad92bbb6312f 124 static bool config_adr_on = true;
falingtrea 0:bdd16076aaa5 125
Shaun Nelson 10:658720d51610 126 #ifdef MTDOT_EVB
falingtrea 0:bdd16076aaa5 127 MMA845x_DATA accel_data;
falingtrea 0:bdd16076aaa5 128 MPL3115A2_DATA baro_data;
falingtrea 0:bdd16076aaa5 129 uint16_t lux_data;
Shaun Nelson 12:ad92bbb6312f 130 MMA845x* evbAccel;
Shaun Nelson 12:ad92bbb6312f 131 MPL3115A2* evbBaro;
Shaun Nelson 12:ad92bbb6312f 132 ISL29011* evbAmbLight;
Shaun Nelson 12:ad92bbb6312f 133 NCP5623B* evbBackLight;
Shaun Nelson 12:ad92bbb6312f 134 DOGS102* evbLCD;
Shaun Nelson 10:658720d51610 135
Shaun Nelson 12:ad92bbb6312f 136 /*
Shaun Nelson 12:ad92bbb6312f 137 * EVB Application state
Shaun Nelson 12:ad92bbb6312f 138 */
Shaun Nelson 12:ad92bbb6312f 139 uint8_t position_value = 0xFF; // 00 unknown, 01 is flat, 02 is vertical
Shaun Nelson 12:ad92bbb6312f 140 uint8_t reflected_value = 0xFE;
Shaun Nelson 12:ad92bbb6312f 141 bool position_changed = true;
Shaun Nelson 10:658720d51610 142
Shaun Nelson 12:ad92bbb6312f 143 unsigned char test;
Shaun Nelson 12:ad92bbb6312f 144 char txtstr[17];
Shaun Nelson 12:ad92bbb6312f 145 int32_t num_whole;
Shaun Nelson 12:ad92bbb6312f 146 uint32_t pressure;
Shaun Nelson 12:ad92bbb6312f 147 int16_t num_frac;
Shaun Nelson 12:ad92bbb6312f 148 uint8_t result, pckt_time=100;
Shaun Nelson 12:ad92bbb6312f 149 char data;
falingtrea 3:68e974f5f532 150 // flags for pushbutton debounce code
falingtrea 3:68e974f5f532 151 bool pb1_low = false;
falingtrea 3:68e974f5f532 152 bool pb2_low = false;
falingtrea 3:68e974f5f532 153
falingtrea 0:bdd16076aaa5 154 void pb1ISR(void);
falingtrea 0:bdd16076aaa5 155 void pb2ISR(void);
falingtrea 3:68e974f5f532 156 void pb1_debounce(void const *args);
falingtrea 3:68e974f5f532 157 void pb2_debounce(void const *args);
falingtrea 3:68e974f5f532 158 Thread* thread_3;
falingtrea 0:bdd16076aaa5 159
falingtrea 0:bdd16076aaa5 160 void config_pkt_xmit (void const *args);
falingtrea 0:bdd16076aaa5 161
Shaun Nelson 12:ad92bbb6312f 162 #elif defined(MTDOT_UDK)
Shaun Nelson 12:ad92bbb6312f 163
Shaun Nelson 12:ad92bbb6312f 164 static X_NUCLEO_IKS01A1 *mems_shield;
Shaun Nelson 12:ad92bbb6312f 165
Shaun Nelson 12:ad92bbb6312f 166 #endif
Shaun Nelson 12:ad92bbb6312f 167
Shaun Nelson 12:ad92bbb6312f 168 mDot* mdot_radio;
Shaun Nelson 12:ad92bbb6312f 169 bool exit_program = false;
Shaun Nelson 12:ad92bbb6312f 170
Shaun Nelson 12:ad92bbb6312f 171 /*
Shaun Nelson 12:ad92bbb6312f 172 * Process downlink
Shaun Nelson 12:ad92bbb6312f 173 */
Shaun Nelson 12:ad92bbb6312f 174 static void ReceiveData(std::vector<uint8_t> frame);
Shaun Nelson 12:ad92bbb6312f 175
Shaun Nelson 12:ad92bbb6312f 176 static bool checkForExit(bool exit);
Shaun Nelson 12:ad92bbb6312f 177
Shaun Nelson 12:ad92bbb6312f 178 /*
Shaun Nelson 12:ad92bbb6312f 179 * prints of mDot error
Shaun Nelson 12:ad92bbb6312f 180 */
Shaun Nelson 12:ad92bbb6312f 181 void log_error(mDot* dot, const char* msg, int32_t retval)
Shaun Nelson 12:ad92bbb6312f 182 {
Shaun Nelson 12:ad92bbb6312f 183 printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str());
Shaun Nelson 12:ad92bbb6312f 184 }
Shaun Nelson 12:ad92bbb6312f 185
Shaun Nelson 12:ad92bbb6312f 186 /*
Shaun Nelson 12:ad92bbb6312f 187 * Send frame
Shaun Nelson 12:ad92bbb6312f 188 */
Shaun Nelson 10:658720d51610 189 void SendFrame(std::vector<uint8_t> frame)
falingtrea 0:bdd16076aaa5 190 {
Shaun Nelson 12:ad92bbb6312f 191 #if 0
Shaun Nelson 12:ad92bbb6312f 192 static uint32_t count = 0;
Shaun Nelson 12:ad92bbb6312f 193 printf("%lu: send data: ",count++);
Shaun Nelson 12:ad92bbb6312f 194 for(uint32_t i = 0;i < frame.size();i++)
Shaun Nelson 12:ad92bbb6312f 195 printf("%02X",frame[i]);
Shaun Nelson 12:ad92bbb6312f 196 printf("\r\n");
Shaun Nelson 12:ad92bbb6312f 197 #endif
Shaun Nelson 12:ad92bbb6312f 198
Shaun Nelson 12:ad92bbb6312f 199 int32_t mdot_ret;
Shaun Nelson 10:658720d51610 200 if ((mdot_ret = mdot_radio->send(frame)) != mDot::MDOT_OK) {
Shaun Nelson 10:658720d51610 201 log_error(mdot_radio, "failed to send", mdot_ret);
Shaun Nelson 10:658720d51610 202 }
Shaun Nelson 10:658720d51610 203 else {
Shaun Nelson 10:658720d51610 204 printf("successfully sent data to gateway\r\n");
Shaun Nelson 10:658720d51610 205 frame.clear();
Shaun Nelson 10:658720d51610 206 if ((mdot_ret = mdot_radio->recv(frame)) != mDot::MDOT_OK) {
Shaun Nelson 10:658720d51610 207 log_error(mdot_radio,"failed to recv:", mdot_ret);
Shaun Nelson 10:658720d51610 208 } else {
Shaun Nelson 12:ad92bbb6312f 209
Shaun Nelson 10:658720d51610 210 printf("recv data: ");
Shaun Nelson 10:658720d51610 211 for(uint32_t i = 0;i < frame.size();i++)
Shaun Nelson 10:658720d51610 212 printf("%02X",frame[i]);
Shaun Nelson 10:658720d51610 213 printf("\r\n");
Shaun Nelson 12:ad92bbb6312f 214
Shaun Nelson 12:ad92bbb6312f 215 ReceiveData(frame);
Shaun Nelson 10:658720d51610 216 }
Shaun Nelson 10:658720d51610 217 }
Shaun Nelson 10:658720d51610 218 }
falingtrea 0:bdd16076aaa5 219
Shaun Nelson 10:658720d51610 220 #ifdef MDOT_EVB
Shaun Nelson 12:ad92bbb6312f 221
Shaun Nelson 12:ad92bbb6312f 222 void ReceiveData(std::vector<uint8_t> frame)
Shaun Nelson 12:ad92bbb6312f 223 {
Shaun Nelson 12:ad92bbb6312f 224 reflected_value = frame[0];
Shaun Nelson 12:ad92bbb6312f 225
Shaun Nelson 12:ad92bbb6312f 226 if(reflected_value == position_value)
Shaun Nelson 12:ad92bbb6312f 227 {
Shaun Nelson 12:ad92bbb6312f 228 evbBackLight->setLEDCurrent(16);
Shaun Nelson 12:ad92bbb6312f 229 position_changed = false;
Shaun Nelson 12:ad92bbb6312f 230 }
Shaun Nelson 12:ad92bbb6312f 231 else
Shaun Nelson 12:ad92bbb6312f 232 {
Shaun Nelson 12:ad92bbb6312f 233 evbBackLight->setLEDCurrent(0);
Shaun Nelson 12:ad92bbb6312f 234 }
Shaun Nelson 12:ad92bbb6312f 235 }
Shaun Nelson 12:ad92bbb6312f 236
Shaun Nelson 10:658720d51610 237 void BoardInit()
Shaun Nelson 10:658720d51610 238 {
Shaun Nelson 10:658720d51610 239 static Thread thread_1(pb1_debounce); // threads for de-bouncing pushbutton switches
Shaun Nelson 10:658720d51610 240 static Thread thread_2(pb2_debounce);
falingtrea 0:bdd16076aaa5 241
dkjendal 4:9ea6ae34157c 242 debugUART.baud(115200);
Shaun Nelson 12:ad92bbb6312f 243 // mDotUART.baud(9600); // mdot UART unused but available on external connector
falingtrea 0:bdd16076aaa5 244
Shaun Nelson 12:ad92bbb6312f 245 thread_3 = new Thread(config_pkt_xmit); // start thread that sends LoRa packet when SW2 pressed
falingtrea 2:75adc72aa6a0 246
Shaun Nelson 12:ad92bbb6312f 247 evbAccel = new MMA845x(mDoti2c,MMA845x::SA0_VSS); // setup Accelerometer
Shaun Nelson 12:ad92bbb6312f 248 evbBaro = new MPL3115A2(mDoti2c); // setup Barometric sensor
Shaun Nelson 12:ad92bbb6312f 249 evbAmbLight = new ISL29011(mDoti2c); // Setup Ambient Light Sensor
Shaun Nelson 12:ad92bbb6312f 250 evbBackLight = new NCP5623B(mDoti2c); // setup backlight and LED 2 driver chip
Shaun Nelson 12:ad92bbb6312f 251 evbLCD = new DOGS102(mDotspi, mDot17, mDot13); // setup LCD
falingtrea 0:bdd16076aaa5 252
falingtrea 2:75adc72aa6a0 253 /*
falingtrea 2:75adc72aa6a0 254 * Setup SW1 as program stop function
falingtrea 2:75adc72aa6a0 255 */
falingtrea 2:75adc72aa6a0 256 mDot08.disable_irq();
falingtrea 2:75adc72aa6a0 257 mDot08.fall(&pb1ISR);
falingtrea 2:75adc72aa6a0 258
falingtrea 2:75adc72aa6a0 259 /*
falingtrea 2:75adc72aa6a0 260 * need to call this function after rise or fall because rise/fall sets
falingtrea 2:75adc72aa6a0 261 * mode to PullNone
falingtrea 2:75adc72aa6a0 262 */
falingtrea 2:75adc72aa6a0 263 mDot08.mode(PullUp);
falingtrea 2:75adc72aa6a0 264
falingtrea 2:75adc72aa6a0 265 mDot08.enable_irq();
falingtrea 2:75adc72aa6a0 266
falingtrea 2:75adc72aa6a0 267 /*
falingtrea 2:75adc72aa6a0 268 * Setup SW2 as packet time change
falingtrea 2:75adc72aa6a0 269 */
falingtrea 2:75adc72aa6a0 270 mDot09.disable_irq();
falingtrea 2:75adc72aa6a0 271 mDot09.fall(&pb2ISR);
falingtrea 2:75adc72aa6a0 272
falingtrea 2:75adc72aa6a0 273 /*
falingtrea 2:75adc72aa6a0 274 * need to call this function after rise or fall because rise/fall sets
falingtrea 2:75adc72aa6a0 275 * mode to PullNone
falingtrea 2:75adc72aa6a0 276 */
falingtrea 2:75adc72aa6a0 277 mDot09.mode(PullUp);
falingtrea 2:75adc72aa6a0 278
falingtrea 2:75adc72aa6a0 279 mDot09.enable_irq();
falingtrea 2:75adc72aa6a0 280
falingtrea 2:75adc72aa6a0 281 /*
falingtrea 2:75adc72aa6a0 282 * Setting other InterruptIn pins with Pull Ups
falingtrea 2:75adc72aa6a0 283 */
falingtrea 2:75adc72aa6a0 284 mDot12.mode(PullUp);
falingtrea 2:75adc72aa6a0 285 mDot15.mode(PullUp);
falingtrea 2:75adc72aa6a0 286 mDot16.mode(PullUp);
falingtrea 2:75adc72aa6a0 287
falingtrea 2:75adc72aa6a0 288 printf("font table address %p\n\r",&font_6x8);
falingtrea 2:75adc72aa6a0 289 printf("bitmap address %p\n\r",&MultiTech_Logo);
falingtrea 2:75adc72aa6a0 290
falingtrea 2:75adc72aa6a0 291 // Setup and display logo on LCD
falingtrea 2:75adc72aa6a0 292 evbLCD->startUpdate();
falingtrea 2:75adc72aa6a0 293
falingtrea 2:75adc72aa6a0 294 evbLCD->writeBitmap(0,0,MultiTech_Logo);
falingtrea 2:75adc72aa6a0 295
falingtrea 2:75adc72aa6a0 296 sprintf(txtstr,"MTDOT");
falingtrea 2:75adc72aa6a0 297 evbLCD->writeText(24,3,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 298 sprintf(txtstr,"Evaluation");
falingtrea 2:75adc72aa6a0 299 evbLCD->writeText(24,4,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 300 sprintf(txtstr,"Board");
falingtrea 2:75adc72aa6a0 301 evbLCD->writeText(24,5,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 302
falingtrea 2:75adc72aa6a0 303 evbLCD->endUpdate();
falingtrea 2:75adc72aa6a0 304
Shaun Nelson 10:658720d51610 305 pckt_time = 10;
Shaun Nelson 10:658720d51610 306 }
falingtrea 2:75adc72aa6a0 307
Shaun Nelson 10:658720d51610 308 void PostJoinInit()
Shaun Nelson 10:658720d51610 309 {
falingtrea 0:bdd16076aaa5 310 osDelay(200);
falingtrea 0:bdd16076aaa5 311 evbBackLight->setPWM(NCP5623B::LED_3,16); // enable LED2 on EVB and set to 50% PWM
falingtrea 0:bdd16076aaa5 312
falingtrea 2:75adc72aa6a0 313 // sets LED2 to 50% max current
falingtrea 0:bdd16076aaa5 314 evbBackLight->setLEDCurrent(16);
falingtrea 0:bdd16076aaa5 315
falingtrea 0:bdd16076aaa5 316 printf("Start of Test\n\r");
falingtrea 0:bdd16076aaa5 317
Shaun Nelson 12:ad92bbb6312f 318 osDelay (500); // allows other threads to process
falingtrea 2:75adc72aa6a0 319 printf("shutdown LED:\n\r");
falingtrea 2:75adc72aa6a0 320 evbBackLight->shutdown();
falingtrea 0:bdd16076aaa5 321
Shaun Nelson 12:ad92bbb6312f 322 osDelay (500); // allows other threads to process
falingtrea 0:bdd16076aaa5 323 printf("Turn on LED2\n\r");
falingtrea 0:bdd16076aaa5 324 evbBackLight->setLEDCurrent(16);
falingtrea 0:bdd16076aaa5 325
falingtrea 0:bdd16076aaa5 326 data = evbAccel->getWhoAmI();
falingtrea 0:bdd16076aaa5 327 printf("Accelerometer who_am_i value = %x \n\r", data);
falingtrea 0:bdd16076aaa5 328
falingtrea 0:bdd16076aaa5 329 result = evbAccel->getStatus();
falingtrea 0:bdd16076aaa5 330 printf("status byte = %x \n\r", result);
falingtrea 0:bdd16076aaa5 331
falingtrea 0:bdd16076aaa5 332 printf("Barometer who_am_i check = %s \n\r", evbBaro->testWhoAmI() ? "TRUE" : "FALSE");
falingtrea 0:bdd16076aaa5 333
falingtrea 0:bdd16076aaa5 334 result = evbBaro->getStatus();
falingtrea 0:bdd16076aaa5 335 printf("status byte = %x \n\r", result);
falingtrea 0:bdd16076aaa5 336
falingtrea 0:bdd16076aaa5 337 /*
falingtrea 0:bdd16076aaa5 338 * Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz
falingtrea 0:bdd16076aaa5 339 * normal oversample mode, High pass filter off
falingtrea 0:bdd16076aaa5 340 */
falingtrea 0:bdd16076aaa5 341 evbAccel->setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF,
falingtrea 0:bdd16076aaa5 342 MMA845x::DR_1_56,MMA845x::OS_NORMAL,MMA845x::HPF_OFF );
falingtrea 0:bdd16076aaa5 343
falingtrea 0:bdd16076aaa5 344 /*
falingtrea 0:bdd16076aaa5 345 * Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition.
falingtrea 0:bdd16076aaa5 346 * and a sample taken every second when in active mode
falingtrea 0:bdd16076aaa5 347 */
falingtrea 0:bdd16076aaa5 348 evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16,
falingtrea 0:bdd16076aaa5 349 MPL3115A2::AT_1);
falingtrea 0:bdd16076aaa5 350 /*
falingtrea 0:bdd16076aaa5 351 * Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution,
falingtrea 0:bdd16076aaa5 352 * and 16000 lux range
falingtrea 0:bdd16076aaa5 353 */
falingtrea 0:bdd16076aaa5 354
falingtrea 0:bdd16076aaa5 355 evbAmbLight->setMode(ISL29011::ALS_CONT);
falingtrea 0:bdd16076aaa5 356 evbAmbLight->setResolution(ISL29011::ADC_16BIT);
falingtrea 0:bdd16076aaa5 357 evbAmbLight->setRange(ISL29011::RNG_16000);
falingtrea 0:bdd16076aaa5 358
falingtrea 0:bdd16076aaa5 359 /*
falingtrea 0:bdd16076aaa5 360 * Set the accelerometer for active mode
falingtrea 0:bdd16076aaa5 361 */
falingtrea 0:bdd16076aaa5 362 evbAccel->activeMode();
falingtrea 0:bdd16076aaa5 363
falingtrea 0:bdd16076aaa5 364 /*
falingtrea 0:bdd16076aaa5 365 * Clear the min-max registers in the Barometric Sensor
falingtrea 0:bdd16076aaa5 366 */
falingtrea 0:bdd16076aaa5 367 evbBaro->clearMinMaxRegs();
falingtrea 0:bdd16076aaa5 368
falingtrea 0:bdd16076aaa5 369 evbBackLight->setLEDCurrent(0);
falingtrea 0:bdd16076aaa5 370
falingtrea 0:bdd16076aaa5 371 /*
falingtrea 2:75adc72aa6a0 372 * Check for PB1 press during network join attempt
falingtrea 2:75adc72aa6a0 373 */
falingtrea 2:75adc72aa6a0 374 if (exit_program) {
falingtrea 2:75adc72aa6a0 375 printf("Exiting program\n\r");
falingtrea 2:75adc72aa6a0 376 evbLCD->clearBuffer();
falingtrea 2:75adc72aa6a0 377 sprintf(txtstr,"Exiting Program");
falingtrea 2:75adc72aa6a0 378 evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 379 exit(1);
falingtrea 2:75adc72aa6a0 380 }
Shaun Nelson 10:658720d51610 381 }
Shaun Nelson 10:658720d51610 382
Shaun Nelson 10:658720d51610 383 void ReadSensors(BoardSensorData &sensorData)
Shaun Nelson 10:658720d51610 384 {
Shaun Nelson 10:658720d51610 385 MMA845x_DATA accel_data;
Shaun Nelson 10:658720d51610 386 /*
Shaun Nelson 10:658720d51610 387 * Test Accelerometer XYZ data ready bit to see if acquisition complete
Shaun Nelson 10:658720d51610 388 */
Shaun Nelson 10:658720d51610 389 do {
Shaun Nelson 12:ad92bbb6312f 390 osDelay(100); // allows other threads to process
Shaun Nelson 10:658720d51610 391 result = evbAccel->getStatus();
Shaun Nelson 10:658720d51610 392 } while ((result & MMA845x::XYZDR) == 0 );
Shaun Nelson 10:658720d51610 393
Shaun Nelson 10:658720d51610 394 /*
Shaun Nelson 10:658720d51610 395 * Retrieve and print out accelerometer data
Shaun Nelson 10:658720d51610 396 */
Shaun Nelson 10:658720d51610 397 accel_data = evbAccel->getXYZ();
Shaun Nelson 10:658720d51610 398
Shaun Nelson 10:658720d51610 399 sprintf(txtstr,"Accelerometer");
Shaun Nelson 10:658720d51610 400 evbLCD->writeText(0,0,font_6x8,txtstr,strlen(txtstr));
Shaun Nelson 10:658720d51610 401 sprintf(txtstr, "x = %d", accel_data._x);
Shaun Nelson 10:658720d51610 402 evbLCD->writeText(20,1,font_6x8,txtstr,strlen(txtstr));
Shaun Nelson 10:658720d51610 403 sprintf(txtstr, "y = %d", accel_data._y);
Shaun Nelson 10:658720d51610 404 evbLCD->writeText(20,2,font_6x8,txtstr,strlen(txtstr));
Shaun Nelson 10:658720d51610 405 sprintf(txtstr, "z = %d", accel_data._z );
Shaun Nelson 10:658720d51610 406 evbLCD->writeText(20,3,font_6x8,txtstr,strlen(txtstr));
Shaun Nelson 10:658720d51610 407
Shaun Nelson 10:658720d51610 408 sensorData->accel_x = accel_data._x;
Shaun Nelson 10:658720d51610 409 sensorData->accel_y = accel_data._y;
Shaun Nelson 10:658720d51610 410 sensorData->accel_z = accel_data._z;
Shaun Nelson 10:658720d51610 411
Shaun Nelson 10:658720d51610 412 // Update accelerometer state
Shaun Nelson 10:658720d51610 413 evbLCD->startUpdate();
Shaun Nelson 10:658720d51610 414 evbLCD->clearBuffer();
Shaun Nelson 10:658720d51610 415
Shaun Nelson 10:658720d51610 416 // convert to simple position value for use in send/recv
Shaun Nelson 10:658720d51610 417 if((accel_data._x > 500)&&(accel_data._z < 500))
Shaun Nelson 10:658720d51610 418 {
Shaun Nelson 10:658720d51610 419 if(position_value != 0x02)
Shaun Nelson 10:658720d51610 420 position_changed = true;
Shaun Nelson 10:658720d51610 421 position_value = 0x02;
Shaun Nelson 10:658720d51610 422 }
Shaun Nelson 10:658720d51610 423 else if((accel_data._x < 500)&&(accel_data._z > 500))
Shaun Nelson 10:658720d51610 424 {
Shaun Nelson 10:658720d51610 425 if(position_value != 0x01)
Shaun Nelson 10:658720d51610 426 position_changed = true;
Shaun Nelson 10:658720d51610 427 position_value = 0x01;
Shaun Nelson 10:658720d51610 428 }
Shaun Nelson 10:658720d51610 429 else
Shaun Nelson 10:658720d51610 430 {
Shaun Nelson 10:658720d51610 431 if(position_value != 0x00)
Shaun Nelson 10:658720d51610 432 position_changed = true;
Shaun Nelson 10:658720d51610 433 position_value= 0x00;
Shaun Nelson 10:658720d51610 434 }
Shaun Nelson 10:658720d51610 435
Shaun Nelson 10:658720d51610 436 if(changed)
Shaun Nelson 10:658720d51610 437 evbBackLight->setLEDCurrent(0);
Shaun Nelson 10:658720d51610 438 /*
Shaun Nelson 10:658720d51610 439 * Trigger a Pressure reading
Shaun Nelson 10:658720d51610 440 */
Shaun Nelson 10:658720d51610 441 evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16,
Shaun Nelson 10:658720d51610 442 MPL3115A2::AT_1);
Shaun Nelson 10:658720d51610 443 evbBaro->triggerOneShot();
Shaun Nelson 10:658720d51610 444
Shaun Nelson 10:658720d51610 445 /*
Shaun Nelson 10:658720d51610 446 * Test barometer device status to see if acquisition is complete
Shaun Nelson 10:658720d51610 447 */
Shaun Nelson 10:658720d51610 448 do {
Shaun Nelson 10:658720d51610 449 osDelay(100); // allows other threads to process
Shaun Nelson 10:658720d51610 450 result = evbBaro->getStatus();
Shaun Nelson 10:658720d51610 451 } while ((result & MPL3115A2::PTDR) == 0 );
Shaun Nelson 10:658720d51610 452
Shaun Nelson 10:658720d51610 453 /*
Shaun Nelson 10:658720d51610 454 * Retrieve and print out barometric pressure
Shaun Nelson 10:658720d51610 455 */
Shaun Nelson 10:658720d51610 456 pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value
Shaun Nelson 10:658720d51610 457 num_whole = pressure >> 2; // 18 bit integer significant
Shaun Nelson 10:658720d51610 458 num_frac = (pressure & 0x3) * 25; // 2 bit fractional 0.25 per bit
Shaun Nelson 10:658720d51610 459 sensorData.pressure = pressure + (.25 * num_frac);
Shaun Nelson 10:658720d51610 460
Shaun Nelson 10:658720d51610 461 sprintf(txtstr,"Press=%ld.%02d Pa", num_whole, num_frac);
Shaun Nelson 10:658720d51610 462 evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
Shaun Nelson 10:658720d51610 463
Shaun Nelson 10:658720d51610 464 /*
Shaun Nelson 10:658720d51610 465 * Trigger a Altitude reading
Shaun Nelson 10:658720d51610 466 */
Shaun Nelson 10:658720d51610 467 evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16,
Shaun Nelson 10:658720d51610 468 MPL3115A2::AT_1);
Shaun Nelson 10:658720d51610 469 evbBaro->triggerOneShot();
Shaun Nelson 10:658720d51610 470
Shaun Nelson 10:658720d51610 471 /*
Shaun Nelson 10:658720d51610 472 * Test barometer device status to see if acquisition is complete
Shaun Nelson 10:658720d51610 473 */
Shaun Nelson 10:658720d51610 474 do {
Shaun Nelson 10:658720d51610 475 osDelay(100); // allows other threads to process
Shaun Nelson 10:658720d51610 476 result = evbBaro->getStatus();
Shaun Nelson 10:658720d51610 477 } while ((result & MPL3115A2::PTDR) == 0 );
Shaun Nelson 10:658720d51610 478
Shaun Nelson 10:658720d51610 479 /*
Shaun Nelson 10:658720d51610 480 * Retrieve and print out altitude and temperature
Shaun Nelson 10:658720d51610 481 */
Shaun Nelson 10:658720d51610 482 baro_data = evbBaro->getAllData(false);
Shaun Nelson 10:658720d51610 483 baro_data._baro /= 4096; // convert 32 bit signed to 20 bit signed value
Shaun Nelson 10:658720d51610 484 num_whole = baro_data._baro / 16; // 18 bit signed significant integer
Shaun Nelson 10:658720d51610 485 num_frac = (baro_data._baro & 0xF) * 625 / 100; // 4 bit fractional .0625 per bit
Shaun Nelson 10:658720d51610 486 sprintf(txtstr,"Alti=%ld.%03d m", num_whole, num_frac);
Shaun Nelson 10:658720d51610 487 evbLCD->writeText(0,5,font_6x8,txtstr,strlen(txtstr));
Shaun Nelson 10:658720d51610 488 num_whole = baro_data._temp / 16; // 8 bit signed significant integer
Shaun Nelson 10:658720d51610 489 num_frac = (baro_data._temp & 0x0F) * 625 / 100; // 4 bit fractional .0625 per bit
Shaun Nelson 10:658720d51610 490 sensor.temperature = num_whole + ((float)num_frac / 100);
Shaun Nelson 10:658720d51610 491 sprintf(txtstr,"Temp=%ld.%03d C", num_whole, num_frac);
Shaun Nelson 10:658720d51610 492 evbLCD->writeText(0,6,font_6x8,txtstr,strlen(txtstr));
Shaun Nelson 10:658720d51610 493
Shaun Nelson 10:658720d51610 494 /*
Shaun Nelson 10:658720d51610 495 * retrieve and print out Ambient Light level
Shaun Nelson 10:658720d51610 496 */
Shaun Nelson 10:658720d51610 497 lux_data = evbAmbLight->getData();
Shaun Nelson 10:658720d51610 498 num_whole = lux_data * 24 / 100; // 16000 lux full scale .24 lux per bit
Shaun Nelson 10:658720d51610 499 num_frac = lux_data * 24 % 100;
Shaun Nelson 10:658720d51610 500 sprintf(txtstr, "Light=%ld.%02d lux", num_whole, num_frac );
Shaun Nelson 10:658720d51610 501 evbLCD->writeText(0,7,font_6x8,txtstr,strlen(txtstr));
Shaun Nelson 10:658720d51610 502
Shaun Nelson 10:658720d51610 503 evbLCD->endUpdate();
Shaun Nelson 10:658720d51610 504 printf("finished iteration %d\n\r",(++i));
Shaun Nelson 10:658720d51610 505 }
Shaun Nelson 10:658720d51610 506
Shaun Nelson 12:ad92bbb6312f 507 uint32_t PrepareFrame(std::vector<uint8_t> &frame, BoardSensorData &data)
Shaun Nelson 12:ad92bbb6312f 508 {
Shaun Nelson 12:ad92bbb6312f 509 if((reflected_value != position_value)|| position_changed)
Shaun Nelson 12:ad92bbb6312f 510 {
Shaun Nelson 12:ad92bbb6312f 511 frame.clear();
Shaun Nelson 12:ad92bbb6312f 512 // we will send a simple byte descriptor of the current position of the device: 01 is laying flat, 02 is vertically oriented
Shaun Nelson 12:ad92bbb6312f 513 frame.push_back(0x00);
Shaun Nelson 12:ad92bbb6312f 514 frame.push_back(position_value);
Shaun Nelson 12:ad92bbb6312f 515 return 2;
Shaun Nelson 12:ad92bbb6312f 516 }
Shaun Nelson 12:ad92bbb6312f 517
Shaun Nelson 12:ad92bbb6312f 518 return 0;
Shaun Nelson 12:ad92bbb6312f 519 }
Shaun Nelson 12:ad92bbb6312f 520
Shaun Nelson 12:ad92bbb6312f 521 bool checkForExit(bool _exit)
Shaun Nelson 12:ad92bbb6312f 522 {
Shaun Nelson 12:ad92bbb6312f 523 // Check for PB1 press during network join attempt
Shaun Nelson 12:ad92bbb6312f 524 if (exit_program) {
Shaun Nelson 12:ad92bbb6312f 525 printf("Exiting program\n\r");
Shaun Nelson 12:ad92bbb6312f 526
Shaun Nelson 12:ad92bbb6312f 527 evbLCD->clearBuffer();
Shaun Nelson 12:ad92bbb6312f 528 sprintf(txtstr,"Exiting Program");
Shaun Nelson 12:ad92bbb6312f 529 evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
Shaun Nelson 12:ad92bbb6312f 530 if(_exit)
Shaun Nelson 12:ad92bbb6312f 531 exit(1);
Shaun Nelson 12:ad92bbb6312f 532 }
Shaun Nelson 12:ad92bbb6312f 533
Shaun Nelson 12:ad92bbb6312f 534 return false;
Shaun Nelson 12:ad92bbb6312f 535 }
Shaun Nelson 12:ad92bbb6312f 536
Shaun Nelson 12:ad92bbb6312f 537 void ExitingProgram()
Shaun Nelson 10:658720d51610 538 {
Shaun Nelson 12:ad92bbb6312f 539 evbBaro->triggerOneShot();
Shaun Nelson 12:ad92bbb6312f 540 do {
Shaun Nelson 12:ad92bbb6312f 541 osDelay(200); // allows other threads to process
Shaun Nelson 12:ad92bbb6312f 542 result = evbBaro->getStatus();
Shaun Nelson 12:ad92bbb6312f 543 } while ((result & MPL3115A2::PTDR) == 0 );
Shaun Nelson 12:ad92bbb6312f 544
Shaun Nelson 12:ad92bbb6312f 545 baro_data = evbBaro->getAllData(true);
Shaun Nelson 12:ad92bbb6312f 546 printf ("minBaro=%ld maxBaro=%ld minTemp=%d maxTemp=%d\n\r", baro_data._minbaro, baro_data._maxbaro,
Shaun Nelson 12:ad92bbb6312f 547 baro_data._mintemp, baro_data._maxtemp);
Shaun Nelson 12:ad92bbb6312f 548 evbLCD->clearBuffer();
Shaun Nelson 12:ad92bbb6312f 549 sprintf(txtstr,"Exiting Program");
Shaun Nelson 12:ad92bbb6312f 550 evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
Shaun Nelson 12:ad92bbb6312f 551 printf("End of Test\n\r");
Shaun Nelson 12:ad92bbb6312f 552 }
Shaun Nelson 12:ad92bbb6312f 553
Shaun Nelson 10:658720d51610 554
Shaun Nelson 12:ad92bbb6312f 555 void wait()
Shaun Nelson 12:ad92bbb6312f 556 {
Shaun Nelson 12:ad92bbb6312f 557 osDelay(1000);
Shaun Nelson 12:ad92bbb6312f 558 }
Shaun Nelson 12:ad92bbb6312f 559
Shaun Nelson 12:ad92bbb6312f 560 /*
Shaun Nelson 12:ad92bbb6312f 561 * Sets pb1_low flag. Slag is cleared in pb1_debounce thread
Shaun Nelson 12:ad92bbb6312f 562 */
Shaun Nelson 12:ad92bbb6312f 563 void pb1ISR(void)
Shaun Nelson 12:ad92bbb6312f 564 {
Shaun Nelson 12:ad92bbb6312f 565 if (!pb1_low)
Shaun Nelson 12:ad92bbb6312f 566 pb1_low = true;
Shaun Nelson 12:ad92bbb6312f 567 }
Shaun Nelson 12:ad92bbb6312f 568
Shaun Nelson 12:ad92bbb6312f 569 /*
Shaun Nelson 12:ad92bbb6312f 570 * Debounces pb1. Also exits program if pushbutton 1 is pressed
Shaun Nelson 12:ad92bbb6312f 571 */
Shaun Nelson 12:ad92bbb6312f 572 void pb1_debounce(void const *args)
Shaun Nelson 12:ad92bbb6312f 573 {
Shaun Nelson 12:ad92bbb6312f 574 static uint8_t count = 0;
Shaun Nelson 10:658720d51610 575
Shaun Nelson 12:ad92bbb6312f 576 while (true) {
Shaun Nelson 12:ad92bbb6312f 577 if (pb1_low && (mDot08 == 0))
Shaun Nelson 12:ad92bbb6312f 578 count++;
Shaun Nelson 12:ad92bbb6312f 579 else {
Shaun Nelson 12:ad92bbb6312f 580 count = 0;
Shaun Nelson 12:ad92bbb6312f 581 pb1_low = false;
Shaun Nelson 12:ad92bbb6312f 582 }
Shaun Nelson 12:ad92bbb6312f 583
Shaun Nelson 12:ad92bbb6312f 584 if (count == 5)
Shaun Nelson 12:ad92bbb6312f 585 exit_program = true;
Shaun Nelson 12:ad92bbb6312f 586
Shaun Nelson 12:ad92bbb6312f 587 Thread::wait(5);
Shaun Nelson 12:ad92bbb6312f 588 }
Shaun Nelson 12:ad92bbb6312f 589 }
Shaun Nelson 12:ad92bbb6312f 590
Shaun Nelson 12:ad92bbb6312f 591 /*
Shaun Nelson 12:ad92bbb6312f 592 * Sets pb2_low flag. Flag is cleared in pb2_debounce thread
Shaun Nelson 12:ad92bbb6312f 593 */
Shaun Nelson 12:ad92bbb6312f 594 void pb2ISR(void)
Shaun Nelson 12:ad92bbb6312f 595 {
Shaun Nelson 12:ad92bbb6312f 596 if (!pb2_low)
Shaun Nelson 12:ad92bbb6312f 597 pb2_low = true;
Shaun Nelson 12:ad92bbb6312f 598 }
Shaun Nelson 12:ad92bbb6312f 599
Shaun Nelson 12:ad92bbb6312f 600 /*
Shaun Nelson 12:ad92bbb6312f 601 * Debounces pb2. Also changes packet transmit time to every other,
Shaun Nelson 12:ad92bbb6312f 602 * every fifth, or every tenth sample when SW2 pushed
Shaun Nelson 12:ad92bbb6312f 603 * Also triggers a thread to transmit a configuration packet
Shaun Nelson 12:ad92bbb6312f 604 */
Shaun Nelson 12:ad92bbb6312f 605 void pb2_debounce(void const *args)
Shaun Nelson 12:ad92bbb6312f 606 {
Shaun Nelson 12:ad92bbb6312f 607 static uint8_t count = 0;
Shaun Nelson 12:ad92bbb6312f 608
Shaun Nelson 12:ad92bbb6312f 609 while (true) {
Shaun Nelson 10:658720d51610 610
Shaun Nelson 12:ad92bbb6312f 611 if (pb2_low && (mDot09 == 0))
Shaun Nelson 12:ad92bbb6312f 612 count++;
Shaun Nelson 12:ad92bbb6312f 613 else {
Shaun Nelson 12:ad92bbb6312f 614 count = 0;
Shaun Nelson 12:ad92bbb6312f 615 pb2_low = false;
Shaun Nelson 12:ad92bbb6312f 616 }
Shaun Nelson 12:ad92bbb6312f 617
Shaun Nelson 12:ad92bbb6312f 618 if (count == 5){
Shaun Nelson 12:ad92bbb6312f 619 if (pckt_time >= 5)
Shaun Nelson 12:ad92bbb6312f 620 pckt_time /= 2;
Shaun Nelson 12:ad92bbb6312f 621 else
Shaun Nelson 12:ad92bbb6312f 622 pckt_time = 20;
Shaun Nelson 12:ad92bbb6312f 623
Shaun Nelson 12:ad92bbb6312f 624 //thread_3->signal_set(0x10); // signal config_pkt_xmit to send packet
Shaun Nelson 12:ad92bbb6312f 625 position_changed = true;
Shaun Nelson 12:ad92bbb6312f 626 }
Shaun Nelson 12:ad92bbb6312f 627 Thread::wait(5);
Shaun Nelson 12:ad92bbb6312f 628 }
Shaun Nelson 12:ad92bbb6312f 629 }
Shaun Nelson 12:ad92bbb6312f 630
Shaun Nelson 12:ad92bbb6312f 631 /*
Shaun Nelson 12:ad92bbb6312f 632 * Thread that is triggered by SW2 ISR. Sends a packet to the LoRa server with the new Packet Transmission time setting
Shaun Nelson 12:ad92bbb6312f 633 */
Shaun Nelson 12:ad92bbb6312f 634 void config_pkt_xmit (void const *args)
Shaun Nelson 12:ad92bbb6312f 635 {
Shaun Nelson 12:ad92bbb6312f 636 int32_t mdot_ret;
Shaun Nelson 12:ad92bbb6312f 637
Shaun Nelson 12:ad92bbb6312f 638 std::vector<uint8_t> data;
Shaun Nelson 12:ad92bbb6312f 639
Shaun Nelson 12:ad92bbb6312f 640 while (true) {
Shaun Nelson 12:ad92bbb6312f 641 Thread::signal_wait(0x10); // wait for pb2ISR to signal send
Shaun Nelson 12:ad92bbb6312f 642 data.clear();
Shaun Nelson 12:ad92bbb6312f 643 data.push_back(0x0F); // key for Configuration data (packet transmission timer)
Shaun Nelson 12:ad92bbb6312f 644 data.push_back(pckt_time);
Shaun Nelson 12:ad92bbb6312f 645
Shaun Nelson 12:ad92bbb6312f 646 if ((mdot_ret = mdot_radio->send(data)) != mDot::MDOT_OK) {
Shaun Nelson 12:ad92bbb6312f 647 log_error(mdot_radio, "failed to send config data", mdot_ret);
Shaun Nelson 12:ad92bbb6312f 648 } else {
Shaun Nelson 12:ad92bbb6312f 649 printf("sent config data to gateway\r\n");
Shaun Nelson 12:ad92bbb6312f 650 }
Shaun Nelson 10:658720d51610 651 }
Shaun Nelson 10:658720d51610 652 }
Shaun Nelson 10:658720d51610 653
Shaun Nelson 10:658720d51610 654 #elif defined(MTDOT_UDK)
Shaun Nelson 10:658720d51610 655
Shaun Nelson 12:ad92bbb6312f 656 void ReceiveData(std::vector<uint8_t> frame)
Shaun Nelson 12:ad92bbb6312f 657 {
Shaun Nelson 12:ad92bbb6312f 658 }
Shaun Nelson 12:ad92bbb6312f 659
Shaun Nelson 10:658720d51610 660 void BoardInit()
Shaun Nelson 10:658720d51610 661 {
Shaun Nelson 10:658720d51610 662 debugUART.baud(115200);
Shaun Nelson 10:658720d51610 663
Shaun Nelson 10:658720d51610 664 // ST X-NUCLEO-IKS01A1 MEMS Shield
Shaun Nelson 12:ad92bbb6312f 665 mems_shield = X_NUCLEO_IKS01A1::Instance(NULL, NC);
Shaun Nelson 12:ad92bbb6312f 666 // mems_shield = X_NUCLEO_IKS01A1::Instance();
Shaun Nelson 10:658720d51610 667 }
Shaun Nelson 10:658720d51610 668
Shaun Nelson 10:658720d51610 669 void PostJoinInit() { }
Shaun Nelson 10:658720d51610 670
Shaun Nelson 10:658720d51610 671
Shaun Nelson 10:658720d51610 672 void ReadSensors(BoardSensorData &data)
Shaun Nelson 10:658720d51610 673 {
Shaun Nelson 10:658720d51610 674 uint32_t ret = 0;
Shaun Nelson 10:658720d51610 675 int32_t accel_data[3];
Shaun Nelson 10:658720d51610 676
Shaun Nelson 10:658720d51610 677 // Temperature
Shaun Nelson 10:658720d51610 678 ret |= (!CALL_METH(mems_shield->pt_sensor, GetTemperature, &data.temperature, 0.0f) ? 0x0 : 0x1);
Shaun Nelson 10:658720d51610 679
Shaun Nelson 10:658720d51610 680 // Pressure
Shaun Nelson 10:658720d51610 681 ret |= (!CALL_METH(mems_shield->pt_sensor, GetPressure, &data.pressure, 0.0f) ? 0x0 : 0x1);
Shaun Nelson 10:658720d51610 682
Shaun Nelson 10:658720d51610 683 // Accelerometer
Shaun Nelson 10:658720d51610 684 MotionSensor *motionSensor = mems_shield->GetAccelerometer();
Shaun Nelson 10:658720d51610 685 if( motionSensor != NULL)
Shaun Nelson 10:658720d51610 686 {
Shaun Nelson 10:658720d51610 687 motionSensor->Get_X_Axes(accel_data);
Shaun Nelson 10:658720d51610 688
Shaun Nelson 10:658720d51610 689 data.accel_x = accel_data[0];
Shaun Nelson 10:658720d51610 690 data.accel_y = accel_data[1];
Shaun Nelson 10:658720d51610 691 data.accel_z = accel_data[2];
Shaun Nelson 10:658720d51610 692 }
Shaun Nelson 10:658720d51610 693
Shaun Nelson 10:658720d51610 694 printf("%s: Temperature=%f, Pressure=%f, x=%ld, y=%ld, z=%ld\r\n",__func__,
Shaun Nelson 10:658720d51610 695 data.temperature, data.pressure,
Shaun Nelson 10:658720d51610 696 data.accel_x, data.accel_y, data.accel_z);
Shaun Nelson 10:658720d51610 697 }
Shaun Nelson 10:658720d51610 698
Shaun Nelson 12:ad92bbb6312f 699 uint32_t PrepareFrame(std::vector<uint8_t> &frame, BoardSensorData &data)
Shaun Nelson 10:658720d51610 700 {
Shaun Nelson 12:ad92bbb6312f 701 static uint8_t buffer[256];
Shaun Nelson 10:658720d51610 702 uint16_t xyz_mask = 0;
Shaun Nelson 10:658720d51610 703
Shaun Nelson 10:658720d51610 704 // Sensor packet type serialized to the LMIC frame buffer
Shaun Nelson 10:658720d51610 705 SensorPacket packet(buffer, sizeof(frame));
Shaun Nelson 10:658720d51610 706
Shaun Nelson 10:658720d51610 707 packet.setPrimarySensor(xyz_mask);
Shaun Nelson 10:658720d51610 708 packet.setTemperature(data.temperature);
Shaun Nelson 10:658720d51610 709 packet.setPressure(data.pressure);
Shaun Nelson 10:658720d51610 710
Shaun Nelson 10:658720d51610 711 // Serialize packet to LMIC transmit buffer
Shaun Nelson 10:658720d51610 712 packet.serialize();
Shaun Nelson 10:658720d51610 713
Shaun Nelson 10:658720d51610 714 frame.assign(packet.payload(), packet.payload() + packet.length());
Shaun Nelson 10:658720d51610 715
Shaun Nelson 10:658720d51610 716 return frame.size();
Shaun Nelson 10:658720d51610 717 }
Shaun Nelson 10:658720d51610 718
Shaun Nelson 12:ad92bbb6312f 719 bool checkForExit(bool _exit) { return false;}
Shaun Nelson 12:ad92bbb6312f 720
Shaun Nelson 12:ad92bbb6312f 721
Shaun Nelson 12:ad92bbb6312f 722 void ExitingProgram()
Shaun Nelson 12:ad92bbb6312f 723 {
Shaun Nelson 12:ad92bbb6312f 724 printf("Exiting\n\r");
Shaun Nelson 12:ad92bbb6312f 725 }
Shaun Nelson 12:ad92bbb6312f 726
Shaun Nelson 12:ad92bbb6312f 727 void wait()
Shaun Nelson 12:ad92bbb6312f 728 {
Shaun Nelson 12:ad92bbb6312f 729 osDelay(5000);
Shaun Nelson 12:ad92bbb6312f 730 }
Shaun Nelson 10:658720d51610 731
Shaun Nelson 10:658720d51610 732
Shaun Nelson 10:658720d51610 733 #else
Shaun Nelson 10:658720d51610 734 #error Board type not defined!
Shaun Nelson 10:658720d51610 735 #endif
Shaun Nelson 10:658720d51610 736
Shaun Nelson 10:658720d51610 737 void mDotConfigureAndJoin()
Shaun Nelson 10:658720d51610 738 {
Shaun Nelson 12:ad92bbb6312f 739 bool ok;
Shaun Nelson 12:ad92bbb6312f 740 int32_t mdot_ret;
Shaun Nelson 12:ad92bbb6312f 741
Shaun Nelson 12:ad92bbb6312f 742 printf("Configuring mDot\r\n");
Shaun Nelson 10:658720d51610 743
Shaun Nelson 10:658720d51610 744 // get mDot handle
Shaun Nelson 10:658720d51610 745 mdot_radio = mDot::getInstance();
Shaun Nelson 10:658720d51610 746 if(mdot_radio == NULL)
Shaun Nelson 10:658720d51610 747 {
Shaun Nelson 10:658720d51610 748 while(1) {
Shaun Nelson 10:658720d51610 749 printf("radio setup failed\n\r");
Shaun Nelson 10:658720d51610 750 osDelay(1000);
Shaun Nelson 10:658720d51610 751 }
Shaun Nelson 10:658720d51610 752 }
Shaun Nelson 10:658720d51610 753
Shaun Nelson 10:658720d51610 754 do{
Shaun Nelson 10:658720d51610 755 ok = true;
Shaun Nelson 10:658720d51610 756
Shaun Nelson 10:658720d51610 757 printf("\n\r setup mdot\n\r");
Shaun Nelson 10:658720d51610 758
Shaun Nelson 10:658720d51610 759 // reset to default config so we know what state we're in
Shaun Nelson 10:658720d51610 760 mdot_radio->resetConfig();
Shaun Nelson 10:658720d51610 761 mdot_radio->setLogLevel(6);
Shaun Nelson 10:658720d51610 762
Shaun Nelson 10:658720d51610 763 mdot_radio->setAntennaGain(-3);
Shaun Nelson 10:658720d51610 764
Shaun Nelson 10:658720d51610 765 // Setting up LED1 as activity LED
Shaun Nelson 10:658720d51610 766 #ifdef MDOT_EVB
Shaun Nelson 10:658720d51610 767 mdot_radio->setActivityLedPin(PB_0);
Shaun Nelson 10:658720d51610 768 mdot_radio->setActivityLedEnable(true);
Shaun Nelson 10:658720d51610 769 #endif
Shaun Nelson 10:658720d51610 770
Shaun Nelson 10:658720d51610 771 // Read node ID
Shaun Nelson 10:658720d51610 772 std::vector<uint8_t> mdot_EUI;
Shaun Nelson 10:658720d51610 773 mdot_EUI = mdot_radio->getDeviceId();
Shaun Nelson 10:658720d51610 774 printf("mDot EUI = ");
Shaun Nelson 10:658720d51610 775
Shaun Nelson 10:658720d51610 776 for (uint8_t i=0; i<mdot_EUI.size(); i++) {
Shaun Nelson 10:658720d51610 777 printf("%02x ", mdot_EUI[i]);
Shaun Nelson 10:658720d51610 778 }
Shaun Nelson 12:ad92bbb6312f 779 printf("\n\r");
Shaun Nelson 12:ad92bbb6312f 780
Shaun Nelson 10:658720d51610 781
Shaun Nelson 12:ad92bbb6312f 782 /*
Shaun Nelson 12:ad92bbb6312f 783 * This call sets up private or public mode on the MTDOT. Set the function to true if
Shaun Nelson 12:ad92bbb6312f 784 * connecting to a public network
Shaun Nelson 12:ad92bbb6312f 785 */
Shaun Nelson 10:658720d51610 786 printf("setting Public Network Mode\r\n");
Shaun Nelson 10:658720d51610 787 if ((mdot_ret = mdot_radio->setPublicNetwork(true)) != mDot::MDOT_OK) {
Shaun Nelson 10:658720d51610 788 log_error(mdot_radio, "failed to set Public Network Mode", mdot_ret);
Shaun Nelson 10:658720d51610 789 }
Shaun Nelson 10:658720d51610 790 mdot_radio->setTxDataRate(mDot::DR0);
Shaun Nelson 10:658720d51610 791 mdot_radio->setTxPower(14);
Shaun Nelson 10:658720d51610 792 mdot_radio->setJoinRetries(1);
Shaun Nelson 12:ad92bbb6312f 793 mdot_radio->setJoinMode(mDot::OTA);
Shaun Nelson 10:658720d51610 794
Shaun Nelson 12:ad92bbb6312f 795 /*
Shaun Nelson 12:ad92bbb6312f 796 * Frequency sub-band is valid for NAM only and for Private networks should be set to a value
Shaun Nelson 12:ad92bbb6312f 797 * between 1-8 that matches the the LoRa gateway setting. Public networks use sub-band 0 only.
Shaun Nelson 12:ad92bbb6312f 798 * This function can be commented out for EU networks
Shaun Nelson 12:ad92bbb6312f 799 */
Shaun Nelson 10:658720d51610 800 printf("setting frequency sub band\r\n");
Shaun Nelson 12:ad92bbb6312f 801 if ((mdot_ret = mdot_radio->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
Shaun Nelson 10:658720d51610 802 log_error(mdot_radio, "failed to set frequency sub band", mdot_ret);
Shaun Nelson 10:658720d51610 803 ok = false;
Shaun Nelson 10:658720d51610 804 }
Shaun Nelson 10:658720d51610 805
Shaun Nelson 10:658720d51610 806 printf("setting ADR\r\n");
Shaun Nelson 10:658720d51610 807 if ((mdot_ret = mdot_radio->setAdr(config_adr_on)) != mDot::MDOT_OK) {
Shaun Nelson 10:658720d51610 808 log_error(mdot_radio, "failed to set ADR", mdot_ret);
Shaun Nelson 10:658720d51610 809 ok = false;
Shaun Nelson 10:658720d51610 810 }
Shaun Nelson 12:ad92bbb6312f 811
Shaun Nelson 12:ad92bbb6312f 812 /*
Shaun Nelson 12:ad92bbb6312f 813 * setNetworkName is used for private networks.
Shaun Nelson 12:ad92bbb6312f 814 * Use setNetworkID(AppID) for public networks
Shaun Nelson 12:ad92bbb6312f 815 */
Shaun Nelson 10:658720d51610 816 printf("setting network name\r\n");
Shaun Nelson 10:658720d51610 817 if ((mdot_ret = mdot_radio->setNetworkId(config_app_id)) != mDot::MDOT_OK) {
Shaun Nelson 10:658720d51610 818 log_error(mdot_radio, "failed to set network name", mdot_ret);
Shaun Nelson 10:658720d51610 819 ok = false;
Shaun Nelson 10:658720d51610 820 }
Shaun Nelson 10:658720d51610 821
Shaun Nelson 12:ad92bbb6312f 822 /*
Shaun Nelson 12:ad92bbb6312f 823 * setNetworkPassphrase is used for private networks
Shaun Nelson 12:ad92bbb6312f 824 * Use setNetworkKey for public networks
Shaun Nelson 12:ad92bbb6312f 825 */
Shaun Nelson 12:ad92bbb6312f 826 printf("setting network key\r\n");
Shaun Nelson 10:658720d51610 827 if ((mdot_ret = mdot_radio->setNetworkKey(config_app_key)) != mDot::MDOT_OK) {
Shaun Nelson 10:658720d51610 828 log_error(mdot_radio, "failed to set network password", mdot_ret);
Shaun Nelson 10:658720d51610 829 ok = false;
Shaun Nelson 10:658720d51610 830 }
Shaun Nelson 12:ad92bbb6312f 831
Shaun Nelson 12:ad92bbb6312f 832 checkForExit(true);
Shaun Nelson 12:ad92bbb6312f 833
Shaun Nelson 10:658720d51610 834 }while(ok == false);
Shaun Nelson 12:ad92bbb6312f 835
Shaun Nelson 10:658720d51610 836 // attempt to join the network
Shaun Nelson 10:658720d51610 837 printf("joining network\r\n");
Shaun Nelson 12:ad92bbb6312f 838 while ((mdot_ret = mdot_radio->joinNetwork()) != mDot::MDOT_OK) {
Shaun Nelson 10:658720d51610 839 log_error(mdot_radio,"failed to join network:", mdot_ret);
Shaun Nelson 10:658720d51610 840 if (mdot_radio->getFrequencyBand() == mDot::FB_868){
Shaun Nelson 10:658720d51610 841 mdot_ret = mdot_radio->getNextTxMs();
Shaun Nelson 10:658720d51610 842 }
Shaun Nelson 10:658720d51610 843 else {
Shaun Nelson 10:658720d51610 844 mdot_ret = 0;
Shaun Nelson 10:658720d51610 845 }
Shaun Nelson 10:658720d51610 846
Shaun Nelson 12:ad92bbb6312f 847 checkForExit(true);
Shaun Nelson 12:ad92bbb6312f 848
Shaun Nelson 10:658720d51610 849 printf("delay = %lu\n\r",mdot_ret);
Shaun Nelson 10:658720d51610 850 osDelay(mdot_ret + 10000);
Shaun Nelson 10:658720d51610 851 }
Shaun Nelson 10:658720d51610 852 }
Shaun Nelson 10:658720d51610 853
Shaun Nelson 12:ad92bbb6312f 854
Shaun Nelson 10:658720d51610 855 int main()
Shaun Nelson 10:658720d51610 856 {
Shaun Nelson 10:658720d51610 857 BoardSensorData sensorData;
Shaun Nelson 12:ad92bbb6312f 858 std::vector<uint8_t> frame;
Shaun Nelson 10:658720d51610 859
Shaun Nelson 10:658720d51610 860 // Board specific initialization
Shaun Nelson 10:658720d51610 861 BoardInit();
Shaun Nelson 10:658720d51610 862
Shaun Nelson 12:ad92bbb6312f 863 // Configure mDot and join
Shaun Nelson 12:ad92bbb6312f 864 mDotConfigureAndJoin();
Shaun Nelson 10:658720d51610 865
Shaun Nelson 12:ad92bbb6312f 866 // Do board specific post join configuration
Shaun Nelson 12:ad92bbb6312f 867 PostJoinInit();
falingtrea 2:75adc72aa6a0 868
falingtrea 2:75adc72aa6a0 869 /*
falingtrea 0:bdd16076aaa5 870 * Main data acquisition loop
falingtrea 0:bdd16076aaa5 871 */
falingtrea 0:bdd16076aaa5 872 do {
Shaun Nelson 10:658720d51610 873 // Acquire sensor values
Shaun Nelson 10:658720d51610 874 ReadSensors(sensorData);
Shaun Nelson 12:ad92bbb6312f 875
Shaun Nelson 10:658720d51610 876 // Send sensor packets
Shaun Nelson 10:658720d51610 877 if( PrepareFrame(frame, sensorData) > 0 ){
Shaun Nelson 10:658720d51610 878 SendFrame( frame );
Shaun Nelson 10:658720d51610 879 }
Shaun Nelson 12:ad92bbb6312f 880 wait();
falingtrea 0:bdd16076aaa5 881
Shaun Nelson 12:ad92bbb6312f 882 } while(!checkForExit(false));
falingtrea 3:68e974f5f532 883
Shaun Nelson 12:ad92bbb6312f 884 ExitingProgram();
falingtrea 3:68e974f5f532 885 }