From 4ff53e79c2c87d8ad110565dcb0cddc23598e095 Mon Sep 17 00:00:00 2001 From: Joshua Saxby <joshua.a.saxby@gmail.com> Date: Sun, 8 Mar 2020 19:33:12 +0000 Subject: [PATCH] Add barrier() method to Pairer --- source/TransceiverConfig.hpp | 8 ++++++++ source/main.cpp | 28 ++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/source/TransceiverConfig.hpp b/source/TransceiverConfig.hpp index 9b7e56a..e93bfa2 100644 --- a/source/TransceiverConfig.hpp +++ b/source/TransceiverConfig.hpp @@ -53,6 +53,14 @@ namespace com_saxbophone { * @details We currently allow a ±10% timing margin */ const static double DURATION_MARGIN = 0.1; + + /** + * @brief This is the total amount of time allocated for one transmission + * @details This includes START/STOP bits, message bits and a bit of + * silence time for good measure. This is set to 600ms. + * @note This is the only duration in this struct to use ms for units + */ + const static unsigned long TRANMISSION_WINDOW = 600; }; }; diff --git a/source/main.cpp b/source/main.cpp index 7dce448..258df6a 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -96,6 +96,29 @@ public: } } + /** + * @brief Returns to the caller once the amount of time since synchronisation + * reaches a multiple of the transmission window. + * @details This is intended to be used to keep both Master and Slave + * synchronised. Assuming their clocks were synchronised correctly, both can + * call this and the calls should return at near enough the same point in time. + */ + void barrier() { + // TODO: consider allowing for a ±10% margin of error + unsigned long time_now = ubit.systemTime(); // what time is it now? + // this is how much time we have left to next sync point + unsigned long time_left = TransceiverConfig::TRANSMISSION_WINDOW - ( + (time_now - this->synchronisation_timestamp) % + TransceiverConfig::TRANSMISSION_WINDOW + ); + /* + * systemTime() can only sleep to 6ms resolution, so sleep to the nearest + * multiple of 6ms, rounded down + */ + ubit.sleep((time_left / 6) * 6); + // TODO: consider adding busy-wait loop to increase accuracy + } + private: bool pair_as_master() { int pin_state = ubit.io.pin[COMMS_PIN_NUMBER].getDigitalValue(); @@ -572,11 +595,8 @@ int main() { ubit.display.scroll("PS"); } - // display a "P" to indicate we're paired - ubit.display.print('P'); - // call barrier to synchronise both master and slave - // transceiver.barrier(); + pairer.barrier(); /* BEGIN COMMS */ -- GitLab