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

Dependents:   LidarLite_test

Fork of MaxbotixDriver by Daniel Casner

LidarLitePwm.cpp

Committer:
joe4465
Date:
2015-03-18
Revision:
1:32f0a592b9e5
Child:
2:be66a4cd86c0

File content as of revision 1:32f0a592b9e5:

#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
}