mDot EVB to M2X demo.

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

Fork of MTDOT-EVBDemo by Multi-Hackers

Revision:
2:75adc72aa6a0
Parent:
1:ac9595d0f0e7
Child:
3:68e974f5f532
--- a/main.cpp	Mon Jul 06 21:10:10 2015 +0000
+++ b/main.cpp	Thu Jul 09 20:32:03 2015 +0000
@@ -2,7 +2,7 @@
  * @file    main.cpp
  * @brief   Main application for mDot-EVB demo
  * @author  Tim Barr  MultiTech Systems Inc.
- * @version 1.01
+ * @version 1.02
  * @see
  *
  * Copyright (c) 2015
@@ -20,6 +20,11 @@
  * limitations under the License.
  *
  * 1.01 TAB 7/6/15 Removed NULL pointer from evbAmbientLight creation call.
+ *
+ * 1.02 TAB 7/9/15 Send logo to LCD before attempting connection to LoRa network. Added
+ *					information on setting up for public LoRa network. Moved SW setup to
+ *					beginning of main. Removed printf call from ISR functions. Added
+ *					additional checks for exit_program.
  */
 
 #include "mbed.h"
@@ -88,11 +93,20 @@
 
 SPI mDotspi(PA_7,PA_6,PA_5);					// mDot external SPI mDot11, mDot4, and mDot18
 
-// replace these values with the proper network settings
+/* **** replace these values with the proper public or private network settings ****
+ * config_network_nameand config_network_pass are for private networks.
+ */
 static std::string config_network_name = "TAB-CubeNet";
 static std::string config_network_pass = "1nt3gral";
 static uint8_t config_frequency_sub_band = 5;
 
+/*  config_app_id and config_app_key are for public networks.
+static uint8_t app_id[8] = {0x00,0x01,0x02,0x03,0x0A,0x0B,0x0C,0x0D};
+std::vector<uint8_t> config_app_id;
+static uint8_t app_key[16] = {0x00,0x01,0x02,0x03,0x0A,0x0B,0x0C,0x0D};
+std::vector<uint8_t> config_app_key;
+*/
+
 uint8_t result, pckt_time=10;
 char data;
 unsigned char test;
@@ -135,14 +149,67 @@
     debugUART.baud(921600);
 //  mDotUART.baud(9600);    // mdot UART unused but available on external connector
 
-    thread1 = new Thread(config_pkt_xmit);
+    thread1 = new Thread(config_pkt_xmit);				// start thread that sends LoRa packet when SW2 pressed
+
     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
     evbLCD = new DOGS102(mDotspi, mDot17, mDot13);		// setup LCD
 
-    printf("\n\r setup mdot\n\r");
+    /*
+     *  Setup SW1 as program stop function
+     */
+    mDot08.disable_irq();
+    mDot08.fall(&pb1ISR);
+
+    /*
+     *  need to call this function after rise or fall because rise/fall sets
+     *  mode to PullNone
+     */
+    mDot08.mode(PullUp);
+
+    mDot08.enable_irq();
+
+    /*
+     *  Setup SW2 as packet time change
+     */
+    mDot09.disable_irq();
+    mDot09.fall(&pb2ISR);
+
+    /*
+     *  need to call this function after rise or fall because rise/fall sets
+     *  mode to PullNone
+     */
+    mDot09.mode(PullUp);
+ 
+    mDot09.enable_irq();
+
+    /*
+    * Setting other InterruptIn pins with Pull Ups
+    */
+    mDot12.mode(PullUp);
+    mDot15.mode(PullUp);
+    mDot16.mode(PullUp);
+
+    printf("font table address %p\n\r",&font_6x8);
+    printf("bitmap address %p\n\r",&MultiTech_Logo);
+
+// Setup and display logo on LCD
+    evbLCD->startUpdate();
+
+    evbLCD->writeBitmap(0,0,MultiTech_Logo);
+
+    sprintf(txtstr,"MTDOT");
+    evbLCD->writeText(24,3,font_6x8,txtstr,strlen(txtstr));
+    sprintf(txtstr,"Evaluation");
+    evbLCD->writeText(24,4,font_6x8,txtstr,strlen(txtstr));
+    sprintf(txtstr,"Board");
+    evbLCD->writeText(24,5,font_6x8,txtstr,strlen(txtstr));
+
+    evbLCD->endUpdate();
+
+   printf("\n\r setup mdot\n\r");
 
     // get a mDot handle
     mdot_radio = mDot::getInstance();
@@ -165,36 +232,79 @@
         printf("\n\r");
 
 
