Template:PT3 OBJC MAC XCODE: Difference between revisions

From Phidgets Support
(Created page with "PT3_OBJC_MAC_XCODE")
 
No edit summary
 
Line 1: Line 1:
PT3_OBJC_MAC_XCODE
<div class="phd-slide-deck" data-deck="PT3_OBJC_MAC_XCODE">
<div class="phd-slide-deck-header"><h3>Language - Objective C</h3></div>
<div class="phd-slides">
<div class="phd-slide">
<h2>macOS with Xcode</h2>
 
<div class="phd-panels">
<div class="phd-panel phd-flex-column-align-center phd-flex-column-justify-middle">
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_OBJC_MAC_XCODE/200x-/50/xcode.jpg"></div>
</div>
<div class="phd-panel phd-flex-column-align-left phd-flex-column-justify-top">
<p>Welcome to using Phidgets with Objective C! By using Objective C, you will have access to the complete Phidget22 API, including events.</p>
<p>Xcode is an IDE provided by Apple that can be used to develop code in a wide variety of programming languages, including Objective C.</p>
</div>
</div>
</div>
 
<div class="phd-slide">
<h2>Requirements</h2>
 
<p>First, make sure you have the following installed:</p>
 
<p>● Phidgets Drivers for MacOS (see Part 1 of this user guide)</p>
 
