importing/using a hex file in my project.

Hello!

I have a hex file which I would like to import into my sketch to be used as an array.
but I believe I need to use it in the way outlined below (unless someone says otherwise)

Converted into a text file my hex code looks like this.
What I am trying to do is to split this up and, in a loop send out the commands to a chip as a stream.

The first byte (01) is a value (which needs to be converted to decimal) and the 2nd byte (20) is the register.

0120 0840 a857 b805 a743 b70a a68a b606
a5b1 b511 a422 b416 a343 b30e a243 b20a
a157 b101 612c 00a0 b1b0 0580 ff81 ff82
ff88 ff89 ff8a ff90 ff91 ff92 ff83 ff84
ff85 ff8b ff8c ff8d ff93 ff94 ff95 ffc0
00e0 0061 2c00 4001 6011 80f5 2000 e300
4300 6311 8315 23ee 4004 4306 a057 b021
c40a e900 4905 6910 8904 2904 ec03 4c00
6cf1 612c 008c 132c 00c5 08ea 004a 056a
828a f32a 14ed 024d 036d 118d f22d 00c6

The above is just a snippet of a 28kb file

I believe it needs to be formatted like so....

const byte oplData[2][] = {
{1, 0x20},{8, 0x40}
}

Which then loops as 
for (x = 0; x < dataMax; x++) {
setReg(oplData[1][x],oplData[0][x]);
delay(whatever);
}

so my problem is, of course, that it would take me endless hours to format my hex code into the const byte array.
Is there some other/better way I can approach this?

thanks in advance for any guidance.

Hopefully, having read through your post, you can see why we ask you to use code tags when posting code.

Here have these - [code][/code] - they're going spare

Where is this hex file, where is it supposed to end up and where/why is the Arduino involved?
Seems like the conversion you want done would take but a minute on a PC.

By the way that is NOT a hex file it's ASCII

Mark

It’s pretty common in situations like this to write a pc-side program in your favorite pc programming language to read the “weird format” file and write C code...

AWOL:
Hopefully, having read through your post, you can see why we ask you to use code tags when posting code.

Here have these - [code][/code] - they're going spare

10 print "My apologies, I fixed that";
15 goto 10

hehe :wink:

DKWatson:
Where is this hex file, where is it supposed to end up and where/why is the Arduino involved?
Seems like the conversion you want done would take but a minute on a PC.

So the hex file is a modified version of a .vgm file, which is a file that includes a stream of register data pushed to music synthesizer ICs in this case, a Yamaha YMF262 OPL3 chip.

I am simply using the arduino at this point just to set up connection with the chip and play around a bit.
Beyond hardcoding a few registers by hand, I would like to stream the register values from the hex out to the chip in a loop and 'hear the music'

Your best bet is to read the data from a SD card.

Mark

Like this:

#include <stdio.h>

int main() {
    int dval, hval;
    printf("const byte oplData[2][] = {\n");
    while (1) {
    int r = scanf("%02x%02x ", &dval, &hval);
    if (r == EOF) break;
    printf("    {%d, 0x%02x},\n", dval, hval);
    }
    printf("};\n");
}

Run like "a.out myhexdata.c"

Beware (perhaps) that avrs have a maximum array size of 32k bytes.

This is a generic parser that is used to read ASCII text files character by character and make changes. As it is set up, it remove from broken sentences and replaces with a space - to take advantage of word wrap.

int main(int argc, char *argv[])
{
	int ch; // place to store each character as read
	int ch_old;
	FILE *fp; // "file pointer"
	FILE *temp;

	
	if (argc < 2)
	{
		printf("Usage: %s filename\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	if ((fp = fopen(argv[1], "r")) == NULL)
	{
		printf("Can't open %s\n", argv[1]);
		exit(EXIT_FAILURE);
	}
	temp = fopen("temp.txt", "w");
	//if (argc == 3) list_threshold = ((int)argv[2] - 48);
	
	while ((ch = getc(fp)) != EOF)
	{
		if(ch == 13) break;
		
		if(ch == 10) 
		{
			ch = getc(fp);
			//if(ch_old == 46 || (ch_old >= 65 && ch_old <= 90))
			if(ch_old == COMMA || islower(ch_old)) (putc(32, temp));
			else
			{
				putc(10, temp);
				putc(10, temp);
			}
		}
		putc(ch, temp);
		ch_old = ch;
	}
	fclose(fp);
	fclose(temp);
	return 0;
}

And yes, it's meant to be run on a desktop.