예제 #1
0
func main() {
	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
		}
	}
}
예제 #2
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()
}
예제 #3
0
파일: bmp180.go 프로젝트: ninadk1092/embd
func main() {
	flag.Parse()

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

	bus := embd.NewI2CBus(1)

	baro := bmp180.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)
	}
}
예제 #4
0
func main() {
	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
		}
	}
}
예제 #5
0
파일: mcp4725.go 프로젝트: ninadk1092/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)
			}
		}
	}
}
예제 #6
0
func main() {
	sig := make(chan os.Signal, 1)
	if err := embd.InitI2C(); err != nil {
		panic(err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)
	baro := bmp085.New(bus)
	defer baro.Close()

	// Draw the outline
	pcd8544.LCDdrawrect(6-1, 6-1, pcd8544.LCDWIDTH-6, pcd8544.LCDHEIGHT-6, pcd8544.BLACK)
	// date string changes slowly
	var (
		date_str string
		temp_str string
	)

	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		s := <-sig
		fmt.Println(s)
		gpio_cleanup()
		os.Exit(0)
	}()

	keep_running := true
	for keep_running {
		// Get temperature
		temp, err := baro.Temperature()
		if err != nil {
			gpio_cleanup()
			panic(err)
		}
		_temp_str := fmt.Sprint(strconv.FormatFloat(temp, 'f', 2, 64), " C")

		time_str, _date_str := get_time()
		pcd8544.LCDdrawstring(20, 12, time_str)
		if date_str != _date_str {
			date_str = _date_str
			pcd8544.LCDdrawstring(18, 24, date_str)
		}
		if temp_str != _temp_str {
			temp_str = _temp_str
			pcd8544.LCDdrawstring(20, 36, temp_str)
		}
		pcd8544.LCDdisplay()
		// wait for 1 sec
		time.Sleep(time.Second)
	}
}
예제 #7
0
func (pd *PiioDigital) Init(address byte, numGpios int) {
	// initialize the Error!
	Error = log.New(os.Stderr,
		"ERROR: ",
		log.Ldate|log.Ltime|log.Lshortfile)
	var err error

	if numGpios < 0 || numGpios > 16 {
		Error.Fatal("Number of Gpios must be between 0 and 16.")
	}

	pd.i2c = embd.NewI2CBus(address)
	pd.address = address
	pd.numGpios = uint(numGpios)

	// set defaults
	if numGpios <= 8 {
		if err := pd.i2c.WriteByte(Mcp23017IoDirA, 0xFF); err != nil { // all inputs on port A
			Error.Fatal("Unable to write byte.")
		}
		if pd.direction, err = pd.i2c.ReadByte(Mcp23017IoDirA); err != nil {
			Error.Fatal("Unable to write byte.")
		}
		if err := pd.i2c.WriteByte(Mcp23008GppuA, 0x00); err != nil {
			Error.Fatal("Unable to write byte.")
		}
	} else if numGpios > 8 {
		if err := pd.i2c.WriteByte(Mcp23017IoDirA, 0XFF); err != nil { // all inputs on port A
			Error.Print(err)
			Error.Fatal("Unable to write byte.")
		}
		if err := pd.i2c.WriteByte(Mcp23017IoDirB, 0XFF); err != nil { // all inpots on port B
			Error.Fatal("Unable to write byte.")
		}
		var t1, t2 byte
		if t1, err = pd.i2c.ReadByte(Mcp23017IoDirA); err != nil {
			Error.Fatal("Unable to read byte.")
		}
		if t2, err = pd.i2c.ReadByte(Mcp23017IoDirB); err != nil {
			Error.Fatal("Unable to read byte.")
		}
		pd.direction = t1 | (t2 << 8)
		if err := pd.i2c.WriteByte(Mcp23017GppuA, 0x00); err != nil {
			Error.Fatal("Unable to write byte.")
		}
		if err := pd.i2c.WriteByte(Mcp23017GppuB, 0x00); err != nil {
			Error.Fatal("Unable to write byte.")
		}
	}
}
예제 #8
0
파일: servo.go 프로젝트: ninadk1092/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() {
	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)
	}
}
예제 #10
0
func main() {
	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)
	}
}
예제 #11
0
파일: hd44780.go 프로젝트: npotts/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()
}
예제 #12
0
파일: pca9685.go 프로젝트: ninadk1092/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
		}
	}
}
예제 #13
0
func main() {
	glog.Info("main: starting up")

	flag.Parse()

	var car Car = NullCar
	if !*fakeCar {
		if err := embd.InitI2C(); err != nil {
			panic(err)
		}
		defer embd.CloseI2C()

		bus := embd.NewI2CBus(byte(*i2cBusNo))

		var cam Camera = NullCamera
		if !*fakeCam {
			cam = NewCamera(*camWidth, *camHeight, *camTurnImage, *camFps)
		}
		defer cam.Close()
		cam.Run()

		var comp Compass = NullCompass
		if !*fakeCompass {
			comp = NewCompass(bus)
		}
		defer comp.Close()

		var rf RangeFinder = NullRangeFinder
		if !*fakeRangeFinder {
			thermometer := bmp180.New(bus)
			defer thermometer.Close()

			if err := embd.InitGPIO(); err != nil {
				panic(err)
			}
			defer embd.CloseGPIO()

			echoPin, err := embd.NewDigitalPin(*echoPinNumber)
			if err != nil {
				panic(err)
			}
			triggerPin, err := embd.NewDigitalPin(*triggerPinNumber)
			if err != nil {
				panic(err)
			}

			rf = NewRangeFinder(echoPin, triggerPin, thermometer)
		}
		defer rf.Close()

		var fw FrontWheel = NullFrontWheel
		if !*fakeFrontWheel {
			sb := servoblaster.New()
			defer sb.Close()

			pwm := sb.Channel(*sbChannel)

			servo := servo.New(pwm)
			fw = &frontWheel{servo}
		}
		defer fw.Turn(0)

		var engine Engine = NullEngine
		if !*fakeEngine {
			ctrl := pca9685.New(bus, 0x41)
			defer ctrl.Close()

			pwm := ctrl.AnalogChannel(15)

			engine = NewEngine(pwm)
		}
		defer engine.Stop()

		var gyro Gyroscope = NullGyroscope
		if !*fakeGyro {
			gyro = NewGyroscope(bus, l3gd20.R250DPS)
		}
		defer gyro.Close()

		car = NewCar(bus, cam, comp, rf, gyro, fw, engine)
	}
	defer car.Close()

	ws := NewWebServer(car)
	ws.Run()

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

	glog.Info("main: all done")
}
예제 #14
0
func main() {
	var zoneFlag zoneslice
	var timeout time.Duration
	var verbose bool
	var repeat uint64

	flag.DurationVar(&timeout, "timeout", 10*time.Second, "amount of time to run each zone")
	flag.Uint64Var(&repeat, "repeat", 1, "how many times to repeat the zone list")
	flag.BoolVar(&verbose, "verbose", false, "whether or not to be verbose")
	flag.Var(&zoneFlag, "zone", "comma-separated list of zones to use")
	flag.Parse()

	if len(zoneFlag) == 0 {
		fmt.Fprintf(os.Stderr, "No zone specified\n")
		flag.PrintDefaults()
		os.Exit(1)
	}

	if timeout == 0 && len(zoneFlag) > 1 {
		fmt.Fprintf(os.Stderr, "No timeout with multiple zones\n")
		os.Exit(1)
	}

	if repeat == 0 {
		fmt.Fprintf(os.Stderr, "Nothing to do!\n")
		os.Exit(1)
	}

	if repeat > 1 && len(zoneFlag) == 1 {
		fmt.Fprintf(os.Stderr, "Repeating only one zone\n")
		os.Exit(1)
	}

	if timeout > 0 && timeout < 5*time.Second {
		fmt.Fprintf(os.Stderr, "Increasing timeout to 5s\n")
		timeout = 5 * time.Second
	}

	if verbose {
		fmt.Printf("Zones: %v\n", zoneFlag)
		fmt.Printf("Repeating: %d\n", repeat)
		fmt.Printf("Timeout: %v\n", timeout)
	}

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

	bus := embd.NewI2CBus(1)
	defer bus.Close()

	pcf8574 := pcf8574.New(bus, 0x20)
	defer pcf8574.SetByte(^byte(0))

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

	timer := time.Tick(timeout)

	for i := uint64(0); i < repeat; i++ {
		for _, zone := range zoneFlag {
			input := ^(byte(1) << (zone - 1))
			pcf8574.SetByte(input)
			if verbose {
				fmt.Printf("Zone: %d Input: %08b\n", zone, input)
			}
			select {
			case <-timer:
				continue
			case <-c:
				return
			}
		}
	}
}
예제 #15
0
func initMPU6050() {
	bus = embd.NewI2CBus(1)
	attSensor = mpu6050.New(bus)
}
예제 #16
0
파일: ry835ai.go 프로젝트: KingoL/stratux
func initI2C() error {
	i2cbus = embd.NewI2CBus(1) //TODO: error checking.
	return nil
}
예제 #17
0
func initBMP180() {
	bus = embd.NewI2CBus(1)
	i2csensor = bmp180.New(bus)
}