-// set up the mDot with our network information
+// Setting up the mDot with network information.
+
+/*
+ * This call sets up private or public mode on the MTDOT. Set the function to true if
+ * connecting to a public network
+ */
+       printf("setting Private Network Mode\r\n");
+       if ((mdot_ret = mdot_radio->setPublicNetwork(false)) != mDot::MDOT_OK) {
+           log_error(mdot_radio, "failed to set Public Network Mode", mdot_ret);
+       }
+
+/*
+ * Frequency sub-band is valid for NAM only and for Private networks should be set to a value
+ * between 1-8 that matches the the LoRa gateway setting. Public networks use sub-band 0 only.
+ * This function can be commented out for EU networks
+ */
         printf("setting frequency sub band\r\n");
         if ((mdot_ret = mdot_radio->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
             log_error(mdot_radio, "failed to set frequency sub band", mdot_ret);
         }
 
+/*
+ * setNetworkName is used for private networks.
+ * Use setNetworkID(AppID) for public networks
+ */
+
+// 		config_app_id.assign(app_id,app_id+7);
+
         printf("setting network name\r\n");
         if ((mdot_ret = mdot_radio->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
+//      if ((mdot_ret = mdot_radio->setNetworkID(config_app_id)) != mDot::MDOT_OK) {
             log_error(mdot_radio, "failed to set network name", mdot_ret);
         }
 
+/*
+ * setNetworkPassphrase is used for private networks
+ * Use setNetworkKey for public networks
+ */
+
+// 		config_app_key.assign(app_key,app_key+15);
+
         printf("setting network password\r\n");
         if ((mdot_ret = mdot_radio->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
+//      if ((mdot_ret = mdot_radio->setNetworkKey(config_app_key)) != mDot::MDOT_OK) {
             log_error(mdot_radio, "failed to set network password", mdot_ret);
         }
 
         // attempt to join the network
         printf("joining network\r\n");
-        while ((mdot_ret = mdot_radio->joinNetwork()) != mDot::MDOT_OK) {
+        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){
             	mdot_ret = mdot_radio->getNextTxMs();
             }
         	else {
         		mdot_ret = 0;
-        	}
+         	}
         		
             printf("delay = %lu\n\r",mdot_ret);
             osDelay(mdot_ret + 1);
         }
+
+        /*
+         * Check for PB1 press during network join attempt
+         */
+        if (exit_program) {
+            printf("Exiting program\n\r");
+            evbLCD->clearBuffer();
+            sprintf(txtstr,"Exiting Program");
+            evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
+            exit(1);
+        }
+
     } else {
         printf("radio setup failed\n\r");
         //exit(1);
@@ -203,64 +313,16 @@
     osDelay(200);
     evbBackLight->setPWM(NCP5623B::LED_3,16); // enable LED2 on EVB and set to 50% PWM
 
-    /*
-     *  Setup SW1 as program stop function
-     */
-    mDot08.disable_irq();
-    mDot08.fall(&pb1ISR);
-
-    /*
-     *  need to call this function after rise or fall because rise/fall sets
-     *  mode to PullNone
-     */
-    mDot08.mode(PullUp);
-    mDot08.enable_irq();
-
-    /*
-     *  Setup SW2 as packet time change
-     */
-    mDot09.disable_irq();
-    mDot09.fall(&pb2ISR);
-
-    /*
-     *  need to call this function after rise or fall because rise/fall sets
-     *  mode to PullNone
-     */
-    mDot09.mode(PullUp);
-    mDot09.enable_irq();
-
-    /*
-    * Setting other InterruptIn pins with Pull Ups
-    */
-    mDot12.mode(PullUp);
-    mDot15.mode(PullUp);
-    mDot16.mode(PullUp);
-
-
-    // sets LED2 to ramp up to 50% max current at 32 mS per step
+    // sets LED2 to 50% max current
     evbBackLight->setLEDCurrent(16);
 
-    printf("font table address %p\n\r",&font_6x8);
-    printf("bitmap address %p\n\r",&MultiTech_Logo);
-
     printf("Start of Test\n\r");
 
-    evbLCD->startUpdate();
-    evbLCD->writeBitmap(0,0,MultiTech_Logo);
-
-    sprintf(txtstr,"MTDOT");
-    evbLCD->writeText(24,3,font_6x8,txtstr,strlen(txtstr));
-    sprintf(txtstr,"Evaluation");
-    evbLCD->writeText(24,4,font_6x8,txtstr,strlen(txtstr));
-    sprintf(txtstr,"Board");
-    evbLCD->writeText(24,5,font_6x8,txtstr,strlen(txtstr));
+    osDelay (500);			// allows other threads to process
+    printf("shutdown LED:\n\r");
+    evbBackLight->shutdown();
 
     osDelay (500);			// allows other threads to process
-    printf("shutdown LEDs and update LCD:\n\r");
-    evbBackLight->shutdown();
-    evbLCD->endUpdate();
-
-    osDelay (1000);			// allows other threads to process
     printf("Turn on LED2\n\r");
     evbBackLight->setLEDCurrent(16);
 
@@ -310,6 +372,17 @@
     evbBackLight->setLEDCurrent(0);
 
     /*
+     * Check for PB1 press during network join attempt
+     */
+    if (exit_program) {
+        printf("Exiting program\n\r");
+        evbLCD->clearBuffer();
+        sprintf(txtstr,"Exiting Program");
+        evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
+        exit(1);
+    }
+
+    /*
      * Main data acquisition loop
      */
     pckt_time = 10;
@@ -435,7 +508,7 @@
                 printf("successfully sent data to gateway\r\n");
             }
         }
-    } while(!exit_program | (i > 65000));
+    } while(!exit_program && (i < 65000));
 
     evbBaro->triggerOneShot();
 
@@ -450,6 +523,10 @@
 
     printf("End of Test\n\r");
 
+    evbLCD->clearBuffer();
+    sprintf(txtstr,"Exiting Program");
+    evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
+
 }
 
 /*
@@ -457,35 +534,36 @@
  */
 void pb1ISR(void)
 {
-    printf("calling ISR for SW1\n\r");
 
     exit_program = true;
 }
 
 /*
- * changes packet transmit time to every other, every fifth, or every tenth sample when button 2 pushed
+ * changes packet transmit time to every other, every fifth, or every tenth sample when SW2 pushed
  * Also triggers a thread to transmit a configuration packet
  */
 void pb2ISR(void)
 {
 
-    printf("calling ISR for SW2\n\r");
-
     if (pckt_time >= 5)
         pckt_time /= 2;
     else pckt_time = 20;
 
     thread1->signal_set(0x10);		// signal config_pkt_xmit to send packet
 
-    printf ("pckt_time = %d\n\r",pckt_time);
-
 }
 
+/*
+ *  Function that print clear text verion of mDot errors
+ */
 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());
 }
 
+/*
+ * 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)
 {