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)