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:
2:be66a4cd86c0
Parent:
1:32f0a592b9e5
Child:
3:0b1f4404cb21

File content as of revision 2:be66a4cd86c0:

#include "LidarLitePwm.h"

LidarLitePwm::LidarLitePwm(PinName pin) : _interrupt(pin)
{
    _pulseStartTime = 0;
    _range = 0;
    _timer.start();
    _interrupt.rise(this, &LidarLitePwm::pulseStart);
    _interrupt.fall(this, &LidarLitePwm::pulseStop);
}

LidarLitePwm::~LidarLitePwm(){}

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
}