#include "debug.h"
#include <stdio.h>

char debugStr[DEBUGCHARS];
volatile char dbgCurChar;
volatile uint8_t dbgStrFill; // amount of available chars in debugStr
volatile uint8_t dbgHasNewSPI;

void dbgLog(const char* fmt, ...) {
	uint8_t maxwrite = DEBUGCHARS - dbgStrFill;
	va_list va;
	va_start(va,fmt);
	char* pos = debugStr + dbgStrFill;
	dbgStrFill += vsnprintf(pos, maxwrite, fmt, va);
	va_end(va);
}

uint8_t dbgHasNew() {
	dbgHasNewSPI = (dbgStrFill > 0);
	return (dbgStrFill > 0);
}

char dbgReadChar() {
	if(dbgHasNew()) {
		char ret = debugStr[0];
		for(int i=0; i<dbgStrFill-1; i++) {
			debugStr[i] = debugStr[i+1];
		}
		return ret;
	} else {
		return 0xFF;
	}
}

void dbgAdvanceChar() {
	dbgCurChar = dbgReadChar();
}
