Introduce short namespace aliases

especially for modules
pull/79/head
D.R.racer 2021-08-02 12:17:30 +02:00 committed by DRracer
parent 9232e55563
commit 2f5dff6c5b
47 changed files with 291 additions and 420 deletions

View File

@ -74,9 +74,9 @@ void USART::puts(const char *str) {
} // namespace hal
ISR(USART1_RX_vect) {
hal::usart::usart1.ISR_RX();
hu::usart1.ISR_RX();
}
ISR(USART1_UDRE_vect) {
hal::usart::usart1.ISR_UDRE();
hu::usart1.ISR_UDRE();
}

View File

@ -88,7 +88,7 @@ public:
husart->UCSRxB &= ~(1 << 5); // disable UDRE interrupt
}
USART(hal::usart::USART::USART_TypeDef *husart)
USART(USART_TypeDef *husart)
: husart(husart) {};
private:
@ -107,3 +107,5 @@ extern USART usart1;
} // namespace hal
#define USART1 ((hal::usart::USART::USART_TypeDef *)&UCSR1A)
namespace hu = hal::usart;

View File

@ -3,10 +3,6 @@
#include "../modules/selector.h"
#include "../modules/motion.h"
namespace mi = modules::idler;
namespace ms = modules::selector;
namespace mm = modules::motion;
namespace logic {
inline ErrorCode &operator|=(ErrorCode &a, ErrorCode b) {

View File

@ -12,12 +12,6 @@ namespace logic {
CutFilament cutFilament;
namespace mm = modules::motion;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace ms = modules::selector;
namespace mg = modules::globals;
void CutFilament::Reset(uint8_t param) {
error = ErrorCode::OK;
cutSlot = param;

View File

@ -12,11 +12,6 @@ namespace logic {
EjectFilament ejectFilament;
namespace mm = modules::motion;
namespace mi = modules::idler;
namespace ms = modules::selector;
namespace mg = modules::globals;
void EjectFilament::Reset(uint8_t param) {
error = ErrorCode::OK;
slot = param;

View File

@ -9,13 +9,6 @@
namespace logic {
namespace mm = modules::motion;
namespace mfs = modules::fsensor;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mp = modules::permanent_storage;
namespace mg = modules::globals;
void FeedToBondtech::Reset(uint8_t maxRetries) {
state = EngagingIdler;
this->maxRetries = maxRetries;
@ -23,7 +16,7 @@ void FeedToBondtech::Reset(uint8_t maxRetries) {
}
bool FeedToBondtech::Step() {
const uint16_t steps = mp::BowdenLength::get();
const uint16_t steps = mps::BowdenLength::get();
switch (state) {
case EngagingIdler:

View File

@ -10,14 +10,6 @@
namespace logic {
namespace mm = modules::motion;
namespace mf = modules::finda;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mg = modules::globals;
namespace ms = modules::selector;
namespace mu = modules::user_input;
void FeedToFinda::Reset(bool feedPhaseLimited) {
state = EngagingIdler;
this->feedPhaseLimited = feedPhaseLimited;
@ -33,11 +25,11 @@ bool FeedToFinda::Step() {
state = PushingFilament;
ml::leds.SetMode(mg::globals.ActiveSlot(), ml::Color::green, ml::blink0);
mm::motion.PlanMove(mm::Pulley, feedPhaseLimited ? 1500 : 32767, 4000); //@@TODO constants
mu::userInput.Clear(); // remove all buffered events if any just before we wait for some input
mui::userInput.Clear(); // remove all buffered events if any just before we wait for some input
}
return false;
case PushingFilament: {
if (mf::finda.Pressed() || (feedPhaseLimited && mu::userInput.AnyEvent())) { // @@TODO probably also a command from the printer
if (mf::finda.Pressed() || (feedPhaseLimited && mui::userInput.AnyEvent())) { // @@TODO probably also a command from the printer
mm::motion.AbortPlannedMoves(); // stop pushing filament
// FINDA triggered - that means it works and detected the filament tip
state = UnloadBackToPTFE;

View File

@ -12,14 +12,6 @@ namespace logic {
LoadFilament loadFilament;
namespace mm = modules::motion;
namespace mi = modules::idler;
namespace ms = modules::selector;
namespace mf = modules::finda;
namespace ml = modules::leds;
namespace mg = modules::globals;
namespace mu = modules::user_input;
void LoadFilament::Reset(uint8_t param) {
state = ProgressCode::EngagingIdler;
error = ErrorCode::OK;
@ -77,23 +69,23 @@ bool LoadFilament::StepInner() {
case ProgressCode::ERRDisengagingIdler: // couldn't unload to FINDA
if (!mi::idler.Engaged()) {
state = ProgressCode::ERRWaitingForUser;
mu::userInput.Clear(); // remove all buffered events if any just before we wait for some input
mui::userInput.Clear(); // remove all buffered events if any just before we wait for some input
}
return false;
case ProgressCode::ERRWaitingForUser: {
// waiting for user buttons and/or a command from the printer
mu::Event ev = mu::userInput.ConsumeEvent();
mui::Event ev = mui::userInput.ConsumeEvent();
switch (ev) {
case mu::Event::Left: // try to manually load just a tiny bit - help the filament with the pulley
case mui::Event::Left: // try to manually load just a tiny bit - help the filament with the pulley
state = ProgressCode::ERREngagingIdler;
mi::idler.Engage(mg::globals.ActiveSlot());
break;
case mu::Event::Middle: // try again the whole sequence
case mui::Event::Middle: // try again the whole sequence
Reset(mg::globals.ActiveSlot());
break;
case mu::Event::Right: // problem resolved - the user pushed the fillament by hand?
modules::leds::leds.SetMode(mg::globals.ActiveSlot(), modules::leds::red, modules::leds::off);
modules::leds::leds.SetMode(mg::globals.ActiveSlot(), modules::leds::green, modules::leds::on);
case mui::Event::Right: // problem resolved - the user pushed the fillament by hand?
ml::leds.SetMode(mg::globals.ActiveSlot(), ml::red, ml::off);
ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::on);
// mm::motion.PlanMove(mm::Pulley, 450, 5000); // @@TODO constants
state = ProgressCode::AvoidingGrind;
break;

View File

@ -12,8 +12,6 @@ namespace logic {
ToolChange toolChange;
namespace mg = modules::globals;
void ToolChange::Reset(uint8_t param) {
if (param == mg::globals.ActiveSlot())
return;

View File

@ -11,13 +11,6 @@ namespace logic {
UnloadFilament unloadFilament;
namespace mm = modules::motion;
namespace mf = modules::finda;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mg = modules::globals;
namespace mu = modules::user_input;
void UnloadFilament::Reset(uint8_t /*param*/) {
// unloads filament from extruder - filament is above Bondtech gears
mm::motion.InitAxis(mm::Pulley);
@ -71,21 +64,21 @@ bool UnloadFilament::StepInner() {
case ProgressCode::ERRDisengagingIdler: // couldn't unload to FINDA
if (!mi::idler.Engaged()) {
state = ProgressCode::ERRWaitingForUser;
mu::userInput.Clear(); // remove all buffered events if any just before we wait for some input
mui::userInput.Clear(); // remove all buffered events if any just before we wait for some input
}
return false;
case ProgressCode::ERRWaitingForUser: {
// waiting for user buttons and/or a command from the printer
mu::Event ev = mu::userInput.ConsumeEvent();
mui::Event ev = mui::userInput.ConsumeEvent();
switch (ev) {
case mu::Event::Left: // try to manually unload just a tiny bit - help the filament with the pulley
case mui::Event::Left: // try to manually unload just a tiny bit - help the filament with the pulley
state = ProgressCode::ERREngagingIdler;
mi::idler.Engage(mg::globals.ActiveSlot());
break;
case mu::Event::Middle: // try again the whole sequence
case mui::Event::Middle: // try again the whole sequence
Reset(0); //@@TODO validate the reset parameter
break;
case mu::Event::Right: // problem resolved - the user pulled the fillament by hand
case mui::Event::Right: // problem resolved - the user pulled the fillament by hand
ml::leds.SetMode(mg::globals.ActiveSlot(), ml::red, ml::off);
ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::on);
// mm::motion.PlanMove(mm::Pulley, 450, 5000); // @@TODO constants

View File

@ -8,13 +8,6 @@
namespace logic {
namespace mm = modules::motion;
namespace mg = modules::globals;
namespace mf = modules::finda;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mp = modules::permanent_storage;
void UnloadToFinda::Reset(uint8_t maxTries) {
this->maxTries = maxTries;
// check the inital state of FINDA and plan the moves
@ -32,7 +25,7 @@ bool UnloadToFinda::Step() {
case EngagingIdler:
if (mi::idler.Engaged()) {
state = WaitingForFINDA;
int unloadSteps = mp::BowdenLength::get() + 1100;
int unloadSteps = mps::BowdenLength::get() + 1100;
const int second_point = unloadSteps - 1300;
mm::motion.PlanMove(mm::Pulley, -1400, 6000); // @@TODO constants
mm::motion.PlanMove(mm::Pulley, -1800 + 1400, 2500); // @@TODO constants 1800-1400 = 400

View File

@ -30,19 +30,6 @@
#include "version.h"
namespace mb = modules::buttons;
namespace mp = modules::protocol;
namespace mf = modules::finda;
namespace mfs = modules::fsensor;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace ms = modules::selector;
namespace mg = modules::globals;
namespace mu = modules::user_input;
namespace mt = modules::time;
namespace hu = hal::usart;
/// Global instance of the protocol codec
static mp::Protocol protocol;
@ -158,9 +145,9 @@ bool WriteToUSART(const uint8_t *src, uint8_t len) {
// The MMU cannot produce response messages on its own - it only responds to requests from the printer.
// That means there is only one message in the output buffer at once as long as the printer waits for the response before sending another request.
for (uint8_t i = 0; i < len; ++i) {
if (hal::usart::usart1.CanWrite()) {
if (hu::usart1.CanWrite()) {
// should not block waiting for the TX buffer to drain as there was an empty spot for at least 1 byte
hal::usart::usart1.Write(src[i]);
hu::usart1.Write(src[i]);
} else {
//buffer full - must skip the rest of the message - the communication will drop out anyway
return false;
@ -265,7 +252,7 @@ void ProcessRequestMsg(const mp::RequestMsg &rq) {
switch (rq.code) {
case mp::RequestMsgCodes::Button:
// behave just like if the user pressed a button
mu::userInput.ProcessMessage(rq.value);
mui::userInput.ProcessMessage(rq.value);
break;
case mp::RequestMsgCodes::Finda:
// immediately report FINDA status
@ -347,7 +334,7 @@ void loop() {
mfs::fsensor.Step();
mi::idler.Step();
ms::selector.Step();
mu::userInput.Step();
mui::userInput.Step();
currentCommand->Step();
// add a watchdog reset
}

View File

@ -23,7 +23,7 @@ int8_t Buttons::DecodeADC(uint16_t rawADC) {
}
void Buttons::Step() {
uint16_t millis = modules::time::timebase.Millis();
uint16_t millis = mt::timebase.Millis();
int8_t currentState = DecodeADC(hal::adc::ReadADC(config::buttonsADCIndex));
for (uint_fast8_t b = 0; b < config::buttonCount; ++b) {
// this button was pressed if b == currentState, released otherwise

View File

@ -59,3 +59,5 @@ extern Buttons buttons;
} // namespace buttons
} // namespace modules
namespace mb = modules::buttons;

View File

@ -53,3 +53,5 @@ private:
} // namespace debounce
} // namespace modules
namespace md = modules::debounce;

View File

@ -8,7 +8,7 @@ namespace finda {
FINDA finda;
void FINDA::Step() {
debounce::Debouncer::Step(modules::time::timebase.Millis(), hal::adc::ReadADC(config::findaADCIndex) > config::findaADCDecisionLevel);
debounce::Debouncer::Step(mt::timebase.Millis(), hal::adc::ReadADC(config::findaADCIndex) > config::findaADCDecisionLevel);
}
} // namespace finda

View File

@ -25,3 +25,5 @@ extern FINDA finda;
} // namespace finda
} // namespace modules
namespace mf = modules::finda;

View File

@ -7,7 +7,7 @@ namespace fsensor {
FSensor fsensor;
void FSensor::Step() {
debounce::Debouncer::Step(modules::time::timebase.Millis(), reportedFSensorState);
debounce::Debouncer::Step(mt::timebase.Millis(), reportedFSensorState);
}
void FSensor::ProcessMessage(bool on) {

View File

@ -33,3 +33,5 @@ extern FSensor fsensor;
} // namespace fsensor
} // namespace modules
namespace mfs = modules::fsensor;

View File

@ -7,7 +7,7 @@ namespace globals {
Globals globals;
void Globals::Init() {
modules::permanent_storage::FilamentLoaded::get(activeSlot); //@@TODO check for errors
mps::FilamentLoaded::get(activeSlot); //@@TODO check for errors
// @@TODO where to obtain information whether a slot is loaded with a filament?
}
@ -17,7 +17,7 @@ uint8_t Globals::ActiveSlot() const {
void Globals::SetActiveSlot(uint8_t newActiveSlot) {
activeSlot = newActiveSlot;
modules::permanent_storage::FilamentLoaded::set(activeSlot);
mps::FilamentLoaded::set(activeSlot);
}
bool Globals::FilamentLoaded() const {
@ -29,11 +29,11 @@ void Globals::SetFilamentLoaded(bool newFilamentLoaded) {
}
uint16_t Globals::DriveErrors() const {
return modules::permanent_storage::DriveError::get();
return mps::DriveError::get();
}
void Globals::IncDriveErrors() {
modules::permanent_storage::DriveError::increment();
mps::DriveError::increment();
}
} // namespace globals

View File

@ -50,3 +50,5 @@ extern Globals globals;
} // namespace globals
} // namespace modules
namespace mg = modules::globals;

View File

@ -9,8 +9,6 @@ namespace idler {
Idler idler;
namespace mm = modules::motion;
void Idler::PrepareMoveToPlannedSlot() {
mm::motion.PlanMoveTo<mm::Idler>(SlotPosition(plannedSlot), 1000._I_deg_s); // @@TODO
}

View File

@ -66,3 +66,5 @@ extern Idler idler;
} // namespace idler
} // namespace modules
namespace mi = modules::idler;

View File

@ -41,7 +41,7 @@ bool LED::Step(bool oddPeriod) {
}
void LEDs::Step() {
uint16_t millis = modules::time::timebase.Millis();
uint16_t millis = mt::timebase.Millis();
bool oddPeriod = ((millis / 1000U) & 0x01U) != 0;
uint16_t result = 0;
for (int8_t i = ledPairs * 2 - 1; i >= 0; --i) {

View File

@ -136,3 +136,5 @@ extern LEDs leds;
} // namespace LEDs
} // namespace modules
namespace ml = modules::leds;

View File

@ -254,3 +254,5 @@ extern Motion motion;
} // namespace motion
} // namespace modules
namespace mm = modules::motion;

View File

@ -88,7 +88,7 @@ static bool validBowdenLen(const uint16_t BowdenLength) {
/// Returns stored value, doesn't return actual value when it is edited by increase() / decrease() unless it is stored.
/// @return stored bowden length
uint16_t BowdenLength::get() {
uint8_t filament = modules::globals::globals.ActiveSlot();
uint8_t filament = mg::globals.ActiveSlot();
if (validFilament(filament)) {
// @@TODO these reinterpret_cast expressions look horrible but I'm keeping them almost intact to respect the original code from MM_control_01
uint16_t bowdenLength = ee::EEPROM::ReadByte(reinterpret_cast<size_t>(&(eepromBase->eepromBowdenLen[filament])));
@ -111,7 +111,7 @@ uint16_t BowdenLength::get() {
/// To be created on stack, new value is permanently stored when object goes out of scope.
/// Active filament and associated bowden length is stored in member variables.
BowdenLength::BowdenLength()
: filament(modules::globals::globals.ActiveSlot()) // @@TODO - verify correct initialization order
: filament(mg::globals.ActiveSlot()) // @@TODO - verify correct initialization order
, length(BowdenLength::get()) // @@TODO
{
}

View File

@ -99,3 +99,5 @@ private:
} // namespace permanent_storage
} // namespace modules
namespace mps = modules::permanent_storage;

View File

@ -162,3 +162,5 @@ private:
} // namespace protocol
} // namespace modules
namespace mp = modules::protocol;

View File

@ -9,8 +9,6 @@ namespace selector {
Selector selector;
namespace mm = modules::motion;
void Selector::PrepareMoveToPlannedSlot() {
mm::motion.PlanMoveTo<mm::Selector>(SlotPosition(plannedSlot), 1000.0_S_mm_s); // @@TODO
}

View File

@ -48,3 +48,5 @@ extern Selector selector;
} // namespace selector
} // namespace modules
namespace ms = modules::selector;

View File

@ -32,3 +32,5 @@ extern Timebase timebase;
} // namespace time
} // namespace modules
namespace mt = modules::time;

View File

@ -42,3 +42,5 @@ extern UserInput userInput;
} // namespace user_input
} // namespace modules
namespace mui = modules::user_input;

View File

@ -19,14 +19,6 @@
using Catch::Matchers::Equals;
namespace mm = modules::motion;
namespace mf = modules::finda;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mb = modules::buttons;
namespace mg = modules::globals;
namespace ms = modules::selector;
#include "../helpers/helpers.ipp"
void CutSlot(uint8_t cutSlot) {

View File

@ -19,14 +19,6 @@
using Catch::Matchers::Equals;
namespace mm = modules::motion;
namespace mf = modules::finda;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mb = modules::buttons;
namespace mg = modules::globals;
namespace ms = modules::selector;
// temporarily disabled
TEST_CASE("eject_filament::eject0", "[eject_filament][.]") {
using namespace logic;
@ -41,8 +33,8 @@ TEST_CASE("eject_filament::eject0", "[eject_filament][.]") {
// it should have instructed the selector and idler to move to slot 1
// check if the idler and selector have the right command
CHECK(modules::motion::axes[modules::motion::Idler].targetPos == mi::Idler::SlotPosition(0).v);
CHECK(modules::motion::axes[modules::motion::Selector].targetPos == ms::Selector::SlotPosition(4).v);
CHECK(mm::axes[mm::Idler].targetPos == mi::Idler::SlotPosition(0).v);
CHECK(mm::axes[mm::Selector].targetPos == ms::Selector::SlotPosition(4).v);
// now cycle at most some number of cycles (to be determined yet) and then verify, that the idler and selector reached their target positions
REQUIRE(WhileTopState(ef, ProgressCode::SelectingFilamentSlot, 5000));

View File

@ -19,14 +19,6 @@
using Catch::Matchers::Equals;
namespace mm = modules::motion;
namespace mf = modules::finda;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mb = modules::buttons;
namespace mg = modules::globals;
namespace ms = modules::selector;
namespace ha = hal::adc;
TEST_CASE("feed_to_finda::feed_phase_unlimited", "[feed_to_finda]") {
@ -66,12 +58,12 @@ TEST_CASE("feed_to_finda::feed_phase_unlimited", "[feed_to_finda]") {
fb,
[&](int step) {
if( step == 1000 ){
modules::fsensor::fsensor.ProcessMessage(true);
mfs::fsensor.ProcessMessage(true);
}
return fb.State() == FeedToBondtech::PushingFilament; },
1500));
REQUIRE(modules::fsensor::fsensor.Pressed());
REQUIRE(mfs::fsensor.Pressed());
// // disengaging idler
// REQUIRE(fb.State() == FeedToBondtech::DisengagingIdler);

View File

@ -19,14 +19,6 @@
using Catch::Matchers::Equals;
namespace mm = modules::motion;
namespace mf = modules::finda;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mb = modules::buttons;
namespace mg = modules::globals;
namespace ms = modules::selector;
namespace ha = hal::adc;
TEST_CASE("feed_to_finda::feed_phase_unlimited", "[feed_to_finda]") {

View File

@ -19,14 +19,6 @@
using Catch::Matchers::Equals;
namespace mm = modules::motion;
namespace mf = modules::finda;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mb = modules::buttons;
namespace mg = modules::globals;
namespace ms = modules::selector;
#include "../helpers/helpers.ipp"
void LoadFilamentCommonSetup(uint8_t slot, logic::LoadFilament &lf) {
@ -73,7 +65,7 @@ void LoadFilamentSuccessful(uint8_t slot, logic::LoadFilament &lf) {
lf,
[&](int step) -> bool {
if(step == 100){ // on 100th step make fsensor trigger
modules::fsensor::fsensor.ProcessMessage(true);
mfs::fsensor.ProcessMessage(true);
}
return lf.TopLevelState() == ProgressCode::FeedingToBondtech; },
5000));

View File

@ -20,16 +20,16 @@
#include <stddef.h>
void main_loop() {
modules::buttons::buttons.Step();
modules::leds::leds.Step();
modules::finda::finda.Step();
modules::fsensor::fsensor.Step();
modules::idler::idler.Step();
modules::selector::selector.Step();
modules::motion::motion.Step();
modules::user_input::userInput.Step();
mb::buttons.Step();
ml::leds.Step();
mf::finda.Step();
mfs::fsensor.Step();
mi::idler.Step();
ms::selector.Step();
mm::motion.Step();
mui::userInput.Step();
modules::time::IncMillis();
mt::IncMillis();
}
void ForceReinitAllAutomata() {
@ -44,13 +44,13 @@ void ForceReinitAllAutomata() {
// As this approach might look like a standard and safer way of doing stuff, it has several drawbacks, especially
// it needs an explicit call to the Init function every time an object like this is created - this can have negative influence on firmware's code size
new (&modules::buttons::buttons) modules::buttons::Buttons();
new (&modules::leds::leds) modules::leds::LEDs();
new (&modules::finda::finda) modules::finda::FINDA();
new (&modules::fsensor::fsensor) modules::fsensor::FSensor();
new (&modules::idler::idler) modules::idler::Idler();
new (&modules::selector::selector) modules::selector::Selector();
new (&modules::motion::motion) modules::motion::Motion();
new (&mb::buttons) mb::Buttons();
new (&ml::leds) ml::LEDs();
new (&mf::finda) mf::FINDA();
new (&mfs::fsensor) mfs::FSensor();
new (&mi::idler) mi::Idler();
new (&ms::selector) ms::Selector();
new (&mm::motion) mm::Motion();
// no buttons involved ;)
hal::adc::ReinitADC(config::buttonsADCIndex, hal::adc::TADCData({ 1023 }), 1);
@ -59,23 +59,23 @@ void ForceReinitAllAutomata() {
hal::adc::ReinitADC(config::findaADCIndex, hal::adc::TADCData({ 0 }), 1);
// reinit timing
modules::time::ReinitTimebase();
mt::ReinitTimebase();
// reinit axes positions
modules::motion::ReinitMotion();
mm::ReinitMotion();
// let's assume we have the filament NOT loaded and active slot 0
modules::globals::globals.SetFilamentLoaded(false);
modules::globals::globals.SetActiveSlot(0);
mg::globals.SetFilamentLoaded(false);
mg::globals.SetActiveSlot(0);
}
void EnsureActiveSlotIndex(uint8_t slot) {
// move selector to the right spot
modules::selector::selector.MoveToSlot(slot);
while (modules::selector::selector.Slot() != slot)
ms::selector.MoveToSlot(slot);
while (ms::selector.Slot() != slot)
main_loop();
modules::globals::globals.SetActiveSlot(slot);
mg::globals.SetActiveSlot(slot);
}
void SetFINDAStateAndDebounce(bool press) {

View File

@ -2,8 +2,6 @@
#include "idler.h"
#include "stub_motion.h"
namespace mi = modules::idler;
namespace modules {
namespace motion {

View File

@ -19,14 +19,6 @@
using Catch::Matchers::Equals;
namespace mm = modules::motion;
namespace mf = modules::finda;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mb = modules::buttons;
namespace mg = modules::globals;
namespace ms = modules::selector;
void ToolChange(uint8_t fromSlot, uint8_t toSlot) {
ForceReinitAllAutomata();

View File

@ -19,14 +19,6 @@
using Catch::Matchers::Equals;
namespace mm = modules::motion;
namespace mf = modules::finda;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mb = modules::buttons;
namespace mg = modules::globals;
namespace ms = modules::selector;
#include "../helpers/helpers.ipp"
void RegularUnloadFromSlot04Init(uint8_t slot, logic::UnloadFilament &uf) {

View File

@ -19,14 +19,6 @@
using Catch::Matchers::Equals;
namespace mm = modules::motion;
namespace mf = modules::finda;
namespace mi = modules::idler;
namespace ml = modules::leds;
namespace mb = modules::buttons;
namespace mg = modules::globals;
namespace ms = modules::selector;
namespace ha = hal::adc;
TEST_CASE("unload_to_finda::regular_unload", "[unload_to_finda]") {

View File

@ -5,10 +5,10 @@
static constexpr const uint16_t adcMaxValue = 1023U;
bool Step_Basic_One_Button_Test(modules::buttons::Buttons &b, uint8_t oversampleFactor, uint8_t testedButtonIndex, uint8_t otherButton1, uint8_t otherButton2) {
bool Step_Basic_One_Button_Test(mb::Buttons &b, uint8_t oversampleFactor, uint8_t testedButtonIndex, uint8_t otherButton1, uint8_t otherButton2) {
for (uint8_t i = 0; i < oversampleFactor; ++i) {
b.Step(); // should detect the press but remain in detected state - wait for debounce
modules::time::IncMillis();
mt::IncMillis();
}
CHECK(!b.ButtonPressed(testedButtonIndex));
CHECK(!b.ButtonPressed(otherButton1));
@ -16,7 +16,7 @@ bool Step_Basic_One_Button_Test(modules::buttons::Buttons &b, uint8_t oversample
for (uint8_t i = 0; i < oversampleFactor; ++i) {
b.Step();
modules::time::IncMillis();
mt::IncMillis();
}
// just before the debounce trigger
@ -26,7 +26,7 @@ bool Step_Basic_One_Button_Test(modules::buttons::Buttons &b, uint8_t oversample
// Tune the alg to overcome an edge case in debouncing timing - just in the unit test
// This is very brittle, needs some work @TODO to clean up
modules::time::IncMillis(4);
mt::IncMillis(4);
b.Step(); // reset to waiting
CHECK(b.ButtonPressed(testedButtonIndex));
@ -35,7 +35,7 @@ bool Step_Basic_One_Button_Test(modules::buttons::Buttons &b, uint8_t oversample
for (uint8_t i = 0; i < oversampleFactor; ++i) {
b.Step(); // pressed again, still in debouncing state
modules::time::IncMillis();
mt::IncMillis();
}
CHECK(!b.ButtonPressed(testedButtonIndex));
CHECK(!b.ButtonPressed(otherButton1));
@ -46,8 +46,7 @@ bool Step_Basic_One_Button_Test(modules::buttons::Buttons &b, uint8_t oversample
/// This test verifies the behaviour of a single button. The other buttons must remain intact.
bool Step_Basic_One_Button(hal::adc::TADCData &&d, uint8_t testedButtonIndex) {
namespace mb = modules::buttons;
modules::time::ReinitTimebase();
mt::ReinitTimebase();
mb::Buttons b;
// need to oversample the data as debouncing takes 100 cycles to accept a pressed button
@ -82,7 +81,6 @@ TEST_CASE("buttons::Step-basic-button", "[buttons]") {
/// and the Buttons class should press first button and release, then the second one and then the third one
/// without being reinitialized.
TEST_CASE("buttons::Step-basic-button-one-after-other", "[buttons]") {
namespace mb = modules::buttons;
hal::adc::TADCData d({ config::buttonADCLimits[0][0], config::buttonADCLimits[0][0] + 1, adcMaxValue,
config::buttonADCLimits[1][0], config::buttonADCLimits[1][0] + 1, adcMaxValue,
config::buttonADCLimits[2][0], config::buttonADCLimits[2][0] + 1, adcMaxValue });
@ -99,22 +97,20 @@ TEST_CASE("buttons::Step-basic-button-one-after-other", "[buttons]") {
/// This test tries to simulate a bouncing effect on data from ADC on the first button
TEST_CASE("buttons::Step-debounce-one-button", "[buttons]") {
namespace mb = modules::buttons;
// make a bounce event on the first press
hal::adc::TADCData d({ 5, adcMaxValue, 5, 9, 6, 7, 8, adcMaxValue, adcMaxValue });
// need to oversample the data as debouncing takes 100 cycles to accept a pressed button
constexpr uint8_t oversampleFactor = 25;
hal::adc::ReinitADC(config::buttonsADCIndex, std::move(d), oversampleFactor);
modules::time::ReinitTimebase();
mt::ReinitTimebase();
mb::Buttons b;
// 5
for (uint8_t i = 0; i < oversampleFactor; ++i) {
b.Step(); // should detect the press but remain in detected state - wait for debounce
modules::time::IncMillis();
mt::IncMillis();
}
CHECK(!b.ButtonPressed(0));
CHECK(!b.ButtonPressed(1));
@ -123,7 +119,7 @@ TEST_CASE("buttons::Step-debounce-one-button", "[buttons]") {
// 1023
for (uint8_t i = 0; i < oversampleFactor; ++i) {
b.Step(); // reset to waiting
modules::time::IncMillis();
mt::IncMillis();
}
CHECK(!b.ButtonPressed(0));
CHECK(!b.ButtonPressed(1));
@ -132,7 +128,7 @@ TEST_CASE("buttons::Step-debounce-one-button", "[buttons]") {
// 5
for (uint8_t i = 0; i < oversampleFactor; ++i) {
b.Step(); // pressed again, still in debouncing state
modules::time::IncMillis();
mt::IncMillis();
}
CHECK(!b.ButtonPressed(0));
CHECK(!b.ButtonPressed(1));
@ -141,7 +137,7 @@ TEST_CASE("buttons::Step-debounce-one-button", "[buttons]") {
// 9
for (uint8_t i = 0; i < oversampleFactor; ++i) {
b.Step(); // no change
modules::time::IncMillis();
mt::IncMillis();
}
CHECK(!b.ButtonPressed(0));
CHECK(!b.ButtonPressed(1));
@ -150,7 +146,7 @@ TEST_CASE("buttons::Step-debounce-one-button", "[buttons]") {
// 6
for (uint8_t i = 0; i < oversampleFactor; ++i) {
b.Step(); // no change
modules::time::IncMillis();
mt::IncMillis();
}
CHECK(!b.ButtonPressed(0));
CHECK(!b.ButtonPressed(1));
@ -159,7 +155,7 @@ TEST_CASE("buttons::Step-debounce-one-button", "[buttons]") {
// 7
for (uint8_t i = 0; i < oversampleFactor; ++i) {
b.Step(); // one step from "pressed"
modules::time::IncMillis();
mt::IncMillis();
}
CHECK(!b.ButtonPressed(0));
CHECK(!b.ButtonPressed(1));
@ -168,7 +164,7 @@ TEST_CASE("buttons::Step-debounce-one-button", "[buttons]") {
// 8
for (uint8_t i = 0; i < oversampleFactor; ++i) {
b.Step(); // fifth set of samples - should report "pressed" finally
modules::time::IncMillis();
mt::IncMillis();
}
CHECK(b.ButtonPressed(0));
CHECK(!b.ButtonPressed(1));
@ -177,7 +173,7 @@ TEST_CASE("buttons::Step-debounce-one-button", "[buttons]") {
// 1023
for (uint8_t i = 0; i < oversampleFactor; ++i) {
b.Step(); // sixth set of samples - button released (no debouncing on release)
modules::time::IncMillis();
mt::IncMillis();
}
CHECK(!b.ButtonPressed(0));
CHECK(!b.ButtonPressed(1));
@ -186,7 +182,7 @@ TEST_CASE("buttons::Step-debounce-one-button", "[buttons]") {
// 1023
for (uint8_t i = 0; i < oversampleFactor; ++i) {
b.Step(); // seventh set of samples - still released
modules::time::IncMillis();
mt::IncMillis();
}
CHECK(!b.ButtonPressed(0));
CHECK(!b.ButtonPressed(1));

View File

@ -26,53 +26,51 @@ extern uint16_t shr16_v_copy;
#define SHR16_LEDR4 0x0080
TEST_CASE("leds::single", "[leds]") {
using namespace modules::leds;
using namespace hal::shr16;
modules::time::ReinitTimebase(); // reset timing
mt::ReinitTimebase(); // reset timing
LEDs leds;
ml::LEDs leds;
uint8_t index;
Color color;
ml::Color color;
uint16_t shr16_register;
std::tie(index, color, shr16_register) = GENERATE(
std::make_tuple(0, green, SHR16_LEDG0),
std::make_tuple(0, red, SHR16_LEDR0),
std::make_tuple(1, green, SHR16_LEDG1),
std::make_tuple(1, red, SHR16_LEDR1),
std::make_tuple(2, green, SHR16_LEDG2),
std::make_tuple(2, red, SHR16_LEDR2),
std::make_tuple(3, green, SHR16_LEDG3),
std::make_tuple(3, red, SHR16_LEDR3),
std::make_tuple(4, green, SHR16_LEDG4),
std::make_tuple(4, red, SHR16_LEDR4));
std::make_tuple(0, ml::green, SHR16_LEDG0),
std::make_tuple(0, ml::red, SHR16_LEDR0),
std::make_tuple(1, ml::green, SHR16_LEDG1),
std::make_tuple(1, ml::red, SHR16_LEDR1),
std::make_tuple(2, ml::green, SHR16_LEDG2),
std::make_tuple(2, ml::red, SHR16_LEDR2),
std::make_tuple(3, ml::green, SHR16_LEDG3),
std::make_tuple(3, ml::red, SHR16_LEDR3),
std::make_tuple(4, ml::green, SHR16_LEDG4),
std::make_tuple(4, ml::red, SHR16_LEDR4));
shr16.Init(); // clears the register for the test
// turn LED on
leds.SetMode(index, color, on);
leds.SetMode(index, color, ml::on);
leds.Step();
modules::time::IncMillis();
mt::IncMillis();
CHECK(leds.LedOn(index, color) == true);
CHECK(shr16_v_copy == shr16_register);
// turn LED off
leds.SetMode(index, color, off);
leds.SetMode(index, color, ml::off);
leds.Step();
modules::time::IncMillis();
mt::IncMillis();
CHECK(leds.LedOn(index, color) == false);
CHECK(shr16_v_copy == 0);
}
void TestBlink(uint8_t index, modules::leds::Color color, uint16_t shr16_register, bool shouldBeOn, modules::leds::Mode blinkMode) {
using namespace modules::leds;
void TestBlink(uint8_t index, ml::Color color, uint16_t shr16_register, bool shouldBeOn, ml::Mode blinkMode) {
using namespace hal::shr16;
modules::time::ReinitTimebase(); // reset timing
LEDs leds;
mt::ReinitTimebase(); // reset timing
ml::LEDs leds;
leds.SetMode(index, color, blinkMode);
leds.Step();
modules::time::IncMillis();
mt::IncMillis();
REQUIRE(leds.LedOn(index, color) == shouldBeOn);
CHECK(shr16_v_copy == (shouldBeOn ? shr16_register : 0));
@ -80,7 +78,7 @@ void TestBlink(uint8_t index, modules::leds::Color color, uint16_t shr16_registe
// test 4 seconds of blinking
for (uint8_t s = 1; s < 4; ++s) {
// one second elapsed ;)
modules::time::IncMillis(1000);
mt::IncMillis(1000);
leds.Step();
shouldBeOn = !shouldBeOn;
CHECK(leds.LedOn(index, color) == shouldBeOn);
@ -88,35 +86,34 @@ void TestBlink(uint8_t index, modules::leds::Color color, uint16_t shr16_registe
}
// turn LED off
leds.SetMode(index, color, off);
leds.SetMode(index, color, ml::off);
leds.Step();
modules::time::IncMillis();
mt::IncMillis();
CHECK(leds.LedOn(index, color) == false);
CHECK(shr16_v_copy == 0);
}
TEST_CASE("leds::blink0-single", "[leds]") {
using namespace modules::leds;
using namespace hal::shr16;
uint8_t index;
Color color;
ml::Color color;
uint16_t shr16_register;
std::tie(index, color, shr16_register) = GENERATE(
std::make_tuple(0, green, SHR16_LEDG0),
std::make_tuple(0, red, SHR16_LEDR0),
std::make_tuple(1, green, SHR16_LEDG1),
std::make_tuple(1, red, SHR16_LEDR1),
std::make_tuple(2, green, SHR16_LEDG2),
std::make_tuple(2, red, SHR16_LEDR2),
std::make_tuple(3, green, SHR16_LEDG3),
std::make_tuple(3, red, SHR16_LEDR3),
std::make_tuple(4, green, SHR16_LEDG4),
std::make_tuple(4, red, SHR16_LEDR4));
std::make_tuple(0, ml::green, SHR16_LEDG0),
std::make_tuple(0, ml::red, SHR16_LEDR0),
std::make_tuple(1, ml::green, SHR16_LEDG1),
std::make_tuple(1, ml::red, SHR16_LEDR1),
std::make_tuple(2, ml::green, SHR16_LEDG2),
std::make_tuple(2, ml::red, SHR16_LEDR2),
std::make_tuple(3, ml::green, SHR16_LEDG3),
std::make_tuple(3, ml::red, SHR16_LEDR3),
std::make_tuple(4, ml::green, SHR16_LEDG4),
std::make_tuple(4, ml::red, SHR16_LEDR4));
shr16.Init(); // clears the register for the test
// set LED into blink0 mode - on in even periods of 1s intervals, starts as OFF
TestBlink(index, color, shr16_register, false, blink0);
TestBlink(index, color, shr16_register, false, ml::blink0);
TestBlink(index, color, shr16_register, true, blink1);
TestBlink(index, color, shr16_register, true, ml::blink1);
}

View File

@ -4,85 +4,81 @@
using Catch::Matchers::Equals;
TEST_CASE("protocol::EncodeRequests", "[protocol]") {
using namespace modules::protocol;
RequestMsgCodes code;
mp::RequestMsgCodes code;
uint8_t value;
std::tie(code, value) = GENERATE(
std::make_tuple(RequestMsgCodes::Button, 0),
std::make_tuple(RequestMsgCodes::Button, 1),
std::make_tuple(RequestMsgCodes::Button, 2),
std::make_tuple(RequestMsgCodes::Cut, 0),
std::make_tuple(RequestMsgCodes::Eject, 0),
std::make_tuple(RequestMsgCodes::Finda, 0),
std::make_tuple(RequestMsgCodes::Load, 0),
std::make_tuple(RequestMsgCodes::Load, 1),
std::make_tuple(RequestMsgCodes::Load, 2),
std::make_tuple(RequestMsgCodes::Load, 3),
std::make_tuple(RequestMsgCodes::Load, 4),
std::make_tuple(RequestMsgCodes::Mode, 0),
std::make_tuple(RequestMsgCodes::Mode, 1),
std::make_tuple(RequestMsgCodes::Query, 0),
std::make_tuple(RequestMsgCodes::Reset, 0),
std::make_tuple(RequestMsgCodes::Tool, 0),
std::make_tuple(RequestMsgCodes::Tool, 1),
std::make_tuple(RequestMsgCodes::Tool, 2),
std::make_tuple(RequestMsgCodes::Tool, 3),
std::make_tuple(RequestMsgCodes::Tool, 4),
std::make_tuple(RequestMsgCodes::Unload, 0),
std::make_tuple(RequestMsgCodes::Version, 0),
std::make_tuple(RequestMsgCodes::Version, 1),
std::make_tuple(RequestMsgCodes::Version, 2),
std::make_tuple(RequestMsgCodes::Wait, 0),
std::make_tuple(RequestMsgCodes::unknown, 0));
std::make_tuple(mp::RequestMsgCodes::Button, 0),
std::make_tuple(mp::RequestMsgCodes::Button, 1),
std::make_tuple(mp::RequestMsgCodes::Button, 2),
std::make_tuple(mp::RequestMsgCodes::Cut, 0),
std::make_tuple(mp::RequestMsgCodes::Eject, 0),
std::make_tuple(mp::RequestMsgCodes::Finda, 0),
std::make_tuple(mp::RequestMsgCodes::Load, 0),
std::make_tuple(mp::RequestMsgCodes::Load, 1),
std::make_tuple(mp::RequestMsgCodes::Load, 2),
std::make_tuple(mp::RequestMsgCodes::Load, 3),
std::make_tuple(mp::RequestMsgCodes::Load, 4),
std::make_tuple(mp::RequestMsgCodes::Mode, 0),
std::make_tuple(mp::RequestMsgCodes::Mode, 1),
std::make_tuple(mp::RequestMsgCodes::Query, 0),
std::make_tuple(mp::RequestMsgCodes::Reset, 0),
std::make_tuple(mp::RequestMsgCodes::Tool, 0),
std::make_tuple(mp::RequestMsgCodes::Tool, 1),
std::make_tuple(mp::RequestMsgCodes::Tool, 2),
std::make_tuple(mp::RequestMsgCodes::Tool, 3),
std::make_tuple(mp::RequestMsgCodes::Tool, 4),
std::make_tuple(mp::RequestMsgCodes::Unload, 0),
std::make_tuple(mp::RequestMsgCodes::Version, 0),
std::make_tuple(mp::RequestMsgCodes::Version, 1),
std::make_tuple(mp::RequestMsgCodes::Version, 2),
std::make_tuple(mp::RequestMsgCodes::Wait, 0),
std::make_tuple(mp::RequestMsgCodes::unknown, 0));
std::array<uint8_t, 3> txbuff;
CHECK(Protocol::EncodeRequest(RequestMsg(code, value), txbuff.data()) == 3);
CHECK(mp::Protocol::EncodeRequest(mp::RequestMsg(code, value), txbuff.data()) == 3);
CHECK(txbuff[0] == (uint8_t)code);
CHECK(txbuff[1] == value + '0');
CHECK(txbuff[2] == '\n');
}
TEST_CASE("protocol::EncodeResponseCmdAR", "[protocol]") {
using namespace modules::protocol;
auto requestMsg = GENERATE(
RequestMsg(RequestMsgCodes::Button, 0),
RequestMsg(RequestMsgCodes::Button, 1),
RequestMsg(RequestMsgCodes::Button, 2),
mp::RequestMsg(mp::RequestMsgCodes::Button, 0),
mp::RequestMsg(mp::RequestMsgCodes::Button, 1),
mp::RequestMsg(mp::RequestMsgCodes::Button, 2),
RequestMsg(RequestMsgCodes::Cut, 0),
mp::RequestMsg(mp::RequestMsgCodes::Cut, 0),
RequestMsg(RequestMsgCodes::Eject, 0),
RequestMsg(RequestMsgCodes::Eject, 1),
RequestMsg(RequestMsgCodes::Eject, 2),
RequestMsg(RequestMsgCodes::Eject, 3),
RequestMsg(RequestMsgCodes::Eject, 4),
mp::RequestMsg(mp::RequestMsgCodes::Eject, 0),
mp::RequestMsg(mp::RequestMsgCodes::Eject, 1),
mp::RequestMsg(mp::RequestMsgCodes::Eject, 2),
mp::RequestMsg(mp::RequestMsgCodes::Eject, 3),
mp::RequestMsg(mp::RequestMsgCodes::Eject, 4),
RequestMsg(RequestMsgCodes::Load, 0),
RequestMsg(RequestMsgCodes::Load, 1),
RequestMsg(RequestMsgCodes::Load, 2),
RequestMsg(RequestMsgCodes::Load, 3),
RequestMsg(RequestMsgCodes::Load, 4),
mp::RequestMsg(mp::RequestMsgCodes::Load, 0),
mp::RequestMsg(mp::RequestMsgCodes::Load, 1),
mp::RequestMsg(mp::RequestMsgCodes::Load, 2),
mp::RequestMsg(mp::RequestMsgCodes::Load, 3),
mp::RequestMsg(mp::RequestMsgCodes::Load, 4),
RequestMsg(RequestMsgCodes::Mode, 0),
RequestMsg(RequestMsgCodes::Mode, 1),
mp::RequestMsg(mp::RequestMsgCodes::Mode, 0),
mp::RequestMsg(mp::RequestMsgCodes::Mode, 1),
RequestMsg(RequestMsgCodes::Tool, 0),
RequestMsg(RequestMsgCodes::Tool, 1),
RequestMsg(RequestMsgCodes::Tool, 2),
RequestMsg(RequestMsgCodes::Tool, 3),
RequestMsg(RequestMsgCodes::Tool, 4),
mp::RequestMsg(mp::RequestMsgCodes::Tool, 0),
mp::RequestMsg(mp::RequestMsgCodes::Tool, 1),
mp::RequestMsg(mp::RequestMsgCodes::Tool, 2),
mp::RequestMsg(mp::RequestMsgCodes::Tool, 3),
mp::RequestMsg(mp::RequestMsgCodes::Tool, 4),
RequestMsg(RequestMsgCodes::Unload, 0),
mp::RequestMsg(mp::RequestMsgCodes::Unload, 0),
RequestMsg(RequestMsgCodes::Wait, 0));
mp::RequestMsg(mp::RequestMsgCodes::Wait, 0));
auto responseStatus = GENERATE(ResponseMsgParamCodes::Accepted, ResponseMsgParamCodes::Rejected);
auto responseStatus = GENERATE(mp::ResponseMsgParamCodes::Accepted, mp::ResponseMsgParamCodes::Rejected);
std::array<uint8_t, 8> txbuff;
uint8_t msglen = Protocol::EncodeResponseCmdAR(requestMsg, responseStatus, txbuff.data());
uint8_t msglen = mp::Protocol::EncodeResponseCmdAR(requestMsg, responseStatus, txbuff.data());
CHECK(msglen == 5);
CHECK(txbuff[0] == (uint8_t)requestMsg.code);
@ -93,39 +89,36 @@ TEST_CASE("protocol::EncodeResponseCmdAR", "[protocol]") {
}
TEST_CASE("protocol::EncodeResponseReadFINDA", "[protocol]") {
using namespace modules::protocol;
auto requestMsg = RequestMsg(RequestMsgCodes::Finda, 0);
auto requestMsg = mp::RequestMsg(mp::RequestMsgCodes::Finda, 0);
uint8_t findaStatus = GENERATE(0, 1);
std::array<uint8_t, 8> txbuff;
uint8_t msglen = Protocol::EncodeResponseReadFINDA(requestMsg, findaStatus, txbuff.data());
uint8_t msglen = mp::Protocol::EncodeResponseReadFINDA(requestMsg, findaStatus, txbuff.data());
CHECK(msglen == 6);
CHECK(txbuff[0] == (uint8_t)requestMsg.code);
CHECK(txbuff[1] == requestMsg.value + '0');
CHECK(txbuff[2] == ' ');
CHECK(txbuff[3] == (uint8_t)ResponseMsgParamCodes::Accepted);
CHECK(txbuff[3] == (uint8_t)mp::ResponseMsgParamCodes::Accepted);
CHECK(txbuff[4] == findaStatus + '0');
CHECK(txbuff[5] == '\n');
}
TEST_CASE("protocol::EncodeResponseVersion", "[protocol]") {
using namespace modules::protocol;
std::uint8_t versionQueryType = GENERATE(0, 1, 2, 3);
auto requestMsg = RequestMsg(RequestMsgCodes::Version, versionQueryType);
auto requestMsg = mp::RequestMsg(mp::RequestMsgCodes::Version, versionQueryType);
auto version = GENERATE(0, 1, 2, 3, 4, 10, 11, 12, 20, 99, 100, 101, 255);
std::array<uint8_t, 8> txbuff;
uint8_t msglen = Protocol::EncodeResponseVersion(requestMsg, version, txbuff.data());
uint8_t msglen = mp::Protocol::EncodeResponseVersion(requestMsg, version, txbuff.data());
CHECK(msglen <= 8);
CHECK(txbuff[0] == (uint8_t)requestMsg.code);
CHECK(txbuff[1] == requestMsg.value + '0');
CHECK(txbuff[2] == ' ');
CHECK(txbuff[3] == (uint8_t)ResponseMsgParamCodes::Accepted);
CHECK(txbuff[3] == (uint8_t)mp::ResponseMsgParamCodes::Accepted);
if (version < 10) {
CHECK(txbuff[4] == version + '0');
@ -142,39 +135,37 @@ TEST_CASE("protocol::EncodeResponseVersion", "[protocol]") {
}
TEST_CASE("protocol::EncodeResponseQueryOperation", "[protocol]") {
using namespace modules::protocol;
auto requestMsg = GENERATE(
RequestMsg(RequestMsgCodes::Cut, 0),
mp::RequestMsg(mp::RequestMsgCodes::Cut, 0),
RequestMsg(RequestMsgCodes::Eject, 0),
RequestMsg(RequestMsgCodes::Eject, 1),
RequestMsg(RequestMsgCodes::Eject, 2),
RequestMsg(RequestMsgCodes::Eject, 3),
RequestMsg(RequestMsgCodes::Eject, 4),
mp::RequestMsg(mp::RequestMsgCodes::Eject, 0),
mp::RequestMsg(mp::RequestMsgCodes::Eject, 1),
mp::RequestMsg(mp::RequestMsgCodes::Eject, 2),
mp::RequestMsg(mp::RequestMsgCodes::Eject, 3),
mp::RequestMsg(mp::RequestMsgCodes::Eject, 4),
RequestMsg(RequestMsgCodes::Load, 0),
RequestMsg(RequestMsgCodes::Load, 1),
RequestMsg(RequestMsgCodes::Load, 2),
RequestMsg(RequestMsgCodes::Load, 3),
RequestMsg(RequestMsgCodes::Load, 4),
mp::RequestMsg(mp::RequestMsgCodes::Load, 0),
mp::RequestMsg(mp::RequestMsgCodes::Load, 1),
mp::RequestMsg(mp::RequestMsgCodes::Load, 2),
mp::RequestMsg(mp::RequestMsgCodes::Load, 3),
mp::RequestMsg(mp::RequestMsgCodes::Load, 4),
RequestMsg(RequestMsgCodes::Tool, 0),
RequestMsg(RequestMsgCodes::Tool, 1),
RequestMsg(RequestMsgCodes::Tool, 2),
RequestMsg(RequestMsgCodes::Tool, 3),
RequestMsg(RequestMsgCodes::Tool, 4),
mp::RequestMsg(mp::RequestMsgCodes::Tool, 0),
mp::RequestMsg(mp::RequestMsgCodes::Tool, 1),
mp::RequestMsg(mp::RequestMsgCodes::Tool, 2),
mp::RequestMsg(mp::RequestMsgCodes::Tool, 3),
mp::RequestMsg(mp::RequestMsgCodes::Tool, 4),
RequestMsg(RequestMsgCodes::Unload, 0),
mp::RequestMsg(mp::RequestMsgCodes::Unload, 0),
RequestMsg(RequestMsgCodes::Wait, 0));
mp::RequestMsg(mp::RequestMsgCodes::Wait, 0));
auto responseStatus = GENERATE(ResponseMsgParamCodes::Processing, ResponseMsgParamCodes::Error, ResponseMsgParamCodes::Finished);
auto responseStatus = GENERATE(mp::ResponseMsgParamCodes::Processing, mp::ResponseMsgParamCodes::Error, mp::ResponseMsgParamCodes::Finished);
auto value = GENERATE(0, 1, 2, 3, 10, 11, 99, 100, 101, 102, 200, 255);
std::array<uint8_t, 8> txbuff;
uint8_t msglen = Protocol::EncodeResponseQueryOperation(requestMsg, responseStatus, value, txbuff.data());
uint8_t msglen = mp::Protocol::EncodeResponseQueryOperation(requestMsg, responseStatus, value, txbuff.data());
CHECK(msglen <= 8);
CHECK(txbuff[0] == (uint8_t)requestMsg.code);
@ -182,7 +173,7 @@ TEST_CASE("protocol::EncodeResponseQueryOperation", "[protocol]") {
CHECK(txbuff[2] == ' ');
CHECK(txbuff[3] == (uint8_t)responseStatus);
if (responseStatus == ResponseMsgParamCodes::Finished) {
if (responseStatus == mp::ResponseMsgParamCodes::Finished) {
CHECK(txbuff[4] == '\n');
CHECK(msglen == 5);
} else {
@ -202,8 +193,7 @@ TEST_CASE("protocol::EncodeResponseQueryOperation", "[protocol]") {
}
TEST_CASE("protocol::DecodeRequest", "[protocol]") {
using namespace modules::protocol;
Protocol p;
mp::Protocol p;
const char *rxbuff = GENERATE(
"B0\n", "B1\n", "B2\n",
"E0\n", "E1\n", "E2\n", "E3\n", "E4\n",
@ -226,21 +216,20 @@ TEST_CASE("protocol::DecodeRequest", "[protocol]") {
break;
} else if (c == '\n') {
// regular end of message line
CHECK(p.DecodeRequest(c) == DecodeStatus::MessageCompleted);
CHECK(p.DecodeRequest(c) == mp::DecodeStatus::MessageCompleted);
} else {
CHECK(p.DecodeRequest(c) == DecodeStatus::NeedMoreData);
CHECK(p.DecodeRequest(c) == mp::DecodeStatus::NeedMoreData);
}
}
// check the message type
const RequestMsg &rq = p.GetRequestMsg();
const mp::RequestMsg &rq = p.GetRequestMsg();
CHECK((uint8_t)rq.code == rxbuff[0]);
CHECK(rq.value == rxbuff[1] - '0');
}
TEST_CASE("protocol::DecodeResponseReadFinda", "[protocol]") {
using namespace modules::protocol;
Protocol p;
mp::Protocol p;
const char *rxbuff = GENERATE(
"P0 A0\n",
"P0 A1\n");
@ -253,14 +242,14 @@ TEST_CASE("protocol::DecodeResponseReadFinda", "[protocol]") {
break;
} else if (c == '\n') {
// regular end of message line
CHECK(p.DecodeResponse(c) == DecodeStatus::MessageCompleted);
CHECK(p.DecodeResponse(c) == mp::DecodeStatus::MessageCompleted);
} else {
CHECK(p.DecodeResponse(c) == DecodeStatus::NeedMoreData);
CHECK(p.DecodeResponse(c) == mp::DecodeStatus::NeedMoreData);
}
}
// check the message type
const ResponseMsg &rsp = p.GetResponseMsg();
const mp::ResponseMsg &rsp = p.GetResponseMsg();
CHECK((uint8_t)rsp.request.code == rxbuff[0]);
CHECK(rsp.request.value == rxbuff[1] - '0');
CHECK((uint8_t)rsp.paramCode == rxbuff[3]);
@ -268,8 +257,7 @@ TEST_CASE("protocol::DecodeResponseReadFinda", "[protocol]") {
}
TEST_CASE("protocol::DecodeResponseQueryOperation", "[protocol]") {
using namespace modules::protocol;
Protocol p;
mp::Protocol p;
const char *cmdReference = GENERATE(
"E0", "E1", "E2", "E3", "E4",
"K0",
@ -294,99 +282,96 @@ TEST_CASE("protocol::DecodeResponseQueryOperation", "[protocol]") {
break;
} else if (c == '\n') {
// regular end of message line
CHECK(p.DecodeResponse(c) == DecodeStatus::MessageCompleted);
CHECK(p.DecodeResponse(c) == mp::DecodeStatus::MessageCompleted);
} else {
CHECK(p.DecodeResponse(c) == DecodeStatus::NeedMoreData);
CHECK(p.DecodeResponse(c) == mp::DecodeStatus::NeedMoreData);
}
}
// check the message type
const ResponseMsg &rsp = p.GetResponseMsg();
const mp::ResponseMsg &rsp = p.GetResponseMsg();
CHECK((uint8_t)rsp.request.code == rxbuff[0]);
CHECK(rsp.request.value == rxbuff[1] - '0');
CHECK((uint8_t)rsp.paramCode == rxbuff[3]);
if ((uint8_t)rsp.paramCode != (uint8_t)ResponseMsgParamCodes::Finished) {
if ((uint8_t)rsp.paramCode != (uint8_t)mp::ResponseMsgParamCodes::Finished) {
CHECK((uint8_t)rsp.paramValue == rxbuff[4] - '0');
}
}
TEST_CASE("protocol::DecodeRequestErrors", "[protocol]") {
using namespace modules::protocol;
Protocol p;
mp::Protocol p;
const char b0[] = "b0";
CHECK(p.DecodeRequest(b0[0]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b0[1]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b0[0]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b0[1]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
// reset protokol decoder
CHECK(p.DecodeRequest('\n') == DecodeStatus::MessageCompleted);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest('\n') == mp::DecodeStatus::MessageCompleted);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
const char B1_[] = "B1 \n";
CHECK(p.DecodeRequest(B1_[0]) == DecodeStatus::NeedMoreData);
CHECK(p.DecodeRequest(B1_[1]) == DecodeStatus::NeedMoreData);
CHECK(p.DecodeRequest(B1_[2]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(B1_[3]) == DecodeStatus::MessageCompleted);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(B1_[0]) == mp::DecodeStatus::NeedMoreData);
CHECK(p.DecodeRequest(B1_[1]) == mp::DecodeStatus::NeedMoreData);
CHECK(p.DecodeRequest(B1_[2]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(B1_[3]) == mp::DecodeStatus::MessageCompleted);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
const char _B2[] = " B2\n";
CHECK(p.DecodeRequest(_B2[0]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B2[1]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B2[2]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B2[3]) == DecodeStatus::MessageCompleted);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B2[0]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B2[1]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B2[2]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B2[3]) == mp::DecodeStatus::MessageCompleted);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
const char _B0_[] = " B0 ";
CHECK(p.DecodeRequest(_B0_[0]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B0_[1]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B0_[2]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B0_[3]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest('\n') == DecodeStatus::MessageCompleted);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B0_[0]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B0_[1]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B0_[2]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(_B0_[3]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest('\n') == mp::DecodeStatus::MessageCompleted);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
}
TEST_CASE("protocol::DecodeResponseErrors", "[protocol]") {
using namespace modules::protocol;
Protocol p;
mp::Protocol p;
const char b0[] = "b0 A\n";
CHECK(p.DecodeRequest(b0[0]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b0[1]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b0[2]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b0[3]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b0[4]) == DecodeStatus::MessageCompleted);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b0[0]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b0[1]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b0[2]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b0[3]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b0[4]) == mp::DecodeStatus::MessageCompleted);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
const char b1[] = "b0A\n";
CHECK(p.DecodeRequest(b1[0]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b1[1]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b1[2]) == DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b1[3]) == DecodeStatus::MessageCompleted);
CHECK(p.GetRequestMsg().code == RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b1[0]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b1[1]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b1[2]) == mp::DecodeStatus::Error);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
CHECK(p.DecodeRequest(b1[3]) == mp::DecodeStatus::MessageCompleted);
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
}
// Beware - this test makes 18M+ combinations, run only when changing the implementation of the codec
// Therefore it is disabled [.] by default
TEST_CASE("protocol::DecodeResponseErrorsCross", "[protocol][.]") {
using namespace modules::protocol;
Protocol p;
mp::Protocol p;
const char *validInitialSpaces = "";
const char *invalidInitialSpaces = GENERATE(" ", " ");
@ -434,12 +419,12 @@ TEST_CASE("protocol::DecodeResponseErrorsCross", "[protocol][.]") {
bool shouldPass = viInitialSpace && viReqCode && /*viReqValue && */ viSpace && viRspCode && viTerminatingSpaces;
bool failed = false;
std::for_each(msg.cbegin(), msg.cend(), [&](uint8_t c) {
if (p.DecodeResponse(c) == DecodeStatus::Error) {
if (p.DecodeResponse(c) == mp::DecodeStatus::Error) {
failed = true;
}
});
CHECK(failed != shouldPass); // it must have failed!
if (failed) {
CHECK(p.GetResponseMsg().paramCode == ResponseMsgParamCodes::unknown);
CHECK(p.GetResponseMsg().paramCode == mp::ResponseMsgParamCodes::unknown);
}
}

View File

@ -11,3 +11,5 @@ extern void IncMillis(uint16_t inc = 1);
} // namespace time
} // namespace modules
namespace mt = modules::time;