ZorbDevice
final public class ZorbDevice
Class encapsulating the all necessary functions for a given device peripheral
-
Differentiates between different hardware versions
See moreDeclaration
Swift
public enum Version: Int
-
Called to initiate connection with a given zorb device
Usage Example:
// Create a `ZorbDevice` object for a given Bluetooth peripheral (or this object may have // already been created as in the case of using `retrieveAvailableDevices` method of SDK let device = ZorbDevice(with: peripheral) // Attempts connection to an advertising device device.connect { result in switch result { case .success: // Connect succeeded case .failure(let error): // An error occurred during connection } }
Declaration
Swift
public func connect(completion: @escaping ConnectPeripheralCallback)
-
Called to end connection with a given zorb device
Usage Example:
// Create a `ZorbDevice` object for a given Bluetooth peripheral (or this object may have // already been created as in the case of using `retrieveAvailableDevices` method of SDK let device = ZorbDevice(with: peripheral) // After calling this method, device disconnection will be guaranteed device.disconnect()
Declaration
Swift
public func disconnect()
-
Writes the appropriate command to reset given device’s Javascript virtual machine.
Usage Example:
// Create a `ZorbDevice` object for a given Bluetooth peripheral (or this object may have // already been created as in the case of using `retrieveAvailableDevices` method of SDK let device = ZorbDevice(with: peripheral) // Reset device SDK environment device.reset { result in switch result { case .success: // Reset succeeded case .failure(let error): // An error occurred during reset } }
Declaration
Swift
public func reset(completion: @escaping WriteRequestCallback)
-
Obtains the current revision version of the firmware on the device
Usage Example:
// Create a `ZorbDevice` object for a given Bluetooth peripheral (or this object may have // already been created as in the case of using `retrieveAvailableDevices` method of SDK let device = ZorbDevice(with: peripheral) // Read version from device device.readVersion { result in switch result { case .success(let version): // Reading version string succeeded case .failure(let error): // An error occurred during read } }
Declaration
Swift
public func readVersion(completion: @escaping (SwiftyBluetooth.Result<String>) -> Void)
-
Obtains the current serial number of the paired device
Usage Example:
// Create a `ZorbDevice` object for a given Bluetooth peripheral (or this object may have // already been created as in the case of using `retrieveAvailableDevices` method of SDK let device = ZorbDevice(with: peripheral) // Read serial from device device.readSerial { result in switch result { case .success(let serial): // Reading version string succeeded case .failure(let error): // An error occurred during read } }
Declaration
Swift
public func readSerial(completion: @escaping (SwiftyBluetooth.Result<String>) -> Void)
-
Writes desired actuator data to given device.
Usage Example:
// Create a `ZorbDevice` object for a given Bluetooth peripheral (or this object may have // already been created as in the case of using `retrieveAvailableDevices` method of SDK let device = ZorbDevice(with: peripheral) // Write data to device actuators device.writeActuators(duration: 100, topLeft: 0, topRight: 0, bottomLeft: 25, bottomRight: 25) { result in switch result { case .success: // Write succeeded case .failure(let error): // An error occurred during write } }
Declaration
Swift
public func writeActuators(duration: UInt16, topLeft: UInt8, topRight: UInt8, bottomLeft: UInt8, bottomRight: UInt8, completion: @escaping WriteRequestCallback)
Parameters
duration
The total duration, in milliseconds for the given set of vibrations to last.
topLeft
Intensity, in a range from 0 to 100, for the top left actuator to be set at.
topRight
Intensity, in a range from 0 to 100, for the top right actuator to be set at.
bottomLeft
Intensity, in a range from 0 to 100, for the bottom left actuator to be set at.
bottomRight
Intensity, in a range from 0 to 100, for the bottom right actuator to be set at.
-
Writes a given string of Javascript to the given device. Using this method requires internet connection, which is used to compile the Javascript to bytecode before transmission.
Usage Example:
// Create a `ZorbDevice` object for a given Bluetooth peripheral (or this object may have // already been created as in the case of using `retrieveAvailableDevices` method of SDK let device = ZorbDevice(with: peripheral) // Write Javascript to device let javascript = "new Zorb.Vibration(" + "0," + "new Zorb.Effect(0,100,11,250)," + "213" + ").start();" device.writeJavascript(javascript) { result in switch result { case .success: // Write succeeded case .failure(let error): // An error occurred during write } }
Declaration
Swift
public func writeJavascript(_ javascript: String, completion: @escaping WriteRequestCallback)
Parameters
javascript
The Javascript code to be written
-
Writes the Javascript code at a given URL to the given device.
Usage Example:
// Create a `ZorbDevice` object for a given Bluetooth peripheral (or this object may have // already been created as in the case of using `retrieveAvailableDevices` method of SDK let device = ZorbDevice(with: peripheral) // Write Javascript from url to device let url = URL(string: "https://gist.githubusercontent.com/jakerockland/17cb9cbfda0e09fa8251fc7666e2c4dc/raw")! device.writeJavascript(at url) { result in switch result { case .success: // Write succeeded case .failure(let error): // An error occurred during write } }
Declaration
Swift
public func writeJavascript(at url: URL, completion: @escaping WriteRequestCallback)
Parameters
url
A URL to the hosted Javascript script to be written
-
Writes a Zorb_Timeline object (as specified by zorb.pb.swift) to the UART RX characteristic.
Usage Example:
// Create a `ZorbDevice` object for a given Bluetooth peripheral (or this object may have // already been created as in the case of using `retrieveAvailableDevices` method of SDK let device = ZorbDevice(with: peripheral) // Create Zorb_Timeline and Zorb_Vibration objects using the generated types provided by zorb.pb.swift. var timeline = Zorb_Timeline() var vibration = Zorb_Vibration() // Populate data in the Zorb_Vibration object. vibration.channels = 0x01 vibration.delay = 1000 vibration.duration = 2000 vibration.position = 0 vibration.start = 0 vibration.end = 100 vibration.easing = 2 // Append the Zorb_Vibration to the Zorb_Timeline. timeline.vibrations.append(vibration) // Write the bytecode to the UART RX characteristic. device.writeTimeline(timeline) { result in switch result { case .success: // Write succeeded case .failure(let error): // An error occurred during write } }
Declaration
Swift
public func writeTimeline(_ timeline: Zorb_Timeline, completion: @escaping WriteRequestCallback)
Parameters
timeline
A Zorb_Timeline object (from zorb-protocol)
-
Writes a given string of base64 encoded bytecode to the given device.
Usage Example:
// Create a `ZorbDevice` object for a given Bluetooth peripheral (or this object may have // already been created as in the case of using `retrieveAvailableDevices` method of SDK let device = ZorbDevice(with: peripheral) // Write bytecode to device let bytecode = "BgAAAFAAAAAsAAAAAQAAAAQAAQABAAUAAAEDBAYAAQACAAYAOwABKQIDxEYBAAAABAABACEAAwABAgMDAAAGAAgAOwECt8gARgAAAAAAAAAFAAAAAAAAAAIAb24JAHRpbWVydGljawABAHQABgBNb21lbnQGAHVwdGltZQ==" device.writeBytecode(bytecode) { result in switch result { case .success: // Write succeeded case .failure(let error): // An error occurred during write } }
Declaration
Swift
public func writeBytecodeString(_ bytecode: String, completion: @escaping WriteRequestCallback)
Parameters
bytecode
The base64 encoded representation of pre-compiled Javascript bytecode to be written
-
Triggers a given pre-loaded pattern on the given device
Usage Example:
device.triggerPattern(.🎊) { result in switch result { case .success: // Pattern triggered successfully case .failure(let error): // An error occurred in triggering pattern } }
Declaration
Swift
public func triggerPattern(_ pattern: Trigger, completion: @escaping WriteRequestCallback)
Parameters
pattern
The
Trigger
enumeration option of the given preloaded pattern to trigger