Compare commits
No commits in common. "ea99a8a6ba165b9b5dbdd8ce9854bd5cdad19bc1" and "8fbb0efd5d154e8d5ccbc7dd19fb22ab3bc03ce0" have entirely different histories.
ea99a8a6ba
...
8fbb0efd5d
@ -17,7 +17,7 @@ AlwaysBreakAfterReturnType: None
|
|||||||
AlwaysBreakTemplateDeclarations: Yes
|
AlwaysBreakTemplateDeclarations: Yes
|
||||||
BreakBeforeBraces: Custom
|
BreakBeforeBraces: Custom
|
||||||
BraceWrapping:
|
BraceWrapping:
|
||||||
AfterCaseLabel: true
|
AfterCaseLabel: false
|
||||||
AfterClass: true
|
AfterClass: true
|
||||||
AfterControlStatement: Always
|
AfterControlStatement: Always
|
||||||
AfterEnum: true
|
AfterEnum: true
|
||||||
@ -25,8 +25,8 @@ BraceWrapping:
|
|||||||
AfterNamespace: true
|
AfterNamespace: true
|
||||||
AfterUnion: true
|
AfterUnion: true
|
||||||
AfterStruct: true
|
AfterStruct: true
|
||||||
BeforeCatch: true
|
BeforeCatch: false
|
||||||
BeforeElse: true
|
BeforeElse: false
|
||||||
IndentBraces: false
|
IndentBraces: false
|
||||||
SplitEmptyFunction: false
|
SplitEmptyFunction: false
|
||||||
SplitEmptyRecord: true
|
SplitEmptyRecord: true
|
||||||
|
|||||||
@ -10,6 +10,8 @@
|
|||||||
|
|
||||||
#include <main.h>
|
#include <main.h>
|
||||||
|
|
||||||
|
#include "peripherals_utility.hpp"
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
#define USE_DELAY_US
|
#define USE_DELAY_US
|
||||||
@ -93,16 +95,10 @@ struct Gpio
|
|||||||
bool read() const { return inverted ? !bool(HAL_GPIO_ReadPin(port, pin)) : bool(HAL_GPIO_ReadPin(port, pin)); }
|
bool read() const { return inverted ? !bool(HAL_GPIO_ReadPin(port, pin)) : bool(HAL_GPIO_ReadPin(port, pin)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
#define CREATE_GPIO(name) \
|
#define CREATE_GPIO(name) \
|
||||||
Peripherals::Hal::St::Gpio \
|
Peripherals::Hal::St::Gpio { name##_GPIO_Port, name##_Pin, false }
|
||||||
{ \
|
#define CREATE_INV_GPIO(name) \
|
||||||
name##_GPIO_Port, name##_Pin, false \
|
Peripherals::Hal::St::Gpio { name##_GPIO_Port, name##_Pin, true }
|
||||||
}
|
|
||||||
#define CREATE_INV_GPIO(name) \
|
|
||||||
Peripherals::Hal::St::Gpio \
|
|
||||||
{ \
|
|
||||||
name##_GPIO_Port, name##_Pin, true \
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif// HAL_GPIO_MODULE_ENABLED
|
#endif// HAL_GPIO_MODULE_ENABLED
|
||||||
|
|
||||||
@ -274,17 +270,17 @@ namespace _Details
|
|||||||
* are const-correct. The others remain unconst. So these wrappers will exist until we're no longer partially
|
* are const-correct. The others remain unconst. So these wrappers will exist until we're no longer partially
|
||||||
* const-correct.
|
* const-correct.
|
||||||
*/
|
*/
|
||||||
inline HAL_StatusTypeDef uartTransmit(UART_HandleTypeDef* huart, std::uint8_t* data, const std::uint16_t size, const std::uint32_t timeout)
|
inline HAL_StatusTypeDef uartTransmit(UART_HandleTypeDef *huart, std::uint8_t *data, const std::uint16_t size, const std::uint32_t timeout)
|
||||||
{
|
{
|
||||||
return HAL_UART_Transmit(huart, data, size, timeout);
|
return HAL_UART_Transmit(huart, data, size, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline HAL_StatusTypeDef uartTransmitDma(UART_HandleTypeDef* huart, std::uint8_t* data, const std::uint16_t size)
|
inline HAL_StatusTypeDef uartTransmitDma(UART_HandleTypeDef *huart, std::uint8_t *data, const std::uint16_t size)
|
||||||
{
|
{
|
||||||
return HAL_UART_Transmit_DMA(huart, data, size);
|
return HAL_UART_Transmit_DMA(huart, data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
}// namespace _Details
|
}
|
||||||
|
|
||||||
using UartInterface =
|
using UartInterface =
|
||||||
SerialInterface<UART_HandleTypeDef, _Details::uartTransmit, HAL_UART_Receive>;
|
SerialInterface<UART_HandleTypeDef, _Details::uartTransmit, HAL_UART_Receive>;
|
||||||
|
|||||||
@ -12,6 +12,7 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
#include "peripherals_imu.hpp"
|
#include "peripherals_imu.hpp"
|
||||||
|
#include "peripherals_utility.hpp"
|
||||||
|
|
||||||
namespace Peripherals
|
namespace Peripherals
|
||||||
{
|
{
|
||||||
|
|||||||
@ -54,8 +54,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);
|
||||||
@ -65,8 +64,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);
|
||||||
|
|||||||
68
Peripherals/Inc/peripherals_utility.hpp
Normal file
68
Peripherals/Inc/peripherals_utility.hpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* peripherals_utility.hpp
|
||||||
|
*
|
||||||
|
* Created on: Feb 24, 2021
|
||||||
|
* Author: erki
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SKULLC_PERIPHERALS_UTILITY_HPP_
|
||||||
|
#define SKULLC_PERIPHERALS_UTILITY_HPP_
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace Peripherals
|
||||||
|
{
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr const T& clamp(const T& v, const T& lo, const T& hi)
|
||||||
|
{
|
||||||
|
if (v > hi)
|
||||||
|
return hi;
|
||||||
|
else if (v < lo)
|
||||||
|
return lo;
|
||||||
|
else
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, std::size_t N>
|
||||||
|
T byteToTypeBE(const std::uint8_t a[N])
|
||||||
|
{
|
||||||
|
T t(0);
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < N; i++)
|
||||||
|
{
|
||||||
|
t |= a[i] << (i * 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, std::size_t N>
|
||||||
|
T byteToTypeLE(const std::uint8_t a[N])
|
||||||
|
{
|
||||||
|
T t(0);
|
||||||
|
|
||||||
|
for (std::size_t i = N; i >= 0; i--)
|
||||||
|
{
|
||||||
|
t |= a[i] << ((i - 1) * 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T zeroInitialized()
|
||||||
|
{
|
||||||
|
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
||||||
|
|
||||||
|
T t;
|
||||||
|
std::memset(&t, 0, sizeof(T));
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
}// namespace Peripherals
|
||||||
|
|
||||||
|
#endif /* SKULLC_PERIPHERALS_UTILITY_HPP_ */
|
||||||
@ -29,7 +29,6 @@ add_executable(tests
|
|||||||
assert_ndebug.cpp
|
assert_ndebug.cpp
|
||||||
assert.cpp
|
assert.cpp
|
||||||
enum_helpers.cpp
|
enum_helpers.cpp
|
||||||
bytes.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(tests
|
target_link_libraries(tests
|
||||||
|
|||||||
@ -1,68 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by erki on 10/12/22.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include <catch2/catch.hpp>
|
|
||||||
|
|
||||||
#include "utility_bytes.hpp"
|
|
||||||
|
|
||||||
TEST_CASE("zeroInitialized creates an appropriate struct.")
|
|
||||||
{
|
|
||||||
struct TestStruct
|
|
||||||
{
|
|
||||||
int a;
|
|
||||||
char b;
|
|
||||||
int c;
|
|
||||||
};
|
|
||||||
|
|
||||||
SECTION("Return by value variant returns a struct where all members are 0.")
|
|
||||||
{
|
|
||||||
const TestStruct test = Utility::zeroInitialized<TestStruct>();
|
|
||||||
REQUIRE(test.a == 0);
|
|
||||||
REQUIRE(test.b == 0);
|
|
||||||
REQUIRE(test.c == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("Pass by reference variant initializes the struct to all 0's.")
|
|
||||||
{
|
|
||||||
TestStruct test;
|
|
||||||
Utility::zeroInitialized(test);
|
|
||||||
|
|
||||||
REQUIRE(test.a == 0);
|
|
||||||
REQUIRE(test.b == 0);
|
|
||||||
REQUIRE(test.c == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("Pass by pointer variant initializes the struct to all 0's.")
|
|
||||||
{
|
|
||||||
TestStruct test;
|
|
||||||
Utility::zeroInitialized(&test);
|
|
||||||
|
|
||||||
REQUIRE(test.a == 0);
|
|
||||||
REQUIRE(test.b == 0);
|
|
||||||
REQUIRE(test.c == 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("arrayToType constructs a type appropriately.")
|
|
||||||
{
|
|
||||||
const std::uint32_t initial_value = 0x12345678;
|
|
||||||
|
|
||||||
SECTION("Using C-arrays the operation works appropriately.")
|
|
||||||
{
|
|
||||||
std::uint8_t raw_data[4] = {0};
|
|
||||||
std::memcpy(raw_data, &initial_value, 4);
|
|
||||||
|
|
||||||
const std::uint32_t output = Utility::arrayToType<std::uint32_t, 4>(raw_data);
|
|
||||||
REQUIRE(output == initial_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("Using C-arrays the operation works appropriately.")
|
|
||||||
{
|
|
||||||
std::array<std::uint8_t, 4> raw_data = {0, 0, 0, 0};
|
|
||||||
std::memcpy(raw_data.data(), &initial_value, 4);
|
|
||||||
|
|
||||||
const std::uint32_t output = Utility::arrayToType<std::uint32_t>(raw_data);
|
|
||||||
REQUIRE(output == initial_value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -65,8 +65,7 @@ public:
|
|||||||
if (!notified)
|
if (!notified)
|
||||||
{
|
{
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
return current_value_;
|
return current_value_;
|
||||||
}
|
}
|
||||||
@ -79,8 +78,7 @@ public:
|
|||||||
if (!waiting_thread_)
|
if (!waiting_thread_)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
return waiting_thread_->notify(0, eNoAction);
|
return waiting_thread_->notify(0, eNoAction);
|
||||||
}
|
}
|
||||||
@ -93,8 +91,7 @@ public:
|
|||||||
if (!waiting_thread_)
|
if (!waiting_thread_)
|
||||||
{
|
{
|
||||||
return {false, pdFALSE};
|
return {false, pdFALSE};
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
auto const [discard, was_notified] = waiting_thread_->notifyFromIsr(0, eNoAction);
|
auto const [discard, was_notified] = waiting_thread_->notifyFromIsr(0, eNoAction);
|
||||||
(void) discard;
|
(void) discard;
|
||||||
@ -161,8 +158,7 @@ struct ExclusiveSignal<void> : public Signallable<void>
|
|||||||
if (!waiting_thread_)
|
if (!waiting_thread_)
|
||||||
{
|
{
|
||||||
return pdFALSE;
|
return pdFALSE;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
auto const [discard, was_notified] = waiting_thread_->notifyFromIsr(0, eNoAction);
|
auto const [discard, was_notified] = waiting_thread_->notifyFromIsr(0, eNoAction);
|
||||||
(void) discard;
|
(void) discard;
|
||||||
|
|||||||
@ -11,9 +11,9 @@
|
|||||||
#include <cmsis_os.h>
|
#include <cmsis_os.h>
|
||||||
#include <freertos_os2.h>
|
#include <freertos_os2.h>
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
namespace Threads
|
namespace Threads
|
||||||
{
|
{
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#include "threads_primitivethread.hpp"
|
#include "threads_primitivethread.hpp"
|
||||||
|
|
||||||
#include "utility_bytes.hpp"
|
#include "peripherals_utility.hpp"
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
@ -108,7 +108,7 @@ PrimitiveThread::PrimitiveThread(TaskHandle_t threadHandle)
|
|||||||
|
|
||||||
osThreadAttr_t PrimitiveThread::constructThreadAttrs_(const char* name, const osPriority_t priority, const std::uint32_t stack_size)
|
osThreadAttr_t PrimitiveThread::constructThreadAttrs_(const char* name, const osPriority_t priority, const std::uint32_t stack_size)
|
||||||
{
|
{
|
||||||
auto attrs = Utility::zeroInitialized<osThreadAttr_t>();
|
auto attrs = Peripherals::zeroInitialized<osThreadAttr_t>();
|
||||||
|
|
||||||
attrs.name = name;
|
attrs.name = name;
|
||||||
attrs.priority = priority;
|
attrs.priority = priority;
|
||||||
|
|||||||
@ -1,69 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by erki on 10/12/22.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef SKULLC_UTILITY_BYTES_HPP_
|
|
||||||
#define SKULLC_UTILITY_BYTES_HPP_
|
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <cstdint>
|
|
||||||
#include <cstring>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
namespace Utility
|
|
||||||
{
|
|
||||||
|
|
||||||
template<typename T, std::size_t N>
|
|
||||||
T arrayToType(const std::uint8_t raw[N])
|
|
||||||
{
|
|
||||||
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
|
||||||
static_assert(sizeof(T) == N, "The raw data array is not the same size as T.");
|
|
||||||
|
|
||||||
T t;
|
|
||||||
std::memcpy(&t, raw, sizeof(T));
|
|
||||||
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, std::size_t N>
|
|
||||||
T arrayToType(const std::array<std::uint8_t, N> raw)
|
|
||||||
{
|
|
||||||
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
|
||||||
static_assert(sizeof(T) == N, "The raw data array is not the same size as T.");
|
|
||||||
|
|
||||||
T t;
|
|
||||||
std::memcpy(&t, raw.data(), sizeof(T));
|
|
||||||
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
constexpr T zeroInitialized()
|
|
||||||
{
|
|
||||||
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
|
||||||
|
|
||||||
T t;
|
|
||||||
std::memset(&t, 0, sizeof(T));
|
|
||||||
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void zeroInitialized(T& t)
|
|
||||||
{
|
|
||||||
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
|
||||||
|
|
||||||
std::memset(&t, 0, sizeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void zeroInitialized(T* t)
|
|
||||||
{
|
|
||||||
static_assert(std::is_trivially_default_constructible<T>::value, "Struct is not trivially default constructible.");
|
|
||||||
|
|
||||||
std::memset(t, 0, sizeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
}// namespace Utility
|
|
||||||
|
|
||||||
#endif//SKULLC_UTILITY_BYTES_HPP_
|
|
||||||
@ -158,18 +158,15 @@ public:
|
|||||||
if (dx == 0 && dy == 0)
|
if (dx == 0 && dy == 0)
|
||||||
{
|
{
|
||||||
at(start) = color;
|
at(start) = color;
|
||||||
}
|
} else if (dx == 0)
|
||||||
else if (dx == 0)
|
|
||||||
{
|
{
|
||||||
for (; y < y_(stop); y++)
|
for (; y < y_(stop); y++)
|
||||||
at({x, y}) = color;
|
at({x, y}) = color;
|
||||||
}
|
} else if (dy == 0)
|
||||||
else if (dy == 0)
|
|
||||||
{
|
{
|
||||||
for (; x < x_(stop); x++)
|
for (; x < x_(stop); x++)
|
||||||
at({x, y}) = color;
|
at({x, y}) = color;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
// Bresenham's algorithm.
|
// Bresenham's algorithm.
|
||||||
std::int32_t p = 2 * dy - dx;
|
std::int32_t p = 2 * dy - dx;
|
||||||
@ -182,8 +179,7 @@ public:
|
|||||||
{
|
{
|
||||||
y++;
|
y++;
|
||||||
p = p + 2 * dy - 2 * dx;
|
p = p + 2 * dy - 2 * dx;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
p = p + 2 * dy;
|
p = p + 2 * dy;
|
||||||
}
|
}
|
||||||
@ -221,8 +217,7 @@ public:
|
|||||||
{
|
{
|
||||||
y--;
|
y--;
|
||||||
d = d + 4 * (x - y) + 10;
|
d = d + 4 * (x - y) + 10;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
d = d + 4 * x + 6;
|
d = d + 4 * x + 6;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -68,8 +68,7 @@ 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,
|
return iterator(_arr_begin + difference_type(remainder), _arr_begin,
|
||||||
@ -83,8 +82,7 @@ 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,
|
return iterator(_arr_end - difference_type(remainder), _arr_begin,
|
||||||
@ -204,8 +202,7 @@ public:
|
|||||||
if (distance > 0)
|
if (distance > 0)
|
||||||
{
|
{
|
||||||
return distance;
|
return distance;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
return head_ - tail_ + 1;
|
return head_ - tail_ + 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user