Advertisement
Guest User

Untitled

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