Esempio n. 1
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
			}

		}
	}
}
Esempio n. 2
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
		}
	}
}
Esempio n. 3
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
		}
	}
}