mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-03-13 21:18:43 +01:00
Adding example
This commit is contained in:
parent
ada3e6e708
commit
9241d64c05
46
examples/software pwm/pwm.go
Normal file
46
examples/software pwm/pwm.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
|
||||
A Software Based PWM example by @Ronin11, using the go-rpio library
|
||||
|
||||
Toggles a LED on physical pin 19 (mcu pin 10)
|
||||
Connect a LED with resistor from pin 19 to ground.
|
||||
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/Ronin11/go-rpio"
|
||||
)
|
||||
|
||||
const pin = rpio.Pin(10)
|
||||
|
||||
func main() {
|
||||
|
||||
if err := rpio.Open(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Unmap gpio memory when done
|
||||
defer rpio.Close()
|
||||
|
||||
//Creates the PWM Signal running on the pin, at 2KHz, with a 50 on 50 off cycle.
|
||||
pwm := rpio.CreateSofwarePWM(pin, 2000, 0, 32)
|
||||
pwm.Start()
|
||||
// five times smoothly fade in and out
|
||||
for i := 0; i < 5; i++ {
|
||||
for i := uint32(0); i < 32; i++ { // increasing brightness
|
||||
pwm.SetDutyCycle(i, 32)
|
||||
time.Sleep(time.Second/32)
|
||||
}
|
||||
for i := uint32(32); i > 0; i-=3 { // decreasing brightness
|
||||
pwm.SetDutyCyclePercentage(i)
|
||||
time.Sleep(time.Second/32)
|
||||
}
|
||||
}
|
||||
pwm.Stop()
|
||||
}
|
114
rpio.go
114
rpio.go
|
@ -100,59 +100,6 @@ type SoftwarePWM struct {
|
|||
state PwmState
|
||||
}
|
||||
|
||||
//Create a Software PWM struct
|
||||
func CreateSofwarePWM(pin Pin, freq uint32, dutyLen uint32, cycleLen uint32) *SoftwarePWM{
|
||||
//Set pin as Output
|
||||
pin.Output()
|
||||
return &SoftwarePWM{pin: pin, freq: freq, dutyLen: dutyLen, cycleLen: cycleLen}
|
||||
}
|
||||
|
||||
func (swPwm *SoftwarePWM) SetDutyCycle(dutyLen uint32, cycleLen uint32){
|
||||
swPwm.dutyLen = dutyLen
|
||||
swPwm.cycleLen = cycleLen
|
||||
}
|
||||
|
||||
func (swPwm *SoftwarePWM) SetDutyCyclePercentage(percentage uint8){
|
||||
if percentage > 100{
|
||||
percentage = 100
|
||||
}
|
||||
swPwm.dutyLen = uint32(percentage)
|
||||
swPwm.cycleLen = 100
|
||||
}
|
||||
|
||||
func (swPwm *SoftwarePWM) SetFreq(freq uint32){
|
||||
swPwm.freq = freq
|
||||
}
|
||||
|
||||
func (swPwm *SoftwarePWM) Start(){
|
||||
swPwm.status = RUNNING
|
||||
swPwm.pwmLoop()
|
||||
}
|
||||
|
||||
func (swPwm *SoftwarePWM) Stop(){
|
||||
swPwm.status = STOPPED
|
||||
swPwm.pin.Write(Low)
|
||||
}
|
||||
|
||||
func (swPwm *SoftwarePWM) pwmLoop(){
|
||||
go func(){
|
||||
var sleepInterval time.Duration
|
||||
for swPwm.status == RUNNING {
|
||||
sleepInterval = time.Second / time.Duration(swPwm.freq)
|
||||
if swPwm.state == OFF {
|
||||
swPwm.pin.Write(High)
|
||||
swPwm.state = ON
|
||||
sleepInterval = sleepInterval * time.Duration(swPwm.dutyLen)
|
||||
} else {
|
||||
swPwm.pin.Write(Low)
|
||||
swPwm.state = OFF
|
||||
sleepInterval = sleepInterval * time.Duration(swPwm.cycleLen - swPwm.dutyLen)
|
||||
}
|
||||
time.Sleep(sleepInterval)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Memory offsets for gpio, see the spec for more details
|
||||
const (
|
||||
bcm2835Base = 0x20000000
|
||||
|
@ -621,6 +568,67 @@ func StartPwm() {
|
|||
pwmMem[pwmCtlReg] = pwmMem[pwmCtlReg] | pwen<<8 | pwen
|
||||
}
|
||||
|
||||
//Create a Software PWM struct
|
||||
func CreateSofwarePWM(pin Pin, freq uint32, dutyLen uint32, cycleLen uint32) *SoftwarePWM{
|
||||
//Set pin as Output
|
||||
pin.Output()
|
||||
return &SoftwarePWM{pin: pin, freq: freq, dutyLen: dutyLen, cycleLen: cycleLen}
|
||||
}
|
||||
|
||||
//Sets the duty cycle for the software PWM
|
||||
func (swPwm *SoftwarePWM) SetDutyCycle(dutyLen uint32, cycleLen uint32){
|
||||
swPwm.dutyLen = dutyLen
|
||||
swPwm.cycleLen = cycleLen
|
||||
}
|
||||
|
||||
//Sets the duty cycle percentage of 100 for the sofware PWM
|
||||
func (swPwm *SoftwarePWM) SetDutyCyclePercentage(percentage uint8){
|
||||
if percentage > 100{
|
||||
percentage = 100
|
||||
}
|
||||
swPwm.dutyLen = uint32(percentage)
|
||||
swPwm.cycleLen = 100
|
||||
}
|
||||
|
||||
//Sets the frequency of the software PWM
|
||||
func (swPwm *SoftwarePWM) SetFreq(freq uint32){
|
||||
swPwm.freq = freq
|
||||
}
|
||||
|
||||
//Starts the software PWM
|
||||
func (swPwm *SoftwarePWM) Start(){
|
||||
swPwm.status = RUNNING
|
||||
swPwm.pwmLoop()
|
||||
}
|
||||
|
||||
//Stops the software PWM
|
||||
func (swPwm *SoftwarePWM) Stop(){
|
||||
swPwm.status = STOPPED
|
||||
swPwm.pin.Write(Low)
|
||||
}
|
||||
|
||||
|
||||
//Internal PWM execution loop
|
||||
func (swPwm *SoftwarePWM) pwmLoop(){
|
||||
go func(){
|
||||
var sleepInterval time.Duration
|
||||
for swPwm.status == RUNNING {
|
||||
//sleepInterval is the basic unit of time between pwm 'ticks'
|
||||
sleepInterval = time.Second / time.Duration(swPwm.freq)
|
||||
if swPwm.state == OFF {
|
||||
swPwm.pin.Write(High)
|
||||
swPwm.state = ON
|
||||
sleepInterval = sleepInterval * time.Duration(swPwm.dutyLen)
|
||||
} else {
|
||||
swPwm.pin.Write(Low)
|
||||
swPwm.state = OFF
|
||||
sleepInterval = sleepInterval * time.Duration(swPwm.cycleLen - swPwm.dutyLen)
|
||||
}
|
||||
time.Sleep(sleepInterval)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Open and memory map GPIO memory range from /dev/mem .
|
||||
// Some reflection magic is used to convert it to a unsafe []uint32 pointer
|
||||
func Open() (err error) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user