func main() { flag.Parse() if err := embd.InitGPIO(); err != nil { panic(err) } defer embd.CloseGPIO() btn, err := embd.NewDigitalPin(10) if err != nil { panic(err) } defer btn.Close() if err := btn.SetDirection(embd.In); err != nil { panic(err) } btn.ActiveLow(false) quit := make(chan interface{}) err = btn.Watch(embd.EdgeFalling, func(btn embd.DigitalPin) { quit <- btn }) if err != nil { panic(err) } fmt.Printf("Button %v was pressed.\n", <-quit) }
func main() { flag.Parse() if err := embd.InitGPIO(); err != nil { panic(err) } defer embd.CloseGPIO() led, err := embd.NewDigitalPin(10) if err != nil { panic(err) } defer led.Close() if err := led.SetDirection(embd.Out); err != nil { panic(err) } if err := led.Write(embd.High); err != nil { panic(err) } time.Sleep(1 * time.Second) if err := led.SetDirection(embd.In); err != nil { panic(err) } }
func main() { flag.Parse() if err := embd.InitGPIO(); err != nil { panic(err) } defer embd.CloseGPIO() pin, err := embd.NewDigitalPin(7) if err != nil { panic(err) } defer pin.Close() fluidSensor := watersensor.New(pin) for { wet, err := fluidSensor.IsWet() if err != nil { panic(err) } if wet { glog.Info("bot is dry") } else { glog.Info("bot is Wet") } time.Sleep(500 * time.Millisecond) } }
func main() { flag.Parse() if err := embd.InitGPIO(); err != nil { panic(err) } defer embd.CloseGPIO() echoPin, err := embd.NewDigitalPin("P9_21") if err != nil { panic(err) } triggerPin, err := embd.NewDigitalPin("P9_22") if err != nil { panic(err) } rf := us020.New(echoPin, triggerPin, nil) defer rf.Close() quit := make(chan os.Signal, 1) signal.Notify(quit, os.Interrupt, os.Kill) for { select { default: distance, err := rf.Distance() if err != nil { panic(err) } fmt.Printf("Distance is %v\n", distance) time.Sleep(500 * time.Millisecond) case <-quit: return } } }
// New creates a new interface for matrix4x3. func New(rowPins, colPins []int) (*Matrix4x3, error) { m := &Matrix4x3{ rowPins: make([]embd.DigitalPin, rows), colPins: make([]embd.DigitalPin, cols), poll: pollDelay, } var err error for i := 0; i < rows; i++ { m.rowPins[i], err = embd.NewDigitalPin(rowPins[i]) if err != nil { return nil, err } } for i := 0; i < cols; i++ { m.colPins[i], err = embd.NewDigitalPin(colPins[i]) if err != nil { return nil, err } } return m, nil }
// NewGPIO creates a new HD44780 connected by a 4-bit GPIO bus. func NewGPIO( rs, en, d4, d5, d6, d7, backlight interface{}, blPolarity BacklightPolarity, rowAddr RowAddress, modes ...ModeSetter, ) (*HD44780, error) { pinKeys := []interface{}{rs, en, d4, d5, d6, d7, backlight} pins := [7]embd.DigitalPin{} for idx, key := range pinKeys { if key == nil { continue } var digitalPin embd.DigitalPin if pin, ok := key.(embd.DigitalPin); ok { digitalPin = pin } else { var err error digitalPin, err = embd.NewDigitalPin(key) if err != nil { glog.V(1).Infof("hd44780: error creating digital pin %+v: %s", key, err) return nil, err } } pins[idx] = digitalPin } for _, pin := range pins { if pin == nil { continue } err := pin.SetDirection(embd.Out) if err != nil { glog.Errorf("hd44780: error setting pin %+v to out direction: %s", pin, err) return nil, err } } return New( NewGPIOConnection( pins[0], pins[1], pins[2], pins[3], pins[4], pins[5], pins[6], blPolarity), rowAddr, modes..., ) }
func main() { flag.Parse() h, _, err := embd.DetectHost() if err != nil { return } var pinNo interface{} switch h { case embd.HostBBB: pinNo = "P9_31" case embd.HostRPi: pinNo = 10 default: panic("host not supported (yet :P)") } if err := embd.InitGPIO(); err != nil { panic(err) } defer embd.CloseGPIO() led, err := embd.NewDigitalPin(pinNo) if err != nil { panic(err) } defer led.Close() if err := led.SetDirection(embd.Out); err != nil { panic(err) } if err := led.Write(embd.High); err != nil { panic(err) } time.Sleep(1 * time.Second) if err := led.SetDirection(embd.In); err != nil { panic(err) } }
func main() { stepDelay := flag.Int("step-delay", 10, "milliseconds between steps") flag.Parse() if err := embd.InitGPIO(); err != nil { panic(err) } defer embd.CloseGPIO() // Physical pins 11,15,16,18 on rasp pi // GPIO17,GPIO22,GPIO23,GPIO24 stepPinNums := []int{17, 22, 23, 24} stepPins := make([]embd.DigitalPin, 4) for i, pinNum := range stepPinNums { pin, err := embd.NewDigitalPin(pinNum) if err != nil { panic(err) } defer pin.Close() if err := pin.SetDirection(embd.Out); err != nil { panic(err) } if err := pin.Write(embd.Low); err != nil { panic(err) } defer pin.SetDirection(embd.In) stepPins[i] = pin } // Define sequence described in manufacturer's datasheet seq := [][]int{ []int{1, 0, 0, 0}, []int{1, 1, 0, 0}, []int{0, 1, 0, 0}, []int{0, 1, 1, 0}, []int{0, 0, 1, 0}, []int{0, 0, 1, 1}, []int{0, 0, 0, 1}, []int{1, 0, 0, 1}, } stepCount := len(seq) - 1 stepDir := 2 // Set to 1 or 2 for clockwise, -1 or -2 for counter-clockwise quit := make(chan os.Signal, 1) signal.Notify(quit, os.Interrupt, os.Kill) defer signal.Stop(quit) // Start main loop ticker := time.NewTicker(time.Duration(*stepDelay) * time.Millisecond) defer ticker.Stop() var stepCounter int for { select { case <-ticker.C: // set pins to appropriate values for given position in the sequence for i, pin := range stepPins { if seq[stepCounter][i] != 0 { fmt.Printf("Enable pin %d, step %d\n", i, stepCounter) if err := pin.Write(embd.High); err != nil { panic(err) } } else { if err := pin.Write(embd.Low); err != nil { panic(err) } } } stepCounter += stepDir // If we reach the end of the sequence start again if stepCounter >= stepCount { stepCounter = 0 } else if stepCounter < 0 { stepCounter = stepCount } case <-quit: return } } }