Skip to content

Commit

Permalink
Embedded compatiable
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Liu <andy@madmachine.io>
  • Loading branch information
andy0808 committed Oct 8, 2024
1 parent b00ca76 commit a3abaa4
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ while true {
}

// Calculate the time in ns.
let duration = cyclesToNanoseconds(start: startClockCycle, stop: getClockCycle())
let ns = cyclesToNanoseconds(start: startClockCycle, stop: getClockCycle())
let duration = ns / 1000_000

// Turn off the indicator.
led.low()
print("Reflex time: \(Float(duration) / 1000_000)ms")
print("Reflex time: \(duration)ms")
}

sleep(ms: 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,22 @@ while true {
sum += humiture.readCelsius()
sleep(ms: 3)
}
let temperature = sum / 20.0
let string = getFloatString(temperature)

// Print the average temperature in celsius.
print("Temperature: \(sum / 20.0)C")
print("Temperature: " + string + "C")
startMeasurement = false
// Turn off the indicator.
led.low()
}

sleep(ms: 20)
}


func getFloatString(_ num: Float) -> String {
let int = Int(num)
let frac = Int((num - Float(int)) * 100)
return "\(int).\(frac)"
}
13 changes: 11 additions & 2 deletions Examples/SwiftIOPlayground/05Humiture/Humiture/Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ let humiture = SHT3x(i2c)
while true {
let temp = humiture.readCelsius()
let humidity = humiture.readHumidity()
print("Temperature: \(temp)C")
print("Humidity: \(humidity)%")
let tempStr = getFloatString(temp)
let humidityStr = getFloatString(humidity)

print("Temperature: " + tempStr + "C")
print("Humidity: " + humidityStr + "%")
sleep(ms: 1000)
}

func getFloatString(_ num: Float) -> String {
let int = Int(num)
let frac = Int((num - Float(int)) * 100)
return "\(int).\(frac)"
}
26 changes: 24 additions & 2 deletions Examples/SwiftIOPlayground/06RTC/Alarm/Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ let buzzer = PWMOut(Id.PWM5A)
// Stop the alarm after it goes off.
let stopButton = DigitalIn(Id.D1)

let daysOfWeek = [
"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday",
"Sunday"
]

// Set the alarm time.
let alarm = AlarmTime(hour: 10, minute: 40)
// Calculate the time when the alarm will stop sounding.
let stopAlarm = getStopAlarm(alarm, after: 1)
var isAlarmed = false


while true {
let time = rtc.readTime()

Expand All @@ -29,15 +36,15 @@ while true {
// if it's time to stop sounding or you press the stop button,
// stop the sound and turn off the LED.
if stopAlarm.isTimeUp(time) || stopButton.read() {
print("Current time: \(time)")
print("Current time: " + formatDateTime(time))
led.low()
buzzer.suspend()
isAlarmed = false
}
} else {
// If the time comes, start the sound and turn on the LED.
if alarm.isTimeUp(time) {
print("Current time: \(time)")
print("Current time: " + formatDateTime(time))
led.high()
buzzer.set(frequency: 500, dutycycle: 0.5)
isAlarmed = true
Expand All @@ -47,6 +54,21 @@ while true {
sleep(ms: 10)
}

// Add leading zero if number is one-digit.
// For example, number 1 will be 01.
func formatNum(_ number: UInt8) -> String {
return number < 10 ? "0\(number)" : "\(number)"
}

// Format the date and time, i.e. 2023/03/01 Wednesday 16:20:00.
func formatDateTime(_ time: PCF8563.Time) -> String {
var string = ""
string += "\(time.year)" + "/" + formatNum(time.month) + "/" + formatNum(time.day)
string += " " + daysOfWeek[Int(time.dayOfWeek)] + " "
string += formatNum(time.hour) + ":" + formatNum(time.minute) + ":" + formatNum(time.second)
return string
}

// Calculate the time for alarm to stop sounding after specified minutes.
func getStopAlarm(_ alarm: AlarmTime, after min: Int) -> AlarmTime {
var stopMinute = alarm.minute + min
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@ let accelerometer = LIS3DH(i2c)
// Read the accelerations and each of them.
while true {
let accelerations = accelerometer.readXYZ()
print("x: \(accelerations.x)g")
print("y: \(accelerations.y)g")
print("z: \(accelerations.z)g")
let xValue = getFloatString(accelerations.x) + "g"
let yValue = getFloatString(accelerations.y) + "g"
let zValue = getFloatString(accelerations.z) + "g"

print("x: " + xValue)
print("y: " + yValue)
print("z: " + zValue)
print("\n")

sleep(ms: 1000)
}


func getFloatString(_ num: Float) -> String {
let int = Int(num)
let frac = Int((num - Float(int)) * 100)
return "\(int).\(frac)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ timer.setInterrupt(start: false) {
reset = true
}

sleep(ms: 1000)
print("Tilt/move your board to start.")

while true {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import MadBoard


let uart = UART(Id.UART0)
let led = DigitalOut(Id.BLUE)
let led = DigitalOut(Id.BLUE, value: true)

var buffer: [UInt8] = []

Expand All @@ -22,18 +22,22 @@ while true {

// Match the message and change the LED state.
if buffer.count > 0 {
// Add a null symbol at the end.
buffer.append(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)
print("Received command: " + command)

buffer.removeAll()

switch command {
case "on":
led.high()
case "off":
led.low()
case "off":
led.high()
default: break
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@ public struct WritingCSVFile {
let i2c = I2C(Id.I2C0)
let humiture = SHT3x(i2c)
let rtc = PCF8563(i2c)
let led = DigitalOut(Id.D18, value: true)

// 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
sleep(ms: 500)

do {
// Create a csv file on SD card.
Expand All @@ -29,11 +23,22 @@ public struct WritingCSVFile {
print(error)
}

// 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
)
let startMinute = currentTime.minute
var previousSecond = currentTime.second

rtc.setTime(currentTime)

while true {
let time = rtc.readTime()

// Read and store the temperature at the start of every minute.
if time.second == 0 && !written {
// Read and store the temperature every second for a duration of 1 minute.
if time.second != previousSecond && time.minute == startMinute {
previousSecond = time.second
do {
let file = try FileDescriptor.open("/SD:/temperature.csv")
// Move file offset to the end in order to store new values.
Expand All @@ -53,14 +58,14 @@ public struct WritingCSVFile {

try file.close()
} catch {
led.low()
print(error)
}
written = true
} else if time.second == 59 && written {
written = false
} else if time.minute != startMinute {
led.low()
}

sleep(ms: 10)
sleep(ms: 50)
}

func formatNum(_ number: UInt8) -> String {
Expand Down

0 comments on commit a3abaa4

Please sign in to comment.