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

using a variable to call a variable without if statements?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
zlittell



Joined: 08 Feb 2012
Posts: 13

View user's profile Send private message Visit poster's website

using a variable to call a variable without if statements?
PostPosted: Tue Mar 06, 2012 4:15 pm     Reply with quote

Well the title is horrible, but I didn't know how to explain it.

Say I have variables called

number
1B1
1B2
2B1
2B2
3B1
3B2
4B1
4B2
5B1
5B2

I want to call one of the B# variables depending on the value of number. Each B corresponds to a Button (button 1 and button 2).

So if number = 2 and I press button 2 i want to use variable 2B2.

How can I do this without a million if statements ? I have found through googling that in normal C++ I can use cout << number << 2 but this does not seem correct for CCS C.

-zack-
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 06, 2012 5:49 pm     Reply with quote

You need to get a book on the C language and study it, or read online
tutorials.

This post has links for tutorials:
http://www.ccsinfo.com/forum/viewtopic.php?t=46384&start=3

Please study the C language and then you will learn the basics
necessary to write programs in C.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Mar 06, 2012 5:50 pm     Reply with quote

have you thought of using this construct that programmers
call an ARRAY, where the indexing value is your button number ??

i bet it would work for you too.
Very Happy Very Happy
zlittell



Joined: 08 Feb 2012
Posts: 13

View user's profile Send private message Visit poster's website

PostPosted: Tue Mar 06, 2012 6:30 pm     Reply with quote

If feel like you guys are getting to used to babying people.

This isn't my first rodeo, I have wrote a lot of programs and I am asking how you would go about this one particular scenario. If I were to read a million tutorials on C that would be a large waste of my time. You might as well shut the forum down if all you are going to do is say google some stuff. I also showed you how it would be done in a similar form of code if that wouldn't work in C compiled for a PC.

Also the array still doesn't solve my problem you just wrapped my problem into a neater package?

I was just wondering how I could append two variables together to look up the value of another variable.

so I would be able to go
currentB1 = number + B1
so if number was 3 it would set currentB1 to the value of 3B1

the only way I can think of doing it right now would involve if statements to check the current value of number and then set all the variables individually.

is there a more elegant way you would go about this.

I am asking for your opinion on if its possible to do this in a better way and how you would go about it compared to my way. im not asking for you to spoon feed me.

also: i guess i could also see what asmboy MIGHT have been trying to say in his highly confusing and slightly condescending post. I could make each button an array of values then use the value of 'number' as the index. I feel like this isn't what he was saying but this would work.

maybe i just wasn't clear enough in my first post
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Mar 06, 2012 6:55 pm     Reply with quote

I do apologize if my attempts at humor are misunderstood,
but my comments on this board are for fun:

i get paid 8 days a week to design circuits and program pics.
i have no ill will for people who are trying seriously to learn.

Perhaps nobody is taking you seriously as the example you give
is lacking in a rather OBVIOUS way - vis a vis CCS 'C and C in general!

I can tell that you have NEVER actually tried to compile what you are going on about , at least in CCS - using variable names such as 3B1 etc?

What result do you get using those variable names ?

That one observation alone more than justifies PCM programmer, who was
quite correct in his suggestion to you. When you so blatantly screw up the "baby stuff" i guess you get what you have coming from the professionals who HAVE wrotten a lot more code than you already.

perhaps you could now show your attempt to CODE something that illustrates the precise difficulty you are having, and then we can help you more. And BTW - make sure it actually COMPILES before you post it ??

That way you maybe get a "2fer" -
more sympathy and more help at the same time
as to :
Quote:

I have wrote a lot of programs


I much prefer to let the quality, accuracy and efficiency of your code
decide the correctness of that assertion.
Very Happy

and lastly unless i read it wrong ,ARRAYS are STILL going to be the way to do this , only with one array potentially containing nested indexing to ANOTHER. But to get a GOOD answer??
POST YOUR CODE ............
zlittell



Joined: 08 Feb 2012
Posts: 13

View user's profile Send private message Visit poster's website

