Пример #1
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)
	}
}
Пример #2
0
func (d *BMP085) calibrate() error {
	d.cmu.RLock()
	if d.calibrated {
		d.cmu.RUnlock()
		return nil
	}
	d.cmu.RUnlock()

	d.cmu.Lock()
	defer d.cmu.Unlock()

	readInt16 := func(reg byte) (int16, error) {
		v, err := d.Bus.ReadWordFromReg(address, reg)
		if err != nil {
			return 0, err
		}
		return int16(v), nil
	}

	readUInt16 := func(reg byte) (uint16, error) {
		v, err := d.Bus.ReadWordFromReg(address, reg)
		if err != nil {
			return 0, err
		}
		return uint16(v), nil
	}

	var err error
	d.ac1, err = readInt16(calAc1)
	if err != nil {
		return err
	}
	d.ac2, err = readInt16(calAc2)
	if err != nil {
		return err
	}
	d.ac3, err = readInt16(calAc3)
	if err != nil {
		return err
	}
	d.ac4, err = readUInt16(calAc4)
	if err != nil {
		return err
	}
	d.ac5, err = readUInt16(calAc5)
	if err != nil {
		return err
	}
	d.ac6, err = readUInt16(calAc6)
	if err != nil {
		return err
	}
	d.b1, err = readInt16(calB1)
	if err != nil {
		return err
	}
	d.b2, err = readInt16(calB2)
	if err != nil {
		return err
	}
	d.mb, err = readInt16(calMB)
	if err != nil {
		return err
	}
	d.mc, err = readInt16(calMC)
	if err != nil {
		return err
	}
	d.md, err = readInt16(calMD)
	if err != nil {
		return err
	}

	d.calibrated = true

	if glog.V(1) {
		glog.Info("bmp085: calibration data retrieved")
		glog.Infof("bmp085: param AC1 = %v", d.ac1)
		glog.Infof("bmp085: param AC2 = %v", d.ac2)
		glog.Infof("bmp085: param AC3 = %v", d.ac3)
		glog.Infof("bmp085: param AC4 = %v", d.ac4)
		glog.Infof("bmp085: param AC5 = %v", d.ac5)
		glog.Infof("bmp085: param AC6 = %v", d.ac6)
		glog.Infof("bmp085: param B1 = %v", d.b1)
		glog.Infof("bmp085: param B2 = %v", d.b2)
		glog.Infof("bmp085: param MB = %v", d.mb)
		glog.Infof("bmp085: param MC = %v", d.mc)
		glog.Infof("bmp085: param MD = %v", d.md)
	}

	return nil
}