Library for the LidarLite, gets distance in cm using the pwm output

Dependents:   LidarLite_test

Fork of MaxbotixDriver by Daniel Casner

Revision:
1:32f0a592b9e5
Child:
2:be66a4cd86c0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LidarLitePwm.cpp	Wed Mar 18 08:55:54 2015 +0000
@@ -0,0 +1,29 @@
+#include "LidarLitePwm.h"
+
+LidarLitePwm::LidarLitePwm(PinName pin, Timer& timer) : _interrupt(pin), _timer(timer), _pulseStartTime(0), _range(0)
+{
+    _interrupt.rise(this, &LidarLitePwm::pulseStart);
+    _interrupt.fall(this, &LidarLitePwm::pulseStop);
+}
+
+int LidarLitePwm::read()
+{
+    return _range;
+}
+
+LidarLitePwm::operator int()
+{
+    return read();
+}
+
+void LidarLitePwm::pulseStart()
+{
+    _pulseStartTime = _timer.read_us();
+}
+
+void LidarLitePwm::pulseStop()
+{
+    int endTime = _timer.read_us();
+    if (endTime < _pulseStartTime) return; // Escape if there's been a roll over
+    _range = (endTime - _pulseStartTime) / 10; // 10uS per CM
+}
\ No newline at end of file