Objective-C is compatible to C. This allows to use the C/C++ bindings in an iOS App. For general information see the documentation about the C/C++ bindings, this page only covers iOS specific things.
In the following we assume that you already have the iOS development environment installed.
As an example we will create a small project that can toggle a Dual Relay Bricklet. It should be easy to adjust this example for your needs.
Start a new Xcode project by clicking on:
Add the C/C++ bindings code:
source/
folder of the C/C++ bindingsBelow is a small example program that turns a relay on and off with a toggle button.
Edit AppDelegate.h
as shown below and add the two variables for the IPConnection
and DualRelay objects and the toggleRelays
Interface Builder action.
#import <UIKit/UIKit.h>
#include "ip_connection.h"
#include "bricklet_dual_relay.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
IPConnection ipcon;
DualRelay dr;
}
@property (strong, nonatomic) UIWindow *window;
- (IBAction)toggleRelays;
@end
Edit AppDelegate.m
as shown below to create the IPConnection and DualRelay
objects after the App is launched. For simplicity no error handling is done here.
In the toggleRelays
action the state of both relays is switched.
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Change to the IP address of your host
ipcon_create(&ipcon);
dual_relay_create(&dr, "XYZ", &ipcon); // Change XYZ to the UID of your Dual Relay Bricklet
ipcon_connect(&ipcon, "192.168.178.46", 4223);
dual_relay_set_state(&dr, true, true);
return YES;
}
- (IBAction) toggleRelays
{
// Get state of both relays and toogle it
bool state1, state2;
dual_relay_get_state(&dr, &state1, &state2);
dual_relay_set_state(&dr, !state1, !state2);
}
@end
Now open MainStoryboard.storyboard
in the Interface Builder and add a Label and
a on/off Switch object as shown in the screenshot. The last step is to connect the
Switch Value Changed event with the toggleRelays action:
Test the App in the simulator by clicking the Run button. Don't forget to change the UID and the host IP address to the correct values for your brickd host and your Dual Relay Bricklet.