Timer

public final class Timer

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

import SwiftIO

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

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

while true {
}
  • Intialize a timer.

    Declaration

    Swift

    public init()
  • Execute a designated task at a scheduled time interval. The task should be executed in a very short time, usually in nanoseconds.

    Declaration

    Swift

    public func setInterrupt(ms period: Int,
                            mode: Mode = .period,
                            start: Bool = true,
                            _ callback: @escaping ()->Void)

    Parameters

    ms

    REQUIRED The time period set for the interrupt.

    mode

    OPTIONAL The times that the interrupt will occur: once or continuous.

    start

    OPTIONAL By default, the interrupt will start directly to work.

    callback

    REQUIRED A void function without a return value.

  • Start the timer.

    Declaration

    Swift

    public func start()
  • Stop the timer.

    Declaration

    Swift

    public func stop()
  • Clear the timer. The timer will be reset to inital value.

    Declaration

    Swift

    public func clear()
  • There are two timer modes: if set to oneShot, the interrupt happens only once; if set to period, the interrupt happens continuously.

    See more

    Declaration

    Swift

    public enum Mode : UInt8