Esempio n. 1
0
func (s *servo) init() error {
	if s.initialized {
		return nil
	}

	var p embd.PWMPin
	var err error

	fmt.Printf("NewPWMPin called for: %v", s.pin)
	p, err = embd.NewPWMPin(s.pin)
	if err != nil {
		return err
	}
	s.pwmDriver = p
	fmt.Printf("Setting polarity here to: %v", embd.Positive)
	if err = s.pwmDriver.SetPolarity(embd.Positive); err != nil {
		return err
	}

	fmt.Printf("Setting period here to: %v", 15000000)
	if err = s.pwmDriver.SetPeriod(15000000); err != nil {
		return err
	}

	fmt.Printf("Setting duty here to: %v", 2500000)
	if err = s.pwmDriver.SetDuty(2500000); err != nil {
		return err
	}
	s.initialized = true
	return nil
}
Esempio n. 2
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)
}
Esempio n. 3
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
		}
	}
}
func (g *GPIO) PinInit(pinId string, dir Direction, pullup PullUp, name string) error {
	var pin interface{}
	state := byte(0)

	if dir == PWM {

		host, _, err := embd.DetectHost()
		if err != nil {
			return err
		}
		if host == embd.HostRPi {
			// use pi blaster pin
			log.Println("Creating PWM pin on Pi")

			// get the host descriptor
			desc, err := embd.DescribeHost()
			if err != nil {
				return err
			}
			// get the pinmap
			embdMap := desc.GPIODriver().PinMap()
			// lookup the pinId in the map
			var pinDesc *embd.PinDesc
			for i := range embdMap {
				pd := embdMap[i]

				if pd.ID == pinId {
					pinDesc = pd
					break
				}

				for j := range pd.Aliases {
					if pd.Aliases[j] == pinId {
						pinDesc = pd
						break
					}
				}
			}
			if pinDesc != nil {
				// we found a pin with that name....what is its first Alias?
				pinIdInt, err := strconv.Atoi(pinDesc.Aliases[0])
				if err != nil {
					log.Println("Failed to parse int from alias : ", pinDesc.Aliases[0])
					return err
				}
				p := NewBlasterPin(pinIdInt)
				pin = p
			} else {
				log.Println("Failed to find Pin ", pinId)
				return errors.New("Failed to find pin " + pinId)
			}
		} else {
			// bbb, so use embd since pwm pins work there
			p, err := embd.NewPWMPin(pinId)
			if err != nil {
				log.Println("Failed to create PWM Pin using key ", pinId, " : ", err.Error())
				return err
			}
			pin = p
		}
	} else {
		// add a pin
		p, err := embd.NewDigitalPin(pinId)
		if err != nil {
			return err
		}
		pin = p

		err = p.SetDirection(embd.Direction(dir))
		if err != nil {
			return err
		}

		if pullup == Pull_Up {
			err = p.PullUp()

			// pullup and down not implemented on rpi host so we need to manually set initial states
			// not ideal as a pullup really isn't the same thing but it works for most use cases

			if err != nil {
				log.Println("Failed to set pullup on " + pinId + " setting high state instead : " + err.Error())
				// we failed to set pullup, so lets set initial state high instead
				err = p.Write(1)
				state = 1
				if err != nil {
					return err
				}
			}
		} else if pullup == Pull_Down {
			err = p.PullDown()

			if err != nil {

				log.Println("Failed to set pulldown on " + pinId + " setting low state instead : " + err.Error())

				err = p.Write(0)
				state = 1
				if err != nil {
					return err
				}
			}
		}
	}

	// test to see if we already have a state for this pin
	existingPin, exists := g.pinStates[pinId]
	if exists {
		existingPin.Pin = pin
		existingPin.Name = name
		existingPin.Dir = dir
		existingPin.State = state
		existingPin.Pullup = pullup
		g.pinStates[pinId] = existingPin

		g.pinStateChanged <- existingPin
		g.pinRemoved <- pinId
		g.pinAdded <- g.pinStates[pinId]
	} else {
		g.pinStates[pinId] = PinState{pin, pinId, dir, state, pullup, name}
		g.pinAdded <- g.pinStates[pinId]
	}

	return nil
}