Skip to content

Instantly share code, notes, and snippets.

@LambdaP
Last active December 16, 2015 17:29
Show Gist options
  • Save LambdaP/5471027 to your computer and use it in GitHub Desktop.
Save LambdaP/5471027 to your computer and use it in GitHub Desktop.
Basic implementation of an escaping mechanism.
#define ESCAPE_CHAR 242
#define START_CHAR 0x7E
#define END_CHAR 0x0D
void usb_putc(uint8_t c);
uint8_t usb_getc();
void usb_escaped_putc(uint8_t c)
{
switch(c) {
case START_CHAR:
case END_CHAR:
case ESCAPE_CHAR:
usb_putc(ESCAPE_CHAR);
usb_putc(ESCAPE_CHAR^c);
break;
default:
usb_putc(c);
}
}
void usb_escaped_puts(uint8_t *b, unsigned int n)
{
for(unsigned int = 0 ; i < n ; i++)
usb_escaped_putc(b[i]);
}
void usb_get(uint8_t *buf)
{
int i = 0;
do{
uint8_t c = usb_getc();
switch(c) {
case ESCAPE_CHAR:
buf[i] = c;
buf[i++] ^= usb_getc();
break;
default:
buf[i++] = c;
break;
}
} while(buf[i-1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment