Пример #1
0
Файл: car.go Проект: arges/cargo
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)
}
Пример #2
0
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)
}
Пример #3
0
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)
	}
}
Пример #4
0
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
}
Пример #5
0
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++
	}
}
Пример #6
0
func SetupIo() Hardware {
	//return Hardware{}
	pins := Hardware{
		InMotion:     GetPin("GPIO2"), // pi rev2 calls it GPIO2
		InSwitch3:    GetPin("GPIO3"), // pi rev2 calls it GPIO3
		InSwitch1:    GetPin("GPIO4"),
		InSwitch2:    GetPin("GPIO17"),
		OutLed:       GetPin("GPIO27"), // pi rev2 calls it GPIO27
		OutSpeaker:   GetPin("GPIO22"),
		InDoorClosed: GetPin("GPIO10"),
		OutStrike:    GetPin("GPIO9"),
	}

	if err := hwio.PinMode(pins.InMotion, hwio.INPUT_PULLUP); err != nil {
		panic(err)
	}
	if err := hwio.PinMode(pins.InSwitch1, hwio.INPUT_PULLUP); err != nil {
		panic(err)
	}
	if err := hwio.PinMode(pins.InSwitch2, hwio.INPUT_PULLUP); err != nil {
		panic(err)
	}
	if err := hwio.PinMode(pins.InSwitch3, hwio.INPUT_PULLUP); err != nil {
		panic(err)
	}
	if err := hwio.PinMode(pins.InDoorClosed, hwio.INPUT_PULLDOWN); err != nil {
		panic(err)
	}
	if err := hwio.PinMode(pins.OutLed, hwio.OUTPUT); err != nil {
		panic(err)
	}
	if err := hwio.PinMode(pins.OutSpeaker, hwio.OUTPUT); err != nil {
		panic(err)
	}
	if err := hwio.PinMode(pins.OutStrike, hwio.OUTPUT); err != nil {
		panic(err)
	}
	pins.SetLed("off")
	pins.SetStrike("locked")
	return pins
}
Пример #7
0
// 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
		}
	}
}
Пример #8
0
// 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
			}

		}
	}
}
Пример #9
0
// 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
		}
	}
}