PostPosted: Tue Mar 06, 2012 7:25 pm     Reply with quote

well i can't post the code without completely rewriting it since this snippet will be used in a commercial product. I have tried to be as descriptive as possible about what I was looking for.

I have, however, figured out how to do it efficiently without using a million if statements to check which profile I am in and which button I am looking for. I will also remember that I should not look to these forums for anything other than sitting around on my elitist pedestal. I will however continue providing help to the people who email and pm me with questions about getting their usb code to work among the other things I help with while treating others as equals and not looking down upon them from my computer desk.

edit:
since I have settled down and realized how I have been contributing to this pissing contest, I will be editing an example to use what I have discussed so that if anyone has a similar problem they can learn.
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Tue Mar 06, 2012 8:29 pm     Reply with quote

One thing to keep in mind here - if you look through the forum posts for any length of time, you will begin to see why some of the regulars are a bit sensitive. The number of obvious students that post questions showing they have no intention of actually putting the effort into doing their homework gets a bit old. Especially when they get irritated when people here won't do their homework for them - even when they are given pointers to where to look, they are mad because people won't do their homework for them. I spent my time in school getting my degree. I really appreciated it when someone could give me a pointer to where to look for how to solve a problem that had me stumped. I NEVER expected anyone to write my code and do my homework for me. I've got my degree - I don't need to do someone elses homework for them. I will be happy to help, but they have to put in the effort to at least try and solve the problem (no, telling someone else to write the code and design the circuit is not doing their own work unless they think they want to go into management). This is why the regulars here get a bit sensitive. I done graduated. In fact I done retired - if I wanted homework, I would ask my wife :-)
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
zlittell



Joined: 08 Feb 2012
Posts: 13

View user's profile Send private message Visit poster's website

PostPosted: Tue Mar 06, 2012 8:35 pm     Reply with quote

Here is the way the example works normally:
Code:


/////////////////////////////////////////////////////////////////////////////
// this is how this example normally works
// it just outputs a fixed keyboard character for each microswitch
// this example uses 2 buttons only
////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
//
// usb_keyboard_task()
//
// Sends a packet of keyboard data.  The protocol was specified in the HID
// report descriptor (see usb_desc_kbmouse.h), and is:
//     tx_msg[0] = modifier (an 8bit bitmap of shift, tab, alt keypress)
//     tx_msg[1] = const 0
//     tx_msg[2:6] = an array of held down keys.  a=4, b=5, etc.
//                   if msg[2:7]={0} then no keys are held down
//
//     rx_msg[0] = 5bit bitmap of led status
//
/////////////////////////////////////////////////////////////////////////////
void usb_keyboard(void)
{
   static int8 tx_msg[7]={0,0,0,0,0,0,0};
   static int8 leds;

   if (!input(micro1))
   {
      tx_msg[2]=0x1D;
   }
   else
   {
      tx_msg[2]=0x00;
   }
   if (!input(micro2))
   {
      tx_msg[3]=0x04;
   }
   else
   {
      tx_msg[3]=0x00;
   }

   usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);

   //receive NUM LOCK, CAPS LOCK, etc LED status from PC.
   //we won't do anything with it.
   if (usb_kbhit(1))
   {
      usb_get_packet(1, &leds, 1);
   }
}

void main(void)
{
   usb_init();
   
   while (TRUE)
   {
      if (usb_enumerated())
      {
         usb_keyboard();
         delay_ms(10);
      }
   }
}


Here is how it works with the array for each button allowing for profiles
Code:


/////////////////////////////////////////////////////////////////////////////
// what if we could use different keyboard characters for the two
// microswitches, depending on the value of a variable named profile
// (this will not cover the changing of profile.  you can change it with a
//  button or with a serial command. TONS OF OPTIONS)
////////////////////////////////////////////////////////////////////////////

int Profile = 0;

