Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fiz Meat Pack with Multi Serial and NO_TIMEOUT #21301

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions Marlin/src/feature/meatpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,16 @@ struct MeatpackSerial : public SerialBase <MeatpackSerial < SerialT >> {

SerialT & out;

char serialBuffer[2];
uint8_t charCount;
uint8_t readIndex;
struct serial_state_t {
char buffer[2];
uint8_t charCount;
uint8_t readIndex;
} serial_state[NUM_SERIAL];

void resetState() { LOOP_L_N(p, NUM_SERIAL) { serial_state[p].readIndex = 0; serial_state[p].charCount = 0; } }
NO_INLINE size_t write(uint8_t c) { return out.write(c); }
void flush() { out.flush(); }
void begin(long br) { out.begin(br); readIndex = 0; }
void flush() { out.flush(); resetState(); }
void begin(long br) { out.begin(br); resetState(); }
void end() { out.end(); }

void msgDone() { out.msgDone(); }
Expand All @@ -147,25 +150,25 @@ struct MeatpackSerial : public SerialBase <MeatpackSerial < SerialT >> {
// So, instead of doing MeatpackSerial<MultiSerial<...>> we should do MultiSerial<MeatpackSerial<...>, MeatpackSerial<...>>
// TODO, let's fix this later on

if (charCount) return charCount; // The buffer still has data
if (serial_state[index.index].charCount) return serial_state[index.index].charCount; // The buffer still has data
if (out.available(index) <= 0) return 0; // No data to read

// Don't read in read method, instead do it here, so we can make progress in the read method
const int r = out.read(index);
if (r == -1) return 0; // This is an error from the underlying serial code
meatpack.handle_rx_char((uint8_t)r, index);
charCount = meatpack.get_result_char(serialBuffer);
readIndex = 0;
serial_state[index.index].charCount = meatpack.get_result_char(serial_state[index.index].buffer);
serial_state[index.index].readIndex = 0;

return charCount;
return serial_state[index.index].charCount;
}

int readImpl(const serial_index_t index) {
// Not enough char to make progress?
if (charCount == 0 && available(index) == 0) return -1;
if (serial_state[index.index].charCount == 0 && available(index) == 0) return -1;

charCount--;
return serialBuffer[readIndex++];
serial_state[index.index].charCount--;
return serial_state[index.index].buffer[serial_state[index.index].readIndex++];
}

int read(serial_index_t index) { return readImpl(index); }
Expand Down