-
Notifications
You must be signed in to change notification settings - Fork 225
Software PWM Work #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the liberty to write some notes.
|
||
//Internal PWM execution loop | ||
func (swPwm *SoftwarePWM) pwmLoop(){ | ||
go func(){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to use anonymous function, you can use go swPwm.pwmLoop()
in Start()
method above.
func (swPwm *SoftwarePWM) pwmLoop(){ | ||
go func(){ | ||
var sleepInterval time.Duration | ||
for swPwm.status == RUNNING { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need for ON/OFF state if you use something like this:
for {
if status != RUNING { breake }
pin.Write(High)
time.Sleep(...)
if status != RUNING { breake }
pin.Write(Low)
time.Sleep(...)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or better break using select and channel as I mentioned before.
select {
case <-stop:
break
default:
}
I would also use time.Ticker
instead of time.Sleep
, for better accuracy.
|
||
//Stops the software PWM | ||
func (swPwm *SoftwarePWM) Stop(){ | ||
swPwm.status = STOPPED |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think go way to stop the loop might be to use "terminating" channel. Using status value like this is not thread safe.
I don't think this is quite ready to be merged in quite yet. But I wanted to get your input earlier rather than later. I was needing some software based PWMs and saw the issue. So I hacked one together tonight real quick. It works pretty alright, the biggest thins is that I want to make it feel more consistent with your current lib. Not having to use construction methods and the like. But with the little bit thats there, what are your thoughts? Do you have any issues with running a goroutine for the pwm? Any blaring issues I missed?
Let me know