I'm new to using classes and making libraries. I'm working on a project that desperately needs to be broken out. So I'm making a library and therefore creating a class that I can access my functions in.

The library is for communications via tx/rx with a GameCube controller

Basically I have a "read" function and a "translate" function. The read dumps bits and bytes into an array. Translate reorganizes the data into a struct array called gc_status.

What i would like to make the translate funtion at some point say "button1 = gc_status.button1" but how would I make it so that I can refer to button1 in the instance of the class. Such as:GCpad controller1(pin)and the button would be:controller1.button1

You have not provided near enough details on your class and struct. From what you have shown, there is no relationship between an instance of the GCpad class and anything with a button1 member. Nor does the class appear to have a gc_status member.

This is not snippets-are-us. They are down the road a ways. We want to see all of your code.

That seems like you want a struct where you can have controller1.button1, controller1.button2, controller1.button3 etcthat should be a relatively simple c code thing however I only looked it up once and don't know the specifics of making a variable like that, looking up struct on google should get you started atleast

However, properties as you probably know them from other languages are hard to do in plain c++. Either you don't mind having the variable settable from outside, or you rather have a method getButton().

BTW: a struct is a class (without methods), why not make your data struct just private variables of your class ?If there are multiple instances of the class but only a global instance of the struct, make the data variables static.But I understand you just use the class to have your library look Arduino style and support a single instance anyway.

This is not snippets-are-us. They are down the road a ways. We want to see all of your code.

Calm yourself. The reason I dont post the whole code is because its horrible and I barely understand some of it. Like I said some functions desperately need to be broken out. I said pseudo code did I not?

Although I could be more clear..Yes the struct is defined outside of the class. Data is dumped into it via the void translate(). The data in it can change after every read and every translate, making it a more volatile place. Not so good if you have more that one controller. Thats why id like to integrate in the translate function a way to store them as variables accessable by referencing controller1.button1. Do you get that?

However, properties as you probably know them from other languages are hard to do in plain c++. Either you don't mind having the variable settable from outside, or you rather have a method getButton().

BTW: a struct is a class (without methods), why not make your data struct just private variables of your class ?If there are multiple instances of the class but only a global instance of the struct, make the data variables static.But I understand you just use the class to have your library look Arduino style and support a single instance anyway.

While yeah it would make sense to make the struct private, id rather not because I dont understand it fully, maybe sometime later. In the mean time its well enough to have it globally accessable. Id just like to pin down those values at a specific time.

So yes what you posted is very nearly what I want. I just wanted to make sure that if in my translate function I say "button1 = gc_status.data1"When I try to reference button1 later, would I type. "controller1.button1"? following the way you stated in your code.

And yes exactly im making it look pretty. Someone else can come and optimize it when I finally post it, but im just trying to get it to work without taking up a global variable like "bool button1" ya know?

im just trying to get it to work without taking up a global variable like "bool button1" ya know?

No, dunno.

Quote

Data is dumped into it via the void translate(). The data in it can change after every read and every translate, making it a more volatile place. Not so good if you have more that one controller.

I understand internally you want to stick with a global variable, but you want to expose it as a member variable ?Isn't that more complicated than necessary / useful ?If more than one controller can change the same data you should read this global variable directly.

Alternatives: -- bool GCpad::getButton() returns the value of that global variable.-- its a static variable GCPad.button common to all instances. Updated by any controller. -- its a bool& ( reference ) type variable.

Ok ill reveal more of my code so you can see what im working with, the code is derived from a sketch made by someone else. their method use a few steps.1. gc_get(); //this creates a gc_raw_dump array2. gc_translate_raw_data(); //this takes the raw dump, assembles the bits into bytes and puts it into struct gc_status

void gc_get(){ // listen for the expected 8 bytes of data back from the controller and // blast it out to the gc_raw_dump array, one bit per byte for extra speed. // Afterwards, call translate_raw_data() to interpret the raw data and pack // it into the gc_status struct. asm volatile (";Starting to listen"); unsigned char timeout; char bitcount = 64; char *bitbin = gc_raw_dump;

// Command to send to the gamecube // The last bit is rumble, flip it to rumble // yes this does need to be inside the loop, the // array gets mutilated when it goes through gc_send unsigned char command[] = { 0x40, 0x03, 0x00 }; if (rumble) { command[2] = 0x01; }

I guess my main problem is what do I do with that gc_status struct? Can I make it private and write to it using the translate command? That would allow it maintain its data whether there are 1 or 2 controllers correct?

But can you even put a struct in a class?If you can is it silly to do so?Or should I just take the entries from the struct and place them as declarations in private and then remove the struct?

Wouldn't it make more sense for your translate() method to return the translated results? Then you don't need the additional call to access the results, and your original question about how to store them becomes moot.

I only provide help via the forum - please do not contact me for private consultancy.

For one thing I want to keep the translate raw data in function intact because I didn't write it. Second then the method would have to return an array of over 17 entries. Thirdly I'm working with the end user in mind so I would like to keep the methods for referencing the button states in "English".

Also I'm looking for an answer to my question of how can I implement variables?

Specifically if I define an int or bool in the public or private how do I write to it in a method? What would I write?

Specifically if I define an int or bool in the public or private how do I write to it in a method? What would I write?

That part is easy and has been explained at least twice in this thread. But what you were trying to do in your original post seemed to be access a 'struct array' and it was unclear whether this was a struct, an array, a struct with an array in it, an array of structs, or what. Of course the syntax for dealing with that depends entirely on what your data structure is - and your code didn't include that.

I only provide help via the forum - please do not contact me for private consultancy.

I give up on this thread. But I'll call it solved because mister Arrch over there did answer my question. The whole deal with the struct, I now just going to replace it by defining its vars in the class.