Classes
The following classes are available globally.
-
The AnalogIn class is used to read the external voltage applied to an analog input pin.
Example: Read and print the analog input value on a analog pin
See moreimport SwiftIO //Initialize an AnalogIn to analog pin A0. let pin = AnalogIn(Id.A0) //Read and print the analog input value every 1 second. while true { var value = pin.readVoltage() print("The analog input volatge is \(value)") sleep(ms: 1000) }
Declaration
Swift
public final class AnalogIn
-
The counter class can be used to count the external signal and measure the number of the pulse. It can detect the rising edge or both edges.
Attention
The maximum count value depends on the lowlevel hardware. For example, SwiftIO Board’s counter is 16bit, so the max count value is 65535. If the counter reaches the value, it will overflow and start from 0 again.Example: Read the count value every 10ms
import SwiftIO // Initiate the counter0. let counter = Counter(Id.C0) // Count and print the value every 10ms. Use wait here to get a more precise delay. while true { // Clear the counter to set the value to 0. counter.clear() wait(us: 10_000) // Read the value accumulated in 10ms. let value = counter.read() print("Conter value = \(value)") }
or
See moreimport SwiftIO // Initiate the counter0. let counter = Counter(Id.C0) // Initialize a timer to set interrupt. let timer = Timer() // Use the timer to read and print the value every 10ms. timer.setInterrupt(ms: 10) { let value = counter.read() // Clear the value to to 0. counter.clear() print("Conter value = \(value)") } while true { }
Declaration
Swift
public final class Counter
-
The DigitalIn class is intended to detect the state of a digital input pin. The input value is either true(1) or false(0).
Example: Read and print the input value on a digital input pin.
See moreimport SwiftIO //Initialize a DigitalIn to the digital pin D0. let pin = DigitalIn(Id.D0) //Read and print the input value every 1 second. while true { var value = pin.read() print("The input value is \(value)") sleep(ms: 1000) }
Declaration
Swift
public final class DigitalIn
-
The DigitalOut class is used to set a High or Low voltage output to a digital output pin. An initiation is required before using the member functions of this class.
Attention
The driving capability of the digital output pins is not very strong. It is meant to be a SIGNAL output and is not capable of driving a device requires large current.Example: Reverse the output value on a digital output pin
import SwiftIO // Initiate a DigitalOut to Pin 0. let pin = DigitalOut(Id.D0) // Reverse the output value every 1 second. while true { pin.toggle() sleep(ms: 1000) }
or
See moreimport SwiftIO // Initiate a DigitalOut to the onboard green LED. let greenLED = DigitalOut(Id.GREEN) // Toggle the output of the pin every 1 second using another member function. while true { greenLED.write(true) sleep(ms: 1000) greenLED.write(false) sleep(ms: 1000) }
Declaration
Swift
public final class DigitalOut
-
I2C (I square C) is a two wire protocol to communicate between different devices. The I2C class allows some operations through I2C protocol, including reading messages from a device and writing messages to a device. Currently the I2C ports support only master mode.
Note
Different I2C devices have different attributes. Please reference the device manual before using the functions below. This class allows the reading and writing of a byteUInt8
or an array of bytes[UInt8]
.Declaration
Swift
public final class I2C
-
The PWMOut class is used to vary the output voltage by controlling the duration of high output in the time periodCycles on the pin.
Attention
The PWM pins with same number are paired, like PWM3A and PWM3B. They can only share the same frequency.Example: Light a LED
See moreimport SwiftIO // Initiate a PWMOut to Pin PWM0A. let led = PWMOut(Id.PWM0A) // Set the brightness of the LED by setting the duty cycle. while true { led.setDutycycle(0.5) }
Declaration
Swift
public final class PWMOut
-
SPI is a four wire serial protocol for communication between devices.
See moreDeclaration
Swift
public final class SPI
-
The Timer class is used to set the occasion to raise the interrupt.
Example: Reverse the output value on a digital output pin
import SwiftIO let timer = Timer() let led = DigitalOut(Id.GREEN) // The setInterrupt function can be written as following: func toggleLed() { led.toggle() } timer.setInterrupt(ms: 1000, toggleLed) while true { }
or
See moreimport SwiftIO let timer = Timer() let led = DigitalOut(Id.GREEN) // Set interrupt with a closure timer.setInterrupt(ms: 1000) { led.toggle() } while true { }
Declaration
Swift
public final class Timer
-
UART is a two-wire serial communication protocol used to communicate with serial devices. The devices must agree on a common transmisson rate before communication.
See moreDeclaration
Swift
public final class UART