[PLUG-Devel] Connect wrapper in c (was OT: C programmers....)

Joseph Sinclair plug-devel at stcaz.net
Fri Jun 19 22:57:18 MST 2009


I sent this originally from the wrong address, so I'm resending it in case the server rejects the other one.

That's good advice.
The second approach is definitely better, here's a snippet for James (using C, if using C++ change to an object and use a class member variable, more complex but also safer and easier to extend later)
that does what Scott suggests:

void connect(g_wiimote* wiimote_handle)
{
  // do stuff to connect to the wiimote here.
}

int main(int argc, char** argv)
{
  g_wiimote wiimote_handle;  // stack allocation much safer than heap and malloc.
  connect(&wiimote_handle); // connect the handle and init the library here.
  // call another function to do stuff here, pass in the handle as above, don't forget the &.
  disconnect(&wiimote_handle); // cleanup the handle and release resources here.
  return 0; // always return a value from main, 0 == success.
}

Scott Harris wrote:
> You could do that in several ways:
> 
> 1) make the g_wiimote object global to the methods:
> 
> /* static makes the handle file scope so it will not be visible outside
> of this file */
> static g_wiimote handle;
> connect(){ connect to wiimote }
> main(){ call connect(), perform actions on that handle}
> 
> 2) or even better pass in the address of the handle and use it:
> 
> connect(g_wiimote* handle) { connect to wiimote using *handle }
> main() { g_wiimote handle; call connect(&handle); perform actions on
> handle}
> 
> // if you are actully using C++ instead of C then you could pass by
> reference (instead of by pointer).
> 
> Regards,
>  Scott
> 
> James Finstrom wrote:
>> Greetings,
>>
>> The original email is below. but I would like to better explain what I
>> want and I am a C newb so my terminology can be assumed incorrect...
>>
>> I have a program I am putting together that basicly connects the
>> gphoto library to the cwiid library. I am currently connecting the
>> wiimote in main(). What I want to do is move the connection of the
>> wiimote out of main but still refer to the handle in main.  So right
>> now in main i connect to the wiimote and it creates g_wiimote. I then 
>> basicly read/write to g_wiimote. What I want to do is move that block
>> of code that does the connect out of main to a function
>> do_connect_wiimote() which will exicute that block of code and return
>> g_wiimote to main so I can read/write to it.
>>
>> Thanks in advance for not bashing the newb :)
>>
>>
>> Original users email:
>> Greetings all,
>>
>> I am writing app that allows me to control my canon SLR with my
>> wiimote. I am trying to figure out how to call a function in main that
>> generats a handle then returns the handle to main
>>
>>
>> Concept:
>>
>> connect(){ connect to wiimote and return handle foo}
>>
>> main(){ call connect(), get handle, perform actions on that handle}
>>
>>
>> James Finstrom
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> PLUG-devel mailing list  -  PLUG-devel at lists.PLUG.phoenix.az.us
>> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-devel
>>   
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> PLUG-devel mailing list  -  PLUG-devel at lists.PLUG.phoenix.az.us
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-devel

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: OpenPGP digital signature
Url : http://lists.PLUG.phoenix.az.us/pipermail/plug-devel/attachments/20090619/56c531b9/attachment.pgp 


More information about the PLUG-devel mailing list