/////////////////////////////////////////////////////////////////////////////
// Define arrays for buttons
////////////////////////////////////////////////////////////////////////////
//when Profile = 0, microswitch 1 sends X to the keyboard
//when Profile = 1, microswitch 1 sends R to the keyboard
int microswitch_1[2]={0x1C,0x15};\
//when Profile = 0, microswitch 2 sends D to the keyboard
//when Profile = 1, microswitch 2 sends I to the keyboard
int microswitch_2[2]={0x07,0x0C};

/////////////////////////////////////////////////////////////////////////////
//
// usb_keyboard_task()
//
// Sends a packet of keyboard data.  The protocol was specified in the HID
// report descriptor (see usb_desc_kbmouse.h), and is:
//     tx_msg[0] = modifier (an 8bit bitmap of shift, tab, alt keypress)
//     tx_msg[1] = const 0
//     tx_msg[2:6] = an array of held down keys.  a=4, b=5, etc.
//                   if msg[2:7]={0} then no keys are held down
//
//     rx_msg[0] = 5bit bitmap of led status
//
/////////////////////////////////////////////////////////////////////////////
void usb_keyboard(void)
{
   static int8 tx_msg[7]={0,0,0,0,0,0,0};
   static int8 leds;

   if (!input(micro1))
   {
      tx_msg[2]=microswitch_1[Profile];
   }
   else
   {
      tx_msg[2]=0x00;
   }
   if (!input(micro2))
   {
      tx_msg[3]=microswitch_2[Profile];
   }
   else
   {
      tx_msg[3]=0x00;
   }

   usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);

   //receive NUM LOCK, CAPS LOCK, etc LED status from PC.
   //we won't do anything with it.
   if (usb_kbhit(1))
   {
      usb_get_packet(1, &leds, 1);
   }
}

void main(void)
{
   usb_init();
   
   while (TRUE)
   {
      if (usb_enumerated())
      {
         usb_keyboard();
         delay_ms(10);
      }
   }
}


The original idea was to just call the specific button depending on the profile:
Something like
#DECLARE P0M1 0x1C
#DECLARE P0M2 0x07
#DECLARE P1M1 0x15
#DECLARE P1M2 0x0C

then in the code call something like this
tx_msg[2]=("P" + profile + "M1")
but I could not get it to work any way and everything I found works only on the PC using commands like cout, sprintf, malloc, memcpy
all of which I assume the PIC cannot do. which is why I then came here...


The code at the beginning works however!

-zack-

edit:
I can see how that could be frustrating. I wasn't expecting anyone to write code for me I just was looking for the one line that could do what i wanted

I was just trying to be vague because of the closed source nature, but at the same time descriptive so I could be helped and it did not work haha

and now I feel bad because either asmboy edited his post of I just completely ignored the beginning. Its that rage from working on code all day just to come into a problem that you can't figure out how to code to work the way you want. I am sure everyone has been there. I should have just taken a breather but instead I, with a hot head, took everything offensively and slammed on the keyboard.

however it did motivate me to fix the problem in a different way quickly!

-zack-
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Mar 07, 2012 12:57 am     Reply with quote

Just as a datapoint, I've pretty much stopped posting because of all the "studentia" last minute cries for "gimme your code" (or something similar).

And it does come in waves... twice a year, typically about mid-semester.. fascinating, really. I experienced it first hand working for a top10 US Engineering university.

So, we lurk - but with even a hint of a question that obviously lacks any real effort of research, I just move on to the next thread.

Just a data-point.
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Wed Mar 07, 2012 3:10 am     Reply with quote

Take a deep breath, and explain clearly what you actually want to do.

Problem is that your description makes no sense. You talk about 'calling' a variable. You can't call a variable. You can read it or write it, but not jump 'to' it, so your description makes no sense at all.

Now, forget 'standard C++'. CCS, is _C_, not C++. It does incorporate a few features that are normally only associated with C++, but as it stands, depending on your compiler settings, it is 99.9% 'out of the box' C, and can do just about everything that C is expected to do, _including_ sprintf, malloc, memcpy, which you 'assume the PIC cannot do'. It can. _read the manual_.

The one ability you list it does not have is cout, since this is a C++ only feature. However it does support it's own stream output ability, and in fact for assembling text messages, this would not seem to be wanted at all anyway.

