Telescope Control Library

Dependents:   PushToGo-F429

Revision:
0:6cb2eaf8b133
Child:
17:7e47bc1630c0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/StepOut.cpp	Sun Aug 19 05:21:20 2018 +0000
@@ -0,0 +1,91 @@
+/*
+ #include <StepOut.h>
+ * ControllablePWMOut.cpp
+ *
+ *  Created on: 2018Äê2ÔÂ9ÈÕ
+ *      Author: caoyuan9642
+ */
+
+#include "mbed.h"
+#include <StepOut.h>
+
+void StepOut::start()
+{
+	if (status == IDLE && freq > 0) // Start only when idle and frequency is not zero
+	{
+		core_util_critical_section_enter();
+		status = STEPPING;
+		this->write(0.5f);
+		tim.reset();
+		core_util_critical_section_exit();
+	}
+}
+
+void StepOut::stop()
+{
+	if (status == STEPPING)
+	{
+		core_util_critical_section_enter();
+		status = IDLE;
+		this->write(0);
+		stepCount += (int64_t) (freq * tim.read_high_resolution_us() / 1.0E6);
+		core_util_critical_section_exit();
+	}
+}
+
+double StepOut::setFrequency(double frequency)
+{
+	if (frequency > 0)
+	{
+		int64_t us_period = ceil(1.0E6 / frequency); /*Ceil to the next microsecond*/
+		if (us_period > INT_MAX)
+		{
+			// Prevent overflow
+			us_period = INT_MAX;
+		}
+		if (status == IDLE)
+			this->period_us(us_period);
+		else
+		{
+			core_util_critical_section_enter();
+			stop(); /*Stop to correctly update the stepCount*/
+			this->period_us(us_period);
+			start();
+			core_util_critical_section_exit();
+		}
+		freq = 1.0E6 / us_period; // get CORRECT frequency!
+	}
+	else
+	{
+		// frequency=0 effectively means stop
+		freq = 0;
+		if (status == STEPPING)
+		{
+			stop();
+		}
+	}
+	return freq; // Return the accurate period
+}
+
+void StepOut::resetCount()
+{
+	core_util_critical_section_enter();
+	stepCount = 0;
+	if (status == STEPPING)
+		tim.reset();
+	core_util_critical_section_exit();
+}
+
+int64_t StepOut::getCount()
+{
+	if (status == IDLE)
+		return stepCount;
+	else
+	{
+		// Fixed: use read_high_resolution_us() to prevent overflow every ~30min
+		return stepCount
+				+ (int64_t) (freq * tim.read_high_resolution_us() / 1.0E6); /*Calculate count at now*/
+	}
+}
+
+