Skip to content

Commit

Permalink
Develop (#74)
Browse files Browse the repository at this point in the history
* Official Embedded Swift compatible

Signed-off-by: Andy Liu <[email protected]>

* Modify ESP32 driver

Signed-off-by: Andy Liu <[email protected]>

* Update Package.mmp

Signed-off-by: Andy Liu <[email protected]>

* Update Package.swift

Signed-off-by: Andy Liu <[email protected]>

* Update SPIReadPressure

Signed-off-by: Andy Liu <[email protected]>

* Update SPIReadPressure

Signed-off-by: Andy Liu <[email protected]>

* Update I2CReadPressure

Signed-off-by: Andy Liu <[email protected]>

* Update print time

Signed-off-by: Andy Liu <[email protected]>

* Update print time

Signed-off-by: Andy Liu <[email protected]>

* Update print time for DS3231

Signed-off-by: Andy Liu <[email protected]>

* Update print time for DS3231

Signed-off-by: Andy Liu <[email protected]>

* Update print in MAG3110

Signed-off-by: Andy Liu <[email protected]>

* Update ESP module

Signed-off-by: Andy Liu <[email protected]>

* Update TMP102

Signed-off-by: Andy Liu <[email protected]>

* Update TMP102

Signed-off-by: Andy Liu <[email protected]>

* Update AMG88xx

Signed-off-by: Andy Liu <[email protected]>

* Update MAX6675

Signed-off-by: Andy Liu <[email protected]>

* Update AS7341

Signed-off-by: Andy Liu <[email protected]>

* Update AS7341

Signed-off-by: Andy Liu <[email protected]>

* Update AS7341

Signed-off-by: Andy Liu <[email protected]>

* Update ADXL345

Signed-off-by: Andy Liu <[email protected]>

* Update TCS34725

Signed-off-by: Andy Liu <[email protected]>

* Update APDS9960

Signed-off-by: Andy Liu <[email protected]>

* Update APDS9960

Signed-off-by: Andy Liu <[email protected]>

---------

Signed-off-by: Andy Liu <[email protected]>
  • Loading branch information
andy0808 authored Sep 24, 2024
1 parent c15ba28 commit 12ecdad
Show file tree
Hide file tree
Showing 68 changed files with 683 additions and 342 deletions.
2 changes: 1 addition & 1 deletion Examples/ADT7410/ReadTemp/Package.mmp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Specify the board name below
# There are "SwiftIOBoard" and "SwiftIOMicro" now
board = "SwiftIOBoard"
board = "SwiftIOMicro"

# Specifiy the target triple below
# There are "thumbv7em-unknown-none-eabi" and "thumbv7em-unknown-none-eabihf" now
Expand Down
2 changes: 1 addition & 1 deletion Examples/ADT7410/ReadTemp/Sources/ReadTemp/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import MadBoard
import ADT7410


let i2c = I2C(Id.I2C1)
let i2c = I2C(Id.I2C0)
let sensor = ADT7410(i2c)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@ let accelerometer = ADXL345(i2c)
while true {
sleep(ms: 1000)
let values = accelerometer.readXYZ()
print(values)
print("x: \(getFloatString(values.x)), y: \(getFloatString(values.y)), z: \(getFloatString(values.z))")
}


func getFloatString(_ num: Float) -> String {
let int = Int(num)
let frac = Int((num - Float(int)) * 100)
return "\(int).\(frac)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,12 @@ let accelerometer = ADXL345(spi)
while true {
sleep(ms: 1000)
let values = accelerometer.readXYZ()
print(values)
print("x: \(getFloatString(values.x)), y: \(getFloatString(values.y)), z: \(getFloatString(values.z))")
}


func getFloatString(_ num: Float) -> String {
let int = Int(num)
let frac = Int((num - Float(int)) * 100)
return "\(int).\(frac)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ let i2c = I2C(Id.I2C0)
let sensor = AMG88xx(i2c)

while true {
print(sensor.readPixels())
printPixels(sensor.readPixels())
sleep(ms: 1000)
}

func printPixels(_ pixels: [Float]) {
print("[")
for index in 0..<pixels.count {
print("\(getFloatString(pixels[index])),")
}
print("]")
}

func getFloatString(_ num: Float) -> String {
let int = Int(num)
let frac = Int((num - Float(int)) * 100)
return "\(int).\(frac)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let sensor = APDS9960(i2c)
sensor.enableColor()

while true {
print(sensor.readColor())
let rawValue = sensor.readColor()
print("Rawvalue: red = \(rawValue.red), green = \(rawValue.green), blue = \(rawValue.blue), clear = \(rawValue.clear)")
sleep(ms: 1000)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ while true {
let gesture = sensor.readGesture()

if gesture != .noGesture {
print(gesture)
printGesture(gesture)
}
}

func printGesture(_ gesture: APDS9960.Gesture) {
let string: String

switch gesture {
case .noGesture:
string = "No Gesture"
case .up:
string = "Up"
case .down:
string = "Down"
case .left:
string = "Left"
case .right:
string = "Right"
}
}
16 changes: 15 additions & 1 deletion Examples/AS7341/ReadAll/Sources/ReadAll/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,22 @@ let i2c = I2C(Id.I2C0)
let sensor = AS7341(i2c)

while true {
print(sensor.readChannels())
printChannels(sensor)
sleep(ms: 1000)
}


func printChannels(_ sensor: AS7341) {
let channels = sensor.readChannels()

print("f1 = \(channels.f1)")
print("f2 = \(channels.f2)")
print("f3 = \(channels.f3)")
print("f4 = \(channels.f4)")
print("f5 = \(channels.f5)")
print("f6 = \(channels.f6)")
print("f7 = \(channels.f7)")
print("f8 = \(channels.f8)")
print("clear = \(channels.clear)")
print("nir = \(channels.nir)")
}
8 changes: 6 additions & 2 deletions Examples/DS3231/Alarm/Sources/Alarm/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ while true {
// Clear the alarm. The alarm will continue to be activated when the time is up.
if value {
sensor.clearAlarm(1)
print(sensor.readTime())
printTime(sensor.readTime())
value = false
}

}

func printTime(_ time: DS3231.Time) {
print("MM/DD/YYYY: \(time.month)/\(time.day)/\(time.year)")
print("Time: \(time.hour):\(time.minute):\(time.second)")
}
7 changes: 6 additions & 1 deletion Examples/DS3231/ReadTime/Sources/ReadTime/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ sensor.setTime(current)

while true {
// Read current time. It returns a optional value and you need to unwrap it.
print(sensor.readTime())
printTime(sensor.readTime())
sleep(ms: 2000)
}

func printTime(_ time: DS3231.Time) {
print("MM/DD/YYYY: \(time.month)/\(time.day)/\(time.year)")
print("Time: \(time.hour):\(time.minute):\(time.second)")
}
7 changes: 6 additions & 1 deletion Examples/DS3231/Timer/Sources/Timer/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ while true {
// Clear the alarm. The alarm will continue to be activated when the time is up.
if value {
sensor.clearAlarm(1)
print(sensor.readTime())
printTime(sensor.readTime())
sensor.setTimer1(second: 10, mode: .second)
value = false
}
}

func printTime(_ time: DS3231.Time) {
print("MM/DD/YYYY: \(time.month)/\(time.day)/\(time.year)")
print("Time: \(time.hour):\(time.minute):\(time.second)")
}
8 changes: 7 additions & 1 deletion Examples/ESP32ATClient/HttpGet/Sources/HttpGet/HttpGet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ public struct HttpGet {
print("ESP32 WiFi status: \(esp.wifiStatus)")

let ipInfo = try esp.getStationIP()
print(ipInfo)
for index in 0..<ipInfo.count {
if index != 0 {
print(".\(ipInfo[index])")
} else {
print(ipInfo[index])
}
}

} catch {
print("Error: \(error)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ public struct HttpPost {
print("ESP32 WiFi status: \(esp.wifiStatus)")

let ipInfo = try esp.getStationIP()
print(ipInfo)
for index in 0..<ipInfo.count {
if index != 0 {
print(".\(ipInfo[index])")
} else {
print(ipInfo[index])
}
}

} catch {
print("Error: \(error)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public struct JoinWiFi {
print("ESP32 WiFi status: \(esp.wifiStatus)")

let ipInfo = try esp.getStationIP()
print(ipInfo)
for index in 0..<ipInfo.count {
if index != 0 {
print(".\(ipInfo[index])")
} else {
print(ipInfo[index])
}
}

} catch {
print("Error: \(error)")
Expand Down
11 changes: 9 additions & 2 deletions Examples/MAG3110/ReadHeading/Sources/ReadHeading/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ let sensor = MAG3110(i2c)
sensor.calibrate()

while true {
print(sensor.readRawValues())
let rawValue = sensor.readRawValues()
print("x = \(rawValue.x), y = \(rawValue.y), z = \(rawValue.z)")
sleep(ms: 1000)
print(sensor.readHeading())
print("Heading: \(getFloatString(sensor.readHeading()))")
sleep(ms: 1000)
}

func getFloatString(_ num: Float) -> String {
let int = Int(num)
let frac = Int((num - Float(int)) * 100)
return "\(int).\(frac)"
}
2 changes: 1 addition & 1 deletion Examples/MAX6675/ReadTemperature/Package.mmp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Specify the board name below
# There are "SwiftIOBoard" and "SwiftIOMicro" now
board = "SwiftIOBoard"
board = "SwiftIOMicro"

# Specifiy the target triple below
# There are "thumbv7em-unknown-none-eabi" and "thumbv7em-unknown-none-eabihf" now
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import MadBoard
print("Read tempature with Max6675 sensor.")

let csPin = DigitalOut(Id.D17)
let spi = SPI(Id.SPI1,csPin: csPin)
let spi = SPI(Id.SPI0,csPin: csPin)
let max6675 = MAX6675(spi: spi)

while true {
sleep(ms: 2000)
if let temparture = max6675.readCelsius(){
print("Temparture is \(temparture)°C")
let string = getDoubleString(temparture)
print("Temparture is \(string)°C")
}else {
print("Thermocouple input of pins T+ and T- is open.")
}
}

func getDoubleString(_ num: Double) -> String {
let int = Int(num)
let frac = Int((num - Double(int)) * 100)
return "\(int).\(frac)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ public struct I2CReadPressure {
let sensor = MS5611(i2c)

while true {
print(sensor.read())
let (temperature, pressure) = sensor.read()

print("Temperature: " + getFloatString(temperature))
print("Pressure: " + getFloatString(pressure))
print(" ")
sleep(ms: 1000)
}
}
}

func getFloatString(_ num: Float) -> String {
let int = Int(num)
let frac = Int((num - Float(int)) * 100)
return "\(int).\(frac)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@ public struct SPIReadPressure {
// let sensor = MS5611(spi)

while true {
print(sensor.read())
let (temperature, pressure) = sensor.read()

print("Temperature: " + getFloatString(temperature))
print("Pressure: " + getFloatString(pressure))
print(" ")
sleep(ms: 1000)
}
}
}

func getFloatString(_ num: Float) -> String {
let int = Int(num)
let frac = Int((num - Float(int)) * 100)
return "\(int).\(frac)"
}
7 changes: 6 additions & 1 deletion Examples/PCF8523/ReadTime/Sources/ReadTime/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ rtc.setTime(time)
// Read the current time and print it every 3s.
while true {
sleep(ms: 3000)
print(rtc.readTime())
printTime(rtc.readTime())
}

func printTime(_ time: PCF8523.Time) {
print("MM/DD/YYYY: \(time.month)/\(time.day)/\(time.year)")
print("Time: \(time.hour):\(time.minute):\(time.second)")
}
7 changes: 6 additions & 1 deletion Examples/PCF8523/Timer/Sources/Timer/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ while true {

// When the interrupt happens, print the current time.
if value {
print(rtc.readTime())
printTime(rtc.readTime())
value = false
}
}

func printTime(_ time: PCF8523.Time) {
print("MM/DD/YYYY: \(time.month)/\(time.day)/\(time.year)")
print("Time: \(time.hour):\(time.minute):\(time.second)")
}
6 changes: 5 additions & 1 deletion Examples/PCF8563/ReadTime/Sources/ReadTime/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ rtc.setTime(current)

while true {
// Read current time. It returns a optional value and you need to unwrap it.
print(rtc.readTime())
printTime(rtc.readTime())
sleep(ms: 2000)
}

func printTime(_ time: PCF8563.Time) {
print("MM/DD/YYYY: \(time.month)/\(time.day)/\(time.year)")
print("Time: \(time.hour):\(time.minute):\(time.second)")
}
16 changes: 12 additions & 4 deletions Examples/TCS34725/ReadColors/Sources/ReadColors/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ let sensor = TCS34725(i2c)
sensor.setIntegrationTime(150)

while true {
print(sensor.readRaw())
print("Lux: \(sensor.readLux())")
print("Color temperature: \(sensor.readColorTemperature())")
print("Color code: \(String(sensor.readColorCode(), radix: 16))")
let rawValue = sensor.readRaw()
print("Rawvalue: red = \(rawValue.red), green = \(rawValue.green), blue = \(rawValue.blue), clear = \(rawValue.clear)")
print("Lux: \(getFloatString(sensor.readLux()))")
print("Color temperature: \(getFloatString(sensor.readColorTemperature()))")
print("Color code: \(sensor.readColorCode()), radix: 16")
sleep(ms: 1000)
}


func getFloatString(_ num: Float) -> String {
let int = Int(num)
let frac = Int((num - Float(int)) * 100)
return "\(int).\(frac)"
}
9 changes: 0 additions & 9 deletions Examples/TMP102/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion Examples/TMP102/MonitorTemp/Package.mmp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Specify the board name below
# There are "SwiftIOBoard" and "SwiftIOMicro" now
board = "SwiftIOBoard"
board = "SwiftIOMicro"

# Specifiy the target triple below
# There are "thumbv7em-unknown-none-eabi" and "thumbv7em-unknown-none-eabihf" now
Expand Down
Loading

0 comments on commit 12ecdad

Please sign in to comment.