6
6
""" PWMOut Class for lgpio lg library tx_pwm library """
7
7
8
8
import lgpio
9
- import board # need board to get access to the CHIP object in the pin module
9
+ import board # need board to get access to the CHIP object in the pin module
10
+
10
11
11
12
# pylint: disable=unnecessary-pass
12
13
class PWMError (IOError ):
@@ -18,16 +19,16 @@ class PWMError(IOError):
18
19
# pylint: enable=unnecessary-pass
19
20
20
21
21
- class PWMOut : # pylint: disable=invalid-name
22
+ class PWMOut : # pylint: disable=invalid-name
22
23
"""Pulse Width Modulation Output Class"""
23
24
24
- def __init__ (self , pin , * , frequency = 500 , duty_cycle = 0 ,
25
- variable_frequency = False ):
25
+ def __init__ (self , pin , * , frequency = 500 , duty_cycle = 0 , variable_frequency = False ):
26
26
if variable_frequency :
27
27
print ("Variable Frequency is not supported, ignoring..." )
28
28
self ._pin = pin
29
- result = lgpio .gpio_claim_output (board .pin .CHIP , self ._pin .id ,
30
- lFlags = lgpio .SET_PULL_NONE )
29
+ result = lgpio .gpio_claim_output (
30
+ board .pin .CHIP , self ._pin .id , lFlags = lgpio .SET_PULL_NONE
31
+ )
31
32
if result < 0 :
32
33
raise RuntimeError (lgpio .error_text (result ))
33
34
self ._enabled = False
@@ -52,12 +53,11 @@ def deinit(self):
52
53
"""Deinit the PWM."""
53
54
if not self ._deinited :
54
55
if self .enabled :
55
- self ._enabled = False # turn off the pwm
56
+ self ._enabled = False # turn off the pwm
56
57
self ._deinited = True
57
58
58
-
59
59
def _is_deinited (self ):
60
- """ raise Value error if the object has been de-inited """
60
+ """raise Value error if the object has been de-inited"""
61
61
if self ._deinited :
62
62
raise ValueError (
63
63
"Object has been deinitialize and can no longer "
@@ -103,15 +103,14 @@ def duty_cycle(self, duty_cycle):
103
103
raise TypeError ("Invalid duty cycle type, should be int or float." )
104
104
105
105
if not 0 <= duty_cycle <= 65535 :
106
- raise ValueError (
107
- "Invalid duty cycle value, should be between 0 and 65535" )
106
+ raise ValueError ("Invalid duty cycle value, should be between 0 and 65535" )
108
107
109
108
# convert from 16-bit
110
109
duty_cycle /= 65535.0
111
110
112
111
self ._duty_cycle = duty_cycle
113
112
if self ._enabled :
114
- self .enabled = True # turn on with new values
113
+ self .enabled = True # turn on with new values
115
114
116
115
@property
117
116
def frequency (self ):
@@ -133,7 +132,7 @@ def frequency(self, frequency):
133
132
134
133
self ._frequency = frequency
135
134
if self .enabled :
136
- self .enabled = True # turn on with new values
135
+ self .enabled = True # turn on with new values
137
136
138
137
@property
139
138
def enabled (self ):
@@ -160,8 +159,9 @@ def enabled(self, value):
160
159
raise RuntimeError (lgpio .error_text (result ))
161
160
return result
162
161
163
-
164
162
# String representation
165
163
def __str__ (self ):
166
- return (f"pin { self ._pin } (freq={ self .frequency :f} Hz, duty_cycle="
167
- f"{ self .duty_cycle } ({ round (self .duty_cycle / 655.35 )} %)" )
164
+ return (
165
+ f"pin { self ._pin } (freq={ self .frequency :f} Hz, duty_cycle="
166
+ f"{ self .duty_cycle } ({ round (self .duty_cycle / 655.35 )} %)"
167
+ )
0 commit comments