Skip to content

Commit

Permalink
Merge pull request #36 from schwabix-1311/master
Browse files Browse the repository at this point in the history
Allow smoother dim operations on rpi.pwm
  • Loading branch information
ranjib authored Jul 10, 2021
2 parents f64f1b5 + 0bd2b69 commit 7b55275
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion hal/pwm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (p *channel) Set(value float64) error {
if err := p.driver.Frequency(p.pin, p.frequency); err != nil {
return err
}
if err := p.driver.DutyCycle(p.pin, int(value)); err != nil {
if err := p.driver.DutyCycle(p.pin, value); err != nil {
return err
}
if err := p.driver.Enable(p.pin); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pwm/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
type Driver interface {
Export(ch int) error
Unexport(ch int) error
DutyCycle(ch, duty int) error
DutyCycle(ch int, duty float64) error
Frequency(ch, freq int) error
Enable(ch int) error
Disable(ch int) error
Expand Down Expand Up @@ -58,10 +58,10 @@ func (d *driver) Unexport(ch int) error {
return d.writeFile(file, toS(ch), 0600)
}

// DutyCycle sets the duty cycle as a 0-100 percentage of the
// DutyCycle sets the duty cycle as a 0-100.0 percentage of the
// given period of the driver. Does not assume the Frequency
// method has been called.
func (d *driver) DutyCycle(ch, duty int) error {
func (d *driver) DutyCycle(ch int, duty float64) error {
freqFile := filepath.Join(d.sysfs, fmt.Sprintf("pwm%d", ch), "period")
data, err := d.readFile(freqFile)
if err != nil {
Expand All @@ -72,7 +72,7 @@ func (d *driver) DutyCycle(ch, duty int) error {
if err != nil {
return err
}
nanoSecondsDuty := int64((float64(duty) / 100.0) * float64(period))
nanoSecondsDuty := int64((duty / 100.0) * float64(period))

file := filepath.Join(d.sysfs, fmt.Sprintf("pwm%d", ch), "duty_cycle")
return d.writeFile(file, toS64(nanoSecondsDuty), 0644)
Expand Down

0 comments on commit 7b55275

Please sign in to comment.