Skip to content

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Software PWM Work #24

wants to merge 6 commits into from

Conversation

Ronin11
Copy link

@Ronin11 Ronin11 commented Jun 25, 2018

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

Copy link
Collaborator

@drahoslove drahoslove left a 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(){
Copy link
Collaborator

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 {
Copy link
Collaborator

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(...)
}

Copy link
Collaborator

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants