Steve Kovarik / MTDOT-EVBDemo-DRH

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

Fork of MTDOT-EVBDemo-DRH by Dave Heitzman

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  * @file    main.cpp
00003  * @brief   Main application for mDot-EVB demo
00004  * @author  Tim Barr  MultiTech Systems Inc.
00005  * @version 1.03
00006  * @see
00007  *
00008  * Copyright (c) 2015
00009  *
00010  * Licensed under the Apache License, Version 2.0 (the "License");
00011  * you may not use this file except in compliance with the License.
00012  * You may obtain a copy of the License at
00013  *
00014  *     http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing, software
00017  * distributed under the License is distributed on an "AS IS" BASIS,
00018  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  * See the License for the specific language governing permissions and
00020  * limitations under the License.
00021  *
00022  * 1.01 TAB 7/6/15 Removed NULL pointer from evbAmbientLight creation call.
00023  *
00024  * 1.02 TAB 7/8/15 Send logo to LCD before attempting connection to LoRa network. Added
00025  *                  information on setting up for public LoRa network. Moved SW setup to
00026  *                  beginning of main. Removed printf call from ISR functions. Added
00027  *                  additional checks for exit_program.
00028  *
00029  * 1.03 TAB 7/15/15 Added threads for push button switch debounce.
00030  *
00031  */
00032 
00033 #include "mbed.h"
00034 #include "MMA845x.h"
00035 #include "MPL3115A2.h"
00036 #include "ISL29011.h"
00037 #include "NCP5623B.h"
00038 #include "DOGS102.h"
00039 #include "font_6x8.h"
00040 #include "MultiTech_Logo.h"
00041 #include "mDot.h"
00042 #include "rtos.h"
00043 #include <string>
00044 #include <vector>
00045 
00046 enum LED1_COLOR {
00047     RED = 0,
00048     GREEN = 1
00049 };
00050 
00051 /*
00052  * union for converting from 32-bit to 4 8-bit values
00053  */
00054 union convert32 {
00055     int32_t f_s;        // convert from signed 32 bit int
00056     uint32_t f_u;       // convert from unsigned 32 bit int
00057     uint8_t t_u[4];     // convert to 8 bit unsigned array
00058 };
00059 
00060 /*
00061  * union for converting from 16- bit to 2 8-bit values
00062  */
00063 union convert16 {
00064     int16_t f_s;        // convert from signed 16 bit int
00065     uint16_t f_u;       // convert from unsigned 16 bit int
00066     uint8_t t_u[2];     // convert to 8 bit unsigned array
00067 };
00068 
00069 //DigitalIn mDot02(PA_2);                       //  GPIO/UART_TX
00070 //DigitalOut mDot03(PA_3);                      //  GPIO/UART_RX
00071 //DigitalIn mDot04(PA_6);                       //  GPIO/SPI_MISO
00072 //DigitalIn mDot06(PA_8);                       //  GPIO/I2C_SCL
00073 //DigitalIn mDot07(PC_9);                       //  GPIO/I2C_SDA
00074 
00075 InterruptIn mDot08(PA_12);                      //  GPIO/USB       PB S1 on EVB
00076 InterruptIn mDot09(PA_11);                      //  GPIO/USB       PB S2 on EVB
00077 
00078 //DigitalIn mDot11(PA_7);                       //  GPIO/SPI_MOSI
00079 
00080 InterruptIn mDot12(PA_0);                       //  GPIO/UART_CTS  PRESSURE_INT2 on EVB
00081 DigitalOut mDot13(PC_13,1);                     //  GPIO           LCD_C/D
00082 InterruptIn mDot15(PC_1);                       //  GPIO           LIGHT_PROX_INT on EVB
00083 InterruptIn mDot16(PA_1);                       //  GPIO/UART_RTS  ACCEL_INT2 on EVB
00084 DigitalOut mDot17(PA_4,1);                      //  GPIO/SPI_NCS   LCD_CS on EVB
00085 
00086 //DigitalIn mDot18(PA_5);                       //  GPIO/SPI_SCK
00087 
00088 //DigitalInOut mDot19(PB_0,PIN_INPUT,PullNone,0); // GPIO         PushPull LED Low=Red High=Green set MODE=INPUT to turn off
00089 AnalogIn mDot20(PB_1);                          //  GPIO          Current Sense Analog in on EVB
00090 
00091 Serial debugUART(PA_9, PA_10);              // mDot debug UART
00092 
00093 //Serial mDotUART(PA_2, PA_3);                  // mDot external UART mDot02 and mDot03
00094 
00095 I2C mDoti2c(PC_9,PA_8);                         // mDot External I2C mDot6 and mDot7
00096 
00097 SPI mDotspi(PA_7,PA_6,PA_5);                    // mDot external SPI mDot11, mDot4, and mDot18
00098 
00099 MPL3115A2* resetBaro(const MPL3115A2* oldBaro);
00100 
00101 AnalogIn moisture_sensor(PB_1);
00102 
00103 /* **** replace these values with the proper public or private network settings ****
00104  * config_network_nameand config_network_pass are for private networks.
00105  */
00106 static std::string config_network_name = "MTCDT-18321832";
00107 static std::string config_network_pass = "MTCDT-18321832";
00108 static uint8_t config_frequency_sub_band = 3;
00109 
00110 /*  config_app_id and config_app_key are for public networks.
00111 static uint8_t app_id[8] = {0x00,0x01,0x02,0x03,0x0A,0x0B,0x0C,0x0D};
00112 std::vector<uint8_t> config_app_id;
00113 static uint8_t app_key[16] = {0x00,0x01,0x02,0x03,0x0A,0x0B,0x0C,0x0D};
00114 std::vector<uint8_t> config_app_key;
00115 */
00116 
00117 uint8_t result, pckt_time=10;
00118 char data;
00119 unsigned char test;
00120 char txtstr[17];
00121 int32_t num_whole, mdot_ret;
00122 uint32_t pressure;
00123 int16_t num_frac;
00124 float moisture;
00125 
00126 bool exit_program = false;
00127 
00128 MMA845x_DATA accel_data;
00129 MPL3115A2_DATA baro_data;
00130 uint16_t  lux_data;
00131 MMA845x* evbAccel;
00132 MPL3115A2* evbBaro;
00133 ISL29011* evbAmbLight;
00134 NCP5623B* evbBackLight;
00135 DOGS102* evbLCD;
00136 mDot* mdot_radio;
00137 
00138 convert32 convertl;
00139 convert16 converts;
00140 
00141 // flags for pushbutton debounce code
00142 bool pb1_low = false;
00143 bool pb2_low = false;
00144 
00145 void pb1ISR(void);
00146 void pb2ISR(void);
00147 void pb1_debounce(void const *args);
00148 void pb2_debounce(void const *args);
00149 Thread* thread_3;
00150 
00151 void log_error(mDot* dot, const char* msg, int32_t retval);
00152 
00153 void config_pkt_xmit (void const *args);
00154 int32_t sendString(const std::string text);
00155 
00156 bool writeValueOrError();
00157 
00158 char sensor_text[54];
00159 char lora_temp_string[7]; 
00160 char lora_alt_string[8]; 
00161 char lora_press_string[9]; 
00162     
00163 char lora_light_string[6]; 
00164 const int FAIL_MAX=15;
00165 int failtime=FAIL_MAX;
00166 
00167 int main()
00168 {
00169 
00170     std::vector<uint8_t> mdot_data;
00171     std::vector<uint8_t> mdot_EUI;
00172     uint16_t i = 0;
00173 
00174     debugUART.baud(115200);
00175 //  mDotUART.baud(9600);    // mdot UART unused but available on external connector
00176 
00177     Thread thread_1(pb1_debounce);                      // threads for de-bouncing pushbutton switches
00178     Thread thread_2(pb2_debounce);
00179 
00180     thread_3 = new Thread(config_pkt_xmit);             // start thread that sends LoRa packet when SW2 pressed
00181 
00182     evbAccel = new MMA845x(mDoti2c,MMA845x::SA0_VSS);   // setup Accelerometer
00183     evbBaro = new MPL3115A2(mDoti2c);                   // setup0 Barometric sensor
00184     evbAmbLight = new ISL29011(mDoti2c);                // Setup Ambient Light Sensor
00185     evbBackLight = new NCP5623B(mDoti2c);               // setup backlight and LED 2 driver chip
00186     evbLCD = new DOGS102(mDotspi, mDot17, mDot13);      // setup LCD
00187 
00188     /*
00189      *  Setup SW1 as program stop function
00190      */
00191     mDot08.disable_irq();
00192     mDot08.fall(&pb1ISR);
00193 
00194     /*
00195      *  need to call this function after rise or fall because rise/fall sets
00196      *  mode to PullNone
00197      */
00198     mDot08.mode(PullUp);
00199 
00200     mDot08.enable_irq();
00201 
00202     /*
00203      *  Setup SW2 as packet time change
00204      */
00205     mDot09.disable_irq();
00206     mDot09.fall(&pb2ISR);
00207 
00208     /*
00209      *  need to call this function after rise or fall because rise/fall sets
00210      *  mode to PullNone
00211      */
00212     mDot09.mode(PullUp);
00213  
00214     mDot09.enable_irq();
00215 
00216     /*
00217     * Setting other InterruptIn pins with Pull Ups
00218     */
00219     mDot12.mode(PullUp);
00220     mDot15.mode(PullUp);
00221     mDot16.mode(PullUp);
00222 
00223     printf("font table address %p\n\r",&font_6x8);
00224     printf("bitmap address %p\n\r",&MultiTech_Logo);
00225 
00226 // Setup and display logo on LCD
00227     evbLCD->startUpdate();
00228 
00229     evbLCD->writeBitmap(0,0,MultiTech_Logo);
00230 
00231     sprintf(txtstr,"MTDOT");
00232     evbLCD->writeText(24,3,font_6x8,txtstr,strlen(txtstr));
00233     sprintf(txtstr,"Evaluation");
00234     evbLCD->writeText(24,4,font_6x8,txtstr,strlen(txtstr));
00235     sprintf(txtstr,"Board");
00236     evbLCD->writeText(24,5,font_6x8,txtstr,strlen(txtstr));
00237 
00238     evbLCD->endUpdate();
00239 
00240    printf("\n\r setup mdot\n\r");
00241 
00242     // get a mDot handle
00243     mdot_radio = mDot::getInstance();
00244 
00245     if (mdot_radio) {
00246         // reset to default config so we know what state we're in
00247         mdot_radio->resetConfig();
00248 
00249         // Setting up LED1 as activity LED
00250         mdot_radio->setActivityLedPin(PB_0);
00251         mdot_radio->setActivityLedEnable(true);
00252 
00253         // Read node ID
00254         mdot_EUI = mdot_radio->getDeviceId();
00255         printf("mDot EUI = ");
00256 
00257         for (i=0; i<mdot_EUI.size(); i++) {
00258             printf("%02x ", mdot_EUI[i]);
00259         }
00260         printf("\n\r");
00261 
00262 
00263 // Setting up the mDot with network information.
00264 
00265 /*
00266  * This call sets up private or public mode on the MTDOT. Set the function to true if
00267  * connecting to a public network
00268  */
00269        printf("setting Private Network Mode\r\n");
00270        if ((mdot_ret = mdot_radio->setPublicNetwork(false)) != mDot::MDOT_OK) {
00271            log_error(mdot_radio, "failed to set Public Network Mode", mdot_ret);
00272        }
00273 
00274 /*
00275  * Frequency sub-band is valid for NAM only and for Private networks should be set to a value
00276  * between 1-8 that matches the the LoRa gateway setting. Public networks use sub-band 0 only.
00277  * This function can be commented out for EU networks
00278  */
00279         printf("setting frequency sub band\r\n");
00280         if ((mdot_ret = mdot_radio->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
00281             log_error(mdot_radio, "failed to set frequency sub band", mdot_ret);
00282         }
00283 
00284 /*
00285  * setNetworkName is used for private networks.
00286  * Use setNetworkID(AppID) for public networks
00287  */
00288 
00289 //      config_app_id.assign(app_id,app_id+7);
00290 
00291         printf("setting network name\r\n");
00292         if ((mdot_ret = mdot_radio->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
00293 //      if ((mdot_ret = mdot_radio->setNetworkID(config_app_id)) != mDot::MDOT_OK) {
00294             log_error(mdot_radio, "failed to set network name", mdot_ret);
00295         }
00296 
00297 /*
00298  * setNetworkPassphrase is used for private networks
00299  * Use setNetworkKey for public networks
00300  */
00301 
00302 //      config_app_key.assign(app_key,app_key+15);
00303 
00304         printf("setting network password\r\n");
00305         if ((mdot_ret = mdot_radio->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
00306 //      if ((mdot_ret = mdot_radio->setNetworkKey(config_app_key)) != mDot::MDOT_OK) {
00307             log_error(mdot_radio, "failed to set network password", mdot_ret);
00308         }
00309 
00310         // attempt to join the network
00311         printf("joining network\r\n");
00312         while (((mdot_ret = mdot_radio->joinNetwork()) != mDot::MDOT_OK) && (!exit_program)) {
00313             log_error(mdot_radio,"failed to join network:", mdot_ret);
00314             if (mdot_radio->getFrequencyBand() == mDot::FB_868){
00315                 mdot_ret = mdot_radio->getNextTxMs();
00316             }
00317             else {
00318                 mdot_ret = 0;
00319             }
00320                 
00321             printf("delay = %lu\n\r",mdot_ret);
00322             osDelay(mdot_ret + 1);
00323         }
00324 
00325         /*
00326          * Check for PB1 press during network join attempt
00327          */
00328         if (exit_program) {
00329             printf("Exiting program\n\r");
00330             evbLCD->clearBuffer();
00331             sprintf(txtstr,"Exiting Program");
00332             evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
00333             exit(1);
00334         }
00335 
00336     } else {
00337         printf("radio setup failed\n\r");
00338         //exit(1);
00339     }
00340 
00341     osDelay(200);
00342     evbBackLight->setPWM(NCP5623B::LED_3,16); // enable LED2 on EVB and set to 50% PWM
00343 
00344     // sets LED2 to 50% max current
00345     evbBackLight->setLEDCurrent(16);
00346 
00347     printf("Start of Test\n\r");
00348 
00349     osDelay (500);          // allows other threads to process
00350     printf("shutdown LED:\n\r");
00351     evbBackLight->shutdown();
00352 
00353     osDelay (500);          // allows other threads to process
00354     printf("Turn on LED2\n\r");
00355     evbBackLight->setLEDCurrent(16);
00356 
00357     data = evbAccel->getWhoAmI();
00358     printf("Accelerometer who_am_i value = %x \n\r", data);
00359 
00360     result = evbAccel->getStatus();
00361     printf("status byte = %x \n\r", result);
00362 
00363     printf("Barometer who_am_i check = %s \n\r", evbBaro->testWhoAmI() ? "TRUE" : "FALSE");
00364 
00365     result = evbBaro->getStatus();
00366     printf("status byte = %x \n\r", result);
00367 
00368     /*
00369      *  Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz
00370      *  normal oversample mode, High pass filter off
00371      */
00372     evbAccel->setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF,
00373                                   MMA845x::DR_1_56,MMA845x::OS_NORMAL,MMA845x::HPF_OFF );
00374 
00375     /*
00376      * Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition.
00377      * and a sample taken every second when in active mode
00378      */
00379     evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16,
00380                            MPL3115A2::AT_1);
00381     /*
00382      * Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution,
00383      * and 16000 lux range
00384      */
00385 
00386     evbAmbLight->setMode(ISL29011::ALS_CONT);
00387     evbAmbLight->setResolution(ISL29011::ADC_16BIT);
00388     evbAmbLight->setRange(ISL29011::RNG_16000);
00389 
00390     /*
00391      * Set the accelerometer for active mode
00392      */
00393     evbAccel->activeMode();
00394 
00395     /*
00396      * Clear the min-max registers in the Barometric Sensor
00397      */
00398     evbBaro->clearMinMaxRegs();
00399 
00400     evbBackLight->setLEDCurrent(0);
00401 
00402     /*
00403      * Check for PB1 press during network join attempt
00404      */
00405     if (exit_program) {
00406         printf("Exiting program\n\r");
00407         evbLCD->clearBuffer();
00408         sprintf(txtstr,"Exiting Program");
00409         evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
00410         exit(1);
00411     }
00412 
00413     /*
00414      * Main data acquisition loop
00415      */
00416     pckt_time = 10;
00417     i = 0;
00418     do {
00419         evbLCD->startUpdate();
00420         evbLCD->clearBuffer();
00421 
00422         /*
00423          * Test Accelerometer XYZ data ready bit to see if acquisition complete
00424          */
00425         failtime = FAIL_MAX;
00426         do {
00427             osDelay(100);           // allows other threads to process
00428             result = evbAccel->getStatus();
00429             failtime--;
00430         } while ((result & MMA845x::XYZDR) == 0 && failtime > 0);
00431         if (failtime==0) {        
00432           evbBaro=resetBaro(evbBaro);
00433           continue; 
00434         }
00435 
00436         /*
00437          * Retrieve and print out accelerometer data
00438          */
00439         accel_data = evbAccel->getXYZ();
00440 
00441         sprintf(txtstr,"Accelerometer");
00442         evbLCD->writeText(0,0,font_6x8,txtstr,strlen(txtstr));
00443         sprintf(txtstr, "x = %d", accel_data._x);
00444         evbLCD->writeText(20,1,font_6x8,txtstr,strlen(txtstr));
00445         sprintf(txtstr, "y = %d", accel_data._y);
00446         evbLCD->writeText(20,2,font_6x8,txtstr,strlen(txtstr));
00447         sprintf(txtstr, "z = %d", accel_data._z );
00448         evbLCD->writeText(20,3,font_6x8,txtstr,strlen(txtstr));
00449 
00450         /*
00451          * Trigger a Pressure reading
00452          */
00453         evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16,
00454                                MPL3115A2::AT_1);
00455         evbBaro->triggerOneShot();
00456 
00457         /*
00458          * Test barometer device status to see if acquisition is complete
00459          */
00460         failtime=FAIL_MAX;
00461         do {
00462             osDelay(100);           // allows other threads to process
00463             result = evbBaro->getStatus();
00464             failtime--;
00465         } while ((result & MPL3115A2::PTDR) == 0 && failtime > 0 );
00466         if (failtime==0) {        
00467           evbBaro=resetBaro(evbBaro);
00468           continue; 
00469         }
00470 
00471 
00472         /*
00473          * Retrieve and print out barometric pressure
00474          */
00475         pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value
00476         num_whole = pressure >> 2;          // 18 bit integer significant
00477         num_frac = (pressure & 0x3) * 25;       // 2 bit fractional  0.25 per bit
00478         // if failtime reached 0 , indicates that the result might be junk. 
00479         sprintf(txtstr,"Press=%ld.%02d Pa", num_whole, num_frac);
00480 
00481         pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value
00482         num_whole = pressure >> 2;          // 18 bit integer significant
00483         num_frac = (pressure & 0x3) * 25;       // 2 bit fractional  0.25 per bit
00484         
00485         writeValueOrError(); // will write to  lorapresstring and txtstr  
00486 
00487 
00488         /*
00489          * Trigger a Altitude reading
00490          */
00491         evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16,
00492                                MPL3115A2::AT_1);
00493         evbBaro->triggerOneShot();
00494 
00495         /*
00496          * Test barometer device status to see if acquisition is complete
00497          */
00498         failtime=FAIL_MAX;
00499         do {
00500             osDelay(100);           // allows other threads to process
00501             result = evbBaro->getStatus();
00502             failtime--;
00503         } while ((result & MPL3115A2::PTDR) == 0 && failtime > 0 );
00504         if (failtime==0) {        
00505           evbBaro=resetBaro(evbBaro);
00506           continue; 
00507         }
00508 
00509         /*
00510          * Retrieve and print out altitude and temperature
00511         const std::string lora_temp_string; 
00512         const std::string lora_alt_string; 
00513         const std::string lora_press_string; 
00514         const std::string lora_light_string; 
00515 
00516          */
00517         baro_data = evbBaro->getAllData(false);
00518         baro_data._baro /= 4096;                // convert 32 bit signed to 20 bit signed value
00519         num_whole = baro_data._baro / 16;       //  18 bit signed significant integer
00520         num_frac = (baro_data._baro & 0xF) * 625 / 100;     // 4 bit fractional .0625 per bit
00521         sprintf(txtstr,"Alti=%ld.%03d m", num_whole, num_frac);
00522         sprintf(lora_alt_string,"%ld.%03d", num_whole, num_frac);
00523         evbLCD->writeText(0,5,font_6x8,txtstr,strlen(txtstr));
00524         num_whole = baro_data._temp / 16;       // 8 bit signed significant integer
00525         num_frac = (baro_data._temp & 0x0F) * 625 / 100;        // 4 bit fractional .0625 per bit
00526         sprintf(txtstr,"Temp=%ld.%03d C", num_whole, num_frac);
00527         sprintf(lora_temp_string,"%ld.%03d", num_whole, num_frac);
00528         evbLCD->writeText(0,6,font_6x8,txtstr,strlen(txtstr));
00529 
00530         /*
00531          * retrieve and print out Ambient Light level
00532          */
00533         lux_data = evbAmbLight->getData();
00534         num_whole = lux_data * 24 / 100;        // 16000 lux full scale .24 lux per bit
00535         num_frac = lux_data * 24 % 100;
00536         sprintf(txtstr, "Light=%ld.%02d lux", num_whole, num_frac );
00537         sprintf(lora_light_string, "%ld.%02d", num_whole, num_frac );
00538         evbLCD->writeText(0,7,font_6x8,txtstr,strlen(txtstr));
00539         
00540         /*
00541          * retrieve and print out moisture from Analog moisture sensor
00542          */
00543          moisture = moisture_sensor;
00544          printf("moisture: %f\r\n", moisture);
00545 
00546         evbLCD->endUpdate();
00547         printf("finished iteration %d\n\r",(++i));
00548         if (i % pckt_time == 0) { // check packet counter will send packet every 2-5-10 data collection loops
00549             mdot_data.clear();
00550             mdot_data.push_back(0x0E);          // key for Current Acceleration 3-Axis Value
00551             converts.f_s = accel_data._x *4;        // shift data 2 bits while retaining sign
00552             mdot_data.push_back(converts.t_u[1]);   // get 8 MSB of 14 bit value
00553             converts.f_s = accel_data._y * 4;       // shift data 2 bits while retaining sign
00554             mdot_data.push_back(converts.t_u[1]);   // get 8 MSB of 14 bit value
00555             converts.f_s = accel_data._z * 4;       // shift data 2 bits while retaining sign
00556             mdot_data.push_back(converts.t_u[1]);   // get 8 MSB of 14 bit value
00557             mdot_data.push_back(0x08);          // key for Current Pressure Value
00558             convertl.f_u = pressure;                // pressure data is 20 bits unsigned
00559             mdot_data.push_back(convertl.t_u[2]);
00560             mdot_data.push_back(convertl.t_u[1]);
00561             mdot_data.push_back(convertl.t_u[0]);
00562             mdot_data.push_back(0x05);          // key for Current Ambient Light Value
00563             converts.f_u = lux_data;                // data is 16 bits unsigned
00564             mdot_data.push_back(converts.t_u[1]);
00565             mdot_data.push_back(converts.t_u[0]);
00566             mdot_data.push_back(0x0B);          // key for Current Temperature Value
00567             converts.f_s = baro_data._temp;     // temperature is signed 12 bit
00568             mdot_data.push_back(converts.t_u[1]);
00569             mdot_data.push_back(converts.t_u[0]);
00570             mdot_data.clear();
00571         
00572 // if you want to go back to sending byte vector instead: 
00573 //         if ((mdot_ret = mdot_radio->send(mdot_data)) != mDot::MDOT_OK) {
00574 //            sprintf(sensor_text, "x:%d,y:%d,z:%d,p:%s,a:%s,t:%s,l:%s",
00575 
00576             sprintf(sensor_text, "%d,%d,%d,%s,%s,%s,%s,%f",
00577                 accel_data._x,
00578                 accel_data._y,
00579                 accel_data._z,
00580                 lora_press_string,
00581                 lora_alt_string,
00582                 lora_temp_string,
00583                 lora_light_string,
00584                 moisture);
00585             if ((mdot_ret = sendString((const std::string)sensor_text)) != mDot::MDOT_OK) {
00586                 log_error(mdot_radio, "failed to send", mdot_ret);
00587             } else {
00588                 printf("successfully sent data to gateway\r\n");
00589             }
00590         }
00591 
00592     } while(!exit_program);
00593 
00594     evbBaro->triggerOneShot();
00595 
00596     do {
00597         osDelay(200);           // allows other threads to process
00598         result = evbBaro->getStatus();
00599     } while ((result & MPL3115A2::PTDR) == 0 );
00600 
00601     baro_data = evbBaro->getAllData(true);
00602     printf ("minBaro=%ld maxBaro=%ld minTemp=%d maxTemp=%d\n\r", baro_data._minbaro, baro_data._maxbaro,
00603             baro_data._mintemp, baro_data._maxtemp);
00604 
00605     printf("End of Test\n\r");
00606 
00607     evbLCD->clearBuffer();
00608     sprintf(txtstr,"Exiting Program");
00609     evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
00610 
00611 }
00612 
00613 /*
00614  * Sets pb1_low flag. Slag is cleared in pb1_debounce thread
00615  */
00616 void pb1ISR(void)
00617 {
00618     if (!pb1_low)
00619         pb1_low = true;
00620 }
00621 
00622 /*
00623  * Debounces pb1. Also exits program if pushbutton 1 is pressed
00624  */
00625 void pb1_debounce(void const *args)
00626 {
00627 
00628     static uint8_t count = 0;
00629 
00630     while (true) {
00631 
00632         if (pb1_low && (mDot08 == 0))
00633             count++;
00634         else {
00635             count = 0;
00636             pb1_low = false;
00637         }
00638         
00639         if (count == 5) 
00640             exit_program = true;
00641         
00642         Thread::wait(5);
00643     }
00644 }
00645 
00646 /*
00647  * Sets pb2_low flag. Flag is cleared in pb2_debounce thread
00648  */
00649 void pb2ISR(void)
00650 {
00651     if (!pb2_low)
00652         pb2_low = true;
00653 }
00654 
00655 /*
00656  * Debounces pb2. Also changes packet transmit time to every other,
00657  * every fifth, or every tenth sample when SW2 pushed
00658  * Also triggers a thread to transmit a configuration packet
00659  */
00660 void pb2_debounce(void const *args)
00661 {
00662 
00663     static uint8_t count = 0;
00664 
00665     while (true) {
00666 
00667         if (pb2_low && (mDot09 == 0))
00668             count++;
00669         else {
00670             count = 0;
00671             pb2_low = false;
00672         }
00673         
00674         if (count == 5){
00675 
00676             if (pckt_time >= 5)
00677                 pckt_time /= 2;
00678             else pckt_time = 20;
00679 
00680             thread_3->signal_set(0x10);     // signal config_pkt_xmit to send packet
00681         }
00682         
00683         Thread::wait(5);
00684     }
00685  }
00686 
00687 /*
00688  *  Function that print clear text verion of mDot errors
00689  */
00690 void log_error(mDot* dot, const char* msg, int32_t retval)
00691 {
00692     printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str());
00693 }
00694 
00695 /*
00696  * Thread that is triggered by SW2 ISR. Sends a packet to the LoRa server with the new Packet Transmission time setting
00697  */
00698 void config_pkt_xmit (void const *args)
00699 {
00700 
00701     std::vector<uint8_t> data;
00702 
00703     while (true) {
00704         Thread::signal_wait(0x10);      // wait for pb2ISR to signal send
00705         data.clear();
00706         data.push_back(0x0F);           // key for Configuration data (packet transmission timer)
00707         data.push_back(pckt_time);
00708 
00709         if ((mdot_ret = mdot_radio->send(data)) != mDot::MDOT_OK) {
00710             log_error(mdot_radio, "failed to send config data", mdot_ret);
00711         } else {
00712             printf("sent config data to gateway\r\n");
00713         }
00714     }
00715 }
00716 
00717 int32_t sendString(const std::string text)
00718 {
00719     int32_t ret;
00720     if (mdot_radio->getNextTxMs() != 0) {
00721         printf("Sending in %lu ms...\r\n", mdot_radio->getNextTxMs());
00722         return false;
00723     }
00724 
00725     printf("Sending: '%s'   ", text.c_str());
00726     std::vector<uint8_t> data(text.begin(), text.end());
00727     if ((ret = mdot_radio->send(data, 1)) != mDot::MDOT_OK)
00728     {
00729         log_error(mdot_radio, "Failed to send data", ret);
00730 
00731     } else {
00732         printf("Data sent!\r\n");
00733     }
00734     return ret;
00735 }
00736 
00737 bool writeValueOrError()
00738 {
00739     bool res;
00740         if (failtime==0){
00741             sprintf(lora_press_string, "%s", "--.--");
00742             sprintf(txtstr, "%s", "--.--");
00743             evbLCD->writeText(0,4,font_6x8,txtstr, strlen(txtstr));
00744             res=false;
00745         }else{
00746             sprintf(lora_press_string, "%ld.%02d", num_whole, num_frac);
00747             evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
00748             res=true;
00749         }
00750     return res;
00751 }
00752 
00753 MPL3115A2* resetBaro(const MPL3115A2* oldBaro){
00754     delete oldBaro;
00755     MPL3115A2* baro = new MPL3115A2(mDoti2c);
00756     baro->testWhoAmI();
00757 
00758     printf("Resetting barometer.. %x \n\r", baro->getStatus() );    
00759     baro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16,
00760                            MPL3115A2::AT_1);
00761     evbBaro->clearMinMaxRegs();
00762     return baro;
00763 }