CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

can a constant have a variable name?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

can a constant have a variable name?
PostPosted: Sat Jun 06, 2009 11:04 pm     Reply with quote

I just figure there has to be a nicer way of doing this:
Code:
      case 0x11:
         _this = SP1_MODE[_this];
         break;
      case 0x12:
         _this = SP2_MODE[_this];
         break;
      case 0x13:
         _this = SP3_MODE[_this];
         break;
      case 0x14:
         _this = SP4_MODE[_this];
         break;
      case 0x15:
         _this = SP5_MODE[_this];
         break;
      case 0x16:
         _this = SP6_MODE[_this];
         break;
      case 0x17:
         _this = SP7_MODE[_this];
         break;
      case 0x18:
         _this = SP8_MODE[_this];
         break;
      case 0x19:
         _this = SP9_MODE[_this];
         break;
      case 0x1A:
         _this = SPA_MODE[_this];
         break;
      case 0x1B:
         _this = SPB_MODE[_this];
         break;
      case 0x1C:
         _this = SPC_MODE[_this];
         break;
      case 0x1D:
         _this = SPD_MODE[_this];
         break;
      case 0x1E:
         _this = SPE_MODE[_this];
         break;
      case 0x1F:
         _this = SPF_MODE[_this];
         break;

It just repeats so nicely Smile I didn't post the very cumbersome tables that the different CONSTs reference. The idea is my variable needs to be assigned the lookup value of one table or another given which case applies. Is there anyway of throwing that in a loop or??

Steven
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Jun 07, 2009 3:14 am     Reply with quote

Looks like an awkward way to realize a two-dimensional constant array.
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Sun Jun 07, 2009 11:51 am     Reply with quote

Ahhh, of course!

One-track mind = one-dimentional thinking = oops.

Thanks.
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Sun Jun 07, 2009 8:30 pm     Reply with quote

Just when I thought I had it... I dont :(

Code:
int PEDAL_MAPS[18][256] = {{ big huge data set }};


So there I have 18 rows of 256 columns. Now I want to pick out any given value from any given row. All I can think to do is explicity reference it
Code:
var = PEDAL_MAPS[4][211]

but then what was the point of making the array? I mean, if I know exactly which one to pick out I may as well just hardcode the value.

I need a way to say, based on the value of a variable equal to the first element of each row, what is the nth element's value.

Does that make sense?

I'd like to avoid having to loop through everything checking if PEDAL_MAPS[i][0] == desired_row.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Jun 07, 2009 11:34 pm     Reply with quote

The above case construct requires a variable argument of the switch() statement. Then a two dimensional array is an equivalent, more compact construct.

If you want to achieve something different, you didn't yet manage to make it clear, at least to me.
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Sun Jun 07, 2009 11:46 pm     Reply with quote

No, you got it. I just don't know how to pick an element out of that 2-dim array in an efficient manner. But what I came up with works ok I suppose:

Code:
void get_options()   //read in the options that are set into memory
{
   Option_PedalMod = read_eeprom( 0x06 );
   for (i=0; i< sizeof PEDAL_MAPS / sizeof PEDAL_MAPS[0]; ++i)
   {
      if (PEDAL_MAPS[i][0] == Option_PedalMod)
      {
         Option_PedalMod = i;
         return;
      }
      restart_wdt();
   }
   Option_PedalMod = 0xff;   //didn't find a matching mode/map so indicate 0xff to skip pedal modification
}

unsigned int8 PedalModifier ( unsigned int8 _this )
{
   if (Option_PedalMod == 0xff) return _this;
   return PEDAL_MAPS[Option_PedalMod][_this];
}


I just thought there's be a better way of nailing down the array row without looping through a bunch of if statements. In PHP, for example, I can assign a label to the first column, then I can directly query which row in that column contains the element X that I'm looking for. From that, I can then directly pull out the data I need.

This works though.

Thanks for getting me on track.


Last edited by s_mack on Mon Jun 08, 2009 10:11 am; edited 1 time in total
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 5:59 am     Reply with quote

I think you are over complicating or not quite understanding what you need.

You nee a 2 dimensional array containing you const values.

int PEDAL_MAPS[18][256] = {{SP1_MODE_DATA}, {SP2}, ...};

You have your 2 indexes, your switch value, you don't show it so take the following example, and _this so

if you have
switch (myVal) {
case 0x11:
_this = SP1_MODE[_this]
break;

then you would use

_this = PEDAL_MAPS[myVal][_this];

This assumes myVal ranges for 0 to 17, your code implies it is from 0x11 (17) to 0x1F (31) so you would need to adjust

_this = PEDAL_MAPS[myVal - 0x11][_this];
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 10:06 am     Reply with quote

But them I'm right back to having n case statements, right?

And myVal doesn't actually "range" from anything. Ideally if I'm going through all my tables it goes 0x01; 0x02 and then 0x11 to 0x1F as shown above and then there are a few more random-ish ones after that. At first I was thinking I could only handle the ones that were in sequence and the others would have to be one-off case statements. And if we're back to diong cases anyway they may as well all be that way.

In terms of memory usage... having the tables at all sucks because it bumps me up from 47% ROM usage to 85%, but dynamically building them in memory bumps the RAM usage to 100% from 49%. Doing the loop takes up less ROM than the cases but a bit more RAM. Overall, if my only two choices are looping through or using a switch... the loop is slightly more efficient and far more scaleable.

Or I'm not getting what you're saying Smile

[edit] Sorry, I think I might see what you're saying and that's what I did do. I notice now I didn't post a very complete example in my previous post from yesterday. I've added the 2nd function at the bottom of it.

My point was I was trying to avoid using either a switch or a for loop and hoped there was a more direct way of refering to the array.
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 12:21 pm     Reply with quote

Grr... I thought I had it anyway!

It tested fine in Visual C++ compiler and compiles fine in CCS, but I was having trouble with in-field testing. I've narrowed it down to this statement:

Code:

sizeof PEDAL_MAPS / sizeof PEDAL_MAPS[0];


With PEDAL_MAPS being a two-demension array of 18 rows, 256 columns.

In Visual C++ the above code results in 18 as expected. However, run through the PIC I'm getting a result of 1.

Ideas?
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 12:28 pm     Reply with quote

Hmm...

Ok, when doing it in Visual I had the table defined only as INT whereas in CCS I have it as INT8 CONST for better memory utilization.

Is that my problem?

How else can I get the number of rows in my dataset?

I can hardcode it, but we'll be adding and removing sets of data all the time and it would suck to have to constantly update the limit for the loop as well.
s_mack



Joined: 04 Jun 2009
Posts: 107

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 12:42 pm     Reply with quote

Nevermind...

I'll just do this:
Code:
int8 CONST NUM_PEDAL_MAPS = 18;   //update this whenever adding/removing maps to PEDAL_MAPS below.  Required for code down below
int8 CONST PEDAL_MAPS[NUM_PEDAL_MAPS][256] = {[big data set]};


Since I'd have to update the size of PEDAL_MAPS anyway I still only have to do it once, I now just do it the line above.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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