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:
- 28:4fd8a894a403
- Parent:
- 27:1753a44fa9ec
- Child:
- 29:055824db068a
--- a/main.cpp Thu Aug 24 17:56:53 2017 -0400
+++ b/main.cpp Fri Aug 25 09:29:01 2017 -0400
@@ -1,5 +1,4 @@
-/***
- * _____ _
+ /* _____ _
* / ____| | |
* | (___ ___ _ __ ___ | |_
* \___ \ / _ \ | '_ \ / _ \ | __|
@@ -27,8 +26,8 @@
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;
+static uint8_t fsb = 0;
+static bool adrOn = true;
/******************************************************************************/
@@ -48,13 +47,27 @@
static Ticker joinTicker;
static Ticker nextTxTimer;
static BoardSensorData sensorData;
-static BoardOrientation orientation;
+static BoardOrientation txOrientation;
// Backend service state (set to false if backend is not configured)
static bool BackendSynchronized = true;
BoardOrientation BackendOrientation;
+
+// 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];
+
// Forward
static void log_error(mDot* dot, const char* msg, int32_t retval);
static void joinLedToggle();
@@ -133,8 +146,6 @@
ok = false;
}
- BoardCheckForExit(true);
-
} while(ok == false);
joinTicker.attach(joinLedToggle,1);
@@ -143,8 +154,6 @@
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);
@@ -164,7 +173,7 @@
SensorPacket packet(buffer, sizeof(buffer));
// Sensor packet type serialized to the frame buffer
- packet.setPrimarySensor(orientation.horizontal ? HORIZONTAL_ORIENTATION_VALUE : VERTICAL_ORIENTATION_VALUE);
+ packet.setPrimarySensor(txOrientation.vertical ? VERTICAL_ORIENTATION_VALUE : HORIZONTAL_ORIENTATION_VALUE);
packet.setTemperature(sensorData.temperature);
packet.setPressure(sensorData.pressure);
packet.serialize();
@@ -192,42 +201,95 @@
void ReceiveData(std::vector<uint8_t> frame)
{
- BackendOrientation.horizontal = (frame[0] == HORIZONTAL_ORIENTATION_VALUE);
+ BackendOrientation.vertical = (frame[0] == VERTICAL_ORIENTATION_VALUE);
+
+ if( BackendOrientation.vertical == txOrientation.vertical )
+ BackendSynchronized = true;
+}
+
+inline uint8_t getNextTxOrientation()
+{
+ static uint8_t txOrientationType = Orientation_Max;
+
+ if (++txOrientationType >= Orientation_Max)
+ txOrientationType = 0;
- if( BackendOrientation.horizontal == orientation.horizontal )
- BackendSynchronized = true;
+ 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;
+ time_t lastTxT;
+ BoardOrientation lastOrientation;
+
+
+ bzero(orientationCount, sizeof(orientationCount));
+ bzero(orientationTxCount, sizeof(orientationTxCount));
// Initialize Board
- BoardSetState(Board_init);
+ BoardInit();
// Join Network
JoinNetwork();
// Start Board sensors
- BoardSetState(Board_start);
+ CBoard::Start();
- // Initialize board orientation
- BoardReadSensors(sensorData);
- orientation = sensorData.orientation;
-
- BackendSynchronized = false;
+ // Send initial state
+ if( CBoard::ReadSensors(sensorData) == Board_Ok )
+ {
+ lastOrientation = sensorData.orientation;
+ incrementOrientation(lastOrientation);
+ }
// Start transmit timer
nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3);
- while( !BoardCheckForExit(false) )
+ while( true )
{
- // Update device orientation
- if( ( BackendSynchronized == true ) && ( sensorData.orientation.horizontal != orientation.horizontal ) )
+ uint8_t nextTxOrientation;
+
+ // Read sensors
+ if( CBoard::ReadSensors(sensorData) == Board_Ok )
+ {
+ if( sensorData.orientation.vertical != lastOrientation.vertical )
+ {
+ 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 ) )
{
BackendSynchronized = false;
- orientation = sensorData.orientation;
+
+ switch(nextTxOrientation)
+ {
+ case Orientation_Horizontal:
+ txOrientation.vertical = false;
+ break;
+ case Orientation_Vertical:
+ txOrientation.vertical = true;
+ break;
+ default:
+ break;
+ }
// Get elapsed time since last transmit
time_t currT = time(NULL);
@@ -239,7 +301,7 @@
nextTxTimer.detach();
NextTx = true;
}
- // Otherwise wait until alarm dutycycle time has elapased
+ // Otherwise wait until alarm time has elapased
else
{
nextTxTimer.detach();
@@ -252,8 +314,8 @@
if ( NextTx == true )
{
/* Backend synchronized flag set true when
- * - backend not enabled
- * - Downlink received containing last known orientation
+ * - Backend not enabled
+ * - Downlink received for current orientation
*/
BackendSynchronized = !BackendEnabled;
@@ -283,13 +345,7 @@
// Delay before next sensor poll
osDelay(2000);
-
- // Read sensors
- BoardReadSensors(sensorData);
}
-
- BoardSetState(Board_stop);
-
return 0;
}
