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
Revision 43:55e7bb4d9b60, committed 2017-08-30
- Comitter:
- Shaun Nelson
- Date:
- Wed Aug 30 12:35:33 2017 -0400
- Parent:
- 42:ebb58fea302b
- Commit message:
- Simplified orientation change detection
Implement button press events
EVB Button1(left) for backend enabled state status (2 sblink = disabled, 3 = enabled)
EVB Button2(right) toggle bacend enabled status
LED Behavior
- LED solid when backend out of sync
- LED blinks on downlink
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Aug 30 12:30:22 2017 -0400
+++ b/main.cpp Wed Aug 30 12:35:33 2017 -0400
@@ -35,7 +35,7 @@
/******************************************************************************
* Application Configuration *
******************************************************************************/
-#define APP_TX_DUTY_CYCLE_NORMAL 300000 // 5 min
+#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
@@ -47,26 +47,20 @@
#define VERTICAL_ORIENTATION_VALUE 2 // transmitted value when device is vertical
-static bool BackendSynchronized = true;
+// Set to true when backend is synchronized
+static bool BackendSynchronized = true;
+// Set to true to force backend resync
+static bool OrientationInit = true;
+static bool DisplayBackendState = true;
static BoardOrientation BackendOrientation;
static Ticker joinTicker;
static Ticker nextTxTimer;
static BoardSensorData sensorData;
static BoardOrientation txOrientation;
+static BoardOrientation lastOrientation;
static bool NextTx = true;
static uint32_t AppTxDutyCycle = APP_TX_DUTY_CYCLE_NORMAL;
-
-// Board Orientation
-enum EOrientationType
-{
- Orientation_Horizontal = 0,
- Orientation_Vertical,
- Orientation_Max,
-};
-//orientation change count
-uint32_t orientationCount[Orientation_Max];
-// orientation transmit count
-uint32_t orientationTxCount[Orientation_Max];
+static uint32_t orientationChangeCount = 0;
// Orientation comparison
inline bool orientationIsEqual(BoardOrientation &a, BoardOrientation &b)
@@ -75,42 +69,6 @@
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();
@@ -245,54 +203,103 @@
void ReceiveData(std::vector<uint8_t> frame)
{
BackendOrientation.vertical = (frame[0] == VERTICAL_ORIENTATION_VALUE);
+ BackendSynchronized = !BackendEnabled || orientationIsEqual(BackendOrientation, txOrientation);
- BackendSynchronized = orientationIsEqual(BackendOrientation, txOrientation);
+ // Blink LED
+ bool ledState = false;
+ for(uint8_t i=0; i<3; i++)
+ {
+ CBoard::SetLED(1, ledState);
+ ledState = !ledState;
+ osDelay(500);
+ }
+}
+
+static void onButtonPress(uint8_t buttonNum)
+{
+ DisplayBackendState = true;
+
+ if(buttonNum == 1)
+ {
+ OrientationInit = true;
+ BackendEnabled = !BackendEnabled;
+
+ // Start next transmit immediately
+ if( BackendEnabled && !NextTx )
+ {
+ nextTxTimer.detach();
+ nextTxTimer.attach_us(onNextTxTimerEvent, 1000);
+ }
+ }
}
int main()
{
- time_t lastTxT;
- BoardOrientation lastOrientation;
-
- memset(orientationCount, 0, sizeof(orientationCount));
- memset(orientationTxCount, 0, sizeof(orientationTxCount));
+ time_t lastTxT;
// Initialize Board
BoardInit();
+ // Register pushbutton handler
+ CBoard::SetButtonCallback(onButtonPress);
+
// Join Network
JoinNetwork();
// Start Board sensors
CBoard::Start();
- // Send initial state
- if( CBoard::ReadSensors(sensorData) == Board_Ok )
- {
- lastOrientation = sensorData.orientation;
- incrementOrientation(lastOrientation);
-
- //Initialize backend state to opposite of current to trigger
- // backend synchronization if enabled
- BackendOrientation.vertical = !lastOrientation.vertical;
- }
-
while( true )
{
// Read sensors
if( CBoard::ReadSensors(sensorData) == Board_Ok )
{
- if( !orientationIsEqual(sensorData.orientation, lastOrientation) )
+ if( !orientationIsEqual(sensorData.orientation, lastOrientation) || OrientationInit)
{
+ orientationChangeCount++;
lastOrientation = sensorData.orientation;
- incrementOrientation(lastOrientation);
}
}
- // Determine next orientation to transmit after backend sync of the last transmitted orientation is finished
- if( ( BackendSynchronized == true ) && ( getNextTxOrientation(txOrientation) < Orientation_Max ) )
+ // Initialize orientation state
+ if( OrientationInit )
+ {
+ OrientationInit = false;
+ orientationChangeCount = 1;
+ // Set tx orientation to opposite of current as it will be toggled before transmit
+ txOrientation.vertical = !lastOrientation.vertical;
+ // Set backend to be out of sync
+ BackendOrientation.vertical = txOrientation.vertical;
+ }
+
+ // Display backend enabled state
+ if(DisplayBackendState)
+ {
+ DisplayBackendState = false;
+
+ uint8_t ledToggleCount = BackendEnabled ? 3 : 2;
+ CBoard::SetLED(1, false);
+ // blink LED to indicate backend state
+ for(uint32_t i=0; i < ledToggleCount*2; i++)
+ {
+ osDelay(1000);
+ CBoard::ToggleLED(1);
+ }
+
+ // Set LED to reflect Backend synchronized state
+ CBoard::SetLED(1, BackendOrientation.vertical != txOrientation.vertical);
+ }
+
+ // Get next orientation change to transmit after backend sync of the last transmitted orientation is finished
+ if( ( BackendSynchronized == true ) && ( orientationChangeCount != 0 ) )
{
BackendSynchronized = false;
+
+ // Set transmit orientation
+ txOrientation.vertical = !txOrientation.vertical;
+ orientationChangeCount--;
+
+ // Turn on out-of-sync LED
CBoard::SetLED(1, true);
// Get elapsed time since last transmit
@@ -304,7 +311,8 @@
nextTxTimer.detach();
// Wait to transmit until elapsed time is greater than alarm mode dutycycle
- if( !( NextTx = ( elapsedT >= APP_TX_DUTY_CYCLE_ALARM ) ) )
+ NextTx = ( elapsedT >= APP_TX_DUTY_CYCLE_ALARM );
+ if( !NextTx )
{
AppTxDutyCycle = APP_TX_DUTY_CYCLE_ALARM - elapsedT;
nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3);
@@ -317,7 +325,7 @@
/* Backend synchronized flag set true when
* o Backend not enabled
- * o Backend state == current orientation
+ * o Backend in sync
*/
BackendSynchronized = !BackendEnabled || orientationIsEqual(BackendOrientation, txOrientation);
@@ -328,7 +336,7 @@
lastTxT = time(NULL);
// Set next transmit time
- if(BackendSynchronized == false)
+ if( BackendSynchronized == false )
{
if( ( AppTxDutyCycle != APP_TX_DUTY_CYCLE_ALARM ) )
{
@@ -344,8 +352,8 @@
nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3);
}
- if(BackendSynchronized == true)
- CBoard::SetLED(1, false);
+ // Set LED to reflect Backend synchronized state
+ CBoard::SetLED(1, BackendOrientation.vertical != txOrientation.vertical);
}
// Delay before next sensor poll
