Op Divider and bitutility updates

- Added unittests for `PeriodicOperationDivider` and the `bitutil` helpers
- Some API changes: Removed redundant bit part, because these functions are already in a namespace
- Some bugfixes for `PeriodicOperationDivider`
This commit is contained in:
2021-11-10 18:48:02 +01:00
parent 9a38106b57
commit 6d5eb5b387
10 changed files with 242 additions and 84 deletions

View File

@ -110,7 +110,7 @@ ReturnValue_t LocalPoolDataSetBase::serializeWithValidityBuffer(uint8_t **buffer
for (uint16_t count = 0; count < fillCount; count++) {
if(registeredVariables[count]->isValid()) {
/* Set bit at correct position */
bitutil::bitSet(validityPtr + validBufferIndex, validBufferIndexBit);
bitutil::set(validityPtr + validBufferIndex, validBufferIndexBit);
}
if(validBufferIndexBit == 7) {
validBufferIndex ++;
@ -156,8 +156,8 @@ ReturnValue_t LocalPoolDataSetBase::deSerializeWithValidityBuffer(
uint8_t validBufferIndexBit = 0;
for (uint16_t count = 0; count < fillCount; count++) {
// set validity buffer here.
bool nextVarValid = bitutil::bitGet(*buffer +
validBufferIndex, validBufferIndexBit);
bool nextVarValid = false;
bitutil::get(*buffer + validBufferIndex, validBufferIndexBit, nextVarValid);
registeredVariables[count]->setValid(nextVarValid);
if(validBufferIndexBit == 7) {

View File

@ -2,43 +2,40 @@
PeriodicOperationDivider::PeriodicOperationDivider(uint32_t divider,
bool resetAutomatically): resetAutomatically(resetAutomatically),
counter(divider), divider(divider) {
bool resetAutomatically): resetAutomatically(resetAutomatically),
divider(divider) {
}
bool PeriodicOperationDivider::checkAndIncrement() {
bool opNecessary = check();
if(opNecessary) {
if(resetAutomatically) {
counter = 0;
}
return opNecessary;
}
counter ++;
return opNecessary;
bool opNecessary = check();
if(opNecessary and resetAutomatically) {
resetCounter();
}
else {
counter++;
}
return opNecessary;
}
bool PeriodicOperationDivider::check() {
if(counter >= divider) {
return true;
}
return false;
if(counter >= divider) {
return true;
}
return false;
}
void PeriodicOperationDivider::resetCounter() {
counter = 0;
counter = 1;
}
void PeriodicOperationDivider::setDivider(uint32_t newDivider) {
divider = newDivider;
divider = newDivider;
}
uint32_t PeriodicOperationDivider::getCounter() const {
return counter;
return counter;
}
uint32_t PeriodicOperationDivider::getDivider() const {
return divider;
return divider;
}

View File

@ -13,51 +13,50 @@
*/
class PeriodicOperationDivider {
public:
/**
* Initialize with the desired divider and specify whether the internal
* counter will be reset automatically.
* @param divider
* @param resetAutomatically
*/
PeriodicOperationDivider(uint32_t divider, bool resetAutomatically = true);
/**
* Initialize with the desired divider and specify whether the internal
* counter will be reset automatically.
* @param divider Value of 0 or 1 will cause #check and #checkAndIncrement to always return
* true
* @param resetAutomatically
*/
PeriodicOperationDivider(uint32_t divider, bool resetAutomatically = true);
/**
* Check whether operation is necessary. If an operation is necessary and the class has been
* configured to be reset automatically, the counter will be reset to 1 automatically
*
* @return
* -@c true if the counter is larger or equal to the divider
* -@c false otherwise
*/
bool checkAndIncrement();
/**
* Check whether operation is necessary.
* If an operation is necessary and the class has been
* configured to be reset automatically, the counter will be reset.
*
* @return
* -@c true if the counter is larger or equal to the divider
* -@c false otherwise
*/
bool checkAndIncrement();
/**
* Checks whether an operation is necessary. This function will not increment the counter.
* @return
* -@c true if the counter is larger or equal to the divider
* -@c false otherwise
*/
bool check();
/**
* Checks whether an operation is necessary.
* This function will not increment the counter!
* @return
* -@c true if the counter is larger or equal to the divider
* -@c false otherwise
*/
bool check();
/**
* Can be used to reset the counter to 1 manually
*/
void resetCounter();
uint32_t getCounter() const;
/**
* Can be used to reset the counter to 0 manually.
*/
void resetCounter();
uint32_t getCounter() const;
/**
* Can be used to set a new divider value.
* @param newDivider
*/
void setDivider(uint32_t newDivider);
uint32_t getDivider() const;
/**
* Can be used to set a new divider value.
* @param newDivider
*/
void setDivider(uint32_t newDivider);
uint32_t getDivider() const;
private:
bool resetAutomatically = true;
uint32_t counter = 0;
uint32_t divider = 0;
bool resetAutomatically = true;
uint32_t counter = 1;
uint32_t divider = 0;
};

View File

@ -1,6 +1,6 @@
#include "fsfw/globalfunctions/bitutility.h"
void bitutil::bitSet(uint8_t *byte, uint8_t position) {
void bitutil::set(uint8_t *byte, uint8_t position) {
if(position > 7) {
return;
}
@ -8,7 +8,7 @@ void bitutil::bitSet(uint8_t *byte, uint8_t position) {
*byte |= 1 << shiftNumber;
}
void bitutil::bitToggle(uint8_t *byte, uint8_t position) {
void bitutil::toggle(uint8_t *byte, uint8_t position) {
if(position > 7) {
return;
}
@ -16,7 +16,7 @@ void bitutil::bitToggle(uint8_t *byte, uint8_t position) {
*byte ^= 1 << shiftNumber;
}
void bitutil::bitClear(uint8_t *byte, uint8_t position) {
void bitutil::clear(uint8_t *byte, uint8_t position) {
if(position > 7) {
return;
}
@ -24,10 +24,11 @@ void bitutil::bitClear(uint8_t *byte, uint8_t position) {
*byte &= ~(1 << shiftNumber);
}
bool bitutil::bitGet(const uint8_t *byte, uint8_t position) {
bool bitutil::get(const uint8_t *byte, uint8_t position, bool& bit) {
if(position > 7) {
return false;
}
uint8_t shiftNumber = position + (7 - 2 * position);
return *byte & (1 << shiftNumber);
bit = *byte & (1 << shiftNumber);
return true;
}

View File

@ -5,13 +5,36 @@
namespace bitutil {
/* Helper functions for manipulating the individual bits of a byte.
Position refers to n-th bit of a byte, going from 0 (most significant bit) to
7 (least significant bit) */
void bitSet(uint8_t* byte, uint8_t position);
void bitToggle(uint8_t* byte, uint8_t position);
void bitClear(uint8_t* byte, uint8_t position);
bool bitGet(const uint8_t* byte, uint8_t position);
// Helper functions for manipulating the individual bits of a byte.
// Position refers to n-th bit of a byte, going from 0 (most significant bit) to
// 7 (least significant bit)
/**
* @brief Set the bit in a given byte
* @param byte
* @param position
*/
void set(uint8_t* byte, uint8_t position);
/**
* @brief Toggle the bit in a given byte
* @param byte
* @param position
*/
void toggle(uint8_t* byte, uint8_t position);
/**
* @brief Clear the bit in a given byte
* @param byte
* @param position
*/
void clear(uint8_t* byte, uint8_t position);
/**
* @brief Get the bit in a given byte
* @param byte
* @param position
* @param If the input is valid, this will be set to true if the bit is set and false otherwise.
* @return False if position is invalid, True otherwise
*/
bool get(const uint8_t* byte, uint8_t position, bool& bit);
}