Ejemplo n.º 1
0
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
		}
	}
}
Ejemplo n.º 2
0
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
		}
	}
}
Ejemplo n.º 3
0
func main() {
	guardianToken := os.Getenv("GUARDIAN_TOKEN")
	if guardianToken == "" {
		log.Fatal("GUARDIAN_TOKEN is required")
	}

	ctx, cancel := context.WithCancel(context.Background())
	sigch := make(chan os.Signal, 2)
	signal.Notify(sigch, os.Interrupt, syscall.SIGTERM)
	go handleSignals(sigch, ctx, cancel)

	if err := embd.InitI2C(); err != nil {
		log.Fatal("I2C init: ", err)
	}
	defer embd.CloseI2C()

	bus := embd.NewI2CBus(1)

	dev := pca9685.New(bus, 0x40)
	dev.Freq = 100
	defer dev.Close()

	// wake the servo controller so we can reset its channels
	if err := dev.Wake(); err != nil {
		log.Fatal("waking: ", err)
	}
	resetAllChannels(dev)
	resetTimer := time.After(time.Second) // long enough for servos to reset

	query := url.Values{}
	query.Set("vsn", "1.0.0")
	query.Set("guardian_token", guardianToken)
	u := url.URL{
		Scheme:   "wss",
		Host:     "opendoor-pusher.herokuapp.com",
		Path:     "/events/websocket",
		RawQuery: query.Encode(),
	}

	joinPayload, err := json.Marshal(GuardianPayload{GuardianToken: guardianToken})
	if err != nil {
		log.Fatal(err)
	}

	client := phoenix.InitClient(u.String(), []string{topicName}, joinPayload)
	eventch := client.Start()
	defer client.Close()

	select {
	case <-resetTimer: // servos have had enough time to reset
		if err := dev.Sleep(); err != nil {
			log.Fatal(err)
		}
	case <-ctx.Done():
		return
	}

	for {
		select {
		case evt := <-eventch:
			switch evt.Event {
			case "acquisition_contract", "resale_contract":
				handleRingEvent(dev, evt)
			case "system_test":
				handleSystemTest(dev, evt)
			default:
				log.Printf("unhandled message received: %#v", evt)
			}
		case <-ctx.Done():
			return
		}
	}
}