func (c *Car) Setup() { c.pins[in1], _ = hwio.GetPin("gpio24") c.pins[in2], _ = hwio.GetPin("gpio17") c.pins[in3], _ = hwio.GetPin("gpio23") c.pins[in4], _ = hwio.GetPin("gpio22") _ = hwio.PinMode(c.pins[in1], hwio.OUTPUT) _ = hwio.PinMode(c.pins[in2], hwio.OUTPUT) _ = hwio.PinMode(c.pins[in3], hwio.OUTPUT) _ = hwio.PinMode(c.pins[in4], hwio.OUTPUT) }
func main() { // Get the pins we're going to use. These are on a beaglebone. sinPin, _ := hwio.GetPin("P9.11") sclkPin, _ := hwio.GetPin("P9.12") xlatPin, _ := hwio.GetPin("P9.13") gsclkPin, _ := hwio.GetPin("P9.14") blankPin, _ := hwio.GetPin("P9.15") fmt.Printf("Pins are: sin=%d, sclk=%d, xlat=%d, gsclk=%d, blank=%d", sinPin, sclkPin, xlatPin, gsclkPin, blankPin) // Make them all outputs e := hwio.PinMode(sinPin, hwio.OUTPUT) if e == nil { hwio.PinMode(sclkPin, hwio.OUTPUT) } if e == nil { hwio.PinMode(xlatPin, hwio.OUTPUT) } if e == nil { hwio.PinMode(gsclkPin, hwio.OUTPUT) } if e == nil { hwio.PinMode(blankPin, hwio.OUTPUT) } if e != nil { fmt.Printf("Could not initialise pins: %s", e) return } // set clocks low hwio.DigitalWrite(sclkPin, hwio.LOW) hwio.DigitalWrite(xlatPin, hwio.LOW) hwio.DigitalWrite(gsclkPin, hwio.LOW) // run GS clock in it's own space hwio.DigitalWrite(blankPin, hwio.HIGH) hwio.DigitalWrite(blankPin, hwio.LOW) clockData(gsclkPin) for b := 0; b < 4096; b++ { writeData(uint(b), sinPin, sclkPin, xlatPin) for j := 0; j < 10; j++ { hwio.DigitalWrite(blankPin, hwio.HIGH) hwio.DigitalWrite(blankPin, hwio.LOW) clockData(gsclkPin) } // hwio.Delay(100) } // hwio.ShiftOut(dataPin, clockPin, uint(data), hwio.MSBFIRST) }
// Create a new servo and initialise it. func New(pwm hwio.PWMModule, pin interface{}) (*Servo, error) { var p hwio.Pin var e error switch pt := pin.(type) { case hwio.Pin: p = pt case string: p, e = hwio.GetPin(pt) if e != nil { return nil, e } } result := &Servo{PWM: pwm, Pin: p} // enable the servo e = pwm.EnablePin(p, true) if e != nil { return nil, e } e = result.SetPeriod(DEFAULT_SERVO_PERIOD) if e != nil { return nil, e } result.SetRange(DEFAULT_DUTY_MIN, DEFAULT_DUTY_MAX) return result, nil }
// hwio.GetPin with a panic instead of an error return func GetPin(id string) hwio.Pin { p, e := hwio.GetPin(id) if e != nil { panic(e) } return p }
func main() { // get a pin by name. You could also just use the logical pin number, but this is // more readable. On BeagleBone, USR0 is an on-board LED. ledPin, err := hwio.GetPin("USR1") // Generally we wouldn't expect an error, but perhaps someone is running this a if err != nil { fmt.Println(err) os.Exit(1) } // Set the mode of the pin to output. This will return an error if, for example, // we were trying to set an analog input to an output. err = hwio.PinMode(ledPin, hwio.OUTPUT) if err != nil { fmt.Println(err) os.Exit(1) } // Run the blink forever for { hwio.DigitalWrite(ledPin, hwio.HIGH) hwio.Delay(1000) hwio.DigitalWrite(ledPin, hwio.LOW) hwio.Delay(1000) } }
func NewButton(pin string) (*Button, error) { buttonPin, err := hwio.GetPin(pin) if err != nil { return nil, err } err = hwio.PinMode(buttonPin, hwio.INPUT) if err != nil { return nil, err } return &Button{pin: buttonPin}, nil }
func main() { // Get the pins we're going to use. These are on a beaglebone. dataPin, _ := hwio.GetPin("P8.3") // connected to pin 14 clockPin, _ := hwio.GetPin("P8.4") // connected to pin 11 storePin, _ := hwio.GetPin("P8.5") // connected to pin 12 // Make them all outputs hwio.PinMode(dataPin, hwio.OUTPUT) hwio.PinMode(clockPin, hwio.OUTPUT) hwio.PinMode(storePin, hwio.OUTPUT) data := 0 // Set the initial state of the clock and store pins to low. These both // trigger on the rising edge. hwio.DigitalWrite(clockPin, hwio.LOW) hwio.DigitalWrite(storePin, hwio.LOW) for { // Shift out 8 bits hwio.ShiftOut(dataPin, clockPin, uint(data), hwio.MSBFIRST) // You can use this line instead to clock out 16 bits if you have // two 74HC595's chained together // hwio.ShiftOutSize(dataPin, clockPin, uint(data), hwio.MSBFIRST, 16) // Pulse the store pin once hwio.DigitalWrite(storePin, hwio.HIGH) hwio.DigitalWrite(storePin, hwio.LOW) // Poor humans cannot read binary as fast as the machine can display it hwio.Delay(100) data++ } }
// Run is the block's main loop. Here we listen on the different channels we set up. func (b *DigitalPin) Run() { var pin hwio.Pin var pinStr string var err error for { select { case ruleI := <-b.inrule: if pinStr != "" { b.Log("closing pin " + pinStr) err = hwio.ClosePin(pin) if err != nil { b.Error(err) } } pinStr, err = util.ParseString(ruleI, "Pin") if err != nil { b.Error(err) continue } pin, err = hwio.GetPin(pinStr) if err != nil { pinStr = "" pin = 0 b.Error(err) continue } err = hwio.PinMode(pin, hwio.INPUT) if err != nil { b.Error(err) continue } case <-b.quit: // quit the block err = hwio.ClosePin(pin) b.Error(err) return case c := <-b.queryrule: // deal with a query request c <- map[string]interface{}{ "Pin": pinStr, } case <-b.inpoll: if pin == 0 { continue } v, err := hwio.DigitalRead(pin) if err != nil { b.Log(v) b.Error(err) continue } outValue := float64(v) out := map[string]interface{}{ "value": outValue, "pin": pinStr, } b.out <- out } } }
// Run is the block's main loop. Here we listen on the different channels we set up. func (b *ToDigitalPin) Run() { var pin hwio.Pin var pinStr string var tree *jee.TokenTree var path string var err error for { select { case ruleI := <-b.inrule: path, err = util.ParseString(ruleI, "Path") if err != nil { b.Error(err) continue } token, err := jee.Lexer(path) if err != nil { b.Error(err) continue } tree, err = jee.Parser(token) if err != nil { b.Error(err) continue } if pinStr != "" { b.Log("closing pin " + pinStr) err = hwio.ClosePin(pin) if err != nil { b.Error(err) } } pinStr, err = util.ParseString(ruleI, "Pin") if err != nil { b.Error(err) continue } pin, err = hwio.GetPin(pinStr) if err != nil { pinStr = "" pin = 0 b.Error(err) continue } err = hwio.PinMode(pin, hwio.OUTPUT) if err != nil { b.Error(err) continue } case <-b.quit: // quit the block err = hwio.ClosePin(pin) b.Error(err) return case c := <-b.queryrule: // deal with a query request c <- map[string]interface{}{ "Pin": pinStr, "Path": path, } case msg := <-b.in: if tree == nil { continue } valI, err := jee.Eval(tree, msg) if err != nil { b.Error(err) continue } val, ok := valI.(float64) if !ok { log.Println(msg) b.Error(errors.New("couldn't assert value to a float")) continue } if int(val) == 0 { hwio.DigitalWrite(pin, hwio.LOW) } else if int(val) == 1 { hwio.DigitalWrite(pin, hwio.HIGH) } else { b.Error(errors.New("value must be 0 for LOW and 1 for HIGH")) continue } } } }
// Run is the block's main loop. Here we listen on the different channels we set up. func (b *AnalogPin) Run() { var pin hwio.Pin var pinStr string var err error // Get the module m, e := hwio.GetAnalogModule() if e != nil { b.Log(e) } // Enable it. e = m.Enable() if e != nil { b.Log(e) } for { select { case ruleI := <-b.inrule: if pinStr != "" { err = hwio.ClosePin(pin) b.Error(err) } pinStr, err = util.ParseString(ruleI, "Pin") if err != nil { b.Error(err) continue } pin, err = hwio.GetPin(pinStr) if err != nil { b.Error(err) continue } err = hwio.PinMode(pin, hwio.INPUT) if err != nil { b.Error(err) continue } case <-b.quit: // quit the block err = hwio.ClosePin(pin) b.Error(err) return case c := <-b.queryrule: // deal with a query request c <- map[string]interface{}{ "Pin": pinStr, } case <-b.inpoll: if pin == 0 { continue } v, err := hwio.AnalogRead(pin) if err != nil { b.Error(err) continue } out := map[string]interface{}{ "value": float64(v), "pin": pinStr, } b.out <- out } } }