beltex / SMCKit

Apple SMC library & tool
https://beltex.github.io/SMCKit
MIT License
473 stars 55 forks source link

How to use in a swift project #16

Closed dducro closed 9 years ago

dducro commented 9 years ago

Hey,

I imported SMC.swift in my project. However I can't get it to print the current Temperature values. The following prints an empty array and 0.0 for every Temperature sensor. Am I missing something?

let smc = SMC()
println(smc.getAllValidTemperatureKeys())
for tempKey in enumerate(SMC.Temperature.allValues) {
    println(smc.getTemperature(tempKey.element.0, unit: SMC.TemperatureUnit.Celsius).0)
}
beltex commented 9 years ago

Hi @dducro!

You have to call the open() method first to get a connection to the SMC driver (and make sure to call close() when your done!)

Here's a simple example:

import IOKit

var smc = SMC()
if smc.open() != kIOReturnSuccess {
    println("ERROR: Failed to open connection to SMC")
} else {
    let temperatureSensors = smc.getAllValidTemperatureKeys()

    for key in temperatureSensors {
        let temperatureSensorName = SMC.Temperature.allValues[key]!
        let temperature           = smc.getTemperature(key).tmp

        println("\(temperatureSensorName): \(temperature)°C")
    }

    smc.close()
}

Take a look at the SMCKitTool source for more detail. Hope that helps!

dducro commented 9 years ago

Awesome! Thanks for the example!

beltex commented 9 years ago

np!

Absolutely, dshb does exactly that :)