Template:Language - Swift Write Code

From Phidgets Support
Revision as of 15:51, 13 September 2018 by Jdecoux (talk | contribs) (Created page with "{{WriteCode_Intro|Swift|Swift}} === Step One: Initialize and Open === You will need to declare your Phidget object in your code. For example, we can declare a digital input o...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

By following the instructions for your operating system and compiler above, you now have working examples and a project that is configured. This teaching section will help you understand how the examples were written so you can start writing your own code.


Remember: your main reference for writing Swift code will be the Phidget22 API Manual and the example 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:

let ch = DigitalInput()


Next, the Phidget object needs to be opened:

ch.open()


Although we are not including it on this page, you need to include error handling for all Phidget functions. Here is an example of the previous code with error handling:

do{
  try ch.open
}catch let error as PhidgetError{
  //handle error
}

Step Two: Wait for Attachment (Plugging In) 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:

ch.open(timeout: 5000)

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


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

ch.attach.addHandler(attach_handler)
Phidget_open(ch)

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

func attach_handler(sender: Phidget){
  let attachedDevice = sender as! DigitalInput
  //configure device here
}

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:

ch.attach.addHandler(attach_handler)
ch.stateChange.addhandler(stateChange_handler)
ch.open()

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

func stateChange_handler(sender: DigitalInput, state: Bool){
  if(state){
    //state is true
  }
  else{
    //State is false
  }
}


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

var state = ch.getState()
stateLabel.text = state ? "True" : "False"

Step Four: Close

At the end of your program, be sure to close your device.

ch.close()