diff --git a/Examples/SwiftIOPlayground/01LED/LEDBlink/.gitignore b/Examples/SwiftIOPlayground/01LED/LEDBlink/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/01LED/LEDBlink/.gitignore +++ b/Examples/SwiftIOPlayground/01LED/LEDBlink/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/01LED/LEDBlink/Package.swift b/Examples/SwiftIOPlayground/01LED/LEDBlink/Package.swift index 8c1a76e..0597e1e 100644 --- a/Examples/SwiftIOPlayground/01LED/LEDBlink/Package.swift +++ b/Examples/SwiftIOPlayground/01LED/LEDBlink/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "LEDBlink", dependencies: [ @@ -9,16 +11,14 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "LEDBlink", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "LEDBlinkTests", - dependencies: ["LEDBlink"]), ] ) + diff --git a/Examples/SwiftIOPlayground/01LED/LEDBlink/README.md b/Examples/SwiftIOPlayground/01LED/LEDBlink/README.md deleted file mode 100644 index 778a700..0000000 --- a/Examples/SwiftIOPlayground/01LED/LEDBlink/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# LEDBlink - -A description of this package. diff --git a/Examples/SwiftIOPlayground/01LED/LEDBlink/Sources/LEDBlink/LEDBlink.swift b/Examples/SwiftIOPlayground/01LED/LEDBlink/Sources/LEDBlink/LEDBlink.swift deleted file mode 100644 index 1abc0c6..0000000 --- a/Examples/SwiftIOPlayground/01LED/LEDBlink/Sources/LEDBlink/LEDBlink.swift +++ /dev/null @@ -1,23 +0,0 @@ -// First import the SwiftIO and MadBoard libraries into the project to use related functionalities. -import SwiftIO -import MadBoard - -@main -public struct LEDBlink { - public static func main() { - // Initialize the specified pin used for digital output. - let led = DigitalOut(Id.D18) - - // The code in the loop will run over and over again. - while true { - //Output high voltage to turn on the LED. - led.write(true) - // Keep the LED on for 1 second. - sleep(ms: 1000) - - // Turn off the LED and then keep that state for 1s. - led.write(false) - sleep(ms: 1000) - } - } -} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/01LED/LEDBlink/Sources/main.swift b/Examples/SwiftIOPlayground/01LED/LEDBlink/Sources/main.swift new file mode 100644 index 0000000..b98bb45 --- /dev/null +++ b/Examples/SwiftIOPlayground/01LED/LEDBlink/Sources/main.swift @@ -0,0 +1,18 @@ +// First import the SwiftIO and MadBoard libraries into the project to use related functionalities. +import SwiftIO +import MadBoard + +// Initialize the specified pin used for digital output. +let led = DigitalOut(Id.D18) + +// The code in the loop will run over and over again. +while true { + //Output high voltage to turn on the LED. + led.write(true) + // Keep the LED on for 1 second. + sleep(ms: 1000) + + // Turn off the LED and then keep that state for 1s. + led.write(false) + sleep(ms: 1000) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/01LED/LEDBlink/Tests/LEDBlinkTests/LEDBlinkTests.swift b/Examples/SwiftIOPlayground/01LED/LEDBlink/Tests/LEDBlinkTests/LEDBlinkTests.swift deleted file mode 100644 index 612439f..0000000 --- a/Examples/SwiftIOPlayground/01LED/LEDBlink/Tests/LEDBlinkTests/LEDBlinkTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import LEDBlink - -final class LEDBlinkTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(LEDBlink().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/.gitignore b/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/.gitignore +++ b/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/Package.swift b/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/Package.swift index a5427cd..9c5f1b4 100644 --- a/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/Package.swift +++ b/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "LEDBlinkWithTimer", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "LEDBlinkWithTimer", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "LEDBlinkWithTimerTests", - dependencies: ["LEDBlinkWithTimer"]), ] ) diff --git a/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/README.md b/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/README.md deleted file mode 100644 index b6de7e3..0000000 --- a/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# LEDBlinkWithTimer - -A description of this package. diff --git a/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/Sources/LEDBlinkWithTimer/LEDBlinkWithTimer.swift b/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/Sources/LEDBlinkWithTimer/LEDBlinkWithTimer.swift deleted file mode 100644 index 4b6586d..0000000 --- a/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/Sources/LEDBlinkWithTimer/LEDBlinkWithTimer.swift +++ /dev/null @@ -1,34 +0,0 @@ -// Import the SwiftIO library to control input and output and the MadBoard to use the id of the pins. -import SwiftIO -import MadBoard - -@main -public struct LEDBlinkWithTimer { - public static func main() { - // Initialize a digital pin for LED module. - let redLed = DigitalOut(Id.D18) - - // Initialize the onboard blue LED. - let blueLed = DigitalOut(Id.BLUE) - - // Initialize a timer for 1500ms. - let timer = Timer(period: 1500) - - // Define a new function used to toggle the LED. - func ToggleLEDSwitch() { - redLed.toggle() - } - - // Set an interrupt to reverse the LED state every time the interrupt occurs. - timer.setInterrupt(ToggleLEDSwitch) - - // Blink onboard blue LED. - while true { - blueLed.high() - sleep(ms: 500) - - blueLed.low() - sleep(ms: 500) - } - } -} diff --git a/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/Sources/main.swift b/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/Sources/main.swift new file mode 100644 index 0000000..59d451e --- /dev/null +++ b/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/Sources/main.swift @@ -0,0 +1,29 @@ +// Import the SwiftIO library to control input and output and the MadBoard to use the id of the pins. +import SwiftIO +import MadBoard + +// Initialize a digital pin for LED module. +let redLed = DigitalOut(Id.D18) + +// Initialize the onboard blue LED. +let blueLed = DigitalOut(Id.BLUE) + +// Initialize a timer for 1500ms. +let timer = Timer(period: 1500) + +// Define a new function used to toggle the LED. +func ToggleLEDSwitch() { + redLed.toggle() +} + +// Set an interrupt to reverse the LED state every time the interrupt occurs. +timer.setInterrupt(ToggleLEDSwitch) + +// Blink onboard blue LED. +while true { + blueLed.high() + sleep(ms: 500) + + blueLed.low() + sleep(ms: 500) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/Tests/LEDBlinkWithTimerTests/LEDBlinkWithTimerTests.swift b/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/Tests/LEDBlinkWithTimerTests/LEDBlinkWithTimerTests.swift deleted file mode 100644 index a90600b..0000000 --- a/Examples/SwiftIOPlayground/01LED/LEDBlinkWithTimer/Tests/LEDBlinkWithTimerTests/LEDBlinkWithTimerTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import LEDBlinkWithTimer - -final class LEDBlinkWithTimerTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(LEDBlinkWithTimer().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/01LED/LEDMorseCode/.gitignore b/Examples/SwiftIOPlayground/01LED/LEDMorseCode/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/01LED/LEDMorseCode/.gitignore +++ b/Examples/SwiftIOPlayground/01LED/LEDMorseCode/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/01LED/LEDMorseCode/Package.swift b/Examples/SwiftIOPlayground/01LED/LEDMorseCode/Package.swift index c7ec4f1..357cfd6 100644 --- a/Examples/SwiftIOPlayground/01LED/LEDMorseCode/Package.swift +++ b/Examples/SwiftIOPlayground/01LED/LEDMorseCode/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "LEDMorseCode", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "LEDMorseCode", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "LEDMorseCodeTests", - dependencies: ["LEDMorseCode"]), ] ) diff --git a/Examples/SwiftIOPlayground/01LED/LEDMorseCode/README.md b/Examples/SwiftIOPlayground/01LED/LEDMorseCode/README.md deleted file mode 100644 index 9a22a39..0000000 --- a/Examples/SwiftIOPlayground/01LED/LEDMorseCode/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# LEDMorseCode - -A description of this package. diff --git a/Examples/SwiftIOPlayground/01LED/LEDMorseCode/Sources/LEDMorseCode/LEDMorseCode.swift b/Examples/SwiftIOPlayground/01LED/LEDMorseCode/Sources/LEDMorseCode/LEDMorseCode.swift deleted file mode 100644 index 322310f..0000000 --- a/Examples/SwiftIOPlayground/01LED/LEDMorseCode/Sources/LEDMorseCode/LEDMorseCode.swift +++ /dev/null @@ -1,46 +0,0 @@ -// Import the libraries to use all their functionalities. -import SwiftIO -import MadBoard - -@main -public struct LEDMorseCode { - public static func main() { - // Initialize the digital output pin. - let led = DigitalOut(Id.D18) - - // Define the LED states to represent the letter s and o. - let sSignal = [false, false, false] - let oSignal = [true, true ,true] - - // Set the LED blink rate according to the values in the array. - func send(_ values: [Bool], to light: DigitalOut) { - // The duration of slow flash and quick flash. - let long = 1000 - let short = 500 - - // Iterate all the values in the array. - // If the value is true, the LED will be on for 1s, which is a slow flash. - // And if it’s false, the LED will be on for 0.5s, which is a quick flash. - for value in values { - light.high() - if value { - sleep(ms: long) - } else { - sleep(ms: short) - } - light.low() - sleep(ms: short) - } - } - - // Blink the LED. - // At first, the LED starts 3 fast blink to represent s, then 3 slow blink to represent o, and 3 fast blink again. - // Wait 1s before repeating again. - while true { - send(sSignal, to: led) - send(oSignal, to: led) - send(sSignal, to: led) - sleep(ms: 1000) - } - } -} diff --git a/Examples/SwiftIOPlayground/01LED/LEDMorseCode/Sources/main.swift b/Examples/SwiftIOPlayground/01LED/LEDMorseCode/Sources/main.swift new file mode 100644 index 0000000..1f5e699 --- /dev/null +++ b/Examples/SwiftIOPlayground/01LED/LEDMorseCode/Sources/main.swift @@ -0,0 +1,41 @@ +// Import the libraries to use all their functionalities. +import SwiftIO +import MadBoard + +// Initialize the digital output pin. +let led = DigitalOut(Id.D18) + +// Define the LED states to represent the letter s and o. +let sSignal = [false, false, false] +let oSignal = [true, true ,true] + +// Set the LED blink rate according to the values in the array. +func send(_ values: [Bool], to light: DigitalOut) { + // The duration of slow flash and quick flash. + let long = 1000 + let short = 500 + + // Iterate all the values in the array. + // If the value is true, the LED will be on for 1s, which is a slow flash. + // And if it’s false, the LED will be on for 0.5s, which is a quick flash. + for value in values { + light.high() + if value { + sleep(ms: long) + } else { + sleep(ms: short) + } + light.low() + sleep(ms: short) + } +} + +// Blink the LED. +// At first, the LED starts 3 fast blink to represent s, then 3 slow blink to represent o, and 3 fast blink again. +// Wait 1s before repeating again. +while true { + send(sSignal, to: led) + send(oSignal, to: led) + send(sSignal, to: led) + sleep(ms: 1000) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/01LED/LEDMorseCode/Tests/LEDMorseCodeTests/LEDMorseCodeTests.swift b/Examples/SwiftIOPlayground/01LED/LEDMorseCode/Tests/LEDMorseCodeTests/LEDMorseCodeTests.swift deleted file mode 100644 index b99c5f8..0000000 --- a/Examples/SwiftIOPlayground/01LED/LEDMorseCode/Tests/LEDMorseCodeTests/LEDMorseCodeTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import LEDMorseCode - -final class LEDMorseCodeTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(LEDMorseCode().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/01LED/RGBLED/.gitignore b/Examples/SwiftIOPlayground/01LED/RGBLED/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/01LED/RGBLED/.gitignore +++ b/Examples/SwiftIOPlayground/01LED/RGBLED/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/01LED/RGBLED/Package.swift b/Examples/SwiftIOPlayground/01LED/RGBLED/Package.swift index 9629684..2e28844 100644 --- a/Examples/SwiftIOPlayground/01LED/RGBLED/Package.swift +++ b/Examples/SwiftIOPlayground/01LED/RGBLED/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "RGBLED", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "RGBLED", dependencies: [ "SwiftIO", "MadBoards", ]), - .testTarget( - name: "RGBLEDTests", - dependencies: ["RGBLED"]), ] ) diff --git a/Examples/SwiftIOPlayground/01LED/RGBLED/README.md b/Examples/SwiftIOPlayground/01LED/RGBLED/README.md deleted file mode 100644 index 7844227..0000000 --- a/Examples/SwiftIOPlayground/01LED/RGBLED/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# RGBLED - -A description of this package. diff --git a/Examples/SwiftIOPlayground/01LED/RGBLED/Sources/RGBLED/RGBLED.swift b/Examples/SwiftIOPlayground/01LED/RGBLED/Sources/RGBLED/RGBLED.swift deleted file mode 100644 index 9bfa5a5..0000000 --- a/Examples/SwiftIOPlayground/01LED/RGBLED/Sources/RGBLED/RGBLED.swift +++ /dev/null @@ -1,43 +0,0 @@ -import SwiftIO -import MadBoard - -@main -public struct RGBLED { - public static func main() { - // Initialize the built-in red, green, blue LED. - // They need a low level to be turned on. - // So set the digital value of true to turn them off in the beginning. - let red = DigitalOut(Id.RED, value: true) - let green = DigitalOut(Id.GREEN, value: true) - let blue = DigitalOut(Id.BLUE, value: true) - - while true { - // Red. - setRGB(true, false, false) - // Green. - setRGB(false, true, false) - // Blue. - setRGB(false, false, true) - // Yellow (red + green). - setRGB(true, true, false) - // Magenta (red + blue). - setRGB(true, false, true) - // Cyan (green + blue). - setRGB(false, true, true) - // White (red + green + blue). - setRGB(true, true, true) - // Off. - setRGB(false, false, false) - } - - // Control red, green and blue LED with the given values. - // Apply low voltage to turn on the built-in LEDs. - // For example, if you want the red LED on, you should write false. - func setRGB(_ redOn: Bool, _ greenOn: Bool, _ blueOn: Bool) { - red.write(!redOn) - green.write(!greenOn) - blue.write(!blueOn) - sleep(ms: 1000) - } - } -} diff --git a/Examples/SwiftIOPlayground/01LED/RGBLED/Sources/main.swift b/Examples/SwiftIOPlayground/01LED/RGBLED/Sources/main.swift new file mode 100644 index 0000000..b4d9f94 --- /dev/null +++ b/Examples/SwiftIOPlayground/01LED/RGBLED/Sources/main.swift @@ -0,0 +1,38 @@ +import SwiftIO +import MadBoard + +// Initialize the built-in red, green, blue LED. +// They need a low level to be turned on. +// So set the digital value of true to turn them off in the beginning. +let red = DigitalOut(Id.RED, value: true) +let green = DigitalOut(Id.GREEN, value: true) +let blue = DigitalOut(Id.BLUE, value: true) + +while true { + // Red. + setRGB(true, false, false) + // Green. + setRGB(false, true, false) + // Blue. + setRGB(false, false, true) + // Yellow (red + green). + setRGB(true, true, false) + // Magenta (red + blue). + setRGB(true, false, true) + // Cyan (green + blue). + setRGB(false, true, true) + // White (red + green + blue). + setRGB(true, true, true) + // Off. + setRGB(false, false, false) +} + +// Control red, green and blue LED with the given values. +// Apply low voltage to turn on the built-in LEDs. +// For example, if you want the red LED on, you should write false. +func setRGB(_ redOn: Bool, _ greenOn: Bool, _ blueOn: Bool) { + red.write(!redOn) + green.write(!greenOn) + blue.write(!blueOn) + sleep(ms: 1000) +} diff --git a/Examples/SwiftIOPlayground/01LED/RGBLED/Tests/RGBLEDTests/RGBLEDTests.swift b/Examples/SwiftIOPlayground/01LED/RGBLED/Tests/RGBLEDTests/RGBLEDTests.swift deleted file mode 100644 index 67a98db..0000000 --- a/Examples/SwiftIOPlayground/01LED/RGBLED/Tests/RGBLEDTests/RGBLEDTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import RGBLED - -final class RGBLEDTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(RGBLED().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/.gitignore b/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/.gitignore +++ b/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/Package.swift b/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/Package.swift index dd6a24f..64921f4 100644 --- a/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/Package.swift +++ b/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "ButtonInterrupt", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "ButtonInterrupt", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "ButtonInterruptTests", - dependencies: ["ButtonInterrupt"]), ] ) diff --git a/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/README.md b/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/README.md deleted file mode 100644 index 47f9400..0000000 --- a/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# ButtonInterrupt - -A description of this package. diff --git a/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/Sources/ButtonInterrupt/ButtonInterrupt.swift b/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/Sources/ButtonInterrupt/ButtonInterrupt.swift deleted file mode 100644 index e57d54d..0000000 --- a/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/Sources/ButtonInterrupt/ButtonInterrupt.swift +++ /dev/null @@ -1,27 +0,0 @@ -// Toggle the LED after you press the buttton by detecting the rising edge. - -// Import the SwiftIO library to control input and output and the MadBoard to use the id of the pins. -import SwiftIO -import MadBoard - -@main -public struct ButtonInterrupt { - public static func main() { - // Initialize the input pin for the button and output pin for the LED. - let button = DigitalIn(Id.D1) - let led = DigitalOut(Id.D18) - - // Define a new function used to toggle the LED. - func toggleLed() { - led.toggle() - } - - // Set the interrupt to detect the rising edge. Once detected, the LED will change its state. - button.setInterrupt(.rising, callback: toggleLed) - - // Keep sleeping if the interrupt hasn't been triggered. - while true { - sleep(ms: 1000) - } - } -} diff --git a/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/Sources/main.swift b/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/Sources/main.swift new file mode 100644 index 0000000..ff77bc6 --- /dev/null +++ b/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/Sources/main.swift @@ -0,0 +1,23 @@ +// Toggle the LED after you press the buttton by detecting the rising edge. + +// Import the SwiftIO library to control input and output and the MadBoard to use the id of the pins. +import SwiftIO +import MadBoard + + +// Initialize the input pin for the button and output pin for the LED. +let button = DigitalIn(Id.D1) +let led = DigitalOut(Id.D18) + +// Define a new function used to toggle the LED. +func toggleLed() { + led.toggle() +} + +// Set the interrupt to detect the rising edge. Once detected, the LED will change its state. +button.setInterrupt(.rising, callback: toggleLed) + +// Keep sleeping if the interrupt hasn't been triggered. +while true { + sleep(ms: 1000) +} diff --git a/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/Tests/ButtonInterruptTests/ButtonInterruptTests.swift b/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/Tests/ButtonInterruptTests/ButtonInterruptTests.swift deleted file mode 100644 index 72080bd..0000000 --- a/Examples/SwiftIOPlayground/02Button/ButtonInterrupt/Tests/ButtonInterruptTests/ButtonInterruptTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import ButtonInterrupt - -final class ButtonInterruptTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(ButtonInterrupt().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/.gitignore b/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/.gitignore +++ b/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/Package.swift b/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/Package.swift index 736b617..f896a21 100644 --- a/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/Package.swift +++ b/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "MomentaryLEDSwitch", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "MomentaryLEDSwitch", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "MomentaryLEDSwitchTests", - dependencies: ["MomentaryLEDSwitch"]), ] ) diff --git a/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/README.md b/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/README.md deleted file mode 100644 index c66f3ec..0000000 --- a/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# MomentaryLEDSwitch - -A description of this package. diff --git a/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/Sources/MomentaryLEDSwitch/MomentaryLEDSwitch.swift b/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/Sources/MomentaryLEDSwitch/MomentaryLEDSwitch.swift deleted file mode 100644 index 4b20b3f..0000000 --- a/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/Sources/MomentaryLEDSwitch/MomentaryLEDSwitch.swift +++ /dev/null @@ -1,34 +0,0 @@ -// When you press the button, the LED turns on. -// If yo continue to hold it, the LED stays on. -// After you release it, the LED turns off. - -// Import the SwiftIO library to control input and output and the MadBoard to use the id of the pins. -import SwiftIO -import MadBoard - -@main -public struct MomentaryLEDSwitch { - public static func main() { - // Initialize the input pin for the button and output pin for the LED. - let led = DigitalOut(Id.D18) - let button = DigitalIn(Id.D1) - - // Keep repeating the following steps. - while true { - // Read the input pin to check the button state. - let buttonPressed = button.read() - - // If the value is true which means the button is pressed, turn on the LED. Otherwise, turn off the LED. - if buttonPressed == true { - led.write(true) - } else { - led.write(false) - } - - // Alternatively - // led.write(button.read()) - - sleep(ms: 10) - } - } -} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/Sources/main.swift b/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/Sources/main.swift new file mode 100644 index 0000000..9f58cd7 --- /dev/null +++ b/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/Sources/main.swift @@ -0,0 +1,30 @@ +// When you press the button, the LED turns on. +// If yo continue to hold it, the LED stays on. +// After you release it, the LED turns off. + +// Import the SwiftIO library to control input and output and the MadBoard to use the id of the pins. +import SwiftIO +import MadBoard + + +// Initialize the input pin for the button and output pin for the LED. +let led = DigitalOut(Id.D18) +let button = DigitalIn(Id.D1) + +// Keep repeating the following steps. +while true { + // Read the input pin to check the button state. + let buttonPressed = button.read() + + // If the value is true which means the button is pressed, turn on the LED. Otherwise, turn off the LED. + if buttonPressed == true { + led.write(true) + } else { + led.write(false) + } + + // Alternatively + // led.write(button.read()) + + sleep(ms: 10) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/Tests/MomentaryLEDSwitchTests/MomentaryLEDSwitchTests.swift b/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/Tests/MomentaryLEDSwitchTests/MomentaryLEDSwitchTests.swift deleted file mode 100644 index 67048bb..0000000 --- a/Examples/SwiftIOPlayground/02Button/MomentaryLEDSwitch/Tests/MomentaryLEDSwitchTests/MomentaryLEDSwitchTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import MomentaryLEDSwitch - -final class MomentaryLEDSwitchTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(MomentaryLEDSwitch().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/02Button/OnOffButtons/.gitignore b/Examples/SwiftIOPlayground/02Button/OnOffButtons/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/02Button/OnOffButtons/.gitignore +++ b/Examples/SwiftIOPlayground/02Button/OnOffButtons/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/02Button/OnOffButtons/Package.swift b/Examples/SwiftIOPlayground/02Button/OnOffButtons/Package.swift index a0f765b..10f9aa2 100644 --- a/Examples/SwiftIOPlayground/02Button/OnOffButtons/Package.swift +++ b/Examples/SwiftIOPlayground/02Button/OnOffButtons/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "OnOffButtons", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "OnOffButtons", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "OnOffButtonsTests", - dependencies: ["OnOffButtons"]), ] ) diff --git a/Examples/SwiftIOPlayground/02Button/OnOffButtons/README.md b/Examples/SwiftIOPlayground/02Button/OnOffButtons/README.md deleted file mode 100644 index 317b484..0000000 --- a/Examples/SwiftIOPlayground/02Button/OnOffButtons/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# OnOffButtons - -A description of this package. diff --git a/Examples/SwiftIOPlayground/02Button/OnOffButtons/Sources/OnOffButtons/OnOffButtons.swift b/Examples/SwiftIOPlayground/02Button/OnOffButtons/Sources/OnOffButtons/OnOffButtons.swift deleted file mode 100644 index 71d6eaf..0000000 --- a/Examples/SwiftIOPlayground/02Button/OnOffButtons/Sources/OnOffButtons/OnOffButtons.swift +++ /dev/null @@ -1,61 +0,0 @@ -// Control the LED with two dedicated buttons. -// The button D1 is used to turn on LED and D19 is to turn off it. - -// SwiftIO is used to set all input and output. -import SwiftIO -// MadBoard is for pin ids. -import MadBoard - -@main -public struct OnOffButtons { - public static func main() { - // Initialize the pin for the button used to turn ON LED. - let onButton = DigitalIn(Id.D1) - // Initialize the pin for the button used to turn OFF the LED. - let offButton = DigitalIn(Id.D19) - let led = DigitalOut(Id.D18) - - // Store the current LED state. - var ledOn = false - - // Store the last button state used to decide whether the button is released now. - var onButtonPressed = false - var offButtonPressed = false - - while true { - if ledOn { - // If the LED is on, check if the off button (D19) is pressed. - // If so, turn off the LED after the button is released. - - // Update the OFF button state if it is pressed. - if offButton.read() { - offButtonPressed = true - } - - // If the OFF button has been pressed and is released now, turn off the LED and update all states. - if offButtonPressed && !offButton.read() { - led.low() - offButtonPressed = false - ledOn = false - } - } else { - // If the LED is off, check if the on button (D1) is pressed. - // If so, turn on the LED after the button is released. - - // Update the ON button state if it is pressed. - if onButton.read() { - onButtonPressed = true - } - - // If the ON button has been pressed and is released now, turn on the LED and update all states. - if onButtonPressed && !onButton.read() { - led.high() - onButtonPressed = false - ledOn = true - } - } - - sleep(ms: 10) - } - } -} diff --git a/Examples/SwiftIOPlayground/02Button/OnOffButtons/Sources/main.swift b/Examples/SwiftIOPlayground/02Button/OnOffButtons/Sources/main.swift new file mode 100644 index 0000000..48c02a1 --- /dev/null +++ b/Examples/SwiftIOPlayground/02Button/OnOffButtons/Sources/main.swift @@ -0,0 +1,57 @@ +// Control the LED with two dedicated buttons. +// The button D1 is used to turn on LED and D19 is to turn off it. + +// SwiftIO is used to set all input and output. +import SwiftIO +// MadBoard is for pin ids. +import MadBoard + + +// Initialize the pin for the button used to turn ON LED. +let onButton = DigitalIn(Id.D1) +// Initialize the pin for the button used to turn OFF the LED. +let offButton = DigitalIn(Id.D19) +let led = DigitalOut(Id.D18) + +// Store the current LED state. +var ledOn = false + +// Store the last button state used to decide whether the button is released now. +var onButtonPressed = false +var offButtonPressed = false + +while true { + if ledOn { + // If the LED is on, check if the off button (D19) is pressed. + // If so, turn off the LED after the button is released. + + // Update the OFF button state if it is pressed. + if offButton.read() { + offButtonPressed = true + } + + // If the OFF button has been pressed and is released now, turn off the LED and update all states. + if offButtonPressed && !offButton.read() { + led.low() + offButtonPressed = false + ledOn = false + } + } else { + // If the LED is off, check if the on button (D1) is pressed. + // If so, turn on the LED after the button is released. + + // Update the ON button state if it is pressed. + if onButton.read() { + onButtonPressed = true + } + + // If the ON button has been pressed and is released now, turn on the LED and update all states. + if onButtonPressed && !onButton.read() { + led.high() + onButtonPressed = false + ledOn = true + } + } + + sleep(ms: 10) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/02Button/OnOffButtons/Tests/OnOffButtonsTests/OnOffButtonsTests.swift b/Examples/SwiftIOPlayground/02Button/OnOffButtons/Tests/OnOffButtonsTests/OnOffButtonsTests.swift deleted file mode 100644 index 76b9fa5..0000000 --- a/Examples/SwiftIOPlayground/02Button/OnOffButtons/Tests/OnOffButtonsTests/OnOffButtonsTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import OnOffButtons - -final class OnOffButtonsTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(OnOffButtons().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/.gitignore b/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/.gitignore +++ b/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/Package.swift b/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/Package.swift index 3f702c5..e4b5d06 100644 --- a/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/Package.swift +++ b/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "ToggleLEDSwitch", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "ToggleLEDSwitch", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "ToggleLEDSwitchTests", - dependencies: ["ToggleLEDSwitch"]), ] ) diff --git a/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/README.md b/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/README.md deleted file mode 100644 index 836acca..0000000 --- a/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# ToggleLEDSwitch - -A description of this package. diff --git a/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/Sources/ToggleLEDSwitch/ToggleLEDSwitch.swift b/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/Sources/ToggleLEDSwitch/ToggleLEDSwitch.swift deleted file mode 100644 index afb72e6..0000000 --- a/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/Sources/ToggleLEDSwitch/ToggleLEDSwitch.swift +++ /dev/null @@ -1,31 +0,0 @@ -// After the button (D1) is pressed and released, the LED (D19) is toggled. - -// Import the SwiftIO library to control input and output and -// the MadBoard to use the id of the pins. -import SwiftIO -import MadBoard - -@main -public struct ToggleLEDSwitch { - public static func main() { - // Initialize the input pin for the button and output pin for the LED. - let led = DigitalOut(Id.D18) - let button = DigitalIn(Id.D1) - - var buttonPressed = false - - while true { - // If buttonPressing is false, the button is released. - // Besides, if buttonPressed is true, it means the button has been pressed. - // So it checks if the button has been pressed and released. - // If so, toggle the LED. - let buttonPressing = button.read() - if !buttonPressing && buttonPressed { - led.toggle() - } - // Store the latest button state. - buttonPressed = buttonPressing - sleep(ms: 10) - } - } -} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/Sources/main.swift b/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/Sources/main.swift new file mode 100644 index 0000000..dee57dc --- /dev/null +++ b/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/Sources/main.swift @@ -0,0 +1,27 @@ +// After the button (D1) is pressed and released, the LED (D19) is toggled. + +// Import the SwiftIO library to control input and output and +// the MadBoard to use the id of the pins. +import SwiftIO +import MadBoard + + +// Initialize the input pin for the button and output pin for the LED. +let led = DigitalOut(Id.D18) +let button = DigitalIn(Id.D1) + +var buttonPressed = false + +while true { + // If buttonPressing is false, the button is released. + // Besides, if buttonPressed is true, it means the button has been pressed. + // So it checks if the button has been pressed and released. + // If so, toggle the LED. + let buttonPressing = button.read() + if !buttonPressing && buttonPressed { + led.toggle() + } + // Store the latest button state. + buttonPressed = buttonPressing + sleep(ms: 10) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/Tests/ToggleLEDSwitchTests/ToggleLEDSwitchTests.swift b/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/Tests/ToggleLEDSwitchTests/ToggleLEDSwitchTests.swift deleted file mode 100644 index e4dcbe3..0000000 --- a/Examples/SwiftIOPlayground/02Button/ToggleLEDSwitch/Tests/ToggleLEDSwitchTests/ToggleLEDSwitchTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import ToggleLEDSwitch - -final class ToggleLEDSwitchTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(ToggleLEDSwitch().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/.gitignore b/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/.gitignore +++ b/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/Package.swift b/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/Package.swift index 7eafdd8..114aa05 100644 --- a/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/Package.swift +++ b/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "TwoWayLEDSwitches", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "TwoWayLEDSwitches", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "TwoWayLEDSwitchesTests", - dependencies: ["TwoWayLEDSwitches"]), ] ) diff --git a/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/README.md b/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/README.md deleted file mode 100644 index fb0c747..0000000 --- a/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# TwoWayLEDSwitches - -A description of this package. diff --git a/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/Sources/TwoWayLEDSwitches/TwoWayLEDSwitches.swift b/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/Sources/TwoWayLEDSwitches/TwoWayLEDSwitches.swift deleted file mode 100644 index e8df3da..0000000 --- a/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/Sources/TwoWayLEDSwitches/TwoWayLEDSwitches.swift +++ /dev/null @@ -1,37 +0,0 @@ -// Pressing any of the two buttons (D1, D19) will toggle the LED. -// The LED state is updated after that button is pressed. - -// SwiftIO is used to set input and output of the pins. -import SwiftIO -// MadBoard is for pin ids. -import MadBoard - -@main -public struct TwoWayLEDSwitches { - public static func main() { - // Initialize the pins for two buttons and the LED. - let button0 = DigitalIn(Id.D1) - let button1 = DigitalIn(Id.D19) - let led = DigitalOut(Id.D18) - - // Store if the LED state is toggled after a button press. - // It ensures the LED state doesn't change again when button is being pressed. - var ledToggled = false - - // Toggle the LED after any of the two buttons are pressed. - while true { - // When any of the buttons are pressed, toggle the LED. - if !ledToggled && (button0.read() || button1.read()) { - led.toggle() - ledToggled = true - } - - // If both buttons are released, update the state to wait for next press. - if !button0.read() && !button1.read() { - ledToggled = false - } - - sleep(ms: 10) - } - } -} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/Sources/main.swift b/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/Sources/main.swift new file mode 100644 index 0000000..b3afd7a --- /dev/null +++ b/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/Sources/main.swift @@ -0,0 +1,33 @@ +// Pressing any of the two buttons (D1, D19) will toggle the LED. +// The LED state is updated after that button is pressed. + +// SwiftIO is used to set input and output of the pins. +import SwiftIO +// MadBoard is for pin ids. +import MadBoard + + +// Initialize the pins for two buttons and the LED. +let button0 = DigitalIn(Id.D1) +let button1 = DigitalIn(Id.D19) +let led = DigitalOut(Id.D18) + +// Store if the LED state is toggled after a button press. +// It ensures the LED state doesn't change again when button is being pressed. +var ledToggled = false + +// Toggle the LED after any of the two buttons are pressed. +while true { + // When any of the buttons are pressed, toggle the LED. + if !ledToggled && (button0.read() || button1.read()) { + led.toggle() + ledToggled = true + } + + // If both buttons are released, update the state to wait for next press. + if !button0.read() && !button1.read() { + ledToggled = false + } + + sleep(ms: 10) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/Tests/TwoWayLEDSwitchesTests/TwoWayLEDSwitchesTests.swift b/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/Tests/TwoWayLEDSwitchesTests/TwoWayLEDSwitchesTests.swift deleted file mode 100644 index 9b1f34b..0000000 --- a/Examples/SwiftIOPlayground/02Button/TwoWayLEDSwitches/Tests/TwoWayLEDSwitchesTests/TwoWayLEDSwitchesTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import TwoWayLEDSwitches - -final class TwoWayLEDSwitchesTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(TwoWayLEDSwitches().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/.gitignore b/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/.gitignore +++ b/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/Package.swift b/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/Package.swift index 2fd9cee..bc3148b 100644 --- a/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/Package.swift +++ b/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "BreathingLED", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "BreathingLED", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "BreathingLEDTests", - dependencies: ["BreathingLED"]), ] -) +) \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/README.md b/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/README.md deleted file mode 100644 index 54613ed..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# BreathingLED - -A description of this package. diff --git a/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/Sources/BreathingLED/BreathingLED.swift b/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/Sources/BreathingLED/BreathingLED.swift deleted file mode 100644 index 7ee9c57..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/Sources/BreathingLED/BreathingLED.swift +++ /dev/null @@ -1,46 +0,0 @@ -// Import the SwiftIO library to set the input and output and the MadBoard to use the pin id. -import SwiftIO -import MadBoard - -@main -public struct BreathingLED { - public static func main() { - // Initialize a PWM output pin for the LED. - let led = PWMOut(Id.PWM4A) - - // Store the maximum and minimum values of the duty cycle to two constants. - let maxDutycycle: Float = 1.0 - let minDutycycle: Float = 0.0 - - // Set the change of duty cycle for each action. - let stepDutycycle: Float = 0.01 - - // Create a variable to store the varying duty cycle. - var dutycycle: Float = 0.0 - - // A condition used to decide whether to increase or decrease the duty cycle. - var upDirection = true - - while true { - // Output a PWM signal with the specified duty cycle to control LED brightness. - led.setDutycycle(dutycycle) - // Keep each brightness last for 10ms, or you may not see the changes. - sleep(ms: 10) - - // Increase or decrease the duty cycle within its range according to the value of upDirection. - if upDirection { - dutycycle += stepDutycycle - if dutycycle >= maxDutycycle { - upDirection = false - dutycycle = maxDutycycle - } - } else { - dutycycle -= stepDutycycle - if dutycycle <= minDutycycle { - upDirection = true - dutycycle = minDutycycle - } - } - } - } -} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/Sources/main.swift b/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/Sources/main.swift new file mode 100644 index 0000000..98e484d --- /dev/null +++ b/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/Sources/main.swift @@ -0,0 +1,42 @@ +// Import the SwiftIO library to set the input and output and the MadBoard to use the pin id. +import SwiftIO +import MadBoard + + +// Initialize a PWM output pin for the LED. +let led = PWMOut(Id.PWM4A) + +// Store the maximum and minimum values of the duty cycle to two constants. +let maxDutycycle: Float = 1.0 +let minDutycycle: Float = 0.0 + +// Set the change of duty cycle for each action. +let stepDutycycle: Float = 0.01 + +// Create a variable to store the varying duty cycle. +var dutycycle: Float = 0.0 + +// A condition used to decide whether to increase or decrease the duty cycle. +var upDirection = true + +while true { + // Output a PWM signal with the specified duty cycle to control LED brightness. + led.setDutycycle(dutycycle) + // Keep each brightness last for 10ms, or you may not see the changes. + sleep(ms: 10) + + // Increase or decrease the duty cycle within its range according to the value of upDirection. + if upDirection { + dutycycle += stepDutycycle + if dutycycle >= maxDutycycle { + upDirection = false + dutycycle = maxDutycycle + } + } else { + dutycycle -= stepDutycycle + if dutycycle <= minDutycycle { + upDirection = true + dutycycle = minDutycycle + } + } +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/Tests/BreathingLEDTests/BreathingLEDTests.swift b/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/Tests/BreathingLEDTests/BreathingLEDTests.swift deleted file mode 100644 index abc92a2..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/BreathingLED/Tests/BreathingLEDTests/BreathingLEDTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import BreathingLED - -final class BreathingLEDTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(BreathingLED().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/.gitignore b/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/.gitignore +++ b/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/Package.swift b/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/Package.swift index dd7bd01..193c6a9 100644 --- a/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/Package.swift +++ b/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "ButtonDimmer", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "ButtonDimmer", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "ButtonDimmerTests", - dependencies: ["ButtonDimmer"]), ] ) diff --git a/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/README.md b/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/README.md deleted file mode 100644 index 86711a3..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# ButtonDimmer - -A description of this package. diff --git a/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/Sources/ButtonDimmer/ButtonDimmer.swift b/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/Sources/ButtonDimmer/ButtonDimmer.swift deleted file mode 100644 index 04040da..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/Sources/ButtonDimmer/ButtonDimmer.swift +++ /dev/null @@ -1,50 +0,0 @@ -// Import the SwiftIO library to set the input and output and the MadBoard to use the pin id. -import SwiftIO -import MadBoard - -@main -public struct ButtonDimmer { - public static func main() { - // Initialize the PWM pin. - let led = PWMOut(Id.PWM4A) - - // Store the max and min values of duty cycle to two constants. - let maxDutycycle: Float = 1.0 - let minDutycycle: Float = 0.0 - - // The variation of duty cycle per button press. - let stepDutycycle: Float = 0.1 - - // Create a variable to store the value of duty cycle. - var dutycycle: Float = 0.0 - - // Initialize the digital pins. downButton is to dim the LED and the upButton is to brighten the LED. - let downButton = DigitalIn(Id.D1) - let upButton = DigitalIn(Id.D19) - - var dutycycleChanged = false - - // Each time this button is pressed, the LED will dim a little until it reaches the minimum brightness. - downButton.setInterrupt(.rising) { - dutycycle -= stepDutycycle - dutycycle = max(dutycycle, minDutycycle) - dutycycleChanged = true - } - - // Once this button is pressed, the LED becomes brighter until it reaches the maximum brightness. - upButton.setInterrupt(.rising) { - dutycycle += stepDutycycle - dutycycle = min(dutycycle, maxDutycycle) - dutycycleChanged = true - } - - // Update the duty cycle of PWM signal to change the LED brightness. - while true { - if dutycycleChanged { - led.setDutycycle(dutycycle) - dutycycleChanged = false - } - sleep(ms: 10) - } - } -} diff --git a/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/Sources/main.swift b/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/Sources/main.swift new file mode 100644 index 0000000..653e992 --- /dev/null +++ b/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/Sources/main.swift @@ -0,0 +1,46 @@ +// Import the SwiftIO library to set the input and output and the MadBoard to use the pin id. +import SwiftIO +import MadBoard + + +// Initialize the PWM pin. +let led = PWMOut(Id.PWM4A) + +// Store the max and min values of duty cycle to two constants. +let maxDutycycle: Float = 1.0 +let minDutycycle: Float = 0.0 + +// The variation of duty cycle per button press. +let stepDutycycle: Float = 0.1 + +// Create a variable to store the value of duty cycle. +var dutycycle: Float = 0.0 + +// Initialize the digital pins. downButton is to dim the LED and the upButton is to brighten the LED. +let downButton = DigitalIn(Id.D1) +let upButton = DigitalIn(Id.D19) + +var dutycycleChanged = false + +// Each time this button is pressed, the LED will dim a little until it reaches the minimum brightness. +downButton.setInterrupt(.rising) { + dutycycle -= stepDutycycle + dutycycle = max(dutycycle, minDutycycle) + dutycycleChanged = true +} + +// Once this button is pressed, the LED becomes brighter until it reaches the maximum brightness. +upButton.setInterrupt(.rising) { + dutycycle += stepDutycycle + dutycycle = min(dutycycle, maxDutycycle) + dutycycleChanged = true +} + +// Update the duty cycle of PWM signal to change the LED brightness. +while true { + if dutycycleChanged { + led.setDutycycle(dutycycle) + dutycycleChanged = false + } + sleep(ms: 10) +} diff --git a/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/Tests/ButtonDimmerTests/ButtonDimmerTests.swift b/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/Tests/ButtonDimmerTests/ButtonDimmerTests.swift deleted file mode 100644 index b3cd1d6..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/ButtonDimmer/Tests/ButtonDimmerTests/ButtonDimmerTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import ButtonDimmer - -final class ButtonDimmerTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(ButtonDimmer().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/.gitignore b/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/.gitignore +++ b/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/Package.swift b/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/Package.swift index e927226..47fbf19 100644 --- a/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/Package.swift +++ b/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "LEDPatternChange", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "LEDPatternChange", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "LEDPatternChangeTests", - dependencies: ["LEDPatternChange"]), ] ) diff --git a/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/README.md b/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/README.md deleted file mode 100644 index 55f1299..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# LEDPatternChange - -A description of this package. diff --git a/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/Sources/LEDPatternChange/LEDPatternChange.swift b/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/Sources/LEDPatternChange/LEDPatternChange.swift deleted file mode 100644 index 59e5659..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/Sources/LEDPatternChange/LEDPatternChange.swift +++ /dev/null @@ -1,114 +0,0 @@ -// Switch the LED pattern by pressing the button. -// The LED patterns are: constant off, constant on, blink, breathing. - -import SwiftIO -import MadBoard - -@main -public struct LEDPatternChange { - public static func main() { - let led = PWMOut(Id.PWM4A) - let button = DigitalIn(Id.D1) - - // Store the maximum and minimum values of the duty cycle to two constants. - let maxDutycycle: Float = 1.0 - let minDutycycle: Float = 0.0 - - var dutycycle: Float = 0.0 - var stepDutycycle: Float = 0.01 - - // The duration in ms of each loop. - let loopDuration = 10 - // Make the LED on for 500ms and off for 500ms. - let blinkPeriod = 1000 - var blinkTime = 0 - - var patternIndex = 0 - var changePattern = false - - // Set the duty cycle depending on the pattern. - resetDutycycle() - - var buttonPressed = false - - // If the button has been pressed and is released now, change to the next pattern. - button.setInterrupt(.falling) { - buttonPressed = true - } - - while true { - if changePattern { - // Reset the duty cycle after changing the pattern. - resetDutycycle() - changePattern = false - } else { - // Update duty cycle depending on the current LED pattern. - updateDutycycle() - } - - // Set the PWM with the given duty cycle to change LED brightness. - led.setDutycycle(dutycycle) - sleep(ms: loopDuration) - - // Update pattern index after button has been pressed and is released now. - if buttonPressed { - changePattern = true - patternIndex += 1 - if patternIndex == LEDPattern.allCases.count { - patternIndex = 0 - } - - buttonPressed = false - } - } - - // Reset the duty cycle for a new LED pattern. - func resetDutycycle() { - switch LEDPattern(rawValue: patternIndex)! { - case .off: - dutycycle = minDutycycle - case .on: - dutycycle = maxDutycycle - case .blink: - dutycycle = maxDutycycle - blinkTime = 0 - case .breathing: - dutycycle = minDutycycle - stepDutycycle = abs(stepDutycycle) - } - } - - // Update the duty cycle of the PWM signal according to the current LED pattern. - func updateDutycycle() { - switch LEDPattern(rawValue: patternIndex)! { - case .blink: - // For the first half of blinkPeriod, turn on the LED. - // For the second half of blinkPeriod, turn off the LED. - // The duty cycle changes between 0.0 and 1.0 to turn on and off the LED in turn. - blinkTime += loopDuration - dutycycle = blinkTime % blinkPeriod < (blinkPeriod / 2) ? maxDutycycle : minDutycycle - case .breathing: - // The duty cycle gradually increases to the maximum, then decrease to the mininum. - // Therefore, the LED brightens and dims in turn. - dutycycle += stepDutycycle - - // Check if LED brightness is increaed to the max or decreased to min. - if stepDutycycle > 0 && dutycycle >= maxDutycycle { - stepDutycycle.negate() - dutycycle = maxDutycycle - } else if stepDutycycle < 0 && dutycycle <= minDutycycle { - stepDutycycle.negate() - dutycycle = minDutycycle - } - default: break - } - } - - enum LEDPattern: Int, CaseIterable { - case off - case on - case blink - case breathing - } - } -} diff --git a/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/Sources/main.swift b/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/Sources/main.swift new file mode 100644 index 0000000..789501a --- /dev/null +++ b/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/Sources/main.swift @@ -0,0 +1,110 @@ +// Switch the LED pattern by pressing the button. +// The LED patterns are: constant off, constant on, blink, breathing. + +import SwiftIO +import MadBoard + + +let led = PWMOut(Id.PWM4A) +let button = DigitalIn(Id.D1) + +// Store the maximum and minimum values of the duty cycle to two constants. +let maxDutycycle: Float = 1.0 +let minDutycycle: Float = 0.0 + +var dutycycle: Float = 0.0 +var stepDutycycle: Float = 0.01 + +// The duration in ms of each loop. +let loopDuration = 10 +// Make the LED on for 500ms and off for 500ms. +let blinkPeriod = 1000 +var blinkTime = 0 + +var patternIndex = 0 +var changePattern = false + +// Set the duty cycle depending on the pattern. +resetDutycycle() + +var buttonPressed = false + +// If the button has been pressed and is released now, change to the next pattern. +button.setInterrupt(.falling) { + buttonPressed = true +} + +while true { + if changePattern { + // Reset the duty cycle after changing the pattern. + resetDutycycle() + changePattern = false + } else { + // Update duty cycle depending on the current LED pattern. + updateDutycycle() + } + + // Set the PWM with the given duty cycle to change LED brightness. + led.setDutycycle(dutycycle) + sleep(ms: loopDuration) + + // Update pattern index after button has been pressed and is released now. + if buttonPressed { + changePattern = true + patternIndex += 1 + if patternIndex == LEDPattern.allCases.count { + patternIndex = 0 + } + + buttonPressed = false + } +} + +// Reset the duty cycle for a new LED pattern. +func resetDutycycle() { + switch LEDPattern(rawValue: patternIndex)! { + case .off: + dutycycle = minDutycycle + case .on: + dutycycle = maxDutycycle + case .blink: + dutycycle = maxDutycycle + blinkTime = 0 + case .breathing: + dutycycle = minDutycycle + stepDutycycle = abs(stepDutycycle) + } +} + +// Update the duty cycle of the PWM signal according to the current LED pattern. +func updateDutycycle() { + switch LEDPattern(rawValue: patternIndex)! { + case .blink: + // For the first half of blinkPeriod, turn on the LED. + // For the second half of blinkPeriod, turn off the LED. + // The duty cycle changes between 0.0 and 1.0 to turn on and off the LED in turn. + blinkTime += loopDuration + dutycycle = blinkTime % blinkPeriod < (blinkPeriod / 2) ? maxDutycycle : minDutycycle + case .breathing: + // The duty cycle gradually increases to the maximum, then decrease to the mininum. + // Therefore, the LED brightens and dims in turn. + dutycycle += stepDutycycle + + // Check if LED brightness is increaed to the max or decreased to min. + if stepDutycycle > 0 && dutycycle >= maxDutycycle { + stepDutycycle.negate() + dutycycle = maxDutycycle + } else if stepDutycycle < 0 && dutycycle <= minDutycycle { + stepDutycycle.negate() + dutycycle = minDutycycle + } + default: break + } +} + +enum LEDPattern: Int, CaseIterable { + case off + case on + case blink + case breathing +} diff --git a/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/Tests/LEDPatternChangeTests/LEDPatternChangeTests.swift b/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/Tests/LEDPatternChangeTests/LEDPatternChangeTests.swift deleted file mode 100644 index 21a2199..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/LEDPatternChange/Tests/LEDPatternChangeTests/LEDPatternChangeTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import LEDPatternChange - -final class LEDPatternChangeTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(LEDPatternChange().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/.gitignore b/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/.gitignore +++ b/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/Package.swift b/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/Package.swift index ae05471..a872bbb 100644 --- a/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/Package.swift +++ b/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "PlayingScale", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "PlayingScale", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "PlayingScaleTests", - dependencies: ["PlayingScale"]), ] ) diff --git a/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/README.md b/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/README.md deleted file mode 100644 index a89694f..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# PlayingScale - -A description of this package. diff --git a/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/Sources/PlayingScale/PlayingScale.swift b/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/Sources/PlayingScale/PlayingScale.swift deleted file mode 100644 index 8871c48..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/Sources/PlayingScale/PlayingScale.swift +++ /dev/null @@ -1,29 +0,0 @@ -// Import the SwiftIO library to set the input and output, and the MadBoard to use the pin id. -import SwiftIO -import MadBoard - -@main -public struct PlayingScale { - public static func main() { - // Initialize a PWM pin for the buzzer. - let buzzer = PWMOut(Id.PWM5A) - - // Declare a constant to store an array of frequencies. - // Consult the pitch-frequency chart above and list all the necessary frequencies in order in the array. - let frequencies = [262, 294, 330, 349, 392, 440, 494, 523] - - // Use the for-in loop to iterate through each frequency. - // Set the frequency of the PWM signal to generate sounds. Each note will last 1s. - for frequency in frequencies { - buzzer.set(frequency: frequency, dutycycle: 0.5) - sleep(ms: 1000) - } - - // Stop the buzzer sound. - buzzer.suspend() - - while true { - sleep(ms: 1000) - } - } -} diff --git a/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/Sources/main.swift b/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/Sources/main.swift new file mode 100644 index 0000000..60a99b1 --- /dev/null +++ b/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/Sources/main.swift @@ -0,0 +1,25 @@ +// Import the SwiftIO library to set the input and output, and the MadBoard to use the pin id. +import SwiftIO +import MadBoard + + +// Initialize a PWM pin for the buzzer. +let buzzer = PWMOut(Id.PWM5A) + +// Declare a constant to store an array of frequencies. +// Consult the pitch-frequency chart above and list all the necessary frequencies in order in the array. +let frequencies = [262, 294, 330, 349, 392, 440, 494, 523] + +// Use the for-in loop to iterate through each frequency. +// Set the frequency of the PWM signal to generate sounds. Each note will last 1s. +for frequency in frequencies { + buzzer.set(frequency: frequency, dutycycle: 0.5) + sleep(ms: 1000) +} + +// Stop the buzzer sound. +buzzer.suspend() + +while true { + sleep(ms: 1000) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/Tests/PlayingScaleTests/PlayingScaleTests.swift b/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/Tests/PlayingScaleTests/PlayingScaleTests.swift deleted file mode 100644 index 71d0ef6..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/PlayingScale/Tests/PlayingScaleTests/PlayingScaleTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import PlayingScale - -final class PlayingScaleTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(PlayingScale().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/.gitignore b/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/.gitignore +++ b/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/Package.swift b/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/Package.swift index c41d07e..c019f02 100644 --- a/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/Package.swift +++ b/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "ReflexGame", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "ReflexGame", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "ReflexGameTests", - dependencies: ["ReflexGame"]), ] ) diff --git a/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/README.md b/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/README.md deleted file mode 100644 index 74c6b71..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# ReflexGame - -A description of this package. diff --git a/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/Sources/ReflexGame/ReflexGame.swift b/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/Sources/ReflexGame/ReflexGame.swift deleted file mode 100644 index b645618..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/Sources/ReflexGame/ReflexGame.swift +++ /dev/null @@ -1,48 +0,0 @@ -// Press the button D1 to start the game. -// Once you see the red LED turns on, press the button D19 immediately. -// Then the LED turns off and you can see your reflex time in serial monitor. -// To restart the game, press button D1 again. - -import SwiftIO -import MadBoard - -@main -public struct ReflexGame { - public static func main() { - let startButton = DigitalIn(Id.D1) - let player = DigitalIn(Id.D19) - let buzzer = PWMOut(Id.PWM5A) - let led = DigitalOut(Id.D18) - - while true { - // Press the start button to begin the game. - if startButton.read() { - // A beep as a notification. - buzzer.set(frequency: 500, dutycycle: 0.5) - sleep(ms: 500) - buzzer.suspend() - - // Wait for several seconds before the game starts. - sleep(ms: 1000 * Int.random(in: 1...5)) - - // Turn on the red LED as a sign of start. - led.high() - // Store the current clock cycle. - let startClockCycle = getClockCycle() - - // Wait until the button is pressed. - while !player.read() { - sleep(ms: 1) - } - - // Calculate the time in ns. - let duration = cyclesToNanoseconds(start: startClockCycle, stop: getClockCycle()) - // Turn off the indicator. - led.low() - print("Reflex time: \(Float(duration) / 1000_000)ms") - } - - sleep(ms: 10) - } - } -} diff --git a/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/Sources/main.swift b/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/Sources/main.swift new file mode 100644 index 0000000..fa979ed --- /dev/null +++ b/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/Sources/main.swift @@ -0,0 +1,44 @@ +// Press the button D1 to start the game. +// Once you see the red LED turns on, press the button D19 immediately. +// Then the LED turns off and you can see your reflex time in serial monitor. +// To restart the game, press button D1 again. + +import SwiftIO +import MadBoard + + +let startButton = DigitalIn(Id.D1) +let player = DigitalIn(Id.D19) +let buzzer = PWMOut(Id.PWM5A) +let led = DigitalOut(Id.D18) + +while true { + // Press the start button to begin the game. + if startButton.read() { + // A beep as a notification. + buzzer.set(frequency: 500, dutycycle: 0.5) + sleep(ms: 500) + buzzer.suspend() + + // Wait for several seconds before the game starts. + sleep(ms: 1000 * Int.random(in: 1...5)) + + // Turn on the red LED as a sign of start. + led.high() + // Store the current clock cycle. + let startClockCycle = getClockCycle() + + // Wait until the button is pressed. + while !player.read() { + sleep(ms: 1) + } + + // Calculate the time in ns. + let duration = cyclesToNanoseconds(start: startClockCycle, stop: getClockCycle()) + // Turn off the indicator. + led.low() + print("Reflex time: \(Float(duration) / 1000_000)ms") + } + + sleep(ms: 10) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/Tests/ReflexGameTests/ReflexGameTests.swift b/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/Tests/ReflexGameTests/ReflexGameTests.swift deleted file mode 100644 index 9474ac4..0000000 --- a/Examples/SwiftIOPlayground/03Buzzer/ReflexGame/Tests/ReflexGameTests/ReflexGameTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import ReflexGame - -final class ReflexGameTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(ReflexGame().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/.gitignore b/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/.gitignore +++ b/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/Package.swift b/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/Package.swift index e167eca..de3f1dd 100644 --- a/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/Package.swift +++ b/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "DoubleLEDDimmer", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "DoubleLEDDimmer", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "DoubleLEDDimmerTests", - dependencies: ["DoubleLEDDimmer"]), ] ) diff --git a/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/README.md b/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/README.md deleted file mode 100644 index a10fb96..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# DoubleLEDDimmer - -A description of this package. diff --git a/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/Sources/DoubleLEDDimmer/DoubleLEDDimmer.swift b/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/Sources/DoubleLEDDimmer/DoubleLEDDimmer.swift deleted file mode 100644 index bdb03aa..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/Sources/DoubleLEDDimmer/DoubleLEDDimmer.swift +++ /dev/null @@ -1,38 +0,0 @@ -// In this project, two potentiometers control the LED brightness. -// One controls the maximum intensity and the other adjusts the brightness between the minimum and the maximum. - -// Import SwiftIO to control input and output. -import SwiftIO -// Import MadBoard to use the pin ids. -import MadBoard - -@main -public struct DoubleLEDDimmer { - public static func main() { - // Initialize the analog pin used to adjust LED brightness from min and max. - let brightnessPot = AnalogIn(Id.A0) - // Initialize the analog pin used to set max brightness. - let intensityPot = AnalogIn(Id.A11) - // Initialize the PWM pin for the LED. - let led = PWMOut(Id.PWM4A) - - // Define the available range of max duty cycle adjusted by A11. - let minIntensity: Float = 0.2 - let maxIntensity: Float = 1 - - while true { - // Map the analog value from the range 0-1 to the range minIntensity-maxIntensity. - // It decides the max duty cycle for the PWM signal. - // That's to say, it changes the maximum intensity of the LED. - let maxDutycycle = intensityPot.readPercent() * (maxIntensity - minIntensity) + minIntensity - - // Read the analog value (0-1) from the pin A0 which serves as ratio for final duty cycle. - let dutycycleRatio = brightnessPot.readPercent() - - // Calculate the final duty cycle value (0-maxDutycycle). - // Set the PWM output using the result. - led.setDutycycle(dutycycleRatio * maxDutycycle) - sleep(ms: 20) - } - } -} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/Sources/main.swift b/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/Sources/main.swift new file mode 100644 index 0000000..d325361 --- /dev/null +++ b/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/Sources/main.swift @@ -0,0 +1,34 @@ +// In this project, two potentiometers control the LED brightness. +// One controls the maximum intensity and the other adjusts the brightness between the minimum and the maximum. + +// Import SwiftIO to control input and output. +import SwiftIO +// Import MadBoard to use the pin ids. +import MadBoard + + +// Initialize the analog pin used to adjust LED brightness from min and max. +let brightnessPot = AnalogIn(Id.A0) +// Initialize the analog pin used to set max brightness. +let intensityPot = AnalogIn(Id.A11) +// Initialize the PWM pin for the LED. +let led = PWMOut(Id.PWM4A) + +// Define the available range of max duty cycle adjusted by A11. +let minIntensity: Float = 0.2 +let maxIntensity: Float = 1 + +while true { + // Map the analog value from the range 0-1 to the range minIntensity-maxIntensity. + // It decides the max duty cycle for the PWM signal. + // That's to say, it changes the maximum intensity of the LED. + let maxDutycycle = intensityPot.readPercent() * (maxIntensity - minIntensity) + minIntensity + + // Read the analog value (0-1) from the pin A0 which serves as ratio for final duty cycle. + let dutycycleRatio = brightnessPot.readPercent() + + // Calculate the final duty cycle value (0-maxDutycycle). + // Set the PWM output using the result. + led.setDutycycle(dutycycleRatio * maxDutycycle) + sleep(ms: 20) +} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/Tests/DoubleLEDDimmerTests/DoubleLEDDimmerTests.swift b/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/Tests/DoubleLEDDimmerTests/DoubleLEDDimmerTests.swift deleted file mode 100644 index d18c788..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/DoubleLEDDimmer/Tests/DoubleLEDDimmerTests/DoubleLEDDimmerTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import DoubleLEDDimmer - -final class DoubleLEDDimmerTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(DoubleLEDDimmer().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/.gitignore b/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/.gitignore +++ b/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/Package.swift b/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/Package.swift index 6bd5e7c..c8f3438 100644 --- a/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/Package.swift +++ b/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "LEDBlinkControl", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "LEDBlinkControl", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "LEDBlinkControlTests", - dependencies: ["LEDBlinkControl"]), ] -) +) \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/README.md b/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/README.md deleted file mode 100644 index d8ca78a..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# LEDBlinkControl - -A description of this package. diff --git a/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/Sources/LEDBlinkControl/LEDBlinkControl.swift b/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/Sources/LEDBlinkControl/LEDBlinkControl.swift deleted file mode 100644 index 422c5a4..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/Sources/LEDBlinkControl/LEDBlinkControl.swift +++ /dev/null @@ -1,33 +0,0 @@ -// Use two potentiometers to control LED. -// One changes LED brightness and the other changes the blink rate. - -// Import SwiftIO to control input and output. -import SwiftIO -// Import MadBoard to use the pin ids. -import MadBoard - -@main -public struct LEDBlinkControl { - public static func main() { - // Initialize the analog pin used to adjust LED brightness. - let brightnessPot = AnalogIn(Id.A0) - // Initialize the analog pin used to change blink rate. - let blinkRatePot = AnalogIn(Id.A11) - // Initialize the PWM pin for the LED. - let led = PWMOut(Id.PWM4A) - - while true { - // Read raw value from the analog pin. It sets the LED blink rate. - let blinkTime = blinkRatePot.readRawValue() / 2 - - // Read value from the analog pin. - // It serves as duty cycle for PWM to set LED brightness. - led.setDutycycle(brightnessPot.readPercent()) - sleep(ms: blinkTime) - - // After a specified time, suspend the output to turn off the LED. - led.suspend() - sleep(ms: blinkTime) - } - } -} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/Sources/main.swift b/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/Sources/main.swift new file mode 100644 index 0000000..2ae34a8 --- /dev/null +++ b/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/Sources/main.swift @@ -0,0 +1,29 @@ +// Use two potentiometers to control LED. +// One changes LED brightness and the other changes the blink rate. + +// Import SwiftIO to control input and output. +import SwiftIO +// Import MadBoard to use the pin ids. +import MadBoard + + +// Initialize the analog pin used to adjust LED brightness. +let brightnessPot = AnalogIn(Id.A0) +// Initialize the analog pin used to change blink rate. +let blinkRatePot = AnalogIn(Id.A11) +// Initialize the PWM pin for the LED. +let led = PWMOut(Id.PWM4A) + +while true { + // Read raw value from the analog pin. It sets the LED blink rate. + let blinkTime = blinkRatePot.readRawValue() / 2 + + // Read value from the analog pin. + // It serves as duty cycle for PWM to set LED brightness. + led.setDutycycle(brightnessPot.readPercent()) + sleep(ms: blinkTime) + + // After a specified time, suspend the output to turn off the LED. + led.suspend() + sleep(ms: blinkTime) +} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/Tests/LEDBlinkControlTests/LEDBlinkControlTests.swift b/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/Tests/LEDBlinkControlTests/LEDBlinkControlTests.swift deleted file mode 100644 index 51b49ac..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/LEDBlinkControl/Tests/LEDBlinkControlTests/LEDBlinkControlTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import LEDBlinkControl - -final class LEDBlinkControlTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(LEDBlinkControl().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/.gitignore b/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/.gitignore +++ b/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/Package.swift b/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/Package.swift index 401ebfb..8479553 100644 --- a/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/Package.swift +++ b/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "LEDDimmer", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "LEDDimmer", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "LEDDimmerTests", - dependencies: ["LEDDimmer"]), ] ) diff --git a/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/README.md b/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/README.md deleted file mode 100644 index 8de6bbd..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# LEDDimmer - -A description of this package. diff --git a/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/Sources/LEDDimmer/LEDDimmer.swift b/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/Sources/LEDDimmer/LEDDimmer.swift deleted file mode 100644 index 7462289..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/Sources/LEDDimmer/LEDDimmer.swift +++ /dev/null @@ -1,22 +0,0 @@ -// Import SwiftIO library to control input and output, and MadBoard to use the pin name. -import SwiftIO -import MadBoard - -@main -public struct LEDDimmer { - public static func main() { - // Initialize the analog pin for the potentiometer and PWM pin for the LED. - let pot = AnalogIn(Id.A0) - let led = PWMOut(Id.PWM4A) - - // Read the input value. - // The value is represented in percentage, while the duty cycle is also between 0 and 1, - // so you can directly use the reading value to set the PWM. - while true { - let dutycycle = pot.readPercent() - led.setDutycycle(dutycycle) - - sleep(ms: 20) - } - } -} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/Sources/main.swift b/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/Sources/main.swift new file mode 100644 index 0000000..93b4e29 --- /dev/null +++ b/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/Sources/main.swift @@ -0,0 +1,18 @@ +// Import SwiftIO library to control input and output, and MadBoard to use the pin name. +import SwiftIO +import MadBoard + + +// Initialize the analog pin for the potentiometer and PWM pin for the LED. +let pot = AnalogIn(Id.A0) +let led = PWMOut(Id.PWM4A) + +// Read the input value. +// The value is represented in percentage, while the duty cycle is also between 0 and 1, +// so you can directly use the reading value to set the PWM. +while true { + let dutycycle = pot.readPercent() + led.setDutycycle(dutycycle) + + sleep(ms: 20) +} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/Tests/LEDDimmerTests/LEDDimmerTests.swift b/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/Tests/LEDDimmerTests/LEDDimmerTests.swift deleted file mode 100644 index beff2c7..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/LEDDimmer/Tests/LEDDimmerTests/LEDDimmerTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import LEDDimmer - -final class LEDDimmerTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(LEDDimmer().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/.gitignore b/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/.gitignore +++ b/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/Package.swift b/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/Package.swift index d526bb1..2d4d390 100644 --- a/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/Package.swift +++ b/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "PitchControl", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "PitchControl", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "PitchControlTests", - dependencies: ["PitchControl"]), ] ) diff --git a/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/README.md b/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/README.md deleted file mode 100644 index e156536..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# PitchControl - -A description of this package. diff --git a/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/Sources/PitchControl/PitchControl.swift b/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/Sources/PitchControl/PitchControl.swift deleted file mode 100644 index 0212947..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/Sources/PitchControl/PitchControl.swift +++ /dev/null @@ -1,22 +0,0 @@ -// Import the SwiftIO to control input and output and the MadBoard to use the pin name. -import SwiftIO -import MadBoard - -@main -public struct PitchControl { - public static func main() { - // Initialize the analog pin for the potentiometer and PWM pin for the LED. - let pot = AnalogIn(Id.A0) - let buzzer = PWMOut(Id.PWM5A) - - // Read the input value in percentage. - // Then calculate the value into the frequency. - // Set the PWM with the frequency and a duty cycle. - while true { - let potPercentage = pot.readPercent() - let frequency = 50 + Int(1000 * potPercentage) - buzzer.set(frequency: frequency, dutycycle: 0.5) - sleep(ms: 20) - } - } -} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/Sources/main.swift b/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/Sources/main.swift new file mode 100644 index 0000000..05ea75a --- /dev/null +++ b/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/Sources/main.swift @@ -0,0 +1,18 @@ +// Import the SwiftIO to control input and output and the MadBoard to use the pin name. +import SwiftIO +import MadBoard + + +// Initialize the analog pin for the potentiometer and PWM pin for the LED. +let pot = AnalogIn(Id.A0) +let buzzer = PWMOut(Id.PWM5A) + +// Read the input value in percentage. +// Then calculate the value into the frequency. +// Set the PWM with the frequency and a duty cycle. +while true { + let potPercentage = pot.readPercent() + let frequency = 50 + Int(1000 * potPercentage) + buzzer.set(frequency: frequency, dutycycle: 0.5) + sleep(ms: 20) +} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/Tests/PitchControlTests/PitchControlTests.swift b/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/Tests/PitchControlTests/PitchControlTests.swift deleted file mode 100644 index 14395ed..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/PitchControl/Tests/PitchControlTests/PitchControlTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import PitchControl - -final class PitchControlTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(PitchControl().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/.gitignore b/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/.gitignore +++ b/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/Package.swift b/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/Package.swift index 4b79891..45b740d 100644 --- a/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/Package.swift +++ b/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "PlayingTones", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "PlayingTones", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "PlayingTonesTests", - dependencies: ["PlayingTones"]), ] ) diff --git a/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/README.md b/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/README.md deleted file mode 100644 index 337683b..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# PlayingTones - -A description of this package. diff --git a/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/Sources/PlayingTones/PlayingTones.swift b/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/Sources/PlayingTones/PlayingTones.swift deleted file mode 100644 index 8ecb375..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/Sources/PlayingTones/PlayingTones.swift +++ /dev/null @@ -1,38 +0,0 @@ -// Play scales by rotating a potentiometer slowly. - -// Import the SwiftIO library to set the input and output, and the MadBoard to use the pin ids. -import SwiftIO -import MadBoard - -@main -public struct PlayingTones { - public static func main() { - // Initialize a PWM pin for the buzzer. - let buzzer = PWMOut(Id.PWM5A) - // Initialize a analog pin for the potentiometer. - let pot = AnalogIn(Id.A0) - - // Declare a constant to store an array of frequencies. - // They matches respectively notes C4, D4, E4, F4, G4, A4, B4, C5. - let frequencies = [262, 294, 330, 349, 392, 440, 494, 523] - - // Store the last frequency index to avoid playing the same note. - var lastFrequencyIndex = frequencies.count - - while true { - // Read the raw analog value from the pin. - // Map the value into the index of the array. - let frequencyIndex = Int((Float(pot.readRawValue() * (frequencies.count - 1)) / Float(pot.maxRawValue)).rounded(.toNearestOrAwayFromZero)) - - // Check if the note hasn't been played. - if frequencyIndex != lastFrequencyIndex { - // Play the new note for 500ms. - buzzer.set(frequency: frequencies[frequencyIndex], dutycycle: 0.5) - sleep(ms: 500) - buzzer.suspend() - lastFrequencyIndex = frequencyIndex - } - sleep(ms: 10) - } - } -} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/Sources/main.swift b/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/Sources/main.swift new file mode 100644 index 0000000..0da39a1 --- /dev/null +++ b/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/Sources/main.swift @@ -0,0 +1,34 @@ +// Play scales by rotating a potentiometer slowly. + +// Import the SwiftIO library to set the input and output, and the MadBoard to use the pin ids. +import SwiftIO +import MadBoard + + +// Initialize a PWM pin for the buzzer. +let buzzer = PWMOut(Id.PWM5A) +// Initialize a analog pin for the potentiometer. +let pot = AnalogIn(Id.A0) + +// Declare a constant to store an array of frequencies. +// They matches respectively notes C4, D4, E4, F4, G4, A4, B4, C5. +let frequencies = [262, 294, 330, 349, 392, 440, 494, 523] + +// Store the last frequency index to avoid playing the same note. +var lastFrequencyIndex = frequencies.count + +while true { + // Read the raw analog value from the pin. + // Map the value into the index of the array. + let frequencyIndex = Int((Float(pot.readRawValue() * (frequencies.count - 1)) / Float(pot.maxRawValue)).rounded(.toNearestOrAwayFromZero)) + + // Check if the note hasn't been played. + if frequencyIndex != lastFrequencyIndex { + // Play the new note for 500ms. + buzzer.set(frequency: frequencies[frequencyIndex], dutycycle: 0.5) + sleep(ms: 500) + buzzer.suspend() + lastFrequencyIndex = frequencyIndex + } + sleep(ms: 10) +} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/Tests/PlayingTonesTests/PlayingTonesTests.swift b/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/Tests/PlayingTonesTests/PlayingTonesTests.swift deleted file mode 100644 index 219fd99..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/PlayingTones/Tests/PlayingTonesTests/PlayingTonesTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import PlayingTones - -final class PlayingTonesTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(PlayingTones().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/.gitignore b/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/.gitignore +++ b/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Package.swift b/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Package.swift index 0e13bff..700faa1 100644 --- a/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Package.swift +++ b/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "Potentiometer", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "Potentiometer", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "PotentiometerTests", - dependencies: ["Potentiometer"]), ] ) diff --git a/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/README.md b/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/README.md deleted file mode 100644 index b15a34e..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Potentiometer - -A description of this package. diff --git a/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Sources/Potentiometer/Potentiometer.swift b/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Sources/Potentiometer/Potentiometer.swift deleted file mode 100644 index 4280d9e..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Sources/Potentiometer/Potentiometer.swift +++ /dev/null @@ -1,18 +0,0 @@ -// Import SwiftIO library to control input and output, and MadBoard to use the pin name. -import SwiftIO -import MadBoard - -@main -public struct Potentiometer { - public static func main() { - // Initialize the analog pin A0 for the potentiometer. - let pot = AnalogIn(Id.A0) - - // Read the voltage value and print it out every second. - while true { - let potVoltage = pot.readVoltage() - print(potVoltage) - sleep(ms: 1000) - } - } -} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Sources/main.swift b/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Sources/main.swift new file mode 100644 index 0000000..7469592 --- /dev/null +++ b/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Sources/main.swift @@ -0,0 +1,14 @@ +// Import SwiftIO library to control input and output, and MadBoard to use the pin name. +import SwiftIO +import MadBoard + + +// Initialize the analog pin A0 for the potentiometer. +let pot = AnalogIn(Id.A0) + +// Read the voltage value and print it out every second. +while true { + let potVoltage = pot.readVoltage() + print(potVoltage) + sleep(ms: 1000) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Tests/PotentiometerTests/PotentiometerTests.swift b/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Tests/PotentiometerTests/PotentiometerTests.swift deleted file mode 100644 index 881417e..0000000 --- a/Examples/SwiftIOPlayground/04Potentiometer/Potentiometer/Tests/PotentiometerTests/PotentiometerTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import Potentiometer - -final class PotentiometerTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(Potentiometer().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/.gitignore b/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/.gitignore +++ b/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/Package.swift b/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/Package.swift index c1f7a9e..f4c6740 100644 --- a/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/Package.swift +++ b/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "AverageTemperature", dependencies: [ @@ -10,18 +12,15 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "AverageTemperature", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure - .product(name: "SHT3x", package: "MadDrivers") + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "SHT3x", package: "MadDrivers"), ]), - .testTarget( - name: "AverageTemperatureTests", - dependencies: ["AverageTemperature"]), ] ) diff --git a/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/README.md b/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/README.md deleted file mode 100644 index d8320b9..0000000 --- a/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# AverageTemperature - -A description of this package. diff --git a/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/Sources/AverageTemperature/AverageTemperature.swift b/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/Sources/AverageTemperature/AverageTemperature.swift deleted file mode 100644 index 70b48af..0000000 --- a/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/Sources/AverageTemperature/AverageTemperature.swift +++ /dev/null @@ -1,51 +0,0 @@ -// Measure the temperature and get an average reading after a button press. - -// Import SwiftIO to use I2C communication and MadBoard to use pin id. -import SwiftIO -import MadBoard -// Import SHT3x to use its functionalities to read values. -import SHT3x - -@main -public struct AverageTemperature { - public static func main() { - // Initialize the I2C interface and use it to initialize the sensor. - let i2c = I2C(Id.I2C0) - let humiture = SHT3x(i2c) - - // Initialize the LED indicator pin. - let led = DigitalOut(Id.D18) - // Initialize the button pin. - let button = DigitalIn(Id.D1) - - // Decide if it's time to read temperature. - var startMeasurement = false - - while true { - // If button is pressed, update the state. - if button.read() { - startMeasurement = true - } - - // If the button has been pressed and is released, time to measure the temperature. - if startMeasurement && !button.read() { - // Turn on the LED as an indicator. - led.high() - // Read the value 20 times to calculate the average. - var sum: Float = 0 - for _ in 0..<20 { - sum += humiture.readCelsius() - sleep(ms: 3) - } - - // Print the average temperature in celsius. - print("Temperature: \(sum / 20.0)C") - startMeasurement = false - // Turn off the indicator. - led.low() - } - - sleep(ms: 20) - } - } -} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/Sources/main.swift b/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/Sources/main.swift new file mode 100644 index 0000000..3a75bc2 --- /dev/null +++ b/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/Sources/main.swift @@ -0,0 +1,47 @@ +// Measure the temperature and get an average reading after a button press. + +// Import SwiftIO to use I2C communication and MadBoard to use pin id. +import SwiftIO +import MadBoard +// Import SHT3x to use its functionalities to read values. +import SHT3x + + +// Initialize the I2C interface and use it to initialize the sensor. +let i2c = I2C(Id.I2C0) +let humiture = SHT3x(i2c) + +// Initialize the LED indicator pin. +let led = DigitalOut(Id.D18) +// Initialize the button pin. +let button = DigitalIn(Id.D1) + +// Decide if it's time to read temperature. +var startMeasurement = false + +while true { + // If button is pressed, update the state. + if button.read() { + startMeasurement = true + } + + // If the button has been pressed and is released, time to measure the temperature. + if startMeasurement && !button.read() { + // Turn on the LED as an indicator. + led.high() + // Read the value 20 times to calculate the average. + var sum: Float = 0 + for _ in 0..<20 { + sum += humiture.readCelsius() + sleep(ms: 3) + } + + // Print the average temperature in celsius. + print("Temperature: \(sum / 20.0)C") + startMeasurement = false + // Turn off the indicator. + led.low() + } + + sleep(ms: 20) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/Tests/AverageTemperatureTests/AverageTemperatureTests.swift b/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/Tests/AverageTemperatureTests/AverageTemperatureTests.swift deleted file mode 100644 index 7615317..0000000 --- a/Examples/SwiftIOPlayground/05Humiture/AverageTemperature/Tests/AverageTemperatureTests/AverageTemperatureTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import AverageTemperature - -final class AverageTemperatureTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(AverageTemperature().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/05Humiture/Humiture/.gitignore b/Examples/SwiftIOPlayground/05Humiture/Humiture/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/05Humiture/Humiture/.gitignore +++ b/Examples/SwiftIOPlayground/05Humiture/Humiture/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/05Humiture/Humiture/Package.swift b/Examples/SwiftIOPlayground/05Humiture/Humiture/Package.swift index 3a8c03a..8663e8c 100644 --- a/Examples/SwiftIOPlayground/05Humiture/Humiture/Package.swift +++ b/Examples/SwiftIOPlayground/05Humiture/Humiture/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "Humiture", dependencies: [ @@ -10,18 +12,15 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "Humiture", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure - .product(name: "SHT3x", package: "MadDrivers") + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "SHT3x", package: "MadDrivers"), ]), - .testTarget( - name: "HumitureTests", - dependencies: ["Humiture"]), ] ) diff --git a/Examples/SwiftIOPlayground/05Humiture/Humiture/README.md b/Examples/SwiftIOPlayground/05Humiture/Humiture/README.md deleted file mode 100644 index da77407..0000000 --- a/Examples/SwiftIOPlayground/05Humiture/Humiture/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Humiture - -A description of this package. diff --git a/Examples/SwiftIOPlayground/05Humiture/Humiture/Sources/Humiture/Humiture.swift b/Examples/SwiftIOPlayground/05Humiture/Humiture/Sources/Humiture/Humiture.swift deleted file mode 100644 index 1459c58..0000000 --- a/Examples/SwiftIOPlayground/05Humiture/Humiture/Sources/Humiture/Humiture.swift +++ /dev/null @@ -1,24 +0,0 @@ -// Import SwiftIO to use I2C communication and MadBoard to use pin id. -import SwiftIO -import MadBoard -// Import SHT3x to use its functionalities to read values. -import SHT3x - -@main -public struct Humiture { - public static func main() { - // Initialize the I2C interface and use it to initialize the sensor. - let i2c = I2C(Id.I2C0) - let humiture = SHT3x(i2c) - - // Read the temperature and humidity and print their values out. - // Stop for 1s and repeat it. - while true { - let temp = humiture.readCelsius() - let humidity = humiture.readHumidity() - print("Temperature: \(temp)C") - print("Humidity: \(humidity)%") - sleep(ms: 1000) - } - } -} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/05Humiture/Humiture/Sources/main.swift b/Examples/SwiftIOPlayground/05Humiture/Humiture/Sources/main.swift new file mode 100644 index 0000000..723be6c --- /dev/null +++ b/Examples/SwiftIOPlayground/05Humiture/Humiture/Sources/main.swift @@ -0,0 +1,20 @@ +// Import SwiftIO to use I2C communication and MadBoard to use pin id. +import SwiftIO +import MadBoard +// Import SHT3x to use its functionalities to read values. +import SHT3x + + +// Initialize the I2C interface and use it to initialize the sensor. +let i2c = I2C(Id.I2C0) +let humiture = SHT3x(i2c) + +// Read the temperature and humidity and print their values out. +// Stop for 1s and repeat it. +while true { + let temp = humiture.readCelsius() + let humidity = humiture.readHumidity() + print("Temperature: \(temp)C") + print("Humidity: \(humidity)%") + sleep(ms: 1000) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/05Humiture/Humiture/Tests/HumitureTests/HumitureTests.swift b/Examples/SwiftIOPlayground/05Humiture/Humiture/Tests/HumitureTests/HumitureTests.swift deleted file mode 100644 index 489bb5a..0000000 --- a/Examples/SwiftIOPlayground/05Humiture/Humiture/Tests/HumitureTests/HumitureTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import Humiture - -final class HumitureTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(Humiture().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/06RTC/Alarm/.gitignore b/Examples/SwiftIOPlayground/06RTC/Alarm/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/06RTC/Alarm/.gitignore +++ b/Examples/SwiftIOPlayground/06RTC/Alarm/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/06RTC/Alarm/Package.swift b/Examples/SwiftIOPlayground/06RTC/Alarm/Package.swift index 2b2c72d..46cd9c0 100644 --- a/Examples/SwiftIOPlayground/06RTC/Alarm/Package.swift +++ b/Examples/SwiftIOPlayground/06RTC/Alarm/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "Alarm", dependencies: [ @@ -10,18 +12,15 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "Alarm", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure - .product(name: "PCF8563", package: "MadDrivers") + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "PCF8563", package: "MadDrivers"), ]), - .testTarget( - name: "AlarmTests", - dependencies: ["Alarm"]), ] ) diff --git a/Examples/SwiftIOPlayground/06RTC/Alarm/README.md b/Examples/SwiftIOPlayground/06RTC/Alarm/README.md deleted file mode 100644 index fca6efb..0000000 --- a/Examples/SwiftIOPlayground/06RTC/Alarm/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Alarm - -A description of this package. diff --git a/Examples/SwiftIOPlayground/06RTC/Alarm/Sources/Alarm/Alarm.swift b/Examples/SwiftIOPlayground/06RTC/Alarm/Sources/Alarm/Alarm.swift deleted file mode 100644 index 6cd1d39..0000000 --- a/Examples/SwiftIOPlayground/06RTC/Alarm/Sources/Alarm/Alarm.swift +++ /dev/null @@ -1,75 +0,0 @@ -// Set an alarm. It will go off at the specified time. -// You can press the button to stop the sound. - -import SwiftIO -import MadBoard -import PCF8563 - -@main -public struct Alarm { - public static func main() { - let i2c = I2C(Id.I2C0) - let rtc = PCF8563(i2c) - - let led = DigitalOut(Id.D18) - let buzzer = PWMOut(Id.PWM5A) - - // Stop the alarm after it goes off. - let stopButton = DigitalIn(Id.D1) - - // Set the alarm time. - let alarm = AlarmTime(hour: 10, minute: 40) - // Calculate the time when the alarm will stop sounding. - let stopAlarm = getStopAlarm(alarm, after: 1) - var isAlarmed = false - - while true { - let time = rtc.readTime() - - if isAlarmed { - // After the alarm clock goes off, - // if it's time to stop sounding or you press the stop button, - // stop the sound and turn off the LED. - if stopAlarm.isTimeUp(time) || stopButton.read() { - print("Current time: \(time)") - led.low() - buzzer.suspend() - isAlarmed = false - } - } else { - // If the time comes, start the sound and turn on the LED. - if alarm.isTimeUp(time) { - print("Current time: \(time)") - led.high() - buzzer.set(frequency: 500, dutycycle: 0.5) - isAlarmed = true - } - } - - sleep(ms: 10) - } - - // Calculate the time for alarm to stop sounding after specified minutes. - func getStopAlarm(_ alarm: AlarmTime, after min: Int) -> AlarmTime { - var stopMinute = alarm.minute + min - var stopHour = alarm.hour - - if stopMinute >= 60 { - stopMinute -= 60 - stopHour = stopHour == 23 ? 0 : stopHour + 1 - } - - return AlarmTime(hour: stopHour, minute: stopMinute) - } - - struct AlarmTime { - let hour: Int - let minute: Int - - // Check if the set time comes. - func isTimeUp(_ time: PCF8563.Time) -> Bool { - return time.hour == hour && time.minute == minute && time.second == 0 - } - } - } -} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/06RTC/Alarm/Sources/main.swift b/Examples/SwiftIOPlayground/06RTC/Alarm/Sources/main.swift new file mode 100644 index 0000000..33cdb74 --- /dev/null +++ b/Examples/SwiftIOPlayground/06RTC/Alarm/Sources/main.swift @@ -0,0 +1,71 @@ +// Set an alarm. It will go off at the specified time. +// You can press the button to stop the sound. + +import SwiftIO +import MadBoard +import PCF8563 + + +let i2c = I2C(Id.I2C0) +let rtc = PCF8563(i2c) + +let led = DigitalOut(Id.D18) +let buzzer = PWMOut(Id.PWM5A) + +// Stop the alarm after it goes off. +let stopButton = DigitalIn(Id.D1) + +// Set the alarm time. +let alarm = AlarmTime(hour: 10, minute: 40) +// Calculate the time when the alarm will stop sounding. +let stopAlarm = getStopAlarm(alarm, after: 1) +var isAlarmed = false + +while true { + let time = rtc.readTime() + + if isAlarmed { + // After the alarm clock goes off, + // if it's time to stop sounding or you press the stop button, + // stop the sound and turn off the LED. + if stopAlarm.isTimeUp(time) || stopButton.read() { + print("Current time: \(time)") + led.low() + buzzer.suspend() + isAlarmed = false + } + } else { + // If the time comes, start the sound and turn on the LED. + if alarm.isTimeUp(time) { + print("Current time: \(time)") + led.high() + buzzer.set(frequency: 500, dutycycle: 0.5) + isAlarmed = true + } + } + + sleep(ms: 10) +} + +// Calculate the time for alarm to stop sounding after specified minutes. +func getStopAlarm(_ alarm: AlarmTime, after min: Int) -> AlarmTime { + var stopMinute = alarm.minute + min + var stopHour = alarm.hour + + if stopMinute >= 60 { + stopMinute -= 60 + stopHour = stopHour == 23 ? 0 : stopHour + 1 + } + + return AlarmTime(hour: stopHour, minute: stopMinute) +} + +struct AlarmTime { + let hour: Int + let minute: Int + + // Check if the set time comes. + func isTimeUp(_ time: PCF8563.Time) -> Bool { + return time.hour == hour && time.minute == minute && time.second == 0 + } +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/06RTC/Alarm/Tests/AlarmTests/AlarmTests.swift b/Examples/SwiftIOPlayground/06RTC/Alarm/Tests/AlarmTests/AlarmTests.swift deleted file mode 100644 index aeaaa0c..0000000 --- a/Examples/SwiftIOPlayground/06RTC/Alarm/Tests/AlarmTests/AlarmTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import Alarm - -final class AlarmTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(Alarm().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/.gitignore b/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/.gitignore +++ b/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/Package.swift b/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/Package.swift index 8830336..0fa94ee 100644 --- a/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/Package.swift +++ b/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "BlinkUsingRTC", dependencies: [ @@ -10,18 +12,15 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "BlinkUsingRTC", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure + // Use specific library name rather than "MadDrivers" would speed up the build procedure. .product(name: "PCF8563", package: "MadDrivers") ]), - .testTarget( - name: "BlinkUsingRTCTests", - dependencies: ["BlinkUsingRTC"]), ] -) +) \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/README.md b/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/README.md deleted file mode 100644 index 34da0a9..0000000 --- a/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# BlinkUsingRTC - -A description of this package. diff --git a/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/Sources/BlinkUsingRTC/BlinkUsingRTC.swift b/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/Sources/BlinkUsingRTC/BlinkUsingRTC.swift deleted file mode 100644 index 08996d4..0000000 --- a/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/Sources/BlinkUsingRTC/BlinkUsingRTC.swift +++ /dev/null @@ -1,20 +0,0 @@ -// Blink LED every second. - -import SwiftIO -import MadBoard -import PCF8563 - -@main -public struct BlinkUsingRTC { - public static func main() { - let i2c = I2C(Id.I2C0) - let rtc = PCF8563(i2c) - let led = DigitalOut(Id.D18) - - while true { - let time = rtc.readTime() - led.write(time.second % 2 == 0) - sleep(ms: 10) - } - } -} diff --git a/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/Sources/main.swift b/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/Sources/main.swift new file mode 100644 index 0000000..7a241d8 --- /dev/null +++ b/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/Sources/main.swift @@ -0,0 +1,16 @@ +// Blink LED every second. + +import SwiftIO +import MadBoard +import PCF8563 + + +let i2c = I2C(Id.I2C0) +let rtc = PCF8563(i2c) +let led = DigitalOut(Id.D18) + +while true { + let time = rtc.readTime() + led.write(time.second % 2 == 0) + sleep(ms: 10) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/Tests/BlinkUsingRTCTests/BlinkUsingRTCTests.swift b/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/Tests/BlinkUsingRTCTests/BlinkUsingRTCTests.swift deleted file mode 100644 index bd0c894..0000000 --- a/Examples/SwiftIOPlayground/06RTC/BlinkUsingRTC/Tests/BlinkUsingRTCTests/BlinkUsingRTCTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import BlinkUsingRTC - -final class BlinkUsingRTCTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(BlinkUsingRTC().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/06RTC/ReadingTime/.gitignore b/Examples/SwiftIOPlayground/06RTC/ReadingTime/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/06RTC/ReadingTime/.gitignore +++ b/Examples/SwiftIOPlayground/06RTC/ReadingTime/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/06RTC/ReadingTime/Package.swift b/Examples/SwiftIOPlayground/06RTC/ReadingTime/Package.swift index d4049e7..20c6525 100644 --- a/Examples/SwiftIOPlayground/06RTC/ReadingTime/Package.swift +++ b/Examples/SwiftIOPlayground/06RTC/ReadingTime/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "ReadingTime", dependencies: [ @@ -10,18 +12,15 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "ReadingTime", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure - .product(name: "PCF8563", package: "MadDrivers") + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "PCF8563", package: "MadDrivers"), ]), - .testTarget( - name: "ReadingTimeTests", - dependencies: ["ReadingTime"]), ] ) diff --git a/Examples/SwiftIOPlayground/06RTC/ReadingTime/README.md b/Examples/SwiftIOPlayground/06RTC/ReadingTime/README.md deleted file mode 100644 index c39da3e..0000000 --- a/Examples/SwiftIOPlayground/06RTC/ReadingTime/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# ReadingTime - -A description of this package. diff --git a/Examples/SwiftIOPlayground/06RTC/ReadingTime/Sources/ReadingTime/ReadingTime.swift b/Examples/SwiftIOPlayground/06RTC/ReadingTime/Sources/ReadingTime/ReadingTime.swift deleted file mode 100644 index 7be8150..0000000 --- a/Examples/SwiftIOPlayground/06RTC/ReadingTime/Sources/ReadingTime/ReadingTime.swift +++ /dev/null @@ -1,52 +0,0 @@ -// Read current time from the RTC and print it every second. - -import SwiftIO -import MadBoard -import PCF8563 - -@main -public struct ReadingTime { - public static func main() { - let i2c = I2C(Id.I2C0) - let rtc = PCF8563(i2c) - - let daysOfWeek = [ - "Monday", "Tuesday", "Wednesday", - "Thursday", "Friday", "Saturday", - "Sunday" - ] - - // Please use current time to adjust the RTC time if it has been lost power. - // The day of week is from 0 to 6. In this case, 0 refers to Monday. - let currentTime = PCF8563.Time( - year: 2023, month: 4, day: 9, hour: 10, - minute: 26, second: 0, dayOfWeek: 6 - ) - // If the RTC has lost power, its time will be updated. - // If not, its time should be accurate and thus won't be changed. - // If you indeed need to adjust it, set the parameter `update` to `true`. - // rtc.setTime(currentTime, update: true) - rtc.setTime(currentTime) - - while true { - let time = rtc.readTime() - print(formatDateTime(time)) - sleep(ms: 1000) - } - - // Add leading zero if number is one-digit. - // For example, number 1 will be 01. - func formatNum(_ number: UInt8) -> String { - return number < 10 ? "0\(number)" : "\(number)" - } - - // Format the date and time, i.e. 2023/03/01 Wednesday 16:20:00. - func formatDateTime(_ time: PCF8563.Time) -> String { - var string = "" - string += "\(time.year)" + "/" + formatNum(time.month) + "/" + formatNum(time.day) - string += " " + daysOfWeek[Int(time.dayOfWeek)] + " " - string += formatNum(time.hour) + ":" + formatNum(time.minute) + ":" + formatNum(time.second) - return string - } - } -} diff --git a/Examples/SwiftIOPlayground/06RTC/ReadingTime/Sources/main.swift b/Examples/SwiftIOPlayground/06RTC/ReadingTime/Sources/main.swift new file mode 100644 index 0000000..5687d47 --- /dev/null +++ b/Examples/SwiftIOPlayground/06RTC/ReadingTime/Sources/main.swift @@ -0,0 +1,48 @@ +// Read current time from the RTC and print it every second. + +import SwiftIO +import MadBoard +import PCF8563 + + +let i2c = I2C(Id.I2C0) +let rtc = PCF8563(i2c) + +let daysOfWeek = [ + "Monday", "Tuesday", "Wednesday", + "Thursday", "Friday", "Saturday", + "Sunday" +] + +// Please use current time to adjust the RTC time if it has been lost power. +// The day of week is from 0 to 6. In this case, 0 refers to Monday. +let currentTime = PCF8563.Time( + year: 2023, month: 4, day: 9, hour: 10, + minute: 26, second: 0, dayOfWeek: 6 +) +// If the RTC has lost power, its time will be updated. +// If not, its time should be accurate and thus won't be changed. +// If you indeed need to adjust it, set the parameter `update` to `true`. +// rtc.setTime(currentTime, update: true) +rtc.setTime(currentTime) + +while true { + let time = rtc.readTime() + print(formatDateTime(time)) + sleep(ms: 1000) +} + +// Add leading zero if number is one-digit. +// For example, number 1 will be 01. +func formatNum(_ number: UInt8) -> String { + return number < 10 ? "0\(number)" : "\(number)" +} + +// Format the date and time, i.e. 2023/03/01 Wednesday 16:20:00. +func formatDateTime(_ time: PCF8563.Time) -> String { + var string = "" + string += "\(time.year)" + "/" + formatNum(time.month) + "/" + formatNum(time.day) + string += " " + daysOfWeek[Int(time.dayOfWeek)] + " " + string += formatNum(time.hour) + ":" + formatNum(time.minute) + ":" + formatNum(time.second) + return string +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/06RTC/ReadingTime/Tests/ReadingTimeTests/ReadingTimeTests.swift b/Examples/SwiftIOPlayground/06RTC/ReadingTime/Tests/ReadingTimeTests/ReadingTimeTests.swift deleted file mode 100644 index c052a30..0000000 --- a/Examples/SwiftIOPlayground/06RTC/ReadingTime/Tests/ReadingTimeTests/ReadingTimeTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import ReadingTime - -final class ReadingTimeTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(ReadingTime().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/.gitignore b/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/.gitignore +++ b/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/Package.swift b/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/Package.swift index 766bfc0..9ee6962 100644 --- a/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/Package.swift +++ b/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "Accelerometer", dependencies: [ @@ -10,18 +12,15 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "Accelerometer", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure - .product(name: "LIS3DH", package: "MadDrivers") + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "LIS3DH", package: "MadDrivers"), ]), - .testTarget( - name: "AccelerometerTests", - dependencies: ["Accelerometer"]), ] ) diff --git a/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/README.md b/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/README.md deleted file mode 100644 index 7171e7e..0000000 --- a/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Accelerometer - -A description of this package. diff --git a/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/Sources/Accelerometer/Accelerometer.swift b/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/Sources/Accelerometer/Accelerometer.swift deleted file mode 100644 index 2d28323..0000000 --- a/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/Sources/Accelerometer/Accelerometer.swift +++ /dev/null @@ -1,24 +0,0 @@ -// Import SwiftIO to set the communication and MadBoard to use pin id. -import SwiftIO -import MadBoard -// Import LIS3DH to read the accelerations from the sensor. -import LIS3DH - -@main -public struct Accelerometer { - public static func main() { - // Initialize the I2C pins and the sensor. - let i2c = I2C(Id.I2C0) - let accelerometer = LIS3DH(i2c) - - // Read the accelerations and each of them. - while true { - let accelerations = accelerometer.readXYZ() - print("x: \(accelerations.x)g") - print("y: \(accelerations.y)g") - print("z: \(accelerations.z)g") - print("\n") - sleep(ms: 1000) - } - } -} diff --git a/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/Sources/main.swift b/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/Sources/main.swift new file mode 100644 index 0000000..970bd52 --- /dev/null +++ b/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/Sources/main.swift @@ -0,0 +1,20 @@ +// Import SwiftIO to set the communication and MadBoard to use pin id. +import SwiftIO +import MadBoard +// Import LIS3DH to read the accelerations from the sensor. +import LIS3DH + + +// Initialize the I2C pins and the sensor. +let i2c = I2C(Id.I2C0) +let accelerometer = LIS3DH(i2c) + +// Read the accelerations and each of them. +while true { + let accelerations = accelerometer.readXYZ() + print("x: \(accelerations.x)g") + print("y: \(accelerations.y)g") + print("z: \(accelerations.z)g") + print("\n") + sleep(ms: 1000) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/Tests/AccelerometerTests/AccelerometerTests.swift b/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/Tests/AccelerometerTests/AccelerometerTests.swift deleted file mode 100644 index 97c9538..0000000 --- a/Examples/SwiftIOPlayground/07Accelerometer/Accelerometer/Tests/AccelerometerTests/AccelerometerTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import Accelerometer - -final class AccelerometerTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(Accelerometer().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/.gitignore b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/.gitignore +++ b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/Package.swift b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/Package.swift index 8369b6b..05834bd 100644 --- a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/Package.swift +++ b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "AccelerometerDice", dependencies: [ @@ -10,18 +12,15 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "AccelerometerDice", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure - .product(name: "LIS3DH", package: "MadDrivers") + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "LIS3DH", package: "MadDrivers"), ]), - .testTarget( - name: "AccelerometerDiceTests", - dependencies: ["AccelerometerDice"]), ] ) diff --git a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/README.md b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/README.md deleted file mode 100644 index 82cd25e..0000000 --- a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# AccelerometerDice - -A description of this package. diff --git a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/Sources/AccelerometerDice/AccelerometerDice.swift b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/Sources/AccelerometerDice/AccelerometerDice.swift deleted file mode 100644 index b89057e..0000000 --- a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/Sources/AccelerometerDice/AccelerometerDice.swift +++ /dev/null @@ -1,40 +0,0 @@ -// Import SwiftIO to set input and output. -import SwiftIO -// Import MadBoard to use the id of the pins. -import MadBoard -// Import this driver to read accelerations on x, y, z-axis. -import LIS3DH - -@main -public struct AccelerometerDice { - public static func main() { - // Initialize an I2C interface and use it to set the sensor. - let i2c = I2C(Id.I2C0) - let dice = LIS3DH(i2c) - - // Initialize an LED used as an indicator when shaking the sensor. - let indicator = DigitalOut(Id.D18) - - // Create a variable to store the time after the sensor stops movement. - var steadyCount = 999 - - // Read accelerations to judge if the sensor is in motion. - // Once the movement stops, a random number prints out. - while true { - let diceValue = dice.readXYZ() - - if abs(diceValue.x) > 0.3 || abs(diceValue.y) > 0.3 { - indicator.high() - steadyCount = 0 - } else { - steadyCount += 1 - if steadyCount == 50 { - indicator.low() - print(Int.random(in: 1...6)) - } - } - - sleep(ms: 5) - } - } -} diff --git a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/Sources/main.swift b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/Sources/main.swift new file mode 100644 index 0000000..23e2db5 --- /dev/null +++ b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/Sources/main.swift @@ -0,0 +1,36 @@ +// Import SwiftIO to set input and output. +import SwiftIO +// Import MadBoard to use the id of the pins. +import MadBoard +// Import this driver to read accelerations on x, y, z-axis. +import LIS3DH + + +// Initialize an I2C interface and use it to set the sensor. +let i2c = I2C(Id.I2C0) +let dice = LIS3DH(i2c) + +// Initialize an LED used as an indicator when shaking the sensor. +let indicator = DigitalOut(Id.D18) + +// Create a variable to store the time after the sensor stops movement. +var steadyCount = 999 + +// Read accelerations to judge if the sensor is in motion. +// Once the movement stops, a random number prints out. +while true { + let diceValue = dice.readXYZ() + + if abs(diceValue.x) > 0.3 || abs(diceValue.y) > 0.3 { + indicator.high() + steadyCount = 0 + } else { + steadyCount += 1 + if steadyCount == 50 { + indicator.low() + print(Int.random(in: 1...6)) + } + } + + sleep(ms: 5) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/Tests/AccelerometerDiceTests/AccelerometerDiceTests.swift b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/Tests/AccelerometerDiceTests/AccelerometerDiceTests.swift deleted file mode 100644 index c95c406..0000000 --- a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerDice/Tests/AccelerometerDiceTests/AccelerometerDiceTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import AccelerometerDice - -final class AccelerometerDiceTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(AccelerometerDice().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/.gitignore b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/.gitignore +++ b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/Package.swift b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/Package.swift index 91333d8..28f9e23 100644 --- a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/Package.swift +++ b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "AccelerometerLock", dependencies: [ @@ -10,18 +12,15 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "AccelerometerLock", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure - .product(name: "LIS3DH", package: "MadDrivers") + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "LIS3DH", package: "MadDrivers"), ]), - .testTarget( - name: "AccelerometerLockTests", - dependencies: ["AccelerometerLock"]), ] ) diff --git a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/README.md b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/README.md deleted file mode 100644 index 9eb5812..0000000 --- a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# AccelerometerLock - -A description of this package. diff --git a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/Sources/AccelerometerLock/AccelerometerLock.swift b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/Sources/AccelerometerLock/AccelerometerLock.swift deleted file mode 100644 index 1a08432..0000000 --- a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/Sources/AccelerometerLock/AccelerometerLock.swift +++ /dev/null @@ -1,140 +0,0 @@ -// Unlock to turn on the LED. -// The password consits of 3 random tilt directions (left/right/forward/backward). -// Tilt your board and try to guess the password. -// The LED turns on for 1s as an indicator if a move matches. -// Any unmatched move will cause the game to restart. -// After time is up and you haven't unlock it, the password will be changed. -// Open the serial monitor for more notification during the game. - -// Import SwiftIO to set the communication and MadBoard to use pin id. -import SwiftIO -import MadBoard -// Import LIS3DH to read the accelerations from the sensor. -import LIS3DH - -@main -public struct AccelerometerLock { - public static func main() { - // Initialize the I2C pins and the sensor. - let i2c = I2C(Id.I2C0) - let accelerometer = LIS3DH(i2c) - - // Initialize LED and buzzer as indicators. - let led = DigitalOut(Id.D18) - let buzzer = PWMOut(Id.PWM5A) - - // Initialize the button to change the password. - let resetButton = DigitalIn(Id.D1) - - // Initialize the timer with time limit of 30ms. - let timer = Timer(period: 30_000) - - // Generate 3 random tilt directions as the password. - var password = [Direction](repeating: .left, count: 3) - updatePassword() - - // Keep track of the index of direction. - var passwordIndex = 0 - - var reset = false - var start = false - - // If time is up, reset the game. - timer.setInterrupt(start: false) { - reset = true - } - - print("Tilt/move your board to start.") - - while true { - if passwordIndex < password.count { - if let direction = getDirection(accelerometer.readXYZ()) { - // After the first movement, start the timer. - if !start { - timer.start() - start = true - } - - // If the movement matches the desired one, generate a beep as an indicator. - if direction == password[passwordIndex] { - passwordIndex += 1 - print("Correct!") - beep(500) - } else { - // If not, restart from the first direction. - passwordIndex = 0 - print("Wrong! Restart from the first one...") - } - } - } else if passwordIndex == password.count { - // If all 3 directions are matched, turn on the LED. - led.high() - print("Unlocked!") - timer.stop() - // End the game. - passwordIndex += 1 - } - - // If reset button is pressed, it's time to reset the game. - if resetButton.read() { - reset = true - } - - // Generate 3 random directions as a new password and reset the game. - // The buzzer will beep as an indicator. - if reset { - updatePassword() - timer.stop() - - passwordIndex = 0 - start = false - reset = false - - led.low() - beep(1000) - - print("Timeout or reset button is pressed. Password is reset. Tilt/move your board to restart.") - } - - sleep(ms: 20) - } - - // Generate a short beep. - func beep(_ frequency: Int) { - buzzer.set(frequency: frequency, dutycycle: 0.5) - sleep(ms: 300) - buzzer.suspend() - } - - // Replace the previous password with a new one. - func updatePassword() { - for i in 0.. Direction? { - if values.x > 0.5 { - return .left - } else if values.x < -0.5 { - return .right - } - - if values.y > 0.5 { - return .backward - } else if values.y < -0.5 { - return .forward - } - - return nil - } - - enum Direction: Int { - case left - case right - case forward - case backward - } - } -} diff --git a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/Sources/main.swift b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/Sources/main.swift new file mode 100644 index 0000000..77386e5 --- /dev/null +++ b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/Sources/main.swift @@ -0,0 +1,136 @@ +// Unlock to turn on the LED. +// The password consits of 3 random tilt directions (left/right/forward/backward). +// Tilt your board and try to guess the password. +// The LED turns on for 1s as an indicator if a move matches. +// Any unmatched move will cause the game to restart. +// After time is up and you haven't unlock it, the password will be changed. +// Open the serial monitor for more notification during the game. + +// Import SwiftIO to set the communication and MadBoard to use pin id. +import SwiftIO +import MadBoard +// Import LIS3DH to read the accelerations from the sensor. +import LIS3DH + + +// Initialize the I2C pins and the sensor. +let i2c = I2C(Id.I2C0) +let accelerometer = LIS3DH(i2c) + +// Initialize LED and buzzer as indicators. +let led = DigitalOut(Id.D18) +let buzzer = PWMOut(Id.PWM5A) + +// Initialize the button to change the password. +let resetButton = DigitalIn(Id.D1) + +// Initialize the timer with time limit of 30ms. +let timer = Timer(period: 30_000) + +// Generate 3 random tilt directions as the password. +var password = [Direction](repeating: .left, count: 3) +updatePassword() + +// Keep track of the index of direction. +var passwordIndex = 0 + +var reset = false +var start = false + +// If time is up, reset the game. +timer.setInterrupt(start: false) { + reset = true +} + +print("Tilt/move your board to start.") + +while true { + if passwordIndex < password.count { + if let direction = getDirection(accelerometer.readXYZ()) { + // After the first movement, start the timer. + if !start { + timer.start() + start = true + } + + // If the movement matches the desired one, generate a beep as an indicator. + if direction == password[passwordIndex] { + passwordIndex += 1 + print("Correct!") + beep(500) + } else { + // If not, restart from the first direction. + passwordIndex = 0 + print("Wrong! Restart from the first one...") + } + } + } else if passwordIndex == password.count { + // If all 3 directions are matched, turn on the LED. + led.high() + print("Unlocked!") + timer.stop() + // End the game. + passwordIndex += 1 + } + + // If reset button is pressed, it's time to reset the game. + if resetButton.read() { + reset = true + } + + // Generate 3 random directions as a new password and reset the game. + // The buzzer will beep as an indicator. + if reset { + updatePassword() + timer.stop() + + passwordIndex = 0 + start = false + reset = false + + led.low() + beep(1000) + + print("Timeout or reset button is pressed. Password is reset. Tilt/move your board to restart.") + } + + sleep(ms: 20) +} + +// Generate a short beep. +func beep(_ frequency: Int) { + buzzer.set(frequency: frequency, dutycycle: 0.5) + sleep(ms: 300) + buzzer.suspend() +} + +// Replace the previous password with a new one. +func updatePassword() { + for i in 0.. Direction? { + if values.x > 0.5 { + return .left + } else if values.x < -0.5 { + return .right + } + + if values.y > 0.5 { + return .backward + } else if values.y < -0.5 { + return .forward + } + + return nil +} + +enum Direction: Int { + case left + case right + case forward + case backward +} diff --git a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/Tests/AccelerometerLockTests/AccelerometerLockTests.swift b/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/Tests/AccelerometerLockTests/AccelerometerLockTests.swift deleted file mode 100644 index 8945fc2..0000000 --- a/Examples/SwiftIOPlayground/07Accelerometer/AccelerometerLock/Tests/AccelerometerLockTests/AccelerometerLockTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import AccelerometerLock - -final class AccelerometerLockTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(AccelerometerLock().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/.gitignore b/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/.gitignore +++ b/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/Package.swift b/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/Package.swift index 2701644..0fcedfc 100644 --- a/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/Package.swift +++ b/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "AccelerationVisualizer", dependencies: [ @@ -10,19 +12,16 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "AccelerationVisualizer", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure + // Use specific library name rather than "MadDrivers" would speed up the build procedure. .product(name: "LIS3DH", package: "MadDrivers"), - .product(name: "ST7789", package: "MadDrivers") + .product(name: "ST7789", package: "MadDrivers"), ]), - .testTarget( - name: "AccelerationVisualizerTests", - dependencies: ["AccelerationVisualizer"]), ] ) diff --git a/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/README.md b/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/README.md deleted file mode 100644 index 388026f..0000000 --- a/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# AccelerationVisualizer - -A description of this package. diff --git a/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/Sources/AccelerationVisualizer/AccelerationVisualizer.swift b/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/Sources/AccelerationVisualizer/AccelerationVisualizer.swift deleted file mode 100644 index 8e11940..0000000 --- a/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/Sources/AccelerationVisualizer/AccelerationVisualizer.swift +++ /dev/null @@ -1,105 +0,0 @@ -// Visualize the x, y , z accelerations in the range by moving the corresponding sliders. - -import SwiftIO -import MadBoard -import LIS3DH -import ST7789 - -@main -public struct AccelerationVisualizer { - public static func main() { - // Initialize the SPI pin and the digital pins for the LCD. - let spi = SPI(Id.SPI0, speed: 30_000_000) - let cs = DigitalOut(Id.D5) - let dc = DigitalOut(Id.D4) - let rst = DigitalOut(Id.D3) - let bl = DigitalOut(Id.D2) - // Initialize the LCD using the pins above. Rotate the screen to keep the original at the upper left. - let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) - - // Initialize the accelerometer using I2C communication. - let i2c = I2C (Id.I2C0) - let accelerometer = LIS3DH(i2c) - - // 16-bit colors for acceleration bars. - let red: UInt16 = 0x07E0 - let green: UInt16 = 0x001F - let blue: UInt16 = 0xF800 - - // Get the acceleration range of the sensor. - let gRange: Int - switch accelerometer.getRange() { - case .g2: gRange = 4 - case .g4: gRange = 8 - case .g8: gRange = 16 - case .g16: gRange = 32 - } - - // Draw the bars of accelerations on x, y, z-axis. - let barWidth = 200 - let barHeight = 40 - let spacer = 20 - let startY = (screen.height - barHeight * 3 - spacer * 2) / 2 - - var xBar = Bar(y: startY, width: barWidth, height: barHeight, color: red, screen: screen) - var yBar = Bar(y: startY + barHeight + spacer, width: barWidth, height: barHeight, color: green, screen: screen) - var zBar = Bar(y: startY + (barHeight + spacer) * 2, width: barWidth, height: barHeight, color: blue, screen: screen) - - while true { - // Update the indicators' position in each bar according to the current accelerations. - let values = accelerometer.readXYZ() - xBar.update(values.x, gRange: gRange) - yBar.update(values.y, gRange: gRange) - zBar.update(values.z, gRange: gRange) - sleep(ms: 10) - } - } - - struct Bar { - let x: Int - let y: Int - let width: Int - let height: Int - let screen: ST7789 - let color: UInt16 - - var indicatorPos: Int? - let indicatorColor: UInt16 = 0xFFFF - - // Draw a bar on the screen. - init(y: Int, width: Int, height: Int, color: UInt16, screen: ST7789) { - self.y = y - self.width = width - self.height = height - self.color = color - self.screen = screen - x = (screen.width - width) / 2 - - let data = [UInt16](repeating: color, count: width * height) - data.withUnsafeBytes { - screen.writeBitmap(x: x, y: y, width: width, height: height, data: $0) - } - } - - // Update indicator's position in the bar with the latest value. - mutating func update(_ accel: Float, gRange: Int) { - let currentPos = x + Int((accel + 2) * Float((width - 1) / gRange)) - - if indicatorPos != currentPos { - // Draw the indicator at its current position. - for py in y.. currentHeight { - drawLine(x: i, y: screen.height - lastHeight, height: lastHeight - currentHeight, color: black) - } else if lastHeight < currentHeight { - drawLine(x: i, y: screen.height - currentHeight, height: currentHeight - lastHeight, color: white) - } - } - - sleep(ms: 100) - } - - // Draw a vertical line on the screen. - func drawLine(x: Int, y: Int, height: Int, color: UInt16) { - let buffer = [UInt16](repeating: color, count: height) - buffer.withUnsafeBytes { - screen.writeBitmap(x: x, y: y, width: 1, height: height, data: $0) - } - } - } -} diff --git a/Examples/SwiftIOPlayground/08LCD/AnalogVisualizer/Sources/main.swift b/Examples/SwiftIOPlayground/08LCD/AnalogVisualizer/Sources/main.swift new file mode 100644 index 0000000..49ac9e5 --- /dev/null +++ b/Examples/SwiftIOPlayground/08LCD/AnalogVisualizer/Sources/main.swift @@ -0,0 +1,61 @@ +// Visualize analog values on the screen. +// The line height changes with the value from the potentiometer. + +import SwiftIO +import MadBoard +import ST7789 + + +// Initialize the SPI pin and the digital pins for the LCD. +let spi = SPI(Id.SPI0, speed: 30_000_000) +let cs = DigitalOut(Id.D5) +let dc = DigitalOut(Id.D4) +let rst = DigitalOut(Id.D3) +let bl = DigitalOut(Id.D2) + +// Initialize the LCD using the pins above. Rotate the screen to keep the original at the upper left. +let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) + +// Initialize the analog pin for the potentiometer. +let pot = AnalogIn(Id.A0) + +// The max line height drawn on the screen. +let maxHeight = 200 + +let white: UInt16 = 0xFFFF +let black: UInt16 = 0 + +// Store the previous heights to make a scrolling display. +var heightValues = [Int](repeating: 0, count: screen.width) + +while true { + // Read current analog value and map it to height. + let height = pot.readRawValue() * maxHeight / pot.maxRawValue + + // Update line heights for display. + heightValues.removeFirst() + heightValues.append(height) + + // Iterate over the array to draw vertical lines with the given height. + for i in 0.. currentHeight { + drawLine(x: i, y: screen.height - lastHeight, height: lastHeight - currentHeight, color: black) + } else if lastHeight < currentHeight { + drawLine(x: i, y: screen.height - currentHeight, height: currentHeight - lastHeight, color: white) + } + } + + sleep(ms: 100) +} + +// Draw a vertical line on the screen. +func drawLine(x: Int, y: Int, height: Int, color: UInt16) { + let buffer = [UInt16](repeating: color, count: height) + buffer.withUnsafeBytes { + screen.writeBitmap(x: x, y: y, width: 1, height: height, data: $0) + } +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/08LCD/AnalogVisualizer/Tests/AnalogVisualizerTests/AnalogVisualizerTests.swift b/Examples/SwiftIOPlayground/08LCD/AnalogVisualizer/Tests/AnalogVisualizerTests/AnalogVisualizerTests.swift deleted file mode 100644 index 44029b0..0000000 --- a/Examples/SwiftIOPlayground/08LCD/AnalogVisualizer/Tests/AnalogVisualizerTests/AnalogVisualizerTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import AnalogVisualizer - -final class AnalogVisualizerTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(AnalogVisualizer().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/08LCD/LCD/.gitignore b/Examples/SwiftIOPlayground/08LCD/LCD/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/08LCD/LCD/.gitignore +++ b/Examples/SwiftIOPlayground/08LCD/LCD/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/08LCD/LCD/Package.swift b/Examples/SwiftIOPlayground/08LCD/LCD/Package.swift index 17b153f..91c78e4 100644 --- a/Examples/SwiftIOPlayground/08LCD/LCD/Package.swift +++ b/Examples/SwiftIOPlayground/08LCD/LCD/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "LCD", dependencies: [ @@ -10,18 +12,15 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "LCD", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure - .product(name: "ST7789", package: "MadDrivers") + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "ST7789", package: "MadDrivers"), ]), - .testTarget( - name: "LCDTests", - dependencies: ["LCD"]), ] ) diff --git a/Examples/SwiftIOPlayground/08LCD/LCD/README.md b/Examples/SwiftIOPlayground/08LCD/LCD/README.md deleted file mode 100644 index 95b8013..0000000 --- a/Examples/SwiftIOPlayground/08LCD/LCD/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# LCD - -A description of this package. diff --git a/Examples/SwiftIOPlayground/08LCD/LCD/Sources/LCD/LCD.swift b/Examples/SwiftIOPlayground/08LCD/LCD/Sources/LCD/LCD.swift deleted file mode 100644 index 7e575bb..0000000 --- a/Examples/SwiftIOPlayground/08LCD/LCD/Sources/LCD/LCD.swift +++ /dev/null @@ -1,93 +0,0 @@ -// Import SwiftIO to set the communication and MadBoard to use pin id. -import SwiftIO -import MadBoard -// Import the library to configure the LCD and write pixels on it. -import ST7789 - -@main -public struct LCD { - public static func main() { - // Initialize the SPI pin and the digital pins for the LCD. - let spi = SPI(Id.SPI0, speed: 30_000_000) - let cs = DigitalOut(Id.D5) - let dc = DigitalOut(Id.D4) - let rst = DigitalOut(Id.D3) - let bl = DigitalOut(Id.D2) - - // Initialize the LCD using the pins above. Rotate the screen to keep the original at the upper left. - let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) - - // Store some color values for easier reference later. - let black: UInt16 = 0x0000 - let red: UInt16 = 0xF800 - let green: UInt16 = 0x07E0 - let blue: UInt16 = 0x001F - let white: UInt16 = 0xFFFF - - // Fill the whole screen with red, green, blue, white, and black in turns. - // The color changes every second. - screen.clearScreen(red) - sleep(ms: 1000) - - screen.clearScreen(green) - sleep(ms: 1000) - - screen.clearScreen(blue) - sleep(ms: 1000) - - screen.clearScreen(white) - sleep(ms: 1000) - - screen.clearScreen(black) - sleep(ms: 1000) - - // Draw red horizontal lines every 10 rows, so the lines will be on rows 0, 10, 20, ..., 230. - for y in stride(from: 0, to: screen.height, by: 10) { - for x in 0.. UInt16 { - return UInt16(((color & 0xF80000) >> 8) | ((color & 0xFC00) >> 5) | ((color & 0xF8) >> 3)).byteSwapped - } - } -} diff --git a/Examples/SwiftIOPlayground/08LCD/Rainbow/Sources/main.swift b/Examples/SwiftIOPlayground/08LCD/Rainbow/Sources/main.swift new file mode 100644 index 0000000..759fc56 --- /dev/null +++ b/Examples/SwiftIOPlayground/08LCD/Rainbow/Sources/main.swift @@ -0,0 +1,51 @@ +// Fill the screen with rainbow colors. + +import SwiftIO +import MadBoard +import ST7789 + + +// Initialize the SPI pin and the digital pins for the LCD. +let spi = SPI(Id.SPI0, speed: 30_000_000) +let cs = DigitalOut(Id.D5) +let dc = DigitalOut(Id.D4) +let rst = DigitalOut(Id.D3) +let bl = DigitalOut(Id.D2) + +// Initialize the LCD using the pins above. Rotate the screen to keep the original at the upper left. +let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) + +let red: UInt32 = 0xFF0000 +let orange: UInt32 = 0xFF7F00 +let yellow: UInt32 = 0xFFFF00 +let green: UInt32 = 0x00FF00 +let blue: UInt32 = 0x0000FF +let indigo: UInt32 = 0x4B0082 +let violet: UInt32 = 0x9400D3 +let colors888 = [red, orange, yellow, green, blue, indigo, violet] +// Get 16bit color data. +let colors565: [UInt16] = colors888.map { getRGB565LE($0) } + +// The width for each color bar. +let width = screen.width / colors565.count + +while true { + // Use 7 colors in order to draw rectangles on display. + for i in colors565.indices { + for y in 0.. UInt16 { + return UInt16(((color & 0xF80000) >> 8) | ((color & 0xFC00) >> 5) | ((color & 0xF8) >> 3)).byteSwapped +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/08LCD/Rainbow/Tests/RainbowTests/RainbowTests.swift b/Examples/SwiftIOPlayground/08LCD/Rainbow/Tests/RainbowTests/RainbowTests.swift deleted file mode 100644 index 8c4eb62..0000000 --- a/Examples/SwiftIOPlayground/08LCD/Rainbow/Tests/RainbowTests/RainbowTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import Rainbow - -final class RainbowTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(Rainbow().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/08LCD/ScrollEffect/.gitignore b/Examples/SwiftIOPlayground/08LCD/ScrollEffect/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/08LCD/ScrollEffect/.gitignore +++ b/Examples/SwiftIOPlayground/08LCD/ScrollEffect/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Package.swift b/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Package.swift index 9d53f06..56edebd 100644 --- a/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Package.swift +++ b/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "ScrollEffect", dependencies: [ @@ -10,18 +12,15 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "ScrollEffect", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure - .product(name: "ST7789", package: "MadDrivers") + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "ST7789", package: "MadDrivers"), ]), - .testTarget( - name: "ScrollEffectTests", - dependencies: ["ScrollEffect"]), ] ) diff --git a/Examples/SwiftIOPlayground/08LCD/ScrollEffect/README.md b/Examples/SwiftIOPlayground/08LCD/ScrollEffect/README.md deleted file mode 100644 index 7515c5f..0000000 --- a/Examples/SwiftIOPlayground/08LCD/ScrollEffect/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# ScrollEffect - -A description of this package. diff --git a/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Sources/ScrollEffect/ScrollEffect.swift b/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Sources/ScrollEffect/ScrollEffect.swift deleted file mode 100644 index f2f584d..0000000 --- a/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Sources/ScrollEffect/ScrollEffect.swift +++ /dev/null @@ -1,59 +0,0 @@ -// Create a scroll effect to update the display with rainbow colors in turn. -import SwiftIO -import MadBoard -import ST7789 - -@main -public struct ScrollEffect { - public static func main() { - // Initialize the SPI pin and the digital pins for the LCD. - let spi = SPI(Id.SPI0, speed: 30_000_000) - let cs = DigitalOut(Id.D5) - let dc = DigitalOut(Id.D4) - let rst = DigitalOut(Id.D3) - let bl = DigitalOut(Id.D2) - - // Initialize the LCD using the pins above. Rotate the screen to keep the original at the upper left. - let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) - - let red: UInt32 = 0xFF0000 - let orange: UInt32 = 0xFF7F00 - let yellow: UInt32 = 0xFFFF00 - let green: UInt32 = 0x00FF00 - let blue: UInt32 = 0x0000FF - let indigo: UInt32 = 0x4B0082 - let violet: UInt32 = 0x9400D3 - let colors888 = [red, orange, yellow, green, blue, indigo, violet] - // Get 16bit color data. - let colors565: [UInt16] = colors888.map { getRGB565LE($0) } - - let scrollStep = 5 - var buffer = [UInt16](repeating: 0, count: scrollStep * screen.height) - - while true { - // Loop over the colors. - for color in colors565 { - // Move left by updating the area with new color. - for i in 1.. UInt16 { - return UInt16(((color & 0xF80000) >> 8) | ((color & 0xFC00) >> 5) | ((color & 0xF8) >> 3)).byteSwapped - } - } -} - diff --git a/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Sources/main.swift b/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Sources/main.swift new file mode 100644 index 0000000..44282cb --- /dev/null +++ b/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Sources/main.swift @@ -0,0 +1,54 @@ +// Create a scroll effect to update the display with rainbow colors in turn. +import SwiftIO +import MadBoard +import ST7789 + + +// Initialize the SPI pin and the digital pins for the LCD. +let spi = SPI(Id.SPI0, speed: 30_000_000) +let cs = DigitalOut(Id.D5) +let dc = DigitalOut(Id.D4) +let rst = DigitalOut(Id.D3) +let bl = DigitalOut(Id.D2) + +// Initialize the LCD using the pins above. Rotate the screen to keep the original at the upper left. +let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) + +let red: UInt32 = 0xFF0000 +let orange: UInt32 = 0xFF7F00 +let yellow: UInt32 = 0xFFFF00 +let green: UInt32 = 0x00FF00 +let blue: UInt32 = 0x0000FF +let indigo: UInt32 = 0x4B0082 +let violet: UInt32 = 0x9400D3 +let colors888 = [red, orange, yellow, green, blue, indigo, violet] +// Get 16bit color data. +let colors565: [UInt16] = colors888.map { getRGB565LE($0) } + +let scrollStep = 5 +var buffer = [UInt16](repeating: 0, count: scrollStep * screen.height) + +while true { + // Loop over the colors. + for color in colors565 { + // Move left by updating the area with new color. + for i in 1.. UInt16 { + return UInt16(((color & 0xF80000) >> 8) | ((color & 0xFC00) >> 5) | ((color & 0xF8) >> 3)).byteSwapped +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Tests/ScrollEffectTests/ScrollEffectTests.swift b/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Tests/ScrollEffectTests/ScrollEffectTests.swift deleted file mode 100644 index 60aa96f..0000000 --- a/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Tests/ScrollEffectTests/ScrollEffectTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import ScrollEffect - -final class ScrollEffectTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(ScrollEffect().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/.gitignore b/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/.gitignore +++ b/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Package.swift b/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Package.swift index 0668002..18af9fd 100644 --- a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Package.swift +++ b/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "MusicPlayer", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "MusicPlayer", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "MusicPlayerTests", - dependencies: ["MusicPlayer"]), ] -) +) \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/README.md b/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/README.md deleted file mode 100644 index f505bc0..0000000 --- a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# MusicPlayer - -A description of this package. diff --git a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Tests/MusicPlayerTests/MusicPlayerTests.swift b/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Tests/MusicPlayerTests/MusicPlayerTests.swift deleted file mode 100644 index b6ce80e..0000000 --- a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Tests/MusicPlayerTests/MusicPlayerTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import MusicPlayer - -final class MusicPlayerTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(MusicPlayer().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/09Speaker/Speaker/.gitignore b/Examples/SwiftIOPlayground/09Speaker/Speaker/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/09Speaker/Speaker/.gitignore +++ b/Examples/SwiftIOPlayground/09Speaker/Speaker/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/09Speaker/Speaker/Package.swift b/Examples/SwiftIOPlayground/09Speaker/Speaker/Package.swift index 591c48e..5737f34 100644 --- a/Examples/SwiftIOPlayground/09Speaker/Speaker/Package.swift +++ b/Examples/SwiftIOPlayground/09Speaker/Speaker/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "Speaker", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "Speaker", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "SpeakerTests", - dependencies: ["Speaker"]), ] ) diff --git a/Examples/SwiftIOPlayground/09Speaker/Speaker/README.md b/Examples/SwiftIOPlayground/09Speaker/Speaker/README.md deleted file mode 100644 index efab5cc..0000000 --- a/Examples/SwiftIOPlayground/09Speaker/Speaker/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Speaker - -A description of this package. diff --git a/Examples/SwiftIOPlayground/09Speaker/Speaker/Tests/SpeakerTests/SpeakerTests.swift b/Examples/SwiftIOPlayground/09Speaker/Speaker/Tests/SpeakerTests/SpeakerTests.swift deleted file mode 100644 index 15f2716..0000000 --- a/Examples/SwiftIOPlayground/09Speaker/Speaker/Tests/SpeakerTests/SpeakerTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import Speaker - -final class SpeakerTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(Speaker().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/.gitignore b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/.gitignore +++ b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/Package.swift b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/Package.swift index bc81660..a65c1a3 100644 --- a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/Package.swift +++ b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "Button", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "Button", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "ButtonTests", - dependencies: ["Button"]), ] ) diff --git a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/README.md b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/README.md deleted file mode 100644 index a944a74..0000000 --- a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Button - -A description of this package. diff --git a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/Tests/ButtonTests/ButtonTests.swift b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/Tests/ButtonTests/ButtonTests.swift deleted file mode 100644 index 4386beb..0000000 --- a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/Button/Tests/ButtonTests/ButtonTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import Button - -final class ButtonTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(Button().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/.gitignore b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/.gitignore +++ b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/Package.swift b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/Package.swift index 9562da2..1814674 100644 --- a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/Package.swift +++ b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "LED", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "LED", dependencies: [ "SwiftIO", "MadBoards", ]), - .testTarget( - name: "LEDTests", - dependencies: ["LED"]), ] ) diff --git a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/README.md b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/README.md deleted file mode 100644 index fe9c3f4..0000000 --- a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# LED - -A description of this package. diff --git a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/Tests/LEDTests/LEDTests.swift b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/Tests/LEDTests/LEDTests.swift deleted file mode 100644 index 34ba0cd..0000000 --- a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/Tests/LEDTests/LEDTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import LED - -final class LEDTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(LED().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/10UART/SerialEcho/.gitignore b/Examples/SwiftIOPlayground/10UART/SerialEcho/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/10UART/SerialEcho/.gitignore +++ b/Examples/SwiftIOPlayground/10UART/SerialEcho/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/10UART/SerialEcho/Package.swift b/Examples/SwiftIOPlayground/10UART/SerialEcho/Package.swift index e8191a6..0ab3dcd 100644 --- a/Examples/SwiftIOPlayground/10UART/SerialEcho/Package.swift +++ b/Examples/SwiftIOPlayground/10UART/SerialEcho/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "SerialEcho", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "SerialEcho", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "SerialEchoTests", - dependencies: ["SerialEcho"]), ] ) diff --git a/Examples/SwiftIOPlayground/10UART/SerialEcho/README.md b/Examples/SwiftIOPlayground/10UART/SerialEcho/README.md deleted file mode 100644 index 76c78b2..0000000 --- a/Examples/SwiftIOPlayground/10UART/SerialEcho/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# SerialEcho - -A description of this package. diff --git a/Examples/SwiftIOPlayground/10UART/SerialEcho/Tests/SerialEchoTests/SerialEchoTests.swift b/Examples/SwiftIOPlayground/10UART/SerialEcho/Tests/SerialEchoTests/SerialEchoTests.swift deleted file mode 100644 index ec04663..0000000 --- a/Examples/SwiftIOPlayground/10UART/SerialEcho/Tests/SerialEchoTests/SerialEchoTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import SerialEcho - -final class SerialEchoTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(SerialEcho().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/.gitignore b/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/.gitignore +++ b/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/Package.swift b/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/Package.swift index 9044506..270baf8 100644 --- a/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/Package.swift +++ b/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "SerialLEDSwitch", dependencies: [ @@ -9,16 +11,13 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "SerialLEDSwitch", dependencies: [ "SwiftIO", - "MadBoards" + "MadBoards", ]), - .testTarget( - name: "SerialLEDSwitchTests", - dependencies: ["SerialLEDSwitch"]), ] ) diff --git a/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/README.md b/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/README.md deleted file mode 100644 index 46b7297..0000000 --- a/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# SerialLEDSwitch - -A description of this package. diff --git a/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/Tests/SerialLEDSwitchTests/SerialLEDSwitchTests.swift b/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/Tests/SerialLEDSwitchTests/SerialLEDSwitchTests.swift deleted file mode 100644 index 6ce8b14..0000000 --- a/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/Tests/SerialLEDSwitchTests/SerialLEDSwitchTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import SerialLEDSwitch - -final class SerialLEDSwitchTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(SerialLEDSwitch().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/.gitignore b/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/.gitignore +++ b/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Package.swift b/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Package.swift index 1dbd4f1..dcc0bcb 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Package.swift +++ b/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "MorseCode", dependencies: [ @@ -10,18 +12,15 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "MorseCode", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure - .product(name: "ST7789", package: "MadDrivers") + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "ST7789", package: "MadDrivers"), ]), - .testTarget( - name: "MorseCodeTests", - dependencies: ["MorseCode"]), ] ) diff --git a/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/README.md b/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/README.md deleted file mode 100644 index 71a7cb1..0000000 --- a/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# MorseCode - -A description of this package. diff --git a/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Tests/MorseCodeTests/MorseCodeTests.swift b/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Tests/MorseCodeTests/MorseCodeTests.swift deleted file mode 100644 index 822d2b6..0000000 --- a/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Tests/MorseCodeTests/MorseCodeTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import MorseCode - -final class MorseCodeTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(MorseCode().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/.gitignore b/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/.gitignore +++ b/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/Package.swift b/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/Package.swift index 8ed4ac6..f48258a 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/Package.swift +++ b/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "MovingBall", dependencies: [ @@ -10,19 +12,16 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "MovingBall", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure + // Use specific library name rather than "MadDrivers" would speed up the build procedure. .product(name: "ST7789", package: "MadDrivers"), - .product(name: "LIS3DH", package: "MadDrivers") + .product(name: "LIS3DH", package: "MadDrivers"), ]), - .testTarget( - name: "MovingBallTests", - dependencies: ["MovingBall"]), ] ) diff --git a/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/README.md b/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/README.md deleted file mode 100644 index be025ef..0000000 --- a/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# MovingBall - -A description of this package. diff --git a/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/Tests/MovingBallTests/MovingBallTests.swift b/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/Tests/MovingBallTests/MovingBallTests.swift deleted file mode 100644 index 0997023..0000000 --- a/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/Tests/MovingBallTests/MovingBallTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import MovingBall - -final class MovingBallTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(MovingBall().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/11MoreProjects/Pong/.gitignore b/Examples/SwiftIOPlayground/11MoreProjects/Pong/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/Pong/.gitignore +++ b/Examples/SwiftIOPlayground/11MoreProjects/Pong/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/11MoreProjects/Pong/Package.swift b/Examples/SwiftIOPlayground/11MoreProjects/Pong/Package.swift index a6f48d0..70269f4 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/Pong/Package.swift +++ b/Examples/SwiftIOPlayground/11MoreProjects/Pong/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "Pong", dependencies: [ @@ -10,18 +12,15 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "Pong", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure - .product(name: "ST7789", package: "MadDrivers") + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "ST7789", package: "MadDrivers"), ]), - .testTarget( - name: "PongTests", - dependencies: ["Pong"]), ] ) diff --git a/Examples/SwiftIOPlayground/11MoreProjects/Pong/README.md b/Examples/SwiftIOPlayground/11MoreProjects/Pong/README.md deleted file mode 100644 index a3457a9..0000000 --- a/Examples/SwiftIOPlayground/11MoreProjects/Pong/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Pong - -A description of this package. diff --git a/Examples/SwiftIOPlayground/11MoreProjects/Pong/Sound/hitball.wav b/Examples/SwiftIOPlayground/11MoreProjects/Pong/Resources/Sounds/hitball.wav similarity index 100% rename from Examples/SwiftIOPlayground/11MoreProjects/Pong/Sound/hitball.wav rename to Examples/SwiftIOPlayground/11MoreProjects/Pong/Resources/Sounds/hitball.wav diff --git a/Examples/SwiftIOPlayground/11MoreProjects/Pong/Sound/hitwall.wav b/Examples/SwiftIOPlayground/11MoreProjects/Pong/Resources/Sounds/hitwall.wav similarity index 100% rename from Examples/SwiftIOPlayground/11MoreProjects/Pong/Sound/hitwall.wav rename to Examples/SwiftIOPlayground/11MoreProjects/Pong/Resources/Sounds/hitwall.wav diff --git a/Examples/SwiftIOPlayground/11MoreProjects/Pong/Sound/score.wav b/Examples/SwiftIOPlayground/11MoreProjects/Pong/Resources/Sounds/score.wav similarity index 100% rename from Examples/SwiftIOPlayground/11MoreProjects/Pong/Sound/score.wav rename to Examples/SwiftIOPlayground/11MoreProjects/Pong/Resources/Sounds/score.wav diff --git a/Examples/SwiftIOPlayground/11MoreProjects/Pong/Sources/Pong/Game.swift b/Examples/SwiftIOPlayground/11MoreProjects/Pong/Sources/Pong/Game.swift index bd6997c..253100d 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/Pong/Sources/Pong/Game.swift +++ b/Examples/SwiftIOPlayground/11MoreProjects/Pong/Sources/Pong/Game.swift @@ -66,9 +66,9 @@ struct PongGame { screen: screen ) - hitBallSound = readSoundData(from: "/SD:/Sound/hitball.wav") - hitWallSound = readSoundData(from: "/SD:/Sound/hitwall.wav") - scoreSound = readSoundData(from: "/SD:/Sound/score.wav") + hitBallSound = readSoundData(from: "/SD:/Resources/Sounds/hitball.wav") + hitWallSound = readSoundData(from: "/SD:/Resources/Sounds/hitwall.wav") + scoreSound = readSoundData(from: "/SD:/Resources/Sounds/score.wav") leftPlayer.y = getPaddleY(leftPot) rightPlayer.y = getPaddleY(rightPot) diff --git a/Examples/SwiftIOPlayground/11MoreProjects/Pong/Tests/PongTests/PongTests.swift b/Examples/SwiftIOPlayground/11MoreProjects/Pong/Tests/PongTests/PongTests.swift deleted file mode 100644 index 8c1e8b0..0000000 --- a/Examples/SwiftIOPlayground/11MoreProjects/Pong/Tests/PongTests/PongTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import Pong - -final class PongTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(Pong().text, "Hello, World!") - } -} diff --git a/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/.gitignore b/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/.gitignore index 3b29812..0023a53 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/.gitignore +++ b/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/.gitignore @@ -1,9 +1,8 @@ .DS_Store /.build /Packages -/*.xcodeproj xcuserdata/ DerivedData/ -.swiftpm/config/registries.json +.swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc diff --git a/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/Package.swift b/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/Package.swift index 2d610e0..275b9da 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/Package.swift +++ b/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/Package.swift @@ -1,6 +1,8 @@ -// swift-tools-version:5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. + import PackageDescription + let package = Package( name: "TicTacToe", dependencies: [ @@ -10,18 +12,15 @@ let package = Package( .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), ], targets: [ - // Targets are the basic building blocks of a package. A target can define a module or a test suite. - // Targets can depend on other targets in this package, and on products in packages this package depends on. + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "TicTacToe", dependencies: [ "SwiftIO", "MadBoards", - // use specific library would speed up the compile procedure - .product(name: "ST7789", package: "MadDrivers") + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "ST7789", package: "MadDrivers"), ]), - .testTarget( - name: "TicTacToeTests", - dependencies: ["TicTacToe"]), ] ) diff --git a/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/README.md b/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/README.md deleted file mode 100644 index d3ec9f4..0000000 --- a/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# TicTacToe - -A description of this package. diff --git a/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/Tests/TicTacToeTests/TicTacToeTests.swift b/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/Tests/TicTacToeTests/TicTacToeTests.swift deleted file mode 100644 index 76be0e0..0000000 --- a/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/Tests/TicTacToeTests/TicTacToeTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -import XCTest -@testable import TicTacToe - -final class TicTacToeTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(TicTacToe().text, "Hello, World!") - } -}