Skip to content

Commit 5027e63

Browse files
committed
Merge branch 'development' into CenterAligned
2 parents 5373015 + d8ec77a commit 5027e63

16 files changed

Lines changed: 293 additions & 283 deletions

File tree

Inc/C++Utilities/RingBuffer.hpp

Lines changed: 59 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,64 @@
22

33
#include "CppImports.hpp"
44

5+
template <typename T, size_t N>
6+
class RingBuffer {
7+
std::array<T, N> buffer{};
58

6-
/**
7-
* @brief a Buffer class that acts as a RingBuffer using FIFO behaviour
8-
*/
9-
template <typename item_type, size_t N>
10-
class RingBuffer{
11-
public:
12-
RingBuffer(): msize(0), mcapacity(N), mhead_index(0), mlast_index(N){}
13-
14-
15-
/**
16-
* @brief introduces a new item on the Ring Buffer unless it is full. Returns false when the Ring Buffer is full.
17-
*/
18-
bool push(item_type item){
19-
if(msize == mcapacity){
20-
return false;
21-
}
22-
23-
msize++;
24-
mlast_index++;
25-
if(mlast_index >= mcapacity){
26-
mlast_index = 0;
27-
}
28-
29-
buffer[mlast_index] = item;
30-
return true;
31-
}
32-
33-
34-
bool is_empty(){return msize == 0;} /**< @brief returns true when RingBuffer is empty, false otherwise.*/
35-
bool is_full(){return msize == mcapacity;} /**< @brief returns true when RingBuffer is full, false otherwise.*/
36-
uint32_t size(){return msize;} /**< @brief returns the occupied size of the RingBuffer.*/
37-
38-
39-
40-
41-
/**
42-
* @brief checks the next variable to read without removing it from the buffer. Do not use on empty buffer.
43-
*/
44-
item_type peek(){
45-
return buffer[mhead_index];
46-
}
47-
48-
/**
49-
* @brief checks the next variable to read and removes it from the buffer. Do not use on empty buffer.
50-
*/
51-
item_type pop(){
52-
uint32_t return_index = mhead_index;
53-
mhead_index++;
54-
msize--;
55-
if(mhead_index >= mcapacity){
56-
mhead_index = 0;
57-
}
58-
return buffer[return_index];
59-
}
60-
61-
/**
62-
* @brief pop later and push new. Don't care size of buffer, needs to be initialize before
63-
*/
64-
void push_pop(item_type item){
65-
mhead_index = (mhead_index+1)%mcapacity;
66-
mlast_index = (mlast_index+1)%mcapacity;
67-
buffer[mhead_index] = item;
68-
}
69-
70-
item_type latest()const{
71-
return buffer[mlast_index];
72-
}
73-
74-
private:
75-
std::array<item_type, N> buffer;
76-
uint32_t msize;
77-
uint32_t mcapacity;
78-
uint32_t mhead_index;
79-
uint32_t mlast_index;
9+
size_t stored_items{0};
10+
11+
size_t front{0};
12+
size_t back{0};
13+
14+
size_t move_forward(size_t origin, size_t amount) {
15+
return (origin + amount) % N;
16+
}
17+
18+
size_t move_backward(size_t origin, size_t amount) {
19+
// N * ((amount / N) + 1) makes it so that the operation doesn't
20+
// overflow (size_t is unsigned) and with the help of modular
21+
// arithmetic, this won't change the result
22+
return ((origin + (N * ((amount / N) + 1))) - amount) % N;
23+
}
24+
25+
public:
26+
RingBuffer() {}
27+
28+
bool push(T item) {
29+
if (is_full()) return false;
30+
31+
buffer[front] = item;
32+
front = move_forward(front, 1);
33+
++stored_items;
34+
35+
return true;
36+
}
37+
38+
bool pop() {
39+
if (is_empty()) return false;
40+
41+
back = move_forward(back, 1);
42+
--stored_items;
43+
44+
return true;
45+
}
46+
47+
bool push_pop(T item) {
48+
if (is_empty()) return false;
49+
50+
buffer[front] = item;
51+
front = move_forward(front, 1);
52+
back = move_forward(back, 1);
53+
54+
return true;
55+
}
56+
57+
T &operator[](size_t index) {
58+
return buffer[move_backward(front, index + 1)];
59+
}
60+
61+
constexpr size_t capacity() { return N; }
62+
size_t size() { return stored_items; }
63+
bool is_full() { return size() >= capacity(); }
64+
bool is_empty() { return size() == 0; }
8065
};

Inc/HALAL/Services/DigitalOutputService/DigitalOutputService.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
#pragma once
88
#include "HALAL/Models/PinModel/Pin.hpp"
99

10-
class DigitalOutputService{
11-
public:
12-
static map<uint8_t,Pin> service_ids;
13-
static uint8_t id_counter;
10+
class DigitalOutputService {
11+
public:
12+
static map<uint8_t, Pin> service_ids;
13+
static uint8_t id_counter;
1414

15-
static uint8_t inscribe(Pin& pin);
16-
static void turn_on(uint8_t id);
17-
static void turn_off(uint8_t id);
18-
static void set_pin_state(uint8_t id, PinState state);
19-
static void toggle(uint8_t id);
15+
static uint8_t inscribe(Pin& pin);
16+
static void turn_on(uint8_t id);
17+
static void turn_off(uint8_t id);
18+
static void set_pin_state(uint8_t id, PinState state);
19+
static void toggle(uint8_t id);
20+
static bool lock_pin_state(uint8_t id, PinState state);
2021
};
Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
#pragma once
2-
#include "HALAL/Models/PinModel/Pin.hpp"
32
#include "C++Utilities/CppUtils.hpp"
43
#include "ErrorHandler/ErrorHandler.hpp"
5-
4+
#include "HALAL/Models/PinModel/Pin.hpp"
65

76
extern IWDG_HandleTypeDef watchdog_handle;
8-
7+
extern bool reset_by_iwdg;
98

109
/**
1110
* @brief The watchdog class resets the board when it gets stuck inside a loop
1211
*
13-
* To ensure that the code its not stuck, the refresh function must be called at least once each period
14-
* Otherwise, the Watchdog will reset the board. The Watchdog its not active until it is started.
12+
* To ensure that the code is not stuck, the refresh function must be called at
13+
* least once every period; otherwise, the Watchdog will reset the board. The
14+
* Watchdog is not active until it is started.
1515
*/
16-
class Watchdog{
17-
public:
18-
19-
template<typename TimeUnit>
20-
static void start(chrono::duration<int64_t, TimeUnit> period){
21-
if((chrono::duration_cast<chrono::microseconds>(period)).count() > 32000000){
22-
ErrorHandler("Watchdog refresh interval is too big");
23-
}
24-
if((chrono::duration_cast<chrono::microseconds>(period)).count() < 125){
25-
ErrorHandler("Watchdog refresh interval is too short");
26-
}
27-
uint64_t microseconds = chrono::duration_cast<chrono::microseconds>(period).count();
28-
uint32_t RL = (uint64_t)((double) microseconds / 4 / 31.25) - 1;
16+
class Watchdog {
17+
public:
18+
static std::chrono::microseconds watchdog_time;
19+
20+
static void start() {
21+
if ((chrono::duration_cast<chrono::microseconds>(watchdog_time)).count() > 32000000) {
22+
ErrorHandler("Watchdog refresh interval is too big");
23+
}
24+
if ((chrono::duration_cast<chrono::microseconds>(watchdog_time)).count() < 125) {
25+
ErrorHandler("Watchdog refresh interval is too short");
26+
}
27+
uint64_t milliseconds = chrono::duration_cast<chrono::milliseconds>(watchdog_time).count();
28+
uint32_t RL = double(milliseconds) * 8.0 - 1; // this is the formula for the Reload
2929
uint32_t prescaler = 0;
30-
31-
while(RL > 4095){
32-
microseconds /= 2;
33-
prescaler += 1;
30+
while (RL > 4095) {
31+
milliseconds /= 2;
32+
prescaler += 1;
33+
RL = double(milliseconds) * 8.0 - 1;
3434
}
3535

3636
watchdog_handle.Instance = IWDG1;
@@ -40,8 +40,18 @@ class Watchdog{
4040
HAL_IWDG_Init(&watchdog_handle);
4141
}
4242

43-
static inline void refresh(){
44-
__HAL_IWDG_RELOAD_COUNTER(&watchdog_handle);
43+
static void refresh() {
44+
HAL_IWDG_Refresh(&watchdog_handle);
45+
}
46+
static void check_reset_flag(){
47+
if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDG1RST)) {
48+
reset_by_iwdg = true;
49+
50+
}
51+
__HAL_RCC_CLEAR_RESET_FLAGS();
52+
}
53+
template <typename TimeUnit>
54+
Watchdog(chrono::duration<int64_t, TimeUnit> period) {
55+
watchdog_time = std::chrono::duration_cast<std::chrono::microseconds>(period);
4556
}
46-
4757
};

Inc/HALALMock/Services/DigitalOutputService/DigitalOutputService.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ class DigitalOutputService {
1919
static void turn_off(uint8_t id);
2020
static void set_pin_state(uint8_t id, PinState state);
2121
static void toggle(uint8_t id);
22+
static bool lock_pin_state(uint8_t id, PinState state);
2223
};

Inc/ST-LIB.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "HALAL/HALAL.hpp" // Erase this when ST-LIB_LOW is finished.
44
#include "ST-LIB_LOW.hpp" // Erase this when ST-LIB_HIGH is finished
55
#include "ST-LIB_HIGH.hpp" // Highest layer up to the moment
6-
76
class STLIB {
87
public:
98

Inc/ST-LIB_LOW/DigitalOutput/DigitalOutput.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
#include "HALAL/HALAL.hpp"
1111

1212
class DigitalOutput {
13-
public:
14-
DigitalOutput() = default;
15-
DigitalOutput(Pin& pin);
16-
17-
void turn_on();
18-
void turn_off();
19-
void toggle();
20-
void set_pin_state(PinState state);
21-
private:
22-
Pin pin;
23-
uint8_t id;
24-
};
13+
public:
14+
DigitalOutput() = default;
15+
DigitalOutput(Pin& pin);
2516

17+
void turn_on();
18+
void turn_off();
19+
void toggle();
20+
void set_pin_state(PinState state);
21+
bool lock_pin_state(PinState state);
2622

23+
private:
24+
Pin pin;
25+
uint8_t id;
26+
};

0 commit comments

Comments
 (0)