Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file system examples #27

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Examples/SwiftIOPlayground/12FileSystem/Album/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: "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 Examples/SwiftIOPlayground/12FileSystem/Album/Resources/Photo/cat.bin

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions Examples/SwiftIOPlayground/12FileSystem/Album/Sources/Album.swift
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)
}
}
}
26 changes: 26 additions & 0 deletions Examples/SwiftIOPlayground/12FileSystem/ReadingFiles/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: "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")
]),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Swift!
Binary file not shown.
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)
}
}
}
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")
]),
]
)
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
}
}
}
8 changes: 8 additions & 0 deletions Examples/SwiftIOPlayground/13MoreProjects/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/13MoreProjects/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
8 changes: 8 additions & 0 deletions Examples/SwiftIOPlayground/13MoreProjects/Snake/.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/13MoreProjects/Snake/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
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/13MoreProjects/TicTacToe/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