DigitalIn

public final class DigitalIn

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.

import 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)
}
  • Initialize a DigitalIn to a specified pin.

    Usage Example

    // The most simple way of initiating a pin D0, with all other parameters set to default.
    let pin = DigitalIn(Id.D0)
    
    // Initialize the pin D0 with the pulldown mode.
    let pin = DigitalIn(Id.D0, mode: .pullDown)
    

    Declaration

    Swift

    public init(_ id: IdName,
                   mode: Mode = .pullDown)

    Parameters

    id

    REQUIRED The Digital id on the board. See Id for reference.

    mode

    OPTIONAL The input mode. .pullDown by default.

  • Get the current input mode on a specified pin.

    Declaration

    Swift

    public func getMode() -> Mode

    Return Value

    The current input mode: .pullUp, .pullDown or .pullNone.

  • Set the input mode for a digital input pin.

    Declaration

    Swift

    public func setMode(_ mode: Mode)

    Parameters

    mode

    The input mode.

  • Read the value from a digital input pin.

    Attention

    Dependind on the hardware, the internal pull resister may be very weak. Don’t just rely on the pull resister for reading the value. Especially when you just changed the input mode, the internel pad need some time to charge or discharge through the pull resister!

    Declaration

    Swift

    @inline(__always)
    public func read() -> Bool

    Return Value

    true or false of the logic value.

  • Add a callback function to a specified digital pin to set interrupt by detcting the changes of the signal. Once the risng or falling edge is detected, the processor will suspend the normal execution to execute the designated task.

    The task should be able to finish in a very short time, usually in nanoseconds. Then, the processor will return back to where it stopped and continue the previous operation.

    Declaration

    Swift

    public func setInterrupt(_ mode: InterruptMode, enable: Bool = true, callback: @escaping () -> Void)

    Parameters

    mode

    The interrupt mode to detect rising or falling edge.

    enable

    Whether to enable the interrupt.

    callback

    A void function without a return value.

  • Trigger the interrupt after detecting the edge.

    Declaration

    Swift

    public func enableInterrupt()
  • Disable the interrupt until the interrupt state is changed.

    Declaration

    Swift

    public func disableInterrupt()
  • Check whether the interrupt is enabled.

    Declaration

    Swift

    public func getInterruptState() -> InterruptState

    Return Value

    The input mode: .enable or .diable.

  • Remove the interrupt.

    Declaration

    Swift

    public func removeInterrupt()
  • The digital input modes can change the default state (high, low or floating) of a pin by using the pull resistors.

    Attention

    The pins D26 to D37 are connected separately to an external 10kΩ resistor on the board. So even if they are changed to pullUp, the output voltage of these pins is still low.
    See more

    Declaration

    Swift

    public enum Mode : UInt8
  • The interrupt mode determines the edge to raise the interrupt: rising, falling or both edges. A rising edge is the transition of a digital input signal from high to low and a falling edge is from low to high.

    See more

    Declaration

    Swift

    public enum InterruptMode : UInt8
  • The interrupt state determines whether the interrupt will be enabled and occur.

    See more

    Declaration

    Swift

    public enum InterruptState : UInt8