Best way to send part of an array to printf?

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a UART input buffer, when I detect '\n' I set a flag that is checked in the main loop. At that stage I have chars in the buffer + possibly an index to the '\n' character (don't have it but not a problem to make this a global variable). I would like to send from the beginning of the array up to the '\n' char to printf. What would be the most 'economical' way to do that?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why do you want to send it to printf()? Using the C string functions, the string needs to be terminated by a NULL. Your string terminated by a \n, so you could put a NULL where the \n is or simply write a function that prints each char until it hits a \n. As for economy - economy of what? Lines of code? Code size? Or execution time?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I did the following:

static FILE mydata = FDEV_SETUP_STREAM(LCDPutcharPrintf, NULL, _FDEV_SETUP_WRITE);

void LCDPutcharPrintf(char var, FILE *stream)
{
	LCDGWriteChar(var);
}

void LCDGWriteChar(char c)
{
	if (c == '\n') // New line
	{
		LCDTextData.cursorY += LCDTextData.textSize * 8;
		LCDTextData.cursorX = 0;
	} 
	else if (c != '\r') // Not cartridge return
	{
		LCDGDrawChar(LCDTextData.cursorX, LCDTextData.cursorY, c, LCDTextData.textColor, LCDTextData.textBGColor, LCDTextData.textSize);
		LCDTextData.cursorX += LCDTextData.textSize * 6;
	}
}

so printf() is handling the output to the LCD, that's why I rather it than go char by char. It works OK until now but if there is a better solution I'm willing to hear about it. I am interested in minimal code. Execution time does not matter as this is all being done in debug mode.