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

Refactor serial class with templates #20783

Merged
merged 36 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b1efb2a
Start implementing a generic serial hook class.
X-Ryl669 Jan 14, 2021
700ba83
Add support for common interface for Serial class and for hook on suc…
X-Ryl669 Jan 15, 2021
5464687
Fix include error in Linux
X-Ryl669 Jan 15, 2021
8ce5831
Fix building bug on numerous platforms
X-Ryl669 Jan 15, 2021
46524cc
More fix for some platform
X-Ryl669 Jan 15, 2021
a8e51c8
More fix for AVR here
X-Ryl669 Jan 15, 2021
d1d2c7d
Merge branch 'bugfix-2.0.x' into SerialHook
X-Ryl669 Jan 16, 2021
ebbc141
cleanup
thinkyhead Jan 17, 2021
2ce2a24
etc
thinkyhead Jan 17, 2021
c09c36f
tweaks
thinkyhead Jan 17, 2021
e894940
Fix build for AVR HAL
X-Ryl669 Jan 20, 2021
89cfe45
Fix for LCD serial
X-Ryl669 Jan 20, 2021
6e81495
Fix DUE build
X-Ryl669 Jan 20, 2021
3cc1eb1
Fix build for Teensy 41
X-Ryl669 Jan 20, 2021
dfe09b2
Fix for Teensy 3.x
X-Ryl669 Jan 20, 2021
fa5d309
Fix formatting difference I've introduced while removing the SERIAL_P…
X-Ryl669 Jan 20, 2021
6d80360
Add message done hook so it's possible to know when a command was pro…
X-Ryl669 Jan 19, 2021
ac208c5
Add a ForwardSerial interface to hook platform defined serial instance
X-Ryl669 Jan 20, 2021
d7c11e5
Fix build for at90usb1286
X-Ryl669 Jan 20, 2021
8d9a1c5
Various fix for many platform
X-Ryl669 Jan 20, 2021
4a415fc
Add missing include
X-Ryl669 Jan 20, 2021
a79539d
Fix build for malyan
X-Ryl669 Jan 20, 2021
8aa66a9
Fix build for mks_robin
X-Ryl669 Jan 20, 2021
ee5717d
Fixing another build error
X-Ryl669 Jan 20, 2021
e4b54ea
Try fixing unreproducable failure
X-Ryl669 Jan 20, 2021
e353384
Add documentation about the interface
X-Ryl669 Jan 21, 2021
7c1ba7f
Fix spelling and formatting mistake
X-Ryl669 Jan 21, 2021
05fbe72
Update HAL.h
thinkyhead Jan 21, 2021
042d2d4
Update HAL.h
thinkyhead Jan 21, 2021
a5e4eaa
Merge branch 'bugfix-2.0.x' into SerialHook
X-Ryl669 Jan 24, 2021
0a340cd
Merge remote-tracking branch 'upstream/bugfix-2.0.x' into PRR/SerialHook
sjasonsmith Jan 24, 2021
262b29f
Update HAL.h
thinkyhead Jan 25, 2021
114115a
Update gcode.cpp
thinkyhead Jan 25, 2021
e01fa54
adjust spacing
thinkyhead Jan 26, 2021
50b39fa
Merge remote-tracking branch 'upstream/bugfix-2.0.x' into pr/20783
thinkyhead Jan 26, 2021
2c34d29
Merge remote-tracking branch 'upstream/bugfix-2.0.x' into pr/20783
thinkyhead Jan 28, 2021
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
7 changes: 7 additions & 0 deletions Marlin/src/HAL/AVR/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
#include "../../inc/MarlinConfig.h"
#include "HAL.h"

#ifdef USBCON
DefaultSerial MSerial(false, Serial);
#ifdef BLUETOOTH
BTSerial btSerial(false, bluetoothSerial);
#endif
#endif

// ------------------------
// Public Variables
// ------------------------
Expand Down
10 changes: 9 additions & 1 deletion Marlin/src/HAL/AVR/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ typedef int8_t pin_t;

