Dave Kjendal / MTDOT-EVBDemo_Senet

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

Fork of MTDOT-EVBDemo by Multi-Hackers

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 /* **** replace these values with the proper public or private network settings ****
00100  * config_network_nameand config_network_pass are for private networks.
00101  */
00102 static std::string config_network_name = "TAB-CubeNet";
00103 static std::string config_network_pass = "1nt3gral";
00104 static uint8_t config_frequency_sub_band = 0;
00105 static bool config_adr_on = true;
00106 
00107 /*  config_app_id and config_app_key are for public networks. */
00108 static uint8_t app_id[8] = {0x00,0x25,0x0C,0x00,0x00,0x01,0x00,0x01};
00109 std::vector<uint8_t> config_app_id(app_id,app_id+sizeof(app_id)/sizeof(uint8_t));
00110 static uint8_t app_key[16] = {0xB4,0xAD,0x1A,0x25,0x69,0x7F,0xF6,0x8E,0xD3,0x4B,0x83,0xC4,0xB6,0xC0,0xF2,0x3C}; // 9C49
00111                               //{0xA0,0xC8,0x3D,0xD1,0x7B,0x32,0x97,0x1B,0x71,0xDC,0x53,0xB5,0x4D,0x89,0xC6,0x19};
00112                               //{0xFC,0xD1,0x8B,0x22,0x9E,0xA0,0xDA,0x4F,0x6B,0x8F,0x39,0x26,0xAF,0x07,0x8A,0x05}; //9CFF
00113                               //{0xB3,0x53,0x00,0xDA,0xF6,0x0A,0x6C,0xF9,0x54,0x57,0x79,0x50,0x6F,0x19,0xE1,0x3A}; //9C35
00114 std::vector<uint8_t> config_app_key(app_key,app_key+sizeof(app_key)/sizeof(uint8_t));
00115 
00116 uint8_t result, pckt_time=100;
00117 char data;
00118 unsigned char test;
00119 char txtstr[17];
00120 int32_t num_whole, mdot_ret;
00121 uint32_t pressure;
00122 int16_t num_frac;
00123 
00124 uint8_t position_value = 0xFF; // 00 unknown, 01 is flat, 02 is vertical
00125 uint8_t reflected_value = 0xFE;
00126 bool    position_changed = true;
00127 
00128 bool exit_program = false;
00129 
00130 MMA845x_DATA accel_data;
00131 MPL3115A2_DATA baro_data;
00132 uint16_t  lux_data;
00133 MMA845x* evbAccel;
00134 MPL3115A2* evbBaro;
00135 ISL29011* evbAmbLight;
00136 NCP5623B* evbBackLight;
00137 DOGS102* evbLCD;
00138 mDot* mdot_radio;
00139 
00140 convert32 convertl;
00141 convert16 converts;
00142 
00143 // flags for pushbutton debounce code
00144 bool pb1_low = false;
00145 bool pb2_low = false;
00146 
00147 void pb1ISR(void);
00148 void pb2ISR(void);
00149 void pb1_debounce(void const *args);
00150 void pb2_debounce(void const *args);
00151 Thread* thread_3;
00152 
00153 void log_error(mDot* dot, const char* msg, int32_t retval);
00154 
00155 void config_pkt_xmit (void const *args);
00156 
00157 int main()
00158 {
00159 
00160     std::vector<uint8_t> mdot_data;
00161     std::vector<uint8_t> mdot_EUI;
00162     uint16_t i = 0;
00163 
00164     debugUART.baud(115200);
00165 //  mDotUART.baud(9600);    // mdot UART unused but available on external connector
00166 
00167     Thread thread_1(pb1_debounce);                      // threads for de-bouncing pushbutton switches
00168     Thread thread_2(pb2_debounce);
00169 
00170     thread_3 = new Thread(config_pkt_xmit);             // start thread that sends LoRa packet when SW2 pressed
00171 
00172     evbAccel = new MMA845x(mDoti2c,MMA845x::SA0_VSS);   // setup Accelerometer
00173     evbBaro = new MPL3115A2(mDoti2c);                   // setup Barometric sensor
00174     evbAmbLight = new ISL29011(mDoti2c);                // Setup Ambient Light Sensor
00175     evbBackLight = new NCP5623B(mDoti2c);               // setup backlight and LED 2 driver chip
00176     evbLCD = new DOGS102(mDotspi, mDot17, mDot13);      // setup LCD
00177 
00178     /*
00179      *  Setup SW1 as program stop function
00180      */
00181     mDot08.disable_irq();
00182     mDot08.fall(&pb1ISR);
00183 
00184     /*
00185      *  need to call this function after rise or fall because rise/fall sets
00186      *  mode to PullNone
00187      */
00188     mDot08.mode(PullUp);
00189 
00190     mDot08.enable_irq();
00191 
00192     /*
00193      *  Setup SW2 as packet time change
00194      */
00195     mDot09.disable_irq();
00196     mDot09.fall(&pb2ISR);
00197 
00198     /*
00199      *  need to call this function after rise or fall because rise/fall sets
00200      *  mode to PullNone
00201      */
00202     mDot09.mode(PullUp);
00203  
00204     mDot09.enable_irq();
00205 
00206     /*
00207     * Setting other InterruptIn pins with Pull Ups
00208     */
00209     mDot12.mode(PullUp);
00210     mDot15.mode(PullUp);
00211     mDot16.mode(PullUp);
00212 
00213     printf("font table address %p\n\r",&font_6x8);
00214     printf("bitmap address %p\n\r",&MultiTech_Logo);
00215 
00216 // Setup and display logo on LCD
00217     evbLCD->startUpdate();
00218 
00219     evbLCD->writeBitmap(0,0,MultiTech_Logo);
00220 
00221     sprintf(txtstr,"MTDOT");
00222     evbLCD->writeText(24,3,font_6x8,txtstr,strlen(txtstr));
00223     sprintf(txtstr,"Evaluation");
00224     evbLCD->writeText(24,4,font_6x8,txtstr,strlen(txtstr));
00225     sprintf(txtstr,"Board");
00226     evbLCD->writeText(24,5,font_6x8,txtstr,strlen(txtstr));
00227 
00228     evbLCD->endUpdate();
00229 
00230    printf("\n\r setup mdot\n\r");
00231 
00232     // get a mDot handle
00233     mdot_radio = mDot::getInstance();
00234 
00235     if (mdot_radio) {
00236         // reset to default config so we know what state we're in
00237         mdot_radio->resetConfig();
00238         mdot_radio->setLogLevel(6);
00239         mdot_radio->setAntennaGain(-3);
00240 
00241         // Setting up LED1 as activity LED
00242         mdot_radio->setActivityLedPin(PB_0);
00243         mdot_radio->setActivityLedEnable(true);
00244 
00245         // Read node ID
00246         mdot_EUI = mdot_radio->getDeviceId();
00247         printf("mDot EUI = ");
00248 
00249         for (i=0; i<mdot_EUI.size(); i++) {
00250             printf("%02x ", mdot_EUI[i]);
00251         }
00252         printf("\n\r");
00253 
00254 
00255 // Setting up the mDot with network information.
00256 
00257 /*
00258  * This call sets up private or public mode on the MTDOT. Set the function to true if
00259  * connecting to a public network
00260  */
00261        printf("setting Public Network Mode\r\n");
00262        if ((mdot_ret = mdot_radio->setPublicNetwork(true)) != mDot::MDOT_OK) {
00263            log_error(mdot_radio, "failed to set Public Network Mode", mdot_ret);
00264        }
00265        mdot_radio->setTxDataRate(mDot::DR0);
00266        mdot_radio->setTxPower(14);
00267        mdot_radio->setJoinRetries(1);
00268        mdot_radio->setJoinMode(mDot::OTA);
00269 
00270 /*
00271  * Frequency sub-band is valid for NAM only and for Private networks should be set to a value
00272  * between 1-8 that matches the the LoRa gateway setting. Public networks use sub-band 0 only.
00273  * This function can be commented out for EU networks
00274  */
00275         printf("setting frequency sub band\r\n");
00276         if ((mdot_ret = mdot_radio->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
00277             log_error(mdot_radio, "failed to set frequency sub band", mdot_ret);
00278         }
00279         
00280         printf("setting ADR\r\n");
00281         if ((mdot_ret = mdot_radio->setAdr(config_adr_on)) != mDot::MDOT_OK) {
00282             log_error(mdot_radio, "failed to set ADR", mdot_ret);
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 + 10000);
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 
00419     do {
00420         evbLCD->startUpdate();
00421         evbLCD->clearBuffer();
00422 
00423         /*
00424          * Test Accelerometer XYZ data ready bit to see if acquisition complete
00425          */
00426         do {
00427             osDelay(100);           // allows other threads to process
00428             result = evbAccel->getStatus();
00429         } while ((result & MMA845x::XYZDR) == 0 );
00430 
00431         /*
00432          * Retrieve and print out accelerometer data
00433          */
00434         accel_data = evbAccel->getXYZ();
00435 
00436         sprintf(txtstr,"Accelerometer");
00437         evbLCD->writeText(0,0,font_6x8,txtstr,strlen(txtstr));
00438         sprintf(txtstr, "x = %d", accel_data._x);
00439         evbLCD->writeText(20,1,font_6x8,txtstr,strlen(txtstr));
00440         sprintf(txtstr, "y = %d", accel_data._y);
00441         evbLCD->writeText(20,2,font_6x8,txtstr,strlen(txtstr));
00442         sprintf(txtstr, "z = %d", accel_data._z );
00443         evbLCD->writeText(20,3,font_6x8,txtstr,strlen(txtstr));
00444         // convert to simple position value for use in send/recv
00445         if((accel_data._x > 500)&&(accel_data._z < 500))
00446         {
00447             if(position_value != 0x02)
00448                 position_changed = true;
00449             position_value = 0x02;
00450         }
00451         else if((accel_data._x < 500)&&(accel_data._z > 500))
00452         {
00453             if(position_value != 0x01)
00454                 position_changed = true;
00455             position_value = 0x01;
00456         }
00457         else
00458         {
00459             if(position_value != 0x00)
00460                 position_changed = true;
00461             position_value= 0x00;
00462         }
00463         if(position_changed)
00464             evbBackLight->setLEDCurrent(0);
00465         
00466         /*
00467          * Trigger a Pressure reading
00468          */
00469         evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16,
00470                                MPL3115A2::AT_1);
00471         evbBaro->triggerOneShot();
00472 
00473         /*
00474          * Test barometer device status to see if acquisition is complete
00475          */
00476         do {
00477             osDelay(100);           // allows other threads to process
00478             result = evbBaro->getStatus();
00479         } while ((result & MPL3115A2::PTDR) == 0 );
00480 
00481         /*
00482          * Retrieve and print out barometric pressure
00483          */
00484         pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value
00485         num_whole = pressure >> 2;          // 18 bit integer significant
00486         num_frac = (pressure & 0x3) * 25;       // 2 bit fractional  0.25 per bit
00487         sprintf(txtstr,"Press=%ld.%02d Pa", num_whole, num_frac);
00488         evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
00489 
00490         /*
00491          * Trigger a Altitude reading
00492          */
00493         evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16,
00494                                MPL3115A2::AT_1);
00495         evbBaro->triggerOneShot();
00496 
00497         /*
00498          * Test barometer device status to see if acquisition is complete
00499          */
00500         do {
00501             osDelay(100);           // allows other threads to process
00502             result = evbBaro->getStatus();
00503         } while ((result & MPL3115A2::PTDR) == 0 );
00504 
00505         /*
00506          * Retrieve and print out altitude and temperature
00507          */
00508         baro_data = evbBaro->getAllData(false);
00509         baro_data._baro /= 4096;                // convert 32 bit signed to 20 bit signed value
00510         num_whole = baro_data._baro / 16;       //  18 bit signed significant integer
00511         num_frac = (baro_data._baro & 0xF) * 625 / 100;     // 4 bit fractional .0625 per bit
00512         sprintf(txtstr,"Alti=%ld.%03d m", num_whole, num_frac);
00513         evbLCD->writeText(0,5,font_6x8,txtstr,strlen(txtstr));
00514         num_whole = baro_data._temp / 16;       // 8 bit signed significant integer
00515         num_frac = (baro_data._temp & 0x0F) * 625 / 100;        // 4 bit fractional .0625 per bit
00516         sprintf(txtstr,"Temp=%ld.%03d C", num_whole, num_frac);
00517         evbLCD->writeText(0,6,font_6x8,txtstr,strlen(txtstr));
00518 
00519         /*
00520          * retrieve and print out Ambient Light level
00521          */
00522         lux_data = evbAmbLight->getData();
00523         num_whole = lux_data * 24 / 100;        // 16000 lux full scale .24 lux per bit
00524         num_frac = lux_data * 24 % 100;
00525         sprintf(txtstr, "Light=%ld.%02d lux", num_whole, num_frac );
00526         evbLCD->writeText(0,7,font_6x8,txtstr,strlen(txtstr));
00527 
00528         evbLCD->endUpdate();
00529         printf("finished iteration %d\n\r",(++i));
00530 
00531         if (i % pckt_time == 0) { // check packet counter will send packet every 2-5-10 data collection loops
00532             mdot_data.clear();
00533             // we will send a simple byte descriptor of the current position of the device: 01 is laying flat, 02 is vertically oriented
00534             mdot_data.push_back(0x00);
00535             mdot_data.push_back(position_value);
00536             /*mdot_data.push_back(0x0E);            // key for Current Acceleration 3-Axis Value
00537             converts.f_s = accel_data._x *4;        // shift data 2 bits while retaining sign
00538             mdot_data.push_back(converts.t_u[1]);   // get 8 MSB of 14 bit value
00539             converts.f_s = accel_data._y * 4;       // shift data 2 bits while retaining sign
00540             mdot_data.push_back(converts.t_u[1]);   // get 8 MSB of 14 bit value
00541             converts.f_s = accel_data._z * 4;       // shift data 2 bits while retaining sign
00542             mdot_data.push_back(converts.t_u[1]);   // get 8 MSB of 14 bit value
00543             */
00544             /*
00545             mdot_data.push_back(0x08);          // key for Current Pressure Value
00546             convertl.f_u = pressure;                // pressure data is 20 bits unsigned
00547             mdot_data.push_back(convertl.t_u[2]);
00548             mdot_data.push_back(convertl.t_u[1]);
00549             mdot_data.push_back(convertl.t_u[0]);
00550             mdot_data.push_back(0x05);          // key for Current Ambient Light Value
00551             converts.f_u = lux_data;                // data is 16 bits unsigned
00552             mdot_data.push_back(converts.t_u[1]);
00553             mdot_data.push_back(converts.t_u[0]);
00554             mdot_data.push_back(0x0B);          // key for Current Temperature Value
00555             converts.f_s = baro_data._temp;     // temperature is signed 12 bit
00556             mdot_data.push_back(converts.t_u[1]);
00557             mdot_data.push_back(converts.t_u[0]);
00558 */
00559             if((reflected_value  != position_value)||(position_changed)||(0 == (i%100)))
00560             {
00561                 if ((mdot_ret = mdot_radio->send(mdot_data)) != mDot::MDOT_OK) {
00562                     log_error(mdot_radio, "failed to send", mdot_ret);
00563                 } else {
00564                     printf("successfully sent data to gateway\r\n");
00565                     mdot_data.clear();
00566                     if ((mdot_ret = mdot_radio->recv(mdot_data)) != mDot::MDOT_OK) {
00567                         log_error(mdot_radio,"failed to recv:", mdot_ret);
00568                     } else {
00569                         printf("recv data: ");
00570                         for(int i = 0;i < mdot_data.size();i++)
00571                             printf("%02X",mdot_data[i]);
00572                         printf("\r\n");
00573                         reflected_value = mdot_data[0];
00574                         if(reflected_value == position_value)
00575                         {
00576                             evbBackLight->setLEDCurrent(16);
00577                             position_changed = false;
00578                         }
00579                         else
00580                             evbBackLight->setLEDCurrent(0);
00581                     }
00582                 }
00583             }
00584         }
00585     } while(!exit_program /*&& (i < 65000)*/);
00586 
00587     evbBaro->triggerOneShot();
00588 
00589     do {
00590         osDelay(200);           // allows other threads to process
00591         result = evbBaro->getStatus();
00592     } while ((result & MPL3115A2::PTDR) == 0 );
00593 
00594     baro_data = evbBaro->getAllData(true);
00595     printf ("minBaro=%ld maxBaro=%ld minTemp=%d maxTemp=%d\n\r", baro_data._minbaro, baro_data._maxbaro,
00596             baro_data._mintemp, baro_data._maxtemp);
00597 
00598     printf("End of Test\n\r");
00599 
00600     evbLCD->clearBuffer();
00601     sprintf(txtstr,"Exiting Program");
00602     evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
00603 
00604 }
00605 
00606 /*
00607  * Sets pb1_low flag. Slag is cleared in pb1_debounce thread
00608  */
00609 void pb1ISR(void)
00610 {
00611     if (!pb1_low)
00612         pb1_low = true;
00613 }
00614 
00615 /*
00616  * Debounces pb1. Also exits program if pushbutton 1 is pressed
00617  */
00618 void pb1_debounce(void const *args)
00619 {
00620 
00621     static uint8_t count = 0;
00622 
00623     while (true) {
00624 
00625         if (pb1_low && (mDot08 == 0))
00626             count++;
00627         else {
00628             count = 0;
00629             pb1_low = false;
00630         }
00631         
00632         if (count == 5) 
00633             exit_program = true;
00634         
00635         Thread::wait(5);
00636     }
00637 }
00638 
00639 /*
00640  * Sets pb2_low flag. Flag is cleared in pb2_debounce thread
00641  */
00642 void pb2ISR(void)
00643 {
00644     if (!pb2_low)
00645         pb2_low = true;
00646 }
00647 
00648 /*
00649  * Debounces pb2. Also changes packet transmit time to every other,
00650  * every fifth, or every tenth sample when SW2 pushed
00651  * Also triggers a thread to transmit a configuration packet
00652  */
00653 void pb2_debounce(void const *args)
00654 {
00655 
00656     static uint8_t count = 0;
00657 
00658     while (true) {
00659 
00660         if (pb2_low && (mDot09 == 0))
00661             count++;
00662         else {
00663             count = 0;
00664             pb2_low = false;
00665         }
00666         
00667         if (count == 5){
00668 
00669             if (pckt_time >= 5)
00670                 pckt_time /= 2;
00671             else pckt_time = 20;
00672 
00673             //thread_3->signal_set(0x10);       // signal config_pkt_xmit to send packet
00674             position_changed = true;
00675         }
00676         
00677         Thread::wait(5);
00678     }
00679  }
00680 
00681 /*
00682  *  Function that print clear text verion of mDot errors
00683  */
00684 void log_error(mDot* dot, const char* msg, int32_t retval)
00685 {
00686     printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str());
00687 }
00688 
00689 /*
00690  * Thread that is triggered by SW2 ISR. Sends a packet to the LoRa server with the new Packet Transmission time setting
00691  */
00692 void config_pkt_xmit (void const *args)
00693 {
00694 
00695     std::vector<uint8_t> data;
00696 
00697     while (true) {
00698         Thread::signal_wait(0x10);      // wait for pb2ISR to signal send
00699         data.clear();
00700         data.push_back(0x0F);           // key for Configuration data (packet transmission timer)
00701         data.push_back(pckt_time);
00702 
00703         if ((mdot_ret = mdot_radio->send(data)) != mDot::MDOT_OK) {
00704             log_error(mdot_radio, "failed to send config data", mdot_ret);
00705         } else {
00706             printf("sent config data to gateway\r\n");
00707         }
00708     }
00709 }