diff --git a/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/Sources/main.swift b/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/Sources/main.swift index 9ecc874..191a66c 100644 --- a/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/Sources/main.swift +++ b/Examples/SwiftIOPlayground/08LCD/AccelerationVisualizer/Sources/main.swift @@ -7,11 +7,11 @@ import ST7789 // Initialize the SPI pin and the digital pins for the LCD. -let spi = SPI(Id.SPI0, speed: 30_000_000) -let cs = DigitalOut(Id.D5) -let dc = DigitalOut(Id.D4) -let rst = DigitalOut(Id.D3) 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) // Initialize the LCD using the pins above. Rotate the screen to keep the original at the upper left. let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) diff --git a/Examples/SwiftIOPlayground/08LCD/AnalogVisualizer/Sources/main.swift b/Examples/SwiftIOPlayground/08LCD/AnalogVisualizer/Sources/main.swift index 49ac9e5..6e85318 100644 --- a/Examples/SwiftIOPlayground/08LCD/AnalogVisualizer/Sources/main.swift +++ b/Examples/SwiftIOPlayground/08LCD/AnalogVisualizer/Sources/main.swift @@ -7,11 +7,11 @@ import ST7789 // Initialize the SPI pin and the digital pins for the LCD. -let spi = SPI(Id.SPI0, speed: 30_000_000) -let cs = DigitalOut(Id.D5) -let dc = DigitalOut(Id.D4) -let rst = DigitalOut(Id.D3) 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) // Initialize the LCD using the pins above. Rotate the screen to keep the original at the upper left. let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) diff --git a/Examples/SwiftIOPlayground/08LCD/LCD/Sources/main.swift b/Examples/SwiftIOPlayground/08LCD/LCD/Sources/main.swift index 49b5ed9..3c6db78 100644 --- a/Examples/SwiftIOPlayground/08LCD/LCD/Sources/main.swift +++ b/Examples/SwiftIOPlayground/08LCD/LCD/Sources/main.swift @@ -6,21 +6,21 @@ import ST7789 // Initialize the SPI pin and the digital pins for the LCD. -let spi = SPI(Id.SPI0, speed: 30_000_000) -let cs = DigitalOut(Id.D5) -let dc = DigitalOut(Id.D4) -let rst = DigitalOut(Id.D3) 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) // Initialize the LCD using the pins above. Rotate the screen to keep the original at the upper left. let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) // Store some color values for easier reference later. -let black: UInt16 = 0x0000 -let red: UInt16 = 0xF800 -let green: UInt16 = 0x07E0 -let blue: UInt16 = 0x001F -let white: UInt16 = 0xFFFF +let black = UInt16(0x0000).byteSwapped +let red = UInt16(0xF800).byteSwapped +let green = UInt16(0x07E0).byteSwapped +let blue = UInt16(0x001F).byteSwapped +let white = UInt16(0xFFFF).byteSwapped // Fill the whole screen with red, green, blue, white, and black in turns. // The color changes every second. diff --git a/Examples/SwiftIOPlayground/08LCD/Rainbow/Sources/main.swift b/Examples/SwiftIOPlayground/08LCD/Rainbow/Sources/main.swift index 759fc56..ece9323 100644 --- a/Examples/SwiftIOPlayground/08LCD/Rainbow/Sources/main.swift +++ b/Examples/SwiftIOPlayground/08LCD/Rainbow/Sources/main.swift @@ -6,11 +6,11 @@ import ST7789 // Initialize the SPI pin and the digital pins for the LCD. -let spi = SPI(Id.SPI0, speed: 30_000_000) -let cs = DigitalOut(Id.D5) -let dc = DigitalOut(Id.D4) -let rst = DigitalOut(Id.D3) 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) // Initialize the LCD using the pins above. Rotate the screen to keep the original at the upper left. let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) @@ -24,7 +24,7 @@ let indigo: UInt32 = 0x4B0082 let violet: UInt32 = 0x9400D3 let colors888 = [red, orange, yellow, green, blue, indigo, violet] // Get 16bit color data. -let colors565: [UInt16] = colors888.map { getRGB565LE($0) } +let colors565: [UInt16] = colors888.map { getRGB565BE($0) } // The width for each color bar. let width = screen.width / colors565.count @@ -46,6 +46,6 @@ while true { // The screen needs RGB565 color data, so change color data from UInt32 to UInt16. // Besides, the board uses little endian format, so the bytes are swapped. -func getRGB565LE(_ color: UInt32) -> UInt16 { +func getRGB565BE(_ color: UInt32) -> UInt16 { return UInt16(((color & 0xF80000) >> 8) | ((color & 0xFC00) >> 5) | ((color & 0xF8) >> 3)).byteSwapped } \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Sources/main.swift b/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Sources/main.swift index 44282cb..0de2c9e 100644 --- a/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Sources/main.swift +++ b/Examples/SwiftIOPlayground/08LCD/ScrollEffect/Sources/main.swift @@ -5,11 +5,11 @@ import ST7789 // Initialize the SPI pin and the digital pins for the LCD. -let spi = SPI(Id.SPI0, speed: 30_000_000) -let cs = DigitalOut(Id.D5) -let dc = DigitalOut(Id.D4) -let rst = DigitalOut(Id.D3) 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) // Initialize the LCD using the pins above. Rotate the screen to keep the original at the upper left. let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) @@ -23,7 +23,7 @@ let indigo: UInt32 = 0x4B0082 let violet: UInt32 = 0x9400D3 let colors888 = [red, orange, yellow, green, blue, indigo, violet] // Get 16bit color data. -let colors565: [UInt16] = colors888.map { getRGB565LE($0) } +let colors565: [UInt16] = colors888.map { getRGB565BE($0) } let scrollStep = 5 var buffer = [UInt16](repeating: 0, count: scrollStep * screen.height) @@ -49,6 +49,6 @@ while true { // The screen needs RGB565 color data, so change color data from UInt32 to UInt16. // Besides, the board uses little endian format, so the bytes are swapped. -func getRGB565LE(_ color: UInt32) -> UInt16 { +func getRGB565BE(_ color: UInt32) -> UInt16 { return UInt16(((color & 0xF80000) >> 8) | ((color & 0xFC00) >> 5) | ((color & 0xF8) >> 3)).byteSwapped } \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/MusicPlayer/Mario.swift b/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/Mario.swift similarity index 100% rename from Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/MusicPlayer/Mario.swift rename to Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/Mario.swift diff --git a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/MusicPlayer/MusicPlayer.swift b/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/MusicPlayer/MusicPlayer.swift deleted file mode 100644 index 1d351a3..0000000 --- a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/MusicPlayer/MusicPlayer.swift +++ /dev/null @@ -1,27 +0,0 @@ -// Play a song using a speaker. -import SwiftIO -import MadBoard - -@main -public struct MusicPlayer { - public static func main() { - // The sample rate of I2SOut and Player should be the same. - // Note: the speaker needs stereo channel but only uses the samples of left channel. - // And the frequencies below 200Hz may sound a little fuzzy with this speaker. - let speaker = I2S(Id.I2S0, rate: 16_000) - - // BPM is beat count per minute. - // Timer signature specifies beats per bar and note value of a beat. - let player = Player(speaker, sampleRate: 16_000) - - player.bpm = Mario.bpm - player.timeSignature = Mario.timeSignature - - // Play the music using the tracks. - player.playTracks(Mario.tracks, waveforms: Mario.trackWaveforms, amplitudeRatios: Mario.amplitudeRatios) - - while true { - sleep(ms: 1000) - } - } -} diff --git a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/MusicPlayer/Note.swift b/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/Note.swift similarity index 100% rename from Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/MusicPlayer/Note.swift rename to Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/Note.swift diff --git a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/MusicPlayer/Player.swift b/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/Player.swift similarity index 100% rename from Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/MusicPlayer/Player.swift rename to Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/Player.swift diff --git a/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/main.swift b/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/main.swift new file mode 100644 index 0000000..38b3b53 --- /dev/null +++ b/Examples/SwiftIOPlayground/09Speaker/MusicPlayer/Sources/main.swift @@ -0,0 +1,23 @@ +// Play a song using a speaker. +import SwiftIO +import MadBoard + + +// The sample rate of I2SOut and Player should be the same. +// Note: the speaker needs stereo channel but only uses the samples of left channel. +// And the frequencies below 200Hz may sound a little fuzzy with this speaker. +let speaker = I2S(Id.I2S0, rate: 16_000) + +// BPM is beat count per minute. +// Timer signature specifies beats per bar and note value of a beat. +let player = Player(speaker, sampleRate: 16_000) + +player.bpm = Mario.bpm +player.timeSignature = Mario.timeSignature + +// Play the music using the tracks. +player.playTracks(Mario.tracks, waveforms: Mario.trackWaveforms, amplitudeRatios: Mario.amplitudeRatios) + +while true { + sleep(ms: 1000) +} diff --git a/Examples/SwiftIOPlayground/09Speaker/Speaker/Sources/Speaker/Speaker.swift b/Examples/SwiftIOPlayground/09Speaker/Speaker/Sources/Speaker/Speaker.swift deleted file mode 100644 index 34cc45d..0000000 --- a/Examples/SwiftIOPlayground/09Speaker/Speaker/Sources/Speaker/Speaker.swift +++ /dev/null @@ -1,106 +0,0 @@ -// Import the SwiftIO library to control input and output. -import SwiftIO -// Import the MadBoard to use the id of the pins. -import MadBoard - -@main -public struct Speaker { - public static func main() { - // Initialize the speaker using I2S communication. - // The default setting is 16k sample rate, 16bit sample bits. - let speaker = I2S(Id.I2S0) - - // The frequencies of note C to B in octave 4. - let frequency: [Float] = [ - 261.626, - 293.665, - 329.628, - 349.228, - 391.995, - 440.000, - 493.883 - ] - - // Set the samples of the waveforms. - let sampleRate = 16_000 - let rawSampleLength = 1000 - var rawSamples = [Int16](repeating: 0, count: rawSampleLength) - var amplitude: Int16 = 10_000 - - while true { - - let duration: Float = 1.0 - - // Iterate through the frequencies from C to B to play a scale. - // The sound waveform is a square wave, so you will hear a buzzing sound. - generateSquare(amplitude: amplitude, &rawSamples) - for f in frequency { - playWave(samples: rawSamples, frequency: f, duration: duration) - } - sleep(ms: 1000) - - // Iterate through the frequencies from C to B to play a scale. - // The sound waveform is a triangle wave, and the sound is much softer. - generateTriangle(amplitude: amplitude, &rawSamples) - for f in frequency { - playWave(samples: rawSamples, frequency: f, duration: duration) - } - sleep(ms: 1000) - - // Decrease the amplitude to lower the sound. - // If it's smaller than zero, it restarts from 20000. - amplitude -= 1000 - if amplitude <= 0 { - amplitude = 10_000 - } - } - - // Generate samples for a square wave with a specified amplitude and store them in an array. - func generateSquare(amplitude: Int16, _ samples: inout [Int16]) { - let count = samples.count - for i in 0.. 0 { - var byte: UInt8 = 0 - uart.read(into: &byte) - buffer.append(byte) - sleep(ms: 1) - } - - // Match the message and change the LED state. - if buffer.count > 0 { - // The message from another board is sent in cString - // which means the last data is 0, so you need to convert - // the data in buffer to string using the given encoding. - let command = String(cString: buffer) - - buffer.removeAll() - - switch command { - case "on": - led.high() - case "off": - led.low() - default: break - } - } - - sleep(ms: 10) - } - } -} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/Sources/main.swift b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/Sources/main.swift new file mode 100644 index 0000000..0cfa1ff --- /dev/null +++ b/Examples/SwiftIOPlayground/10UART/RemoteLEDSwitch/LED/Sources/main.swift @@ -0,0 +1,42 @@ +// This code is downloaded to board connected to an LED. +// Wait UART data from other board to turn on/off LED. +// Connect UART0 (RX/TX) of this board to the UART0 (TX/RX) of the other board. + +import SwiftIO +import MadBoard + + +let uart = UART(Id.UART0) +let led = DigitalOut(Id.BLUE) + +var buffer: [UInt8] = [] + +while true { + // Receive all data from another board via UART bus. + while uart.checkBufferReceived() > 0 { + var byte: UInt8 = 0 + uart.read(into: &byte) + buffer.append(byte) + sleep(ms: 1) + } + + // Match the message and change the LED state. + if buffer.count > 0 { + // The message from another board is sent in cString + // which means the last data is 0, so you need to convert + // the data in buffer to string using the given encoding. + let command = String(cString: buffer) + + buffer.removeAll() + + switch command { + case "on": + led.high() + case "off": + led.low() + default: break + } + } + + sleep(ms: 10) +} \ No newline at end of file diff --git a/Examples/SwiftIOPlayground/10UART/SerialEcho/Sources/SerialEcho/SerialEcho.swift b/Examples/SwiftIOPlayground/10UART/SerialEcho/Sources/SerialEcho/SerialEcho.swift deleted file mode 100644 index 621b261..0000000 --- a/Examples/SwiftIOPlayground/10UART/SerialEcho/Sources/SerialEcho/SerialEcho.swift +++ /dev/null @@ -1,30 +0,0 @@ -// Send message from computer to USB-Serial converter using serial monitor. -// Then send the message back using UART. -import SwiftIO -import MadBoard - -@main -public struct SerialEcho { - public static func main() { - let uart = UART(Id.UART0) - - var message = [UInt8](repeating: 0, count: 100) - - while true { - // Check if there is available data in the UART buffer. - let count = uart.checkBufferReceived() - - if count > 0 { - for i in message.indices { - message[i] = 0 - } - // Read data from the buffer. - uart.read(into: &message, count: count) - // Send the message back. - uart.write(Array(message[0.. 0 { + for i in message.indices { + message[i] = 0 + } + // Read data from the buffer. + uart.read(into: &message, count: count) + // Send the message back. + uart.write(Array(message[0.. 0 { - // Read data from UART buffer. - var buffer = [UInt8](repeating: 0, count: count) - uart.read(into: &buffer) - - // Decode the data since the text from computer is sent in UTF8 format. - let command = String(decoding: buffer, as: UTF8.self) - // Connect the port on your micro board in serial monitor to see printed message - print(command) - - // Set digital output according to the command from computer. - switch command { - case "0": led.low() - case "1": led.high() - default: break - } - } - - sleep(ms: 10) - } - } -} diff --git a/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/Sources/main.swift b/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/Sources/main.swift new file mode 100644 index 0000000..c92ea99 --- /dev/null +++ b/Examples/SwiftIOPlayground/10UART/SerialLEDSwitch/Sources/main.swift @@ -0,0 +1,35 @@ +// Connect the USB-Serial converter module to your computer +// and choose the port in serial monitor. +// Send message (1/0) from computer to turn on/off the LED. + +import SwiftIO +import MadBoard + + +let uart = UART(Id.UART0) +let led = DigitalOut(Id.D18) + +while true { + // Check if there is any message from computer. + let count = uart.checkBufferReceived() + + if count > 0 { + // Read data from UART buffer. + var buffer = [UInt8](repeating: 0, count: count) + uart.read(into: &buffer) + + // Decode the data since the text from computer is sent in UTF8 format. + let command = String(decoding: buffer, as: UTF8.self) + // Connect the port on your micro board in serial monitor to see printed message + print(command) + + // Set digital output according to the command from computer. + switch command { + case "0": led.low() + case "1": led.high() + default: break + } + } + + sleep(ms: 10) +} diff --git a/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Package.swift b/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Package.swift index dcc0bcb..7b6cc68 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Package.swift +++ b/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Package.swift @@ -9,7 +9,6 @@ let package = Package( // 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. @@ -19,8 +18,6 @@ let package = Package( dependencies: [ "SwiftIO", "MadBoards", - // Use specific library name rather than "MadDrivers" would speed up the build procedure. - .product(name: "ST7789", package: "MadDrivers"), ]), ] ) diff --git a/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Sources/MorseCode/MorseCode.swift b/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Sources/MorseCode/MorseCode.swift index fc712af..f636722 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Sources/MorseCode/MorseCode.swift +++ b/Examples/SwiftIOPlayground/11MoreProjects/MorseCode/Sources/MorseCode/MorseCode.swift @@ -1,8 +1,6 @@ // Import the SwiftIO library to set input/output and MadBoard to use pin id. import SwiftIO import MadBoard -// Import the driver for the screen. -import ST7789 @main public struct MorseCode { diff --git a/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/Sources/MovingBall/MovingBall.swift b/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/Sources/MovingBall/MovingBall.swift index 14ad7dc..23a9c62 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/Sources/MovingBall/MovingBall.swift +++ b/Examples/SwiftIOPlayground/11MoreProjects/MovingBall/Sources/MovingBall/MovingBall.swift @@ -15,11 +15,11 @@ public struct MovingBall { let accelerometer = LIS3DH(i2c) // Initialize the pins for the screen. - let spi = SPI(Id.SPI0, speed: 30_000_000) - let cs = DigitalOut(Id.D5) - let dc = DigitalOut(Id.D4) - let rst = DigitalOut(Id.D3) 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) // Initialize the screen with the pins above. let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) diff --git a/Examples/SwiftIOPlayground/11MoreProjects/Pong/Sources/Pong/Pong.swift b/Examples/SwiftIOPlayground/11MoreProjects/Pong/Sources/Pong/Pong.swift index d0a434b..e148a41 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/Pong/Sources/Pong/Pong.swift +++ b/Examples/SwiftIOPlayground/11MoreProjects/Pong/Sources/Pong/Pong.swift @@ -14,10 +14,11 @@ public struct Pong { let spi = SPI(Id.SPI0, speed: 30_000_000) // Initialize the pins used for the screen. - let cs = DigitalOut(Id.D5) - let dc = DigitalOut(Id.D4) - let rst = DigitalOut(Id.D3) let bl = DigitalOut(Id.D2) + let rst = DigitalOut(Id.D12) + let dc = DigitalOut(Id.D13) + let cs = DigitalOut(Id.D5) + // Initialize the LCD using the pins above. // Rotate the screen to keep the original at the upper left. let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90) diff --git a/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/Sources/TicTacToe/TicTacToe.swift b/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/Sources/TicTacToe/TicTacToe.swift index e491f00..be06acc 100644 --- a/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/Sources/TicTacToe/TicTacToe.swift +++ b/Examples/SwiftIOPlayground/11MoreProjects/TicTacToe/Sources/TicTacToe/TicTacToe.swift @@ -8,11 +8,11 @@ import ST7789 public struct TicTacToe { public static func main() { // Initialize the SPI pin and the digital pins for the LCD. - let spi = SPI(Id.SPI0, speed: 30_000_000) - let cs = DigitalOut(Id.D5) - let dc = DigitalOut(Id.D4) - let rst = DigitalOut(Id.D3) 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) // Initialize the LCD using the pins above. Rotate the screen to keep the original at the upper left. let screen = ST7789(spi: spi, cs: cs, dc: dc, rst: rst, bl: bl, rotation: .angle90)