Skip to content
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

Implement clock and pwm modes #15

Merged
merged 21 commits into from
Jan 12, 2018
Merged

Conversation

drahoslove
Copy link
Collaborator

Hi there.
I managed to add third pin mode - clock.

It is just clock (using CM_GPnDIV and CM_GPnCTL registers) - no pwm (yet).

Suggestions about better method/function naming are welcomed.
I tried to be consistent with current ideology, but not sure if it makes sense.

So this: PinMode(pin, rpio.Clock) <=> pin.Mode(rpio.Clock) <=> pin.Clock()
is analogical to: PinMode(pin, rpio.Output) <=> pin.Mode(rpio.Output) <=> pin.Output()

and this: SetFreq(pin, 10000) <=> pin.Freq(10000) <=> ???
is analogical to: WritePin(pin, rpio.High) <=> pin.Write(rpio.High) <=> pin.High()

Also not sure if it might make sense to add more public methods eg. for stopping/resetting clocks.

Please merge this if you like it.

@drahoslove drahoslove changed the title Implement clock mode Implement clock and pwm modes Nov 20, 2017
@drahoslove
Copy link
Collaborator Author

So I also implemented simple pulse width modulation functionality.
For example, to set pin to 38kHz with 1/4 duty cycle you can do:

pin.Pwm()
pin.DutyCycle(1, 4)
pin.Freq(38000 * 4)

Dimming led example:

package main

import (
        "os"
        "time"
        "github.com/stianeikeland/go-rpio"
)

func main() {
        err := rpio.Open()
        if err != nil {
                os.Exit(1)
        }
        defer rpio.Close()

        pin := rpio.Pin(19)
        pin.Mode(rpio.Pwm)
        pin.Freq(60000)
        pin.DutyCycle(0, 32)

        for i := 0; i < 5; i++ {
                for i := uint32(0); i < 32; i++ { // increasing brightness
                        pin.DutyCycle(i, 32)
                        time.Sleep(time.Second/32)
                }
                for i := uint32(32); i != 0; i-- { // decreasing brightness
                        pin.DutyCycle(i, 32)
                        time.Sleep(time.Second/32)
                }
        }
}

@Albert221
Copy link

Can @stianeikeland merge this?

@drahoslove
Copy link
Collaborator Author

@stianeikeland, I know it's a lot of changes to review at once, I've done some refactoring even in your code and so on. But I would appreciate if you can check it and merge.

Or, if you do not want to maintain this repo anymore, then I would like to ask your permission to separate my fork from your repo and make my own stand-alone repository (with a different name perhaps and with mention, it is based on your work, of course.) which I will maintain by myself.

@stianeikeland
Copy link
Owner

stianeikeland commented Jan 12, 2018

Hey,
sorry for dropping the ball on this, I've been meaning to play with it but other things keep getting in the way.

Anyway, I've had a go at it this evening, and managed to make a LED phase in and out using an old Raspberry PI 1 on pin 18.

Code seems well written and works as intended, so I'll merge this as is.

Would love to have an example program (maybe like the one you have above) that demonstrate how to use PWM. Feel free to add that if you want :) (Or I can just grab your example from above, let me know) :)

EDIT: i copied your example program to the examples folder, feel free to change it if you want.

Thanks!

@stianeikeland stianeikeland merged commit ca7ded7 into stianeikeland:master Jan 12, 2018
@drahoslove
Copy link
Collaborator Author

Great, thank you.
I think the example is ok. But I might change it just slightly - point out that result frequency of pin in PWM mode depends both on Freq and DutyCycle arguments - which may not seem obvious.

I noticed a tiny error in the code. Will make PR soon.

I would also consider adding a link to https://godoc.org/github.com/stianeikeland/go-rpio to readme - for people who do not know Golang so well and do not know this service exists, and just because it is convenient.

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.

3 participants