-
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
57 changed files
with
1,184 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions
26
Examples/SwiftIOPlayground/12FileSystem/Album/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: "Album", | ||
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: "Album", | ||
dependencies: [ | ||
"SwiftIO", | ||
"MadBoards", | ||
// Use specific library name rather than "MadDrivers" would speed up the build procedure. | ||
.product(name: "ST7789", package: "MadDrivers") | ||
]), | ||
] | ||
) |
861 changes: 861 additions & 0 deletions
861
Examples/SwiftIOPlayground/12FileSystem/Album/Resources/Photo/cat.bin
Large diffs are not rendered by default.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
Examples/SwiftIOPlayground/12FileSystem/Album/Sources/Album.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,38 @@ | ||
import SwiftIO | ||
import MadBoard | ||
import ST7789 | ||
|
||
@main | ||
public struct Album { | ||
public static func main() { | ||
let bl = DigitalOut(Id.D2) | ||
let rst = DigitalOut(Id.D12) | ||
let dc = DigitalOut(Id.D13) | ||
let cs = DigitalOut(Id.D5) | ||
let spi = SPI(Id.SPI0, speed: 30_000_000) | ||
let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) | ||
|
||
// Read the image from the specified path and display it on the screen. | ||
do { | ||
// You can get the binary file of your photo here: https://lvgl.io/tools/imageconverter. | ||
// The output format should be Binary RGB565 Swap. | ||
let file = try FileDescriptor.open("/lfs/Resources/Photo/cat.bin") | ||
|
||
var buffer = [UInt16](repeating: 0, count: 240 * 240) | ||
|
||
try buffer.withUnsafeMutableBytes() { | ||
try file.read(fromAbsoluteOffest: 0, into: $0) | ||
} | ||
|
||
screen.writeScreen(buffer) | ||
|
||
try file.close() | ||
} catch { | ||
print(error) | ||
} | ||
|
||
while true { | ||
sleep(ms: 1000) | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions
26
Examples/SwiftIOPlayground/12FileSystem/ReadingFiles/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: "ReadingFiles", | ||
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: "ReadingFiles", | ||
dependencies: [ | ||
"SwiftIO", | ||
"MadBoards", | ||
// Use specific library name rather than "MadDrivers" would speed up the build procedure. | ||
// .product(name: "MadDrivers", package: "MadDrivers") | ||
]), | ||
] | ||
) |
1 change: 1 addition & 0 deletions
1
Examples/SwiftIOPlayground/12FileSystem/ReadingFiles/Resources/Document/hello.txt
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 @@ | ||
Hello Swift! |
Binary file added
BIN
+1.78 MB
...iftIOPlayground/12FileSystem/ReadingFiles/Resources/Music/twinkle-twinkle-little-star.wav
Binary file not shown.
53 changes: 53 additions & 0 deletions
53
Examples/SwiftIOPlayground/12FileSystem/ReadingFiles/Sources/ReadingFiles.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,53 @@ | ||
import SwiftIO | ||
import MadBoard | ||
|
||
@main | ||
public struct ReadingFiles { | ||
public static func main() { | ||
let speaker = I2S(Id.I2S0, rate: 16_000) | ||
|
||
do { | ||
// let file = try FileDescriptor.open("/SD:/Resources/Document/hello.txt") | ||
let file = try FileDescriptor.open("/lfs/Resources/Document/hello.txt") | ||
|
||
// Repositions the offset to the end of the file. | ||
// Get the current offset to get file size. | ||
try file.seek(offset: 0, from: FileDescriptor.SeekOrigin.end) | ||
let size = try file.tell() | ||
|
||
// Reposition the offset to the beginning of the file and start reading. | ||
var buffer = [UInt8](repeating: 0, count: size) | ||
try file.read(fromAbsoluteOffest: 0, into: &buffer) | ||
|
||
print(String(decoding: buffer[0..<size], as: UTF8.self)) | ||
|
||
try file.close() | ||
} catch { | ||
print(error) | ||
} | ||
|
||
do { | ||
// let file = try FileDescriptor.open("/SD:/Resources/Music/twinkle-twinkle-little-star.wav") | ||
let file = try FileDescriptor.open("/lfs/Resources/Music/twinkle-twinkle-little-star.wav") | ||
|
||
try file.seek(offset: 0, from: FileDescriptor.SeekOrigin.end) | ||
|
||
// WAV file header size. | ||
let headerSize = 0x2C | ||
let size = try file.tell() - headerSize | ||
|
||
var buffer = [UInt8](repeating: 0, count: size) | ||
try file.read(fromAbsoluteOffest: headerSize, into: &buffer, count: size) | ||
|
||
try file.close() | ||
|
||
speaker.write(buffer) | ||
} catch { | ||
print(error) | ||
} | ||
|
||
while true { | ||
sleep(ms: 1000) | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions
27
Examples/SwiftIOPlayground/12FileSystem/WritingCSVFile/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,27 @@ | ||
// 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: "WritingCSVFile", | ||
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: "WritingCSVFile", | ||
dependencies: [ | ||
"SwiftIO", | ||
"MadBoards", | ||
// Use specific library name rather than "MadDrivers" would speed up the build procedure. | ||
.product(name: "SHT3x", package: "MadDrivers"), | ||
.product(name: "PCF8563", package: "MadDrivers") | ||
]), | ||
] | ||
) |
77 changes: 77 additions & 0 deletions
77
Examples/SwiftIOPlayground/12FileSystem/WritingCSVFile/Sources/WritingCSVFile.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,77 @@ | ||
import SwiftIO | ||
import MadBoard | ||
import SHT3x | ||
import PCF8563 | ||
|
||
@main | ||
public struct WritingCSVFile { | ||
public static func main() { | ||
let i2c = I2C(Id.I2C0) | ||
let humiture = SHT3x(i2c) | ||
let rtc = PCF8563(i2c) | ||
|
||
// Update the RTC time if it is not current. | ||
let currentTime = PCF8563.Time( | ||
year: 2024, month: 6, day: 3, hour: 15, | ||
minute: 0, second: 0, dayOfWeek: 0 | ||
) | ||
rtc.setTime(currentTime) | ||
|
||
var written = false | ||
|
||
do { | ||
// Create a csv file on SD card. | ||
let file = try FileDescriptor.open("/SD:/temperature.csv", options: .create) | ||
// Create a table header. | ||
try file.write("Time, Temperature\n") | ||
try file.close() | ||
} catch { | ||
print(error) | ||
} | ||
|
||
while true { | ||
let time = rtc.readTime() | ||
|
||
// Read and store the temperature at the start of every minute. | ||
if time.second == 0 && !written { | ||
do { | ||
let file = try FileDescriptor.open("/SD:/temperature.csv") | ||
// Move file offset to the end in order to store new values. | ||
try file.seek(offset: 0, from: FileDescriptor.SeekOrigin.end) | ||
|
||
// Write time and temperature to the csv file. | ||
// CSV uses commas to separate values and newlines to separate records. | ||
let temp = humiture.readCelsius() | ||
let string = formatDateTime(time) + ", " + String(temp) + "\n" | ||
try file.write(string) | ||
|
||
// Read the file content and print it out. | ||
let size = try file.tell() | ||
var buffer = [UInt8](repeating: 0, count: size) | ||
try file.read(fromAbsoluteOffest: 0, into: &buffer) | ||
print(String(decoding: buffer, as: UTF8.self)) | ||
|
||
try file.close() | ||
} catch { | ||
print(error) | ||
} | ||
written = true | ||
} else if time.second == 59 && written { | ||
written = false | ||
} | ||
|
||
sleep(ms: 10) | ||
} | ||
|
||
func formatNum(_ number: UInt8) -> String { | ||
return number < 10 ? "0\(number)" : "\(number)" | ||
} | ||
|
||
func formatDateTime(_ time: PCF8563.Time) -> String { | ||
var string = "" | ||
string += "\(time.year)" + "/" + formatNum(time.month) + "/" + formatNum(time.day) + " " | ||
string += formatNum(time.hour) + ":" + formatNum(time.minute) + ":" + formatNum(time.second) | ||
return string | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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/13MoreProjects/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 |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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/13MoreProjects/Snake/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 |
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions
8
Examples/SwiftIOPlayground/13MoreProjects/TicTacToe/.gitignore
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/13MoreProjects/TicTacToe/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 |
File renamed without changes.
File renamed without changes.
File renamed without changes.