Skip to content

Commit

Permalink
updates required for updated go-rpio library, balanced PWM
Browse files Browse the repository at this point in the history
  • Loading branch information
youngkin committed Dec 2, 2021
1 parent 3c8edb0 commit 47efb76
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ go 1.15
require (
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1
github.com/rivo/tview v0.0.0-20210923051754-2cb20002bc4c
github.com/stianeikeland/go-rpio/v4 v4.5.1
github.com/stianeikeland/go-rpio/v4 v4.6.0
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
)

replace github.com/stianeikeland/go-rpio/v4 => github.com/youngkin/go-rpio/v4 v4.5.2
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/rivo/tview v0.0.0-20210923051754-2cb20002bc4c h1:ye4bWm8SafYmr0DADOKS
github.com/rivo/tview v0.0.0-20210923051754-2cb20002bc4c/go.mod h1:WIfMkQNY+oq/mWwtsjOYHIZBuwthioY2srOmljJkTnk=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/youngkin/go-rpio/v4 v4.5.2 h1:UTTRDNV/RVmRdLLecfbKlYK7re9NyyGndjiF6bVbSGU=
github.com/youngkin/go-rpio/v4 v4.5.2/go.mod h1:A3GvHxC1Om5zaId+HqB3HKqx4K/AqeckxB7qRjxMK7o=
github.com/stianeikeland/go-rpio/v4 v4.6.0 h1:eAJgtw3jTtvn/CqwbC82ntcS+dtzUTgo5qlZKe677EY=
github.com/stianeikeland/go-rpio/v4 v4.6.0/go.mod h1:A3GvHxC1Om5zaId+HqB3HKqx4K/AqeckxB7qRjxMK7o=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
Expand Down
12 changes: 6 additions & 6 deletions pwmdemo/pwmexplorer/apps/freqtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {
rrange int
pwmPin int
pulsewidth int
pwmMode int
pwmMode bool
)
pwmType := ""

Expand All @@ -56,10 +56,10 @@ func main() {
flag.IntVar(&rrange, "range", 10, "PWM range")
flag.IntVar(&pwmPin, "pin", 18, "BCM pin number")
flag.IntVar(&pulsewidth, "pulsewidth", 2, "PWM Pulse Width")
flag.IntVar(&pwmMode, "pwmmode", 0, "PWM Mode, rpio.Balanced or rpio.MarkSpace")
flag.BoolVar(&pwmMode, "pwmmode", rpio.Balanced, "PWM Mode, Balanced (true) or MarkSpace (false)")
flag.Parse()

fmt.Printf("Using: PWM pin: %d, PWM Type: %s, freq: %d, range: %d, pulse width: %d, pwm mode: %d\n",
fmt.Printf("Using: PWM pin: %d, PWM Type: %s, freq: %d, range: %d, pulse width: %d, pwm mode: %t\n",
pwmPin, pwmType, freq, rrange, pulsewidth, pwmMode)

// Obtain GPIO resources
Expand All @@ -73,11 +73,11 @@ func main() {
return
}

runHardwarePwm(pwmPin, freq, uint32(rrange), uint32(pulsewidth), uint32(pwmMode))
runHardwarePwm(pwmPin, freq, uint32(rrange), uint32(pulsewidth), pwmMode)
}

// runHardwarePWM starts PWM as implemented in the board hardware.
func runHardwarePwm(gpin, freq int, rrange, pulsewidth, pwmMode uint32) {
func runHardwarePwm(gpin, freq int, rrange, pulsewidth uint32, pwmMode bool) {
// Define the pin to be used,
// followed by setting its mode to PWM,
// then set directly set the PWM Clock frequency (note lack of divisor),
Expand All @@ -87,7 +87,7 @@ func runHardwarePwm(gpin, freq int, rrange, pulsewidth, pwmMode uint32) {
pin.Mode(rpio.Pwm)
pin.Freq(freq)
// To test rpio.SetDutyCycle() comment out the next line and uncomment the one below it.
rpio.SetDutyCycleMsen(pin, pulsewidth, rrange, pwmMode)
rpio.SetDutyCycleWithPwmMode(pin, pulsewidth, rrange, pwmMode)
//rpio.SetDutyCycle(pin, pulsewidth, rrange)

// Initialize signal handling needed to catch ctl-C
Expand Down
22 changes: 8 additions & 14 deletions pwmdemo/pwmexplorer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func newPrimitive(text string) tview.Primitive {
SetText(text)
}

func buildGoCommand(pin, freq, rrange, pulsewidth, pwmType, pwmMode string) []string {
func buildGoCommand(pin, freq, rrange, pulsewidth, pwmType string, pwmMode bool) []string {
if freq == "" && pwmType == software {
// For software PWM freq can be empty, but freqtest.go requires a valid flag value
// to be passed in. So set it to an arbitrary value. 5000 is good since it would
Expand All @@ -376,7 +376,7 @@ func buildGoCommand(pin, freq, rrange, pulsewidth, pwmType, pwmMode string) []st
return []string{"/usr/local/go/bin/go", "run", "./apps/freqtest.go",
fmt.Sprintf("-pin=%s", pin), fmt.Sprintf("-freq=%s", freq),
fmt.Sprintf("-range=%s", rrange), fmt.Sprintf("-pulsewidth=%s", pulsewidth),
fmt.Sprintf("-pwmType=%s", pwmType), fmt.Sprintf("-pwmmode=%s", pwmMode),
fmt.Sprintf("-pwmType=%s", pwmType), fmt.Sprintf("-pwmmode=%t", pwmMode),
}
}

Expand Down Expand Up @@ -463,23 +463,17 @@ func getButtonForm(ui *tview.Application, pwmApp *PWMApp, msg *tview.TextView) *
freqMsg := fmt.Sprintf("PWM Clock Frequency(Hz): %9.2f, GPIO Pin Frequency(Hz): %9.2f",
pwmClockFreq, pwmClockFreq/float32(rrangeInt))

pwmModeRpio := "0"
pwmModeGo := false
if pwmMode == pwmModeBal {
pwmModeRpio = "0"
} else {
pwmModeRpio = "1"
pwmModeGo = true
}

// if nonPwmPin is populated use it
pin := ""
pinWarningText := ""
pin := pwmPin
if nonPwmPin != "" {
pin = nonPwmPin
} else {
pin = pwmPin
}
pinWarningText := ""
if lang == goLang {
msg.SetText(fmt.Sprintf("Command line: %v\n%s%s", buildGoCommand(pin, divisor, rrange, pulsewidth, pwmType, pwmModeRpio),
msg.SetText(fmt.Sprintf("Command line: %v\n%s%s", buildGoCommand(pin, divisor, rrange, pulsewidth, pwmType, pwmModeGo),
freqMsg, pinWarningText))
} else {
msg.SetText(fmt.Sprintf("Command line: %v\n%s%s", buildCCommand(pin, divisor, rrange, pulsewidth, pwmType,
Expand All @@ -489,7 +483,7 @@ func getButtonForm(ui *tview.Application, pwmApp *PWMApp, msg *tview.TextView) *
var out bytes.Buffer
var cmd *exec.Cmd
if lang == goLang {
cmd = exec.Command("sudo", buildGoCommand(pin, divisor, rrange, pulsewidth, pwmType, pwmModeRpio)...)
cmd = exec.Command("sudo", buildGoCommand(pin, divisor, rrange, pulsewidth, pwmType, pwmModeGo)...)
} else {
cmd = exec.Command("sudo", buildCCommand(pin, divisor, rrange, pulsewidth, pwmType, pwmMode)...)
}
Expand Down

0 comments on commit 47efb76

Please sign in to comment.