The Open Lighting Project has moved!
We've launched our new site at www.openlighting.org. This wiki will remain and be updated with more technical information.
We've launched our new site at www.openlighting.org. This wiki will remain and be updated with more technical information.
Difference between revisions of "Using OLA with Xcode"
From wiki.openlighting.org
Chatchavan (talk | contribs) (→Example code: add channel variable) |
|||
(10 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
− | You can use OLA to implement your application in Objective-C++, a bridge between Objective-C and C++. This page explains how to integrate OLA client into your program. | + | You can use OLA to implement your application in Objective-C++, a bridge between Objective-C and C++. This page explains how to integrate OLA client into your program. |
+ | |||
+ | See also: http://www.bennigraf.de/wp-content/uploads/2012/11/writing-scola.pdf | ||
+ | |||
==What you'll need== | ==What you'll need== | ||
* Xcode | * Xcode | ||
− | * [http:// | + | * [http://www.openlighting.org/ola/mac-install/ OLA for Mac OS X] installed |
+ | |||
+ | == Create a new Project == | ||
+ | # File -> New Project, from the right under Mac OS X, select 'Application', then in the left panel choose 'Cocoa Application' | ||
+ | # Once the main window pops up, right click the 'main.m' file and select rename. Change the name to 'main.mm' (required for Objective-C++ extensions) | ||
==Configure your project== | ==Configure your project== | ||
# Select menu: "Project" -> "Edit Project Settings". All settings below are referred in a "Build" tab | # Select menu: "Project" -> "Edit Project Settings". All settings below are referred in a "Build" tab | ||
− | # | + | # Under 'Search Paths', set "Header Search Paths" to /usr/local/include (Source) or /opt/local/include (MacPorts) , depending on how you installed OLA. |
− | |||
− | |||
− | ==Add | + | ==Add the required libraries== |
− | # In your project tree, right-click on a group that contain frameworks (e.g. "External Frameworks and Libraries", " | + | # In your project tree, right-click on a group that contain frameworks (e.g. "External Frameworks and Libraries", "Frameworks/Linked Frameworks"), select "Add" -> "Existing Frameworks" |
# Select following frameworks: | # Select following frameworks: | ||
#* libprotobuf.dylib | #* libprotobuf.dylib | ||
Line 19: | Line 24: | ||
==Example code== | ==Example code== | ||
− | The following code sends value 255 to channel 0 in universe 0. First, it creates a client from a SimpleClient object. Then, it creates a DMX | + | The following code sends value 255 to channel 0 in universe 0. First, it creates a client from a SimpleClient object. Then, it creates a DMX buffer and sets channel 0 to 255. Finally it sends the buffer to a server. |
+ | |||
+ | #import <Cocoa/Cocoa.h> | ||
+ | #import <ola/DmxBuffer.h> | ||
+ | #import <ola/SimpleClient.h> | ||
+ | |||
+ | int main(int argc, char *argv[]) { | ||
+ | ola::SimpleClient simpleClient; | ||
+ | if (!simpleClient.Setup()) { | ||
+ | NSLog(@"Client setup failed"); | ||
+ | return -1; | ||
+ | } | ||
+ | |||
+ | ola::OlaClient *client = simpleClient.GetClient(); | ||
+ | |||
+ | ola::DmxBuffer buffer; | ||
+ | buffer.Blackout(); | ||
+ | buffer.SetChannel(0, 255); | ||
+ | |||
+ | unsigned int universe = 0; | ||
+ | if (!client->SendDmx(universe, buffer)) { | ||
+ | NSLog(@"Sending DMX failed"); | ||
+ | } | ||
+ | return 0; | ||
+ | // you could also run the main app here | ||
+ | return NSApplicationMain(argc, (const char **) argv); | ||
+ | } | ||
+ | |||
+ | You can find more information by browsing the source file of [http://linux-lighting.googlecode.com/files/ola-examples-0.6.0.tar.gz DMX example]. For me, the src/ola-client.cpp is quite useful. | ||
+ | |||
+ | Here is another example that create OlaServer and communicate with a pipe socket. | ||
#import <Foundation/Foundation.h> | #import <Foundation/Foundation.h> | ||
#include <errno.h> | #include <errno.h> | ||
#import <ola/DmxBuffer.h> | #import <ola/DmxBuffer.h> | ||
− | #import <ola/ | + | #import <ola/OlaClient.h> |
+ | #import <ola/network/SelectServer.h> | ||
+ | #import <ola/network/Socket.h> | ||
+ | |||
+ | using ola::network::PipeSocket; | ||
// define type for DMX message | // define type for DMX message | ||
Line 34: | Line 73: | ||
int main (int argc, const char * argv[]) { | int main (int argc, const char * argv[]) { | ||
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | ||
+ | |||
+ | // create a server | ||
+ | ola::network::SelectServer server; | ||
+ | |||
+ | // create a select server | ||
+ | ola::network::SelectServer server; | ||
+ | |||
+ | // create pipe socket | ||
+ | PipeSocket *socket = new PipeSocket(); | ||
+ | if (!socket->Init()) | ||
+ | { | ||
+ | NSLog(@"Cannot create pipe socket"); | ||
+ | return -1; | ||
+ | } | ||
+ | |||
+ | // add socket to select server | ||
+ | server.AddSocket(socket, true); | ||
+ | |||
+ | // create server daemon | ||
+ | // *** see: olad/Olad.cpp for more detail *** | ||
// get a client | // get a client | ||
− | ola:: | + | ola::OlaClient client(socket); |
− | if (! | + | if (!client.Setup()) { |
NSLog(@"Client setup failed %s", strerror(errno)); | NSLog(@"Client setup failed %s", strerror(errno)); | ||
return -1; | return -1; | ||
} | } | ||
− | |||
− | |||
− | |||
// prepare data | // prepare data | ||
Line 54: | Line 110: | ||
// send DMX message | // send DMX message | ||
int universe = 0; | int universe = 0; | ||
− | if (!client | + | if (!client.SendDmx(universe, buffer)) { |
NSLog(@"Sending DMX failed %s", strerror(errno)); | NSLog(@"Sending DMX failed %s", strerror(errno)); | ||
} | } | ||
+ | |||
+ | // cleanup | ||
+ | free(dmx); | ||
[pool drain]; | [pool drain]; | ||
return 0; | return 0; | ||
} | } | ||
− | |||
− |
Latest revision as of 09:49, 20 December 2014
You can use OLA to implement your application in Objective-C++, a bridge between Objective-C and C++. This page explains how to integrate OLA client into your program.
See also: http://www.bennigraf.de/wp-content/uploads/2012/11/writing-scola.pdf
Contents
What you'll need
- Xcode
- OLA for Mac OS X installed
Create a new Project
- File -> New Project, from the right under Mac OS X, select 'Application', then in the left panel choose 'Cocoa Application'
- Once the main window pops up, right click the 'main.m' file and select rename. Change the name to 'main.mm' (required for Objective-C++ extensions)
Configure your project
- Select menu: "Project" -> "Edit Project Settings". All settings below are referred in a "Build" tab
- Under 'Search Paths', set "Header Search Paths" to /usr/local/include (Source) or /opt/local/include (MacPorts) , depending on how you installed OLA.
Add the required libraries
- In your project tree, right-click on a group that contain frameworks (e.g. "External Frameworks and Libraries", "Frameworks/Linked Frameworks"), select "Add" -> "Existing Frameworks"
- Select following frameworks:
- libprotobuf.dylib
- libola.dylib
- libolacommon.dylib
Example code
The following code sends value 255 to channel 0 in universe 0. First, it creates a client from a SimpleClient object. Then, it creates a DMX buffer and sets channel 0 to 255. Finally it sends the buffer to a server.
#import <Cocoa/Cocoa.h> #import <ola/DmxBuffer.h> #import <ola/SimpleClient.h> int main(int argc, char *argv[]) { ola::SimpleClient simpleClient; if (!simpleClient.Setup()) { NSLog(@"Client setup failed"); return -1; } ola::OlaClient *client = simpleClient.GetClient(); ola::DmxBuffer buffer; buffer.Blackout(); buffer.SetChannel(0, 255); unsigned int universe = 0; if (!client->SendDmx(universe, buffer)) { NSLog(@"Sending DMX failed"); } return 0; // you could also run the main app here return NSApplicationMain(argc, (const char **) argv); }
You can find more information by browsing the source file of DMX example. For me, the src/ola-client.cpp is quite useful.
Here is another example that create OlaServer and communicate with a pipe socket.
#import <Foundation/Foundation.h> #include <errno.h> #import <ola/DmxBuffer.h> #import <ola/OlaClient.h> #import <ola/network/SelectServer.h> #import <ola/network/Socket.h> using ola::network::PipeSocket; // define type for DMX message typedef unsigned char dmx_t ; // maximum number of channels int MAXCHANNELS=512; int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // create a server ola::network::SelectServer server; // create a select server ola::network::SelectServer server; // create pipe socket PipeSocket *socket = new PipeSocket(); if (!socket->Init()) { NSLog(@"Cannot create pipe socket"); return -1; } // add socket to select server server.AddSocket(socket, true); // create server daemon // *** see: olad/Olad.cpp for more detail *** // get a client ola::OlaClient client(socket); if (!client.Setup()) { NSLog(@"Client setup failed %s", strerror(errno)); return -1; } // prepare data int channel = 0; dmx_t *dmx = (dmx_t *)calloc(MAXCHANNELS + 10, sizeof(dmx_t)); dmx[channel] = 255; ola::DmxBuffer buffer(dmx, MAXCHANNELS); // send DMX message int universe = 0; if (!client.SendDmx(universe, buffer)) { NSLog(@"Sending DMX failed %s", strerror(errno)); } // cleanup free(dmx); [pool drain]; return 0; }