<p>● <span>[https://developer.apple.com/xcode/ Xcode]</span> from the Mac App Store</p>
</div>
 
<div class="phd-slide">
<h2>Use Our Examples</h2>
 
<p>Next, download some of our sample code:</p>
 
<p>● <span>[https://www.phidgets.com/?view=code_samples&lang=Objective-C Objective-C Examples]</span></p>
 
<p>
Download the example(s) that correspond to your Phidget's channel classes. You can find them listed on the enclosure in most cases, or on the <b>API</b> tab of
the product page.
</p>
 
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_OBJC_MAC_XCODE/-x260/50/example.jpg"></div>
</div>
 
<div class="phd-slide">
<h2>Use Our Examples</h2>
 
<p>Start the example by pressing the <b>Run</b> button:</p>
 
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_OBJC_MAC_XCODE/Macos_RunExample.png"></div>
</div>
 
<div class="phd-slide">
<h2>Use Our Examples</h2>
 
<p>The application will attach to the Phidget and show you some basic information. Here is an example of a Digital Output channel on a RFID Phidget:</p>
 
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_OBJC_MAC_XCODE/Macos_DigitalOutputExample.png"></div>
 
<p>
You should now have the example up and running for your device. Play around with the device and experiment with some of the functionality. The next step is
configuring your project and writing your own code!
</p>
</div>
 
<div class="phd-slide">
<h2>Configure Your Project</h2>
 
<p>
When you are building a project from scratch, or adding Phidgets to an existing project, you'll need to configure your development environment to properly link
the Phidget iOS library. To begin, create a new Xcode project:
</p>
 
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_OBJC_MAC_XCODE/-x325/50/Cocoa_CreateProject.png"></div>
</div>
 
<div class="phd-slide">
<h2>Configure Your Project</h2>
 
<p>Next, select a macOS Cocoa application:</p>
 
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_OBJC_MAC_XCODE/-x350/50/Cocoa_Application.png"></div>
</div>
 
<div class="phd-slide">
<h2>Configure Your Project</h2>
 
<p>Name that project:</p>
 
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_OBJC_MAC_XCODE/-x350/50/Cocoa_NameProject.png"></div>
</div>
 
<div class="phd-slide">
<h2>Configure Your Project</h2>
 
<p>Navigate to your target's Build Settings and find the <b>Framework Search Path</b> setting:</p>
 
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_OBJC_MAC_XCODE/-x350/50/Macos_frameworksearch.png"></div>
</div>
 
<div class="phd-slide">
<h2>Configure Your Project</h2>
 
<p>Add a reference to <b>/Library/Frameworks</b> where the Phidget22 framework is installed:</p>
 
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_OBJC_MAC_XCODE/-x350/50/Macos_frameworkpath.png"></div>
</div>
 
<div class="phd-slide">
<h2>Configure Your Project</h2>
 
<p>
Next, navigate to the <b>Linked Frameworks and Libraries</b> setting under <b>General</b> and add a reference to the Phidget22 framework which is installed to
/Library/Frameworks:
</p>
 
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_OBJC_MAC_XCODE/-x325/50/Macos_linkframework.png"></div>
</div>
 
<div class="phd-slide">
<h2>Configure Your Project</h2>
 
<p>Finally, navigate to your header file and add a reference to phidget22.h</p>
 
<pre data-syntax-language="objectivec">#import <Phidget22/phidget22.h></pre>
 
<p>
Success! Your project now has access to Phidgets. Now that you have working examples and a project that is configured, we'll cover how the examples were written
so you can start writing your own code.
</p>
 
<p>Remember: your main reference for writing Objective C code will be:</p>
 
<p>● The <span>[https://www.phidgets.com/?view=api Phidget22 API]</span> Manual (select 'C' as the language)</p>
 
<p>● Objective C <span>[https://www.phidgets.com/?view=code_samples&lang=Objective-C example code]</span></p>
</div>
 
<div class="phd-slide">
<h2>Write Code</h2>
 
<h3>Step One: Initialize and Open</h3>
 
<p>You will need to declare your Phidget object in your code. For example, we can declare a digital input object like this:</p>
 
<pre data-syntax-language="objectivec">PhidgetDigitalInput ch;</pre>
 
<p>Next, the Phidget object needs to be initialized and opened.</p>
 
<pre data-syntax-language="objectivec">
PhidgetDigitalInput_create(&ch);
Phidget_open((PhidgetHandle)ch);</pre
>
</div>
 
<div class="phd-slide">
<h2>Write Code</h2>
 
<h3>Step One: Initialize and Open</h3>
 
<p>We won't go into detail on it in this guide, but this is the previous code with error handling:</p>
 
<pre data-syntax-language="objectivec">
PhidgetReturnCode res;
const char* errorString;
 
res = PhidgetDigitalInput_create(&ch);
if(res != EPHIDGET_OK){
  Phidget_getErrorDescription ( returnValue, &errorString );
  NSLog(@"Handle error here");
}
 
res = Phidget_open((PhidgetHandle)ch);
if(res != EPHIDGET_OK){
  Phidget_getErrorDescription ( returnValue, &errorString );
  NSLog(@"Handle error here");
}</pre
>
</div>
 
<div class="phd-slide">
<h2>Write Code</h2>
 
<h3>Step Two: Wait for Attachment of the Phidget</h3>
 
<p>
Simply calling open does not guarantee you can use the Phidget immediately. To use a Phidget, it must be plugged in (attached). We can handle this by using
event driven programming and tracking the attach events. Alternatively, we can modify our code so we wait for an attachment:
</p>
 
<pre data-syntax-language="objectivec">
PhidgetDigitalInput_create(&ch);
Phidget_openWaitForAttachment(ch, 5000);</pre
>
 
<p>Waiting for attachment will block indefinitely until a connection is made, or until the timeout value is exceeded.</p>
 
<p>To use events to handle attachments, we have to modify our code slightly:</p>
 
<pre data-syntax-language="objectivec">
PhidgetDigitalInput_create(&ch);
Phidget_setOnAttachHandler((PhidgetHandle)ch,gotAttach,(__bridge void*)self);
Phidget_open((PhidgetHandle)ch);</pre
>
</div>
 
<div class="phd-slide">
<h2>Write Code</h2>
 
<h3>Step Two: Wait for Attachment of the Phidget</h3>
 
<p>Next, we have to declare the function that will be called when an attach event is fired - in this case the function <b>gotAttach</b> will be called:</p>
 
<pre data-syntax-language="objectivec">
static void gotAttach(PhidgetHandle phid, void *context){
    [(__bridge id)context performSelectorOnMainThread:@selector(deviceAttached)
                                          withObject:nil
                                        waitUntilDone:NO];
}</pre
>
</div>
 
<div class="phd-slide">
<h2>Write Code</h2>
 
<h3>Step Three: Do Things with the Phidget</h3>
 
<p>
We recommend the use of event driven programming when working with Phidgets. In a similar way to handling an attach event as described above, we can also add an
event handler for a state change event:
</p>
 
<pre data-syntax-language="objectivec">
PhidgetDigitalInput_create(&ch);
Phidget_setOnAttachHandler((PhidgetHandle)ch,gotAttach,(__bridge void*)self);
PhidgetDigitalInput_setOnStateChangeHandler(ch, gotStateChange, (__bridge void*)self);
Phidget_open((PhidgetHandle)ch);</pre
>
 
<p>
This code will connect a function and an event. In this case, the <b>gotStateChange</b> function will be called when there has been a change to the devices
input. Next, we need to create the <b>gotStateChange</b> function.
</p>
 
<pre data-syntax-language="objectivec">
void gotStateChange(PhidgetDigitalInputHandle phid, void *context, int state){
        [(__bridge id)context performSelectorOnMainThread:@selector(onStateChangeHandler:)
                                              withObject:[NSNumber numberWithInt:state]
                                            waitUntilDone:NO];
}</pre
>
 
<p>Above, the onStateChangeHandler method is invoked on the main thread. Event data is stored as an NSNumber.</p>
</div>
 
<div class="phd-slide">
<h2>Write Code</h2>
 
<h3>Step Three: Do Things with the Phidget</h3>
 
<p>The method <b>onStateChangeHandler</b> is defined as follows:</p>
 
<pre data-syntax-language="objectivec">
- (void)onStateChangeHandler:(NSNumber *)state{
    if(state.intValue)
        stateTextField.stringValue = @"True";
    else
        stateTextField.stringValue = @"False";
}</pre
>
 
<p>If events do not suit your needs, you can also poll the device directly for data using code like this:</p>
 
<pre data-syntax-language="objectivec">
int state;
 
PhidgetDigitalInput_getState(ch, &state);
stateTextField.stringValue = [NSString stringWithFormat:@"%@", state ? @"True" : @"False"];</pre
>
</div>
 
<div class="phd-slide">
<h2>Write Code</h2>
 
<h3>Step Four: Close and Delete</h3>
 
<p>At the end of your program, be sure to close and delete your device:</p>
 
<pre data-syntax-language="objectivec">
Phidget_close((PhidgetHandle)ch);
PhidgetDigitalInput_delete(&ch);</pre
>
</div>
 
<div class="phd-slide">
<h2>What's Next?</h2>
 
<p>{{Slide_Deck_PT3_Outro}}</p>
</div>
</div>
 
<div class="phd-navigation-buttons">
<div class="phd-simple-button phd-to-slide-button" data-navigation-direction="prev">«</div>
<div class="phd-progress-container">
<div class="phd-progress-bar">
<div class="phd-progress-slide-info"></div>
</div>
</div>
 
<div class="phd-simple-button phd-to-slide-button" data-navigation-direction="next">»</div>
</div>
<div hidden class="phd-hidden-breadcrumb-label" data-label="Xcode"></div>
</div>

Latest revision as of 21:47, 8 March 2021

macOS with Xcode

Welcome to using Phidgets with Objective C! By using Objective C, you will have access to the complete Phidget22 API, including events.

Xcode is an IDE provided by Apple that can be used to develop code in a wide variety of programming languages, including Objective C.

Requirements

First, make sure you have the following installed:

● Phidgets Drivers for MacOS (see Part 1 of this user guide)

Xcode from the Mac App Store

Use Our Examples

Next, download some of our sample code:

Objective-C Examples

Download the example(s) that correspond to your Phidget's channel classes. You can find them listed on the enclosure in most cases, or on the API tab of the product page.

Use Our Examples

Start the example by pressing the Run button:

Use Our Examples

The application will attach to the Phidget and show you some basic information. Here is an example of a Digital Output channel on a RFID Phidget:

You should now have the example up and running for your device. Play around with the device and experiment with some of the functionality. The next step is configuring your project and writing your own code!

Configure Your Project

When you are building a project from scratch, or adding Phidgets to an existing project, you'll need to configure your development environment to properly link the Phidget iOS library. To begin, create a new Xcode project:

Configure Your Project

Next, select a macOS Cocoa application:

Configure Your Project

Name that project:

Configure Your Project

Navigate to your target's Build Settings and find the Framework Search Path setting:

Configure Your Project

Add a reference to /Library/Frameworks where the Phidget22 framework is installed:

Configure Your Project

Next, navigate to the Linked Frameworks and Libraries setting under General and add a reference to the Phidget22 framework which is installed to /Library/Frameworks:

Configure Your Project

Finally, navigate to your header file and add a reference to phidget22.h

#import <Phidget22/phidget22.h>

Success! Your project now has access to Phidgets. Now that you have working examples and a project that is configured, we'll cover how the examples were written so you can start writing your own code.

Remember: your main reference for writing Objective C code will be:

● The Phidget22 API Manual (select 'C' as the language)

● Objective C example code

Write Code

Step One: Initialize and Open

You will need to declare your Phidget object in your code. For example, we can declare a digital input object like this:

PhidgetDigitalInput ch;

Next, the Phidget object needs to be initialized and opened.

PhidgetDigitalInput_create(&ch);
Phidget_open((PhidgetHandle)ch);

Write Code

Step One: Initialize and Open

We won't go into detail on it in this guide, but this is the previous code with error handling:

PhidgetReturnCode res;
const char* errorString;

res = PhidgetDigitalInput_create(&ch);
if(res != EPHIDGET_OK){
  Phidget_getErrorDescription ( returnValue, &errorString );
  NSLog(@"Handle error here");
}

res = Phidget_open((PhidgetHandle)ch);
if(res != EPHIDGET_OK){
  Phidget_getErrorDescription ( returnValue, &errorString );
   NSLog(@"Handle error here");
}

Write Code

Step Two: Wait for Attachment of the Phidget

Simply calling open does not guarantee you can use the Phidget immediately. To use a Phidget, it must be plugged in (attached). We can handle this by using event driven programming and tracking the attach events. Alternatively, we can modify our code so we wait for an attachment:

PhidgetDigitalInput_create(&ch);
Phidget_openWaitForAttachment(ch, 5000);

Waiting for attachment will block indefinitely until a connection is made, or until the timeout value is exceeded.

To use events to handle attachments, we have to modify our code slightly:

PhidgetDigitalInput_create(&ch);
Phidget_setOnAttachHandler((PhidgetHandle)ch,gotAttach,(__bridge void*)self);
Phidget_open((PhidgetHandle)ch);

Write Code

Step Two: Wait for Attachment of the Phidget

Next, we have to declare the function that will be called when an attach event is fired - in this case the function gotAttach will be called:

static void gotAttach(PhidgetHandle phid, void *context){
    [(__bridge id)context performSelectorOnMainThread:@selector(deviceAttached)
                                           withObject:nil
                                        waitUntilDone:NO];
}

Write Code

Step Three: Do Things with the Phidget

We recommend the use of event driven programming when working with Phidgets. In a similar way to handling an attach event as described above, we can also add an event handler for a state change event:

PhidgetDigitalInput_create(&ch);
Phidget_setOnAttachHandler((PhidgetHandle)ch,gotAttach,(__bridge void*)self);
PhidgetDigitalInput_setOnStateChangeHandler(ch, gotStateChange, (__bridge void*)self);
Phidget_open((PhidgetHandle)ch);

This code will connect a function and an event. In this case, the gotStateChange function will be called when there has been a change to the devices input. Next, we need to create the gotStateChange function.

void gotStateChange(PhidgetDigitalInputHandle phid, void *context, int state){
        [(__bridge id)context performSelectorOnMainThread:@selector(onStateChangeHandler:)
                                               withObject:[NSNumber numberWithInt:state]
                                            waitUntilDone:NO];
}

Above, the onStateChangeHandler method is invoked on the main thread. Event data is stored as an NSNumber.

Write Code

Step Three: Do Things with the Phidget

The method onStateChangeHandler is defined as follows:

- (void)onStateChangeHandler:(NSNumber *)state{
    if(state.intValue)
        stateTextField.stringValue = @"True";
    else
        stateTextField.stringValue = @"False";
}

If events do not suit your needs, you can also poll the device directly for data using code like this:

int state;

PhidgetDigitalInput_getState(ch, &state);
stateTextField.stringValue = [NSString stringWithFormat:@"%@", state ? @"True" : @"False"];

Write Code

Step Four: Close and Delete

At the end of your program, be sure to close and delete your device:

Phidget_close((PhidgetHandle)ch);
PhidgetDigitalInput_delete(&ch);

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

Continue reading below for advanced information and troubleshooting for your device.

«
»