-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpwm.py
More file actions
executable file
·38 lines (25 loc) · 1.11 KB
/
pwm.py
File metadata and controls
executable file
·38 lines (25 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os.path
from os import listdir
MAX_PWM_LEVEL = 255
def analog_write(pin, value):
"""Write to one of the pwm pins.
value can be an number between 0 and 255.
"""
path = '/sys/class/misc/pwmtimer/'
ending = 'pwm' + str(pin)
pins = listdir(os.path.join(path, 'enable'))
if not ending in pins:
raise ValueError("Pin not found, PWM only possible on " + " ".join(str(p) for p in pins) + ".")
with open(os.path.join(path, 'max_level',ending)) as f:
max_value = int(f.read())
if value < 0 or value > MAX_PWM_LEVEL:
raise ValueError("value must be between 0 and %s" % MAX_PWM_LEVEL)
map_level = ((max_value-1) * value) // MAX_PWM_LEVEL
#-1 because if it puts max_value the duty cycle somehow becomes 0 (overflow)
#disable -> change level -> enable , as requested by documentation
with open(os.path.join(path, 'enable', ending), 'w+') as f:
f.write("0\n")
with open(os.path.join(path, 'level', ending), 'w+') as f:
f.write("%d\n" % map_level)
with open(os.path.join(path, 'enable', ending), 'w+') as f:
f.write("1\n")