Then you are really misunderstanding the language. #define, and macro processing, is a _preprocessor_ operation, not something you can use based upon a variable in code. However a simple character array would allow this, and seems to be all you need, which you have already done. Seriously though, why not just go two dimensional?.

I was a bit puzzled by your descriptions, since you refer to particular switches returning particular characters, but the numbers in the arrays you post don't agree with these at all.

Code:

char_from_micro1[2][2]={
        {0,0},
        {'X','R'}
};
char_from_micro2[2][2]={
       {0,0},
       {'D','I'}
};
//Assuming the comment values are right, not what was shown in the array

//Then in the code
    tx_msg[2]=char_from_micro1[input(micro1)][Profile];
    tx_msg[3]=char_from_micro2[input(micro1)][Profile];


You seem to be trying to make a 'mountain out of a molehill', because of fundamental misunderstandings of the language. A 'C 101' book might be a good place to start.

Best Wishes
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Mar 07, 2012 6:52 am     Reply with quote

I, for one was 'lost' as to what you're trying to accomplish,then again English is my native language, and I still don't understand it !
However, it sounds to me like you want what I think of as a 'keyboard remapper code', where if a key is pressed is 'sends' a string of data 'somewhere'. Depending on the 'profile', that key would send different data. PC keyboards have 'country' codes.
If so, that's an easy task, BTDT for the past 20 years. My original programs were in assembler, now in C. BTW, CCS C does have the sprintf function. In my case depending on the 'profile'( 1 of 4 choices), various keyboard keys will send different 'messages' to a serial port.
As far as the 'comments', as one of the 'old guys' I too am tired of those who fail to read datasheets, cut real code, or have never pressed F11. Guess it's a 'new' generation thing, sigh.
With respect to IP rights and commercial product code, the truth is that anything, any of us 'create', 1000s of others can do it as well, in 100s of different ways. Some use less code space, others are faster but it's all down to ones and zeros at the end of the day.
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Wed Mar 07, 2012 11:14 am     Reply with quote

There was an article in Circuit Cellar not too long ago that sort of sounds like what you are doing - the project used a matrix of single digit displays to not only show a digit on each display, but acted as the switch as well. It allowed the program to remap the display/keyboard each time it was used to a different pattern which eliminated the ability of someone off to the side figuring which keys you were pressing for an access/pin code for example since the "matrix" was different each time. I don't see it in a quick look thru the stack of magazines I have here, but it seems to me it was in the last year if you are interested. It may apply to what you are working on.
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
zlittell



Joined: 08 Feb 2012
Posts: 13

View user's profile Send private message Visit poster's website

PostPosted: Wed Mar 07, 2012 2:13 pm     Reply with quote

i have the code working now. i just wanted to do it a different way that was possible in C for a computer just didn't work with the compiler I suppose.

i wanted to call a variable by basically combining two strings obtained from a variable and a constant.

instead I used the profile as the index for the button arrays.

if you paste this code into the usb_keyboard.c example it will allow you to use profiles to select which character appears when you press each button.

that isn't my goal or my code I quickly edited an example so that I could show everyone what I was trying to do.

Maybe you guys didn't read the whole thread ?

edit:
gpsmikey I will be looking through my old magazines for that article it sounds like an interesting project. not really pertaining to my project but interesting none the less.
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Wed Mar 07, 2012 3:39 pm     Reply with quote

Found it. August, 2011 Circuit Cellar (#253) - "Microcontroller-based PIN reader". Had some interesting ideas in it - different processor, but the idea is valid for any processor - one of the PICS should do nicely too.
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
dorinm



Joined: 07 Jan 2006
Posts: 38

View user's profile Send private message

PostPosted: Wed Mar 07, 2012 4:06 pm     Reply with quote

you need an evaluator ...
tx_msg[2]=("P" + profile + "M1") ..mainly, to concatenate " "P" + profile + "M1" " into a string (a lot of mcu cycles!) , then evaluate the expression and compare to your defines ... basically it can be done, but would you waste so many mcu cycles?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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