Compare commits
2 Commits
ce4f8eb8f5
...
55a8efa579
| Author | SHA1 | Date | |
|---|---|---|---|
| 55a8efa579 | |||
| 5c8e6f09b9 |
67
.clang-format
Normal file
67
.clang-format
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
# Generated from CLion C/C++ Code Style settings
|
||||||
|
BasedOnStyle: LLVM
|
||||||
|
AccessModifierOffset: -2
|
||||||
|
AlignAfterOpenBracket: Align
|
||||||
|
AlignConsecutiveAssignments: false
|
||||||
|
AlignOperands: true
|
||||||
|
AllowAllArgumentsOnNextLine: false
|
||||||
|
AllowAllConstructorInitializersOnNextLine: false
|
||||||
|
AllowAllParametersOfDeclarationOnNextLine: false
|
||||||
|
AllowShortBlocksOnASingleLine: Always
|
||||||
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
|
AllowShortFunctionsOnASingleLine: All
|
||||||
|
AllowShortIfStatementsOnASingleLine: Always
|
||||||
|
AllowShortLambdasOnASingleLine: All
|
||||||
|
AllowShortLoopsOnASingleLine: true
|
||||||
|
AlwaysBreakAfterReturnType: None
|
||||||
|
AlwaysBreakTemplateDeclarations: Yes
|
||||||
|
BreakBeforeBraces: Custom
|
||||||
|
BraceWrapping:
|
||||||
|
AfterCaseLabel: false
|
||||||
|
AfterClass: true
|
||||||
|
AfterControlStatement: Always
|
||||||
|
AfterEnum: true
|
||||||
|
AfterFunction: true
|
||||||
|
AfterNamespace: true
|
||||||
|
AfterUnion: true
|
||||||
|
AfterStruct: true
|
||||||
|
BeforeCatch: false
|
||||||
|
BeforeElse: false
|
||||||
|
IndentBraces: false
|
||||||
|
SplitEmptyFunction: false
|
||||||
|
SplitEmptyRecord: true
|
||||||
|
BreakBeforeBinaryOperators: None
|
||||||
|
BreakBeforeTernaryOperators: true
|
||||||
|
BreakConstructorInitializers: BeforeColon
|
||||||
|
BreakInheritanceList: BeforeComma
|
||||||
|
ColumnLimit: 0
|
||||||
|
CompactNamespaces: false
|
||||||
|
ContinuationIndentWidth: 8
|
||||||
|
IndentCaseLabels: true
|
||||||
|
IndentPPDirectives: None
|
||||||
|
IndentWidth: 2
|
||||||
|
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||||
|
MaxEmptyLinesToKeep: 2
|
||||||
|
NamespaceIndentation: None
|
||||||
|
ObjCSpaceAfterProperty: false
|
||||||
|
ObjCSpaceBeforeProtocolList: true
|
||||||
|
PointerAlignment: Left
|
||||||
|
ReflowComments: false
|
||||||
|
SpaceAfterCStyleCast: true
|
||||||
|
SpaceAfterLogicalNot: false
|
||||||
|
SpaceAfterTemplateKeyword: false
|
||||||
|
SpaceBeforeAssignmentOperators: true
|
||||||
|
SpaceBeforeCpp11BracedList: false
|
||||||
|
SpaceBeforeCtorInitializerColon: true
|
||||||
|
SpaceBeforeInheritanceColon: true
|
||||||
|
SpaceBeforeParens: ControlStatements
|
||||||
|
SpaceBeforeRangeBasedForLoopColon: true
|
||||||
|
SpaceInEmptyParentheses: false
|
||||||
|
SpacesBeforeTrailingComments: 0
|
||||||
|
SpacesInAngles: false
|
||||||
|
SpacesInCStyleCastParentheses: false
|
||||||
|
SpacesInContainerLiterals: false
|
||||||
|
SpacesInParentheses: false
|
||||||
|
SpacesInSquareBrackets: false
|
||||||
|
TabWidth: 2
|
||||||
|
UseTab: Never
|
||||||
@ -8,9 +8,9 @@
|
|||||||
#ifndef MESSAGING_INC_MESSAGING_PACKET_HPP_
|
#ifndef MESSAGING_INC_MESSAGING_PACKET_HPP_
|
||||||
#define MESSAGING_INC_MESSAGING_PACKET_HPP_
|
#define MESSAGING_INC_MESSAGING_PACKET_HPP_
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
#include <cstdint>
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
namespace Messaging
|
namespace Messaging
|
||||||
{
|
{
|
||||||
@ -34,13 +34,16 @@ struct Packet
|
|||||||
}
|
}
|
||||||
|
|
||||||
Packet() = default;
|
Packet() = default;
|
||||||
|
|
||||||
Packet(const Packet&) = default;
|
Packet(const Packet&) = default;
|
||||||
|
|
||||||
Packet(Packet&&) noexcept = default;
|
Packet(Packet&&) noexcept = default;
|
||||||
|
|
||||||
template<std::size_t data_in_length>
|
template<std::size_t data_in_length>
|
||||||
void copy_data_in(const std::uint8_t (&data_in)[data_in_length])
|
void copy_data_in(const std::uint8_t (&data_in)[data_in_length])
|
||||||
{
|
{
|
||||||
const std::uint32_t to_copy_length = std::min(std::uint32_t(data_in_length), max_data_length);
|
const std::uint32_t to_copy_length =
|
||||||
|
std::min(std::uint32_t(data_in_length), max_data_length);
|
||||||
|
|
||||||
std::memcpy(data.data(), data_in, to_copy_length);
|
std::memcpy(data.data(), data_in, to_copy_length);
|
||||||
data_length = to_copy_length;
|
data_length = to_copy_length;
|
||||||
@ -49,7 +52,8 @@ struct Packet
|
|||||||
template<std::size_t data_in_length>
|
template<std::size_t data_in_length>
|
||||||
void copy_data_in(const std::array<std::uint8_t, data_in_length>& data_in)
|
void copy_data_in(const std::array<std::uint8_t, data_in_length>& data_in)
|
||||||
{
|
{
|
||||||
const std::uint32_t to_copy_length = std::min(std::uint32_t(data_in_length), max_data_length);
|
const std::uint32_t to_copy_length =
|
||||||
|
std::min(std::uint32_t(data_in_length), max_data_length);
|
||||||
|
|
||||||
std::memcpy(data.data(), data_in.data(), to_copy_length);
|
std::memcpy(data.data(), data_in.data(), to_copy_length);
|
||||||
data_length = to_copy_length;
|
data_length = to_copy_length;
|
||||||
@ -57,7 +61,8 @@ struct Packet
|
|||||||
|
|
||||||
bool serialize(std::uint8_t* buffer, const std::uint32_t max_length)
|
bool serialize(std::uint8_t* buffer, const std::uint32_t max_length)
|
||||||
{
|
{
|
||||||
const std::uint32_t required_size = preamble_length + data_length_length + data_length;
|
const std::uint32_t required_size =
|
||||||
|
preamble_length + data_length_length + data_length;
|
||||||
|
|
||||||
if (max_length < required_size)
|
if (max_length < required_size)
|
||||||
return false;
|
return false;
|
||||||
@ -99,7 +104,6 @@ struct Packet
|
|||||||
template<std::size_t N>
|
template<std::size_t N>
|
||||||
constexpr std::uint8_t Packet<N>::preamble[2];
|
constexpr std::uint8_t Packet<N>::preamble[2];
|
||||||
|
|
||||||
}
|
}// namespace Messaging
|
||||||
|
|
||||||
|
|
||||||
#endif /* MESSAGING_INC_MESSAGING_PACKET_HPP_ */
|
#endif /* MESSAGING_INC_MESSAGING_PACKET_HPP_ */
|
||||||
|
|||||||
@ -5,9 +5,9 @@
|
|||||||
#ifndef SKULLC_MESSAGING_PARSER_HPP
|
#ifndef SKULLC_MESSAGING_PARSER_HPP
|
||||||
#define SKULLC_MESSAGING_PARSER_HPP
|
#define SKULLC_MESSAGING_PARSER_HPP
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <array>
|
|
||||||
|
|
||||||
namespace Messaging
|
namespace Messaging
|
||||||
{
|
{
|
||||||
@ -19,11 +19,10 @@ public:
|
|||||||
using Packet = P;
|
using Packet = P;
|
||||||
const std::size_t buffer_length = N;
|
const std::size_t buffer_length = N;
|
||||||
|
|
||||||
Parser()
|
Parser() { reset(); }
|
||||||
{
|
|
||||||
reset();
|
|
||||||
}
|
|
||||||
Parser(const Parser&) = delete;
|
Parser(const Parser&) = delete;
|
||||||
|
|
||||||
Parser(Parser&&) = delete;
|
Parser(Parser&&) = delete;
|
||||||
|
|
||||||
void reset()
|
void reset()
|
||||||
@ -65,10 +64,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool packetReady() const
|
bool packetReady() const { return _state == _State::Done; }
|
||||||
{
|
|
||||||
return _state == _State::Done;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool getPacket(Packet& packet) const
|
bool getPacket(Packet& packet) const
|
||||||
{
|
{
|
||||||
@ -126,6 +122,6 @@ private:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}// namespace Messaging
|
||||||
|
|
||||||
#endif// SKULLC_MESSAGING_PARSER_HPP
|
#endif// SKULLC_MESSAGING_PARSER_HPP
|
||||||
|
|||||||
@ -8,8 +8,8 @@
|
|||||||
#ifndef PERIPHERALS_INC_PERIPHERALS_ADC_HPP_
|
#ifndef PERIPHERALS_INC_PERIPHERALS_ADC_HPP_
|
||||||
#define PERIPHERALS_INC_PERIPHERALS_ADC_HPP_
|
#define PERIPHERALS_INC_PERIPHERALS_ADC_HPP_
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
#include <adc.h>
|
#include <adc.h>
|
||||||
|
|
||||||
@ -22,16 +22,11 @@ struct Adc
|
|||||||
std::array<std::uint16_t, N> readings = {0};
|
std::array<std::uint16_t, N> readings = {0};
|
||||||
ADC_HandleTypeDef* hadc = nullptr;
|
ADC_HandleTypeDef* hadc = nullptr;
|
||||||
|
|
||||||
static constexpr std::uint32_t readingsCount()
|
static constexpr std::uint32_t readingsCount() { return N; }
|
||||||
{
|
|
||||||
return N;
|
|
||||||
}
|
|
||||||
|
|
||||||
Adc() = delete;
|
Adc() = delete;
|
||||||
|
|
||||||
explicit Adc(ADC_HandleTypeDef* hadc)
|
explicit Adc(ADC_HandleTypeDef* hadc) : hadc(hadc) {}
|
||||||
: hadc(hadc)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
Adc(const Adc&) = delete;
|
Adc(const Adc&) = delete;
|
||||||
Adc(Adc&&) = delete;
|
Adc(Adc&&) = delete;
|
||||||
@ -56,16 +51,13 @@ struct Adc
|
|||||||
|
|
||||||
void startDma()
|
void startDma()
|
||||||
{
|
{
|
||||||
HAL_ADC_Start_DMA(hadc, reinterpret_cast<std::uint32_t*>(readings.data()), readingsCount());
|
HAL_ADC_Start_DMA(hadc, reinterpret_cast<std::uint32_t*>(readings.data()),
|
||||||
|
readingsCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
void stopDma()
|
void stopDma() { HAL_ADC_Stop_DMA(hadc); }
|
||||||
{
|
|
||||||
HAL_ADC_Stop_DMA(hadc);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}// namespace Peripherals
|
||||||
|
|
||||||
|
|
||||||
#endif /* PERIPHERALS_INC_PERIPHERALS_ADC_HPP_ */
|
#endif /* PERIPHERALS_INC_PERIPHERALS_ADC_HPP_ */
|
||||||
|
|||||||
@ -24,17 +24,12 @@ namespace St
|
|||||||
template<typename Origin>
|
template<typename Origin>
|
||||||
using IsrCallbackFn = void (*)(Origin*);
|
using IsrCallbackFn = void (*)(Origin*);
|
||||||
|
|
||||||
template<typename Origin, typename Handler, void (Handler::*func)(), typename Tag>
|
template<typename Origin, typename Handler, void (Handler::*func)(),
|
||||||
|
typename Tag>
|
||||||
IsrCallbackFn<Origin> createCallback(Handler& h_in)
|
IsrCallbackFn<Origin> createCallback(Handler& h_in)
|
||||||
{
|
{
|
||||||
static Handler* h = &h_in;
|
static Handler* h = &h_in;
|
||||||
printf("Set handler: %p\n", h);
|
return +[](Origin*) { (h->*func)(); };
|
||||||
return +[](Origin*)
|
|
||||||
{
|
|
||||||
printf("Into callback we go.\n");
|
|
||||||
(h->*func)();
|
|
||||||
printf("And we're out.\n");
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct StaticHal
|
struct StaticHal
|
||||||
@ -59,21 +54,16 @@ struct StaticHal
|
|||||||
const std::uint32_t tick_start = DWT->CYCCNT;
|
const std::uint32_t tick_start = DWT->CYCCNT;
|
||||||
const std::uint32_t ticks_delay = micros * (SystemCoreClock / 1'000'000);
|
const std::uint32_t ticks_delay = micros * (SystemCoreClock / 1'000'000);
|
||||||
|
|
||||||
while (DWT->CYCCNT - tick_start < ticks_delay);
|
while (DWT->CYCCNT - tick_start < ticks_delay)
|
||||||
|
;
|
||||||
#else
|
#else
|
||||||
(void) micros;
|
(void) micros;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void enableInterrupts()
|
static void enableInterrupts() { __enable_irq(); }
|
||||||
{
|
|
||||||
__enable_irq();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void disableInterrupts()
|
static void disableInterrupts() { __disable_irq(); }
|
||||||
{
|
|
||||||
__disable_irq();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef HAL_GPIO_MODULE_ENABLED
|
#ifdef HAL_GPIO_MODULE_ENABLED
|
||||||
@ -85,40 +75,33 @@ struct Gpio
|
|||||||
|
|
||||||
Gpio() = delete;
|
Gpio() = delete;
|
||||||
explicit Gpio(GPIO_TypeDef* port, const std::uint16_t pin)
|
explicit Gpio(GPIO_TypeDef* port, const std::uint16_t pin)
|
||||||
: port(port)
|
: port(port), pin(pin) {}
|
||||||
, pin(pin)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
void Set(const bool& state)
|
void Set(const bool& state)
|
||||||
{
|
{
|
||||||
HAL_GPIO_WritePin(port, pin, GPIO_PinState(state));
|
HAL_GPIO_WritePin(port, pin, GPIO_PinState(state));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Toggle()
|
void Toggle() { HAL_GPIO_TogglePin(port, pin); }
|
||||||
{
|
|
||||||
HAL_GPIO_TogglePin(port, pin);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Read() const
|
bool Read() const { return HAL_GPIO_ReadPin(port, pin); }
|
||||||
{
|
|
||||||
return HAL_GPIO_ReadPin(port, pin);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif// HAL_GPIO_MODULE_ENABLED
|
#endif// HAL_GPIO_MODULE_ENABLED
|
||||||
|
|
||||||
template<typename T,
|
template<
|
||||||
HAL_StatusTypeDef (*transmit)(T*, std::uint8_t* data, std::uint16_t data_len, std::uint32_t timeout),
|
typename T,
|
||||||
HAL_StatusTypeDef (*receive)(T*, std::uint8_t* data, std::uint16_t data_len, std::uint32_t timeout)>
|
HAL_StatusTypeDef (*transmit)(
|
||||||
|
T*, std::uint8_t* data, std::uint16_t data_len, std::uint32_t timeout),
|
||||||
|
HAL_StatusTypeDef (*receive)(T*, std::uint8_t* data,
|
||||||
|
std::uint16_t data_len, std::uint32_t timeout)>
|
||||||
struct SerialInterface
|
struct SerialInterface
|
||||||
{
|
{
|
||||||
using underlying_handle_type = T;
|
using underlying_handle_type = T;
|
||||||
underlying_handle_type* handle;
|
underlying_handle_type* handle;
|
||||||
|
|
||||||
SerialInterface() = delete;
|
SerialInterface() = delete;
|
||||||
explicit SerialInterface(underlying_handle_type* handle)
|
explicit SerialInterface(underlying_handle_type* handle) : handle(handle) {}
|
||||||
: handle(handle)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
bool Transmit(std::uint8_t* data, const std::uint32_t data_len)
|
bool Transmit(std::uint8_t* data, const std::uint32_t data_len)
|
||||||
{
|
{
|
||||||
@ -132,8 +115,10 @@ struct SerialInterface
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<typename T,
|
template<typename T,
|
||||||
HAL_StatusTypeDef (*transmit)(T*, std::uint8_t* data, std::uint16_t data_len),
|
HAL_StatusTypeDef (*transmit)(T*, std::uint8_t* data,
|
||||||
HAL_StatusTypeDef (*receive)(T*, std::uint8_t* data, std::uint16_t data_len)>
|
std::uint16_t data_len),
|
||||||
|
HAL_StatusTypeDef (*receive)(T*, std::uint8_t* data,
|
||||||
|
std::uint16_t data_len)>
|
||||||
struct SerialInterfaceAsync
|
struct SerialInterfaceAsync
|
||||||
{
|
{
|
||||||
using underlying_handle_type = T;
|
using underlying_handle_type = T;
|
||||||
@ -141,8 +126,7 @@ struct SerialInterfaceAsync
|
|||||||
|
|
||||||
SerialInterfaceAsync() = delete;
|
SerialInterfaceAsync() = delete;
|
||||||
explicit SerialInterfaceAsync(underlying_handle_type* handle)
|
explicit SerialInterfaceAsync(underlying_handle_type* handle)
|
||||||
: handle(handle)
|
: handle(handle) {}
|
||||||
{ }
|
|
||||||
|
|
||||||
bool Transmit(std::uint8_t* data, const std::uint32_t data_len)
|
bool Transmit(std::uint8_t* data, const std::uint32_t data_len)
|
||||||
{
|
{
|
||||||
@ -157,7 +141,8 @@ struct SerialInterfaceAsync
|
|||||||
|
|
||||||
#ifdef HAL_SPI_MODULE_ENABLED
|
#ifdef HAL_SPI_MODULE_ENABLED
|
||||||
|
|
||||||
using SpiInterface = SerialInterface<SPI_HandleTypeDef, HAL_SPI_Transmit, HAL_SPI_Receive>;
|
using SpiInterface =
|
||||||
|
SerialInterface<SPI_HandleTypeDef, HAL_SPI_Transmit, HAL_SPI_Receive>;
|
||||||
|
|
||||||
struct SpiRegisters
|
struct SpiRegisters
|
||||||
{
|
{
|
||||||
@ -166,9 +151,7 @@ struct SpiRegisters
|
|||||||
|
|
||||||
SpiRegisters() = delete;
|
SpiRegisters() = delete;
|
||||||
explicit SpiRegisters(const SpiInterface& handle, const Gpio& chip_select)
|
explicit SpiRegisters(const SpiInterface& handle, const Gpio& chip_select)
|
||||||
: handle(handle)
|
: handle(handle), chip_select(chip_select) {}
|
||||||
, chip_select(chip_select)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
void WriteRegister(std::uint8_t reg, uint8_t data)
|
void WriteRegister(std::uint8_t reg, uint8_t data)
|
||||||
{
|
{
|
||||||
@ -180,7 +163,8 @@ struct SpiRegisters
|
|||||||
chip_select.Set(true);
|
chip_select.Set(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteRegisterMultibyte(std::uint8_t reg, std::uint8_t* data, const std::uint32_t len)
|
void WriteRegisterMultibyte(std::uint8_t reg, std::uint8_t* data,
|
||||||
|
const std::uint32_t len)
|
||||||
{
|
{
|
||||||
chip_select.Set(false);
|
chip_select.Set(false);
|
||||||
|
|
||||||
@ -190,7 +174,8 @@ struct SpiRegisters
|
|||||||
chip_select.Set(true);
|
chip_select.Set(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::uint8_t ReadRegister(std::uint8_t reg, const std::uint32_t read_delay = 0)
|
std::uint8_t ReadRegister(std::uint8_t reg,
|
||||||
|
const std::uint32_t read_delay = 0)
|
||||||
{
|
{
|
||||||
chip_select.Set(false);
|
chip_select.Set(false);
|
||||||
|
|
||||||
@ -208,7 +193,9 @@ struct SpiRegisters
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReadRegisterMultibyte(std::uint8_t reg, std::uint8_t* data, const std::uint32_t len, const std::uint32_t read_delay = 0)
|
void ReadRegisterMultibyte(std::uint8_t reg, std::uint8_t* data,
|
||||||
|
const std::uint32_t len,
|
||||||
|
const std::uint32_t read_delay = 0)
|
||||||
{
|
{
|
||||||
chip_select.Set(false);
|
chip_select.Set(false);
|
||||||
|
|
||||||
@ -227,8 +214,11 @@ struct SpiRegisters
|
|||||||
|
|
||||||
#ifdef HAL_UART_MODULE_ENABLED
|
#ifdef HAL_UART_MODULE_ENABLED
|
||||||
|
|
||||||
using UartInterface = SerialInterface<UART_HandleTypeDef, HAL_UART_Transmit, HAL_UART_Receive>;
|
using UartInterface =
|
||||||
using UartInterfaceDMA = SerialInterfaceAsync<UART_HandleTypeDef, HAL_UART_Transmit_DMA, HAL_UART_Receive_DMA>;
|
SerialInterface<UART_HandleTypeDef, HAL_UART_Transmit, HAL_UART_Receive>;
|
||||||
|
using UartInterfaceDMA =
|
||||||
|
SerialInterfaceAsync<UART_HandleTypeDef, HAL_UART_Transmit_DMA,
|
||||||
|
HAL_UART_Receive_DMA>;
|
||||||
|
|
||||||
#endif// HAL_UART_MODULE_ENABLED
|
#endif// HAL_UART_MODULE_ENABLED
|
||||||
|
|
||||||
@ -241,29 +231,18 @@ struct PwmChannel
|
|||||||
|
|
||||||
PwmChannel() = delete;
|
PwmChannel() = delete;
|
||||||
explicit PwmChannel(TIM_HandleTypeDef* handle, const std::uint32_t channel)
|
explicit PwmChannel(TIM_HandleTypeDef* handle, const std::uint32_t channel)
|
||||||
: handle(handle)
|
: handle(handle), channel(channel) {}
|
||||||
, channel(channel)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
void Enable()
|
void Enable() { HAL_TIM_PWM_Start(handle, channel); }
|
||||||
{
|
|
||||||
HAL_TIM_PWM_Start(handle, channel);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Disable()
|
void Disable() { HAL_TIM_PWM_Stop(handle, channel); }
|
||||||
{
|
|
||||||
HAL_TIM_PWM_Stop(handle, channel);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetCompare(const std::uint32_t compare)
|
void SetCompare(const std::uint32_t compare)
|
||||||
{
|
{
|
||||||
__HAL_TIM_SET_COMPARE(handle, channel, compare);
|
__HAL_TIM_SET_COMPARE(handle, channel, compare);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::uint32_t MaxValue()
|
std::uint32_t MaxValue() { return handle->Init.Period; }
|
||||||
{
|
|
||||||
return handle->Init.Period;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif// HAL_TIM_MODULE_ENABLED
|
#endif// HAL_TIM_MODULE_ENABLED
|
||||||
@ -280,8 +259,8 @@ struct ItmSerialInterface
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}// namespace St
|
||||||
}
|
}// namespace Hal
|
||||||
}
|
}// namespace Peripherals
|
||||||
|
|
||||||
#endif /* PERIPHERALS_INC_PERIPHERALS_HAL_ST_HPP_ */
|
#endif /* PERIPHERALS_INC_PERIPHERALS_HAL_ST_HPP_ */
|
||||||
|
|||||||
@ -33,7 +33,6 @@ public:
|
|||||||
virtual void ReadAccelerometerRaw(std::int16_t* output) = 0;
|
virtual void ReadAccelerometerRaw(std::int16_t* output) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}// namespace Peripherals
|
||||||
|
|
||||||
|
|
||||||
#endif /* PERIPHERALS_IMU_HPP_ */
|
#endif /* PERIPHERALS_IMU_HPP_ */
|
||||||
|
|||||||
@ -43,25 +43,29 @@ public:
|
|||||||
registers_handle registers;
|
registers_handle registers;
|
||||||
|
|
||||||
ImuIcm() = delete;
|
ImuIcm() = delete;
|
||||||
ImuIcm(const registers_handle& registers)
|
ImuIcm(const registers_handle& registers) : registers(registers) {}
|
||||||
: registers(registers)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
void Setup() override
|
void Setup() override
|
||||||
{
|
{
|
||||||
registers.WriteRegister(_Registers::PWR_MGMT_1 & _Registers::WRITE_MASK, 0b10000000);
|
registers.WriteRegister(_Registers::PWR_MGMT_1 & _Registers::WRITE_MASK,
|
||||||
|
0b10000000);
|
||||||
hal::Delay(10);
|
hal::Delay(10);
|
||||||
|
|
||||||
registers.WriteRegister(_Registers::USER_CTRL & _Registers::WRITE_MASK, 0b00010000);
|
registers.WriteRegister(_Registers::USER_CTRL & _Registers::WRITE_MASK,
|
||||||
|
0b00010000);
|
||||||
hal::Delay(10);
|
hal::Delay(10);
|
||||||
|
|
||||||
registers.WriteRegister(_Registers::PWR_MGMT_1 & _Registers::WRITE_MASK, 0b00000000);
|
registers.WriteRegister(_Registers::PWR_MGMT_1 & _Registers::WRITE_MASK,
|
||||||
|
0b00000000);
|
||||||
hal::Delay(10);
|
hal::Delay(10);
|
||||||
|
|
||||||
registers.WriteRegister(_Registers::PWR_MGMT_1 & _Registers::WRITE_MASK, 0b00000001);
|
registers.WriteRegister(_Registers::PWR_MGMT_1 & _Registers::WRITE_MASK,
|
||||||
|
0b00000001);
|
||||||
hal::Delay(10);
|
hal::Delay(10);
|
||||||
|
|
||||||
registers.WriteRegister(_Registers::CONFIG & _Registers::WRITE_MASK, 0x03); // DLPF_CFG = 3, gyro filter = 41/59.0, gyro rate = 1KHz, temp filter = 42
|
registers.WriteRegister(_Registers::CONFIG & _Registers::WRITE_MASK,
|
||||||
|
0x03);// DLPF_CFG = 3, gyro filter = 41/59.0, gyro
|
||||||
|
// rate = 1KHz, temp filter = 42
|
||||||
hal::Delay(10);
|
hal::Delay(10);
|
||||||
|
|
||||||
SetGyroscopeScale(_scale_gyro);
|
SetGyroscopeScale(_scale_gyro);
|
||||||
@ -69,10 +73,12 @@ public:
|
|||||||
SetAccelerometerScale(_scale_accel);
|
SetAccelerometerScale(_scale_accel);
|
||||||
|
|
||||||
// ACCEL_FCHOICE_B = 0, A_DLPF_CFG = 3 filter=44.8/61.5 rate=1KHz
|
// ACCEL_FCHOICE_B = 0, A_DLPF_CFG = 3 filter=44.8/61.5 rate=1KHz
|
||||||
registers.WriteRegister(_Registers::ACCEL_CONFIG2 & _Registers::WRITE_MASK, 0x03);
|
registers.WriteRegister(_Registers::ACCEL_CONFIG2 & _Registers::WRITE_MASK,
|
||||||
|
0x03);
|
||||||
hal::Delay(10);
|
hal::Delay(10);
|
||||||
|
|
||||||
// SAMPLE_RATE = INTERNAL_SAMPLE_RATE / (1 + SMPLRT_DIV) Where INTERNAL_SAMPLE_RATE = 1kHz
|
// SAMPLE_RATE = INTERNAL_SAMPLE_RATE / (1 + SMPLRT_DIV) Where
|
||||||
|
// INTERNAL_SAMPLE_RATE = 1kHz
|
||||||
registers.WriteRegister(_Registers::SMPLRT_DIV & _Registers::WRITE_MASK, 0);
|
registers.WriteRegister(_Registers::SMPLRT_DIV & _Registers::WRITE_MASK, 0);
|
||||||
hal::Delay(10);
|
hal::Delay(10);
|
||||||
|
|
||||||
@ -82,7 +88,8 @@ public:
|
|||||||
// INT/DRDY pin is configured as push-pull.
|
// INT/DRDY pin is configured as push-pull.
|
||||||
// INT/DRDY pin indicates interrupt pulse's width is 50us.
|
// INT/DRDY pin indicates interrupt pulse's width is 50us.
|
||||||
// Interrupt status is cleared only by reading INT_STATUS register
|
// Interrupt status is cleared only by reading INT_STATUS register
|
||||||
registers.WriteRegister(_Registers::INT_PIN_CFG & _Registers::WRITE_MASK, 0);
|
registers.WriteRegister(_Registers::INT_PIN_CFG & _Registers::WRITE_MASK,
|
||||||
|
0);
|
||||||
hal::Delay(10);
|
hal::Delay(10);
|
||||||
|
|
||||||
registers.WriteRegister(_Registers::INT_ENABLE & _Registers::WRITE_MASK, 1);
|
registers.WriteRegister(_Registers::INT_ENABLE & _Registers::WRITE_MASK, 1);
|
||||||
@ -97,8 +104,7 @@ public:
|
|||||||
for (std::uint32_t i = 0; i < samples; i++)
|
for (std::uint32_t i = 0; i < samples; i++)
|
||||||
{
|
{
|
||||||
std::array<std::int16_t, 3> raw;
|
std::array<std::int16_t, 3> raw;
|
||||||
auto add_to_avg = [&raw](std::array<std::int32_t, 3>& out)
|
auto add_to_avg = [&raw](std::array<std::int32_t, 3>& out) {
|
||||||
{
|
|
||||||
for (std::uint32_t j = 0; j < 3; j++)
|
for (std::uint32_t j = 0; j < 3; j++)
|
||||||
out[j] += raw[j];
|
out[j] += raw[j];
|
||||||
};
|
};
|
||||||
@ -124,18 +130,24 @@ public:
|
|||||||
|
|
||||||
void SetGyroscopeScale(const GyroScale scale)
|
void SetGyroscopeScale(const GyroScale scale)
|
||||||
{
|
{
|
||||||
const std::uint8_t current_config = registers.ReadRegister(_Registers::GYRO_CONFIG | _Registers::READ_MASK);
|
const std::uint8_t current_config =
|
||||||
const std::uint8_t new_config = (current_config & 0xE7) | (std::uint8_t(scale) << 3);
|
registers.ReadRegister(_Registers::GYRO_CONFIG | _Registers::READ_MASK);
|
||||||
registers.WriteRegister(_Registers::GYRO_CONFIG & _Registers::WRITE_MASK, new_config);
|
const std::uint8_t new_config =
|
||||||
|
(current_config & 0xE7) | (std::uint8_t(scale) << 3);
|
||||||
|
registers.WriteRegister(_Registers::GYRO_CONFIG & _Registers::WRITE_MASK,
|
||||||
|
new_config);
|
||||||
|
|
||||||
_scale_gyro = scale;
|
_scale_gyro = scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetAccelerometerScale(const AccelerometerScale scale)
|
void SetAccelerometerScale(const AccelerometerScale scale)
|
||||||
{
|
{
|
||||||
const std::uint8_t current_config = registers.ReadRegister(_Registers::ACCEL_CONFIG | _Registers::READ_MASK);
|
const std::uint8_t current_config = registers.ReadRegister(
|
||||||
const std::uint8_t new_config = (current_config & 0xE7) | (std::uint8_t(scale) << 3);
|
_Registers::ACCEL_CONFIG | _Registers::READ_MASK);
|
||||||
registers.WriteRegister(_Registers::ACCEL_CONFIG & _Registers::WRITE_MASK, new_config);
|
const std::uint8_t new_config =
|
||||||
|
(current_config & 0xE7) | (std::uint8_t(scale) << 3);
|
||||||
|
registers.WriteRegister(_Registers::ACCEL_CONFIG & _Registers::WRITE_MASK,
|
||||||
|
new_config);
|
||||||
|
|
||||||
_scale_accel = scale;
|
_scale_accel = scale;
|
||||||
}
|
}
|
||||||
@ -143,7 +155,8 @@ public:
|
|||||||
void ReadGyro(float* output) override
|
void ReadGyro(float* output) override
|
||||||
{
|
{
|
||||||
uint8_t data[6] = {0};
|
uint8_t data[6] = {0};
|
||||||
registers.ReadRegisterMultibyte(_Registers::GYRO_XOUT_H | _Registers::READ_MASK, data, 6);
|
registers.ReadRegisterMultibyte(
|
||||||
|
_Registers::GYRO_XOUT_H | _Registers::READ_MASK, data, 6);
|
||||||
|
|
||||||
for (std::uint32_t i = 0; i < 3; i++)
|
for (std::uint32_t i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
@ -155,7 +168,8 @@ public:
|
|||||||
void ReadGyroRaw(std::int16_t* output) override
|
void ReadGyroRaw(std::int16_t* output) override
|
||||||
{
|
{
|
||||||
uint8_t data[6] = {0};
|
uint8_t data[6] = {0};
|
||||||
registers.ReadRegisterMultibyte(_Registers::GYRO_XOUT_H | _Registers::READ_MASK, data, 6);
|
registers.ReadRegisterMultibyte(
|
||||||
|
_Registers::GYRO_XOUT_H | _Registers::READ_MASK, data, 6);
|
||||||
|
|
||||||
for (std::uint32_t i = 0; i < 3; i++)
|
for (std::uint32_t i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
@ -166,7 +180,8 @@ public:
|
|||||||
void ReadAccelerometer(float* output) override
|
void ReadAccelerometer(float* output) override
|
||||||
{
|
{
|
||||||
uint8_t data[6] = {0};
|
uint8_t data[6] = {0};
|
||||||
registers.ReadRegisterMultibyte(_Registers::ACCEL_XOUT_H | _Registers::READ_MASK, data, 6);
|
registers.ReadRegisterMultibyte(
|
||||||
|
_Registers::ACCEL_XOUT_H | _Registers::READ_MASK, data, 6);
|
||||||
|
|
||||||
for (std::uint32_t i = 0; i < 3; i++)
|
for (std::uint32_t i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
@ -178,7 +193,8 @@ public:
|
|||||||
void ReadAccelerometerRaw(std::int16_t* output) override
|
void ReadAccelerometerRaw(std::int16_t* output) override
|
||||||
{
|
{
|
||||||
uint8_t data[6] = {0};
|
uint8_t data[6] = {0};
|
||||||
registers.ReadRegisterMultibyte(_Registers::ACCEL_XOUT_H | _Registers::READ_MASK, data, 6);
|
registers.ReadRegisterMultibyte(
|
||||||
|
_Registers::ACCEL_XOUT_H | _Registers::READ_MASK, data, 6);
|
||||||
|
|
||||||
for (std::uint32_t i = 0; i < 3; i++)
|
for (std::uint32_t i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
@ -214,18 +230,12 @@ private:
|
|||||||
std::array<std::int16_t, 3> _bias_accel;
|
std::array<std::int16_t, 3> _bias_accel;
|
||||||
|
|
||||||
static constexpr float _accel_fs_to_bit_constants[4] = {
|
static constexpr float _accel_fs_to_bit_constants[4] = {
|
||||||
(2.0f / 32768.0f),
|
(2.0f / 32768.0f), (4.0f / 32768.0f), (8.0f / 32768.0f),
|
||||||
(4.0f / 32768.0f),
|
(16.0f / 32768.0f)};
|
||||||
(8.0f / 32768.0f),
|
|
||||||
(16.0f / 32768.0f)
|
|
||||||
};
|
|
||||||
|
|
||||||
static constexpr float _gyro_fs_to_bit_constants[4] = {
|
static constexpr float _gyro_fs_to_bit_constants[4] = {
|
||||||
(250.0f / 32768.0f),
|
(250.0f / 32768.0f), (500.0f / 32768.0f), (1000.0f / 32768.0f),
|
||||||
(500.0f / 32768.0f),
|
(2000.0f / 32768.0f)};
|
||||||
(1000.0f / 32768.0f),
|
|
||||||
(2000.0f / 32768.0f)
|
|
||||||
};
|
|
||||||
|
|
||||||
struct _Registers
|
struct _Registers
|
||||||
{
|
{
|
||||||
@ -285,6 +295,6 @@ constexpr float ImuIcm<T, HAL>::_accel_fs_to_bit_constants[4];
|
|||||||
template<typename T, typename HAL>
|
template<typename T, typename HAL>
|
||||||
constexpr float ImuIcm<T, HAL>::_gyro_fs_to_bit_constants[4];
|
constexpr float ImuIcm<T, HAL>::_gyro_fs_to_bit_constants[4];
|
||||||
|
|
||||||
}
|
}// namespace Peripherals
|
||||||
|
|
||||||
#endif /* PERIPHERALS_IMU_ICM_HPP_ */
|
#endif /* PERIPHERALS_IMU_ICM_HPP_ */
|
||||||
|
|||||||
@ -34,10 +34,9 @@ public:
|
|||||||
using single_motor = TwoChannelMotorData<T>;
|
using single_motor = TwoChannelMotorData<T>;
|
||||||
using gpio = I;
|
using gpio = I;
|
||||||
|
|
||||||
DualDrvMotors(const single_motor& left, const single_motor& right, const gpio& sleep_pin)
|
DualDrvMotors(const single_motor& left, const single_motor& right,
|
||||||
: _left(left)
|
const gpio& sleep_pin)
|
||||||
, _right(right)
|
: _left(left), _right(right), _sleep_pin(sleep_pin)
|
||||||
, _sleep_pin(sleep_pin)
|
|
||||||
{
|
{
|
||||||
_left.forward.Enable();
|
_left.forward.Enable();
|
||||||
_left.backward.Enable();
|
_left.backward.Enable();
|
||||||
@ -53,8 +52,7 @@ public:
|
|||||||
{
|
{
|
||||||
_left.forward.SetCompare(left);
|
_left.forward.SetCompare(left);
|
||||||
_left.backward.SetCompare(0);
|
_left.backward.SetCompare(0);
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
_left.forward.SetCompare(0);
|
_left.forward.SetCompare(0);
|
||||||
_left.backward.SetCompare(-1 * left);
|
_left.backward.SetCompare(-1 * left);
|
||||||
@ -64,8 +62,7 @@ public:
|
|||||||
{
|
{
|
||||||
_right.forward.SetCompare(right);
|
_right.forward.SetCompare(right);
|
||||||
_right.backward.SetCompare(0);
|
_right.backward.SetCompare(0);
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
_right.forward.SetCompare(0);
|
_right.forward.SetCompare(0);
|
||||||
_right.backward.SetCompare(-1 * right);
|
_right.backward.SetCompare(-1 * right);
|
||||||
@ -96,7 +93,6 @@ private:
|
|||||||
gpio _sleep_pin;
|
gpio _sleep_pin;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}// namespace Peripherals
|
||||||
|
|
||||||
|
|
||||||
#endif /* PERIPHERALS_MOTORS_HPP_ */
|
#endif /* PERIPHERALS_MOTORS_HPP_ */
|
||||||
|
|||||||
@ -26,10 +26,8 @@ struct PwmChannel
|
|||||||
|
|
||||||
PwmChannel() = delete;
|
PwmChannel() = delete;
|
||||||
|
|
||||||
PwmChannel(TIM_HandleTypeDef* timer,
|
PwmChannel(TIM_HandleTypeDef* timer, const std::uint32_t channel,
|
||||||
const std::uint32_t channel,
|
const std::uint32_t timer_code, const Io& pin);
|
||||||
const std::uint32_t timer_code,
|
|
||||||
const Io& pin);
|
|
||||||
|
|
||||||
void PinToPwm();
|
void PinToPwm();
|
||||||
void PinToGpio();
|
void PinToGpio();
|
||||||
@ -40,7 +38,6 @@ struct PwmChannel
|
|||||||
void SetCompare(const std::uint32_t compare);
|
void SetCompare(const std::uint32_t compare);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}// namespace Peripherals
|
||||||
|
|
||||||
|
|
||||||
#endif /* PERIPHERALS_PWM_CHANNEL_HPP_ */
|
#endif /* PERIPHERALS_PWM_CHANNEL_HPP_ */
|
||||||
|
|||||||
@ -36,9 +36,7 @@ struct Rgb
|
|||||||
|
|
||||||
Rgb() = delete;
|
Rgb() = delete;
|
||||||
Rgb(const IOType& r, const IOType& g, const IOType& b)
|
Rgb(const IOType& r, const IOType& g, const IOType& b)
|
||||||
: red(r)
|
: red(r), green(g), blue(b)
|
||||||
, green(g)
|
|
||||||
, blue(b)
|
|
||||||
{
|
{
|
||||||
Set(RgbColor::OFF);
|
Set(RgbColor::OFF);
|
||||||
}
|
}
|
||||||
@ -93,7 +91,6 @@ struct Rgb
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}// namespace Peripherals
|
||||||
|
|
||||||
|
|
||||||
#endif /* PERIPHERALS_INC_PERIPHERALS_RGB_HPP_ */
|
#endif /* PERIPHERALS_INC_PERIPHERALS_RGB_HPP_ */
|
||||||
|
|||||||
@ -54,7 +54,6 @@ T ByteToTypeLE(const std::uint8_t a[N])
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}// namespace Peripherals
|
||||||
|
|
||||||
|
|
||||||
#endif /* PERIPHERALS_UTILITY_HPP_ */
|
#endif /* PERIPHERALS_UTILITY_HPP_ */
|
||||||
|
|||||||
@ -2,5 +2,6 @@
|
|||||||
// Created by erki on 13.03.21.
|
// Created by erki on 13.03.21.
|
||||||
//
|
//
|
||||||
|
|
||||||
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
|
#define CATCH_CONFIG_MAIN// This tells Catch to provide a main() - only do this \
|
||||||
|
// in one cpp file
|
||||||
#include <catch2/catch.hpp>
|
#include <catch2/catch.hpp>
|
||||||
|
|||||||
@ -34,7 +34,8 @@ TEST_CASE("Packet copy_data_in copies data in.", "[messaging],[packet]")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Packet copying data in cuts off excess bytes.", "[messaging],[packet]")
|
TEST_CASE("Packet copying data in cuts off excess bytes.",
|
||||||
|
"[messaging],[packet]")
|
||||||
{
|
{
|
||||||
Messaging::Packet<2> packet;
|
Messaging::Packet<2> packet;
|
||||||
|
|
||||||
@ -82,7 +83,8 @@ TEST_CASE("Packet gets serialized properly.", "[messaging],[packet]")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Packet serialization fails if buffer too small.", "[messaging],[packet]")
|
TEST_CASE("Packet serialization fails if buffer too small.",
|
||||||
|
"[messaging],[packet]")
|
||||||
{
|
{
|
||||||
Messaging::Packet<2> packet;
|
Messaging::Packet<2> packet;
|
||||||
|
|
||||||
@ -103,9 +105,7 @@ TEST_CASE("Packet serialization fails if buffer too small.", "[messaging],[packe
|
|||||||
|
|
||||||
TEST_CASE("Packet deserialization works as expected.", "[messaging],[packet]")
|
TEST_CASE("Packet deserialization works as expected.", "[messaging],[packet]")
|
||||||
{
|
{
|
||||||
std::uint8_t data[] = {
|
std::uint8_t data[] = {'A', 'A', 0, 0, 0, 0, 'C', 'D'};
|
||||||
'A', 'A', 0, 0, 0, 0, 'C', 'D'
|
|
||||||
};
|
|
||||||
|
|
||||||
const std::uint32_t data_length = 2;
|
const std::uint32_t data_length = 2;
|
||||||
std::memcpy(data + 2, &data_length, sizeof(data_length));
|
std::memcpy(data + 2, &data_length, sizeof(data_length));
|
||||||
@ -118,11 +118,10 @@ TEST_CASE("Packet deserialization works as expected.", "[messaging],[packet]")
|
|||||||
REQUIRE(packet.data[1] == 'D');
|
REQUIRE(packet.data[1] == 'D');
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Packet deserialization fails with invalid conditions.", "[messaging],[packet]")
|
TEST_CASE("Packet deserialization fails with invalid conditions.",
|
||||||
|
"[messaging],[packet]")
|
||||||
{
|
{
|
||||||
std::uint8_t data[] = {
|
std::uint8_t data[] = {'A', 'A', 0, 0, 0, 0, 'C', 'D'};
|
||||||
'A', 'A', 0, 0, 0, 0, 'C', 'D'
|
|
||||||
};
|
|
||||||
|
|
||||||
const std::uint32_t data_length = 2;
|
const std::uint32_t data_length = 2;
|
||||||
std::memcpy(data + 2, &data_length, sizeof(data_length));
|
std::memcpy(data + 2, &data_length, sizeof(data_length));
|
||||||
|
|||||||
@ -4,8 +4,8 @@
|
|||||||
|
|
||||||
#include <catch2/catch.hpp>
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
#include <messaging_parser.hpp>
|
|
||||||
#include <messaging_packet.hpp>
|
#include <messaging_packet.hpp>
|
||||||
|
#include <messaging_parser.hpp>
|
||||||
|
|
||||||
using Packet = Messaging::Packet<2>;
|
using Packet = Messaging::Packet<2>;
|
||||||
|
|
||||||
@ -14,11 +14,10 @@ namespace
|
|||||||
|
|
||||||
std::array<std::uint8_t, 2> test_data = {'C', 'D'};
|
std::array<std::uint8_t, 2> test_data = {'C', 'D'};
|
||||||
|
|
||||||
std::array<std::uint8_t, 8> getRawData(const std::array<std::uint8_t, 2>& data)
|
std::array<std::uint8_t, 8>
|
||||||
|
getRawData(const std::array<std::uint8_t, 2>& data)
|
||||||
{
|
{
|
||||||
std::array<std::uint8_t, 8> raw = {
|
std::array<std::uint8_t, 8> raw = {'A', 'A', 0, 0, 0, 0, data[0], data[1]};
|
||||||
'A', 'A', 0, 0, 0, 0, data[0], data[1]
|
|
||||||
};
|
|
||||||
|
|
||||||
const std::uint32_t len = 2;
|
const std::uint32_t len = 2;
|
||||||
std::memcpy(raw.data() + 2, &len, 4);
|
std::memcpy(raw.data() + 2, &len, 4);
|
||||||
@ -26,12 +25,9 @@ std::array<std::uint8_t, 8> getRawData(const std::array<std::uint8_t, 2>& data)
|
|||||||
return raw;
|
return raw;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::array<std::uint8_t, 8> getRawData()
|
std::array<std::uint8_t, 8> getRawData() { return getRawData({'C', 'D'}); }
|
||||||
{
|
|
||||||
return getRawData({'C', 'D'});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}// namespace
|
||||||
|
|
||||||
TEST_CASE("Parser parses raw message successfully.", "[messaging],[parser]")
|
TEST_CASE("Parser parses raw message successfully.", "[messaging],[parser]")
|
||||||
{
|
{
|
||||||
@ -82,12 +78,12 @@ TEST_CASE("Parser ignores extra bytes when done.", "[messaging],[parser]")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Parser ignores junk data until header is spotted.", "[messaging],[parser]")
|
TEST_CASE("Parser ignores junk data until header is spotted.",
|
||||||
|
"[messaging],[parser]")
|
||||||
{
|
{
|
||||||
Messaging::Parser<Packet, Packet::totalLength()> parser;
|
Messaging::Parser<Packet, Packet::totalLength()> parser;
|
||||||
const std::array<std::uint8_t, 8> junk_data = {
|
const std::array<std::uint8_t, 8> junk_data = {'E', 'F', 'A', 'H',
|
||||||
'E', 'F', 'A', 'H', 'I', 'J', 'K', 'L'
|
'I', 'J', 'K', 'L'};
|
||||||
};
|
|
||||||
|
|
||||||
for (const std::uint8_t& byte : junk_data)
|
for (const std::uint8_t& byte : junk_data)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
template<size_t N>
|
template<size_t N>
|
||||||
using Ringbuffer = Utility::Ringbuffer<int, N>;
|
using Ringbuffer = Utility::Ringbuffer<int, N>;
|
||||||
|
|
||||||
TEST_CASE("Ringbuffer iterator", "[peripherals],[ringbuffer]")
|
TEST_CASE("Ringbuffer iterator", "[utility],[ringbuffer]")
|
||||||
{
|
{
|
||||||
using iterator = Ringbuffer<10>::iterator;
|
using iterator = Ringbuffer<10>::iterator;
|
||||||
const auto begin = iterator::pointer(10);
|
const auto begin = iterator::pointer(10);
|
||||||
@ -43,7 +43,7 @@ TEST_CASE("Ringbuffer iterator", "[peripherals],[ringbuffer]")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Ringbuffer iterator at the end", "[peripherals],[ringbuffer]")
|
TEST_CASE("Ringbuffer iterator at the end", "[utility],[ringbuffer]")
|
||||||
{
|
{
|
||||||
using iterator = Ringbuffer<10>::iterator;
|
using iterator = Ringbuffer<10>::iterator;
|
||||||
const auto begin = iterator::pointer(10);
|
const auto begin = iterator::pointer(10);
|
||||||
@ -71,7 +71,7 @@ TEST_CASE("Ringbuffer iterator at the end", "[peripherals],[ringbuffer]")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Ringbuffer iterator at the beginning", "[peripherals],[ringbuffer]")
|
TEST_CASE("Ringbuffer iterator at the beginning", "[utility],[ringbuffer]")
|
||||||
{
|
{
|
||||||
using iterator = Ringbuffer<10>::iterator;
|
using iterator = Ringbuffer<10>::iterator;
|
||||||
const auto begin = iterator::pointer(10);
|
const auto begin = iterator::pointer(10);
|
||||||
@ -87,16 +87,17 @@ TEST_CASE("Ringbuffer iterator at the beginning", "[peripherals],[ringbuffer]")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Constructed buffer is empty.", "[peripherals],[ringbuffer]")
|
TEST_CASE("Ringbuffer constructed buffer is empty.", "[utility],[ringbuffer]")
|
||||||
{
|
{
|
||||||
Ringbuffer<10> buffer;
|
Ringbuffer<10> buffer;
|
||||||
|
|
||||||
REQUIRE(buffer.begin() == buffer.end());
|
REQUIRE(buffer.begin() == buffer.end());
|
||||||
REQUIRE(buffer.size() == 0);
|
REQUIRE(buffer.size() == 0);
|
||||||
REQUIRE(buffer.empty());
|
REQUIRE(buffer.empty());
|
||||||
|
REQUIRE(&buffer.front() == &buffer.back());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Buffer reports size properly.", "[peripherals],[ringbuffer]")
|
TEST_CASE("Ringbuffer reports size properly.", "[utility],[ringbuffer]")
|
||||||
{
|
{
|
||||||
Ringbuffer<3> buffer;
|
Ringbuffer<3> buffer;
|
||||||
|
|
||||||
@ -140,7 +141,7 @@ TEST_CASE("Buffer reports size properly.", "[peripherals],[ringbuffer]")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Adding single element.", "[peripherals],[ringbuffer]")
|
TEST_CASE("Ringbuffer adding single element.", "[utility],[ringbuffer]")
|
||||||
{
|
{
|
||||||
Ringbuffer<10> buffer;
|
Ringbuffer<10> buffer;
|
||||||
const auto old_end = buffer.end();
|
const auto old_end = buffer.end();
|
||||||
@ -154,15 +155,9 @@ TEST_CASE("Adding single element.", "[peripherals],[ringbuffer]")
|
|||||||
REQUIRE(!buffer.empty());
|
REQUIRE(!buffer.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Updates end() appropriately.")
|
SECTION("Updates end() appropriately.") { REQUIRE(old_end != buffer.end()); }
|
||||||
{
|
|
||||||
REQUIRE(old_end != buffer.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("begin() remains the same.")
|
SECTION("begin() remains the same.") { REQUIRE(old_begin == buffer.begin()); }
|
||||||
{
|
|
||||||
REQUIRE(old_begin == buffer.begin());
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("Makes begin() refer to the inserted member.")
|
SECTION("Makes begin() refer to the inserted member.")
|
||||||
{
|
{
|
||||||
@ -191,7 +186,7 @@ TEST_CASE("Adding single element.", "[peripherals],[ringbuffer]")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Adding multiple elements.", "[peripherals],[ringbuffer]")
|
TEST_CASE("Ringbuffer adding multiple elements.", "[utility],[ringbuffer]")
|
||||||
{
|
{
|
||||||
Ringbuffer<10> buffer;
|
Ringbuffer<10> buffer;
|
||||||
const auto old_begin = buffer.begin();
|
const auto old_begin = buffer.begin();
|
||||||
@ -223,7 +218,8 @@ TEST_CASE("Adding multiple elements.", "[peripherals],[ringbuffer]")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Removing elements from the ringbuffer.", "[peripherals],[ringbuffer]")
|
TEST_CASE("Ringbuffer removing elements from the ringbuffer.",
|
||||||
|
"[utility],[ringbuffer]")
|
||||||
{
|
{
|
||||||
Ringbuffer<10> buffer;
|
Ringbuffer<10> buffer;
|
||||||
const auto old_begin = buffer.begin();
|
const auto old_begin = buffer.begin();
|
||||||
@ -244,34 +240,25 @@ TEST_CASE("Removing elements from the ringbuffer.", "[peripherals],[ringbuffer]"
|
|||||||
REQUIRE(buffer.begin() - old_begin == 1);
|
REQUIRE(buffer.begin() - old_begin == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Updates size() appropriately.")
|
SECTION("Updates size() appropriately.") { REQUIRE(buffer.size() == 2); }
|
||||||
{
|
|
||||||
REQUIRE(buffer.size() == 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("Erasing remaining elements")
|
SECTION("Erasing remaining elements")
|
||||||
{
|
{
|
||||||
buffer.pop_front();
|
buffer.pop_front();
|
||||||
buffer.pop_front();
|
buffer.pop_front();
|
||||||
|
|
||||||
SECTION("Updates empty() appropriately.")
|
SECTION("Updates empty() appropriately.") { REQUIRE(buffer.empty()); }
|
||||||
{
|
|
||||||
REQUIRE(buffer.empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("Updates begin() and end() appropriately.")
|
SECTION("Updates begin() and end() appropriately.")
|
||||||
{
|
{
|
||||||
REQUIRE(buffer.begin() == buffer.end());
|
REQUIRE(buffer.begin() == buffer.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Updates size() appropriately.")
|
SECTION("Updates size() appropriately.") { REQUIRE(buffer.size() == 0); }
|
||||||
{
|
|
||||||
REQUIRE(buffer.size() == 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Clearing a ringbuffer works.", "[peripherals],[ringbuffer]")
|
TEST_CASE("Ringbuffer clearing a ringbuffer works.", "[utility],[ringbuffer]")
|
||||||
{
|
{
|
||||||
Ringbuffer<10> buffer;
|
Ringbuffer<10> buffer;
|
||||||
|
|
||||||
@ -290,10 +277,12 @@ TEST_CASE("Clearing a ringbuffer works.", "[peripherals],[ringbuffer]")
|
|||||||
SECTION("Sets begin() and end() pointers appropriately.")
|
SECTION("Sets begin() and end() pointers appropriately.")
|
||||||
{
|
{
|
||||||
REQUIRE(buffer.begin() == buffer.end());
|
REQUIRE(buffer.begin() == buffer.end());
|
||||||
|
REQUIRE(&buffer.front() == &buffer.back());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Manually incrementing tail works.", "[peripherals],[ringbuffer]")
|
TEST_CASE("Ringbuffer manually incrementing tail works.",
|
||||||
|
"[utility],[ringbuffer]")
|
||||||
{
|
{
|
||||||
Ringbuffer<10> buffer;
|
Ringbuffer<10> buffer;
|
||||||
|
|
||||||
@ -312,7 +301,57 @@ TEST_CASE("Manually incrementing tail works.", "[peripherals],[ringbuffer]")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Manually incrementing tail when full deletes data.", "[peripherals],[ringbuffer]")
|
TEST_CASE("Ringbuffer manually incrementing tail from empty state works.",
|
||||||
|
"[utility],[ringbuffer]")
|
||||||
|
{
|
||||||
|
Ringbuffer<10> buffer;
|
||||||
|
|
||||||
|
const auto old_end = buffer.end();
|
||||||
|
const auto old_begin = buffer.begin();
|
||||||
|
auto* old_tail_item_ptr = &(*old_end);
|
||||||
|
|
||||||
|
buffer.increment_tail();
|
||||||
|
|
||||||
|
SECTION("Head remains in place.")
|
||||||
|
{
|
||||||
|
const auto new_end = buffer.end();
|
||||||
|
const auto new_begin = buffer.begin();
|
||||||
|
REQUIRE(old_begin == new_begin);
|
||||||
|
REQUIRE(new_end != new_begin);
|
||||||
|
REQUIRE(new_end - new_begin == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Tail has been incremented.")
|
||||||
|
{
|
||||||
|
const auto new_end = buffer.end();
|
||||||
|
REQUIRE(new_end != old_end);
|
||||||
|
REQUIRE(new_end - old_end == 1);
|
||||||
|
REQUIRE(buffer.size() == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Item pointer for end is increased appropriately.")
|
||||||
|
{
|
||||||
|
auto* new_tail_item_ptr = &(*buffer.end());
|
||||||
|
REQUIRE(new_tail_item_ptr != old_tail_item_ptr);
|
||||||
|
REQUIRE(new_tail_item_ptr - old_tail_item_ptr == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Second incrementation works as expected.")
|
||||||
|
{
|
||||||
|
buffer.increment_tail();
|
||||||
|
const auto new_end = buffer.end();
|
||||||
|
const auto new_begin = buffer.begin();
|
||||||
|
auto* new_tail_item_ptr = &(*buffer.end());
|
||||||
|
|
||||||
|
REQUIRE(new_begin == old_begin);
|
||||||
|
REQUIRE(new_end - old_end == 2);
|
||||||
|
REQUIRE(new_tail_item_ptr - old_tail_item_ptr == 2);
|
||||||
|
REQUIRE(buffer.size() == 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Ringbuffer manually incrementing tail when full deletes data.",
|
||||||
|
"[utility],[ringbuffer]")
|
||||||
{
|
{
|
||||||
Ringbuffer<2> buffer;
|
Ringbuffer<2> buffer;
|
||||||
|
|
||||||
|
|||||||
@ -9,8 +9,8 @@
|
|||||||
#define UTILITY_INC_UTILITY_ASYNCLOGGER_HPP_
|
#define UTILITY_INC_UTILITY_ASYNCLOGGER_HPP_
|
||||||
|
|
||||||
#include "utility_atomicscopeguard.hpp"
|
#include "utility_atomicscopeguard.hpp"
|
||||||
#include "utility_ringbuffer.hpp"
|
|
||||||
#include "utility_ilogger.hpp"
|
#include "utility_ilogger.hpp"
|
||||||
|
#include "utility_ringbuffer.hpp"
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
@ -19,7 +19,8 @@
|
|||||||
namespace Utility
|
namespace Utility
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename T, typename H, std::size_t BufferCount, std::size_t BufferSize>
|
template<typename T, typename H, std::size_t BufferCount,
|
||||||
|
std::size_t BufferSize>
|
||||||
class AsyncLogger : public ILogger
|
class AsyncLogger : public ILogger
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -27,9 +28,7 @@ public:
|
|||||||
using hal = H;
|
using hal = H;
|
||||||
|
|
||||||
AsyncLogger() = delete;
|
AsyncLogger() = delete;
|
||||||
explicit AsyncLogger(const serial_interface& serial)
|
explicit AsyncLogger(const serial_interface& serial) : _serial(serial) {}
|
||||||
: _serial(serial)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
AsyncLogger(const AsyncLogger&) = delete;
|
AsyncLogger(const AsyncLogger&) = delete;
|
||||||
AsyncLogger(AsyncLogger&&) = delete;
|
AsyncLogger(AsyncLogger&&) = delete;
|
||||||
@ -48,10 +47,9 @@ public:
|
|||||||
std::va_list args;
|
std::va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
|
|
||||||
auto tail = _buffer_queue.end();
|
_Data& tail = (*_buffer_queue.end());
|
||||||
std::array<char, 255>& buffer = tail->buffer;
|
tail.length =
|
||||||
|
vsnprintf(tail.buffer.data(), tail.buffer.size(), format, args);
|
||||||
tail->length = vsnprintf(buffer.data(), buffer.size(), format, args);
|
|
||||||
|
|
||||||
{
|
{
|
||||||
AtomicScopeGuard<hal> s;
|
AtomicScopeGuard<hal> s;
|
||||||
@ -60,6 +58,8 @@ public:
|
|||||||
if (!_in_flight)
|
if (!_in_flight)
|
||||||
_sendNextLog();
|
_sendNextLog();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
void txCompleteCallback()
|
void txCompleteCallback()
|
||||||
@ -86,12 +86,12 @@ private:
|
|||||||
_in_flight = true;
|
_in_flight = true;
|
||||||
|
|
||||||
_Data& head = _buffer_queue.front();
|
_Data& head = _buffer_queue.front();
|
||||||
_serial.Transmit(reinterpret_cast<uint8_t*>(head.buffer.data()), head.length);
|
_serial.Transmit(reinterpret_cast<uint8_t*>(head.buffer.data()),
|
||||||
|
head.length);
|
||||||
_buffer_queue.pop_front();
|
_buffer_queue.pop_front();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}// namespace Utility
|
||||||
|
|
||||||
|
|
||||||
#endif /* UTILITY_INC_UTILITY_ASYNCLOGGER_HPP_ */
|
#endif /* UTILITY_INC_UTILITY_ASYNCLOGGER_HPP_ */
|
||||||
|
|||||||
@ -36,7 +36,6 @@ struct AtomicScopeGuard
|
|||||||
hal::enableInterrupts();
|
hal::enableInterrupts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::int32_t _reentrancy_counter;
|
static std::int32_t _reentrancy_counter;
|
||||||
};
|
};
|
||||||
@ -44,6 +43,6 @@ private:
|
|||||||
template<typename H>
|
template<typename H>
|
||||||
std::int32_t AtomicScopeGuard<H>::_reentrancy_counter = 0;
|
std::int32_t AtomicScopeGuard<H>::_reentrancy_counter = 0;
|
||||||
|
|
||||||
}
|
}// namespace Utility
|
||||||
|
|
||||||
#endif /* UTILITY_INC_UTILITY_ATOMICSCOPEGUARD_HPP_ */
|
#endif /* UTILITY_INC_UTILITY_ATOMICSCOPEGUARD_HPP_ */
|
||||||
|
|||||||
@ -26,14 +26,8 @@ public:
|
|||||||
LOG_FATAL
|
LOG_FATAL
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr static const char* level_strs[] = {
|
constexpr static const char* level_strs[] = {"DEBUG", "INFO", "NOTICE",
|
||||||
"DEBUG",
|
"WARNING", "ERROR", "FATAL"};
|
||||||
"INFO",
|
|
||||||
"NOTICE",
|
|
||||||
"WARNING",
|
|
||||||
"ERROR",
|
|
||||||
"FATAL"
|
|
||||||
};
|
|
||||||
|
|
||||||
ILogger() = default;
|
ILogger() = default;
|
||||||
ILogger(const ILogger&) = delete;
|
ILogger(const ILogger&) = delete;
|
||||||
@ -44,6 +38,6 @@ public:
|
|||||||
virtual void log(const char* format, ...) = 0;
|
virtual void log(const char* format, ...) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}// namespace Utility
|
||||||
|
|
||||||
#endif// SKULLC_UTILITY_ILOGGER_HPP_
|
#endif// SKULLC_UTILITY_ILOGGER_HPP_
|
||||||
|
|||||||
@ -7,13 +7,22 @@
|
|||||||
|
|
||||||
#include "utility_ilogger.hpp"
|
#include "utility_ilogger.hpp"
|
||||||
|
|
||||||
#define SKULLC_LOG(sev, msg, ...) Utility::skullc_logger->log("%s: " msg "\n\r", Utility::ILogger::level_strs[std::uint8_t(sev)], ## __VA_ARGS__)
|
#define SKULLC_LOG(sev, msg, ...) \
|
||||||
#define SKULLC_LOG_DEBUG(msg, ...) SKULLC_LOG(Utility::ILogger::LogLevel::LOG_DEBUG, msg, ## __VA_ARGS__)
|
Utility::skullc_logger->log("%s: " msg "\n\r", \
|
||||||
#define SKULLC_LOG_INFO(msg, ...) SKULLC_LOG(Utility::ILogger::LogLevel::LOG_INFO, msg, ## __VA_ARGS__)
|
Utility::ILogger::level_strs[std::uint8_t(sev)], \
|
||||||
#define SKULLC_LOG_NOTICE(msg, ...) SKULLC_LOG(Utility::ILogger::LogLevel::LOG_NOTICE, msg, ## __VA_ARGS__)
|
##__VA_ARGS__)
|
||||||
#define SKULLC_LOG_WARNING(msg, ...) SKULLC_LOG(Utility::ILogger::LogLevel::LOG_WARNING, msg, ## __VA_ARGS__)
|
#define SKULLC_LOG_DEBUG(msg, ...) \
|
||||||
#define SKULLC_LOG_ERROR(msg, ...) SKULLC_LOG(Utility::ILogger::LogLevel::LOG_ERROR, msg, ## __VA_ARGS__)
|
SKULLC_LOG(Utility::ILogger::LogLevel::LOG_DEBUG, msg, ##__VA_ARGS__)
|
||||||
#define SKULLC_LOG_FATAL(msg, ...) SKULLC_LOG(Utility::ILogger::LogLevel::LOG_FATAL, msg, ## __VA_ARGS__)
|
#define SKULLC_LOG_INFO(msg, ...) \
|
||||||
|
SKULLC_LOG(Utility::ILogger::LogLevel::LOG_INFO, msg, ##__VA_ARGS__)
|
||||||
|
#define SKULLC_LOG_NOTICE(msg, ...) \
|
||||||
|
SKULLC_LOG(Utility::ILogger::LogLevel::LOG_NOTICE, msg, ##__VA_ARGS__)
|
||||||
|
#define SKULLC_LOG_WARNING(msg, ...) \
|
||||||
|
SKULLC_LOG(Utility::ILogger::LogLevel::LOG_WARNING, msg, ##__VA_ARGS__)
|
||||||
|
#define SKULLC_LOG_ERROR(msg, ...) \
|
||||||
|
SKULLC_LOG(Utility::ILogger::LogLevel::LOG_ERROR, msg, ##__VA_ARGS__)
|
||||||
|
#define SKULLC_LOG_FATAL(msg, ...) \
|
||||||
|
SKULLC_LOG(Utility::ILogger::LogLevel::LOG_FATAL, msg, ##__VA_ARGS__)
|
||||||
|
|
||||||
namespace Utility
|
namespace Utility
|
||||||
{
|
{
|
||||||
@ -23,6 +32,6 @@ extern ILogger* skullc_logger;
|
|||||||
void setLogger(ILogger* log);
|
void setLogger(ILogger* log);
|
||||||
void setLogger(ILogger& log);
|
void setLogger(ILogger& log);
|
||||||
|
|
||||||
}
|
}// namespace Utility
|
||||||
|
|
||||||
#endif// UTILITY_LOGGING_HPP_
|
#endif// UTILITY_LOGGING_HPP_
|
||||||
|
|||||||
@ -9,8 +9,8 @@
|
|||||||
#define UTILITY_RINGBUFFER_HPP_
|
#define UTILITY_RINGBUFFER_HPP_
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <iterator>
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
namespace Utility
|
namespace Utility
|
||||||
{
|
{
|
||||||
@ -36,24 +36,15 @@ public:
|
|||||||
using reference = value_type&;
|
using reference = value_type&;
|
||||||
|
|
||||||
iterator(const pointer ptr, const pointer begin, const pointer end)
|
iterator(const pointer ptr, const pointer begin, const pointer end)
|
||||||
: _ptr(ptr)
|
: _ptr(ptr), _arr_begin(begin), _arr_end(end) {}
|
||||||
, _arr_begin(begin)
|
|
||||||
, _arr_end(end)
|
|
||||||
{ }
|
|
||||||
iterator(const iterator&) = default;
|
iterator(const iterator&) = default;
|
||||||
iterator(iterator&&) noexcept = default;
|
iterator(iterator&&) noexcept = default;
|
||||||
|
|
||||||
iterator& operator=(const iterator&) = default;
|
iterator& operator=(const iterator&) = default;
|
||||||
|
|
||||||
reference operator*() const
|
reference operator*() const { return *_ptr; }
|
||||||
{
|
|
||||||
return *_ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
pointer operator->()
|
pointer operator->() { return _ptr; }
|
||||||
{
|
|
||||||
return _ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
iterator& operator++()
|
iterator& operator++()
|
||||||
{
|
{
|
||||||
@ -77,11 +68,11 @@ public:
|
|||||||
if (naive < _arr_end)
|
if (naive < _arr_end)
|
||||||
{
|
{
|
||||||
return iterator(naive, _arr_begin, _arr_end);
|
return iterator(naive, _arr_begin, _arr_end);
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
const pointer remainder = pointer(naive - _arr_end);
|
const pointer remainder = pointer(naive - _arr_end);
|
||||||
return iterator(_arr_begin + difference_type(remainder), _arr_begin, _arr_end);
|
return iterator(_arr_begin + difference_type(remainder), _arr_begin,
|
||||||
|
_arr_end);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,11 +82,11 @@ public:
|
|||||||
if (naive >= _arr_begin)
|
if (naive >= _arr_begin)
|
||||||
{
|
{
|
||||||
return iterator(naive, _arr_begin, _arr_end);
|
return iterator(naive, _arr_begin, _arr_end);
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
const pointer remainder = pointer(_arr_begin - naive);
|
const pointer remainder = pointer(_arr_begin - naive);
|
||||||
return iterator(_arr_end - difference_type(remainder), _arr_begin, _arr_end);
|
return iterator(_arr_end - difference_type(remainder), _arr_begin,
|
||||||
|
_arr_end);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,28 +116,16 @@ public:
|
|||||||
pointer _arr_end;
|
pointer _arr_end;
|
||||||
};
|
};
|
||||||
|
|
||||||
Ringbuffer()
|
Ringbuffer() : _head(&_data[0], &_data[0], &_data[N]), _tail(_head) {}
|
||||||
: _head(&_data[0], &_data[0], &_data[N])
|
|
||||||
, _tail(_head)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
Ringbuffer(const Ringbuffer&) = delete;
|
Ringbuffer(const Ringbuffer&) = delete;
|
||||||
Ringbuffer(Ringbuffer&&) noexcept = default;
|
Ringbuffer(Ringbuffer&&) noexcept = default;
|
||||||
|
|
||||||
iterator begin()
|
iterator begin() { return _head; }
|
||||||
{
|
|
||||||
return _head;
|
|
||||||
}
|
|
||||||
|
|
||||||
iterator end()
|
iterator end() { return _tail; }
|
||||||
{
|
|
||||||
return _tail;
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear()
|
void clear() { _head = _tail; }
|
||||||
{
|
|
||||||
_head = _tail;
|
|
||||||
}
|
|
||||||
|
|
||||||
void push_back(const T& value)
|
void push_back(const T& value)
|
||||||
{
|
{
|
||||||
@ -172,7 +151,7 @@ public:
|
|||||||
|
|
||||||
void increment_tail()
|
void increment_tail()
|
||||||
{
|
{
|
||||||
if (_tail == _head)
|
if (_is_full)
|
||||||
++_head;
|
++_head;
|
||||||
|
|
||||||
++_tail;
|
++_tail;
|
||||||
@ -187,23 +166,23 @@ public:
|
|||||||
_is_full = false;
|
_is_full = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
reference front()
|
reference front() { return *_head; }
|
||||||
{
|
|
||||||
return *_head;
|
|
||||||
}
|
|
||||||
|
|
||||||
const_reference front() const
|
const_reference front() const { return *_head; }
|
||||||
{
|
|
||||||
return *_head;
|
|
||||||
}
|
|
||||||
|
|
||||||
reference back()
|
reference back()
|
||||||
{
|
{
|
||||||
|
if (empty())
|
||||||
|
return *_tail;
|
||||||
|
else
|
||||||
return *(_tail - 1);
|
return *(_tail - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const_reference back() const
|
const_reference back() const
|
||||||
{
|
{
|
||||||
|
if (empty())
|
||||||
|
return *_tail;
|
||||||
|
else
|
||||||
return *(_tail - 1);
|
return *(_tail - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,27 +196,21 @@ public:
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const typename Ringbuffer<T, N>::iterator::difference_type distance = _tail - _head;
|
const typename Ringbuffer<T, N>::iterator::difference_type distance =
|
||||||
|
_tail - _head;
|
||||||
|
|
||||||
if (distance > 0)
|
if (distance > 0)
|
||||||
{
|
{
|
||||||
return distance;
|
return distance;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
return _head - _tail + 1;
|
return _head - _tail + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr size_type max_size() const
|
constexpr size_type max_size() const { return N; }
|
||||||
{
|
|
||||||
return N;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool empty() const
|
bool empty() const { return size() == 0; }
|
||||||
{
|
|
||||||
return size() == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::array<T, N> _data;
|
std::array<T, N> _data;
|
||||||
@ -247,7 +220,6 @@ private:
|
|||||||
iterator _tail;
|
iterator _tail;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}// namespace Utility
|
||||||
|
|
||||||
|
|
||||||
#endif /* UTILITY_RINGBUFFER_HPP_ */
|
#endif /* UTILITY_RINGBUFFER_HPP_ */
|
||||||
|
|||||||
@ -24,16 +24,15 @@ public:
|
|||||||
using serial_interface = T;
|
using serial_interface = T;
|
||||||
|
|
||||||
SerialLogger() = delete;
|
SerialLogger() = delete;
|
||||||
explicit SerialLogger(const serial_interface& serial)
|
explicit SerialLogger(const serial_interface& serial) : _serial(serial) {}
|
||||||
: _serial(serial)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
void log(const char* format, ...) override
|
void log(const char* format, ...) override
|
||||||
{
|
{
|
||||||
std::va_list args;
|
std::va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
|
|
||||||
const std::int32_t len = vsnprintf(_buffer.data(), _buffer.size(), format, args);
|
const std::int32_t len =
|
||||||
|
vsnprintf(_buffer.data(), _buffer.size(), format, args);
|
||||||
|
|
||||||
if (len > 0)
|
if (len > 0)
|
||||||
_serial.Transmit(reinterpret_cast<std::uint8_t*>(_buffer.data()), len);
|
_serial.Transmit(reinterpret_cast<std::uint8_t*>(_buffer.data()), len);
|
||||||
@ -46,7 +45,6 @@ private:
|
|||||||
std::array<char, N> _buffer;
|
std::array<char, N> _buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}// namespace Utility
|
||||||
|
|
||||||
|
|
||||||
#endif /* UTILITY_INC_UTILITY_SERIALLOGGER_HPP_ */
|
#endif /* UTILITY_INC_UTILITY_SERIALLOGGER_HPP_ */
|
||||||
|
|||||||
@ -12,14 +12,8 @@ namespace Utility
|
|||||||
|
|
||||||
ILogger* skullc_logger = nullptr;
|
ILogger* skullc_logger = nullptr;
|
||||||
|
|
||||||
void setLogger(ILogger* log)
|
void setLogger(ILogger* log) { skullc_logger = log; }
|
||||||
{
|
|
||||||
skullc_logger = log;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setLogger(ILogger& log)
|
void setLogger(ILogger& log) { skullc_logger = &log; }
|
||||||
{
|
|
||||||
skullc_logger = &log;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}// namespace Utility
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user