From 1b7beac587b219f8f4876d9488ac5aed22c00643 Mon Sep 17 00:00:00 2001 From: Robert Wolterman Date: Fri, 26 Feb 2016 08:48:07 -0600 Subject: [PATCH] Updating the readme for PWM instructions and added a test case for PRM which hasn't been tested --- README.rst | 40 +++++---- test/test_pwm_setup.py | 185 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 210 insertions(+), 15 deletions(-) create mode 100644 test/test_pwm_setup.py diff --git a/README.rst b/README.rst index 6f572fa..5b0c377 100644 --- a/README.rst +++ b/README.rst @@ -11,12 +11,12 @@ Manual:: sudo python setup.py install cd .. sudo rm -rf CHIP_IO - + **Usage** Using the library is very similar to the excellent RPi.GPIO library used on the Raspberry Pi. Below are some examples. -**GPIO Setup** +**GPIO Setup** Import the library, and setup as GPIO.OUT or GPIO.IN:: @@ -27,12 +27,12 @@ You can also refer to the pin number:: GPIO.setup("U14_31", GPIO.OUT) -**GPIO Output** +**GPIO Output** Setup the pin for output, and write GPIO.HIGH or GPIO.LOW. Or you can use 1 or 0.:: import CHIP_IO.GPIO as GPIO - GPIO.setup("CSID0", GPIO.OUT) + GPIO.setup("CSID0", GPIO.OUT) GPIO.output("CSID0", GPIO.HIGH) **GPIO Input** @@ -41,9 +41,9 @@ Inputs work similarly to outputs.:: import CHIP_IO.GPIO as GPIO GPIO.setup("CSID0", GPIO.IN) - + Polling inputs:: - + if GPIO.input("CSID0"): print("HIGH") else: @@ -55,15 +55,26 @@ Waiting for an edge (GPIO.RISING, GPIO.FALLING, or GPIO.BOTH:: Detecting events:: - GPIO.add_event_detect("CSID0", GPIO.FALLING) - #your amazing code here - #detect wherever: + GPIO.add_event_detect("CSID0", GPIO.FALLING) + #your amazing code here + #detect wherever: if GPIO.event_detected("CSID0"): print "event detected!" **PWM**:: - Not implemented yet + import CHIP_IO.PWM as PWM + #PWM.start(channel, duty, freq=2000, polarity=0) + #duty values are valid 0 (off) to 100 (on) + PWM.start("PWM0", 50) + PWM.set_duty_cycle("PWM0", 25.5) + PWM.set_frequency("PWM0", 10) + + PWM.stop("PWM0") + PWM.cleanup() + + #set polarity to 1 on start: + PWM.start("PWM0", 50, 2000, 1) **ADC**:: @@ -73,15 +84,15 @@ Detecting events:: Install py.test to run the tests. You'll also need the python compiler package for py.test.:: - opkg update && opkg install python-compiler - #Either pip or easy_install - pip install -U pytest + opkg update && opkg install python-compiler + #Either pip or easy_install + pip install -U pytest easy_install -U pytest Execute the following in the root of the project:: py.test - + **Credits** The CHIP IO Python library was originally forked from the Adafruit Beaglebone IO Python Library. @@ -91,4 +102,3 @@ The BeagleBone IO Python library was originally forked from the excellent MIT Li CHIP IO port by Robert Wolterman, released under the MIT License. Beaglebone IO Library Written by Justin Cooper, Adafruit Industries. BeagleBone IO Python library is released under the MIT License. - diff --git a/test/test_pwm_setup.py b/test/test_pwm_setup.py new file mode 100644 index 0000000..065f6bc --- /dev/null +++ b/test/test_pwm_setup.py @@ -0,0 +1,185 @@ +import pytest +import os + +import CHIP_IO.PWM as PWM + +def teardown_module(module): + PWM.cleanup() + +class TestPwmSetup: + def test_start_pwm(self): + PWM.start("PWM0", 0) + + pwm_test = '/sys/class/pwm/pwmchip0/pwm0' + + assert os.path.exists(pwm_test) + duty = open(pwm_test + '/duty').read() + period = open(pwm_test + '/period').read() + assert int(duty) == 0 + assert int(period) == 500000 + PWM.cleanup() + + def test_start_pwm_with_polarity_one(self): + PWM.start("PWM0", 0, 2000, 1) + + pwm_test = '/sys/class/pwm/pwmchip0/pwm0' + + assert os.path.exists(pwm_test) + duty = open(pwm_test + '/duty').read() + period = open(pwm_test + '/period').read() + polarity = open(pwm_test + '/polarity').read() + assert int(duty) == 0 + assert int(period) == 500000 + assert int(polarity) == 1 + PWM.cleanup() + + def test_start_pwm_with_polarity_default(self): + PWM.start("PWM0", 0, 2000, 0) + + pwm_test = '/sys/class/pwm/pwmchip0/pwm0' + + assert os.path.exists(pwm_test) + duty = open(pwm_test + '/duty').read() + period = open(pwm_test + '/period').read() + polarity = open(pwm_test + '/polarity').read() + assert int(duty) == 0 + assert int(period) == 500000 + assert int(polarity) == 0 + PWM.cleanup() + + def test_start_pwm_with_polarity_zero(self): + PWM.start("PWM0", 0, 2000, 0) + + pwm_test = '/sys/class/pwm/pwmchip0/pwm0' + + assert os.path.exists(pwm_test) + duty = open(pwm_test + '/duty').read() + period = open(pwm_test + '/period').read() + polarity = open(pwm_test + '/polarity').read() + assert int(duty) == 0 + assert int(period) == 500000 + assert int(polarity) == 0 + PWM.cleanup() + + def test_pwm_start_invalid_pwm_key(self): + with pytest.raises(ValueError): + PWM.start("P8_25", -1) + + def test_pwm_start_invalid_duty_cycle_negative(self): + with pytest.raises(ValueError): + PWM.start("PWM0", -1) + + def test_pwm_start_valid_duty_cycle_min(self): + #testing an exception isn't thrown + PWM.start("PWM0", 0) + PWM.cleanup() + + def test_pwm_start_valid_duty_cycle_max(self): + #testing an exception isn't thrown + PWM.start("PWM0", 100) + PWM.cleanup() + + def test_pwm_start_invalid_duty_cycle_high(self): + with pytest.raises(ValueError): + PWM.start("PWM0", 101) + + def test_pwm_start_invalid_duty_cycle_string(self): + with pytest.raises(TypeError): + PWM.start("PWM0", "1") + + def test_pwm_start_invalid_frequency_negative(self): + with pytest.raises(ValueError): + PWM.start("PWM0", 0, -1) + + def test_pwm_start_invalid_frequency_string(self): + with pytest.raises(TypeError): + PWM.start("PWM0", 0, "1") + + def test_pwm_start_negative_polarity(self): + with pytest.raises(ValueError): + PWM.start("PWM0", 0, 100, -1) + + def test_pwm_start_invalid_positive_polarity(self): + with pytest.raises(ValueError): + PWM.start("PWM0", 0, 100, 2) + + def test_pwm_start_invalid_polarity_type(self): + with pytest.raises(TypeError): + PWM.start("PWM0", 0, 100, "1") + + def test_pwm_duty_modified(self): + PWM.start("PWM0", 0) + + pwm_test = '/sys/class/pwm/pwmchip0/pwm0' + + assert os.path.exists(pwm_test) + duty = open(pwm_test + '/duty').read() + period = open(pwm_test + '/period').read() + assert int(duty) == 0 + assert int(period) == 500000 + + PWM.set_duty_cycle("PWM0", 100) + duty = open(pwm_test + '/duty').read() + period = open(pwm_test + '/period').read() + assert int(duty) == 500000 + assert int(period) == 500000 + PWM.cleanup() + + def test_pwm_duty_cycle_non_setup_key(self): + with pytest.raises(RuntimeError): + PWM.set_duty_cycle("PWM0", 100) + PWM.cleanup() + + def test_pwm_duty_cycle_invalid_key(self): + with pytest.raises(ValueError): + PWM.set_duty_cycle("P9_15", 100) + PWM.cleanup() + + def test_pwm_duty_cycle_invalid_value_high(self): + PWM.start("PWM0", 0) + with pytest.raises(ValueError): + PWM.set_duty_cycle("PWM0", 101) + PWM.cleanup() + + def test_pwm_duty_cycle_invalid_value_negative(self): + PWM.start("PWM0", 0) + with pytest.raises(ValueError): + PWM.set_duty_cycle("PWM0", -1) + PWM.cleanup() + + def test_pwm_duty_cycle_invalid_value_string(self): + PWM.start("PWM0", 0) + with pytest.raises(TypeError): + PWM.set_duty_cycle("PWM0", "a") + PWM.cleanup() + + def test_pwm_frequency_invalid_value_negative(self): + PWM.start("PWM0", 0) + with pytest.raises(ValueError): + PWM.set_frequency("PWM0", -1) + PWM.cleanup() + + def test_pwm_frequency_invalid_value_string(self): + PWM.start("PWM0", 0) + with pytest.raises(TypeError): + PWM.set_frequency("PWM0", "11") + PWM.cleanup() + + def test_pwm_freq_non_setup_key(self): + with pytest.raises(RuntimeError): + PWM.set_frequency("PWM0", 100) + PWM.cleanup() + + def test_pwm_freq_non_setup_key(self): + with pytest.raises(ValueError): + PWM.set_frequency("P9_15", 100) + PWM.cleanup() + + def test_stop_pwm(self): + pass + #PWM.start("PWM0", 1) + #PWM.stop("PWM0") + #assert os.path.exists('/sys/class/gpio/gpio68') + #direction = open('/sys/class/gpio/gpio68/direction').read() + #assert direction == 'out\n' + #PWM.cleanup()