// SetPwm sets the ON and OFF time registers for pwm signal shaping.
// channel: 0-15
// onTime/offTime: 0-4095
19385
func (d *PCA9685) SetPwm(channel, onTime, offTime int) error {
	if err := d.setup(); err != nil {
		return err
	}

	onTimeLowReg := byte(pwm0OnLowReg + (4 * channel))

	onTimeLow := byte(onTime & 0xFF)
	onTimeHigh := byte(onTime >> 8)
	offTimeLow := byte(offTime & 0xFF)
	offTimeHigh := byte(offTime >> 8)

	if err := d.Bus.WriteByteToReg(d.Addr, onTimeLowReg, onTimeLow); err != nil {
		return err
	}

	glog.V(2).Infof("pca9685: writing on-time low [%#02x] to CHAN%v_ON_L reg [reg: %#02x]", onTimeLow, channel, onTimeLowReg)

	onTimeHighReg := onTimeLowReg + 1
	if err := d.Bus.WriteByteToReg(d.Addr, onTimeHighReg, onTimeHigh); err != nil {
		return err
	}
	glog.V(2).Infof("pca9685: writing on-time high [%#02x] to CHAN%v_ON_H reg [reg: %#02x]", onTimeHigh, channel, onTimeHighReg)

	offTimeLowReg := onTimeHighReg + 1
	if err := d.Bus.WriteByteToReg(d.Addr, offTimeLowReg, offTimeLow); err != nil {
		return err
	}
	glog.V(2).Infof("pca9685: writing off-time low [%#02x] to CHAN%v_OFF_L reg [reg: %#02x]", offTimeLow, channel, offTimeLowReg)

	offTimeHighReg := offTimeLowReg + 1
	if err := d.Bus.WriteByteToReg(d.Addr, offTimeHighReg, offTimeHigh); err != nil {
		return err
	}
	glog.V(2).Infof("pca9685: writing off-time high [%#02x] to CHAN%v_OFF_H reg [reg: %#02x]", offTimeHigh, channel, offTimeHighReg)

	return nil
}
943857
func (s *ManagedAction) SetWindowStartTime(v time.Time) *ManagedAction {
	s.WindowStartTime = &v
	return s
}
1036994
func (f *FakeClock) SetTime(t time.Time) {
	f.lock.Lock()
	defer f.lock.Unlock()
	f.setTimeLocked(t)
}
760671
func (s *SuggestStatus) SetTimems(v int64) *SuggestStatus {
	s.Timems = &v
	return s
}
263453
func (p *PCA9685Driver) SetPWMFreq(freq float32) error {
	// IC oscillator frequency is 25 MHz
	var prescalevel float32 = 25000000
	// Find frequency of PWM waveform
	prescalevel /= 4096
	// Ratio between desired frequency and maximum
	prescalevel /= freq
	prescalevel -= 1
	// Round value to nearest whole
	prescale := byte(prescalevel + 0.5)

	if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1)}); err != nil {
		return err
	}
	data := make([]byte, 1)
	oldmode, err := p.connection.Read(data)
	if err != nil {
		return err
	}

	// Put oscillator in sleep mode, clear bit 7 here to avoid overwriting
	// previous setting
	newmode := (oldmode & 0x7F) | 0x10
	if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(newmode)}); err != nil {
		return err
	}
	// Write prescaler value
	if _, err := p.connection.Write([]byte{byte(PCA9685_PRESCALE), prescale}); err != nil {
		return err
	}
	// Put back to old settings
	if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(oldmode)}); err != nil {
		return err
	}

	time.Sleep(100 * time.Millisecond)
	// Enable response to All Call address, enable auto-increment, clear restart
	if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(oldmode | 0xa1)}); err != nil {
		return err
	}

	return nil
}
631109
func (c *Adaptor) PWMPin(pin string) (sysfsPin sysfs.PWMPinner, err error) {
	c.mutex.Lock()
	defer c.mutex.Unlock()

	i, err := c.translatePwmPin(pin)
	if err != nil {
		return nil, err
	}
	if i == -1 {
		return nil, errors.New("Not a PWM pin")
	}

	if c.pwmPins[i] == nil {
		newPin := sysfs.NewPWMPin(i)
		if err = newPin.Export(); err != nil {
			return
		}
		// Make sure pwm is disabled when setting polarity
		if err = newPin.Enable(false); err != nil {
			return
		}
		if err = newPin.InvertPolarity(false); err != nil {
			return
		}
		if err = newPin.Enable(true); err != nil {
			return
		}
		if err = newPin.SetPeriod(10000000); err != nil {
			return
		}
		c.pwmPins[i] = newPin
	}

	sysfsPin = c.pwmPins[i]
	return
}
204047
func (a *AdafruitMotorHatDriver) SetDCMotorSpeed(dcMotor int, speed int32) (err error) {
	if err = a.setPWM(a.motorHatConnection, a.dcMotors[dcMotor].pwmPin, 0, speed*16); err != nil {
		return
	}
	return
}
613689
func (module *BBPWMModule) EnablePin(pin Pin, enabled bool) error {
	if module.definedPins[pin] == nil {
		return fmt.Errorf("Pin %d is not known as a PWM pin on module %s", pin, module.GetName())
	}

	openPin := module.openPins[pin]
	if enabled {
		// ensure pin is enabled by creating an open pin
		if openPin == nil {
			p, e := module.makeOpenPin(pin)
			if e != nil {
				return e
			}
			module.openPins[pin] = p
			return p.enabled(true)
		}
	} else {
		// disable the pin if enabled
		if openPin != nil {
			return openPin.enabled(false)
		}
	}
	return nil
}
1322082
func (s *GenericRevisionInfo) SetRegisterTime(v time.Time) *GenericRevisionInfo {
	s.RegisterTime = &v
	return s
}
73718
func (module *BBPWMModule) SetOptions(options map[string]interface{}) error {
	v := options["pins"]
	if v == nil {
		return fmt.Errorf("Module '%s' SetOptions() did not get 'pins' values", module.GetName())
	}

	module.definedPins = v.(BBPWMModulePinDefMap)
	return nil
}
1048582
func (a *DispatchMouseEventArgs) SetTimestamp(timestamp TimeSinceEpoch) *DispatchMouseEventArgs {
	a.Timestamp = timestamp
	return a
}
1227529
func (s *UpdateChannelOutput) SetChannel(v *Channel) *UpdateChannelOutput {
	s.Channel = v
	return s
}
1351948
func (s *JobTemplateSettings) SetEsam(v *EsamSettings) *JobTemplateSettings {
	s.Esam = v
	return s
}
775446
func (s *SearchStatus) SetTimems(v int64) *SearchStatus {
	s.Timems = &v
	return s
}
1080541
func (g *Driver) SetPWMDuty(port Grove, duty uint16) (err error) {
	if duty < 0 {
		duty = 0
	}
	if duty > 100 {
		duty = 100
	}
	duty = duty * 10
	return g.writeBytes([]byte{
		goPiGo3Address,
		SET_GROVE_PWM_DUTY,
		byte(port),
		byte((duty >> 8) & 0xFF),
		byte(duty & 0xFF),
	})
}
556404
func (d *Driver) SetFastMode() error {
	d.cmdMutex.Lock()
	defer d.cmdMutex.Unlock()

	d.throttle = 1
	return nil
}
405551
func (e *Adaptor) PwmWrite(pin string, val byte) (err error) {
	pwmPin, err := e.PWMPin(pin)
	if err != nil {
		return
	}
	period, err := pwmPin.Period()
	if err != nil {
		return err
	}
	duty := gobot.FromScale(float64(val), 0, 255.0)
	return pwmPin.SetDutyCycle(uint32(float64(period) * duty))
}