diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c476369..335ee5f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,4 +16,4 @@ jobs: build: uses: madmachineio/actions/.github/workflows/build.yml@main with: - runners: '["ubuntu-24.04", "macos-13"]' \ No newline at end of file + runners: '["ubuntu-24.04", "macos-15"]' diff --git a/Examples/IS31FL3731/Frame/TODO_Package.swift b/Examples/IS31FL3731/Frame/Package.swift similarity index 92% rename from Examples/IS31FL3731/Frame/TODO_Package.swift rename to Examples/IS31FL3731/Frame/Package.swift index bb6bb3d..7e917af 100644 --- a/Examples/IS31FL3731/Frame/TODO_Package.swift +++ b/Examples/IS31FL3731/Frame/Package.swift @@ -20,8 +20,5 @@ let package = Package( // use specific library would speed up the compile procedure .product(name: "IS31FL3731", package: "MadDrivers") ]), - .testTarget( - name: "FrameTests", - dependencies: ["Frame"]), ] ) diff --git a/Examples/IS31FL3731/Frame/Sources/Frame/main.swift b/Examples/IS31FL3731/Frame/Sources/Frame/main.swift index 71167b5..0afdbf4 100644 --- a/Examples/IS31FL3731/Frame/Sources/Frame/main.swift +++ b/Examples/IS31FL3731/Frame/Sources/Frame/main.swift @@ -3,7 +3,6 @@ import SwiftIO import MadBoard import IS31FL3731 -import MadDisplay let i2c = I2C(Id.I2C0) let led = IS31FL3731(i2c) diff --git a/Examples/IS31FL3731/Frame/Tests/FrameTests/FrameTests.swift b/Examples/IS31FL3731/Frame/Tests/FrameTests/FrameTests.swift deleted file mode 100644 index 45065be..0000000 --- a/Examples/IS31FL3731/Frame/Tests/FrameTests/FrameTests.swift +++ /dev/null @@ -1,47 +0,0 @@ -import XCTest -import class Foundation.Bundle - -final class FrameTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - - // Some of the APIs that we use below are available in macOS 10.13 and above. - guard #available(macOS 10.13, *) else { - return - } - - // Mac Catalyst won't have `Process`, but it is supported for executables. - #if !targetEnvironment(macCatalyst) - - let fooBinary = productsDirectory.appendingPathComponent("Frame") - - let process = Process() - process.executableURL = fooBinary - - let pipe = Pipe() - process.standardOutput = pipe - - try process.run() - process.waitUntilExit() - - let data = pipe.fileHandleForReading.readDataToEndOfFile() - let output = String(data: data, encoding: .utf8) - - XCTAssertEqual(output, "Hello, world!\n") - #endif - } - - /// Returns path to the built products directory. - var productsDirectory: URL { - #if os(macOS) - for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { - return bundle.bundleURL.deletingLastPathComponent() - } - fatalError("couldn't find the products directory") - #else - return Bundle.main.bundleURL - #endif - } -} diff --git a/Examples/IS31FL3731/ScrollingText/TODO_Package.swift b/Examples/IS31FL3731/ScrollingText/Package.swift similarity index 90% rename from Examples/IS31FL3731/ScrollingText/TODO_Package.swift rename to Examples/IS31FL3731/ScrollingText/Package.swift index b23aaf8..02e6c4b 100644 --- a/Examples/IS31FL3731/ScrollingText/TODO_Package.swift +++ b/Examples/IS31FL3731/ScrollingText/Package.swift @@ -8,6 +8,7 @@ let package = Package( .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"), + .package(url: "https://github.com/madmachineio/CFreeType.git", from: "2.13.2"), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. @@ -18,10 +19,8 @@ let package = Package( "SwiftIO", "MadBoards", // use specific library would speed up the compile procedure - .product(name: "IS31FL3731", package: "MadDrivers") + .product(name: "IS31FL3731", package: "MadDrivers"), + "CFreeType", ]), - .testTarget( - name: "ScrollingTextTests", - dependencies: ["ScrollingText"]), ] ) diff --git a/Examples/IS31FL3731/ScrollingText/Resources/Fonts/RobotoMono-VariableFont_wght.ttf b/Examples/IS31FL3731/ScrollingText/Resources/Fonts/RobotoMono-VariableFont_wght.ttf new file mode 100644 index 0000000..3a2d704 Binary files /dev/null and b/Examples/IS31FL3731/ScrollingText/Resources/Fonts/RobotoMono-VariableFont_wght.ttf differ diff --git a/Examples/IS31FL3731/ScrollingText/Sources/ScrollingText/main.swift b/Examples/IS31FL3731/ScrollingText/Sources/ScrollingText/main.swift index 28c2fb0..24a0157 100644 --- a/Examples/IS31FL3731/ScrollingText/Sources/ScrollingText/main.swift +++ b/Examples/IS31FL3731/ScrollingText/Sources/ScrollingText/main.swift @@ -2,15 +2,23 @@ import SwiftIO import MadBoard import IS31FL3731 -import MadDisplay +import MadGraphics let i2c = I2C(Id.I2C0) -let led = IS31FL3731(i2c) -let display = MadDisplay(screen: led) -let group = Group() +let screen = IS31FL3731(i2c) -let text = Label(y: 4, text: "Hello", font: ASCII8()) -group.append(text) +var screenBuffer = [UInt8](repeating: 0, count: screen.width * screen.height) +var frameBuffer = [UInt32](repeating: 0, count: screen.width * screen.height) + +let robotoFont = Font(path: "/lfs/Resources/Fonts/RobotoMono-VariableFont_wght.ttf" , pointSize: 12) + +let rootLayer = Layer(width: screen.width, height: screen.height, backgroundColor: Pixel.black) +rootLayer.render(into: &frameBuffer, output: &screenBuffer, transform: Pixel.toGray) { dirty, data in + screen.writeBitmap(x: dirty.x, y: dirty.y, width: dirty.width, height: dirty.height, data: data) +} + +let text = TextLayer(at: Point(x: 0, y: 0), string: "Hello", font: robotoFont, foregroundColor: Pixel.white) +rootLayer.append(text) // It is decided by the width of your text. let xmin = -24 @@ -20,10 +28,21 @@ let xmax = 16 while true { // The text will scroll from right to left. for x in (xmin...xmax).reversed() { - text.setX(x) - display.update(group) + text.position = Point(x: x, y: 0) + rootLayer.render(into: &frameBuffer, output: &screenBuffer, transform: Pixel.toGray) { dirty, data in + screen.writeBitmap(x: dirty.x, y: dirty.y, width: dirty.width, height: dirty.height, data: data) + } sleep(ms: 100) } } +extension Pixel { + static func toGray(_ pixel: UInt32) -> UInt8 { + let r = (pixel >> 16) & 0xFF + let g = (pixel >> 8) & 0xFF + let b = pixel & 0xFF + // Use the luminosity method to convert RGB to grayscale. + return UInt8(0.299 * Float(r) + 0.587 * Float(g) + 0.114 * Float(b)) + } +} \ No newline at end of file diff --git a/Examples/IS31FL3731/ScrollingText/Tests/ScrollingTextTests/ScrollingTextTests.swift b/Examples/IS31FL3731/ScrollingText/Tests/ScrollingTextTests/ScrollingTextTests.swift deleted file mode 100644 index 36f05dc..0000000 --- a/Examples/IS31FL3731/ScrollingText/Tests/ScrollingTextTests/ScrollingTextTests.swift +++ /dev/null @@ -1,47 +0,0 @@ -import XCTest -import class Foundation.Bundle - -final class ScrollingTextTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - - // Some of the APIs that we use below are available in macOS 10.13 and above. - guard #available(macOS 10.13, *) else { - return - } - - // Mac Catalyst won't have `Process`, but it is supported for executables. - #if !targetEnvironment(macCatalyst) - - let fooBinary = productsDirectory.appendingPathComponent("ScrollingText") - - let process = Process() - process.executableURL = fooBinary - - let pipe = Pipe() - process.standardOutput = pipe - - try process.run() - process.waitUntilExit() - - let data = pipe.fileHandleForReading.readDataToEndOfFile() - let output = String(data: data, encoding: .utf8) - - XCTAssertEqual(output, "Hello, world!\n") - #endif - } - - /// Returns path to the built products directory. - var productsDirectory: URL { - #if os(macOS) - for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { - return bundle.bundleURL.deletingLastPathComponent() - } - fatalError("couldn't find the products directory") - #else - return Bundle.main.bundleURL - #endif - } -} diff --git a/Examples/ST7789/DrawPixels/Sources/DrawPixels/main.swift b/Examples/ST7789/DrawPixels/Sources/DrawPixels/main.swift index 6393be4..9dd0b2b 100644 --- a/Examples/ST7789/DrawPixels/Sources/DrawPixels/main.swift +++ b/Examples/ST7789/DrawPixels/Sources/DrawPixels/main.swift @@ -3,7 +3,7 @@ import SwiftIO import MadBoard import ST7789 -let spi = SPI(Id.SPI0, speed: 50_000_000) +let spi = SPI(Id.SPI0, speed: 30_000_000) let cs = DigitalOut(Id.D9) let dc = DigitalOut(Id.D10) let rst = DigitalOut(Id.D14) @@ -25,4 +25,4 @@ for x in 60..<180 { while true { sleep(ms: 1000) -} \ No newline at end of file +} diff --git a/Examples/ST7789/DrawUsingMadDisplay/Sources/DrawUsingMadDisplay/main.swift b/Examples/ST7789/DrawUsingMadDisplay/Sources/DrawUsingMadDisplay/main.swift deleted file mode 100644 index f455b61..0000000 --- a/Examples/ST7789/DrawUsingMadDisplay/Sources/DrawUsingMadDisplay/main.swift +++ /dev/null @@ -1,49 +0,0 @@ -// Use MadDisplay library to draw shapes and text on the screen. -import SwiftIO -import MadBoard -import ST7789 -import MadDisplay - -let spi = SPI(Id.SPI0, speed: 50_000_000) -let cs = DigitalOut(Id.D9) -let dc = DigitalOut(Id.D10) -let rst = DigitalOut(Id.D14) -let bl = DigitalOut(Id.D2) - -let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) - -let display = MadDisplay(screen: screen) -let group = Group() - -// Draw a yellow background. -let palette = Palette() -palette.append(Color.white) -palette.append(Color.yellow) - -let bitmap = Bitmap(width: 240, height: 240, bitCount: 1) - -for x in 0..<240 { - for y in 0..<240 { - bitmap.setPixel(x:x, y:y, 1) - } -} - -let tile = Tile(bitmap: bitmap, palette: palette) -group.append(tile) -display.update(group) -sleep(ms: 1000) - -// Draw an orange rectangle. -let rect = Rect(x: 20, y: 20, width: 200, height: 200, fill: Color.orange) -group.append(rect) -display.update(group) -sleep(ms: 1000) - -// Display "Hello world!" on the screen. -let label = Label(x: 100, y: 100, text: "Hello world!", color: Color.white) -group.append(label) -display.update(group) - -while true { - sleep(ms: 1000) -} \ No newline at end of file diff --git a/Examples/ST7789/DrawUsingMadDisplay/Tests/DrawUsingMadDisplayTests/DrawUsingMadDisplayTests.swift b/Examples/ST7789/DrawUsingMadDisplay/Tests/DrawUsingMadDisplayTests/DrawUsingMadDisplayTests.swift deleted file mode 100644 index e6e5c87..0000000 --- a/Examples/ST7789/DrawUsingMadDisplay/Tests/DrawUsingMadDisplayTests/DrawUsingMadDisplayTests.swift +++ /dev/null @@ -1,47 +0,0 @@ -import XCTest -import class Foundation.Bundle - -final class DrawUsingMadDisplayTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - - // Some of the APIs that we use below are available in macOS 10.13 and above. - guard #available(macOS 10.13, *) else { - return - } - - // Mac Catalyst won't have `Process`, but it is supported for executables. - #if !targetEnvironment(macCatalyst) - - let fooBinary = productsDirectory.appendingPathComponent("DrawUsingMadDisplay") - - let process = Process() - process.executableURL = fooBinary - - let pipe = Pipe() - process.standardOutput = pipe - - try process.run() - process.waitUntilExit() - - let data = pipe.fileHandleForReading.readDataToEndOfFile() - let output = String(data: data, encoding: .utf8) - - XCTAssertEqual(output, "Hello, world!\n") - #endif - } - - /// Returns path to the built products directory. - var productsDirectory: URL { - #if os(macOS) - for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { - return bundle.bundleURL.deletingLastPathComponent() - } - fatalError("couldn't find the products directory") - #else - return Bundle.main.bundleURL - #endif - } -} diff --git a/Examples/ST7789/DrawUsingMadDisplay/.gitignore b/Examples/ST7789/DrawUsingMadGraphics/.gitignore similarity index 100% rename from Examples/ST7789/DrawUsingMadDisplay/.gitignore rename to Examples/ST7789/DrawUsingMadGraphics/.gitignore diff --git a/Examples/ST7789/DrawUsingMadDisplay/Package.mmp b/Examples/ST7789/DrawUsingMadGraphics/Package.mmp similarity index 100% rename from Examples/ST7789/DrawUsingMadDisplay/Package.mmp rename to Examples/ST7789/DrawUsingMadGraphics/Package.mmp diff --git a/Examples/ST7789/DrawUsingMadDisplay/TODO_Package.swift b/Examples/ST7789/DrawUsingMadGraphics/Package.swift similarity index 72% rename from Examples/ST7789/DrawUsingMadDisplay/TODO_Package.swift rename to Examples/ST7789/DrawUsingMadGraphics/Package.swift index 98e22a6..531515b 100644 --- a/Examples/ST7789/DrawUsingMadDisplay/TODO_Package.swift +++ b/Examples/ST7789/DrawUsingMadGraphics/Package.swift @@ -2,28 +2,25 @@ // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription let package = Package( - name: "DrawUsingMadDisplay", + name: "DrawUsingMadGraphics", 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"), - .package(url: "https://github.com/madmachineio/MadDisplay.git", branch: "main"), + .package(url: "https://github.com/madmachineio/CFreeType.git", from: "2.13.2"), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. // Targets can depend on other targets in this package, and on products in packages this package depends on. .executableTarget( - name: "DrawUsingMadDisplay", + name: "DrawUsingMadGraphics", dependencies: [ "SwiftIO", "MadBoards", - "MadDisplay", // use specific library would speed up the compile procedure - .product(name: "ST7789", package: "MadDrivers") + .product(name: "ST7789", package: "MadDrivers"), + "CFreeType", ]), - .testTarget( - name: "DrawUsingMadDisplayTests", - dependencies: ["DrawUsingMadDisplay"]), ] ) diff --git a/Examples/ST7789/DrawUsingMadGraphics/README.md b/Examples/ST7789/DrawUsingMadGraphics/README.md new file mode 100644 index 0000000..ad3e43a --- /dev/null +++ b/Examples/ST7789/DrawUsingMadGraphics/README.md @@ -0,0 +1,17 @@ +# DrawUsingMadGraphics + +Use another library `MadGraphics` to draw graphics on the LCD. You'll fill the screen with yellow, draw an orange square in the middle and display the text "Hello world!". + +## Library reference + +* [SwiftIO](https://github.com/madmachineio/SwiftIO) +* [MadBoard](https://github.com/madmachineio/MadBoards) +* [ST7789](https://github.com/madmachineio/MadDrivers/tree/main/Sources/ST7789/ST7789.swift) +* [MadGraphics](https://madmachineio.github.io/MadGraphicsDocs/documentation/madgraphics/) + + +## How to use + +1. Download the MadDrivers folder. +2. Open and run one of the examples in Visual Studio Code. If you are not familiar with the operation, you could refer to [this tutorial](https://docs.madmachine.io/overview/advanced/run-example). + diff --git a/Examples/ST7789/DrawUsingMadGraphics/Resources/Fonts/Roboto-Italic.ttf b/Examples/ST7789/DrawUsingMadGraphics/Resources/Fonts/Roboto-Italic.ttf new file mode 100644 index 0000000..1b5eaa3 Binary files /dev/null and b/Examples/ST7789/DrawUsingMadGraphics/Resources/Fonts/Roboto-Italic.ttf differ diff --git a/Examples/ST7789/DrawUsingMadGraphics/Resources/Fonts/Roboto-Regular.ttf b/Examples/ST7789/DrawUsingMadGraphics/Resources/Fonts/Roboto-Regular.ttf new file mode 100644 index 0000000..ddf4bfa Binary files /dev/null and b/Examples/ST7789/DrawUsingMadGraphics/Resources/Fonts/Roboto-Regular.ttf differ diff --git a/Examples/ST7789/DrawUsingMadGraphics/Sources/DrawUsingMadGraphics/main.swift b/Examples/ST7789/DrawUsingMadGraphics/Sources/DrawUsingMadGraphics/main.swift new file mode 100644 index 0000000..b7dbff2 --- /dev/null +++ b/Examples/ST7789/DrawUsingMadGraphics/Sources/DrawUsingMadGraphics/main.swift @@ -0,0 +1,43 @@ +// Use MadGraphics library to draw shapes and text on the screen. +import SwiftIO +import MadBoard +import ST7789 +import MadGraphics + + +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) +var screenBuffer = [UInt16](repeating: 0, count: screen.width * screen.height) +var frameBuffer = [UInt32](repeating: 0, count: screen.width * screen.height) + +let robotoFont = Font(path: "/lfs/Resources/Fonts/Roboto-Regular.ttf" , pointSize: 12, dpi: 220) + +let rootLayer = Layer(width: screen.width, height: screen.height, backgroundColor: Pixel.yellow) +rootLayer.render(into: &frameBuffer, output: &screenBuffer, transform: Pixel.toRGB565LE) { dirty, data in + screen.writeBitmap(x: dirty.x, y: dirty.y, width: dirty.width, height: dirty.height, data: data) +} +sleep(ms: 1000) + +let rect = Layer(at: Point(x: 20, y: 20), width: 200, height: 200, backgroundColor: Pixel.orange) +rootLayer.append(rect) +rootLayer.render(into: &frameBuffer, output: &screenBuffer, transform: Pixel.toRGB565LE) { dirty, data in + screen.writeBitmap(x: dirty.x, y: dirty.y, width: dirty.width, height: dirty.height, data: data) +} +sleep(ms: 1000) + +let label = TextLayer(at: Point(x: 120, y: 120), anchorPoint: UnitPoint.center, string: "Hello world!", font: robotoFont, foregroundColor: Pixel.white) +rootLayer.append(label) +rootLayer.render(into: &frameBuffer, output: &screenBuffer, transform: Pixel.toRGB565LE) { dirty, data in + screen.writeBitmap(x: dirty.x, y: dirty.y, width: dirty.width, height: dirty.height, data: data) +} +sleep(ms: 1000) + + +while true { + sleep(ms: 1000) +} \ No newline at end of file diff --git a/Examples/ST7796/DrawPixels/.gitignore b/Examples/ST7796/DrawPixels/.gitignore new file mode 100644 index 0000000..bb460e7 --- /dev/null +++ b/Examples/ST7796/DrawPixels/.gitignore @@ -0,0 +1,7 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ +DerivedData/ +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata diff --git a/Examples/ST7796/DrawPixels/Package.mmp b/Examples/ST7796/DrawPixels/Package.mmp new file mode 100644 index 0000000..102d0e2 --- /dev/null +++ b/Examples/ST7796/DrawPixels/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/ST7796/DrawPixels/Package.swift b/Examples/ST7796/DrawPixels/Package.swift new file mode 100644 index 0000000..acfd701 --- /dev/null +++ b/Examples/ST7796/DrawPixels/Package.swift @@ -0,0 +1,24 @@ +// 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: "DrawPixels", + 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. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .executableTarget( + name: "DrawPixels", + dependencies: [ + "SwiftIO", + "MadBoards", + // use specific library would speed up the compile procedure + .product(name: "ST7796", package: "MadDrivers") + ]), + ] +) diff --git a/Examples/ST7789/DrawUsingMadDisplay/README.md b/Examples/ST7796/DrawPixels/README.md similarity index 65% rename from Examples/ST7789/DrawUsingMadDisplay/README.md rename to Examples/ST7796/DrawPixels/README.md index 4996ea1..f3bb6bc 100644 --- a/Examples/ST7789/DrawUsingMadDisplay/README.md +++ b/Examples/ST7796/DrawPixels/README.md @@ -1,17 +1,16 @@ -# DrawUsingMadDisplay +# DrawPixels -Use another library `MadDisplay` to draw graphics on the LCD. You'll fill the screen with yellow, draw an orange square on the middle and display the text "Hello world!". +Fill the LCD with white and draw a red square on the middle. ## Library reference * [SwiftIO](https://github.com/madmachineio/SwiftIO) * [MadBoard](https://github.com/madmachineio/MadBoards) * [ST7789](https://github.com/madmachineio/MadDrivers/tree/main/Sources/ST7789/ST7789.swift) -* [MadDisplay](https://github.com/madmachineio/MadDisplay) ## How to use 1. Download the MadDrivers folder. 2. Open and run one of the examples in Visual Studio Code. If you are not familiar with the operation, you could refer to [this tutorial](https://docs.madmachine.io/overview/advanced/run-example). - + \ No newline at end of file diff --git a/Examples/ST7796/DrawPixels/Sources/DrawPixels/main.swift b/Examples/ST7796/DrawPixels/Sources/DrawPixels/main.swift new file mode 100644 index 0000000..2f821f7 --- /dev/null +++ b/Examples/ST7796/DrawPixels/Sources/DrawPixels/main.swift @@ -0,0 +1,28 @@ +// Draw a red square on a white background on the screen. +import SwiftIO +import MadBoard +import ST7796 + +let spi = SPI(Id.SPI0, speed: 30_000_000) +let cs = DigitalOut(Id.D1) +let dc = DigitalOut(Id.D5) +let rst = DigitalOut(Id.D0) +let bl = DigitalOut(Id.D2) + +let screen = ST7796(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl) + +let white: UInt16 = 0xFFFF +let red: UInt16 = 0xF800 + +screen.clearScreen(white) +sleep(ms: 1000) + +for y in 100..<150 { + for x in 100..<200 { + screen.writePixel(x: x, y: y, color: red) + } +} + +while true { + sleep(ms: 1000) +} \ No newline at end of file diff --git a/Examples/ST7796/DrawUsingMadGraphics/.gitignore b/Examples/ST7796/DrawUsingMadGraphics/.gitignore new file mode 100644 index 0000000..bb460e7 --- /dev/null +++ b/Examples/ST7796/DrawUsingMadGraphics/.gitignore @@ -0,0 +1,7 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ +DerivedData/ +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata diff --git a/Examples/ST7796/DrawUsingMadGraphics/Package.mmp b/Examples/ST7796/DrawUsingMadGraphics/Package.mmp new file mode 100644 index 0000000..102d0e2 --- /dev/null +++ b/Examples/ST7796/DrawUsingMadGraphics/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/ST7796/DrawUsingMadGraphics/Package.swift b/Examples/ST7796/DrawUsingMadGraphics/Package.swift new file mode 100644 index 0000000..583bd3b --- /dev/null +++ b/Examples/ST7796/DrawUsingMadGraphics/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: "DrawUsingMadGraphics", + 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"), + .package(url: "https://github.com/madmachineio/CFreeType.git", from: "2.13.2"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .executableTarget( + name: "DrawUsingMadGraphics", + dependencies: [ + "SwiftIO", + "MadBoards", + // use specific library would speed up the compile procedure + .product(name: "ST7796", package: "MadDrivers"), + "CFreeType", + ]), + ] +) diff --git a/Examples/ST7796/DrawUsingMadGraphics/README.md b/Examples/ST7796/DrawUsingMadGraphics/README.md new file mode 100644 index 0000000..ad3e43a --- /dev/null +++ b/Examples/ST7796/DrawUsingMadGraphics/README.md @@ -0,0 +1,17 @@ +# DrawUsingMadGraphics + +Use another library `MadGraphics` to draw graphics on the LCD. You'll fill the screen with yellow, draw an orange square in the middle and display the text "Hello world!". + +## Library reference + +* [SwiftIO](https://github.com/madmachineio/SwiftIO) +* [MadBoard](https://github.com/madmachineio/MadBoards) +* [ST7789](https://github.com/madmachineio/MadDrivers/tree/main/Sources/ST7789/ST7789.swift) +* [MadGraphics](https://madmachineio.github.io/MadGraphicsDocs/documentation/madgraphics/) + + +## How to use + +1. Download the MadDrivers folder. +2. Open and run one of the examples in Visual Studio Code. If you are not familiar with the operation, you could refer to [this tutorial](https://docs.madmachine.io/overview/advanced/run-example). + diff --git a/Examples/ST7796/DrawUsingMadGraphics/Resources/Fonts/Roboto-Italic.ttf b/Examples/ST7796/DrawUsingMadGraphics/Resources/Fonts/Roboto-Italic.ttf new file mode 100644 index 0000000..1b5eaa3 Binary files /dev/null and b/Examples/ST7796/DrawUsingMadGraphics/Resources/Fonts/Roboto-Italic.ttf differ diff --git a/Examples/ST7796/DrawUsingMadGraphics/Resources/Fonts/Roboto-Regular.ttf b/Examples/ST7796/DrawUsingMadGraphics/Resources/Fonts/Roboto-Regular.ttf new file mode 100644 index 0000000..ddf4bfa Binary files /dev/null and b/Examples/ST7796/DrawUsingMadGraphics/Resources/Fonts/Roboto-Regular.ttf differ diff --git a/Examples/ST7796/DrawUsingMadGraphics/Sources/DrawUsingMadGraphics/main.swift b/Examples/ST7796/DrawUsingMadGraphics/Sources/DrawUsingMadGraphics/main.swift new file mode 100644 index 0000000..ce40fcf --- /dev/null +++ b/Examples/ST7796/DrawUsingMadGraphics/Sources/DrawUsingMadGraphics/main.swift @@ -0,0 +1,43 @@ +// Use MadGraphics library to draw shapes and text on the screen. +import SwiftIO +import MadBoard +import ST7796 +import MadGraphics + + +let spi = SPI(Id.SPI0, speed: 30_000_000) +let cs = DigitalOut(Id.D1) +let dc = DigitalOut(Id.D5) +let rst = DigitalOut(Id.D0) +let bl = DigitalOut(Id.D2) + +let screen = ST7796(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl) +var screenBuffer = [UInt16](repeating: 0, count: screen.width * screen.height) +var frameBuffer = [UInt32](repeating: 0, count: screen.width * screen.height) + +let robotoFont = Font(path: "/lfs/Resources/Fonts/Roboto-Regular.ttf" , pointSize: 12, dpi: 220) + +let rootLayer = Layer(width: screen.width, height: screen.height, backgroundColor: Pixel.yellow) +rootLayer.render(into: &frameBuffer, output: &screenBuffer, transform: Pixel.toRGB565LE) { dirty, data in + screen.writeBitmap(x: dirty.x, y: dirty.y, width: dirty.width, height: dirty.height, data: data) +} +sleep(ms: 1000) + +let rect = Layer(at: Point(x: 20, y: 20), width: 200, height: 200, backgroundColor: Pixel.orange) +rootLayer.append(rect) +rootLayer.render(into: &frameBuffer, output: &screenBuffer, transform: Pixel.toRGB565LE) { dirty, data in + screen.writeBitmap(x: dirty.x, y: dirty.y, width: dirty.width, height: dirty.height, data: data) +} +sleep(ms: 1000) + +let label = TextLayer(at: Point(x: 120, y: 120), anchorPoint: UnitPoint.center, string: "Hello world!", font: robotoFont, foregroundColor: Pixel.white) +rootLayer.append(label) +rootLayer.render(into: &frameBuffer, output: &screenBuffer, transform: Pixel.toRGB565LE) { dirty, data in + screen.writeBitmap(x: dirty.x, y: dirty.y, width: dirty.width, height: dirty.height, data: data) +} +sleep(ms: 1000) + + +while true { + sleep(ms: 1000) +} \ No newline at end of file