示例#1
0
文件: l3gd20.go 项目: zlowred/embd
func main() {
	flag.Parse()

	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	gyro := l3gd20.New(bus, l3gd20.R250DPS)
	defer gyro.Close()

	gyro.Start()

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

	orientations, err := gyro.Orientations()
	if err != nil {
		panic(err)
	}

	timer := time.Tick(250 * time.Millisecond)

	for {
		select {
		case <-timer:
			orientation := <-orientations
			fmt.Printf("x: %v, y: %v, z: %v\n", orientation.X, orientation.Y, orientation.Z)
		case <-quit:
			return
		}
	}
}
示例#2
0
文件: tmp006.go 项目: zlowred/embd
func main() {
	flag.Parse()

	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	sensor := tmp006.New(bus, 0x40)
	if status, err := sensor.Present(); err != nil || !status {
		fmt.Println("tmp006: not found")
		fmt.Println(err)
		return
	}
	defer sensor.Close()

	sensor.Start()

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

	for {
		select {
		case temp := <-sensor.ObjTemps():
			fmt.Printf("tmp006: got obj temp %.2f\n", temp)
		case temp := <-sensor.RawDieTemps():
			fmt.Printf("tmp006: got die temp %.2f\n", temp)
		case <-stop:
			return
		}
	}
}
示例#3
0
文件: mcp4725.go 项目: zlowred/embd
func main() {
	flag.Parse()

	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	dac := mcp4725.New(bus, 0x62)
	defer dac.Close()

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

	for {
		select {
		case <-stop:
			return
		default:
			voltage := rand.Intn(4096)
			if err := dac.SetVoltage(voltage); err != nil {
				fmt.Printf("mcp4725: %v\n", err)
			}
		}
	}
}
示例#4
0
文件: bmp085.go 项目: zlowred/embd
func main() {
	flag.Parse()

	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	baro := bmp085.New(bus)
	defer baro.Close()

	for {
		temp, err := baro.Temperature()
		if err != nil {
			panic(err)
		}
		fmt.Printf("Temp is %v\n", temp)
		pressure, err := baro.Pressure()
		if err != nil {
			panic(err)
		}
		fmt.Printf("Pressure is %v\n", pressure)
		altitude, err := baro.Altitude()
		if err != nil {
			panic(err)
		}
		fmt.Printf("Altitude is %v\n", altitude)

		time.Sleep(500 * time.Millisecond)
	}
}
示例#5
0
文件: pca9955b.go 项目: zlowred/embd
func main() {
	flag.Parse()

	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	pca9955b := pca9955b.New(bus, 0x0B)

	pca9955b.Reset()

	fmt.Println("running in cycle")
	for {
		for x := 0; x < 256; x++ {
			if err := pca9955b.SetOutput(byte(7), byte(x)); err != nil {
				panic(err)
			}
		}
		for x := 255; x > 0; x-- {
			if err := pca9955b.SetOutput(byte(7), byte(x)); err != nil {
				panic(err)
			}
		}
	}
}
示例#6
0
func main() {
	flag.Parse()

	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	controller, err := hd44780.NewI2C(
		bus,
		0x20,
		hd44780.PCF8574PinMap,
		hd44780.RowAddress20Col,
		hd44780.TwoLine,
		hd44780.BlinkOn,
	)
	if err != nil {
		panic(err)
	}

	display := characterdisplay.New(controller, 20, 4)
	defer display.Close()

	display.Clear()
	display.Message("Hello, world!\n@embd | characterdisplay")
	time.Sleep(10 * time.Second)
	display.BacklightOff()
}
示例#7
0
文件: ads1115.go 项目: zlowred/embd
func main() {
	flag.Parse()

	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	sensor := ads1115.New(bus, 0x48)

	if res, err := sensor.Read(); err != nil {
		panic(err)
	} else {
		fmt.Printf("Converted value: %04X\n", res)
	}
}
示例#8
0
文件: servo.go 项目: zlowred/embd
func main() {
	flag.Parse()

	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	d := pca9685.New(bus, 0x41)
	d.Freq = 50
	defer d.Close()

	pwm := d.ServoChannel(0)

	servo := servo.New(pwm)

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

	turnTimer := time.Tick(500 * time.Millisecond)
	left := true

	servo.SetAngle(90)
	defer func() {
		servo.SetAngle(90)
	}()

	for {
		select {
		case <-turnTimer:
			left = !left
			switch left {
			case true:
				servo.SetAngle(70)
			case false:
				servo.SetAngle(110)
			}
		case <-c:
			return
		}
	}
}
示例#9
0
func main() {
	flag.Parse()

	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	sensor := npa700.New(bus, 0x28)

	err := sensor.Read()
	if err != nil {
		panic(err)
	}

	fmt.Printf("Temp is %fC\n", sensor.Celsius())
	fmt.Printf("Temp is %fF\n", sensor.Fahrenheit())
	fmt.Printf("Pres is %fPa\n", sensor.Pascals(0, 1638, 14745, -6894.76, 6894.76))
}
示例#10
0
文件: hd44780.go 项目: zlowred/embd
func main() {
	flag.Parse()

	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	hd, err := hd44780.NewI2C(
		bus,
		0x20,
		hd44780.PCF8574PinMap,
		hd44780.RowAddress20Col,
		hd44780.TwoLine,
		hd44780.BlinkOn,
	)
	if err != nil {
		panic(err)
	}
	defer hd.Close()

	hd.Clear()
	message := "Hello, world!"
	bytes := []byte(message)
	for _, b := range bytes {
		hd.WriteChar(b)
	}
	hd.SetCursor(0, 1)

	message = "@embd | hd44780"
	bytes = []byte(message)
	for _, b := range bytes {
		hd.WriteChar(b)
	}
	time.Sleep(10 * time.Second)
	hd.BacklightOff()
}
示例#11
0
文件: pca9685.go 项目: zlowred/embd
func main() {
	flag.Parse()

	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	pca9685 := pca9685.New(bus, 0x41)
	pca9685.Freq = 1000
	defer pca9685.Close()

	if err := pca9685.SetPwm(15, 0, 2000); err != nil {
		panic(err)
	}

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

	timer := time.Tick(2 * time.Second)
	sleeping := false

	for {
		select {
		case <-timer:
			sleeping = !sleeping
			if sleeping {
				pca9685.Sleep()
			} else {
				pca9685.Wake()
			}
		case <-c:
			return
		}
	}
}
示例#12
0
文件: lsm303.go 项目: zlowred/embd
func main() {
	flag.Parse()

	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	mems := lsm303.New(bus)
	defer mems.Close()

	for {
		heading, err := mems.Heading()
		if err != nil {
			panic(err)
		}
		fmt.Printf("Heading is %v\n", heading)

		time.Sleep(500 * time.Millisecond)
	}
}
示例#13
0
文件: bh1750fvi.go 项目: zlowred/embd
func main() {
	flag.Parse()

	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	sensor := bh1750fvi.New(bh1750fvi.High, bus)
	defer sensor.Close()

	for {
		lighting, err := sensor.Lighting()
		if err != nil {
			panic(err)
		}
		fmt.Printf("Lighting is %v lx\n", lighting)

		time.Sleep(500 * time.Millisecond)
	}
}