davecheney / gpio

GPIO for Go
BSD 2-Clause "Simplified" License
228 stars 52 forks source link

Add function to return PCB revision #16

Open rgl opened 10 years ago

rgl commented 10 years ago

To get the PCB revision (because at least two pins are different) you should add to gpio_linux.go something like:

var pcbRevision int

func PcbRevision() int {
    return pcbRevision
}

func getPcbRevision() int {
    r := regexp.MustCompile(`Revision\s*:\s*[0-9a-f]*([0-9a-f]{4})`)

    // See http://elinux.org/RPi_HardwareHistory#Board_Revision_History
    bytes, err := ioutil.ReadFile("/proc/cpuinfo")

    if err != nil {
        panic(err)
    }

    result := r.FindSubmatch(bytes)

    if result == nil {
        panic("Failed to get Revision from /proc/cpuinfo")
    }

    revision, err := strconv.ParseInt(string(result[1]), 16, 0)

    if err != nil {
        panic("Failed to parse Revision from /proc/cpuinfo")
    }

    if revision < 4 {
        return 1
    } else {
        return 2
    }
}

func init() {
    pcbRevision = getPcbRevision()
}