Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://pastebin.com/24TGQxWf
- package main
- import (
- "fmt"
- "math"
- "os"
- "strconv"
- "strings"
- "github.com/stianeikeland/go-rpio"
- )
- var strobe func()
- var read func(int) int
- var write func(int)
- func main() {
- // usage: rpicmd board=36 phase=2
- word := 0
- pi := connect()
- for i, a := range os.Args {
- if i != 0 {
- cmds := strings.Split(a, "=")
- cmd := cmds[0]
- val, _ := strconv.Atoi(cmds[1])
- switch cmd {
- case "rw": // both
- val = val & 1
- word += val & 1
- case "sel": // r only
- val = val & 7
- word += (val << 1) & 14
- case "quad": // r only
- val = val & 3
- word += (val << 1) & 6
- case "anadr": // w only
- val = val & 15
- word += (val << 3) & 120
- case "board": // both
- val = val & 127
- word += (val << 8) & 32512
- case "phase": // w only
- val = val & 3
- word += (val << 17) & 393216
- case "pwm": // w only
- val = val & 15
- word += (val << 19) & 7864320
- }
- }
- }
- fmt.Println(word)
- fmt.Println(strconv.FormatInt(int64(word), 2))
- pi.write(word)
- }
- type rpi struct {
- Pins []rpio.Pin
- }
- func connect() rpi {
- err := rpio.Open()
- if err != nil {
- fmt.Println(err)
- }
- pi := rpi{}
- pinids := []int{2, 3, 4, 14, 15, 17, 18, 27, 22, 23, 24, 10, 9, 25, 11, 8, 7, 5, 6, 12, 13, 19, 18, 26, 20, 21}
- for i := range pinids {
- pi.Pins = append(pi.Pins, rpio.Pin(pinids[i]))
- }
- return pi
- }
- func (pi rpi) strobe() {
- pi.Pins[15].Output()
- pi.Pins[15].High()
- pi.Pins[15].Low()
- }
- func (pi rpi) write(word int) {
- if word > 8388607 {
- fmt.Println("word too large,", word, "> 8388607")
- return
- }
- for i, p := range pi.Pins {
- p.Output()
- if word&int(math.Pow(2, float64(i)))>>uint(i) == 1 {
- p.High()
- } else {
- p.Low()
- }
- }
- pi.strobe()
- }
- func (pi rpi) read(cmd int) (data int) {
- write(cmd)
- for i, p := range pi.Pins {
- if i < 8 {
- p.Input()
- } else {
- p.Output()
- }
- }
- pi.strobe()
- for i, p := range pi.Pins {
- if i < 8 {
- data = (data << 1) + int(p.Read())
- }
- }
- return data
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement