mDot EVB to M2X demo.
Dependencies: DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B libmDot mbed-rtos mbed-src
Fork of MTDOT-EVBDemo by
Diff: main.cpp
- Revision:
- 7:c9d37522b975
- Parent:
- 6:c212427700af
- Child:
- 8:5b33eee8e2a5
diff -r c212427700af -r c9d37522b975 main.cpp --- a/main.cpp Mon Jul 20 16:17:19 2015 +0000 +++ b/main.cpp Mon Jul 20 20:35:44 2015 +0000 @@ -32,6 +32,8 @@ * * 1.05 JCM 7/20/15 Integrate Senet Public network demo * + * 1.06 JCM 7/20/15 Clean up code, send message using clock + * */ #include "mbed.h" @@ -135,11 +137,13 @@ SPI mDotspi(PA_7,PA_6,PA_5); // mDot external SPI mDot11, mDot4, and mDot18 -uint8_t result, pckt_time=10; +uint8_t result; +uint8_t tx_interval = 10; char data; unsigned char test; char txtstr[64]; -int32_t num_whole, mdot_ret; +int32_t num_whole; +int32_t mdot_ret; uint32_t pressure; int16_t num_frac; @@ -150,12 +154,12 @@ MMA845x_DATA accel_data; MPL3115A2_DATA baro_data; uint16_t lux_data; -MMA845x* evbAccel; -MPL3115A2* evbBaro; -ISL29011* evbAmbLight; -NCP5623B* evbBackLight; -DOGS102* evbLCD; -mDot* mdot_radio; +MMA845x *evbAccel; +MPL3115A2 *evbBaro; +ISL29011 *evbAmbLight; +NCP5623B *evbBackLight; +DOGS102 *evbLCD; +mDot *mdot_radio; convert32 convertl; convert16 converts; @@ -170,15 +174,17 @@ void pb2_debounce(void const *args); Thread* thread_3; -void log_error(mDot* dot, const char* msg, int32_t retval); +void log_error(mDot *dot, const char *msg, int32_t retval); -void config_pkt_xmit (void const *args); +void config_pkt_xmit(void const *args); int main() { std::vector<uint8_t> lora_pl; std::vector<uint8_t> mdot_EUI; - uint16_t i = 0; + int count; + int loops; + int i; debugUART.baud(115200); // mDotUART.baud(9600); // mdot UART unused but available on external connector @@ -188,7 +194,7 @@ thread_3 = new Thread(config_pkt_xmit); // start thread that sends LoRa packet when SW2 pressed - evbAccel = new MMA845x(mDoti2c,MMA845x::SA0_VSS); // setup Accelerometer + evbAccel = new MMA845x(mDoti2c, MMA845x::SA0_VSS); // setup Accelerometer evbBaro = new MPL3115A2(mDoti2c); // setup Barometric sensor evbAmbLight = new ISL29011(mDoti2c); // Setup Ambient Light Sensor evbBackLight = new NCP5623B(mDoti2c); // setup backlight and LED 2 driver chip @@ -219,7 +225,7 @@ * mode to PullNone */ mDot09.mode(PullUp); - + mDot09.enable_irq(); /* @@ -229,24 +235,24 @@ mDot15.mode(PullUp); mDot16.mode(PullUp); - printf("font table address %p\n\r",&font_6x8); - printf("bitmap address %p\n\r",&MultiTech_Logo); + printf("font table address %p\n\r", &font_6x8); + printf("bitmap address %p\n\r", &MultiTech_Logo); -// Setup and display logo on LCD + // Setup and display logo on LCD evbLCD->startUpdate(); - evbLCD->writeBitmap(0,0,MultiTech_Logo); + evbLCD->writeBitmap(0, 0, MultiTech_Logo); sprintf(txtstr,"MTDOT"); - evbLCD->writeText(24,3,font_6x8,txtstr,strlen(txtstr)); + evbLCD->writeText(24, 3, font_6x8, txtstr, strlen(txtstr)); sprintf(txtstr,"Evaluation"); - evbLCD->writeText(24,4,font_6x8,txtstr,strlen(txtstr)); + evbLCD->writeText(24, 4, font_6x8, txtstr, strlen(txtstr)); sprintf(txtstr,"Board"); - evbLCD->writeText(24,5,font_6x8,txtstr,strlen(txtstr)); + evbLCD->writeText(24, 5, font_6x8, txtstr, strlen(txtstr)); evbLCD->endUpdate(); - printf("\n\r setup mdot\n\r"); + printf("\n\r setup mdot\n\r"); // get a mDot handle mdot_radio = mDot::getInstance(); @@ -262,8 +268,7 @@ // Read node ID mdot_EUI = mdot_radio->getDeviceId(); printf("mDot EUI = "); - - for (i=0; i<mdot_EUI.size(); i++) { + for (i = 0; i < mdot_EUI.size(); i++) { printf("%02x ", mdot_EUI[i]); } printf("\n\r"); @@ -309,12 +314,12 @@ printf("joining network\r\n"); while (((mdot_ret = mdot_radio->joinNetwork()) != mDot::MDOT_OK) && (!exit_program)) { log_error(mdot_radio,"failed to join network:", mdot_ret); - if (mdot_radio->getFrequencyBand() == mDot::FB_868){ + if (mdot_radio->getFrequencyBand() == mDot::FB_868) { mdot_ret = mdot_radio->getNextTxMs(); } else { mdot_ret = 0; - } - printf("delay = %lu\n\r",mdot_ret); + } + printf("delay = %lu\n\r", mdot_ret); osDelay(mdot_ret + 1); } @@ -325,17 +330,16 @@ printf("Exiting program\n\r"); evbLCD->clearBuffer(); sprintf(txtstr,"Exiting Program"); - evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr)); + evbLCD->writeText(0, 4, font_6x8,txtstr,strlen(txtstr)); exit(1); } - } else { printf("radio setup failed\n\r"); //exit(1); } osDelay(200); - evbBackLight->setPWM(NCP5623B::LED_3,16); // enable LED2 on EVB and set to 50% PWM + evbBackLight->setPWM(NCP5623B::LED_3, 16); // enable LED2 on EVB and set to 50% PWM // sets LED2 to 50% max current evbBackLight->setLEDCurrent(16); @@ -365,8 +369,8 @@ * Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz * normal oversample mode, High pass filter off */ - evbAccel->setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF, - MMA845x::DR_1_56,MMA845x::OS_NORMAL,MMA845x::HPF_OFF ); + evbAccel->setCommonParameters(MMA845x::RANGE_8g, MMA845x::RES_MAX,MMA845x::LN_OFF, + MMA845x::DR_1_56,MMA845x::OS_NORMAL,MMA845x::HPF_OFF); /* * Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition. @@ -402,16 +406,10 @@ printf("Exiting program\n\r"); evbLCD->clearBuffer(); sprintf(txtstr,"Exiting Program"); - evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr)); + evbLCD->writeText(0, 4, font_6x8, txtstr, strlen(txtstr)); exit(1); } - /* - * Main data acquisition loop - */ - pckt_time = 10; - i = 0; - if (!senet_demo) { lora_pl.clear(); lora_pl.push_back(19); @@ -432,17 +430,29 @@ } } - do { + loops = 0; + time_t tx_next = time(NULL) + tx_interval; + + /* + * Main data acquisition loop + */ + while (!exit_program) { + osDelay(200); + evbLCD->startUpdate(); evbLCD->clearBuffer(); /* * Test Accelerometer XYZ data ready bit to see if acquisition complete */ + count = 1; do { osDelay(100); // allows other threads to process result = evbAccel->getStatus(); - } while ((result & MMA845x::XYZDR) == 0 ); + if ((count++ % 10) == 0) { + printf("waiting on accelerometer reading\r\n"); + } + } while ((result & MMA845x::XYZDR) == 0); /* * Retrieve and print out accelerometer data @@ -450,13 +460,13 @@ accel_data = evbAccel->getXYZ(); sprintf(txtstr,"Accelerometer"); - evbLCD->writeText(0,0,font_6x8,txtstr,strlen(txtstr)); + evbLCD->writeText(0, 0, font_6x8, txtstr, strlen(txtstr)); sprintf(txtstr, "x = %d", accel_data._x); - evbLCD->writeText(20,1,font_6x8,txtstr,strlen(txtstr)); + evbLCD->writeText(20, 1, font_6x8, txtstr, strlen(txtstr)); sprintf(txtstr, "y = %d", accel_data._y); - evbLCD->writeText(20,2,font_6x8,txtstr,strlen(txtstr)); + evbLCD->writeText(20, 2, font_6x8, txtstr, strlen(txtstr)); sprintf(txtstr, "z = %d", accel_data._z ); - evbLCD->writeText(20,3,font_6x8,txtstr,strlen(txtstr)); + evbLCD->writeText(20, 3, font_6x8, txtstr, strlen(txtstr)); // convert to simple position value for use in send/recv if (accel_data._x > 500 && accel_data._z < 500) { @@ -477,10 +487,14 @@ /* * Test barometer device status to see if acquisition is complete */ + count = 1; do { osDelay(100); // allows other threads to process result = evbBaro->getStatus(); - } while ((result & MPL3115A2::PTDR) == 0 ); + if ((count++ % 10) == 0) { + printf("waiting on barometer reading\r\n"); + } + } while ((result & MPL3115A2::PTDR) == 0); /* * Retrieve and print out barometric pressure @@ -489,10 +503,10 @@ num_whole = pressure >> 2; // 18 bit integer significant num_frac = (pressure & 0x3) * 25; // 2 bit fractional 0.25 per bit sprintf(txtstr,"Press=%ld.%02d Pa", num_whole, num_frac); - evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr)); + evbLCD->writeText(0, 4, font_6x8, txtstr, strlen(txtstr)); /* - * Trigger a Altitude reading + * Trigger an Altitude reading */ evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16, MPL3115A2::AT_1); @@ -501,10 +515,14 @@ /* * Test barometer device status to see if acquisition is complete */ + count = 1; do { osDelay(100); // allows other threads to process result = evbBaro->getStatus(); - } while ((result & MPL3115A2::PTDR) == 0 ); + if ((count++ % 10) == 0) { + printf("waiting on temperature reading\r\n"); + } + } while ((result & MPL3115A2::PTDR) == 0); /* * Retrieve and print out altitude and temperature @@ -513,12 +531,12 @@ baro_data._baro /= 4096; // convert 32 bit signed to 20 bit signed value num_whole = baro_data._baro / 16; // 18 bit signed significant integer num_frac = (baro_data._baro & 0xF) * 625 / 100; // 4 bit fractional .0625 per bit - sprintf(txtstr,"Alti=%ld.%03d m", num_whole, num_frac); + sprintf(txtstr, "Alti=%ld.%03d m", num_whole, num_frac); evbLCD->writeText(0,5,font_6x8,txtstr,strlen(txtstr)); num_whole = baro_data._temp / 16; // 8 bit signed significant integer num_frac = (baro_data._temp & 0x0F) * 625 / 10; // 4 bit fractional .0625 per bit - sprintf(txtstr,"Temp=%ld.%03d C", num_whole, num_frac); - evbLCD->writeText(0,6,font_6x8,txtstr,strlen(txtstr)); + sprintf(txtstr, "Temp=%ld.%03d C", num_whole, num_frac); + evbLCD->writeText(0, 6, font_6x8, txtstr, strlen(txtstr)); /* * retrieve and print out Ambient Light level @@ -527,12 +545,13 @@ num_whole = lux_data * 24 / 100; // 16000 lux full scale .24 lux per bit num_frac = lux_data * 24 % 100; sprintf(txtstr, "Light=%ld.%02d lux", num_whole, num_frac); - evbLCD->writeText(0,7,font_6x8,txtstr,strlen(txtstr)); + evbLCD->writeText(0, 7, font_6x8, txtstr, strlen(txtstr)); evbLCD->endUpdate(); - printf("finished iteration %d\n\r",(++i)); - if (i % pckt_time == 0) { // check packet counter will send packet every 2-5-10 data collection loops + if (time(NULL) > tx_next) { + tx_next = time(NULL) + tx_interval; + lora_pl.clear(); if (senet_demo) { @@ -598,14 +617,16 @@ } } } - } while(!exit_program && (i < 65000)); + + printf("finished iteration %d\n\r", loops++); + } evbBaro->triggerOneShot(); do { osDelay(200); // allows other threads to process result = evbBaro->getStatus(); - } while ((result & MPL3115A2::PTDR) == 0 ); + } while ((result & MPL3115A2::PTDR) == 0); baro_data = evbBaro->getAllData(true); printf ("minBaro=%ld maxBaro=%ld minTemp=%d maxTemp=%d\n\r", baro_data._minbaro, baro_data._maxbaro, @@ -615,8 +636,7 @@ evbLCD->clearBuffer(); sprintf(txtstr,"Exiting Program"); - evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr)); - + evbLCD->writeText(0, 4, font_6x8, txtstr, strlen(txtstr)); } /* @@ -643,8 +663,10 @@ pb1_low = false; } - if (count == 5) + if (count == 5) { + printf("pb1_debounce: setting exit_program\r\n"); exit_program = true; + } Thread::wait(5); } @@ -677,9 +699,11 @@ } if (count == 5) { - if (pckt_time >= 5) - pckt_time /= 2; - else pckt_time = 20; + if (tx_interval >= 5) { + tx_interval /= 2; + } else { + tx_interval = 20; + } thread_3->signal_set(0x10); // signal config_pkt_xmit to send packet } @@ -691,7 +715,7 @@ /* * Function that print clear text verion of mDot errors */ -void log_error(mDot* dot, const char* msg, int32_t retval) +void log_error(mDot *dot, const char *msg, int32_t retval) { printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str()); } @@ -699,7 +723,7 @@ /* * Thread that is triggered by SW2 ISR. Sends a packet to the LoRa server with the new Packet Transmission time setting */ -void config_pkt_xmit (void const *args) +void config_pkt_xmit(void const *args) { std::vector<uint8_t> lora_pl; @@ -707,9 +731,10 @@ Thread::signal_wait(0x10); // wait for pb2ISR to signal send lora_pl.clear(); - lora_pl.push_back(15); // key for Configuration data (packet transmission timer) + // Send packet transmission interval + lora_pl.push_back(15); lora_pl.push_back(1); - lora_pl.push_back(pckt_time); + lora_pl.push_back(tx_interval); if ((mdot_ret = mdot_radio->send(lora_pl)) != mDot::MDOT_OK) { log_error(mdot_radio, "failed to send config data", mdot_ret);