-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
340 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#include "InputIBUS.hpp" | ||
#include "Utils/MemoryHelper.h" | ||
#include <algorithm> | ||
|
||
namespace Espfc::Device | ||
{ | ||
|
||
InputIBUS::InputIBUS() : _serial(NULL), _state(IBUS_LENGTH), _idx(0), _new_data(false) {} | ||
|
||
int InputIBUS::begin(Device::SerialDevice *serial) | ||
{ | ||
_serial = serial; | ||
|
||
std::fill_n((uint8_t*)&_data, IBUS_FRAME_SIZE, 0); | ||
std::fill_n(_channels, CHANNELS, 0); | ||
|
||
return 1; | ||
} | ||
|
||
InputStatus FAST_CODE_ATTR InputIBUS::update() | ||
{ | ||
if (!_serial) return INPUT_IDLE; | ||
|
||
uint8_t buff[64] = {0}; | ||
size_t len = std::min((size_t)_serial->available(), (size_t)sizeof(buff)); | ||
|
||
if (len) | ||
{ | ||
_serial->readMany(buff, len); | ||
uint8_t* ptr = buff; | ||
while(len--) | ||
{ | ||
parse(_data, *ptr++); | ||
} | ||
} | ||
|
||
if (_new_data) | ||
{ | ||
_new_data = false; | ||
return INPUT_RECEIVED; | ||
} | ||
return INPUT_IDLE; | ||
} | ||
|
||
void FAST_CODE_ATTR InputIBUS::parse(IBusData& frameData, int d) | ||
{ | ||
uint8_t* data = reinterpret_cast<uint8_t*>(&frameData); | ||
uint8_t c = d & 0xff; | ||
switch(_state) | ||
{ | ||
case IBUS_LENGTH: | ||
if(c == IBUS_FRAME_SIZE) | ||
{ | ||
data[_idx++] = c; | ||
_state = IBUS_CMD; | ||
} | ||
break; | ||
case IBUS_CMD: | ||
if(c == IBUS_COMMAND) | ||
{ | ||
data[_idx++] = c; | ||
_state = IBUS_DATA; | ||
} | ||
else | ||
{ | ||
_state = IBUS_LENGTH; | ||
} | ||
break; | ||
case IBUS_DATA: | ||
data[_idx] = c; | ||
if(++_idx >= IBUS_FRAME_SIZE - 2) | ||
{ | ||
_state = IBUS_CRC_LO; | ||
} | ||
break; | ||
case IBUS_CRC_LO: | ||
data[_idx++] = c; | ||
_state = IBUS_CRC_HI; | ||
break; | ||
case IBUS_CRC_HI: | ||
data[_idx++] = c; | ||
uint16_t csum = 0xffff; | ||
for(size_t i = 0; i < IBUS_FRAME_SIZE - 2; i++) | ||
{ | ||
csum -= data[i]; | ||
} | ||
if(frameData.checksum == csum) apply(frameData); | ||
_state = IBUS_LENGTH; | ||
_idx = 0; | ||
break; | ||
} | ||
} | ||
|
||
void FAST_CODE_ATTR InputIBUS::apply(IBusData& data) | ||
{ | ||
for(size_t i = 0; i < CHANNELS; i++) | ||
{ | ||
_channels[i] = data.ch[i]; | ||
} | ||
_new_data = true; | ||
} | ||
|
||
uint16_t FAST_CODE_ATTR InputIBUS::get(uint8_t i) const | ||
{ | ||
return _channels[i]; | ||
} | ||
|
||
void FAST_CODE_ATTR InputIBUS::get(uint16_t *data, size_t len) const | ||
{ | ||
const uint16_t *src = _channels; | ||
while (len--) | ||
{ | ||
*data++ = *src++; | ||
} | ||
} | ||
|
||
size_t InputIBUS::getChannelCount() const { return CHANNELS; } | ||
|
||
bool InputIBUS::needAverage() const { return false; } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
|
||
#include "Device/SerialDevice.h" | ||
#include "Device/InputDevice.h" | ||
|
||
namespace Espfc::Device | ||
{ | ||
|
||
class InputIBUS : public InputDevice | ||
{ | ||
public: | ||
struct IBusData | ||
{ | ||
uint8_t len; | ||
uint8_t cmd; | ||
uint16_t ch[14]; | ||
uint16_t checksum; | ||
} __attribute__((__packed__)); | ||
|
||
InputIBUS(); | ||
|
||
int begin(Device::SerialDevice *serial); | ||
InputStatus update() override; | ||
uint16_t get(uint8_t i) const override; | ||
void get(uint16_t *data, size_t len) const override; | ||
size_t getChannelCount() const override; | ||
bool needAverage() const override; | ||
|
||
void parse(IBusData& frame, int d); | ||
|
||
private: | ||
enum IbusState | ||
{ | ||
IBUS_LENGTH, | ||
IBUS_CMD, | ||
IBUS_DATA, | ||
IBUS_CRC_LO, | ||
IBUS_CRC_HI, | ||
}; | ||
|
||
void apply(IBusData& frame); | ||
|
||
static constexpr size_t IBUS_FRAME_SIZE = sizeof(IBusData); | ||
static constexpr uint8_t IBUS_COMMAND = 0x40; | ||
static constexpr size_t CHANNELS = 14; | ||
|
||
Device::SerialDevice *_serial; | ||
IbusState _state; | ||
uint8_t _idx = 0; | ||
bool _new_data; | ||
|
||
IBusData _data; | ||
uint16_t _channels[CHANNELS]; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
|
||
#include "Device/SerialDevice.h" | ||
#include "Utils/Timer.h" | ||
|
||
namespace Espfc::Output { | ||
|
||
class OutputIBUS | ||
{ | ||
public: | ||
OutputIBUS() {} | ||
|
||
int begin(Device::SerialDevice* serial) | ||
{ | ||
_serial = serial; | ||
_timer.setInterval(7000); // 7ms | ||
|
||
return 1; | ||
} | ||
|
||
int update() | ||
{ | ||
if(!_timer.check()) return 0; | ||
|
||
// const uint8_t data[] = { | ||
// 0x20, 0x40, // preambule (len, cmd) | ||
// 0xDC, 0x05, 0xDC, 0x05, 0xBE, 0x05, 0xDC, 0x05, // channel 1-4 | ||
// 0xD0, 0x07, 0xD0, 0x07, 0xDC, 0x05, 0xDC, 0x05, // channel 5-8 | ||
// 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, // channel 9-12 | ||
// 0xDC, 0x05, 0xDC, 0x05, // channel 13-14 | ||
// 0x83, 0xF3 // checksum | ||
// }; | ||
|
||
const uint8_t data[] = { | ||
0x20, 0x40, | ||
0xDB, 0x05, 0xDC, 0x05, 0x54, 0x05, 0xDC, 0x05, 0xE8, 0x03, 0xD0, 0x07, 0xD2, 0x05, 0xE8, 0x03, | ||
0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, 0xDC, 0x05, | ||
0xDA, 0xF3, | ||
}; | ||
|
||
_serial->write(data, sizeof(data)); | ||
|
||
return 1; | ||
} | ||
|
||
private: | ||
Device::SerialDevice* _serial; | ||
Utils::Timer _timer; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.