
#include "usbd_cdc_if.h"

const long USB_SEND_TIMEOUT = 10000L;


uint8_t buffer[128];
volatile int bufferindex = 0;

void __io_flush() {
	long count = 0;

	while(count < USB_SEND_TIMEOUT && CDC_Transmit_FS(buffer, bufferindex) == USBD_BUSY) {
		++count;
	}

	bufferindex = 0;
}

int __io_putchar( int ch )
{
	if (ch == '\n') __io_putchar('\r');
	buffer[bufferindex++] = ch;
	if (ch == '\n' || bufferindex == sizeof(buffer)) __io_flush();

    return 1;
}
