C equivalent to A$="string"

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

Another historic day as the 10K code size was broken for the first time in my life, should have listened to by friend ABBAh. :o

I have a function to read a file off the USB stick, but passing the filename to it seems to need contortionism galore.

void VMUSIC2_read_file (char * file_name)
	{
	putc (VMUSIC2_read_file_cmd1, com1_out);
	putc (VMUSIC2_read_file_cmd2, com1_out);
	fprintf ( com1_out, "%s\r", file_name);
	}

if I could just use the BASIC format it would make my life a lot easier instead of

#define	filename_ext "display.txt";

then

	static char * file_name = filename_ext;

and finally

		VMUSIC2_read_file (file_name); 

It would be nice to use something like A$="display.txt" and then

	VMUSIC2_read_file (A$ or equivalent);

or even better

	VMUSIC2_read_file ("display.txt");

so how is it done in a simpler way?

Need a break from all this for a day or 2. :-)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Well your:

A$="display.txt"

in C would be:

strcpy(A_dollar, "display.txt");

where "char A_dollar[N];" has been used and N is big enough for all the characters in "display.txt" PLUS one more to hold the hidden 0 that terminates the string.

But as written your preferred version:

VMUSIC2_read_file ("display.txt");

will simply work anyway.

When you write that in C the compiler will make a copy of "display.txt" (with a terminating 0 added) in SRAM and will also create a "hidden" pointer to it which holds the start address (that is the address where the 'd' lives) and when it makes the call to VMUSIC2_read_file() it will pass that pointer into the function.

So C already takes care of all this for you.

Cliff

PS more complex is when you don't want to waste the SRAM to hold "display.txt"<0> and instead want it to only be in the .cseg so that LPM needs to be used to access it.

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

js wrote:
if I could just use the BASIC format it would make my life a lot easier
It probably would. BASCOM seems to be a popular BASIC compiler for AVR. However, using BASIC compared to C further removes you from the output of your well-known AVR assembly language.

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

OK, will try things out after I collect my leftover brain cells from the garbage bin in a couple of days. :-)

edit and I realise that I should be able to get rid of the putc lines, I was just trying things out after getting the com1 I/O streams working with stdio.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

js wrote:
It would be nice to use something like A$="display.txt" and then
	VMUSIC2_read_file (A$ or equivalent);

or even better

	VMUSIC2_read_file ("display.txt");

so how is it done in a simpler way?

Need a break from all this for a day or 2. :-)

There was another thread pertaining to the general problem of the inconvenience of text-resident string data last weekend that I risked jumping into, and posted an example of a technique that I think can be useful sometimes. Nobody's downloaded the little .zip I made of an example of using it, so at the risk of compounding my embarrassment, I offer it again, in hopes that it might appeal to js:

passing string characters to an arbitrary function

Best regards!