Skip to content

Commit 2caef32

Browse files
committed
Update for latest pin numbers.
1 parent 1fff004 commit 2caef32

13 files changed

+50
-83
lines changed

examples/a.out

-444 Bytes
Binary file not shown.

examples/direct

11.4 KB
Binary file not shown.

examples/directMemory.swift

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

10-
// This is the base memory address for *all* peripherals of the
11-
// Rasbperry Pi (I2C, SPI, GPIO, UART, etc)
12-
let peripheralsBaseAddress = 0x3f000000
13-
14-
// This is the offset of just the GPIO configuration registers
15-
// from the base peripheral address
16-
let gpioBaseOffset = 0x200000
17-
18-
// We only really care about the memory from this point
19-
let gpioBaseAddress = peripheralsBaseAddress + gpioBaseOffset
20-
2110
// Open the /dev/mem file (all memory) with read and write
2211
// privileges (we want to turn on output pins) at the proper
2312
// offset (we don't care about the rest of the addresses)
2413
// 0x3f200000
25-
guard let rawPointer = mmap(nil, 1024 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, file, gpioBaseAddress) else {
14+
guard let rawPointer = mmap(nil, 1024 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, file, 0x3f200000) else {
2615
perror("Cannot mmap bytes for path")
2716
return
2817
}
@@ -32,7 +21,7 @@ func loopGPIO() {
3221

3322
// Configure GPIO pin 4 & 6 as output (3 bits/pin)
3423
// pin numbers: 6 5 4 3 2 1 0
35-
basePointer.pointee = 0b001000001000000000000
24+
basePointer.pointee = 0b001001000000000000000
3625

3726
// Set up set/clear pointer objects
3827
// Offsets from docs were given in byte-size values
@@ -52,11 +41,11 @@ func loopGPIO() {
5241
// Loop through and show the LED turning on and off
5342
for i in 0...5 {
5443
print("High \(setPointer)")
55-
setPointer.pointee = 0b1010000 // Pin 6 & 4
44+
setPointer.pointee = 0b1100000 // Pin 6 & 4
5645
usleep(500000)
5746
print("Counter: \(i)")
5847
print("Low \(clearPointer)")
59-
clearPointer.pointee = 0b1010000 // Pin 6 & 4
48+
clearPointer.pointee = 0b1100000 // Pin 6 & 4
6049
usleep(500000)
6150
}
6251
}

examples/direct_old

19.2 KB
Binary file not shown.

examples/hello

9.74 KB
Binary file not shown.

examples/hello.swift

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Hello Swift Summit")

examples/memory

224 Bytes
Binary file not shown.

source/Package.pins

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
{
22
"autoPin": true,
33
"pins": [
4+
{
5+
"package": "DS18B20",
6+
"reason": null,
7+
"repositoryURL": "https://github.com/uraimo/DS18B20.swift.git",
8+
"version": "1.0.3"
9+
},
410
{
511
"package": "SwiftyGPIO",
612
"reason": null,

source/Package.swift

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ let package = Package(
1414
],
1515
dependencies: [
1616
.Package(url: "https://github.com/uraimo/SwiftyGPIO.git", majorVersion: 0),
17+
.Package(url: "https://github.com/uraimo/DS18B20.swift.git", majorVersion: 1),
1718
]
1819
)

source/Rakefile

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace :log do
3333
system_and_log 'aws cloudwatch put-metric-data --metric-name ProcessorTemperatureCelcius --namespace RaspberrySwift --unit Count --value $(expr `cat /sys/class/thermal/thermal_zone0/temp` / 1000)'
3434
end
3535

36+
desc 'Log the GPU temperature to Cloudwatch'
3637
task :gpu_temp do
3738
require 'open3'
3839
temp_string, success = Open3.capture2e('/opt/vc/bin/vcgencmd', 'measure_temp')
@@ -41,6 +42,7 @@ namespace :log do
4142
system_and_log command
4243
end
4344

45+
desc 'Log boot event to Cloudwatch'
4446
task :boot do
4547
system_and_log 'aws cloudwatch put-metric-data --metric-name Boot --namespace RaspberrySwift --unit Count --value 1'
4648
end
+11-40
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,25 @@
11

22
import Foundation
33
import SwiftyGPIO
4+
import DS18B20
45

56
func shortSleep() {
67
usleep(100000)
78
}
89

910
public struct AnalogReader {
10-
let dataStore = DataStore()
11-
let i2c: SysFSI2C
12-
11+
let ds: DS18B20
1312
public init() {
14-
let i2cs = SwiftyGPIO.hardwareI2Cs(for:.RaspberryPi3)!
15-
i2c = i2cs[1] as! SysFSI2C
13+
let onewires = SwiftyGPIO.hardware1Wires(for:.RaspberryPi3)!
14+
print("One wires: \(onewires)")
15+
let onewire = onewires[0]
16+
// let slaveId = onewire.getSlaves()[]
17+
print("Slaves: \(onewire.getSlaves())")
18+
ds = DS18B20(onewire, slaveId: "28-0000097eb360")
19+
// GPIO16
1620
}
1721

18-
public func readValue() {
19-
// sleep(1)
20-
21-
// let values: [UInt8] = [0xC3, 0xA3]
22-
// let values: [UInt8] = [0xA3, 0xC3]
23-
// let values: [UInt8] = [195, 163]
24-
// print("195: ", 195.hex()) // c3
25-
// print("163: ", 163.hex()) // a3
26-
27-
// i2cset 1 0x48 0x01 0xc3a3 w
28-
// i2cget 1 0x48 0x0 w // 0x3bf5
29-
// i2cdump 1 0x48 b
30-
31-
// Write config data
32-
33-
// let values = [195, 163]
34-
i2c.writeWord(0x48, command: 1, value: (195 << 8 | 163))
35-
// i2c.writeData(0x48, command: 1, values: [195, 163])
36-
// print("Write: \(i2c.writeData(0x48, command: 1, values:values))")
37-
// print("Write: \(i2c.writeWord(0x48, command: 1, value: 0xc3a5))")
38-
39-
shortSleep()
40-
let value = i2c.readWord(0x48, command: 0)
41-
print("Value: \(value)")
42-
// let values = i2c.readData(0x48, command:0)
43-
// let high = value & 0xFF
44-
// let low = (value >> 8) & 0xFF
45-
46-
// let value = i2c.readData(0x48, command: 0)
47-
// let high: UInt16 = UInt16(value[0]) << 8
48-
// let low: UInt16 = UInt16(value[1])
49-
// let calculated = Double(high | low) * 4096 / 32768.0
50-
// print("Calculated: \(calculated)")
51-
52-
// print("Read: \(i2c.readWord(0x48, command: 0).hex())")
22+
public func readValue() -> Float {
23+
return ds.Temperature
5324
}
5425
}

source/Sources/Home/LightSwitch.swift

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

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

77
public init?() {
8-
print("New light switch.")
8+
// Create the pins
99
let gpios = SwiftyGPIO.GPIOs(for:.RaspberryPi3)
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
10+
guard let ledPin = gpios[.P5] else { print("Invalid GPIO: .P5"); return nil }
11+
guard let relayPin = gpios[.P6] else { print("Invalid GPIO: .P6"); return nil }
12+
self.ledPin = ledPin
13+
self.relayPin = relayPin
14+
15+
// Set the pins to be outputs
16+
ledPin.direction = .OUT
17+
relayPin.direction = .OUT
1618
}
1719

20+
// Turn all of the pins on
1821
public func on() {
19-
gpio4.value = 1
20-
gpio6.value = 1
22+
print("🌕 On")
23+
ledPin.value = 1
24+
relayPin.value = 1
2125
}
2226

27+
// Turn all of the pins off
2328
public func off() {
24-
gpio4.value = 0
25-
gpio6.value = 0
29+
print("🌒 Off")
30+
ledPin.value = 0
31+
relayPin.value = 0
2632
}
2733
}
34+
35+

source/Sources/RaspberrySwift/main.swift

+4-15
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,22 @@
33
//
44

55
import Foundation
6-
import SwiftyGPIO
76
import Home
87

9-
public func i2c() {
10-
let analogReader = AnalogReader()
11-
12-
for _ in 1...12 {
13-
analogReader.readValue()
14-
sleep(1)
15-
}
16-
}
17-
188
func switch_toggle() {
199
guard let light = LightSwitch() else { print("Failed to create switch"); return }
10+
let reader = AnalogReader()
2011

12+
print("Temperature: \(reader.readValue())")
2113
for _ in 1...5 {
22-
print("On.")
2314
light.on()
24-
sleep(1)
25-
print("Off.")
15+
usleep(500_000)
2616
light.off()
27-
sleep(1)
17+
sleep(500_000)
2818
}
2919
}
3020

3121
switch_toggle()
32-
//i2c()
3322

3423
func main() -> Int {
3524
print("Main")

0 commit comments

Comments
 (0)