Optimized kiss state machine

pull/15/head
sh123 2021-02-06 13:45:24 +02:00
rodzic 693698e165
commit 3cd67e07d2
2 zmienionych plików z 37 dodań i 40 usunięć

Wyświetl plik

@ -3,8 +3,7 @@
namespace Kiss {
Processor::Processor()
: cmd_(Cmd::NoCmd)
, state_(State::Void)
: state_(State::GetStart)
, txQueue_(new cppQueue(sizeof(unsigned char), CfgTxQueueSize))
{
}
@ -55,37 +54,34 @@ void Processor::serialProcessRx()
}
}
void Processor::resetState()
{
cmd_ = Cmd::NoCmd;
state_ = State::Void;
}
bool Processor::processCommand(byte rxByte) {
switch (rxByte) {
case Cmd::Data:
if (!onRigTxBegin()) return false;
if (!onRigTxBegin()) {
return false;
}
state_ = State::GetData;
dataType_ = DataType::Raw;
break;
case Cmd::RadioControl:
state_ = State::GetData;
dataType_ = DataType::Control;
cmdBuffer_.clear();
break;
case Cmd::P:
state_ = State::GetP;
break;
case Cmd::SlotTime:
state_ = State::GetSlotTime;
break;
case Cmd::RadioControl:
state_ = State::GetData;
dataType_ = DataType::Control;
cmdBuffer_.clear();
case Marker::Fend:
break;
default:
// unknown command
resetState();
state_ = State::GetEnd;
return true;
}
cmd_ = (Cmd)rxByte;
return true;
}
@ -95,23 +91,19 @@ void Processor::processData(byte rxByte) {
state_ = State::Escape;
break;
case Marker::Fend:
if (cmd_ == Cmd::Data) {
if (dataType_ == DataType::Raw) {
onRigTxEnd();
} else if (dataType_ == DataType::Control) {
onRadioControlCommand(cmdBuffer_);
}
}
resetState();
state_ = State::GetStart;
break;
default:
if (cmd_ == Cmd::Data) {
if (dataType_ == DataType::Raw) {
onRigTx(rxByte);
} else if (dataType_ == DataType::Control) {
cmdBuffer_.push_back(rxByte);
}
}
break;
}
}
@ -119,24 +111,28 @@ void Processor::processData(byte rxByte) {
bool Processor::receiveByte(byte rxByte) {
switch (state_) {
case State::Void:
case State::GetStart:
if (rxByte == Marker::Fend) {
cmd_ = Cmd::NoCmd;
state_ = State::GetCmd;
}
break;
case State::GetEnd:
if (rxByte == Marker::Fend) {
state_ = State::GetStart;
}
break;
case State::GetCmd:
if (rxByte != Marker::Fend) {
if (!processCommand(rxByte)) return false;
if (!processCommand(rxByte)) {
return false;
}
break;
case State::GetP:
onControlCommand(Cmd::P, rxByte);
state_ = State::GetData;
state_ = State::GetEnd;
break;
case State::GetSlotTime:
onControlCommand(Cmd::SlotTime, rxByte);
state_ = State::GetData;
state_ = State::GetEnd;
break;
case State::GetData:
processData(rxByte);
@ -150,11 +146,13 @@ bool Processor::receiveByte(byte rxByte) {
onRigTx((byte)Marker::Fesc);
state_ = State::GetData;
}
else {
resetState();
else if (rxByte != Marker::Fend) {
state_ = State::GetEnd;
}
break;
default:
// unknown state
state_ = State::GetStart;
break;
}
return true;

Wyświetl plik

@ -24,7 +24,8 @@ protected:
};
enum State {
Void = 0,
GetStart = 0,
GetEnd,
GetCmd,
GetData,
GetP,
@ -72,12 +73,10 @@ private:
bool receiveByte(byte rxByte);
void processData(byte rxByte);
bool processCommand(byte rxByte);
void resetState();
private:
Cmd cmd_;
DataType dataType_;
State state_;
DataType dataType_;
std::shared_ptr<cppQueue> txQueue_;
std::vector<byte> cmdBuffer_;
};