Skip to content

Commit e9549d9

Browse files
committed
Add convenience initializers so this adds more value and makes consuming programs simpler.
1 parent 7c257a8 commit e9549d9

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ This makes it easier to use LEDs with Swift.
55

66
Here is a small example that will turn on an LED that is connected to GPIO pin 17 on a Raspberry Pi 3.
77
```
8+
let led = LED(gpioPin: .P17)
9+
led.value = 1
10+
```
11+
12+
Here is a more verbose example, that is equivalent to the first example:
13+
```
814
let gpios = SwiftyGPIO.GPIOs(for:.RaspberryPi3)
915
let led = LED(gpioPin: .P17, swiftyGPIOs: gpios)
1016
led.value = 1

Sources/led.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,26 @@ public class LED {
2626
}
2727
}
2828

29-
public convenience init(gpioPin: GPIOName, forBoard board: SupportedBoard) {
29+
30+
/**
31+
Initializes a LED, the easy way.
32+
33+
- Parameters:
34+
- gpioPin: The pin that LED is connected to. Use a GPIOName type. Ex: .P17 for GPIO pin 17.
35+
- board: The board that is supported. By default, it is a .RaspberryPi3.
36+
*/
37+
public convenience init(gpioPin: GPIOName, forBoard board: SupportedBoard = .RaspberryPi3) {
3038
let swiftyGPIOs = SwiftyGPIO.GPIOs(for: board)
3139
self.init(gpioPin: gpioPin, swiftyGPIOs: swiftyGPIOs)
3240
}
33-
41+
42+
/**
43+
Initializes a LED.
44+
45+
- Parameters:
46+
- gpioPin: The pin that LED is connected to. Use a GPIOName type. Ex: .P17 for GPIO pin 17.
47+
- swiftyGPIOs: A dictionary created by SwiftyGPIO.GPIOs(for: .SupportedBoard).
48+
*/
3449
public init(gpioPin: GPIOName, swiftyGPIOs: [GPIOName: GPIO]) {
3550
self.gpioPin = gpioPin
3651

Tests/ledTests/ledTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ class LEDTests: XCTestCase {
5757
func testSimplerConstructor() {
5858
let led = LED(gpioPin: .P17, forBoard: .RaspberryPi3)
5959
XCTAssertTrue(led.isOff())
60+
XCTAssertEqual(.P17, led.gpioPin)
61+
XCTAssertNotNil(led.gpio)
62+
}
63+
64+
func testSimplerConstructorWithDefaultRaspberryPi3Pins() {
65+
let led = LED(gpioPin: .P17)
66+
XCTAssertTrue(led.isOff())
67+
XCTAssertEqual(.P17, led.gpioPin)
68+
XCTAssertNotNil(led.gpio)
6069
}
6170

6271
static var allTests = [

0 commit comments

Comments
 (0)