Skip to content

Commit 0eedc1d

Browse files
committed
error checking
1 parent 51f8e83 commit 0eedc1d

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

‎pcduino/pwm.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1+
MAX_PWM_NUM = 5
12
PWM_IF_PREFIX = "/sys/class/leds/pwm"
23
PWM_IF_MAX = "max_brightness"
34
PWM_IF = "brightness"
45

56
MAX_PWM_LEVEL = 255
67

7-
#sprintf(path, "%s%d/%s", PWM_IF_PREFIX, pin, PWM_IF_MAX);
8-
98
def _max_value(pin):
10-
return 256
9+
with open("%s%d/%s" % (PWM_IF_PREFIX, pin, PWM_IF_MAX)) as f:
10+
return int(f.read())
1111

1212
def analog_write(pin, value):
13+
if (pin < 0 or pin > MAX_PWM_NUM or
14+
value < 0 or value > MAX_PWM_LEVEL):
15+
raise ValueError(
16+
"pin must be between 0 and %s. value must be between 0 and %s" % (
17+
MAX_PWM_NUM, MAX_PWM_LEVEL))
18+
1319
map_level = (_max_value(pin) * value) / MAX_PWM_LEVEL
1420
with open("%s%d/%s" % (PWM_IF_PREFIX, pin, PWM_IF), 'w+') as f:
1521
f.write("%d\n" % map_level)

0 commit comments

Comments
 (0)