DigitalOut
public final class DigitalOut
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
import 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)
}
-
Initialize a DigitalOut to a specific output pin.
Usage Example
// The most simple way of initiating a pin D0, with all other parameters set to default. let outputPin0 = DigitalOut(Id.D0) // Initialize the pin D1 with the output mode openDrain. let outputPin1 = DigitalOut(Id.D1, mode: .openDrain) // Initialize the pin D2 with a High voltage output. let outputPin2 = DigitalOut(Id.D2, value: true) // Initialize the pin D3 with the openDrain mode and a High voltage output. let outputPin3 = DigitalOut(Id.D3, mode: .openDrain, value: true)
Parameters
id
REQUIRED The name of output pin. Reference the Id enumerate.
mode
OPTIONAL The output mode of the pin.
.pushPull
by default.value
OPTIONAL The output value after initiation.
false
by default. -
Return the current output mode in a format of DigitalOut.Mode enumerate.
Usage Example
let pin = DigitalOut(Id.D0) if pin.getMode() == .pushPull { //do something }
Declaration
Swift
public func getMode() -> Mode
Return Value
The current mode:
.pushPull
or.openDrain
. -
Change the output mode.
Declaration
Swift
public func setMode(_ mode: Mode)
Parameters
mode
The output mode:
.pushPull
or.openDrain
. -
Set the output value of the specific pin: true for high voltage and false for low voltage.
Declaration
Swift
@inline(__always) public func write(_ value: Bool)
Parameters
value
The output value:
true
orfalse
. -
Reverse the current output value of the specific pin.
Declaration
Swift
@inline(__always) public func toggle()
-
Get the current output value in Boolean format.
Attention
The return value of this function has nothing to do with the actual output of the pin. For example, a pin is set totrue
but it is short to ground. The actual pin voltage would be low. This function will still returntrue
despite of the actual low output, since this pin is set to HIGH.Declaration
Swift
public func getValue() -> Bool
Return Value
true
orfalse
of the logic value. -
The Mode enumerate includes the available output modes. The default output mode in most cases is pushPull. The pushPull mode enables the digital pin to output high and low voltage levels while the open-drain output cannot truly output a high level.
See moreDeclaration
Swift
public enum Mode : UInt8