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

Fix/modify pwm api #20

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct Mission4_Potentiometer_RGB {
led.toggle()

// Return the percentage of the voltage in the range of 0.0 to 1.0.
let analogValue = a0.readPercent()
let analogValue = a0.readPercentage()
let delayTime = Int(analogValue * 500)

// Stop the program for a certain period based on the value to keep current led state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public struct Mission5_Buzzer {

while true {
// Read the input voltage.
let value = a0.readPercent()
let value = a0.readPercentage()

//let value = a0.readPercent()
//let value = a0.readPercentage()
let frequency = Int(400 + 2000 * value) // calculate the float value into Int type to serve as frequency.
buzzer.set(frequency: frequency, dutycycle: 0.5) // Set PWM parameters.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct Mission7_DC_Motors {

while true {
// Read the input value and use it to set the duty cycle of pwm.
let value = a0.readPercent()
let value = a0.readPercentage()
motor.setDutycycle(value)
sleep(ms: 50)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public struct Mission8_Servo_Motor {
let servo = PWMOut(Id.PWM4A)

while true {
let value = a0.readPercent() // Read the analog value and return a value between 0.0 and 1.0.
let value = a0.readPercentage() // Read the analog value and return a value between 0.0 and 1.0.
let pulse = Int(500 + 2000 * value) // Calculate the value to get the pulse duration.
servo.set(period: 20000, pulse: pulse) // Set the servo position according to the scaled value.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ while true {
// Map the analog value from the range 0-1 to the range minIntensity-maxIntensity.
// It decides the max duty cycle for the PWM signal.
// That's to say, it changes the maximum intensity of the LED.
let maxDutycycle = intensityPot.readPercent() * (maxIntensity - minIntensity) + minIntensity
let maxDutycycle = intensityPot.readPercentage() * (maxIntensity - minIntensity) + minIntensity

// Read the analog value (0-1) from the pin A0 which serves as ratio for final duty cycle.
let dutycycleRatio = brightnessPot.readPercent()
let dutycycleRatio = brightnessPot.readPercentage()

// Calculate the final duty cycle value (0-maxDutycycle).
// Set the PWM output using the result.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ while true {

// Read value from the analog pin.
// It serves as duty cycle for PWM to set LED brightness.
led.setDutycycle(brightnessPot.readPercent())
led.setDutycycle(brightnessPot.readPercentage())
sleep(ms: blinkTime)

// After a specified time, suspend the output to turn off the LED.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let led = PWMOut(Id.PWM4A)
// The value is represented in percentage, while the duty cycle is also between 0 and 1,
// so you can directly use the reading value to set the PWM.
while true {
let dutycycle = pot.readPercent()
let dutycycle = pot.readPercentage()
led.setDutycycle(dutycycle)

sleep(ms: 20)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let buzzer = PWMOut(Id.PWM5A)
// Then calculate the value into the frequency.
// Set the PWM with the frequency and a duty cycle.
while true {
let potPercentage = pot.readPercent()
let potPercentage = pot.readPercentage()
let frequency = 50 + Int(1000 * potPercentage)
buzzer.set(frequency: frequency, dutycycle: 0.5)
sleep(ms: 20)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,26 @@ struct PongGame {
func readSoundData(from path: String) -> [UInt8] {
let headerSize = 0x2C

let file = FileDescriptor.open(path)
defer { file.close() }
let _file = try? FileDescriptor.open(path)
andy0808 marked this conversation as resolved.
Show resolved Hide resolved
guard let file = _file else {
print("Read sound data \(path) failed!")
return []
}

var buffer = [UInt8]()

file.seek(offset: 0, from: FileDescriptor.SeekOrigin.end)
let size = file.tell() - headerSize
do {
try file.seek(offset: 0, from: FileDescriptor.SeekOrigin.end)
let size = try file.tell() - headerSize

var buffer = [UInt8](repeating: 0, count: size)
buffer.withUnsafeMutableBytes { rawBuffer in
_ = file.read(fromAbsoluteOffest: headerSize, into: rawBuffer, count: size)
buffer = [UInt8](repeating: 0, count: size)
try buffer.withUnsafeMutableBytes { rawBuffer in
_ = try file.read(fromAbsoluteOffest: headerSize, into: rawBuffer, count: size)
}
try file.close()
} catch {
print("File \(path) handle error: \(error)")
return []
}

return buffer
Expand Down