-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from youngkin/master
Add support for PWM balanced mode
- Loading branch information
Showing
2 changed files
with
120 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
A PWM example by @youngkin, using the go-rpio library | ||
Fades a PWM hardware pin in and out using PWM mode balanced (vs. markspace) | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/stianeikeland/go-rpio/v4" | ||
) | ||
|
||
func main() { | ||
err := rpio.Open() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
defer rpio.Close() | ||
|
||
pin := rpio.Pin(19) | ||
pin.Mode(rpio.Pwm) | ||
pin.Freq(64000) | ||
pin.DutyCycleWithPwmMode(0, 32, rpio.Balanced) | ||
// the LED will be blinking at 2000Hz | ||
// (source frequency divided by cycle length => 64000/32 = 2000) | ||
|
||
// five times smoothly fade in and out | ||
for i := 0; i < 5; i++ { | ||
for i := uint32(0); i < 32; i++ { // increasing brightness | ||
pin.DutyCycleWithPwmMode(i, 32, rpio.Balanced) | ||
time.Sleep(time.Second / 32) | ||
} | ||
for i := uint32(32); i > 0; i-- { // decreasing brightness | ||
pin.DutyCycleWithPwmMode(i, 32, rpio.Balanced) | ||
time.Sleep(time.Second / 32) | ||
} | ||
} | ||
|
||
pin.DutyCycleWithPwmMode(0, 32, rpio.Balanced) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters