26 auto byteToBits = [](uint8_t byte) -> std::vector<bool> {
27 std::vector<bool> bits(8);
28 for (uint8_t i = 0; i < 8; ++i)
30 bits[7 - i] = (
byte >> i) & 0x1;
38 constexpr std::bitset<8> flagPattern(
"01111110");
41 for (
size_t i = 0; i < window.size(); ++i)
43 if (window.at(i) != flagPattern[i])
52 std::vector<uint8_t> byteVector((bitBuffer.size() + 7) / 8, 0);
53 for (
size_t i = 0; i < bitBuffer.size(); ++i)
57 byteVector[i / 8] |= (1 << (7 - (i % 8)));
64 auto bits = byteToBits(dataByte);
85 for (
int i = 0; i < 7; ++i)
90 std::vector<uint8_t> byteVector = bitBufferToByteVector(
_bitBuffer);
92 auto p = std::make_unique<uart::protocol::Packet>(byteVector, &
_sensor);
142 for (
size_t i = 0; i < data.size(); ++i, sensor->_runningDataIndex++)
144 auto packetPointer = sensor->findPacket(data.at(i));
146 if (packetPointer !=
nullptr)
148 uart::protocol::Packet packet = *packetPointer;
149 dispatchPacket(dispatchPacketUserData, packet, sensor->_runningDataIndex, timestamp);
156 return uart::protocol::Packet::Type::TYPE_BINARY;
162 if (packet.type() == uart::protocol::Packet::Type::TYPE_BINARY)
166 LOG_CRITICAL(
"Can't calculate checksum of packet with unknown type");
Class to read out LN-200 Sensors.
Utility class for logging to console and file.
#define LOG_CRITICAL(...)
Critical Event, which causes the program to work entirely and throws an exception.
#define LOG_DATA
All output which occurs repeatedly every time observations are received.
Abstract Uart Sensor Class.
@ Receiving
Sensor is receiving data.
State _state
Internal state of the sensor.
Ln200UartSensor()=default
Default constructor.
Ln200UartSensor(std::string name)
Constructor.
const std::string _name
Name of the Parent Node.
static uart::protocol::Packet::Type packetTypeFunction(const uart::protocol::Packet &packet)
Function which is called to determine the packet type (ascii/binary)
static bool isResponseFunction(const uart::protocol::Packet &packet)
Function which determines, if the packet is a Response.
static bool isErrorFunction(const uart::protocol::Packet &packet)
Function which determines, if the packet is an Error Packet.
static void packetFinderFunction(const std::vector< uint8_t > &data, const uart::xplat::TimeStamp ×tamp, uart::sensors::UartSensor::ValidPacketFoundHandler dispatchPacket, void *dispatchPacketUserData, void *userData)
Function which is called to find packets in the provided data buffer.
static bool checksumFunction(const uart::protocol::Packet &packet)
Function which is called to verify packet integrity.
int _consecutiveOnes
Counter for bit insertion.
std::unique_ptr< uart::protocol::Packet > findPacket(uint8_t dataByte)
Collects data bytes and searches for packages inside of them.
uart::sensors::UartSensor _sensor
UartSensor object which handles the UART interface.
bool _wasIdle
Flag to detect the start of a frame.
ScrollingBuffer< bool > _bitBuffer
Buffer to accumulate bits of the current frame.
ScrollingBuffer< bool > _flagWindow
Sliding window buffer to detect flags.
void move(std::vector< T > &v, size_t sourceIdx, size_t targetIdx)
Moves an element within a vector to a new position.