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

jetson(PWM): fix set period #990

Closed
wants to merge 1 commit into from
Closed

Conversation

Jwen98
Copy link

@Jwen98 Jwen98 commented Sep 25, 2023

Solved issues and/or description of the change

changed so the SetPeriod uses the value from the parameter
...

Manual test

  • OS and Version (Win/Mac/Linux):
  • Adaptor(s) and/or driver(s):
    ...

Checklist

  • The PR's target branch is 'hybridgroup:dev'
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (e.g. by run make test)
  • No linter errors exist locally (e.g. by run make fmt_check)
  • I have performed a self-review of my own code

If this is a new driver or adaptor:

  • I have added the name to the corresponding README.md
  • I have added an example to see how to setup and use it
  • I have checked or build at least my new example (e.g. by run make examples_check)

If this is a PR for release:

  • The PR's target branch is 'hybridgroup:release'
  • I have adjusted the CHANGELOG.md (or already prepared and will be merged as soon as possible)

changed so the SetPeriod uses the value from the parameter
@gen2thomas
Copy link
Collaborator

gen2thomas commented Oct 10, 2023

Hi @Jwen98 , thanks for finding this issue and providing a fix. Please read our contributing guide, the target branch needs to be "hybridgroup:dev".

Please can you change the related test file "pwm_pin_test.go" to this content, to test all paths and switch to testify package according to #960 :

Without your fix it will fail.

package jetson

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"gobot.io/x/gobot/v2"
	"gobot.io/x/gobot/v2/system"
)

var _ gobot.PWMPinner = (*PWMPin)(nil)

func TestPwmPin(t *testing.T) {
	a := system.NewAccesser()
	const (
		exportPath    = "/sys/class/pwm/pwmchip0/export"
		unexportPath  = "/sys/class/pwm/pwmchip0/unexport"
		enablePath    = "/sys/class/pwm/pwmchip0/pwm3/enable"
		periodPath    = "/sys/class/pwm/pwmchip0/pwm3/period"
		dutyCyclePath = "/sys/class/pwm/pwmchip0/pwm3/duty_cycle"
	)
	mockPaths := []string{
		exportPath,
		unexportPath,
		enablePath,
		periodPath,
		dutyCyclePath,
	}
	fs := a.UseMockFilesystem(mockPaths)

	pin := NewPWMPin(a, "/sys/class/pwm/pwmchip0", "3")
	require.Equal(t, "", fs.Files[exportPath].Contents)
	require.Equal(t, "", fs.Files[unexportPath].Contents)
	require.Equal(t, "", fs.Files[enablePath].Contents)
	require.Equal(t, "", fs.Files[periodPath].Contents)
	require.Equal(t, "", fs.Files[dutyCyclePath].Contents)

	assert.Nil(t, pin.Export())
	assert.Equal(t, "3", fs.Files[exportPath].Contents)

	assert.Nil(t, pin.SetEnabled(true))
	assert.Equal(t, "1", fs.Files[enablePath].Contents)

	val, _ := pin.Polarity()
	assert.True(t, val)
	assert.Nil(t, pin.SetPolarity(false))
	val, _ = pin.Polarity()
	assert.True(t, val)

	_, err := pin.Period()
	assert.Error(t, err, "Jetson PWM pin period not set")
	assert.Error(t, pin.SetDutyCycle(10000), "Jetson PWM pin period not set")
	assert.Equal(t, "", fs.Files[dutyCyclePath].Contents)

	assert.Nil(t, pin.SetPeriod(20000000))
	assert.Equal(t, "20000000", fs.Files[periodPath].Contents)
	period, _ := pin.Period()
	assert.Equal(t, uint32(20000000), period)
	assert.Error(t, pin.SetPeriod(10000000), "Cannot set the period of individual PWM pins on Jetson")
	assert.Equal(t, "20000000", fs.Files[periodPath].Contents)

	dc, _ := pin.DutyCycle()
	assert.Equal(t, uint32(0), dc)

	assert.Nil(t, pin.SetDutyCycle(10000))
	assert.Equal(t, "10000", fs.Files[dutyCyclePath].Contents)
	dc, _ = pin.DutyCycle()
	assert.Equal(t, uint32(10000), dc)

	assert.Error(t, pin.SetDutyCycle(999999999), "Duty cycle exceeds period")
	dc, _ = pin.DutyCycle()
	assert.Equal(t, "10000", fs.Files[dutyCyclePath].Contents)
	assert.Equal(t, uint32(10000), dc)

	assert.Nil(t, pin.Unexport())
	assert.Equal(t, "3", fs.Files[unexportPath].Contents)
}

@gen2thomas gen2thomas changed the title Update pwm_pin.go jetson(PWM): fix set period Oct 10, 2023
@gen2thomas gen2thomas self-assigned this Oct 10, 2023
@gen2thomas
Copy link
Collaborator

Hi @Jwen98 , because I plan to release in the next few days, I set this PR to draft and create a new one to fix this issue for the next release.

Thanks again for finding this issue and your work.
Thomas

@gen2thomas gen2thomas marked this pull request as draft October 28, 2023 11:47
@gen2thomas gen2thomas mentioned this pull request Oct 28, 2023
5 tasks
@gen2thomas
Copy link
Collaborator

done with #1019

@gen2thomas gen2thomas closed this Oct 28, 2023
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