示例#1
0
// Start starts the data acquisition loop.
func (d *TMP006) Start() error {
	if err := d.setup(); err != nil {
		return err
	}

	d.rawDieTemps = make(chan float64)
	d.objTemps = make(chan float64)

	go func() {
		var rawDieTemp, objTemp float64
		var rdtAvlb, otAvlb bool
		var err error
		var timer <-chan time.Time
		resetTimer := func() {
			timer = time.After(time.Duration(d.SampleRate.timeRequired*1000) * time.Millisecond)
		}
		resetTimer()

		for {
			var rawDieTemps, objTemps chan float64

			if rdtAvlb {
				rawDieTemps = d.rawDieTemps
			}
			if otAvlb {
				objTemps = d.objTemps
			}

			select {
			case <-timer:
				var rdt float64
				if rdt, err = d.measureRawDieTemp(); err != nil {
					glog.Errorf("tmp006: %v", err)
				} else {
					rawDieTemp = rdt
					rdtAvlb = true
				}
				var ot float64
				if ot, err = d.measureObjTemp(); err != nil {
					glog.Errorf("tmp006: %v", err)
				} else {
					objTemp = ot
					otAvlb = true
				}
				resetTimer()
			case rawDieTemps <- rawDieTemp:
				rdtAvlb = false
			case objTemps <- objTemp:
				otAvlb = false
			case waitc := <-d.closing:
				waitc <- struct{}{}
				close(d.rawDieTemps)
				close(d.objTemps)
				return
			}
		}
	}()

	return nil
}
示例#2
0
// 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...,
	)
}
示例#3
0
// Close closes all open DigitalPins.
func (conn *GPIOConnection) Close() error {
	glog.V(2).Info("hd44780: closing all GPIO pins")
	pins := []embd.DigitalPin{
		conn.RS,
		conn.EN,
		conn.D4,
		conn.D5,
		conn.D6,
		conn.D7,
		conn.Backlight,
	}

	for _, pin := range pins {
		err := pin.Close()
		if err != nil {
			glog.Errorf("hd44780: error closing pin %+v: %s", pin, err)
			return err
		}
	}
	return nil
}
示例#4
0
// Start starts the data acquisition loop.
func (d *L3GD20) Start() error {
	if err := d.setup(); err != nil {
		return err
	}

	d.closing = make(chan chan struct{})

	go func() {
		var x, y, z float64
		var orientations chan Orientation

		timer := time.Tick(time.Duration(math.Floor(pollDelay)) * time.Microsecond)

		for {
			select {
			case <-timer:
				dx, dy, dz, err := d.measureOrientationDelta()
				if err != nil {
					glog.Errorf("l3gd20: %v", err)
				} else {
					x += dx * mult
					y += dy * mult
					z += dz * mult
					orientations = d.orientations
				}
			case orientations <- Orientation{x, y, z}:
				orientations = nil
			case waitc := <-d.closing:
				waitc <- struct{}{}
				close(d.orientations)
				return
			}

		}
	}()

	return nil
}