From b00ca76025bfdb4a1e591effcada2572210bece2 Mon Sep 17 00:00:00 2001 From: Andy Liu Date: Thu, 26 Sep 2024 21:23:27 +0800 Subject: [PATCH] Develop (#29) * Update Signed-off-by: Andy Liu * Update for Embedded Swift Signed-off-by: Andy Liu * Update for Embedded Swift Signed-off-by: Andy Liu * Update for Embedded Swift Signed-off-by: Andy Liu * Update for Embedded Swift(Remove swift-extra-json dependenci) Signed-off-by: Andy Liu * Update for Embedded Swift Signed-off-by: Andy Liu * Update for Embedded Swift Signed-off-by: Andy Liu * Update for Embedded Swift Signed-off-by: Andy Liu * Update for Embedded Swift Signed-off-by: Andy Liu * Update for Embedded Swift Signed-off-by: Andy Liu * Update for Embedded Swift Signed-off-by: Andy Liu * Update for Embedded Swift Signed-off-by: Andy Liu --------- Signed-off-by: Andy Liu --- .../Mission10_Humiture_Sensor/LCD1602.swift | 13 +- .../Mission10_Humiture_Sensor/SHT3x.swift | 4 +- .../Mission12_Buzzer_Music/Twinkle.swift | 2 +- .../Sources/Mission9_LCD/LCD1602.swift | 13 +- .../Potentiometer/Sources/main.swift | 9 +- .../11WiFi/JoiningWiFi/Sources/main.swift | 9 +- .../TemperatureDataLogger/Sources/main.swift | 8 +- .../11WiFi/Weather/.gitignore | 8 -- .../11WiFi/Weather/Package.mmp | 17 --- .../11WiFi/Weather/Package.swift | 28 ---- .../11WiFi/Weather/Sources/WeatherInfo.swift | 124 ------------------ .../11WiFi/Weather/Sources/main.swift | 60 --------- .../12FileSystem/Album/Sources/Album.swift | 6 +- .../Sources/WritingCSVFile.swift | 10 +- .../Pong/Sources/Pong/Game.swift | 6 +- .../13MoreProjects/Prank/Sources/main.swift | 16 ++- README.md | 1 + 17 files changed, 75 insertions(+), 259 deletions(-) delete mode 100644 Examples/SwiftIOPlayground/11WiFi/Weather/.gitignore delete mode 100644 Examples/SwiftIOPlayground/11WiFi/Weather/Package.mmp delete mode 100644 Examples/SwiftIOPlayground/11WiFi/Weather/Package.swift delete mode 100644 Examples/SwiftIOPlayground/11WiFi/Weather/Sources/WeatherInfo.swift delete mode 100644 Examples/SwiftIOPlayground/11WiFi/Weather/Sources/main.swift diff --git a/Examples/MakerKit/Mission10_Humiture_Sensor/Sources/Mission10_Humiture_Sensor/LCD1602.swift b/Examples/MakerKit/Mission10_Humiture_Sensor/Sources/Mission10_Humiture_Sensor/LCD1602.swift index c5cab4a..ce2f776 100644 --- a/Examples/MakerKit/Mission10_Humiture_Sensor/Sources/Mission10_Humiture_Sensor/LCD1602.swift +++ b/Examples/MakerKit/Mission10_Humiture_Sensor/Sources/Mission10_Humiture_Sensor/LCD1602.swift @@ -243,9 +243,11 @@ final public class LCD1602 { mul *= 10 } let expandValue = Int(num * Float(mul)) - write(x: x, y: y, String(Float(expandValue) / Float(mul))) + let str = getFloatString(Float(expandValue) / Float(mul)) + write(x: x, y: y, str) } else { - write(x: x, y: y, String(num)) + let str = getFloatString(num) + write(x: x, y: y, str) } } } @@ -279,3 +281,10 @@ extension LCD1602 { } } } + + +func getFloatString(_ num: Float) -> String { + let int = Int(num) + let frac = Int((num - Float(int)) * 100) + return "\(int).\(frac)" +} \ No newline at end of file diff --git a/Examples/MakerKit/Mission10_Humiture_Sensor/Sources/Mission10_Humiture_Sensor/SHT3x.swift b/Examples/MakerKit/Mission10_Humiture_Sensor/Sources/Mission10_Humiture_Sensor/SHT3x.swift index 974dbba..e1786f4 100644 --- a/Examples/MakerKit/Mission10_Humiture_Sensor/Sources/Mission10_Humiture_Sensor/SHT3x.swift +++ b/Examples/MakerKit/Mission10_Humiture_Sensor/Sources/Mission10_Humiture_Sensor/SHT3x.swift @@ -91,7 +91,7 @@ extension SHT3x { // Split the 16-bit data into two 8-bit data. // Write the data to the default address of the sensor. - private func writeCommand(_ command: Command) throws { + private func writeCommand(_ command: Command) throws(Errno) { let value = command.rawValue let result = i2c.write([UInt8(value >> 8), UInt8(value & 0xFF)], to: address) if case .failure(let err) = result { @@ -99,7 +99,7 @@ extension SHT3x { } } - private func readRawValue(into buffer: inout [UInt8]) throws { + private func readRawValue(into buffer: inout [UInt8]) throws(Errno) { for i in 0.. String { + let int = Int(num) + let frac = Int((num - Float(int)) * 100) + return "\(int).\(frac)" +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Sources/main.swift b/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Sources/main.swift index 7469592..9b24094 100644 --- a/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Sources/main.swift +++ b/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Sources/main.swift @@ -8,7 +8,14 @@ let pot = AnalogIn(Id.A0) // Read the voltage value and print it out every second. while true { - let potVoltage = pot.readVoltage() + let potVoltage = getFloatString(pot.readVoltage()) print(potVoltage) sleep(ms: 1000) +} + + +func getFloatString(_ num: Float) -> String { + let int = Int(num) + let frac = Int((num - Float(int)) * 100) + return "\(int).\(frac)" } \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/11WiFi/JoiningWiFi/Sources/main.swift b/Examples/SwiftIOPlayground/11WiFi/JoiningWiFi/Sources/main.swift index 1a5d617..1ba02b7 100644 --- a/Examples/SwiftIOPlayground/11WiFi/JoiningWiFi/Sources/main.swift +++ b/Examples/SwiftIOPlayground/11WiFi/JoiningWiFi/Sources/main.swift @@ -23,7 +23,14 @@ do { print("ESP32 WiFi status: \(esp.wifiStatus)") let ipInfo = try esp.getStationIP() - print(ipInfo) + for index in 0.. String { + let int = Int(num) + let frac = Int((num - Float(int)) * 100) + return "\(int).\(frac)" + } } } \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/13MoreProjects/Pong/Sources/Pong/Game.swift b/Examples/SwiftIOPlayground/13MoreProjects/Pong/Sources/Pong/Game.swift index 40c368d..70146a3 100644 --- a/Examples/SwiftIOPlayground/13MoreProjects/Pong/Sources/Pong/Game.swift +++ b/Examples/SwiftIOPlayground/13MoreProjects/Pong/Sources/Pong/Game.swift @@ -111,7 +111,7 @@ struct PongGame { ball.updateAfterHitWall(window: window) speaker.write(hitWallSound) } - print(ball.x, ball.y) + print("\(ball.x), \(ball.y)") // If the ball hits left/right paddle, change the ball's // direction to bounce it in an opposite direction. @@ -189,8 +189,8 @@ struct PongGame { let size = try file.tell() - headerSize buffer = [UInt8](repeating: 0, count: size) - try buffer.withUnsafeMutableBytes { rawBuffer in - _ = try file.read(fromAbsoluteOffest: headerSize, into: rawBuffer, count: size) + buffer.withUnsafeMutableBytes { rawBuffer in + _ = try? file.read(fromAbsoluteOffest: headerSize, into: rawBuffer, count: size) } try file.close() } catch { diff --git a/Examples/SwiftIOPlayground/13MoreProjects/Prank/Sources/main.swift b/Examples/SwiftIOPlayground/13MoreProjects/Prank/Sources/main.swift index 617a812..f525b68 100644 --- a/Examples/SwiftIOPlayground/13MoreProjects/Prank/Sources/main.swift +++ b/Examples/SwiftIOPlayground/13MoreProjects/Prank/Sources/main.swift @@ -24,7 +24,8 @@ while true { // Once the acceleration exceeds the threshold, the speaker will play a sound effect. // Adjust the threshold to suit your situation. if acceleration.z > 0.25 { - print(acceleration.z) + let zString = getFloatString(acceleration.z) + print(zString) speaker.write(sounds[index]) index = (index + 1) % sounds.count } @@ -41,13 +42,13 @@ func readSoundData(from path: String) -> [UInt8] { var buffer = [UInt8]() - do { + do throws(Errno) { try file.seek(offset: 0, from: FileDescriptor.SeekOrigin.end) let size = try file.tell() - headerSize buffer = [UInt8](repeating: 0, count: size) - try buffer.withUnsafeMutableBytes { rawBuffer in - _ = try file.read(fromAbsoluteOffest: headerSize, into: rawBuffer, count: size) + try? buffer.withUnsafeMutableBytes { rawBuffer in + _ = try? file.read(fromAbsoluteOffest: headerSize, into: rawBuffer, count: size) } try file.close() } catch { @@ -56,4 +57,11 @@ func readSoundData(from path: String) -> [UInt8] { } return buffer +} + + +func getFloatString(_ num: Float) -> String { + let int = Int(num) + let frac = Int((num - Float(int)) * 100) + return "\(int).\(frac)" } \ No newline at end of file diff --git a/README.md b/README.md index bddfcf1..685e1f4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # MadExamples + ![build](https://github.com/madmachineio/MadExamples/actions/workflows/build.yml/badge.svg) [![Discord](https://img.shields.io/discord/592743353049808899?&logo=Discord&colorB=7289da)](https://madmachine.io/discord) [![twitter](https://img.shields.io/twitter/follow/madmachineio?label=%40madmachineio&style=social)](https://twitter.com/madmachineio)