Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libmDot-mbed5 DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B X_NUCLEO_IKS01A1 Senet_Packet
Fork of MTDOT-UDKDemo_Senet by
Diff: main.cpp
- Branch:
- develop
- Revision:
- 36:cd1077f40dbf
- Parent:
- 32:5873d0638277
- Child:
- 39:022b327d6bf0
--- a/main.cpp Fri Aug 25 14:04:00 2017 -0400
+++ b/main.cpp Tue Aug 29 13:51:51 2017 -0400
@@ -29,32 +29,32 @@
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
+
+/******************************************************************************
+ * Application Configuration *
+ ******************************************************************************/
+#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;
+/******************************************************************************/
+// Transmitted orientation values
#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 bool BackendSynchronized = true;
+static BoardOrientation BackendOrientation;
static Ticker joinTicker;
static Ticker nextTxTimer;
static BoardSensorData sensorData;
static BoardOrientation txOrientation;
-
-
-// Backend service state (set to false if backend is not configured)
-static bool BackendSynchronized = true;
-BoardOrientation BackendOrientation;
-
+static bool NextTx = true;
+static uint32_t AppTxDutyCycle = APP_TX_DUTY_CYCLE_NORMAL;
// Board Orientation
enum EOrientationType
@@ -63,18 +63,60 @@
Orientation_Vertical,
Orientation_Max,
};
-
//orientation change count
uint32_t orientationCount[Orientation_Max];
// orientation transmit count
uint32_t orientationTxCount[Orientation_Max];
-// Forward
+// Orientation comparison
+inline bool orientationIsEqual(BoardOrientation &a, BoardOrientation &b)
+{
+ // For this application only vertical/horizontal state is tested
+ return ( a.vertical == b.vertical );
+}
+
+// Next orientation change to transmit
+inline uint8_t getNextTxOrientation(BoardOrientation &orientation)
+{
+ static uint8_t txOrientationType = Orientation_Max;
+
+ if (++txOrientationType >= Orientation_Max)
+ txOrientationType = 0;
+
+ for( ; txOrientationType < Orientation_Max; txOrientationType++)
+ {
+ if( orientationTxCount[txOrientationType] < orientationCount[txOrientationType] )
+ {
+ orientationTxCount[txOrientationType]++;
+ switch(txOrientationType)
+ {
+ case Orientation_Horizontal:
+ orientation.vertical = false;
+ break;
+ case Orientation_Vertical:
+ orientation.vertical = true;
+ break;
+ default:
+ break;
+ }
+ break;
+ }
+ }
+
+ return txOrientationType;
+}
+
+inline void incrementOrientation(BoardOrientation &orientation)
+{
+ orientation.vertical ? orientationCount[Orientation_Vertical]++ : orientationCount[Orientation_Horizontal]++;
+}
+
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;
@@ -204,41 +246,14 @@
{
BackendOrientation.vertical = (frame[0] == VERTICAL_ORIENTATION_VALUE);
- if( BackendOrientation.vertical == txOrientation.vertical )
- BackendSynchronized = true;
+ BackendSynchronized = orientationIsEqual(BackendOrientation, txOrientation);
}
-inline uint8_t getNextTxOrientation()
-{
- static uint8_t txOrientationType = Orientation_Max;
-
- if (++txOrientationType >= Orientation_Max)
- txOrientationType = 0;
-
- for( ; txOrientationType < Orientation_Max; txOrientationType++)
- {
- if( orientationTxCount[txOrientationType] < orientationCount[txOrientationType] )
- {
- orientationTxCount[txOrientationType]++;
- break;
- }
- }
-
- return txOrientationType;
-}
-
-inline void incrementOrientation(BoardOrientation &orientation)
-{
- orientation.vertical ? orientationCount[Orientation_Vertical]++ : orientationCount[Orientation_Horizontal]++;
-}
-
-
int main()
{
time_t lastTxT;
BoardOrientation lastOrientation;
-
memset(orientationCount, 0, sizeof(orientationCount));
memset(orientationTxCount, 0, sizeof(orientationTxCount));
@@ -256,91 +271,76 @@
{
lastOrientation = sensorData.orientation;
incrementOrientation(lastOrientation);
+
+ //Initialize backend state to opposite of current to trigger
+ // backend synchronization if enabled
+ BackendOrientation.vertical = !lastOrientation.vertical;
}
- // Start transmit timer
- nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3);
-
while( true )
{
- uint8_t nextTxOrientation;
-
// Read sensors
if( CBoard::ReadSensors(sensorData) == Board_Ok )
{
- if( sensorData.orientation.vertical != lastOrientation.vertical )
+ if( !orientationIsEqual(sensorData.orientation, lastOrientation) )
{
lastOrientation = sensorData.orientation;
incrementOrientation(lastOrientation);
}
}
- // Determine next orientation to transmit after backend sync of the last orientation is finished
- if( ( BackendSynchronized == true ) && ( ( nextTxOrientation = getNextTxOrientation() ) < Orientation_Max ) )
+ // Determine next orientation to transmit after backend sync of the last transmitted orientation is finished
+ if( ( BackendSynchronized == true ) && ( getNextTxOrientation(txOrientation) < Orientation_Max ) )
{
- BackendSynchronized = false;
-
- switch(nextTxOrientation)
- {
- case Orientation_Horizontal:
- txOrientation.vertical = false;
- break;
- case Orientation_Vertical:
- txOrientation.vertical = true;
- break;
- default:
- break;
- }
+ BackendSynchronized = false;
// 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 )
+ // Stop transmit timer
+ AppTxDutyCycle = 0;
+ nextTxTimer.detach();
+
+ // Wait to transmit until elapsed time is greater than alarm mode dutycycle
+ if( !( NextTx = ( elapsedT >= APP_TX_DUTY_CYCLE_ALARM ) ) )
{
- nextTxTimer.detach();
- NextTx = true;
+ AppTxDutyCycle = APP_TX_DUTY_CYCLE_ALARM - elapsedT;
+ nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3);
}
- // Otherwise wait until alarm 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 )
{
+ NextTx = false;
+
/* Backend synchronized flag set true when
- * - Backend not enabled
- * - Downlink received for current orientation
+ * o Backend not enabled
+ * o Backend state == current orientation
*/
- BackendSynchronized = !BackendEnabled;
+ BackendSynchronized = !BackendEnabled || orientationIsEqual(BackendOrientation, txOrientation);
// Transmit application frame
SendFrame();
+
+ // Update transmit timestamp
lastTxT = time(NULL);
- NextTx = false;
-
- // Fast transmit rate while backend is out of sync with device state
+ // Set next transmit time
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);
+ nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 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);
+ nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3);
}
}
@@ -358,13 +358,5 @@
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;
-}
-
+void joinLedToggle() { appLED= !appLED; }
+void onNextTxTimerEvent( void ) { NextTx = true; }
