-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
134 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/configuration/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
17 changes: 17 additions & 0 deletions
17
Examples/SwiftIOPlayground/12MoreProjects/Prank/Package.mmp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
26 changes: 26 additions & 0 deletions
26
Examples/SwiftIOPlayground/12MoreProjects/Prank/Package.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
]), | ||
] | ||
) |
Binary file added
BIN
+598 KB
Examples/SwiftIOPlayground/12MoreProjects/Prank/Resources/Sounds/Laugh.wav
Binary file not shown.
Binary file added
BIN
+655 KB
Examples/SwiftIOPlayground/12MoreProjects/Prank/Resources/Sounds/Pigs.wav
Binary file not shown.
Binary file added
BIN
+1.21 MB
Examples/SwiftIOPlayground/12MoreProjects/Prank/Resources/Sounds/Scream.wav
Binary file not shown.
59 changes: 59 additions & 0 deletions
59
Examples/SwiftIOPlayground/12MoreProjects/Prank/Sources/main.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters