|
|
View previous topic :: View next topic |
Author |
Message |
aaaaamartin
Joined: 17 Apr 2005 Posts: 39 Location: Germany Stuttgart
|
variable coding help |
Posted: Sat Jul 21, 2007 11:54 pm |
|
|
Hello,
I'm looking for a way to assign a value to a variable as shown below.
Note that I used long for "var" ...
What I do not want is: 1) waste ram, 2) waste rom, 3) no solution ;-)
I have PCH 3.235.
Code: |
long var;
int val;
int a, b, c;
val = 10;
var = 1;
switch (var) {
case 0:
a = val;
break;
case 1:
b = val;
break;
case 2:
c = val;
break;
default:
break;
}
|
Any suggestions ?
Regards Martin |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
aaaaamartin
Joined: 17 Apr 2005 Posts: 39 Location: Germany Stuttgart
|
|
Posted: Sun Jul 22, 2007 1:21 am |
|
|
Thanks PCM,
I see, switch-code is efficient.
But, let me go a bit further into detail...
That's what I'm doing:
The Pic can recieve dumps of data, from a host, that it needs to assign to certain variables.
The host of course, does not worry about any internal variable names and it references to these with an address.
A packet recieved therefore looks like: [address - value] * n.
The Sender does not take care about the order of these pares among each other,
In other means, it does not necessarily need to update all of these variables.
Further, the host can requeset the pic to send back the value of a variable given an address.
Finally the host can request the pic to dump all of it's variables together with the address as the host knows them.
I need some hints, to not end up in a mess.
Thanks Martin |
|
|
Ttelmah Guest
|
|
Posted: Sun Jul 22, 2007 2:12 am |
|
|
Questions:
How many variables are involved?.
What are the sizes of the variables (all the same, as shown, or different for different commands)?.
Are the commands 'sequential' (in that you get a 'number' - var in your example that goes through a limited range, without gaps)?.
How many possible 'var' values exist?.
The normal way for a small number of values as shown, would be to simply use a array. If you needed local 'names', rather than using numeric values, then use a union.
Best Wishes |
|
|
aaaaamartin
Joined: 17 Apr 2005 Posts: 39 Location: Germany Stuttgart
|
|
Posted: Sun Jul 22, 2007 3:12 am |
|
|
Quote: | How many variables are involved?.
What are the sizes of the variables (all the same, as shown, or different for different commands)?.
|
variables involved : ~ 100
types: signed or unsigned 8-32 Bit, maybe bool, no floats so far.
Quote: | Are the commands 'sequential' (in that you get a 'number' - var in your example that goes through a limited range, without gaps)?.
|
Range is of course limited, to "address space" I invent.
But the var-val pairs are not in order.
Quote: |
How many possible 'var' values exist?.
|
hm, I plan "adress-space" of 14 bit's, but I don't use all of this, so some addresses don't have a corresponding variable.
Quote: |
The normal way for a small number of values as shown, would be to simply use a array. If you needed local 'names', rather than using numeric values, then use a union.
|
What can a union do in this case ?
Ok I can use an array, but what comes into play are the different types of the variables.
Further more this messes up my code, as the variables are defined and used over many sourcefiles. And I need to go quick in many of the places I use them in the running pic-code.
Just to make things a little clearer:
A host dump can set various variables in a running PIC system to new values. Each variable is referenced by unique number in the range of the address space. It is unlikely to have the complete address space assigned to variables, as it is 14bits and I don't have that much variables in use.
However do the variables spread over the address space, and are not in consecutive order.
My thoughts so far is to use a const table, that collects just the involved addresses that the host knows I have running, in case it requests a dump of all variables. This is then done by looping through the table and looking up each of the variables values in a switch statement as in my first example. I think this is terribly inefficient and I cannot come over thinking, that this is script kiddy coding style.
I would then reuse exactly that switch statement to assign a new value if the host asks for it.
The main problem is centrally assigning each var its address,
quickly referenciate a variable, and to collect them all for a dump to the host.
The Task is to react upon midi sysex messages.
Further advise appreciated , Martin |
|
|
Ttelmah Guest
|
|
Posted: Sun Jul 22, 2007 5:57 am |
|
|
The key about the union, is that it allows handling of the different types.
If you declare a 'structure' that contains the data you require, with the names you require, and then a union, of this structure to a byte array, you can access the data either 'by name' through the structure, or as bytes, in order through the array.
Now, I do a thing like this, with a system I have. I have five different 'packet' forms, sent back to the host computer, each with different layouts. I have five structures defining these layouts, used internaly by the PIC code, and a union between these and a byte array. My parser, returns a number (keep this much smaller - if you are only dealing with 100 values, limit it to perhaps 8bits), which is then used as an index to an array, which contains the address in the byte array of th data required, and a second array, which contains the sizes. So in my case (for example), value 32, comprises the address in the union, of an array of four floating point TOC values, and the corresponding 'size', is 16. The transfer program simply transfers the bytes (adds a checksum, etc.), and doesn't care what the data actually is. It simply knows it has to send 16bytes from the 'data' block for this command.
In memory, the same area, is accessed using the structures, allowing the PIC code to talk to it by name etc..
Making your address range larger, just increases code size. If you want it larger to allow multiple units to be used on the same bus, then have the large address comprise a smaller 'local' address, with the upper bits giving the unit address.
Best Wishes |
|
|
aaaaamartin
Joined: 17 Apr 2005 Posts: 39 Location: Germany Stuttgart
|
|
Posted: Sun Jul 22, 2007 6:28 pm |
|
|
Ttelmah,
I must admitt, I don't get it to work.
Can you please show an example.
Thanks Martin |
|
|
aaaaamartin
Joined: 17 Apr 2005 Posts: 39 Location: Germany Stuttgart
|
|
Posted: Sun Jul 22, 2007 7:57 pm |
|
|
Hello,
I now use a const array of pointers to each var and another arrray,
telling about the size of var, because I use int pointers.
Anyway I would like to see an example of Ttelmah's approach.
Thanks Martin |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|