Skip to content

Commit 1fff004

Browse files
committed
Updated SPM logic.
1 parent 98f9aae commit 1fff004

File tree

5 files changed

+28
-42
lines changed

5 files changed

+28
-42
lines changed

examples/directMemory.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ func loopGPIO() {
77
let file = open("/dev/mem", O_RDWR | O_SYNC);
88
defer { close(file) }
99

10-
1110
// This is the base memory address for *all* peripherals of the
1211
// Rasbperry Pi (I2C, SPI, GPIO, UART, etc)
1312
let peripheralsBaseAddress = 0x3f000000
@@ -22,6 +21,7 @@ func loopGPIO() {
2221
// Open the /dev/mem file (all memory) with read and write
2322
// privileges (we want to turn on output pins) at the proper
2423
// offset (we don't care about the rest of the addresses)
24+
// 0x3f200000
2525
guard let rawPointer = mmap(nil, 1024 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, file, gpioBaseAddress) else {
2626
perror("Cannot mmap bytes for path")
2727
return

source/Package.pins

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
{
22
"autoPin": true,
33
"pins": [
4-
4+
{
5+
"package": "SwiftyGPIO",
6+
"reason": null,
7+
"repositoryURL": "https://github.com/uraimo/SwiftyGPIO.git",
8+
"version": "0.9.11"
9+
}
510
],
611
"version": 1
712
}

source/Sources/Home/AnalogReader.swift

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public struct AnalogReader {
3838

3939
shortSleep()
4040
let value = i2c.readWord(0x48, command: 0)
41+
print("Value: \(value)")
4142
// let values = i2c.readData(0x48, command:0)
4243
// let high = value & 0xFF
4344
// let low = (value >> 8) & 0xFF

source/Sources/Home/LightSwitch.swift

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
import SwiftyGPIO
22

33
public struct LightSwitch {
4-
let pin13: GPIO
5-
let pin7: GPIO
4+
let gpio4: GPIO // LED
5+
let gpio6: GPIO // Relay
66

7-
public init() {
7+
public init?() {
88
print("New light switch.")
99
let gpios = SwiftyGPIO.GPIOs(for:.RaspberryPi3)
10-
pin13 = gpios[.P27]!
11-
pin7 = gpios[.P4]!
12-
pin13.direction = .OUT
13-
pin7.direction = .OUT
10+
guard let gpio4 = gpios[.P4] else { print("Invalid GPIO: .P4"); return nil }
11+
self.gpio4 = gpio4
12+
guard let gpio6 = gpios[.P6] else { print("Invalid GPIO: .P6"); return nil }
13+
self.gpio6 = gpio6
14+
gpio4.direction = .OUT
15+
gpio6.direction = .OUT
1416
}
1517

1618
public func on() {
17-
pin13.value = 1
18-
pin7.value = 1
19+
gpio4.value = 1
20+
gpio6.value = 1
1921
}
2022

2123
public func off() {
22-
pin13.value = 0
23-
pin7.value = 0
24+
gpio4.value = 0
25+
gpio6.value = 0
2426
}
2527
}
+7-29
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
1+
//
2+
// main.swift
3+
//
14

25
import Foundation
36
import SwiftyGPIO
47
import Home
58

6-
extension UInt8 {
7-
func hex() -> String {
8-
return String(format: "%02x", self)
9-
}
10-
}
11-
12-
extension Int {
13-
func hex() -> String {
14-
return String(format: "%02x", self)
15-
}
16-
}
17-
extension UInt16 {
18-
func hex() -> String {
19-
// 0x4e07
20-
let low = self & 0xFF
21-
let high = (self >> 8) & 0xFF
22-
return "0x" + String(format: "%02x", high) + String(format: "%02x", low)
23-
// return "0x" + String(high, radix: 16, uppercase: true) + String(low, radix: 16, uppercase: true)
24-
}
25-
}
26-
27-
func shortSleep() {
28-
usleep(100000)
29-
}
30-
319
public func i2c() {
3210
let analogReader = AnalogReader()
3311

@@ -38,7 +16,7 @@ public func i2c() {
3816
}
3917

4018
func switch_toggle() {
41-
let light = LightSwitch()
19+
guard let light = LightSwitch() else { print("Failed to create switch"); return }
4220

4321
for _ in 1...5 {
4422
print("On.")
@@ -50,10 +28,10 @@ func switch_toggle() {
5028
}
5129
}
5230

53-
// switch_toggle()
54-
i2c()
31+
switch_toggle()
32+
//i2c()
5533

5634
func main() -> Int {
57-
print("Hey yo. Hi.")
35+
print("Main")
5836
return 0
5937
}

0 commit comments

Comments
 (0)