예제 #1
0
func OpenDigits() (*Digits, error) {
	mosiPin, err := embd.NewDigitalPin(10)
	if err != nil {
		return nil, err
	}
	clkPin, err := embd.NewDigitalPin(11)
	if err != nil {
		return nil, err
	}
	csPin, err := embd.NewDigitalPin(8)
	if err != nil {
		return nil, err
	}

	if err := mosiPin.SetDirection(embd.Out); err != nil {
		panic(err)
	}
	if err := clkPin.SetDirection(embd.Out); err != nil {
		panic(err)
	}
	if err := csPin.SetDirection(embd.Out); err != nil {
		panic(err)
	}

	return &Digits{
		mosiPin: mosiPin,
		clkPin:  clkPin,
		csPin:   csPin,
	}, nil
}
예제 #2
0
func main() {
	if err := embd.InitGPIO(); err != nil {
		panic(err)
	}
	defer embd.CloseGPIO()

	echoPin, err := embd.NewDigitalPin(10)
	if err != nil {
		panic(err)
	}
	triggerPin, err := embd.NewDigitalPin(9)
	if err != nil {
		panic(err)
	}

	rf := us020.New(echoPin, triggerPin, nil)
	defer rf.Close()

	for {
		distance, err := rf.Distance()
		if err != nil {
			panic(err)
		}
		fmt.Printf("Distance is %v\n", distance)

		time.Sleep(500 * time.Millisecond)
	}
}
예제 #3
0
func OpenLights() (*Lights, error) {
	redPin, err := embd.NewDigitalPin(2)
	if err != nil {
		return nil, err
	}
	yellowPin, err := embd.NewDigitalPin(3)
	if err != nil {
		return nil, err
	}
	greenPin, err := embd.NewDigitalPin(4)
	if err != nil {
		return nil, err
	}

	if err := redPin.SetDirection(embd.Out); err != nil {
		panic(err)
	}
	if err := yellowPin.SetDirection(embd.Out); err != nil {
		panic(err)
	}
	if err := greenPin.SetDirection(embd.Out); err != nil {
		panic(err)
	}

	l := &Lights{
		redPin:    redPin,
		yellowPin: yellowPin,
		greenPin:  greenPin,
	}

	return l, nil
}
예제 #4
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)
	}
}
예제 #5
0
파일: gpio.go 프로젝트: ninadk1092/embd
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)
	}
}
예제 #6
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)
}
예제 #7
0
func newButtons() Buttons {
	btns := &buttons{
		clicks:   map[string]int{"0": 0, "1": 0, "2": 0},
		gpioBtns: []*gpioButton{},
	}

	for i := 0; i < 3; i++ {
		btnNum := i
		pinNumber := 22 + i

		log.Println("Set up", btnNum+1, "at pin", pinNumber)

		pin, err := embd.NewDigitalPin(pinNumber)
		if err != nil {
			fmt.Printf("Error in NewDigitalPin (%d)!", pinNumber)
			panic(err)
		}

		if err := pin.SetDirection(embd.In); err != nil {
			fmt.Printf("Error in SetDirection (%d)!", pinNumber)
			panic(err)
		}

		gpioBtn := &gpioButton{number: fmt.Sprintf("%d", btnNum), pin: pin}
		btns.gpioBtns = append(btns.gpioBtns, gpioBtn)
	}

	return btns
}
예제 #8
0
파일: led.go 프로젝트: cassava/pillr
func New(pin int) *LED {
	p, err := embd.NewDigitalPin(pin)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error instantiating pin %v: %s\n", pin, err)
		os.Exit(1)
	}
	p.SetDirection(embd.Out)
	return &LED{pin: p}
}
예제 #9
0
func inputPin(n int) embd.DigitalPin {
	pin, err := embd.NewDigitalPin(n)
	if err != nil {
		panic(err)
	}
	pin.SetDirection(embd.In)

	return pin
}
예제 #10
0
func outputPin(n int, value int) embd.DigitalPin {
	pin, err := embd.NewDigitalPin(n)
	if err != nil {
		panic(err)
	}
	pin.SetDirection(embd.Out)
	pin.Write(value)

	return pin
}
예제 #11
0
func main() {
	flag.Parse()

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

	data, err := embd.NewDigitalPin(4)
	if err != nil {
		panic(err)
	}
	defer data.Close()

	clock, err := embd.NewDigitalPin(3)
	if err != nil {
		panic(err)
	}
	defer clock.Close()

	sensor := sht1x.New(data, clock)

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

	ticker := time.NewTicker(2 * time.Second)
	defer ticker.Stop()

	for {
		select {
		case <-ticker.C:
			m, err := sensor.Measure()
			if err != nil {
				fmt.Printf("err: %v\n", err)
			}
			fmt.Printf("Temperature: %.1fC, Relative Humidity: %.1f%%, Dew Point: %.1fC\n", m.Temperature, m.RelativeHumidity, m.DewPoint)

		case <-quit:
			return
		}
	}
}
예제 #12
0
func InitGPIO(file string) (embd.DigitalPin, error) {
	embd.InitGPIO()
	pin, pin_err := embd.NewDigitalPin(file)
	if pin_err != nil {
		log.Fatal("Error opening pin! \n", pin_err)
		return nil, pin_err
	}

	pin.SetDirection(embd.In)

	return pin, nil
}
예제 #13
0
파일: us020.go 프로젝트: ninadk1092/embd
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
		}
	}
}
예제 #14
0
// 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
}
예제 #15
0
파일: hd44780.go 프로젝트: npotts/embd
// 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() {

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

	led, err := embd.NewDigitalPin(17)
	if err != nil {
		panic(err)
	}
	defer led.Close()
	if err := led.SetDirection(embd.In); err != nil {
		panic(err)
	}

	state := false
	count := 0

	for {
		pin, err := led.Read()
		if err != nil {
			panic(err)
		}

		if pin == 1 {
			if state == false {
				count++
			}
		} else {
			count = 0
			state = false
		}

		if count >= 3 {
			state = true
			count = 0
			client.SendCounterTick("GAS_1")
		}

		time.Sleep(10 * time.Millisecond)

	}
}
예제 #17
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)
	}
}
예제 #18
0
func main() {
	glog.Info("main: starting up")

	flag.Parse()

	var car Car = NullCar
	if !*fakeCar {
		if err := embd.InitI2C(); err != nil {
			panic(err)
		}
		defer embd.CloseI2C()

		bus := embd.NewI2CBus(byte(*i2cBusNo))

		var cam Camera = NullCamera
		if !*fakeCam {
			cam = NewCamera(*camWidth, *camHeight, *camTurnImage, *camFps)
		}
		defer cam.Close()
		cam.Run()

		var comp Compass = NullCompass
		if !*fakeCompass {
			comp = NewCompass(bus)
		}
		defer comp.Close()

		var rf RangeFinder = NullRangeFinder
		if !*fakeRangeFinder {
			thermometer := bmp180.New(bus)
			defer thermometer.Close()

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

			echoPin, err := embd.NewDigitalPin(*echoPinNumber)
			if err != nil {
				panic(err)
			}
			triggerPin, err := embd.NewDigitalPin(*triggerPinNumber)
			if err != nil {
				panic(err)
			}

			rf = NewRangeFinder(echoPin, triggerPin, thermometer)
		}
		defer rf.Close()

		var fw FrontWheel = NullFrontWheel
		if !*fakeFrontWheel {
			sb := servoblaster.New()
			defer sb.Close()

			pwm := sb.Channel(*sbChannel)

			servo := servo.New(pwm)
			fw = &frontWheel{servo}
		}
		defer fw.Turn(0)

		var engine Engine = NullEngine
		if !*fakeEngine {
			ctrl := pca9685.New(bus, 0x41)
			defer ctrl.Close()

			pwm := ctrl.AnalogChannel(15)

			engine = NewEngine(pwm)
		}
		defer engine.Stop()

		var gyro Gyroscope = NullGyroscope
		if !*fakeGyro {
			gyro = NewGyroscope(bus, l3gd20.R250DPS)
		}
		defer gyro.Close()

		car = NewCar(bus, cam, comp, rf, gyro, fw, engine)
	}
	defer car.Close()

	ws := NewWebServer(car)
	ws.Run()

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

	glog.Info("main: all done")
}
예제 #19
0
파일: main.go 프로젝트: jonaz/magicmirror
func main() {

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

	btn17, err := embd.NewDigitalPin(17)
	if err != nil {
		panic(err)
	}
	btn18, err := embd.NewDigitalPin(18)
	if err != nil {
		panic(err)
	}

	defer btn17.Close()
	defer btn18.Close()

	if err := btn17.SetDirection(embd.In); err != nil {
		panic(err)
	}
	if err := btn18.SetDirection(embd.In); err != nil {
		panic(err)
	}
	btn17.ActiveLow(false)
	btn18.ActiveLow(false)

	motion := make(chan embd.DigitalPin)
	err = btn17.Watch(embd.EdgeBoth, func(btn embd.DigitalPin) {
		motion <- btn
	})
	if err != nil {
		panic(err)
	}
	err = btn18.Watch(embd.EdgeBoth, func(btn embd.DigitalPin) {
		motion <- btn
	})
	if err != nil {
		panic(err)
	}

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

	fmt.Println("listening...")
	dur, _ := time.ParseDuration("-5m")
	onTime := time.Now().Add(dur)
	for {
		select {
		case <-c:
			fmt.Println("bye")
			return
		case btn := <-motion:
			direction := getDirection(btn.N())
			v, _ := btn.Read()
			if v == 0 {
				log.Printf("Motion stopped from %s.\n", direction)
				continue
			}
			log.Printf("Motion detected from %s.\n", direction)
			if time.Since(onTime).Seconds() > 300 {
				turnScreenOn()
				onTime = time.Now()
			}

		}
	}
}
예제 #20
0
파일: main.go 프로젝트: immesys/powerup
func initHardware() {
	err := embd.InitGPIO()
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays = make([]embd.DigitalPin, 8)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[0], err = embd.NewDigitalPin(4) //7)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[1], err = embd.NewDigitalPin(17) //0)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[2], err = embd.NewDigitalPin(18) //1)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[3], err = embd.NewDigitalPin(27) //2)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[4], err = embd.NewDigitalPin(22) //3)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[5], err = embd.NewDigitalPin(23) //4)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[6], err = embd.NewDigitalPin(24) //5)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[7], err = embd.NewDigitalPin(25) //6)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	for _, r := range relays {
		err = r.SetDirection(embd.Out)
		if err != nil {
			fmt.Println("GPIO ERR:", err)
			os.Exit(1)
		}
		err = r.Write(embd.Low)
		if err != nil {
			fmt.Println("GPIO ERR:", err)
			os.Exit(1)
		}
	}
}
예제 #21
0
파일: 28bjy-48.go 프로젝트: npotts/embd
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
		}
	}
}
func (g *GPIO) PinInit(pinId string, dir Direction, pullup PullUp, name string) error {
	var pin interface{}
	state := byte(0)

	if dir == PWM {

		host, _, err := embd.DetectHost()
		if err != nil {
			return err
		}
		if host == embd.HostRPi {
			// use pi blaster pin
			log.Println("Creating PWM pin on Pi")

			// get the host descriptor
			desc, err := embd.DescribeHost()
			if err != nil {
				return err
			}
			// get the pinmap
			embdMap := desc.GPIODriver().PinMap()
			// lookup the pinId in the map
			var pinDesc *embd.PinDesc
			for i := range embdMap {
				pd := embdMap[i]

				if pd.ID == pinId {
					pinDesc = pd
					break
				}

				for j := range pd.Aliases {
					if pd.Aliases[j] == pinId {
						pinDesc = pd
						break
					}
				}
			}
			if pinDesc != nil {
				// we found a pin with that name....what is its first Alias?
				pinIdInt, err := strconv.Atoi(pinDesc.Aliases[0])
				if err != nil {
					log.Println("Failed to parse int from alias : ", pinDesc.Aliases[0])
					return err
				}
				p := NewBlasterPin(pinIdInt)
				pin = p
			} else {
				log.Println("Failed to find Pin ", pinId)
				return errors.New("Failed to find pin " + pinId)
			}
		} else {
			// bbb, so use embd since pwm pins work there
			p, err := embd.NewPWMPin(pinId)
			if err != nil {
				log.Println("Failed to create PWM Pin using key ", pinId, " : ", err.Error())
				return err
			}
			pin = p
		}
	} else {
		// add a pin
		p, err := embd.NewDigitalPin(pinId)
		if err != nil {
			return err
		}
		pin = p

		err = p.SetDirection(embd.Direction(dir))
		if err != nil {
			return err
		}

		if pullup == Pull_Up {
			err = p.PullUp()

			// pullup and down not implemented on rpi host so we need to manually set initial states
			// not ideal as a pullup really isn't the same thing but it works for most use cases

			if err != nil {
				log.Println("Failed to set pullup on " + pinId + " setting high state instead : " + err.Error())
				// we failed to set pullup, so lets set initial state high instead
				err = p.Write(1)
				state = 1
				if err != nil {
					return err
				}
			}
		} else if pullup == Pull_Down {
			err = p.PullDown()

			if err != nil {

				log.Println("Failed to set pulldown on " + pinId + " setting low state instead : " + err.Error())

				err = p.Write(0)
				state = 1
				if err != nil {
					return err
				}
			}
		}
	}

	// test to see if we already have a state for this pin
	existingPin, exists := g.pinStates[pinId]
	if exists {
		existingPin.Pin = pin
		existingPin.Name = name
		existingPin.Dir = dir
		existingPin.State = state
		existingPin.Pullup = pullup
		g.pinStates[pinId] = existingPin

		g.pinStateChanged <- existingPin
		g.pinRemoved <- pinId
		g.pinAdded <- g.pinStates[pinId]
	} else {
		g.pinStates[pinId] = PinState{pin, pinId, dir, state, pullup, name}
		g.pinAdded <- g.pinStates[pinId]
	}

	return nil
}