Ag demo with soil moisture

Dependencies:   DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B libmDot mbed-rtos mbed-src

Fork of MTDOT-EVBDemo-DRH by Dave Heitzman

Committer:
daveheitzman
Date:
Thu Aug 20 21:28:45 2015 +0000
Revision:
5:5a794cedd728
Parent:
4:51d7c24bbf91
Child:
6:b62e5106b5bc
runs and works, sends string of csv for data points

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"
falingtrea 0:bdd16076aaa5 34 #include "MMA845x.h"
falingtrea 0:bdd16076aaa5 35 #include "MPL3115A2.h"
falingtrea 0:bdd16076aaa5 36 #include "ISL29011.h"
falingtrea 0:bdd16076aaa5 37 #include "NCP5623B.h"
falingtrea 0:bdd16076aaa5 38 #include "DOGS102.h"
falingtrea 0:bdd16076aaa5 39 #include "font_6x8.h"
falingtrea 0:bdd16076aaa5 40 #include "MultiTech_Logo.h"
falingtrea 0:bdd16076aaa5 41 #include "mDot.h"
falingtrea 0:bdd16076aaa5 42 #include "rtos.h"
falingtrea 0:bdd16076aaa5 43 #include <string>
falingtrea 0:bdd16076aaa5 44 #include <vector>
falingtrea 0:bdd16076aaa5 45
falingtrea 0:bdd16076aaa5 46 enum LED1_COLOR {
falingtrea 0:bdd16076aaa5 47 RED = 0,
falingtrea 0:bdd16076aaa5 48 GREEN = 1
falingtrea 0:bdd16076aaa5 49 };
falingtrea 0:bdd16076aaa5 50
falingtrea 0:bdd16076aaa5 51 /*
falingtrea 0:bdd16076aaa5 52 * union for converting from 32-bit to 4 8-bit values
falingtrea 0:bdd16076aaa5 53 */
falingtrea 0:bdd16076aaa5 54 union convert32 {
falingtrea 0:bdd16076aaa5 55 int32_t f_s; // convert from signed 32 bit int
falingtrea 0:bdd16076aaa5 56 uint32_t f_u; // convert from unsigned 32 bit int
falingtrea 0:bdd16076aaa5 57 uint8_t t_u[4]; // convert to 8 bit unsigned array
falingtrea 0:bdd16076aaa5 58 };
falingtrea 0:bdd16076aaa5 59
falingtrea 0:bdd16076aaa5 60 /*
falingtrea 0:bdd16076aaa5 61 * union for converting from 16- bit to 2 8-bit values
falingtrea 0:bdd16076aaa5 62 */
falingtrea 0:bdd16076aaa5 63 union convert16 {
falingtrea 0:bdd16076aaa5 64 int16_t f_s; // convert from signed 16 bit int
falingtrea 0:bdd16076aaa5 65 uint16_t f_u; // convert from unsigned 16 bit int
falingtrea 0:bdd16076aaa5 66 uint8_t t_u[2]; // convert to 8 bit unsigned array
falingtrea 0:bdd16076aaa5 67 };
falingtrea 0:bdd16076aaa5 68
falingtrea 0:bdd16076aaa5 69 //DigitalIn mDot02(PA_2); // GPIO/UART_TX
falingtrea 0:bdd16076aaa5 70 //DigitalOut mDot03(PA_3); // GPIO/UART_RX
falingtrea 0:bdd16076aaa5 71 //DigitalIn mDot04(PA_6); // GPIO/SPI_MISO
falingtrea 0:bdd16076aaa5 72 //DigitalIn mDot06(PA_8); // GPIO/I2C_SCL
falingtrea 0:bdd16076aaa5 73 //DigitalIn mDot07(PC_9); // GPIO/I2C_SDA
falingtrea 0:bdd16076aaa5 74
falingtrea 0:bdd16076aaa5 75 InterruptIn mDot08(PA_12); // GPIO/USB PB S1 on EVB
falingtrea 0:bdd16076aaa5 76 InterruptIn mDot09(PA_11); // GPIO/USB PB S2 on EVB
falingtrea 0:bdd16076aaa5 77
falingtrea 0:bdd16076aaa5 78 //DigitalIn mDot11(PA_7); // GPIO/SPI_MOSI
falingtrea 0:bdd16076aaa5 79
falingtrea 0:bdd16076aaa5 80 InterruptIn mDot12(PA_0); // GPIO/UART_CTS PRESSURE_INT2 on EVB
falingtrea 0:bdd16076aaa5 81 DigitalOut mDot13(PC_13,1); // GPIO LCD_C/D
falingtrea 0:bdd16076aaa5 82 InterruptIn mDot15(PC_1); // GPIO LIGHT_PROX_INT on EVB
falingtrea 0:bdd16076aaa5 83 InterruptIn mDot16(PA_1); // GPIO/UART_RTS ACCEL_INT2 on EVB
falingtrea 0:bdd16076aaa5 84 DigitalOut mDot17(PA_4,1); // GPIO/SPI_NCS LCD_CS on EVB
falingtrea 0:bdd16076aaa5 85
falingtrea 0:bdd16076aaa5 86 //DigitalIn mDot18(PA_5); // GPIO/SPI_SCK
falingtrea 0:bdd16076aaa5 87
falingtrea 0:bdd16076aaa5 88 //DigitalInOut mDot19(PB_0,PIN_INPUT,PullNone,0); // GPIO PushPull LED Low=Red High=Green set MODE=INPUT to turn off
falingtrea 0:bdd16076aaa5 89 AnalogIn mDot20(PB_1); // GPIO Current Sense Analog in on EVB
falingtrea 0:bdd16076aaa5 90
falingtrea 0:bdd16076aaa5 91 Serial debugUART(PA_9, PA_10); // mDot debug UART
falingtrea 0:bdd16076aaa5 92
falingtrea 0:bdd16076aaa5 93 //Serial mDotUART(PA_2, PA_3); // mDot external UART mDot02 and mDot03
falingtrea 0:bdd16076aaa5 94
falingtrea 0:bdd16076aaa5 95 I2C mDoti2c(PC_9,PA_8); // mDot External I2C mDot6 and mDot7
falingtrea 0:bdd16076aaa5 96
falingtrea 0:bdd16076aaa5 97 SPI mDotspi(PA_7,PA_6,PA_5); // mDot external SPI mDot11, mDot4, and mDot18
falingtrea 0:bdd16076aaa5 98
falingtrea 2:75adc72aa6a0 99 /* **** replace these values with the proper public or private network settings ****
falingtrea 2:75adc72aa6a0 100 * config_network_nameand config_network_pass are for private networks.
falingtrea 2:75adc72aa6a0 101 */
daveheitzman 4:51d7c24bbf91 102 static std::string config_network_name = "Escalation";
daveheitzman 4:51d7c24bbf91 103 static std::string config_network_pass = "Escalation";
daveheitzman 4:51d7c24bbf91 104 static uint8_t config_frequency_sub_band = 3;
falingtrea 0:bdd16076aaa5 105
falingtrea 2:75adc72aa6a0 106 /* config_app_id and config_app_key are for public networks.
falingtrea 2:75adc72aa6a0 107 static uint8_t app_id[8] = {0x00,0x01,0x02,0x03,0x0A,0x0B,0x0C,0x0D};
falingtrea 2:75adc72aa6a0 108 std::vector<uint8_t> config_app_id;
falingtrea 2:75adc72aa6a0 109 static uint8_t app_key[16] = {0x00,0x01,0x02,0x03,0x0A,0x0B,0x0C,0x0D};
falingtrea 2:75adc72aa6a0 110 std::vector<uint8_t> config_app_key;
falingtrea 2:75adc72aa6a0 111 */
falingtrea 2:75adc72aa6a0 112
falingtrea 0:bdd16076aaa5 113 uint8_t result, pckt_time=10;
falingtrea 0:bdd16076aaa5 114 char data;
falingtrea 0:bdd16076aaa5 115 unsigned char test;
falingtrea 0:bdd16076aaa5 116 char txtstr[17];
falingtrea 0:bdd16076aaa5 117 int32_t num_whole, mdot_ret;
falingtrea 0:bdd16076aaa5 118 uint32_t pressure;
falingtrea 0:bdd16076aaa5 119 int16_t num_frac;
falingtrea 0:bdd16076aaa5 120
falingtrea 0:bdd16076aaa5 121 bool exit_program = false;
falingtrea 0:bdd16076aaa5 122
falingtrea 0:bdd16076aaa5 123 MMA845x_DATA accel_data;
falingtrea 0:bdd16076aaa5 124 MPL3115A2_DATA baro_data;
falingtrea 0:bdd16076aaa5 125 uint16_t lux_data;
falingtrea 0:bdd16076aaa5 126 MMA845x* evbAccel;
falingtrea 0:bdd16076aaa5 127 MPL3115A2* evbBaro;
falingtrea 0:bdd16076aaa5 128 ISL29011* evbAmbLight;
falingtrea 0:bdd16076aaa5 129 NCP5623B* evbBackLight;
falingtrea 0:bdd16076aaa5 130 DOGS102* evbLCD;
falingtrea 0:bdd16076aaa5 131 mDot* mdot_radio;
falingtrea 0:bdd16076aaa5 132
falingtrea 0:bdd16076aaa5 133 convert32 convertl;
falingtrea 0:bdd16076aaa5 134 convert16 converts;
falingtrea 0:bdd16076aaa5 135
falingtrea 3:68e974f5f532 136 // flags for pushbutton debounce code
falingtrea 3:68e974f5f532 137 bool pb1_low = false;
falingtrea 3:68e974f5f532 138 bool pb2_low = false;
falingtrea 3:68e974f5f532 139
falingtrea 0:bdd16076aaa5 140 void pb1ISR(void);
falingtrea 0:bdd16076aaa5 141 void pb2ISR(void);
falingtrea 3:68e974f5f532 142 void pb1_debounce(void const *args);
falingtrea 3:68e974f5f532 143 void pb2_debounce(void const *args);
falingtrea 3:68e974f5f532 144 Thread* thread_3;
falingtrea 0:bdd16076aaa5 145
falingtrea 0:bdd16076aaa5 146 void log_error(mDot* dot, const char* msg, int32_t retval);
falingtrea 0:bdd16076aaa5 147
falingtrea 0:bdd16076aaa5 148 void config_pkt_xmit (void const *args);
daveheitzman 5:5a794cedd728 149 int32_t sendString(const std::string text);
falingtrea 0:bdd16076aaa5 150
falingtrea 0:bdd16076aaa5 151 int main()
falingtrea 0:bdd16076aaa5 152 {
falingtrea 0:bdd16076aaa5 153
falingtrea 0:bdd16076aaa5 154 std::vector<uint8_t> mdot_data;
falingtrea 0:bdd16076aaa5 155 std::vector<uint8_t> mdot_EUI;
falingtrea 0:bdd16076aaa5 156 uint16_t i = 0;
falingtrea 0:bdd16076aaa5 157
falingtrea 0:bdd16076aaa5 158 debugUART.baud(921600);
falingtrea 0:bdd16076aaa5 159 // mDotUART.baud(9600); // mdot UART unused but available on external connector
falingtrea 0:bdd16076aaa5 160
falingtrea 3:68e974f5f532 161 Thread thread_1(pb1_debounce); // threads for de-bouncing pushbutton switches
falingtrea 3:68e974f5f532 162 Thread thread_2(pb2_debounce);
falingtrea 3:68e974f5f532 163
falingtrea 3:68e974f5f532 164 thread_3 = new Thread(config_pkt_xmit); // start thread that sends LoRa packet when SW2 pressed
falingtrea 2:75adc72aa6a0 165
falingtrea 1:ac9595d0f0e7 166 evbAccel = new MMA845x(mDoti2c,MMA845x::SA0_VSS); // setup Accelerometer
falingtrea 0:bdd16076aaa5 167 evbBaro = new MPL3115A2(mDoti2c); // setup Barometric sensor
falingtrea 1:ac9595d0f0e7 168 evbAmbLight = new ISL29011(mDoti2c); // Setup Ambient Light Sensor
falingtrea 0:bdd16076aaa5 169 evbBackLight = new NCP5623B(mDoti2c); // setup backlight and LED 2 driver chip
falingtrea 1:ac9595d0f0e7 170 evbLCD = new DOGS102(mDotspi, mDot17, mDot13); // setup LCD
falingtrea 0:bdd16076aaa5 171
falingtrea 2:75adc72aa6a0 172 /*
falingtrea 2:75adc72aa6a0 173 * Setup SW1 as program stop function
falingtrea 2:75adc72aa6a0 174 */
falingtrea 2:75adc72aa6a0 175 mDot08.disable_irq();
falingtrea 2:75adc72aa6a0 176 mDot08.fall(&pb1ISR);
falingtrea 2:75adc72aa6a0 177
falingtrea 2:75adc72aa6a0 178 /*
falingtrea 2:75adc72aa6a0 179 * need to call this function after rise or fall because rise/fall sets
falingtrea 2:75adc72aa6a0 180 * mode to PullNone
falingtrea 2:75adc72aa6a0 181 */
falingtrea 2:75adc72aa6a0 182 mDot08.mode(PullUp);
falingtrea 2:75adc72aa6a0 183
falingtrea 2:75adc72aa6a0 184 mDot08.enable_irq();
falingtrea 2:75adc72aa6a0 185
falingtrea 2:75adc72aa6a0 186 /*
falingtrea 2:75adc72aa6a0 187 * Setup SW2 as packet time change
falingtrea 2:75adc72aa6a0 188 */
falingtrea 2:75adc72aa6a0 189 mDot09.disable_irq();
falingtrea 2:75adc72aa6a0 190 mDot09.fall(&pb2ISR);
falingtrea 2:75adc72aa6a0 191
falingtrea 2:75adc72aa6a0 192 /*
falingtrea 2:75adc72aa6a0 193 * need to call this function after rise or fall because rise/fall sets
falingtrea 2:75adc72aa6a0 194 * mode to PullNone
falingtrea 2:75adc72aa6a0 195 */
falingtrea 2:75adc72aa6a0 196 mDot09.mode(PullUp);
falingtrea 2:75adc72aa6a0 197
falingtrea 2:75adc72aa6a0 198 mDot09.enable_irq();
falingtrea 2:75adc72aa6a0 199
falingtrea 2:75adc72aa6a0 200 /*
falingtrea 2:75adc72aa6a0 201 * Setting other InterruptIn pins with Pull Ups
falingtrea 2:75adc72aa6a0 202 */
falingtrea 2:75adc72aa6a0 203 mDot12.mode(PullUp);
falingtrea 2:75adc72aa6a0 204 mDot15.mode(PullUp);
falingtrea 2:75adc72aa6a0 205 mDot16.mode(PullUp);
falingtrea 2:75adc72aa6a0 206
falingtrea 2:75adc72aa6a0 207 printf("font table address %p\n\r",&font_6x8);
falingtrea 2:75adc72aa6a0 208 printf("bitmap address %p\n\r",&MultiTech_Logo);
falingtrea 2:75adc72aa6a0 209
falingtrea 2:75adc72aa6a0 210 // Setup and display logo on LCD
falingtrea 2:75adc72aa6a0 211 evbLCD->startUpdate();
falingtrea 2:75adc72aa6a0 212
falingtrea 2:75adc72aa6a0 213 evbLCD->writeBitmap(0,0,MultiTech_Logo);
falingtrea 2:75adc72aa6a0 214
falingtrea 2:75adc72aa6a0 215 sprintf(txtstr,"MTDOT");
falingtrea 2:75adc72aa6a0 216 evbLCD->writeText(24,3,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 217 sprintf(txtstr,"Evaluation");
falingtrea 2:75adc72aa6a0 218 evbLCD->writeText(24,4,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 219 sprintf(txtstr,"Board");
falingtrea 2:75adc72aa6a0 220 evbLCD->writeText(24,5,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 221
falingtrea 2:75adc72aa6a0 222 evbLCD->endUpdate();
falingtrea 2:75adc72aa6a0 223
falingtrea 2:75adc72aa6a0 224 printf("\n\r setup mdot\n\r");
falingtrea 0:bdd16076aaa5 225
falingtrea 0:bdd16076aaa5 226 // get a mDot handle
falingtrea 0:bdd16076aaa5 227 mdot_radio = mDot::getInstance();
falingtrea 0:bdd16076aaa5 228
falingtrea 0:bdd16076aaa5 229 if (mdot_radio) {
falingtrea 0:bdd16076aaa5 230 // reset to default config so we know what state we're in
falingtrea 0:bdd16076aaa5 231 mdot_radio->resetConfig();
falingtrea 0:bdd16076aaa5 232
falingtrea 0:bdd16076aaa5 233 // Setting up LED1 as activity LED
falingtrea 0:bdd16076aaa5 234 mdot_radio->setActivityLedPin(PB_0);
falingtrea 0:bdd16076aaa5 235 mdot_radio->setActivityLedEnable(true);
falingtrea 0:bdd16076aaa5 236
falingtrea 0:bdd16076aaa5 237 // Read node ID
falingtrea 0:bdd16076aaa5 238 mdot_EUI = mdot_radio->getDeviceId();
falingtrea 0:bdd16076aaa5 239 printf("mDot EUI = ");
falingtrea 0:bdd16076aaa5 240
falingtrea 0:bdd16076aaa5 241 for (i=0; i<mdot_EUI.size(); i++) {
falingtrea 0:bdd16076aaa5 242 printf("%02x ", mdot_EUI[i]);
falingtrea 0:bdd16076aaa5 243 }
falingtrea 0:bdd16076aaa5 244 printf("\n\r");
falingtrea 0:bdd16076aaa5 245
falingtrea 0:bdd16076aaa5 246
falingtrea 2:75adc72aa6a0 247 // Setting up the mDot with network information.
falingtrea 2:75adc72aa6a0 248
falingtrea 2:75adc72aa6a0 249 /*
falingtrea 2:75adc72aa6a0 250 * This call sets up private or public mode on the MTDOT. Set the function to true if
falingtrea 2:75adc72aa6a0 251 * connecting to a public network
falingtrea 2:75adc72aa6a0 252 */
falingtrea 2:75adc72aa6a0 253 printf("setting Private Network Mode\r\n");
falingtrea 2:75adc72aa6a0 254 if ((mdot_ret = mdot_radio->setPublicNetwork(false)) != mDot::MDOT_OK) {
falingtrea 2:75adc72aa6a0 255 log_error(mdot_radio, "failed to set Public Network Mode", mdot_ret);
falingtrea 2:75adc72aa6a0 256 }
falingtrea 2:75adc72aa6a0 257
falingtrea 2:75adc72aa6a0 258 /*
falingtrea 2:75adc72aa6a0 259 * Frequency sub-band is valid for NAM only and for Private networks should be set to a value
falingtrea 2:75adc72aa6a0 260 * between 1-8 that matches the the LoRa gateway setting. Public networks use sub-band 0 only.
falingtrea 2:75adc72aa6a0 261 * This function can be commented out for EU networks
falingtrea 2:75adc72aa6a0 262 */
falingtrea 0:bdd16076aaa5 263 printf("setting frequency sub band\r\n");
falingtrea 0:bdd16076aaa5 264 if ((mdot_ret = mdot_radio->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
falingtrea 0:bdd16076aaa5 265 log_error(mdot_radio, "failed to set frequency sub band", mdot_ret);
falingtrea 0:bdd16076aaa5 266 }
falingtrea 0:bdd16076aaa5 267
falingtrea 2:75adc72aa6a0 268 /*
falingtrea 2:75adc72aa6a0 269 * setNetworkName is used for private networks.
falingtrea 2:75adc72aa6a0 270 * Use setNetworkID(AppID) for public networks
falingtrea 2:75adc72aa6a0 271 */
falingtrea 2:75adc72aa6a0 272
falingtrea 2:75adc72aa6a0 273 // config_app_id.assign(app_id,app_id+7);
falingtrea 2:75adc72aa6a0 274
falingtrea 0:bdd16076aaa5 275 printf("setting network name\r\n");
falingtrea 0:bdd16076aaa5 276 if ((mdot_ret = mdot_radio->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
falingtrea 2:75adc72aa6a0 277 // if ((mdot_ret = mdot_radio->setNetworkID(config_app_id)) != mDot::MDOT_OK) {
falingtrea 0:bdd16076aaa5 278 log_error(mdot_radio, "failed to set network name", mdot_ret);
falingtrea 0:bdd16076aaa5 279 }
falingtrea 0:bdd16076aaa5 280
falingtrea 2:75adc72aa6a0 281 /*
falingtrea 2:75adc72aa6a0 282 * setNetworkPassphrase is used for private networks
falingtrea 2:75adc72aa6a0 283 * Use setNetworkKey for public networks
falingtrea 2:75adc72aa6a0 284 */
falingtrea 2:75adc72aa6a0 285
falingtrea 2:75adc72aa6a0 286 // config_app_key.assign(app_key,app_key+15);
falingtrea 2:75adc72aa6a0 287
falingtrea 0:bdd16076aaa5 288 printf("setting network password\r\n");
falingtrea 0:bdd16076aaa5 289 if ((mdot_ret = mdot_radio->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
falingtrea 2:75adc72aa6a0 290 // if ((mdot_ret = mdot_radio->setNetworkKey(config_app_key)) != mDot::MDOT_OK) {
falingtrea 0:bdd16076aaa5 291 log_error(mdot_radio, "failed to set network password", mdot_ret);
falingtrea 0:bdd16076aaa5 292 }
falingtrea 0:bdd16076aaa5 293
falingtrea 0:bdd16076aaa5 294 // attempt to join the network
falingtrea 0:bdd16076aaa5 295 printf("joining network\r\n");
falingtrea 2:75adc72aa6a0 296 while (((mdot_ret = mdot_radio->joinNetwork()) != mDot::MDOT_OK) && (!exit_program)) {
falingtrea 0:bdd16076aaa5 297 log_error(mdot_radio,"failed to join network:", mdot_ret);
falingtrea 0:bdd16076aaa5 298 if (mdot_radio->getFrequencyBand() == mDot::FB_868){
falingtrea 0:bdd16076aaa5 299 mdot_ret = mdot_radio->getNextTxMs();
falingtrea 0:bdd16076aaa5 300 }
falingtrea 0:bdd16076aaa5 301 else {
falingtrea 0:bdd16076aaa5 302 mdot_ret = 0;
falingtrea 2:75adc72aa6a0 303 }
falingtrea 0:bdd16076aaa5 304
falingtrea 0:bdd16076aaa5 305 printf("delay = %lu\n\r",mdot_ret);
falingtrea 0:bdd16076aaa5 306 osDelay(mdot_ret + 1);
falingtrea 0:bdd16076aaa5 307 }
falingtrea 2:75adc72aa6a0 308
falingtrea 2:75adc72aa6a0 309 /*
falingtrea 2:75adc72aa6a0 310 * Check for PB1 press during network join attempt
falingtrea 2:75adc72aa6a0 311 */
falingtrea 2:75adc72aa6a0 312 if (exit_program) {
falingtrea 2:75adc72aa6a0 313 printf("Exiting program\n\r");
falingtrea 2:75adc72aa6a0 314 evbLCD->clearBuffer();
falingtrea 2:75adc72aa6a0 315 sprintf(txtstr,"Exiting Program");
falingtrea 2:75adc72aa6a0 316 evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 317 exit(1);
falingtrea 2:75adc72aa6a0 318 }
falingtrea 2:75adc72aa6a0 319
falingtrea 0:bdd16076aaa5 320 } else {
falingtrea 0:bdd16076aaa5 321 printf("radio setup failed\n\r");
falingtrea 0:bdd16076aaa5 322 //exit(1);
falingtrea 0:bdd16076aaa5 323 }
falingtrea 0:bdd16076aaa5 324
falingtrea 0:bdd16076aaa5 325 osDelay(200);
falingtrea 0:bdd16076aaa5 326 evbBackLight->setPWM(NCP5623B::LED_3,16); // enable LED2 on EVB and set to 50% PWM
falingtrea 0:bdd16076aaa5 327
falingtrea 2:75adc72aa6a0 328 // sets LED2 to 50% max current
falingtrea 0:bdd16076aaa5 329 evbBackLight->setLEDCurrent(16);
falingtrea 0:bdd16076aaa5 330
falingtrea 0:bdd16076aaa5 331 printf("Start of Test\n\r");
falingtrea 0:bdd16076aaa5 332
falingtrea 2:75adc72aa6a0 333 osDelay (500); // allows other threads to process
falingtrea 2:75adc72aa6a0 334 printf("shutdown LED:\n\r");
falingtrea 2:75adc72aa6a0 335 evbBackLight->shutdown();
falingtrea 0:bdd16076aaa5 336
falingtrea 0:bdd16076aaa5 337 osDelay (500); // allows other threads to process
falingtrea 0:bdd16076aaa5 338 printf("Turn on LED2\n\r");
falingtrea 0:bdd16076aaa5 339 evbBackLight->setLEDCurrent(16);
falingtrea 0:bdd16076aaa5 340
falingtrea 0:bdd16076aaa5 341 data = evbAccel->getWhoAmI();
falingtrea 0:bdd16076aaa5 342 printf("Accelerometer who_am_i value = %x \n\r", data);
falingtrea 0:bdd16076aaa5 343
falingtrea 0:bdd16076aaa5 344 result = evbAccel->getStatus();
falingtrea 0:bdd16076aaa5 345 printf("status byte = %x \n\r", result);
falingtrea 0:bdd16076aaa5 346
falingtrea 0:bdd16076aaa5 347 printf("Barometer who_am_i check = %s \n\r", evbBaro->testWhoAmI() ? "TRUE" : "FALSE");
falingtrea 0:bdd16076aaa5 348
falingtrea 0:bdd16076aaa5 349 result = evbBaro->getStatus();
falingtrea 0:bdd16076aaa5 350 printf("status byte = %x \n\r", result);
falingtrea 0:bdd16076aaa5 351
falingtrea 0:bdd16076aaa5 352 /*
falingtrea 0:bdd16076aaa5 353 * Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz
falingtrea 0:bdd16076aaa5 354 * normal oversample mode, High pass filter off
falingtrea 0:bdd16076aaa5 355 */
falingtrea 0:bdd16076aaa5 356 evbAccel->setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF,
falingtrea 0:bdd16076aaa5 357 MMA845x::DR_1_56,MMA845x::OS_NORMAL,MMA845x::HPF_OFF );
falingtrea 0:bdd16076aaa5 358
falingtrea 0:bdd16076aaa5 359 /*
falingtrea 0:bdd16076aaa5 360 * Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition.
falingtrea 0:bdd16076aaa5 361 * and a sample taken every second when in active mode
falingtrea 0:bdd16076aaa5 362 */
falingtrea 0:bdd16076aaa5 363 evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16,
falingtrea 0:bdd16076aaa5 364 MPL3115A2::AT_1);
falingtrea 0:bdd16076aaa5 365 /*
falingtrea 0:bdd16076aaa5 366 * Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution,
falingtrea 0:bdd16076aaa5 367 * and 16000 lux range
falingtrea 0:bdd16076aaa5 368 */
falingtrea 0:bdd16076aaa5 369
falingtrea 0:bdd16076aaa5 370 evbAmbLight->setMode(ISL29011::ALS_CONT);
falingtrea 0:bdd16076aaa5 371 evbAmbLight->setResolution(ISL29011::ADC_16BIT);
falingtrea 0:bdd16076aaa5 372 evbAmbLight->setRange(ISL29011::RNG_16000);
falingtrea 0:bdd16076aaa5 373
falingtrea 0:bdd16076aaa5 374 /*
falingtrea 0:bdd16076aaa5 375 * Set the accelerometer for active mode
falingtrea 0:bdd16076aaa5 376 */
falingtrea 0:bdd16076aaa5 377 evbAccel->activeMode();
falingtrea 0:bdd16076aaa5 378
falingtrea 0:bdd16076aaa5 379 /*
falingtrea 0:bdd16076aaa5 380 * Clear the min-max registers in the Barometric Sensor
falingtrea 0:bdd16076aaa5 381 */
falingtrea 0:bdd16076aaa5 382 evbBaro->clearMinMaxRegs();
falingtrea 0:bdd16076aaa5 383
falingtrea 0:bdd16076aaa5 384 evbBackLight->setLEDCurrent(0);
falingtrea 0:bdd16076aaa5 385
falingtrea 0:bdd16076aaa5 386 /*
falingtrea 2:75adc72aa6a0 387 * Check for PB1 press during network join attempt
falingtrea 2:75adc72aa6a0 388 */
falingtrea 2:75adc72aa6a0 389 if (exit_program) {
falingtrea 2:75adc72aa6a0 390 printf("Exiting program\n\r");
falingtrea 2:75adc72aa6a0 391 evbLCD->clearBuffer();
falingtrea 2:75adc72aa6a0 392 sprintf(txtstr,"Exiting Program");
falingtrea 2:75adc72aa6a0 393 evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 394 exit(1);
falingtrea 2:75adc72aa6a0 395 }
falingtrea 2:75adc72aa6a0 396
falingtrea 2:75adc72aa6a0 397 /*
falingtrea 0:bdd16076aaa5 398 * Main data acquisition loop
falingtrea 0:bdd16076aaa5 399 */
falingtrea 0:bdd16076aaa5 400 pckt_time = 10;
falingtrea 0:bdd16076aaa5 401 i = 0;
daveheitzman 5:5a794cedd728 402 char sensor_text[54];
daveheitzman 5:5a794cedd728 403 char lora_temp_string[7];
daveheitzman 5:5a794cedd728 404 char lora_alt_string[8];
daveheitzman 5:5a794cedd728 405 char lora_press_string[9];
daveheitzman 5:5a794cedd728 406 char lora_light_string[6];
falingtrea 0:bdd16076aaa5 407
falingtrea 0:bdd16076aaa5 408 do {
falingtrea 0:bdd16076aaa5 409 evbLCD->startUpdate();
falingtrea 0:bdd16076aaa5 410 evbLCD->clearBuffer();
falingtrea 0:bdd16076aaa5 411
falingtrea 0:bdd16076aaa5 412 /*
falingtrea 0:bdd16076aaa5 413 * Test Accelerometer XYZ data ready bit to see if acquisition complete
falingtrea 0:bdd16076aaa5 414 */
falingtrea 0:bdd16076aaa5 415 do {
falingtrea 0:bdd16076aaa5 416 osDelay(100); // allows other threads to process
falingtrea 0:bdd16076aaa5 417 result = evbAccel->getStatus();
falingtrea 0:bdd16076aaa5 418 } while ((result & MMA845x::XYZDR) == 0 );
falingtrea 0:bdd16076aaa5 419
falingtrea 0:bdd16076aaa5 420 /*
falingtrea 0:bdd16076aaa5 421 * Retrieve and print out accelerometer data
falingtrea 0:bdd16076aaa5 422 */
falingtrea 0:bdd16076aaa5 423 accel_data = evbAccel->getXYZ();
falingtrea 0:bdd16076aaa5 424
falingtrea 0:bdd16076aaa5 425 sprintf(txtstr,"Accelerometer");
falingtrea 0:bdd16076aaa5 426 evbLCD->writeText(0,0,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 427 sprintf(txtstr, "x = %d", accel_data._x);
falingtrea 0:bdd16076aaa5 428 evbLCD->writeText(20,1,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 429 sprintf(txtstr, "y = %d", accel_data._y);
falingtrea 0:bdd16076aaa5 430 evbLCD->writeText(20,2,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 431 sprintf(txtstr, "z = %d", accel_data._z );
falingtrea 0:bdd16076aaa5 432 evbLCD->writeText(20,3,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 433
falingtrea 0:bdd16076aaa5 434 /*
falingtrea 0:bdd16076aaa5 435 * Trigger a Pressure reading
falingtrea 0:bdd16076aaa5 436 */
falingtrea 0:bdd16076aaa5 437 evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16,
falingtrea 0:bdd16076aaa5 438 MPL3115A2::AT_1);
falingtrea 0:bdd16076aaa5 439 evbBaro->triggerOneShot();
falingtrea 0:bdd16076aaa5 440
falingtrea 0:bdd16076aaa5 441 /*
falingtrea 0:bdd16076aaa5 442 * Test barometer device status to see if acquisition is complete
falingtrea 0:bdd16076aaa5 443 */
falingtrea 0:bdd16076aaa5 444 do {
falingtrea 0:bdd16076aaa5 445 osDelay(100); // allows other threads to process
falingtrea 0:bdd16076aaa5 446 result = evbBaro->getStatus();
falingtrea 0:bdd16076aaa5 447 } while ((result & MPL3115A2::PTDR) == 0 );
falingtrea 0:bdd16076aaa5 448
falingtrea 0:bdd16076aaa5 449 /*
falingtrea 0:bdd16076aaa5 450 * Retrieve and print out barometric pressure
falingtrea 0:bdd16076aaa5 451 */
falingtrea 0:bdd16076aaa5 452 pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value
falingtrea 0:bdd16076aaa5 453 num_whole = pressure >> 2; // 18 bit integer significant
falingtrea 0:bdd16076aaa5 454 num_frac = (pressure & 0x3) * 25; // 2 bit fractional 0.25 per bit
falingtrea 0:bdd16076aaa5 455 sprintf(txtstr,"Press=%ld.%02d Pa", num_whole, num_frac);
daveheitzman 5:5a794cedd728 456
daveheitzman 5:5a794cedd728 457 pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value
daveheitzman 5:5a794cedd728 458 num_whole = pressure >> 2; // 18 bit integer significant
daveheitzman 5:5a794cedd728 459 num_frac = (pressure & 0x3) * 25; // 2 bit fractional 0.25 per bit
daveheitzman 5:5a794cedd728 460
daveheitzman 5:5a794cedd728 461 sprintf(lora_press_string, "%ld.%02d", num_whole, num_frac);
daveheitzman 5:5a794cedd728 462
falingtrea 0:bdd16076aaa5 463 evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 464
falingtrea 0:bdd16076aaa5 465 /*
falingtrea 0:bdd16076aaa5 466 * Trigger a Altitude reading
falingtrea 0:bdd16076aaa5 467 */
falingtrea 0:bdd16076aaa5 468 evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16,
falingtrea 0:bdd16076aaa5 469 MPL3115A2::AT_1);
falingtrea 0:bdd16076aaa5 470 evbBaro->triggerOneShot();
falingtrea 0:bdd16076aaa5 471
falingtrea 0:bdd16076aaa5 472 /*
falingtrea 0:bdd16076aaa5 473 * Test barometer device status to see if acquisition is complete
falingtrea 0:bdd16076aaa5 474 */
falingtrea 0:bdd16076aaa5 475 do {
falingtrea 0:bdd16076aaa5 476 osDelay(100); // allows other threads to process
falingtrea 0:bdd16076aaa5 477 result = evbBaro->getStatus();
falingtrea 0:bdd16076aaa5 478 } while ((result & MPL3115A2::PTDR) == 0 );
falingtrea 0:bdd16076aaa5 479
falingtrea 0:bdd16076aaa5 480 /*
falingtrea 0:bdd16076aaa5 481 * Retrieve and print out altitude and temperature
daveheitzman 5:5a794cedd728 482 const std::string lora_temp_string;
daveheitzman 5:5a794cedd728 483 const std::string lora_alt_string;
daveheitzman 5:5a794cedd728 484 const std::string lora_press_string;
daveheitzman 5:5a794cedd728 485 const std::string lora_light_string;
daveheitzman 5:5a794cedd728 486
falingtrea 0:bdd16076aaa5 487 */
falingtrea 0:bdd16076aaa5 488 baro_data = evbBaro->getAllData(false);
falingtrea 0:bdd16076aaa5 489 baro_data._baro /= 4096; // convert 32 bit signed to 20 bit signed value
falingtrea 0:bdd16076aaa5 490 num_whole = baro_data._baro / 16; // 18 bit signed significant integer
falingtrea 0:bdd16076aaa5 491 num_frac = (baro_data._baro & 0xF) * 625 / 100; // 4 bit fractional .0625 per bit
falingtrea 0:bdd16076aaa5 492 sprintf(txtstr,"Alti=%ld.%03d m", num_whole, num_frac);
daveheitzman 5:5a794cedd728 493 sprintf(lora_alt_string,"%ld.%03d", num_whole, num_frac);
falingtrea 0:bdd16076aaa5 494 evbLCD->writeText(0,5,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 495 num_whole = baro_data._temp / 16; // 8 bit signed significant integer
falingtrea 0:bdd16076aaa5 496 num_frac = (baro_data._temp & 0x0F) * 625 / 100; // 4 bit fractional .0625 per bit
falingtrea 0:bdd16076aaa5 497 sprintf(txtstr,"Temp=%ld.%03d C", num_whole, num_frac);
daveheitzman 5:5a794cedd728 498 sprintf(lora_temp_string,"%ld.%03d", num_whole, num_frac);
falingtrea 0:bdd16076aaa5 499 evbLCD->writeText(0,6,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 500
falingtrea 0:bdd16076aaa5 501 /*
falingtrea 0:bdd16076aaa5 502 * retrieve and print out Ambient Light level
falingtrea 0:bdd16076aaa5 503 */
falingtrea 0:bdd16076aaa5 504 lux_data = evbAmbLight->getData();
falingtrea 0:bdd16076aaa5 505 num_whole = lux_data * 24 / 100; // 16000 lux full scale .24 lux per bit
falingtrea 0:bdd16076aaa5 506 num_frac = lux_data * 24 % 100;
falingtrea 0:bdd16076aaa5 507 sprintf(txtstr, "Light=%ld.%02d lux", num_whole, num_frac );
daveheitzman 5:5a794cedd728 508 sprintf(lora_light_string, "%ld.%02d", num_whole, num_frac );
falingtrea 0:bdd16076aaa5 509 evbLCD->writeText(0,7,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 510
falingtrea 0:bdd16076aaa5 511 evbLCD->endUpdate();
falingtrea 0:bdd16076aaa5 512 printf("finished iteration %d\n\r",(++i));
falingtrea 0:bdd16076aaa5 513 if (i % pckt_time == 0) { // check packet counter will send packet every 2-5-10 data collection loops
falingtrea 0:bdd16076aaa5 514 mdot_data.clear();
falingtrea 0:bdd16076aaa5 515 mdot_data.push_back(0x0E); // key for Current Acceleration 3-Axis Value
falingtrea 0:bdd16076aaa5 516 converts.f_s = accel_data._x *4; // shift data 2 bits while retaining sign
falingtrea 0:bdd16076aaa5 517 mdot_data.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value
falingtrea 0:bdd16076aaa5 518 converts.f_s = accel_data._y * 4; // shift data 2 bits while retaining sign
falingtrea 0:bdd16076aaa5 519 mdot_data.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value
falingtrea 0:bdd16076aaa5 520 converts.f_s = accel_data._z * 4; // shift data 2 bits while retaining sign
falingtrea 0:bdd16076aaa5 521 mdot_data.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value
falingtrea 0:bdd16076aaa5 522 mdot_data.push_back(0x08); // key for Current Pressure Value
falingtrea 0:bdd16076aaa5 523 convertl.f_u = pressure; // pressure data is 20 bits unsigned
falingtrea 0:bdd16076aaa5 524 mdot_data.push_back(convertl.t_u[2]);
falingtrea 0:bdd16076aaa5 525 mdot_data.push_back(convertl.t_u[1]);
falingtrea 0:bdd16076aaa5 526 mdot_data.push_back(convertl.t_u[0]);
falingtrea 0:bdd16076aaa5 527 mdot_data.push_back(0x05); // key for Current Ambient Light Value
falingtrea 0:bdd16076aaa5 528 converts.f_u = lux_data; // data is 16 bits unsigned
falingtrea 0:bdd16076aaa5 529 mdot_data.push_back(converts.t_u[1]);
falingtrea 0:bdd16076aaa5 530 mdot_data.push_back(converts.t_u[0]);
falingtrea 0:bdd16076aaa5 531 mdot_data.push_back(0x0B); // key for Current Temperature Value
falingtrea 0:bdd16076aaa5 532 converts.f_s = baro_data._temp; // temperature is signed 12 bit
falingtrea 0:bdd16076aaa5 533 mdot_data.push_back(converts.t_u[1]);
falingtrea 0:bdd16076aaa5 534 mdot_data.push_back(converts.t_u[0]);
daveheitzman 5:5a794cedd728 535 mdot_data.clear();
daveheitzman 5:5a794cedd728 536
daveheitzman 5:5a794cedd728 537 // if you want to go back to sending byte vector instead:
daveheitzman 5:5a794cedd728 538 // if ((mdot_ret = mdot_radio->send(mdot_data)) != mDot::MDOT_OK) {
daveheitzman 5:5a794cedd728 539 // sprintf(sensor_text, "x:%d,y:%d,z:%d,p:%s,a:%s,t:%s,l:%s",
daveheitzman 5:5a794cedd728 540
daveheitzman 5:5a794cedd728 541 sprintf(sensor_text, "%d,%d,%d,%s,%s,%s,%s",
daveheitzman 5:5a794cedd728 542 accel_data._x,
daveheitzman 5:5a794cedd728 543 accel_data._y,
daveheitzman 5:5a794cedd728 544 accel_data._z,
daveheitzman 5:5a794cedd728 545 lora_press_string,
daveheitzman 5:5a794cedd728 546 lora_alt_string,
daveheitzman 5:5a794cedd728 547 lora_temp_string,
daveheitzman 5:5a794cedd728 548 lora_light_string);
daveheitzman 5:5a794cedd728 549 if ((mdot_ret = sendString((const std::string)sensor_text)) != mDot::MDOT_OK) {
falingtrea 0:bdd16076aaa5 550 log_error(mdot_radio, "failed to send", mdot_ret);
falingtrea 0:bdd16076aaa5 551 } else {
falingtrea 0:bdd16076aaa5 552 printf("successfully sent data to gateway\r\n");
falingtrea 0:bdd16076aaa5 553 }
falingtrea 0:bdd16076aaa5 554 }
daveheitzman 5:5a794cedd728 555
daveheitzman 4:51d7c24bbf91 556 } while(!exit_program);
falingtrea 0:bdd16076aaa5 557
falingtrea 0:bdd16076aaa5 558 evbBaro->triggerOneShot();
falingtrea 0:bdd16076aaa5 559
falingtrea 0:bdd16076aaa5 560 do {
falingtrea 0:bdd16076aaa5 561 osDelay(200); // allows other threads to process
falingtrea 0:bdd16076aaa5 562 result = evbBaro->getStatus();
falingtrea 0:bdd16076aaa5 563 } while ((result & MPL3115A2::PTDR) == 0 );
falingtrea 0:bdd16076aaa5 564
falingtrea 0:bdd16076aaa5 565 baro_data = evbBaro->getAllData(true);
falingtrea 0:bdd16076aaa5 566 printf ("minBaro=%ld maxBaro=%ld minTemp=%d maxTemp=%d\n\r", baro_data._minbaro, baro_data._maxbaro,
falingtrea 0:bdd16076aaa5 567 baro_data._mintemp, baro_data._maxtemp);
falingtrea 0:bdd16076aaa5 568
falingtrea 0:bdd16076aaa5 569 printf("End of Test\n\r");
falingtrea 0:bdd16076aaa5 570
falingtrea 2:75adc72aa6a0 571 evbLCD->clearBuffer();
falingtrea 2:75adc72aa6a0 572 sprintf(txtstr,"Exiting Program");
falingtrea 2:75adc72aa6a0 573 evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 574
falingtrea 0:bdd16076aaa5 575 }
falingtrea 0:bdd16076aaa5 576
falingtrea 0:bdd16076aaa5 577 /*
falingtrea 3:68e974f5f532 578 * Sets pb1_low flag. Slag is cleared in pb1_debounce thread
falingtrea 0:bdd16076aaa5 579 */
falingtrea 0:bdd16076aaa5 580 void pb1ISR(void)
falingtrea 0:bdd16076aaa5 581 {
falingtrea 3:68e974f5f532 582 if (!pb1_low)
falingtrea 3:68e974f5f532 583 pb1_low = true;
falingtrea 3:68e974f5f532 584 }
falingtrea 0:bdd16076aaa5 585
falingtrea 3:68e974f5f532 586 /*
falingtrea 3:68e974f5f532 587 * Debounces pb1. Also exits program if pushbutton 1 is pressed
falingtrea 3:68e974f5f532 588 */
falingtrea 3:68e974f5f532 589 void pb1_debounce(void const *args)
falingtrea 3:68e974f5f532 590 {
falingtrea 3:68e974f5f532 591
falingtrea 3:68e974f5f532 592 static uint8_t count = 0;
falingtrea 3:68e974f5f532 593
falingtrea 3:68e974f5f532 594 while (true) {
falingtrea 3:68e974f5f532 595
falingtrea 3:68e974f5f532 596 if (pb1_low && (mDot08 == 0))
falingtrea 3:68e974f5f532 597 count++;
falingtrea 3:68e974f5f532 598 else {
falingtrea 3:68e974f5f532 599 count = 0;
falingtrea 3:68e974f5f532 600 pb1_low = false;
falingtrea 3:68e974f5f532 601 }
falingtrea 3:68e974f5f532 602
falingtrea 3:68e974f5f532 603 if (count == 5)
falingtrea 3:68e974f5f532 604 exit_program = true;
falingtrea 3:68e974f5f532 605
falingtrea 3:68e974f5f532 606 Thread::wait(5);
falingtrea 3:68e974f5f532 607 }
falingtrea 0:bdd16076aaa5 608 }
falingtrea 0:bdd16076aaa5 609
falingtrea 0:bdd16076aaa5 610 /*
falingtrea 3:68e974f5f532 611 * Sets pb2_low flag. Flag is cleared in pb2_debounce thread
falingtrea 0:bdd16076aaa5 612 */
falingtrea 0:bdd16076aaa5 613 void pb2ISR(void)
falingtrea 0:bdd16076aaa5 614 {
falingtrea 3:68e974f5f532 615 if (!pb2_low)
falingtrea 3:68e974f5f532 616 pb2_low = true;
falingtrea 3:68e974f5f532 617 }
falingtrea 0:bdd16076aaa5 618
falingtrea 3:68e974f5f532 619 /*
falingtrea 3:68e974f5f532 620 * Debounces pb2. Also changes packet transmit time to every other,
falingtrea 3:68e974f5f532 621 * every fifth, or every tenth sample when SW2 pushed
falingtrea 3:68e974f5f532 622 * Also triggers a thread to transmit a configuration packet
falingtrea 3:68e974f5f532 623 */
falingtrea 3:68e974f5f532 624 void pb2_debounce(void const *args)
falingtrea 3:68e974f5f532 625 {
falingtrea 3:68e974f5f532 626
falingtrea 3:68e974f5f532 627 static uint8_t count = 0;
falingtrea 3:68e974f5f532 628
falingtrea 3:68e974f5f532 629 while (true) {
falingtrea 0:bdd16076aaa5 630
falingtrea 3:68e974f5f532 631 if (pb2_low && (mDot09 == 0))
falingtrea 3:68e974f5f532 632 count++;
falingtrea 3:68e974f5f532 633 else {
falingtrea 3:68e974f5f532 634 count = 0;
falingtrea 3:68e974f5f532 635 pb2_low = false;
falingtrea 3:68e974f5f532 636 }
falingtrea 3:68e974f5f532 637
falingtrea 3:68e974f5f532 638 if (count == 5){
falingtrea 0:bdd16076aaa5 639
falingtrea 3:68e974f5f532 640 if (pckt_time >= 5)
falingtrea 3:68e974f5f532 641 pckt_time /= 2;
falingtrea 3:68e974f5f532 642 else pckt_time = 20;
falingtrea 3:68e974f5f532 643
falingtrea 3:68e974f5f532 644 thread_3->signal_set(0x10); // signal config_pkt_xmit to send packet
falingtrea 3:68e974f5f532 645 }
falingtrea 3:68e974f5f532 646
falingtrea 3:68e974f5f532 647 Thread::wait(5);
falingtrea 3:68e974f5f532 648 }
falingtrea 3:68e974f5f532 649 }
falingtrea 0:bdd16076aaa5 650
falingtrea 2:75adc72aa6a0 651 /*
falingtrea 2:75adc72aa6a0 652 * Function that print clear text verion of mDot errors
falingtrea 2:75adc72aa6a0 653 */
falingtrea 0:bdd16076aaa5 654 void log_error(mDot* dot, const char* msg, int32_t retval)
falingtrea 0:bdd16076aaa5 655 {
falingtrea 0:bdd16076aaa5 656 printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str());
falingtrea 0:bdd16076aaa5 657 }
falingtrea 0:bdd16076aaa5 658
falingtrea 2:75adc72aa6a0 659 /*
falingtrea 2:75adc72aa6a0 660 * Thread that is triggered by SW2 ISR. Sends a packet to the LoRa server with the new Packet Transmission time setting
falingtrea 2:75adc72aa6a0 661 */
falingtrea 0:bdd16076aaa5 662 void config_pkt_xmit (void const *args)
falingtrea 0:bdd16076aaa5 663 {
falingtrea 0:bdd16076aaa5 664
falingtrea 0:bdd16076aaa5 665 std::vector<uint8_t> data;
falingtrea 0:bdd16076aaa5 666
falingtrea 0:bdd16076aaa5 667 while (true) {
falingtrea 0:bdd16076aaa5 668 Thread::signal_wait(0x10); // wait for pb2ISR to signal send
falingtrea 0:bdd16076aaa5 669 data.clear();
falingtrea 0:bdd16076aaa5 670 data.push_back(0x0F); // key for Configuration data (packet transmission timer)
falingtrea 0:bdd16076aaa5 671 data.push_back(pckt_time);
falingtrea 0:bdd16076aaa5 672
falingtrea 0:bdd16076aaa5 673 if ((mdot_ret = mdot_radio->send(data)) != mDot::MDOT_OK) {
falingtrea 0:bdd16076aaa5 674 log_error(mdot_radio, "failed to send config data", mdot_ret);
falingtrea 0:bdd16076aaa5 675 } else {
falingtrea 0:bdd16076aaa5 676 printf("sent config data to gateway\r\n");
falingtrea 0:bdd16076aaa5 677 }
falingtrea 0:bdd16076aaa5 678 }
falingtrea 0:bdd16076aaa5 679 }
daveheitzman 5:5a794cedd728 680
daveheitzman 5:5a794cedd728 681 int32_t sendString(const std::string text)
daveheitzman 5:5a794cedd728 682 {
daveheitzman 5:5a794cedd728 683 int32_t ret;
daveheitzman 5:5a794cedd728 684 if (mdot_radio->getNextTxMs() != 0) {
daveheitzman 5:5a794cedd728 685 printf("Sending in %lu ms...\r\n", mdot_radio->getNextTxMs());
daveheitzman 5:5a794cedd728 686 return false;
daveheitzman 5:5a794cedd728 687 }
daveheitzman 5:5a794cedd728 688
daveheitzman 5:5a794cedd728 689 printf("Sending: '%s' ", text.c_str());
daveheitzman 5:5a794cedd728 690 std::vector<uint8_t> data(text.begin(), text.end());
daveheitzman 5:5a794cedd728 691 if ((ret = mdot_radio->send(data, 1)) != mDot::MDOT_OK)
daveheitzman 5:5a794cedd728 692 {
daveheitzman 5:5a794cedd728 693 log_error(mdot_radio, "Failed to send data", ret);
daveheitzman 5:5a794cedd728 694
daveheitzman 5:5a794cedd728 695 } else {
daveheitzman 5:5a794cedd728 696 printf("Data sent!\r\n");
daveheitzman 5:5a794cedd728 697 }
daveheitzman 5:5a794cedd728 698 return ret;
daveheitzman 5:5a794cedd728 699 }