2013-01-03 13:56:23 -04:00
|
|
|
|
2013-01-09 17:32:34 -04:00
|
|
|
#include <AP_HAL_Boards.h>
|
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SMACCM
|
|
|
|
|
2013-01-03 13:56:23 -04:00
|
|
|
#include "RCInput.h"
|
|
|
|
|
2013-01-07 14:48:56 -04:00
|
|
|
#include <hwf4/timer.h>
|
|
|
|
|
2013-01-03 13:56:23 -04:00
|
|
|
using namespace SMACCM;
|
2013-01-07 14:48:56 -04:00
|
|
|
|
|
|
|
/* Constrain captured pulse to be between min and max pulsewidth. */
|
|
|
|
static inline uint16_t constrain_pulse(uint16_t p)
|
|
|
|
{
|
|
|
|
if (p > RC_INPUT_MAX_PULSEWIDTH)
|
|
|
|
return RC_INPUT_MAX_PULSEWIDTH;
|
|
|
|
if (p < RC_INPUT_MIN_PULSEWIDTH)
|
|
|
|
return RC_INPUT_MIN_PULSEWIDTH;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2013-01-03 13:56:23 -04:00
|
|
|
SMACCMRCInput::SMACCMRCInput()
|
2013-01-07 14:48:56 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMACCMRCInput::init(void *unused)
|
|
|
|
{
|
|
|
|
clear_overrides();
|
|
|
|
}
|
2013-01-03 13:56:23 -04:00
|
|
|
|
2013-01-07 14:48:56 -04:00
|
|
|
uint8_t SMACCMRCInput::valid()
|
|
|
|
{
|
|
|
|
// If any of the overrides are positive, we have valid data.
|
2013-01-09 21:19:12 -04:00
|
|
|
for (int i = 0; i < SMACCM_RCINPUT_CHANNELS; ++i)
|
2013-01-07 14:48:56 -04:00
|
|
|
if (_override[i] > 0)
|
|
|
|
return true;
|
2013-01-03 13:56:23 -04:00
|
|
|
|
2013-01-07 14:48:56 -04:00
|
|
|
return timer_is_ppm_valid();
|
2013-01-03 13:56:23 -04:00
|
|
|
}
|
|
|
|
|
2013-01-07 14:48:56 -04:00
|
|
|
// It looks like the APM2 driver clears the PPM sample after this
|
|
|
|
// function is called, so we do the same thing here for compatibility.
|
|
|
|
uint16_t SMACCMRCInput::read(uint8_t ch)
|
|
|
|
{
|
|
|
|
uint16_t result = 0;
|
|
|
|
|
|
|
|
if (_override[ch] != 0) {
|
|
|
|
result = _override[ch];
|
|
|
|
} else {
|
|
|
|
timer_get_ppm_channel(ch, &result);
|
|
|
|
result = constrain_pulse(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
timer_clear_ppm();
|
|
|
|
return constrain_pulse(result);
|
2013-01-03 13:56:23 -04:00
|
|
|
}
|
|
|
|
|
2013-01-07 14:48:56 -04:00
|
|
|
// It looks like the APM2 driver clears the PPM sample after this
|
|
|
|
// function is called, so we do the same thing here for compatibility.
|
|
|
|
uint8_t SMACCMRCInput::read(uint16_t *periods, uint8_t len)
|
|
|
|
{
|
|
|
|
uint8_t result;
|
|
|
|
result = timer_get_ppm(periods, len, NULL);
|
|
|
|
|
|
|
|
for (int i = 0; i < result; ++i) {
|
|
|
|
periods[i] = constrain_pulse(periods[i]);
|
|
|
|
if (_override[i] != 0)
|
|
|
|
periods[i] = _override[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
timer_clear_ppm();
|
|
|
|
return result;
|
2013-01-03 13:56:23 -04:00
|
|
|
}
|
|
|
|
|
2013-01-07 14:48:56 -04:00
|
|
|
bool SMACCMRCInput::set_overrides(int16_t *overrides, uint8_t len)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
result |= set_override(i, overrides[i]);
|
|
|
|
|
|
|
|
return result;
|
2013-01-03 13:56:23 -04:00
|
|
|
}
|
|
|
|
|
2013-01-07 14:48:56 -04:00
|
|
|
bool SMACCMRCInput::set_override(uint8_t channel, int16_t override)
|
|
|
|
{
|
|
|
|
if (override < 0)
|
|
|
|
return false;
|
|
|
|
|
2013-01-09 21:19:12 -04:00
|
|
|
if (channel < SMACCM_RCINPUT_CHANNELS) {
|
2013-01-07 14:48:56 -04:00
|
|
|
_override[channel] = override;
|
|
|
|
if (override != 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-01-03 13:56:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SMACCMRCInput::clear_overrides()
|
2013-01-07 14:48:56 -04:00
|
|
|
{
|
2013-01-09 21:19:12 -04:00
|
|
|
for (int i = 0; i < SMACCM_RCINPUT_CHANNELS; ++i)
|
2013-01-07 14:48:56 -04:00
|
|
|
_override[i] = 0;
|
|
|
|
}
|
2013-01-09 17:32:34 -04:00
|
|
|
|
|
|
|
#endif // CONFIG_HAL_BOARD == HAL_BOARD_SMACCM
|