How to send a string from flash to the LCD in the best way possible?

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

I'm sending a bunch of strings to an ESP8266 like so:

UARTSendString_P(PSTR("ATE0\r\n"));

The UART send command looks like so:

void UARTSendString_P (const char *data)
{
	#ifdef DEBUG
		uint8_t i = 0;
		LCDSetTextColor(GREEN, BLACK);
	#endif

	for (char c = pgm_read_byte(data) ; c != 0x000 ; c = pgm_read_byte(++data))
	{
		UARTSendChar(c); // Send to ESP8266

		#ifdef DEBUG
			LCDGWriteChar(c);

			if (i++ == 25) // LCD EOL in default font size
			{
				LCDGWriteChar('\n');
			}
		#endif
	}
}

Incoming data is processed like so:

ISR(USART0_RXC_vect)
{
	UARTRXBuffer[UARTRXBufferIndex] = USART0.RXDATAL;

	if (UARTRXBuffer[UARTRXBufferIndex] == '\n')
	{
		UARTRXFlag = true;

		#ifdef DEBUG
			LCDSetTextColor(BLUE, BLACK);
			for (uint8_t i = 0; i <= UARTRXBufferIndex; i++)
			{
				LCDGWriteChar(UARTRXBuffer[i]);
			}
		#endif

		UARTRXBufferIndex = 0;
	}

	if (UARTRXBufferIndex < UART_RX_BUFFER_SIZE)
	{
		UARTRXBufferIndex++;
	}
	else
	{
		UARTRXBufferIndex = 0;
	}
}

Now... I have overwritten printf like so:

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;
	}
}

I would rather use printf to send strings and not individual characters all the time, what would be the shortest way to do that in the above code?

Last Edited: Sat. Feb 10, 2018 - 08:53 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You do know about printf_P(), sprintf_P() and the use of %S as an alternative to %S don't you?

 

Also these days use __flash so pgm_read_*() is no longer required.

 

Finally explore FDEV_SETUP_STREAM so printf() will output direct to LCD.

 

(when I've done the latter I've extended my LCD driver to recognise some little used character such as ` to act as an "escape" so something like "`hHello" would interpret the `h at the start as LCD_HOME etc. You can even extend this to things like `c3,7 to position the cursor to line 3 position 7 etc)

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

There is an issue inside "ISR(USART0_RXC_vect)" routine, you aim to reset index after receiving '\n'
.
But the next "if (UARTRXBufferIndex < UART_RX_BUFFER_SIZE)" will immediately increment the index to 1
.
You may write
"else if (UARTRXBufferIndex < UART_RX_BUFFER_SIZE)"
instead of
"if (UARTRXBufferIndex < UART_RX_BUFFER_SIZE)"

Majid

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

clawson wrote:
the use of %S as an alternative to %S

Is this what you call "spot the difference"?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

theusch wrote:

clawson wrote:
the use of %S as an alternative to %S

Is this what you call "spot the difference"?

I read the same thing and said "HUH?"

 

What IS the difference?

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Bloody auto correct on tablet! It was supposed to be %S versus %s