Test program for AccurateWaiter

Dependencies:   SerialStream AccurateWaiter

This test program shows how to use AccurateWaiter and demonstrates its performance.

Here's what it looks like running on a NUCLEO_F429ZI: https://app.box.com/shared/static/a7vuulqeqodh7pn5skgvniwo0b4h3pmj.png

Files at this revision

API Documentation at this revision

Comitter:
Jamie Smith
Date:
Thu Nov 12 23:17:01 2020 -0800
Commit message:
Initial commit

Changed in this revision

.gitignore Show annotated file Show diff for this revision Revisions of this file
.hgignore Show annotated file Show diff for this revision Revisions of this file
AccurateWaiter.lib Show annotated file Show diff for this revision Revisions of this file
SerialStream.lib Show annotated file Show diff for this revision Revisions of this file
TestAccurateWaiter.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
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.gitignore	Thu Nov 12 23:17:01 2020 -0800
@@ -0,0 +1,7 @@
+.build
+.mbed
+projectfiles
+*.py*
+mbed-os
+BUILD
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Thu Nov 12 23:17:01 2020 -0800
@@ -0,0 +1,5 @@
+^BUILD$
+^.mbed$
+^mbed-os$
+^AccurateWaiter$
+^SerialStream$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AccurateWaiter.lib	Thu Nov 12 23:17:01 2020 -0800
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/MultipleMonomials/code/AccurateWaiter/#85e56bb98f6e969f9f47c9ce11fd20a1dc0235c0
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialStream.lib	Thu Nov 12 23:17:01 2020 -0800
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/MultipleMonomials/code/SerialStream/#3736580f2dbeb8adf0607fcfd0f434b4faa08a03
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TestAccurateWaiter.cpp	Thu Nov 12 23:17:01 2020 -0800
@@ -0,0 +1,66 @@
+//
+// Created by jamie on 11/12/2020.
+//
+
+#include "AccurateWaiter.h"
+#include "SerialStream.h"
+#include <cinttypes>
+#include <random>
+
+namespace chrono = std::chrono;
+
+BufferedSerial serial(USBTX, USBRX, 115200);
+SerialStream<BufferedSerial> pc(serial);
+
+std::mt19937 randomGen;
+std::uniform_int_distribution<uint64_t> distrib(0, 1e6);
+
+// Thread to perform a precise wait and print the results
+void testWaiting()
+{
+	AccurateWaiter waiter;
+
+	while(true)
+	{
+		// choose a random wait time
+		chrono::microseconds randomWaitTime(distrib(randomGen));
+
+		auto startTime = waiter.clock().now();
+		waiter.wait_for(randomWaitTime);
+		auto endTime = waiter.clock().now();
+
+		uint64_t actualWaitTime = chrono::duration_cast<chrono::microseconds>(endTime - startTime).count();
+		pc.printf("Expected time: %" PRIu64 " us, actual: %" PRIu64 " us, offset = %" PRIi64 "us\n", randomWaitTime.count(), actualWaitTime,
+			static_cast<int64_t>(actualWaitTime) - static_cast<int64_t>(randomWaitTime.count()));
+	}
+}
+
+// infinite loop which is run at normal priority
+void infiniteLoop()
+{
+	int workload;
+	while(true)
+	{
+		workload++;
+	}
+}
+
+int main()
+{
+	// print info
+	pc.printf("---------------------------------------------\n");
+	pc.printf("        Starting Accurate Waiter Test\n");
+	pc.printf("---------------------------------------------\n");
+
+	pc.printf("us Ticker Resolution: %.02f us\n", 1e6f/us_ticker_get_info()->frequency);
+
+	// start timing thread
+	Thread timingThread(osPriorityHigh);
+	timingThread.start(callback(testWaiting));
+    
+    while(true) {}
+
+	infiniteLoop();
+
+	return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Thu Nov 12 23:17:01 2020 -0800
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os.git#0db72d0cf26539016efbe38f80d6f2cb7a3d4414
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Thu Nov 12 23:17:01 2020 -0800
@@ -0,0 +1,8 @@
+{
+    "target_overrides": {
+        "*": {
+            "platform.stdio-baud-rate": 115200,
+            "target.printf_lib": "std"
+        }
+    }
+}
\ No newline at end of file