Beispiel #1
0
func main() {
	rowPins := []int{4, 17, 27, 22}
	colPins := []int{23, 24, 25}

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

	keypad, err := matrix4x3.New(rowPins, colPins)
	if err != nil {
		panic(err)
	}

	for {
		key, err := keypad.PressedKey()
		if err != nil {
			panic(err)
		}
		if key != matrix4x3.KNone {
			fmt.Printf("Key Pressed = %v\n", key)
		}

		time.Sleep(500 * time.Millisecond)
	}
}
Beispiel #2
0
func main() {
	if err := embd.InitGPIO(); err != nil {
		panic(err)
	}
	defer embd.CloseGPIO()

	echoPin, err := embd.NewDigitalPin(10)
	if err != nil {
		panic(err)
	}
	triggerPin, err := embd.NewDigitalPin(9)
	if err != nil {
		panic(err)
	}

	rf := us020.New(echoPin, triggerPin, nil)
	defer rf.Close()

	for {
		distance, err := rf.Distance()
		if err != nil {
			panic(err)
		}
		fmt.Printf("Distance is %v\n", distance)

		time.Sleep(500 * time.Millisecond)
	}
}
Beispiel #3
0
func main() {
	flag.Parse()

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

	led, err := embd.NewDigitalPin(10)
	if err != nil {
		panic(err)
	}
	defer led.Close()

	if err := led.SetDirection(embd.Out); err != nil {
		panic(err)
	}
	if err := led.Write(embd.High); err != nil {
		panic(err)
	}

	time.Sleep(1 * time.Second)

	if err := led.SetDirection(embd.In); err != nil {
		panic(err)
	}
}
Beispiel #4
0
func main() {
	flag.Parse()

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

	btn, err := embd.NewDigitalPin(10)
	if err != nil {
		panic(err)
	}
	defer btn.Close()

	if err := btn.SetDirection(embd.In); err != nil {
		panic(err)
	}
	btn.ActiveLow(false)

	quit := make(chan interface{})
	err = btn.Watch(embd.EdgeFalling, func(btn embd.DigitalPin) {
		quit <- btn
	})
	if err != nil {
		panic(err)
	}

	fmt.Printf("Button %v was pressed.\n", <-quit)
}
Beispiel #5
0
func main() {
	embd.InitGPIO()
	defer embd.CloseGPIO()

	embd.SetDirection(10, embd.Out)
	embd.DigitalWrite(10, embd.High)
}
Beispiel #6
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)
	}
}
func (g *GPIO) Init(pinStateChanged chan PinState, pinAdded chan PinState, pinRemoved chan string, states map[string]PinState) error {
	g.pinStateChanged = pinStateChanged
	g.pinRemoved = pinRemoved
	g.pinAdded = pinAdded
	g.pinStates = states

	// if its a raspberry pi initialize pi-blaster too
	host, _, err := embd.DetectHost()
	if err != nil {
		return err
	}
	if host == embd.HostRPi {
		InitBlaster()
	}

	err = embd.InitGPIO()
	if err != nil {
		return err
	}
	// now init pins
	for key, pinState := range g.pinStates {
		if pinState.Name == "" {
			pinState.Name = pinState.PinId
		}
		g.PinInit(key, pinState.Dir, pinState.Pullup, pinState.Name)
		g.PinSet(key, pinState.State)
	}
	return nil
}
Beispiel #8
0
func main() {
	if err := embd.InitGPIO(); err != nil {
		panic(err)
	}
	defer embd.CloseGPIO()

	pin, err := embd.NewAnalogPin(0)
	if err != nil {
		panic(err)
	}
	defer pin.Close()

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

	for {
		select {
		case <-time.After(100 * time.Millisecond):
			val, err := pin.Read()
			if err != nil {
				panic(err)
			}
			fmt.Printf("reading: %v\n", val)
		case <-quit:
			return
		}
	}
}
Beispiel #9
0
func main() {
	embd.InitGPIO()
	defer embd.CloseGPIO()

	val, _ := embd.AnalogRead(0)
	fmt.Printf("Reading: %v\n", val)
}
Beispiel #10
0
func (p *PCD8544) init() {
	if err := embd.InitGPIO(); err != nil {
		panic(err)
	}
	// All pin direction are output
	embd.SetDirection(p.PIN_SCLK, embd.Out)
	embd.SetDirection(p.PIN_DIN, embd.Out)
	embd.SetDirection(p.PIN_DC, embd.Out)
	embd.SetDirection(p.PIN_CS, embd.Out)
	embd.SetDirection(p.PIN_RST, embd.Out)

	// Reset controller state
	embd.DigitalWrite(p.PIN_RST, embd.High)
	embd.DigitalWrite(p.PIN_CS, embd.High)
	embd.DigitalWrite(p.PIN_RST, embd.Low)
	// delay(100)
	time.Sleep(100 * time.Millisecond)
	embd.DigitalWrite(p.PIN_RST, embd.High)

	// LCD parameters
	p.send(LCD_CMD, 0x21) // extended instruction set control (H=1)
	p.send(LCD_CMD, 0x13) // bias system (1:48)
	p.send(LCD_CMD, 0xc2) // default Vop (3.06 + 66 * 0.06 = 7v)

	p.Clear()

	// Activate LCD
	p.send(LCD_CMD, 0x08) // display blank
	p.send(LCD_CMD, 0x0c) // normal mode (0x0d = inverse mode)
	// delay(100)
	time.Sleep(100 * time.Millisecond)

	// Place the cursor at the origin
	p.SetCursor(0, 0)
}
Beispiel #11
0
func hardware() {
	if err := embd.InitGPIO(); err != nil {
		panic(err)
	}
	defer embd.CloseGPIO()

	if err := embd.InitSPI(); err != nil {
		panic(err)
	}
	defer embd.CloseSPI()

	lights, err := OpenLights()
	if err != nil {
		panic(err)
	}
	defer lights.Close()

	digits, err := OpenDigits()
	if err != nil {
		panic(err)
	}
	defer digits.Close()

	go digits.Try()

	for !quit {
		statusMutex.Lock()
		yellow := status.Warning
		red := status.Critical
		redBlink := status.Paging
		green := !(red || yellow)
		//lastPageTime := status.LastPageTime
		statusMutex.Unlock()

		var redVal int
		var yellowVal int
		var greenVal int
		if red {
			redVal = embd.High
		}
		if yellow {
			yellowVal = embd.High
		}
		if green {
			greenVal = embd.High
		}
		lights.redPin.Write(redVal)
		lights.yellowPin.Write(yellowVal)
		lights.greenPin.Write(greenVal)
		time.Sleep(500 * time.Millisecond)
		if red && redBlink {
			lights.redPin.Write(embd.Low)
		}
		time.Sleep(500 * time.Millisecond)
	}
}
Beispiel #12
0
func init() {
	log.Info("Init Shooter")
	embd.InitGPIO()

	pinTrigger1 = outputPin(trigger1, embd.High)
	pinTrigger2 = outputPin(trigger2, embd.High)
	pinTriggerReturn = inputPin(trigger2)
	pinSlowSwitch = outputPin(slowSwitch, embd.High)
	pinFastSwitch = outputPin(fastSwitch, embd.Low)

	startShooter()
}
Beispiel #13
0
func InitGPIO(file string) (embd.DigitalPin, error) {
	embd.InitGPIO()
	pin, pin_err := embd.NewDigitalPin(file)
	if pin_err != nil {
		log.Fatal("Error opening pin! \n", pin_err)
		return nil, pin_err
	}

	pin.SetDirection(embd.In)

	return pin, nil
}
func (t *TemperatureController) Monitor_temperature_humidity(rw http.ResponseWriter, req *http.Request) {

	flag.Parse()
	embd.InitGPIO()
	// var lig models.Light

	sensorType := dht.DHT11
	temperature, humidity, retried, err := dht.ReadDHTxxWithRetry(sensorType, 4, true, 10)
	if err != nil {
		log.Fatal(err)
	}
	// Print temperature and humidity
	fmt.Printf("Temperature = %v*C, Humidity = %v%% (retried %d times)\n",
		temperature, humidity, retried)
	b, err := json.Marshal(models.WeatherMessage{
		Success:     "True",
		Message:     "Temperature and Humidity updated",
		Temperature: temperature,
		Humidity:    humidity,
	})
	if err != nil {
		log.Fatal(err)
	}
	rw.Header().Set("Content-Type", "application/json")
	rw.Write(b)
	// if temperature <= 22 {
	// 	embd.SetDirection(lig.Pin_number, embd.Out)
	// 	embd.DigitalWrite(lig.Pin_number, embd.High)
	// 	b, err := json.Marshal(models.WeatherLEDMessage{
	// 		Success: "True",
	// 		Message: "It's getting cooler!!",
	// 	})
	// 	if err != nil {
	// 		log.Fatal(err)
	// 	}
	// 	rw.Header().Set("Content-Type", "application/json")
	// 	rw.Write(b)
	// } else if temperature > 22 {
	// 	embd.SetDirection(lig.Pin_number, embd.Out)
	// 	embd.DigitalWrite(lig.Pin_number, embd.High)
	// 	b, err := json.Marshal(models.WeatherLEDMessage{
	// 		Success: "True",
	// 		Message: "It's getting hotter!!",
	// 	})
	// 	if err != nil {
	// 		log.Fatal(err)
	// 	}
	// 	rw.Header().Set("Content-Type", "application/json")
	// 	rw.Write(b)
	// }
}
Beispiel #15
0
func NewRPi() *RPi {
	pi := &RPi{LEDs: []string{"GPIO_17", "GPIO_27", "GPIO_22"}}
	err := embd.InitGPIO()
	if err != nil {
		fmt.Println(err.Error())
	}
	for _, l := range pi.LEDs {
		err := embd.SetDirection(l, embd.Out)
		if err != nil {
			fmt.Println(err.Error())
		}
	}
	return pi
}
Beispiel #16
0
func main() {
	fmt.Println("initGpio called")
	var err error
	if err = embd.InitGPIO(); err != nil {
		panic(err)
	}
	defer embd.CloseGPIO()
	s := newServo("P9_14")

	for i := 0; i < 18; i++ {
		angle := 10 + (10 * i)
		fmt.Printf("Setting angle: %v\n", angle)
		if err := s.SetAngle(angle); err != nil {
			panic(err)
		}
		time.Sleep(1 * time.Second)
	}
}
Beispiel #17
0
func main() {
	if err := embd.InitGPIO(); err != nil {
		panic(err)
	}
	defer embd.CloseGPIO()

	pwm, err := embd.NewPWMPin("P9_14")
	if err != nil {
		panic(err)
	}
	defer pwm.Close()

	if err := pwm.SetDuty(embd.BBBPWMDefaultPeriod / 2); err != nil {
		panic(err)
	}

	time.Sleep(1 * time.Second)
}
Beispiel #18
0
func main() {
	pinnr := flag.Int("pin", -1, "gpio pin number of LED")
	flag.Parse()

	if *pinnr < 0 {
		fmt.Println("Please specify pin which LED is on! Be careful!")
		os.Exit(1)
	}

	panicIf(embd.InitGPIO())
	defer embd.CloseGPIO()

	l := led.New(*pinnr)
	if flag.NArg() <= 0 {
		fmt.Println("Blinking Heartbeat1000 pattern till you quit...")
		l.Blink(led.Heartbeat1000...)
	} else {
		pattern := make([]time.Duration, flag.NArg())
		for i, s := range flag.Args() {
			d, err := time.ParseDuration(s)
			if err != nil {
				fmt.Println("Error parsing duration:", err)
				os.Exit(1)
			}
			pattern[i] = d
		}

		fmt.Printf("Blinking your pattern %v till you quit...\n", pattern)
		l.Blink(pattern...)
	}

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

	fmt.Println("\nBye-bye.")
	go func() {
		// This we do in case l.Stop() doesn't work.
		// It's a way to force quit.
		<-c
		os.Exit(1)
	}()
	l.Stop()
}
func main() {

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

	led, err := embd.NewDigitalPin(17)
	if err != nil {
		panic(err)
	}
	defer led.Close()
	if err := led.SetDirection(embd.In); err != nil {
		panic(err)
	}

	state := false
	count := 0

	for {
		pin, err := led.Read()
		if err != nil {
			panic(err)
		}

		if pin == 1 {
			if state == false {
				count++
			}
		} else {
			count = 0
			state = false
		}

		if count >= 3 {
			state = true
			count = 0
			client.SendCounterTick("GAS_1")
		}

		time.Sleep(10 * time.Millisecond)

	}
}
Beispiel #20
0
func main() {
	flag.Parse()

	h, _, err := embd.DetectHost()
	if err != nil {
		return
	}

	var pinNo interface{}

	switch h {
	case embd.HostBBB:
		pinNo = "P9_31"
	case embd.HostRPi:
		pinNo = 10
	default:
		panic("host not supported (yet :P)")
	}

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

	led, err := embd.NewDigitalPin(pinNo)
	if err != nil {
		panic(err)
	}
	defer led.Close()

	if err := led.SetDirection(embd.Out); err != nil {
		panic(err)
	}
	if err := led.Write(embd.High); err != nil {
		panic(err)
	}

	time.Sleep(1 * time.Second)

	if err := led.SetDirection(embd.In); err != nil {
		panic(err)
	}
}
Beispiel #21
0
func main() {
	if err := embd.InitGPIO(); err != nil {
		panic(err)
	}
	defer embd.CloseGPIO()

	if err := embd.SetDirection(10, embd.Out); err != nil {
		panic(err)
	}
	if err := embd.DigitalWrite(10, embd.High); err != nil {
		panic(err)
	}

	time.Sleep(1 * time.Second)

	if err := embd.SetDirection(10, embd.In); err != nil {
		panic(err)
	}
}
func (l *LightsController) Toggle_led_light(rw http.ResponseWriter, req *http.Request) {
	flag.Parse()
	embd.InitGPIO()

	body, err := ioutil.ReadAll(req.Body)

	var lig models.Light

	if err != nil {
		panic(err)
	}
	err = json.Unmarshal(body, &lig)
	if err != nil {
		panic(err)
	}
	fmt.Println(lig.Pin_number)

	if lig.Status == true {
		embd.SetDirection(lig.Pin_number, embd.Out)
		embd.DigitalWrite(lig.Pin_number, embd.High)
		b, err := json.Marshal(models.LightMessage{
			Success: "True",
			Message: "Switched On!!!",
		})
		if err != nil {
			log.Fatal(err)
		}
		rw.Header().Set("Content-Type", "application/json")
		rw.Write(b)
	} else {
		embd.SetDirection(lig.Pin_number, embd.Out)
		embd.DigitalWrite(lig.Pin_number, embd.Low)
		b, err := json.Marshal(models.LightMessage{
			Success: "false",
			Message: "Switched Off!!!",
		})
		if err != nil {
			log.Fatal(err)
		}
		rw.Header().Set("Content-Type", "application/json")
		rw.Write(b)
	}
}
Beispiel #23
0
func main() {
	flag.Parse()

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

	data, err := embd.NewDigitalPin(4)
	if err != nil {
		panic(err)
	}
	defer data.Close()

	clock, err := embd.NewDigitalPin(3)
	if err != nil {
		panic(err)
	}
	defer clock.Close()

	sensor := sht1x.New(data, clock)

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

	ticker := time.NewTicker(2 * time.Second)
	defer ticker.Stop()

	for {
		select {
		case <-ticker.C:
			m, err := sensor.Measure()
			if err != nil {
				fmt.Printf("err: %v\n", err)
			}
			fmt.Printf("Temperature: %.1fC, Relative Humidity: %.1f%%, Dew Point: %.1fC\n", m.Temperature, m.RelativeHumidity, m.DewPoint)

		case <-quit:
			return
		}
	}
}
Beispiel #24
0
func main() {
	flag.Parse()

	embd.InitGPIO()
	defer embd.CloseGPIO()

	pwm, err := embd.NewPWMPin("P9_14")
	if err != nil {
		panic(err)
	}
	defer pwm.Close()

	servo := servo.New(pwm)

	quit := make(chan os.Signal, 1)
	signal.Notify(quit, 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 <-quit:
			return
		}
	}
}
Beispiel #25
0
func main() {
	flag.Parse()

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

	echoPin, err := embd.NewDigitalPin("P9_21")
	if err != nil {
		panic(err)
	}

	triggerPin, err := embd.NewDigitalPin("P9_22")
	if err != nil {
		panic(err)
	}

	rf := us020.New(echoPin, triggerPin, nil)
	defer rf.Close()

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

	for {
		select {
		default:
			distance, err := rf.Distance()
			if err != nil {
				panic(err)
			}
			fmt.Printf("Distance is %v\n", distance)

			time.Sleep(500 * time.Millisecond)
		case <-quit:
			return
		}
	}
}
Beispiel #26
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")
}
Beispiel #27
0
func initHardware() {
	err := embd.InitGPIO()
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays = make([]embd.DigitalPin, 8)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[0], err = embd.NewDigitalPin(4) //7)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[1], err = embd.NewDigitalPin(17) //0)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[2], err = embd.NewDigitalPin(18) //1)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[3], err = embd.NewDigitalPin(27) //2)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[4], err = embd.NewDigitalPin(22) //3)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[5], err = embd.NewDigitalPin(23) //4)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[6], err = embd.NewDigitalPin(24) //5)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	relays[7], err = embd.NewDigitalPin(25) //6)
	if err != nil {
		fmt.Println("GPIO ERR:", err)
		os.Exit(1)
	}
	for _, r := range relays {
		err = r.SetDirection(embd.Out)
		if err != nil {
			fmt.Println("GPIO ERR:", err)
			os.Exit(1)
		}
		err = r.Write(embd.Low)
		if err != nil {
			fmt.Println("GPIO ERR:", err)
			os.Exit(1)
		}
	}
}
Beispiel #28
0
	Use:   "pimon",
	Short: "monitor temperature and humidity",
	Long: `Pimon monitors the temperature and humidity and warns you
if if is not in the safe range defined by Larrivee.

  If pimon is run with default options and without any specific command,
  it will read all the configuration files it finds in the XDG config path.
  It will also store any measurements in XDG_DATA_HOME/pimon/th.csv.
`,
	Run: func(cmd *cobra.Command, args []string) {
		Conf.Assert()

		exitIf(pimonLock())
		defer pimonUnlock()

		exitIf(embd.InitGPIO())
		defer embd.CloseGPIO()

		done := make(chan struct{})
		c := make(chan os.Signal, 1)
		signal.Notify(c, os.Interrupt)

		nl := &WarningLED{led.New(Conf.PinWarningLED), guitar.Low}
		csv, err := NewCSVPersister(xdg.UserData(databaseSuffix))
		if err != nil {
			log.Error("error persisting: ", err)
			return
		}
		m, err := NewMonitor(csv, 0.1)
		if err != nil {
			log.Error("error reading persistent file: ", err)
Beispiel #29
0
func main() {

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

	btn17, err := embd.NewDigitalPin(17)
	if err != nil {
		panic(err)
	}
	btn18, err := embd.NewDigitalPin(18)
	if err != nil {
		panic(err)
	}

	defer btn17.Close()
	defer btn18.Close()

	if err := btn17.SetDirection(embd.In); err != nil {
		panic(err)
	}
	if err := btn18.SetDirection(embd.In); err != nil {
		panic(err)
	}
	btn17.ActiveLow(false)
	btn18.ActiveLow(false)

	motion := make(chan embd.DigitalPin)
	err = btn17.Watch(embd.EdgeBoth, func(btn embd.DigitalPin) {
		motion <- btn
	})
	if err != nil {
		panic(err)
	}
	err = btn18.Watch(embd.EdgeBoth, func(btn embd.DigitalPin) {
		motion <- btn
	})
	if err != nil {
		panic(err)
	}

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

	fmt.Println("listening...")
	dur, _ := time.ParseDuration("-5m")
	onTime := time.Now().Add(dur)
	for {
		select {
		case <-c:
			fmt.Println("bye")
			return
		case btn := <-motion:
			direction := getDirection(btn.N())
			v, _ := btn.Read()
			if v == 0 {
				log.Printf("Motion stopped from %s.\n", direction)
				continue
			}
			log.Printf("Motion detected from %s.\n", direction)
			if time.Since(onTime).Seconds() > 300 {
				turnScreenOn()
				onTime = time.Now()
			}

		}
	}
}
Beispiel #30
0
func main() {
	stepDelay := flag.Int("step-delay", 10, "milliseconds between steps")
	flag.Parse()

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

	// Physical pins 11,15,16,18 on rasp pi
	// GPIO17,GPIO22,GPIO23,GPIO24
	stepPinNums := []int{17, 22, 23, 24}

	stepPins := make([]embd.DigitalPin, 4)

	for i, pinNum := range stepPinNums {
		pin, err := embd.NewDigitalPin(pinNum)
		if err != nil {
			panic(err)
		}
		defer pin.Close()
		if err := pin.SetDirection(embd.Out); err != nil {
			panic(err)
		}
		if err := pin.Write(embd.Low); err != nil {
			panic(err)
		}
		defer pin.SetDirection(embd.In)

		stepPins[i] = pin
	}

	// Define sequence described in manufacturer's datasheet
	seq := [][]int{
		[]int{1, 0, 0, 0},
		[]int{1, 1, 0, 0},
		[]int{0, 1, 0, 0},
		[]int{0, 1, 1, 0},
		[]int{0, 0, 1, 0},
		[]int{0, 0, 1, 1},
		[]int{0, 0, 0, 1},
		[]int{1, 0, 0, 1},
	}
	stepCount := len(seq) - 1
	stepDir := 2 // Set to 1 or 2 for clockwise, -1 or -2 for counter-clockwise

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

	// Start main loop
	ticker := time.NewTicker(time.Duration(*stepDelay) * time.Millisecond)
	defer ticker.Stop()

	var stepCounter int
	for {
		select {
		case <-ticker.C:
			// set pins to appropriate values for given position in the sequence
			for i, pin := range stepPins {
				if seq[stepCounter][i] != 0 {
					fmt.Printf("Enable pin %d, step %d\n", i, stepCounter)
					if err := pin.Write(embd.High); err != nil {
						panic(err)
					}
				} else {
					if err := pin.Write(embd.Low); err != nil {
						panic(err)
					}
				}
			}
			stepCounter += stepDir

			// If we reach the end of the sequence start again
			if stepCounter >= stepCount {
				stepCounter = 0
			} else if stepCounter < 0 {
				stepCounter = stepCount
			}

		case <-quit:
			return
		}
	}
}