Compare commits
2 Commits
8fbb0efd5d
...
ea99a8a6ba
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ea99a8a6ba | ||
|
|
500c2704bb |
@ -17,7 +17,7 @@ AlwaysBreakAfterReturnType: None
|
|||||||
AlwaysBreakTemplateDeclarations: Yes
|
AlwaysBreakTemplateDeclarations: Yes
|
||||||
BreakBeforeBraces: Custom
|
BreakBeforeBraces: Custom
|
||||||
BraceWrapping:
|
BraceWrapping:
|
||||||
AfterCaseLabel: false
|
AfterCaseLabel: true
|
||||||
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: false
|
BeforeCatch: true
|
||||||
BeforeElse: false
|
BeforeElse: true
|
||||||
IndentBraces: false
|
IndentBraces: false
|
||||||
SplitEmptyFunction: false
|
SplitEmptyFunction: false
|
||||||
SplitEmptyRecord: true
|
SplitEmptyRecord: true
|
||||||
|
|||||||
@ -10,8 +10,6 @@
|
|||||||
|
|
||||||
#include <main.h>
|
#include <main.h>
|
||||||
|
|
||||||
#include "peripherals_utility.hpp"
|
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
#define USE_DELAY_US
|
#define USE_DELAY_US
|
||||||
@ -96,9 +94,15 @@ struct Gpio
|
|||||||
};
|
};
|
||||||
|
|
||||||
#define CREATE_GPIO(name) \
|
#define CREATE_GPIO(name) \
|
||||||
Peripherals::Hal::St::Gpio { name##_GPIO_Port, name##_Pin, false }
|
Peripherals::Hal::St::Gpio \
|
||||||
|
{ \
|
||||||
|
name##_GPIO_Port, name##_Pin, false \
|
||||||
|
}
|
||||||
#define CREATE_INV_GPIO(name) \
|
#define CREATE_INV_GPIO(name) \
|
||||||
Peripherals::Hal::St::Gpio { name##_GPIO_Port, name##_Pin, true }
|
Peripherals::Hal::St::Gpio \
|
||||||
|
{ \
|
||||||
|
name##_GPIO_Port, name##_Pin, true \
|
||||||
|
}
|
||||||
|
|
||||||
#endif// HAL_GPIO_MODULE_ENABLED
|
#endif// HAL_GPIO_MODULE_ENABLED
|
||||||
|
|
||||||
@ -280,7 +284,7 @@ inline HAL_StatusTypeDef uartTransmitDma(UART_HandleTypeDef *huart, std::uint8_t
|
|||||||
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,7 +12,6 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
#include "peripherals_imu.hpp"
|
#include "peripherals_imu.hpp"
|
||||||
#include "peripherals_utility.hpp"
|
|
||||||
|
|
||||||
namespace Peripherals
|
namespace Peripherals
|
||||||
{
|
{
|
||||||
|
|||||||
@ -54,7 +54,8 @@ 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,7 +65,8 @@ 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);
|
||||||
|
|||||||
@ -1,68 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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,6 +29,7 @@ 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
|
||||||
|
|||||||
68
Tests/bytes.cpp
Normal file
68
Tests/bytes.cpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
//
|
||||||
|
// 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,7 +65,8 @@ public:
|
|||||||
if (!notified)
|
if (!notified)
|
||||||
{
|
{
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
return current_value_;
|
return current_value_;
|
||||||
}
|
}
|
||||||
@ -78,7 +79,8 @@ 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);
|
||||||
}
|
}
|
||||||
@ -91,7 +93,8 @@ 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;
|
||||||
@ -158,7 +161,8 @@ 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 "peripherals_utility.hpp"
|
#include "utility_bytes.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 = Peripherals::zeroInitialized<osThreadAttr_t>();
|
auto attrs = Utility::zeroInitialized<osThreadAttr_t>();
|
||||||
|
|
||||||
attrs.name = name;
|
attrs.name = name;
|
||||||
attrs.priority = priority;
|
attrs.priority = priority;
|
||||||
|
|||||||
69
Utility/Inc/utility_bytes.hpp
Normal file
69
Utility/Inc/utility_bytes.hpp
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
//
|
||||||
|
// 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,15 +158,18 @@ 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;
|
||||||
@ -179,7 +182,8 @@ 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;
|
||||||
}
|
}
|
||||||
@ -217,7 +221,8 @@ 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,7 +68,8 @@ 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,
|
||||||
@ -82,7 +83,8 @@ 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,
|
||||||
@ -202,7 +204,8 @@ 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