Timer

The Timer class can measure the time passed. If the time limit is reached, it can execute a specified task.

Timer.swift:72
final class Timer

It is actually a software timer. You need to initialize it at first. It can operate as one-shot or periodic, with a customizable period.

/// Create a periodic timer whose period is 2000s.
let timer = Timer(period: 2000)

The timer can be really useful to carry out an operation after a speicified time using setInterrupt(start:_:). Here’a an example:

Example: Reverse the output value on a digital output pin

// Import the SwiftIO to use the related board functions.
import SwiftIO
// Import the MadBoard to decide which pin is used for the specific function.
import MadBoard

// Initialize a timer with a default period 1000ms.
let timer = Timer()
// Initialize the onboard green LED.
let led = DigitalOut(Id.GREEN)

// The setInterrupt function can be written as follow:
func toggleLed() {
   led.toggle()
}
timer.setInterrupt(toggleLed)

while true {
   sleep(ms: 1000)
}

or

import SwiftIO
import MadBoard

let timer = Timer()
let led = DigitalOut(Id.GREEN)

// Set interrupt with a closure.
timer.setInterrupt() {
   led.toggle()
}

while true {
   sleep(ms: 1000)
}