Compare commits

...

2 Commits

Author SHA1 Message Date
erki
ea99a8a6ba Other: fix ./clang-format to apply new-lines for else statements properly
All checks were successful
continuous-integration/drone/push Build is passing
gitea/skullc-peripherals/pipeline/head This commit looks good
2022-12-10 17:15:55 +02:00
erki
500c2704bb Peripherals, Utility: refactor out peripherals_utility.hpp into utility_bytes.hpp 2022-12-10 17:14:46 +02:00
13 changed files with 185 additions and 98 deletions

View File

@ -17,7 +17,7 @@ AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
@ -25,8 +25,8 @@ BraceWrapping:
AfterNamespace: true
AfterUnion: true
AfterStruct: true
BeforeCatch: false
BeforeElse: false
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: true

View File

@ -10,8 +10,6 @@
#include <main.h>
#include "peripherals_utility.hpp"
#include <array>
#define USE_DELAY_US
@ -96,9 +94,15 @@ struct Gpio
};
#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) \
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
@ -270,17 +274,17 @@ namespace _Details
* are const-correct. The others remain unconst. So these wrappers will exist until we're no longer partially
* 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);
}
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);
}
}
}// namespace _Details
using UartInterface =
SerialInterface<UART_HandleTypeDef, _Details::uartTransmit, HAL_UART_Receive>;

View File

@ -12,7 +12,6 @@
#include <limits>
#include "peripherals_imu.hpp"
#include "peripherals_utility.hpp"
namespace Peripherals
{

View File

@ -54,7 +54,8 @@ public:
{
left_.forward.setCompare(left);
left_.backward.setCompare(0);
} else
}
else
{
left_.forward.setCompare(0);
left_.backward.setCompare(-1 * left);
@ -64,7 +65,8 @@ public:
{
right_.forward.setCompare(right);
right_.backward.setCompare(0);
} else
}
else
{
right_.forward.setCompare(0);
right_.backward.setCompare(-1 * right);

View File

@ -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_ */

View File

@ -29,6 +29,7 @@ add_executable(tests
assert_ndebug.cpp
assert.cpp
enum_helpers.cpp
bytes.cpp
)
target_link_libraries(tests

68
Tests/bytes.cpp Normal file
View 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);
}
}

View File

@ -65,7 +65,8 @@ public:
if (!notified)
{
return std::nullopt;
} else
}
else
{
return current_value_;
}
@ -78,7 +79,8 @@ public:
if (!waiting_thread_)
{
return false;
} else
}
else
{
return waiting_thread_->notify(0, eNoAction);
}
@ -91,7 +93,8 @@ public:
if (!waiting_thread_)
{
return {false, pdFALSE};
} else
}
else
{
auto const [discard, was_notified] = waiting_thread_->notifyFromIsr(0, eNoAction);
(void) discard;
@ -158,7 +161,8 @@ struct ExclusiveSignal<void> : public Signallable<void>
if (!waiting_thread_)
{
return pdFALSE;
} else
}
else
{
auto const [discard, was_notified] = waiting_thread_->notifyFromIsr(0, eNoAction);
(void) discard;

View File

@ -11,9 +11,9 @@
#include <cmsis_os.h>
#include <freertos_os2.h>
#include <cstdint>
#include <tuple>
#include <type_traits>
#include <cstdint>
namespace Threads
{

View File

@ -7,7 +7,7 @@
#include "threads_primitivethread.hpp"
#include "peripherals_utility.hpp"
#include "utility_bytes.hpp"
#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)
{
auto attrs = Peripherals::zeroInitialized<osThreadAttr_t>();
auto attrs = Utility::zeroInitialized<osThreadAttr_t>();
attrs.name = name;
attrs.priority = priority;

View 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_

View File

@ -158,15 +158,18 @@ public:
if (dx == 0 && dy == 0)
{
at(start) = color;
} else if (dx == 0)
}
else if (dx == 0)
{
for (; y < y_(stop); y++)
at({x, y}) = color;
} else if (dy == 0)
}
else if (dy == 0)
{
for (; x < x_(stop); x++)
at({x, y}) = color;
} else
}
else
{
// Bresenham's algorithm.
std::int32_t p = 2 * dy - dx;
@ -179,7 +182,8 @@ public:
{
y++;
p = p + 2 * dy - 2 * dx;
} else
}
else
{
p = p + 2 * dy;
}
@ -217,7 +221,8 @@ public:
{
y--;
d = d + 4 * (x - y) + 10;
} else
}
else
{
d = d + 4 * x + 6;
}

View File

@ -68,7 +68,8 @@ public:
if (naive < _arr_end)
{
return iterator(naive, _arr_begin, _arr_end);
} else
}
else
{
const pointer remainder = pointer(naive - _arr_end);
return iterator(_arr_begin + difference_type(remainder), _arr_begin,
@ -82,7 +83,8 @@ public:
if (naive >= _arr_begin)
{
return iterator(naive, _arr_begin, _arr_end);
} else
}
else
{
const pointer remainder = pointer(_arr_begin - naive);
return iterator(_arr_end - difference_type(remainder), _arr_begin,
@ -202,7 +204,8 @@ public:
if (distance > 0)
{
return distance;
} else
}
else
{
return head_ - tail_ + 1;
}