Skip to content

Commit

Permalink
Fix/update lcd pinout (#19)
Browse files Browse the repository at this point in the history
* Modify LCD pinout and example structure

Signed-off-by: Andy Liu <andy@madmachine.io>

* Modify LCD pinout

Signed-off-by: Andy Liu <andy@madmachine.io>

* Update button ID for SwiftIO

Signed-off-by: Andy Liu <andy@madmachine.io>

---------

Signed-off-by: Andy Liu <andy@madmachine.io>
  • Loading branch information
andy0808 authored Nov 4, 2023
1 parent 421a06c commit ca4756b
Show file tree
Hide file tree
Showing 25 changed files with 304 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions Examples/SwiftIOPlayground/08LCD/LCD/Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions Examples/SwiftIOPlayground/08LCD/Rainbow/Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
}
12 changes: 6 additions & 6 deletions Examples/SwiftIOPlayground/08LCD/ScrollEffect/Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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)
}

This file was deleted.

102 changes: 102 additions & 0 deletions Examples/SwiftIOPlayground/09Speaker/Speaker/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Import the SwiftIO library to control input and output.
import SwiftIO
// Import the MadBoard to use the id of the pins.
import MadBoard


// 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..<count / 2 {
samples[i] = -amplitude
}
for i in count / 2..<count {
samples[i] = amplitude
}
}

// Generate samples for a triangle wave with a specified amplitude and store the them in an array.
func generateTriangle(amplitude: Int16, _ samples: inout [Int16]) {
let count = samples.count

let step = Float(amplitude) / Float(count / 2)
for i in 0..<count / 4 {
samples[i] = Int16(step * Float(i))
}
for i in count / 4..<count / 4 * 3 {
samples[i] = amplitude - Int16(step * Float(i))
}
for i in count / 4 * 3..<count {
samples[i] = -amplitude + Int16(step * Float(i))
}
}

// Send the samples over I2s bus and play the note with a specified frequency and duration.
func playWave(samples: [Int16], frequency: Float, duration: Float) {
let playCount = Int(duration * Float(sampleRate))
var data = [Int16](repeating: 0, count: playCount)

let step: Float = frequency * Float(samples.count) / Float(sampleRate)

var volume: Float = 1.0
let volumeStep = 1.0 / Float(playCount)

for i in 0..<playCount {
let pos = Int(Float(i) * step) % samples.count
data[i] = Int16(Float(samples[pos]) * volume)
volume -= volumeStep
}
data.withUnsafeBytes { ptr in
let u8Array = ptr.bindMemory(to: UInt8.self)
speaker.write(Array(u8Array))
}
}
Loading

0 comments on commit ca4756b

Please sign in to comment.