Advertisement
Guest User

asdf

a guest
Jul 11th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.05 KB | None | 0 0
  1. //https://pastebin.com/24TGQxWf
  2. package main
  3.  
  4. import (
  5.     "fmt"
  6.     "math"
  7.     "os"
  8.     "strconv"
  9.     "strings"
  10.  
  11.     "github.com/stianeikeland/go-rpio"
  12. )
  13.  
  14. var strobe func()
  15. var read func(int) int
  16. var write func(int)
  17.  
  18. func main() {
  19.     // usage: rpicmd board=36 phase=2
  20.     word := 0
  21.  
  22.     pi := connect()
  23.  
  24.     for i, a := range os.Args {
  25.         if i != 0 {
  26.             cmds := strings.Split(a, "=")
  27.             cmd := cmds[0]
  28.             val, _ := strconv.Atoi(cmds[1])
  29.             switch cmd {
  30.             case "rw": // both
  31.                 val = val & 1
  32.                 word += val & 1
  33.             case "sel": // r only
  34.                 val = val & 7
  35.                 word += (val << 1) & 14
  36.             case "quad": // r only
  37.                 val = val & 3
  38.                 word += (val << 1) & 6
  39.             case "anadr": // w only
  40.                 val = val & 15
  41.                 word += (val << 3) & 120
  42.             case "board": // both
  43.                 val = val & 127
  44.                 word += (val << 8) & 32512
  45.             case "phase": // w only
  46.                 val = val & 3
  47.                 word += (val << 17) & 393216
  48.             case "pwm": // w only
  49.                 val = val & 15
  50.                 word += (val << 19) & 7864320
  51.             }
  52.         }
  53.     }
  54.     fmt.Println(word)
  55.     fmt.Println(strconv.FormatInt(int64(word), 2))
  56.  
  57.     pi.write(word)
  58.  
  59. }
  60.  
  61. type rpi struct {
  62.     Pins []rpio.Pin
  63. }
  64.  
  65. func connect() rpi {
  66.     err := rpio.Open()
  67.     if err != nil {
  68.         fmt.Println(err)
  69.     }
  70.     pi := rpi{}
  71.     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}
  72.     for i := range pinids {
  73.         pi.Pins = append(pi.Pins, rpio.Pin(pinids[i]))
  74.     }
  75.     return pi
  76. }
  77.  
  78. func (pi rpi) strobe() {
  79.     pi.Pins[15].Output()
  80.     pi.Pins[15].High()
  81.     pi.Pins[15].Low()
  82. }
  83.  
  84. func (pi rpi) write(word int) {
  85.     if word > 8388607 {
  86.         fmt.Println("word too large,", word, "> 8388607")
  87.         return
  88.     }
  89.     for i, p := range pi.Pins {
  90.         p.Output()
  91.         if word&int(math.Pow(2, float64(i)))>>uint(i) == 1 {
  92.             p.High()
  93.         } else {
  94.             p.Low()
  95.         }
  96.     }
  97.     pi.strobe()
  98. }
  99.  
  100. func (pi rpi) read(cmd int) (data int) {
  101.     write(cmd)
  102.     for i, p := range pi.Pins {
  103.         if i < 8 {
  104.             p.Input()
  105.         } else {
  106.             p.Output()
  107.         }
  108.     }
  109.     pi.strobe()
  110.     for i, p := range pi.Pins {
  111.         if i < 8 {
  112.             data = (data << 1) + int(p.Read())
  113.         }
  114.     }
  115.     return data
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement