Senet / Mbed OS MTDOT-UDKDemo

Dependencies:   libmDot-mbed5 DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B X_NUCLEO_IKS01A1 Senet_Packet

Fork of MTDOT-UDKDemo_Senet by canuck lehead

Files at this revision

API Documentation at this revision

Comitter:
Shaun Nelson
Date:
Thu Aug 24 17:56:53 2017 -0400
Branch:
develop
Child:
28:4fd8a894a403
Commit message:
Reimplement for mbed 5
Move bsp to board files
UDK implemented
EVB in progress

Changed in this revision

board/board.h Show annotated file Show diff for this revision Revisions of this file
board/board_common.cpp Show annotated file Show diff for this revision Revisions of this file
board/evb/DOGS102.lib Show annotated file Show diff for this revision Revisions of this file
board/evb/ISL29011.lib Show annotated file Show diff for this revision Revisions of this file
board/evb/MMA845x.lib Show annotated file Show diff for this revision Revisions of this file
board/evb/MPL3115A2.lib Show annotated file Show diff for this revision Revisions of this file
board/evb/NCP5623B.lib Show annotated file Show diff for this revision Revisions of this file
board/evb/board_evb.cpp Show annotated file Show diff for this revision Revisions of this file
board/udk/X_NUCLEO_IKS01A1.lib Show annotated file Show diff for this revision Revisions of this file
board/udk/board_udk.cpp Show annotated file Show diff for this revision Revisions of this file
libmDot-mbed5.lib Show annotated file Show diff for this revision Revisions of this file
libs/Senet_Packet.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/board/board.h	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,79 @@
+/***
+ *       _____                         _   
+ *      / ____|                       | |  
+ *     | (___     ___   _ __     ___  | |_ 
+ *      \___ \   / _ \ | '_ \   / _ \ | __|
+ *      ____) | |  __/ | | | | |  __/ | |_ 
+ *     |_____/   \___| |_| |_|  \___|  \__|
+ *         (C) Senet, Inc                                
+ *                                         
+ */
+#ifndef BOARD_BOARD_H_
+#define BOARD_BOARD_H_
+
+#include "mbed.h"
+#include "mdot.h"
+
+extern Serial     debugUART;
+extern mDot      *mDotPtr;
+extern DigitalOut appLED;
+
+enum EBoardState
+{
+	Board_init,
+	Board_start,
+	Board_stop,
+};
+
+struct BoardOrientation
+{
+	bool horizontal;
+	bool up;
+	bool down;
+	bool right;
+	bool left;
+
+	void init()
+	{
+		horizontal = false;
+		up         = false;
+		down       = false;
+		right      = false;
+		left       = false;
+	}
+
+	BoardOrientation() { init(); }
+};
+
+/*
+ * Board sensor data
+ */
+struct BoardSensorData
+{
+    float temperature;
+    float pressure;
+    int32_t accel_x;
+    int32_t accel_y;
+    int32_t accel_z;
+    BoardOrientation orientation;
+
+    inline void init()
+    {
+        temperature= 0;
+        pressure = 0;
+        accel_x = 0;
+        accel_y = 0;
+        accel_z = 0;
+        orientation.init();
+    }
+
+    BoardSensorData() { init(); }
+};
+
+
+extern void BoardSetState( EBoardState state );
+extern bool BoardCheckForExit( bool exit );
+extern void BoardReadSensors( BoardSensorData &sensorData);
+
+
+#endif /* BOARD_BOARD_H_ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/board/board_common.cpp	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,52 @@
+/***
+ *       _____                         _   
+ *      / ____|                       | |  
+ *     | (___     ___   _ __     ___  | |_ 
+ *      \___ \   / _ \ | '_ \   / _ \ | __|
+ *      ____) | |  __/ | | | | |  __/ | |_ 
+ *     |_____/   \___| |_| |_|  \___|  \__|
+ *         (C) 2016 Senet, Inc                                
+ *                                         
+ */
+#include "board.h"
+#include "ChannelPlans.h"
+
+mDot *mDotPtr = NULL;
+DigitalOut appLED(PA_0);
+
+// To be implemented by the target
+extern void BoardInit();
+extern void BoardStart();
+extern void BoardStop();
+
+void BoardCommonInit()
+{
+	if(mDotPtr == NULL)
+	{
+		lora::ChannelPlan* plan = new lora::ChannelPlan_US915();
+		assert(plan);
+
+		mDotPtr = mDot::getInstance(plan);
+		assert(mDotPtr);
+	}
+
+	debugUART.baud(115200);
+}
+
+void BoardSetState( EBoardState state )
+{
+	switch (state)
+	{
+	    case Board_init:
+	    	BoardCommonInit();
+	    	BoardInit();
+	    	break;
+	    case Board_start:
+	    	BoardStart();
+	    	break;
+	    case Board_stop:
+	    	BoardStop();
+	    default:
+	    	break;
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/board/evb/DOGS102.lib	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/Multi-Hackers/code/DOGS102/#f40dbeaefe69
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/board/evb/ISL29011.lib	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/Multi-Hackers/code/ISL29011/#c1d5f4999b9e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/board/evb/MMA845x.lib	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/Multi-Hackers/code/MMA845x/#41af2b3eefb5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/board/evb/MPL3115A2.lib	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/Multi-Hackers/code/MPL3115A2/#3f52908a334d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/board/evb/NCP5623B.lib	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/Multi-Hackers/code/NCP5623B/#b28a2dfe05fd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/board/evb/board_evb.cpp	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,467 @@
+/***
+ *       _____                         _   
+ *      / ____|                       | |  
+ *     | (___     ___   _ __     ___  | |_ 
+ *      \___ \   / _ \ | '_ \   / _ \ | __|
+ *      ____) | |  __/ | | | | |  __/ | |_ 
+ *     |_____/   \___| |_| |_|  \___|  \__|
+ *         (C) 2016 Senet, Inc                                
+ *                                         
+ */
+#ifdef MTDOT_EVB
+#include "board.h"
+
+#include "MMA845x.h"
+#include "MPL3115A2.h"
+#include "ISL29011.h"
+#include "NCP5623B.h"
+#include "DOGS102.h"
+#include "font_6x8.h"
+#include "MultiTech_Logo.h"
+
+//DigitalIn mDot02(PA_2);  //  GPIO/UART_TX
+//DigitalOut mDot03(PA_3); //  GPIO/UART_RX
+//DigitalIn mDot04(PA_6);  //  GPIO/SPI_MISO
+//DigitalIn mDot06(PA_8);  //  GPIO/I2C_SCL
+//DigitalIn mDot07(PC_9);  //  GPIO/I2C_SDA
+
+InterruptIn mDot08(PA_12);  //  GPIO/USB       PB S1 on EVB
+InterruptIn mDot09(PA_11);  //  GPIO/USB       PB S2 on EVB
+
+//DigitalIn mDot11(PA_7);   //  GPIO/SPI_MOSI
+
+InterruptIn mDot12(PA_0);    //  GPIO/UART_CTS  PRESSURE_INT2 on EVB
+DigitalOut  mDot13(PC_13,1); //  GPIO           LCD_C/D
+InterruptIn mDot15(PC_1);    //  GPIO           LIGHT_PROX_INT on EVB
+InterruptIn mDot16(PA_1);    //  GPIO/UART_RTS  ACCEL_INT2 on EVB
+DigitalOut mDot17(PA_4,1);   //  GPIO/SPI_NCS   LCD_CS on EVB
+
+//DigitalIn mDot18(PA_5);    //  GPIO/SPI_SCK
+
+//DigitalInOut mDot19(PB_0,PIN_INPUT,PullNone,0); // GPIO         PushPull LED Low=Red High=Green set MODE=INPUT to turn off
+AnalogIn mDot20(PB_1);         //  GPIO          Current Sense Analog in on EVB
+Serial debugUART(PA_9, PA_10); // mDot debug UART
+//Serial mDotUART(PA_2, PA_3); // mDot external UART mDot02 and mDot03
+I2C mDoti2c(PC_9,PA_8);	// mDot External I2C mDot6 and mDot7
+
+SPI mDotspi(PA_7,PA_6,PA_5); // mDot external SPI mDot11, mDot4, and mDot18
+
+static MMA845x_DATA   accel_data   = 0;
+static MPL3115A2_DATA baro_data    = 0;
+static MMA845x*       evbAccel     = 0;
+static MPL3115A2*     evbBaro      = 0;
+static ISL29011*      evbAmbLight  = 0;
+static NCP5623B*      evbBackLight = 0;
+static DOGS102*       evbLCD       = 0;
+static Thread*        thread_3     = 0;
+
+uint8_t  result, pckt_time=100;
+char     data;
+
+// flags for pushbutton debounce code
+bool pb1_low = false;
+bool pb2_low = false;
+
+void pb1ISR(void);
+void pb2ISR(void);
+void pb1_debounce(void const *args);
+void pb2_debounce(void const *args);
+void config_pkt_xmit (void const *args);
+
+
+
+void BoardInit()
+{
+	// Setting up LED1 as activity LED
+	mDotPtr->setActivityLedPin(PB_0);
+	mDotPtr->setActivityLedEnable(true);
+
+    // threads for de-bouncing pushbutton switches
+	static Thread thread_1(pb1_debounce);
+    static Thread thread_2(pb2_debounce);
+
+    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
+    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
+
+    /*
+     *  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();
+
+    pckt_time = 10;
+}
+
+void BoardStart()
+{
+	osDelay(200);
+	evbBackLight->setPWM(NCP5623B::LED_3,16); // enable LED2 on EVB and set to 50% PWM
+
+	// sets LED2 to 50% max current
+	evbBackLight->setLEDCurrent(16);
+
+	printf("Start of Test\n\r");
+
+	osDelay (500); // allows other threads to process
+	printf("shutdown LED:\n\r");
+	evbBackLight->shutdown();
+
+	osDelay (500); // allows other threads to process
+	printf("Turn on LED2\n\r");
+	evbBackLight->setLEDCurrent(16);
+
+	data = evbAccel->getWhoAmI();
+	printf("Accelerometer who_am_i value = %x \n\r", data);
+
+	result = evbAccel->getStatus();
+	printf("status byte = %x \n\r", result);
+
+	printf("Barometer who_am_i check = %s \n\r", evbBaro->testWhoAmI() ? "TRUE" : "FALSE");
+
+	result = evbBaro->getStatus();
+	printf("status byte = %x \n\r", result);
+
+	/*
+	*  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 );
+
+	/*
+	 * Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition.
+	 * and a sample taken every second when in active mode
+	 */
+	evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, MPL3115A2::AT_1);
+
+	/*
+	 * Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution,
+	 * and 16000 lux range
+	 */
+	evbAmbLight->setMode(ISL29011::ALS_CONT);
+	evbAmbLight->setResolution(ISL29011::ADC_16BIT);
+	evbAmbLight->setRange(ISL29011::RNG_16000);
+
+	/*
+	 * Set the accelerometer for active mode
+	 */
+	evbAccel->activeMode();
+
+	/*
+	 * Clear the min-max registers in the Barometric Sensor
+	 */
+	evbBaro->clearMinMaxRegs();
+
+	evbBackLight->setLEDCurrent(0);
+}
+
+void BoardStop()
+{
+    evbBaro->triggerOneShot();
+    do {
+        osDelay(200);			// allows other threads to process
+        result = evbBaro->getStatus();
+    } while ((result & MPL3115A2::PTDR) == 0 );
+
+    baro_data = evbBaro->getAllData(true);
+    evbLCD->clearBuffer();
+    sprintf(txtstr,"Exiting Program");
+    evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
+}
+
+
+
+bool BoardCheckForExit( bool exit )
+{
+	// 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));
+
+		if(_exit)
+			exit(1);
+	}
+	return false;
+}
+
+/*
+ * Sets pb1_low flag. Slag is cleared in pb1_debounce thread
+ */
+void pb1ISR(void)
+{
+    if (!pb1_low)
+        pb1_low = true;
+}
+
+/*
+ * Debounces pb1. Also exits program if pushbutton 1 is pressed
+ */
+void pb1_debounce(void const *args)
+{
+    static uint8_t count = 0;
+
+    while (true) {
+        if (pb1_low && (mDot08 == 0))
+            count++;
+        else {
+            count = 0;
+            pb1_low = false;
+        }
+
+        if (count == 5)
+            exit_program = true;
+
+        Thread::wait(5);
+    }
+}
+
+/*
+ * Sets pb2_low flag. Flag is cleared in pb2_debounce thread
+ */
+void pb2ISR(void)
+{
+    if (!pb2_low)
+        pb2_low = true;
+}
+
+/*
+ * Debounces pb2. Also 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 pb2_debounce(void const *args)
+{
+    static uint8_t count = 0;
+
+    while (true) {
+
+        if (pb2_low && (mDot09 == 0))
+            count++;
+        else {
+            count = 0;
+            pb2_low = false;
+        }
+
+        if (count == 5){
+            if (pckt_time >= 5)
+                pckt_time /= 2;
+            else
+                pckt_time = 20;
+
+            //thread_3->signal_set(0x10);		// signal config_pkt_xmit to send packet
+            position_changed = true;
+        }
+        Thread::wait(5);
+    }
+ }
+
+/*
+ * 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)
+{
+    int32_t mdot_ret;
+
+    std::vector<uint8_t> data;
+
+    while (true) {
+        Thread::signal_wait(0x10);		// wait for pb2ISR to signal send
+        data.clear();
+        data.push_back(0x0F);			// key for Configuration data (packet transmission timer)
+        data.push_back(pckt_time);
+
+        if ((mdot_ret = mdot_radio->send(data)) != mDot::MDOT_OK) {
+            log_error(mdot_radio, "failed to send config data", mdot_ret);
+        } else {
+            printf("sent config data to gateway\r\n");
+        }
+    }
+}
+
+void ReadSensors(BoardSensorData &sensorData)
+{
+    MMA845x_DATA accel_data;
+    /*
+     * Test Accelerometer XYZ data ready bit to see if acquisition complete
+     */
+    do {
+        osDelay(100); // allows other threads to process
+        result = evbAccel->getStatus();
+    } while ((result & MMA845x::XYZDR) == 0 );
+
+    /*
+     * Retrieve and print out accelerometer data
+     */
+    accel_data = evbAccel->getXYZ();
+
+    sprintf(txtstr,"Accelerometer");
+    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));
+    sprintf(txtstr, "y = %d", accel_data._y);
+    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));
+
+    sensorData.accel_x = accel_data._x;
+    sensorData.accel_y = accel_data._y;
+    sensorData.accel_z = accel_data._z;
+
+    // Update accelerometer state
+    evbLCD->startUpdate();
+    evbLCD->clearBuffer();
+
+    // 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);
+        // Turn LED off to indicate server not in agreement
+        SYNC_LED=SYNC_LED_OOS;
+        // Set reflected_value to an out of range value to stay
+        // in fast transmit mode until server responds
+        reflected_value = 0;
+    }
+
+    /*
+     * Trigger a Pressure reading
+     */
+    evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16,
+                           MPL3115A2::AT_1);
+    evbBaro->triggerOneShot();
+
+    /*
+     * Test barometer device status to see if acquisition is complete
+     */
+    do {
+        osDelay(100);			// allows other threads to process
+        result = evbBaro->getStatus();
+    } while ((result & MPL3115A2::PTDR) == 0 );
+
+    /*
+     * Retrieve and print out barometric pressure
+     */
+    pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value
+    num_whole = pressure >> 2;			// 18 bit integer significant
+    num_frac = (pressure & 0x3) * 25;		// 2 bit fractional  0.25 per bit
+    sensorData.pressure = pressure + (.25 * num_frac);
+
+    sprintf(txtstr,"Press=%ld.%02d Pa", num_whole, num_frac);
+    evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
+
+    /*
+     * Trigger a Altitude reading
+     */
+    evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16,
+                           MPL3115A2::AT_1);
+    evbBaro->triggerOneShot();
+
+    /*
+     * Test barometer device status to see if acquisition is complete
+     */
+    do {
+        osDelay(100);			// allows other threads to process
+        result = evbBaro->getStatus();
+    } while ((result & MPL3115A2::PTDR) == 0 );
+
+    /*
+     * Retrieve and print out altitude and temperature
+     */
+    baro_data = evbBaro->getAllData(false);
+    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);
+    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 / 100;		// 4 bit fractional .0625 per bit
+    sensorData.temperature = num_whole  + ((float)num_frac / 100);
+    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
+     */
+    lux_data = evbAmbLight->getData();
+    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->endUpdate();
+}
+
+
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/board/udk/X_NUCLEO_IKS01A1.lib	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/dudmuck/code/X_NUCLEO_IKS01A1/#b5b8e7cab622
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/board/udk/board_udk.cpp	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,119 @@
+/***
+ *       _____                         _   
+#if defined MTDOT_UDK
+ *      / ____|                       | |  
+ *     | (___     ___   _ __     ___  | |_ 
+ *      \___ \   / _ \ | '_ \   / _ \ | __|
+ *      ____) | |  __/ | | | | |  __/ | |_ 
+ *     |_____/   \___| |_| |_|  \___|  \__|
+ *         (C) 2016 Senet, Inc                                
+ *                                         
+ */
+#if defined MTDOT_UDK
+
+#include "board.h"
+#include "x_nucleo_iks01a1.h"
+
+extern void mDotInit();
+
+Serial debugUART(USBTX, USBRX);
+static X_NUCLEO_IKS01A1 *mems_shield;
+
+
+void BoardInit()
+{
+	mems_shield = X_NUCLEO_IKS01A1::Instance(NULL, NC);
+}
+
+void BoardStart() { }
+void BoardStop()  { }
+
+void BoardReadSensors(BoardSensorData &data)
+{
+    uint32_t ret = 0;
+    int32_t  accel_data[3];
+
+   // Temperature
+   ret |= (!CALL_METH(mems_shield->pt_sensor, GetTemperature, &data.temperature, 0.0f) ? 0x0 : 0x1);
+
+   // Pressure
+   ret |= (!CALL_METH(mems_shield->pt_sensor, GetPressure, &data.pressure, 0.0f) ? 0x0 : 0x1);
+
+   // Accelerometer
+   MotionSensor *motionSensor = mems_shield->GetAccelerometer();
+   if( motionSensor != NULL)
+   {
+       motionSensor->Get_X_Axes(accel_data);
+
+       data.accel_x = accel_data[0];
+       data.accel_y = accel_data[1];
+       data.accel_z = accel_data[2];
+       /*  z-axis : > 0 = rightside up, < 0 upside down
+        *  x-axis: com LED to the left x < 0, x > 0 on the right
+        *  y-axis: y > 0 COM LED down, y < 0  COM LED up
+        */
+       data.orientation.init();
+
+       // rightside up
+       if(data.accel_z >= 750)
+       {
+           data.orientation.horizontal = true;
+       }
+       // upside down
+       else if(data.accel_z <= -750)
+       {
+           data.orientation.horizontal  = true;
+           data.orientation.down        = true;
+           // position_value = (2 << 12) | (1 << 8);
+       }
+       // vertical down
+       else if(data.accel_y >= 900 )
+       {
+           data.orientation.down = true;
+       }
+       // vertical up
+       else if(data.accel_y <= -900 )
+       {
+           data.orientation.up = true;
+       }
+       // side right
+       else if(data.accel_x > 900)
+       {
+           data.orientation.right = true;
+       }
+       // side left
+       else
+       {
+           data.orientation.left = true;
+       }
+
+       /*
+       if(horizontal)
+       {
+           next_value = (2 << 12) | (upsidedown << 8);
+       }
+       else
+       {
+           next_value = (up << 12) | (left << 8) | (down << 4) | right;
+       }
+
+       if(next_value != position_value)
+       {
+           position_value = next_value;
+           position_changed = true;
+
+           // Set reflected_value to an out of range value to stay
+           // in fast transmit mode until server responds
+           reflected_value = 0;
+
+            // Turn LED off to indicate server is not in agreement
+            SYNC_LED=SYNC_LED_OOS;
+       }
+       */
+   }
+}
+
+
+bool BoardCheckForExit( bool exit ) { return false; }
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libmDot-mbed5.lib	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/#255e2ddc294e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libs/Senet_Packet.lib	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,1 @@
+https://mbed.org/teams/Senet/code/Senet_Packet/#17e4e5f99d0c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,314 @@
+/***
+ *       _____                         _   
+ *      / ____|                       | |  
+ *     | (___     ___   _ __     ___  | |_ 
+ *      \___ \   / _ \ | '_ \   / _ \ | __|
+ *      ____) | |  __/ | | | | |  __/ | |_ 
+ *     |_____/   \___| |_| |_|  \___|  \__|
+ *         (C) 2016 Senet, Inc                                
+ *                                         
+ */
+
+#include "board.h"
+#include "senet_packet.h"
+
+/******************************************************************************
+ * LoRaWAN Configuration                                                      *
+ ******************************************************************************/
+ // Senet Developer Portal Application EUI
+static uint8_t APP_EUI[8]  = {0x00,0x25,0x0C,0x00,0x00,0x01,0x00,0x01};
+
+// Get Application Key from Senet Developer Portal Device Edit page
+static uint8_t APP_KEY[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+
+#define DATARATE            mDot::DR0
+#define TXPOWER             20
+#define JOIN_RETRIES        1
+
+static std::vector<uint8_t> appEUI(APP_EUI,APP_EUI+sizeof(APP_EUI)/sizeof(uint8_t));
+static std::vector<uint8_t> appKey(APP_KEY,APP_KEY+sizeof(APP_KEY)/sizeof(uint8_t));
+static uint8_t              fsb   = 0;
+static bool                 adrOn = true;
+
+/******************************************************************************/
+
+#define APP_TX_DUTY_CYCLE_NORMAL     300000  // 5 min
+#define APP_TX_DUTY_CYCLE_ALARM      15000   // 15 s
+
+// Backend configured state. Set true to enable alarm rate transmits until backend response
+static bool  BackendEnabled = false;
+
+#define HORIZONTAL_ORIENTATION_VALUE 1 // transmitted value when device is horizontal
+#define VERTICAL_ORIENTATION_VALUE   2 // transmitted value when device is vertical
+
+// Transmit rate related variables
+static bool     NextTx          = true;
+static uint32_t AppTxDutyCycle  = APP_TX_DUTY_CYCLE_NORMAL;
+
+static Ticker           joinTicker;
+static Ticker           nextTxTimer;
+static BoardSensorData  sensorData;
+static BoardOrientation orientation;
+
+
+// Backend service state (set to false if backend is not configured)
+static bool      BackendSynchronized = true;
+BoardOrientation BackendOrientation;
+
+// Forward
+static void log_error(mDot* dot, const char* msg, int32_t retval);
+static void joinLedToggle();
+static void onNextTxTimerEvent();
+static void ReceiveData(std::vector<uint8_t> frame);
+
+void JoinNetwork()
+{
+	bool    ok;
+	int32_t mdot_ret;
+
+	do{
+		ok = true;
+
+		// reset to default config so we know what state we're in
+		mDotPtr->resetConfig();
+		mDotPtr->setLogLevel(6);
+		mDotPtr->setAntennaGain(-3);
+
+		// Read node ID
+		std::vector<uint8_t> mdot_EUI;
+		mdot_EUI = mDotPtr->getDeviceId();
+		printf("mDot EUI = ");
+
+		for (uint8_t i=0; i<mdot_EUI.size(); i++)
+			printf("%02x ", mdot_EUI[i]);
+		printf("\n\r");
+
+	  /*
+	   * This call sets up private or public mode on the MTDOT. Set the function to true if
+	   * connecting to a public network
+	   */
+		printf("setting Public Network Mode\r\n");
+		if ((mdot_ret = mDotPtr->setPublicNetwork(true)) != mDot::MDOT_OK)
+			log_error(mDotPtr, "failed to set Public Network Mode", mdot_ret);
+
+		mDotPtr->setTxDataRate(DATARATE);
+		mDotPtr->setTxPower(TXPOWER);
+		mDotPtr->setJoinRetries(JOIN_RETRIES);
+		mDotPtr->setJoinMode(mDot::OTA);
+
+	  /*
+	   * 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 = mDotPtr->setFrequencySubBand(fsb)) != mDot::MDOT_OK) {
+			log_error(mDotPtr, "failed to set frequency sub band", mdot_ret);
+			ok = false;
+		}
+
+		printf("setting ADR\r\n");
+		if ((mdot_ret = mDotPtr->setAdr(adrOn)) != mDot::MDOT_OK) {
+			log_error(mDotPtr, "failed to set ADR", mdot_ret);
+			ok = false;
+		}
+
+	   /*
+		* setNetworkName is used for private networks.
+		* Use setNetworkID(AppID) for public networks
+		*/
+		printf("setting network name\r\n");
+		if ((mdot_ret = mDotPtr->setNetworkId(appEUI)) != mDot::MDOT_OK) {
+			log_error(mDotPtr, "failed to set network name", mdot_ret);
+			ok = false;
+		}
+
+	   /*
+		* setNetworkPassphrase is used for private networks
+		* Use setNetworkKey for public networks
+		*/
+		printf("setting network key\r\n");
+		if ((mdot_ret = mDotPtr->setNetworkKey(appKey)) != mDot::MDOT_OK) {
+			log_error(mDotPtr, "failed to set network password", mdot_ret);
+			ok = false;
+		}
+
+		BoardCheckForExit(true);
+
+	} while(ok == false);
+
+	joinTicker.attach(joinLedToggle,1);
+
+	// attempt to join the network
+	printf("joining network\r\n");
+	while ((mdot_ret = mDotPtr->joinNetwork()) != mDot::MDOT_OK)
+	{
+		BoardCheckForExit(true);
+
+		log_error(mDotPtr,"failed to join network:", mdot_ret);
+		uint32_t delay_s = (mDotPtr->getNextTxMs() / 1000) + 1;
+		wait(delay_s);
+	}
+
+	printf("network joined\r\n");
+
+	joinTicker.detach();
+	appLED=1;
+}
+
+void SendFrame()
+{
+	std::vector<uint8_t> frame;
+    int32_t              mdot_ret;
+    uint8_t              buffer[20];
+    SensorPacket         packet(buffer, sizeof(buffer));
+
+    // Sensor packet type serialized to the frame buffer
+    packet.setPrimarySensor(orientation.horizontal ? HORIZONTAL_ORIENTATION_VALUE : VERTICAL_ORIENTATION_VALUE);
+    packet.setTemperature(sensorData.temperature);
+    packet.setPressure(sensorData.pressure);
+    packet.serialize();
+
+    frame.assign(packet.payload(), packet.payload() + packet.length());
+    if ((mdot_ret = mDotPtr->send(frame)) != mDot::MDOT_OK)
+    {
+        log_error(mDotPtr, "failed to send", mdot_ret);
+    }
+    else
+    {
+        printf("successfully sent data\r\n");
+        frame.clear();
+        if ((mdot_ret = mDotPtr->recv(frame)) == mDot::MDOT_OK)
+        {
+            printf("recv data: ");
+            for(uint32_t i = 0;i < frame.size();i++)
+                printf("%02X",frame[i]);
+            printf("\r\n");
+
+            ReceiveData(frame);
+        }
+    }
+}
+
+void ReceiveData(std::vector<uint8_t> frame)
+{
+	BackendOrientation.horizontal = (frame[0] == HORIZONTAL_ORIENTATION_VALUE);
+
+	if( BackendOrientation.horizontal == orientation.horizontal )
+		BackendSynchronized = true;
+}
+
+
+int main()
+{
+	time_t lastTxT;
+
+	// Initialize Board
+	BoardSetState(Board_init);
+
+	// Join Network
+	JoinNetwork();
+
+	// Start Board sensors
+	BoardSetState(Board_start);
+
+	// Initialize board orientation
+	BoardReadSensors(sensorData);
+	orientation = sensorData.orientation;
+
+	BackendSynchronized = false;
+
+	// Start transmit timer
+	nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3);
+
+	while( !BoardCheckForExit(false) )
+	{
+		// Update device orientation
+		if( ( BackendSynchronized == true ) && ( sensorData.orientation.horizontal != orientation.horizontal ) )
+		{
+			BackendSynchronized  = false;
+			orientation = sensorData.orientation;
+
+			// Get elapsed time since last transmit
+			time_t currT    = time(NULL);
+			time_t elapsedT = ( currT - lastTxT ) * 1e3;
+
+			// Transmit now if elapsed time since last tx is greater than alarm mode dutycycle
+			if( elapsedT >= APP_TX_DUTY_CYCLE_ALARM )
+			{
+				nextTxTimer.detach();
+				NextTx = true;
+			}
+			// Otherwise wait until alarm dutycycle time has elapased
+			else
+			{
+				nextTxTimer.detach();
+				nextTxTimer.attach_us(onNextTxTimerEvent, (APP_TX_DUTY_CYCLE_ALARM - elapsedT)* 1e3);
+			}
+
+			AppTxDutyCycle = APP_TX_DUTY_CYCLE_ALARM;
+		}
+
+		if ( NextTx == true )
+		{
+			/*  Backend synchronized flag set true when
+			 *    - backend not enabled
+			 *    - Downlink received containing last known orientation
+			 */
+			BackendSynchronized = !BackendEnabled;
+
+			// Transmit application frame
+			SendFrame();
+			lastTxT = time(NULL);
+
+			NextTx = false;
+
+			// Fast transmit rate while backend is out of sync with device state
+			if(BackendSynchronized == false)
+			{
+				if( ( AppTxDutyCycle != APP_TX_DUTY_CYCLE_ALARM ) )
+				{
+					AppTxDutyCycle = APP_TX_DUTY_CYCLE_ALARM;
+					nextTxTimer.detach();
+					nextTxTimer.attach_us(onNextTxTimerEvent, APP_TX_DUTY_CYCLE_ALARM * 1e3);
+				}
+			}
+			else if( AppTxDutyCycle != APP_TX_DUTY_CYCLE_NORMAL )
+			{
+				AppTxDutyCycle = APP_TX_DUTY_CYCLE_NORMAL;
+				nextTxTimer.detach();
+				nextTxTimer.attach_us(onNextTxTimerEvent, APP_TX_DUTY_CYCLE_NORMAL * 1e3);
+			}
+		}
+
+		// Delay before next sensor poll
+		osDelay(2000);
+
+    	// Read sensors
+		BoardReadSensors(sensorData);
+	}
+
+	BoardSetState(Board_stop);
+
+	return 0;
+}
+
+
+/*
+ *  prints of mDot error
+ */
+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());
+}
+
+void joinLedToggle()
+{
+    appLED= !appLED;
+}
+
+void onNextTxTimerEvent( void )
+{
+	NextTx = true;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Thu Aug 24 17:56:53 2017 -0400
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#fc1836545dcc2fc86f03b01292b62bf2089f67c3