Пример #1
0
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)
}
Пример #2
0
func main() {
	rowPins := []int{4, 17, 27, 22}
	colPins := []int{23, 24, 25}

	if err := embd.InitGPIO(); err != nil {
		panic(err)
	}
	defer embd.CloseGPIO()

	keypad, err := matrix4x3.New(rowPins, colPins)
	if err != nil {
		panic(err)
	}

	for {
		key, err := keypad.PressedKey()
		if err != nil {
			panic(err)
		}
		if key != matrix4x3.KNone {
			fmt.Printf("Key Pressed = %v\n", key)
		}

		time.Sleep(500 * time.Millisecond)
	}
}
Пример #3
0
func main() {
	flag.Parse()

	if err := embd.InitGPIO(); err != nil {
		panic(err)
	}
	defer embd.CloseGPIO()

	pin, err := embd.NewAnalogPin(0)
	if err != nil {
		panic(err)
	}
	defer pin.Close()

	quit := make(chan os.Signal, 1)
	signal.Notify(quit, os.Interrupt, os.Kill)
	defer signal.Stop(quit)

	for {
		select {
		case <-time.After(100 * time.Millisecond):
			val, err := pin.Read()
			if err != nil {
				panic(err)
			}
			fmt.Printf("reading: %v\n", val)
		case <-quit:
			return
		}
	}
}
Пример #4
0
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)
	}
}
Пример #5
0
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)
	}
}
Пример #6
0
func main() {
	flag.Parse()

	embd.InitGPIO()
	defer embd.CloseGPIO()

	val, _ := embd.AnalogRead(0)
	fmt.Printf("Reading: %v\n", val)
}
Пример #7
0
func main() {
	flag.Parse()

	embd.InitGPIO()
	defer embd.CloseGPIO()

	embd.SetDirection(10, embd.Out)
	embd.DigitalWrite(10, embd.High)
}
Пример #8
0
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)
	}
}
Пример #9
0
func main() {
	flag.Parse()

	if err := embd.InitGPIO(); err != nil {
		panic(err)
	}
	defer embd.CloseGPIO()

	pwm, err := embd.NewPWMPin("P9_14")
	if err != nil {
		panic(err)
	}
	defer pwm.Close()

	if err := pwm.SetDuty(bbb.PWMDefaultPeriod / 2); err != nil {
		panic(err)
	}

	time.Sleep(1 * time.Second)
}
Пример #10
0
func main() {
	flag.Parse()

	embd.InitGPIO()
	defer embd.CloseGPIO()

	pwm, err := embd.NewPWMPin("P9_14")
	if err != nil {
		panic(err)
	}
	defer pwm.Close()

	servo := servo.New(pwm)

	quit := make(chan os.Signal, 1)
	signal.Notify(quit, os.Interrupt, os.Kill)

	turnTimer := time.Tick(500 * time.Millisecond)
	left := true

	servo.SetAngle(90)
	defer func() {
		servo.SetAngle(90)
	}()

	for {
		select {
		case <-turnTimer:
			left = !left
			switch left {
			case true:
				servo.SetAngle(70)
			case false:
				servo.SetAngle(110)
			}
		case <-quit:
			return
		}
	}
}
Пример #11
0
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
		}
	}
}
Пример #12
0
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
		}
	}
}