Skip to content

Instantly share code, notes, and snippets.

@jaz303
Created March 23, 2017 12:09
Show Gist options
  • Save jaz303/653d9659a0302480bae67e08ade14704 to your computer and use it in GitHub Desktop.
Save jaz303/653d9659a0302480bae67e08ade14704 to your computer and use it in GitHub Desktop.
template <HardwareSerial* T>
class SerialWriter
{
public:
void writeMessage(unsigned char *message, int length);
};
template <HardwareSerial* T>
void SerialWriter<T>::writeMessage(unsigned char *message, int length) {
T->write(SafeSerial::MESSAGE_START);
for (int ix = 0; ix < length; ++ix) {
byte b = message[ix];
if (b == SafeSerial::MESSAGE_START || b == SafeSerial::MESSAGE_END || b == SafeSerial::MESSAGE_ESCAPE) {
T->write(SafeSerial::MESSAGE_ESCAPE);
T->write(SafeSerial::MESSAGE_XOR ^ b);
} else {
T->write(b);
}
}
T->write(SafeSerial::MESSAGE_END);
}
SerialWriter<&SERIAL> writer;
unsigned char inBuffer[BUFFER_SIZE];
void main() {
// Error: undefined reference to `SerialWriter<&Serial>::writeMessage(unsigned char*, int)'
writer.writeMessage(inBuffer, 10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment