mirror of
https://github.com/xtacocorex/CHIP_IO
synced 2025-07-20 12:53:22 +00:00
checking in debug stuff for the hw pwm issue #17
This commit is contained in:
97
test/pwmtest.py
Executable file
97
test/pwmtest.py
Executable file
@ -0,0 +1,97 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import CHIP_IO.PWM as PWM
|
||||
import CHIP_IO.GPIO as GPIO
|
||||
import CHIP_IO.OverlayManager as OM
|
||||
import time
|
||||
import datetime
|
||||
import threading
|
||||
|
||||
class PWMReceiver(threading.Thread):
|
||||
def __init__(self,gpio,key,maxcount=20,sleeptime=0.5):
|
||||
self.gpio = gpio
|
||||
self.key = key
|
||||
self.counter = 0
|
||||
self.maxcount = maxcount
|
||||
self.sleeptime = sleeptime
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
def run(self):
|
||||
print("SETTING UP RECEIVER GPIO")
|
||||
self.gpio.cleanup()
|
||||
self.gpio.setup(self.key, self.gpio.IN)
|
||||
print("STARTING RECEIVE LOOP")
|
||||
try:
|
||||
while self.counter < self.maxcount:
|
||||
pwmval = self.gpio.input(self.key)
|
||||
print("PWM VALUE: {0} @ {1}".format(pwmval, datetime.datetime.now()))
|
||||
time.sleep(self.sleeptime)
|
||||
self.counter += 1
|
||||
except KeyboardInterrupt:
|
||||
self.gpio.cleanup(self.key)
|
||||
|
||||
def PrintPwmData():
|
||||
print("PRINTING PWM SYSFS DATA")
|
||||
f = open("/sys/class/pwm/pwmchip0/pwm0/enable")
|
||||
print("PWM0 ENABLE:\t{}".format(f.readline()))
|
||||
f.close()
|
||||
f = open("/sys/class/pwm/pwmchip0/pwm0/period")
|
||||
print("PWM0 PERIOD:\t{}".format(f.readline()))
|
||||
f.close()
|
||||
f = open("/sys/class/pwm/pwmchip0/pwm0/duty_cycle")
|
||||
print("PWM0 DUTY CYCLE:\t{}".format(f.readline()))
|
||||
f.close()
|
||||
f = open("/sys/class/pwm/pwmchip0/pwm0/polarity")
|
||||
print("PWM0 POLARITY:\t{}".format(f.readline()))
|
||||
f.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# SETUP VARIABLES
|
||||
PWMGPIO = "PWM0"
|
||||
RECEIVERGPIO = "CSID0"
|
||||
COUNT = 50
|
||||
SLEEPTIME = 0.01
|
||||
|
||||
# LOAD THE PWM OVERLAY
|
||||
print("LOADING PWM OVERLAY")
|
||||
OM.load("PWM0")
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
# CLEANUP THE GPIO
|
||||
GPIO.cleanup()
|
||||
PWM.cleanup()
|
||||
|
||||
# SETUP PWM
|
||||
try:
|
||||
print("PWM START")
|
||||
PWM.start(PWMGPIO, 35, 20, 0)
|
||||
PrintPwmData()
|
||||
|
||||
# UNCOMMENT FOR CRASH
|
||||
#print("PWM SET FREQUENCY")
|
||||
#PWM.set_frequency(PWMGPIO, 200)
|
||||
#PrintPwmData()
|
||||
|
||||
# UNCOMMENT FOR CRASH
|
||||
#print("PWM SET DUTY CYCLE")
|
||||
#PWM.set_duty_cycle(PWMGPIO, 25)
|
||||
#PrintPwmData()
|
||||
|
||||
# SETUP PWM RECEIVER
|
||||
rcvr = PWMReceiver(GPIO, RECEIVERGPIO, COUNT, SLEEPTIME)
|
||||
rcvr.start()
|
||||
|
||||
time.sleep(COUNT*SLEEPTIME + 1)
|
||||
|
||||
except:
|
||||
raise
|
||||
finally:
|
||||
# CLEANUP
|
||||
print("CLEANUP")
|
||||
PWM.stop(PWMGPIO)
|
||||
PWM.cleanup()
|
||||
#OM.unload("PWM0")
|
||||
GPIO.cleanup()
|
||||
|
||||
|
@ -2,12 +2,15 @@ import pytest
|
||||
import os
|
||||
|
||||
import CHIP_IO.PWM as PWM
|
||||
import CHIP_IO.OverlayManager as OM
|
||||
|
||||
def teardown_module(module):
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
class TestPwmSetup:
|
||||
def test_start_pwm(self):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0)
|
||||
|
||||
pwm_test = '/sys/class/pwm/pwmchip0/pwm0'
|
||||
@ -18,8 +21,10 @@ class TestPwmSetup:
|
||||
assert int(duty) == 0
|
||||
assert int(period) == 500000
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_start_pwm_with_polarity_one(self):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0, 2000, 1)
|
||||
|
||||
pwm_test = '/sys/class/pwm/pwmchip0/pwm0'
|
||||
@ -32,8 +37,10 @@ class TestPwmSetup:
|
||||
assert int(period) == 500000
|
||||
assert str(polarity) == "inverted"
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_start_pwm_with_polarity_default(self):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0, 2000, 0)
|
||||
|
||||
pwm_test = '/sys/class/pwm/pwmchip0/pwm0'
|
||||
@ -46,8 +53,10 @@ class TestPwmSetup:
|
||||
assert int(period) == 500000
|
||||
assert str(polarity) == "normal"
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_start_pwm_with_polarity_zero(self):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0, 2000, 0)
|
||||
|
||||
pwm_test = '/sys/class/pwm/pwmchip0/pwm0'
|
||||
@ -60,54 +69,78 @@ class TestPwmSetup:
|
||||
assert int(period) == 500000
|
||||
assert str(polarity) == "normal"
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_start_invalid_pwm_key(self):
|
||||
with pytest.raises(ValueError):
|
||||
OM.load("PWM0")
|
||||
PWM.start("P8_25", -1)
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_start_invalid_duty_cycle_negative(self):
|
||||
with pytest.raises(ValueError):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", -1)
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_start_valid_duty_cycle_min(self):
|
||||
#testing an exception isn't thrown
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0)
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_start_valid_duty_cycle_max(self):
|
||||
#testing an exception isn't thrown
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 100)
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_start_invalid_duty_cycle_high(self):
|
||||
with pytest.raises(ValueError):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 101)
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_start_invalid_duty_cycle_string(self):
|
||||
with pytest.raises(TypeError):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", "1")
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_start_invalid_frequency_negative(self):
|
||||
with pytest.raises(ValueError):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0, -1)
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_start_invalid_frequency_string(self):
|
||||
with pytest.raises(TypeError):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0, "1")
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_start_negative_polarity(self):
|
||||
with pytest.raises(ValueError):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0, 100, -1)
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_start_invalid_positive_polarity(self):
|
||||
with pytest.raises(ValueError):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0, 100, 2)
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_start_invalid_polarity_type(self):
|
||||
with pytest.raises(TypeError):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0, 100, "1")
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_duty_modified(self):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0)
|
||||
|
||||
pwm_test = '/sys/class/pwm/pwmchip0/pwm0'
|
||||
@ -124,56 +157,75 @@ class TestPwmSetup:
|
||||
assert int(duty) == 500000
|
||||
assert int(period) == 500000
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_duty_cycle_non_setup_key(self):
|
||||
with pytest.raises(RuntimeError):
|
||||
OM.load("PWM0")
|
||||
PWM.set_duty_cycle("PWM0", 100)
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_duty_cycle_invalid_key(self):
|
||||
with pytest.raises(ValueError):
|
||||
OM.load("PWM0")
|
||||
PWM.set_duty_cycle("P9_15", 100)
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_duty_cycle_invalid_value_high(self):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0)
|
||||
with pytest.raises(ValueError):
|
||||
PWM.set_duty_cycle("PWM0", 101)
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_duty_cycle_invalid_value_negative(self):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0)
|
||||
with pytest.raises(ValueError):
|
||||
PWM.set_duty_cycle("PWM0", -1)
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_duty_cycle_invalid_value_string(self):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0)
|
||||
with pytest.raises(TypeError):
|
||||
PWM.set_duty_cycle("PWM0", "a")
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_frequency_invalid_value_negative(self):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0)
|
||||
with pytest.raises(ValueError):
|
||||
PWM.set_frequency("PWM0", -1)
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_frequency_invalid_value_string(self):
|
||||
OM.load("PWM0")
|
||||
PWM.start("PWM0", 0)
|
||||
with pytest.raises(TypeError):
|
||||
PWM.set_frequency("PWM0", "11")
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_freq_non_setup_key(self):
|
||||
with pytest.raises(RuntimeError):
|
||||
OM.load("PWM0")
|
||||
PWM.set_frequency("PWM0", 100)
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_pwm_freq_non_setup_key(self):
|
||||
with pytest.raises(ValueError):
|
||||
OM.load("PWM0")
|
||||
PWM.set_frequency("P9_15", 100)
|
||||
PWM.cleanup()
|
||||
OM.unload("PWM0")
|
||||
|
||||
def test_stop_pwm(self):
|
||||
pass
|
||||
|
Reference in New Issue
Block a user