UDK + Shield

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

Fork of MTDOT-EVBDemo_Senet by Dave Kjendal

Revision:
4:9ea6ae34157c
Parent:
3:68e974f5f532
Child:
5:a33dcda3507b
--- a/main.cpp	Wed Jul 15 14:20:33 2015 +0000
+++ b/main.cpp	Wed Sep 23 14:31:59 2015 +0000
@@ -101,16 +101,16 @@
  */
 static std::string config_network_name = "TAB-CubeNet";
 static std::string config_network_pass = "1nt3gral";
-static uint8_t config_frequency_sub_band = 5;
+static uint8_t config_frequency_sub_band = 1;
 
-/*  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;
-*/
+/*  config_app_id and config_app_key are for public networks. */
+static uint8_t app_id[8] = {0x00,0x25,0x0C,0x00,0x00,0x01,0x00,0x01};
+std::vector<uint8_t> config_app_id(app_id,app_id+sizeof(app_id)/sizeof(uint8_t));
+static uint8_t app_key[16] = //9C49 - {0xB4,0xAD,0x1A,0x25,0x69,0x7F,0xF6,0x8E,0xD3,0x4B,0x83,0xC4,0xB6,0xC0,0xF2,0x3C};
+							 {0xA0,0xC8,0x3D,0xD1,0x7B,0x32,0x97,0x1B,0x71,0xDC,0x53,0xB5,0x4D,0x89,0xC6,0x19};
+std::vector<uint8_t> config_app_key(app_key,app_key+sizeof(app_key)/sizeof(uint8_t));
 
-uint8_t result, pckt_time=10;
+uint8_t result, pckt_time=100;
 char data;
 unsigned char test;
 char txtstr[17];
@@ -118,6 +118,10 @@
 uint32_t pressure;
 int16_t num_frac;
 
+uint8_t position_value = 0xFF; // 00 unknown, 01 is flat, 02 is vertical
+uint8_t reflected_value = 0xFE;
+bool    position_changed = true;
+
 bool exit_program = false;
 
 MMA845x_DATA accel_data;
@@ -154,7 +158,7 @@
     std::vector<uint8_t> mdot_EUI;
     uint16_t i = 0;
 
-    debugUART.baud(921600);
+    debugUART.baud(115200);
 //  mDotUART.baud(9600);    // mdot UART unused but available on external connector
 
     Thread thread_1(pb1_debounce);						// threads for de-bouncing pushbutton switches
@@ -249,8 +253,8 @@
  * 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) {
+       printf("setting Public Network Mode\r\n");
+       if ((mdot_ret = mdot_radio->setPublicNetwork(true)) != mDot::MDOT_OK) {
            log_error(mdot_radio, "failed to set Public Network Mode", mdot_ret);
        }
 
@@ -272,8 +276,8 @@
 // 		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) {
+//        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);
         }
 
@@ -285,8 +289,8 @@
 // 		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) {
+//        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);
         }
 
@@ -424,7 +428,28 @@
         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));
-
+        // convert to simple position value for use in send/recv
+		if((accel_data._x > 500)&&(accel_data._z < 500))
+		{
+			if(position_value != 0x02)
+				position_changed = true;
+			position_value = 0x02;
+		}
+		else if((accel_data._x < 500)&&(accel_data._z > 500))
+		{
+			if(position_value != 0x01)
+				position_changed = true;
+			position_value = 0x01;
+		}
+		else
+		{
+			if(position_value != 0x00)
+				position_changed = true;
+			position_value= 0x00;
+		}
+		if(position_changed)
+			evbBackLight->setLEDCurrent(0);
+		
         /*
          * Trigger a Pressure reading
          */
@@ -492,13 +517,18 @@
 
         if (i % pckt_time == 0) { // check packet counter will send packet every 2-5-10 data collection loops
             mdot_data.clear();
-            mdot_data.push_back(0x0E);			// key for Current Acceleration 3-Axis Value
+            // we will send a simple byte descriptor of the current position of the device: 01 is laying flat, 02 is vertically oriented
+            mdot_data.push_back(0x00);
+            mdot_data.push_back(position_value);
+			/*mdot_data.push_back(0x0E);			// key for Current Acceleration 3-Axis Value
             converts.f_s = accel_data._x *4;		// shift data 2 bits while retaining sign
             mdot_data.push_back(converts.t_u[1]);	// get 8 MSB of 14 bit value
             converts.f_s = accel_data._y * 4;		// shift data 2 bits while retaining sign
             mdot_data.push_back(converts.t_u[1]);	// get 8 MSB of 14 bit value
             converts.f_s = accel_data._z * 4;		// shift data 2 bits while retaining sign
             mdot_data.push_back(converts.t_u[1]);	// get 8 MSB of 14 bit value
+            */
+            /*
             mdot_data.push_back(0x08);			// key for Current Pressure Value
             convertl.f_u = pressure;				// pressure data is 20 bits unsigned
             mdot_data.push_back(convertl.t_u[2]);
@@ -512,11 +542,31 @@
             converts.f_s = baro_data._temp;		// temperature is signed 12 bit
             mdot_data.push_back(converts.t_u[1]);
             mdot_data.push_back(converts.t_u[0]);
-
-            if ((mdot_ret = mdot_radio->send(mdot_data)) != mDot::MDOT_OK) {
-                log_error(mdot_radio, "failed to send", mdot_ret);
-            } else {
-                printf("successfully sent data to gateway\r\n");
+*/
+			if((reflected_value  != position_value)||(position_changed))
+			{
+	            if ((mdot_ret = mdot_radio->send(mdot_data)) != mDot::MDOT_OK) {
+	                log_error(mdot_radio, "failed to send", mdot_ret);
+	            } else {
+	                printf("successfully sent data to gateway\r\n");
+	                mdot_data.clear();
+	                if ((mdot_ret = mdot_radio->recv(mdot_data)) != mDot::MDOT_OK) {
+		                log_error(mdot_radio,"failed to recv:", mdot_ret);
+		            } else {
+		                printf("recv data: ");
+		                for(int i = 0;i < mdot_data.size();i++)
+		                	printf("%02X",mdot_data[i]);
+	                	printf("\r\n");
+	                	reflected_value = mdot_data[0];
+	                	if(reflected_value == position_value)
+	                	{
+	                		evbBackLight->setLEDCurrent(16);
+	                		position_changed = false;
+                		}
+	                	else
+	                		evbBackLight->setLEDCurrent(0);
+	                }
+	            }
             }
         }
     } while(!exit_program && (i < 65000));