Skip to content

Commit

Permalink
Add prank project and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Ines333 committed Apr 5, 2024
1 parent 67af891 commit 571a657
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 15 deletions.
8 changes: 8 additions & 0 deletions Examples/SwiftIOPlayground/12MoreProjects/Prank/.gitignore
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 Examples/SwiftIOPlayground/12MoreProjects/Prank/Package.mmp
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 Examples/SwiftIOPlayground/12MoreProjects/Prank/Package.swift
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 not shown.
Binary file not shown.
Binary file not shown.
59 changes: 59 additions & 0 deletions Examples/SwiftIOPlayground/12MoreProjects/Prank/Sources/main.swift
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
}
39 changes: 24 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 👀.

0 comments on commit 571a657

Please sign in to comment.