// Serial ports
#ifdef USBCON
#define MYSERIAL0 TERN(BLUETOOTH, bluetoothSerial, Serial)
#include "../../core/serial_hook.h"
typedef ForwardSerial0Type< decltype(Serial) > DefaultSerial;
extern DefaultSerial MSerial;
#ifdef BLUETOOTH
typedef ForwardSerial0Type< decltype(bluetoothSerial) > BTSerial;
extern BTSerial btSerial;
#endif

#define MYSERIAL0 TERN(BLUETOOTH, btSerial, MSerial)
#else
#if !WITHIN(SERIAL_PORT, -1, 3)
#error "SERIAL_PORT must be from -1 to 3. Please update your configuration."
Expand Down
195 changes: 14 additions & 181 deletions Marlin/src/HAL/AVR/MarlinSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ void MarlinSerial<Cfg>::flush() {
}

template<typename Cfg>
void MarlinSerial<Cfg>::write(const uint8_t c) {
size_t MarlinSerial<Cfg>::write(const uint8_t c) {
if (Cfg::TX_SIZE == 0) {

_written = true;
Expand All @@ -480,7 +480,7 @@ void MarlinSerial<Cfg>::write(const uint8_t c) {
// location". This makes sure flush() won't return until the bytes
// actually got written
B_TXC = 1;
return;
return 1;
}

const uint8_t i = (tx_buffer.head + 1) & (Cfg::TX_SIZE - 1);
Expand Down Expand Up @@ -510,6 +510,7 @@ void MarlinSerial<Cfg>::write(const uint8_t c) {
// Enable TX ISR - Non atomic, but it will eventually enable TX ISR
B_UDRIE = 1;
}
return 1;
}

template<typename Cfg>
Expand Down Expand Up @@ -556,161 +557,6 @@ void MarlinSerial<Cfg>::flushTX() {
}
}

/**
* Imports from print.h
*/

template<typename Cfg>
void MarlinSerial<Cfg>::print(char c, int base) {
print((long)c, base);
}

template<typename Cfg>
void MarlinSerial<Cfg>::print(unsigned char b, int base) {
print((unsigned long)b, base);
}

template<typename Cfg>
void MarlinSerial<Cfg>::print(int n, int base) {
print((long)n, base);
}

template<typename Cfg>
void MarlinSerial<Cfg>::print(unsigned int n, int base) {
print((unsigned long)n, base);
}

template<typename Cfg>
void MarlinSerial<Cfg>::print(long n, int base) {
if (base == 0) write(n);
else if (base == 10) {
if (n < 0) { print('-'); n = -n; }
printNumber(n, 10);
}
else
printNumber(n, base);
}

template<typename Cfg>
void MarlinSerial<Cfg>::print(unsigned long n, int base) {
if (base == 0) write(n);
else printNumber(n, base);
}

template<typename Cfg>
void MarlinSerial<Cfg>::print(double n, int digits) {
printFloat(n, digits);
}

template<typename Cfg>
void MarlinSerial<Cfg>::println() {
print('\r');
print('\n');
}

template<typename Cfg>
void MarlinSerial<Cfg>::println(const String& s) {
print(s);
println();
}

template<typename Cfg>
void MarlinSerial<Cfg>::println(const char c[]) {
print(c);
println();
}

template<typename Cfg>
void MarlinSerial<Cfg>::println(char c, int base) {
print(c, base);
println();
}

template<typename Cfg>
void MarlinSerial<Cfg>::println(unsigned char b, int base) {
print(b, base);
println();
}

template<typename Cfg>
void MarlinSerial<Cfg>::println(int n, int base) {
print(n, base);
println();
}

template<typename Cfg>
void MarlinSerial<Cfg>::println(unsigned int n, int base) {
print(n, base);
println();
}

template<typename Cfg>
void MarlinSerial<Cfg>::println(long n, int base) {
print(n, base);
println();
}

template<typename Cfg>
void MarlinSerial<Cfg>::println(unsigned long n, int base) {
print(n, base);
println();
}

template<typename Cfg>
void MarlinSerial<Cfg>::println(double n, int digits) {
print(n, digits);
println();
}

// Private Methods

template<typename Cfg>
void MarlinSerial<Cfg>::printNumber(unsigned long n, uint8_t base) {
if (n) {
unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
int8_t i = 0;
while (n) {
buf[i++] = n % base;
n /= base;
}
while (i--)
print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
}
else
print('0');
}

template<typename Cfg>
void MarlinSerial<Cfg>::printFloat(double number, uint8_t digits) {
// Handle negative numbers
if (number < 0.0) {
print('-');
number = -number;
}

// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
LOOP_L_N(i, digits) rounding *= 0.1;
number += rounding;

// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
print(int_part);

// Print the decimal point, but only if there are digits beyond
if (digits) {
print('.');
// Extract digits from the remainder one at a time
while (digits--) {
remainder *= 10.0;
int toPrint = int(remainder);
print(toPrint);
remainder -= toPrint;
}
}
}

// Hookup ISR handlers
ISR(SERIAL_REGNAME(USART, SERIAL_PORT, _RX_vect)) {
MarlinSerial<MarlinSerialCfg<SERIAL_PORT>>::store_rxd_char();
Expand All @@ -720,11 +566,9 @@ ISR(SERIAL_REGNAME(USART, SERIAL_PORT, _UDRE_vect)) {
MarlinSerial<MarlinSerialCfg<SERIAL_PORT>>::_tx_udr_empty_irq();
}

// Preinstantiate
template class MarlinSerial<MarlinSerialCfg<SERIAL_PORT>>;

// Instantiate
MarlinSerial<MarlinSerialCfg<SERIAL_PORT>> customizedSerial1;
// Because of the template definition above, it's required to instantiate the template to have all method generated
template class MarlinSerial< MarlinSerialCfg<SERIAL_PORT> >;
MSerialT customizedSerial1(MSerialT::HasEmergencyParser);

#ifdef SERIAL_PORT_2

Expand All @@ -737,12 +581,8 @@ MarlinSerial<MarlinSerialCfg<SERIAL_PORT>> customizedSerial1;
MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>>::_tx_udr_empty_irq();
}

// Preinstantiate
template class MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>>;

// Instantiate
MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>> customizedSerial2;

template class MarlinSerial< MarlinSerialCfg<SERIAL_PORT_2> >;
MSerialT2 customizedSerial2(MSerialT2::HasEmergencyParser);
#endif

#ifdef MMU2_SERIAL_PORT
Expand All @@ -755,12 +595,8 @@ MarlinSerial<MarlinSerialCfg<SERIAL_PORT>> customizedSerial1;
MarlinSerial<MMU2SerialCfg<MMU2_SERIAL_PORT>>::_tx_udr_empty_irq();
}

