diff --git a/Examples/SwiftIOPlayground/12MoreProjects/Prank/.gitignore b/Examples/SwiftIOPlayground/12MoreProjects/Prank/.gitignore new file mode 100644 index 0000000..0023a53 --- /dev/null +++ b/Examples/SwiftIOPlayground/12MoreProjects/Prank/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/Examples/SwiftIOPlayground/12MoreProjects/Prank/Package.mmp b/Examples/SwiftIOPlayground/12MoreProjects/Prank/Package.mmp new file mode 100644 index 0000000..102d0e2 --- /dev/null +++ b/Examples/SwiftIOPlayground/12MoreProjects/Prank/Package.mmp @@ -0,0 +1,17 @@ +# This is a MadMachine project file in TOML format +# This file holds those parameters that could not be managed by SwiftPM +# Edit this file would change the behavior of the building/downloading procedure +# Those project files in the dependent libraries would be IGNORED + +# Specify the board name below +# There are "SwiftIOBoard" and "SwiftIOMicro" now +board = "SwiftIOMicro" + +# Specifiy the target triple below +# There are "thumbv7em-unknown-none-eabi" and "thumbv7em-unknown-none-eabihf" now +# If your code use significant floating-point calculation, +# plz set it to "thumbv7em-unknown-none-eabihf" +triple = "thumbv7em-unknown-none-eabi" + +# Reserved for future use +version = 1 diff --git a/Examples/SwiftIOPlayground/12MoreProjects/Prank/Package.swift b/Examples/SwiftIOPlayground/12MoreProjects/Prank/Package.swift new file mode 100644 index 0000000..3ca2e24 --- /dev/null +++ b/Examples/SwiftIOPlayground/12MoreProjects/Prank/Package.swift @@ -0,0 +1,26 @@ +// 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: "Prank", + dependencies: [ + // Dependencies declare other packages that this package depends on. + .package(url: "https://github.com/madmachineio/SwiftIO.git", branch: "main"), + .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), + .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), + ], + targets: [ + // 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: "Prank", + dependencies: [ + "SwiftIO", + "MadBoards", + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "LIS3DH", package: "MadDrivers") + ]), + ] +) diff --git a/Examples/SwiftIOPlayground/12MoreProjects/Prank/Resources/Sounds/Laugh.wav b/Examples/SwiftIOPlayground/12MoreProjects/Prank/Resources/Sounds/Laugh.wav new file mode 100644 index 0000000..edc0846 Binary files /dev/null and b/Examples/SwiftIOPlayground/12MoreProjects/Prank/Resources/Sounds/Laugh.wav differ diff --git a/Examples/SwiftIOPlayground/12MoreProjects/Prank/Resources/Sounds/Pigs.wav b/Examples/SwiftIOPlayground/12MoreProjects/Prank/Resources/Sounds/Pigs.wav new file mode 100644 index 0000000..4833109 Binary files /dev/null and b/Examples/SwiftIOPlayground/12MoreProjects/Prank/Resources/Sounds/Pigs.wav differ diff --git a/Examples/SwiftIOPlayground/12MoreProjects/Prank/Resources/Sounds/Scream.wav b/Examples/SwiftIOPlayground/12MoreProjects/Prank/Resources/Sounds/Scream.wav new file mode 100644 index 0000000..427696b Binary files /dev/null and b/Examples/SwiftIOPlayground/12MoreProjects/Prank/Resources/Sounds/Scream.wav differ diff --git a/Examples/SwiftIOPlayground/12MoreProjects/Prank/Sources/main.swift b/Examples/SwiftIOPlayground/12MoreProjects/Prank/Sources/main.swift new file mode 100644 index 0000000..617a812 --- /dev/null +++ b/Examples/SwiftIOPlayground/12MoreProjects/Prank/Sources/main.swift @@ -0,0 +1,59 @@ +// A simple prank idea: play a high-volume sound effect to scare people. +// Hang the kit on a door, place it under a chair, or put it on any object your friend might move. + +import SwiftIO +import MadBoard +import LIS3DH + +let i2c = I2C(Id.I2C0) +let accelerometer = LIS3DH(i2c) + +let speaker = I2S(Id.I2S0, rate: 44_100) + +// The sound files are stored on Resources folder. +// Press Copy Resources button on MadMachine extension for VS code to copy them to SD card/flash. +let screamSound = readSoundData(from: "/lfs/Resources/Sounds/Scream.wav") +let laughSound = readSoundData(from: "/lfs/Resources/Sounds/Laugh.wav") +let pigsSound = readSoundData(from: "/lfs/Resources/Sounds/Pigs.wav") + +var sounds = [screamSound, laughSound, pigsSound] +var index = 0 + +while true { + let acceleration = accelerometer.readXYZ() + // Once the acceleration exceeds the threshold, the speaker will play a sound effect. + // Adjust the threshold to suit your situation. + if acceleration.z > 0.25 { + print(acceleration.z) + speaker.write(sounds[index]) + index = (index + 1) % sounds.count + } + sleep(ms: 2) +} + +func readSoundData(from path: String) -> [UInt8] { + let headerSize = 0x2C + + guard let file = try? FileDescriptor.open(path) else { + print("Read sound data \(path) failed!") + return [] + } + + var buffer = [UInt8]() + + do { + try file.seek(offset: 0, from: FileDescriptor.SeekOrigin.end) + let size = try file.tell() - headerSize + + buffer = [UInt8](repeating: 0, count: size) + try buffer.withUnsafeMutableBytes { rawBuffer in + _ = try file.read(fromAbsoluteOffest: headerSize, into: rawBuffer, count: size) + } + try file.close() + } catch { + print("File \(path) handle error: \(error)") + return [] + } + + return buffer +} \ No newline at end of file diff --git a/README.md b/README.md index 5da47e5..bddfcf1 100644 --- a/README.md +++ b/README.md @@ -6,17 +6,35 @@ [![twitter](https://img.shields.io/twitter/follow/madmachineio?label=%40madmachineio&style=social)](https://twitter.com/madmachineio) -The MadExamples repository contains a set of examples for you to dive into physical programming and learn how to program the boards using Swift. +The MadExamples repository offers a collection of examples designed to help you delve into embedded Swift programming. Explore these resources to master programming microcontrollers using Swift. -## Tutorials +## Get started -These examples come with [detailed tutorials](https://docs.madmachine.io/projects/overview) about background knowledge, circuit connection and code explanation. It's a good idea to follow these guides and then try the examples. +Refer to [this step-by-step tutorial](https://docs.madmachine.io/overview/getting-started/software-prerequisite) for setting up the required software environment and working on a "Hello, World" project. -As for how to run these examples on your board, check [this step-by-step guide](https://docs.madmachine.io/overview/advanced/run-example). -## Examples +## SwiftIOPlayground -### MakerKit +Immerse yourself and become thoroughly acquainted with embedded Swift programming by leveraging the [SwiftIO Playground kit](https://madmachine.io/products/swiftio-playground-kit). + +![SwiftIO Playground Kit](https://madmachine.io/cdn/shop/files/UseKit_fee8d964-e9cf-4d13-bf2e-daa4bc53c165.jpg?v=1709822154&width=1680) + +It contains a series of demo projects to guide you from fundamental concepts, including electronics and Swift programming, and to progressively advanced use cases such as sound generation and graphic display. Additionally, it is complemented by a series of comprehensive [tutorials](https://docs.madmachine.io/learn/introduction) to provide detailed guidance throughout your learning journey. + +* [01LED](./Examples/SwiftIOPlayground/01LED) - start by learning how to blink an LED, which will help you become familiar with digital output. +* [02Button](./Examples/SwiftIOPlayground/02Button) - interact with a button to grasp digital input concepts. +* [03Buzzer](./Examples/SwiftIOPlayground/03Buzzer) - create sound with a buzzer to understand PWM (Pulse Width Modulation). +* [04Potentiometer](./Examples/SwiftIOPlayground/04Potentiometer) - rotate a potentiometer to control an LED or buzzer and explore analog input concepts. +* [05Humiture](./Examples/SwiftIOPlayground/05Humiture) - measure temperature and humidity while learning to utilize I2C communication. +* [06RTC](./Examples/SwiftIOPlayground/06RTC) - retrieve the current time using an RTC via I2C communication. +* [07Accelerometer](./Examples/SwiftIOPlayground/07Accelerometer) - detect movement by reading acceleration data using I2C communication. +* [08LCD](./Examples/SwiftIOPlayground/08LCD) - create graphics on a small screen and explore SPI communication. +* [09Speaker](./Examples/SwiftIOPlayground/09Speaker) - play music through a speaker, grasp essential sound principles, and delve into I2S communication. +* [10UART](./Examples/SwiftIOPlayground/10UART) - learn how to utilize a USB-serial converter to establish communication between your board and other USB devices. +* [11WiFi](./Examples/SwiftIOPlayground/11WiFi) - utilize ESP32 to establish a WiFi connection and transmit/receive data from the internet. +* [12MoreProjects](./Examples/SwiftIOPlayground/12MoreProjects) - engage in more advanced projects by incorporating different modules from the kit. This allows you to apply what you've learned and explore a wider range of possibilities. + +## MakerKit These examples provide dozens of missions that come with the SwiftIO Maker kit to explore all modules. @@ -32,12 +50,3 @@ These examples provide dozens of missions that come with the SwiftIO Maker kit t * [Mission10_Humiture_Sensor](./Examples/MakerKit/Mission10_Humiture_Sensor) - use I2C protocol to read the current temperature and communicate with 16x2 LCD to display the temperature. * [Mission11_Reproduce_Mission10](./Examples/MakerKit/Mission11_Reproduce_Mission10) - display temperature on a 16x2 LCD using external libraries. * [Mission12_Buzzer_Music](./Examples/MakerKit/Mission12_Buzzer_Music) - play a piece of music according to the score. - - -### SwiftIOPlayground - -[These examples](./Examples/SwiftIOPlayground) allow you to get fully acquainted with hardware programming in Swift using the SwiftIO Playground kit. - -A series of demo projects walk you through the most basic knowledge and gradually increase in complexity to show more advanced use cases, like sound and graphic display. It comes with a series of detailed [tutorials](https://docs.madmachine.io/learn/introduction). - -More to come 👀. \ No newline at end of file