// Preinstantiate
template class MarlinSerial<MMU2SerialCfg<MMU2_SERIAL_PORT>>;

// Instantiate
MarlinSerial<MMU2SerialCfg<MMU2_SERIAL_PORT>> mmuSerial;

template class MarlinSerial< MarlinSerialCfg<MMU2_SERIAL_PORT> >;
MSerialT3 mmuSerial(MSerialT3::HasEmergencyParser);
#endif

#ifdef LCD_SERIAL_PORT
Expand All @@ -773,12 +609,9 @@ MarlinSerial<MarlinSerialCfg<SERIAL_PORT>> customizedSerial1;
MarlinSerial<LCDSerialCfg<LCD_SERIAL_PORT>>::_tx_udr_empty_irq();
}

// Preinstantiate
template class MarlinSerial<LCDSerialCfg<LCD_SERIAL_PORT>>;

// Instantiate
MarlinSerial<LCDSerialCfg<LCD_SERIAL_PORT>> lcdSerial;

template class MarlinSerial< LCDSerialCfg<LCD_SERIAL_PORT> >;
MSerialT4 lcdSerial(MSerialT4::HasEmergencyParser);

#if HAS_DGUS_LCD
template<typename Cfg>
typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::get_tx_buffer_free() {
Expand All @@ -796,7 +629,7 @@ MarlinSerial<MarlinSerialCfg<SERIAL_PORT>> customizedSerial1;

// For AT90USB targets use the UART for BT interfacing
#if defined(USBCON) && ENABLED(BLUETOOTH)
HardwareSerial bluetoothSerial;
MSerialT5 bluetoothSerial(false);
#endif

#endif // __AVR__
Loading