Skip to content

Commit 5414ebc

Browse files
committed
cleanup
1 parent 3882483 commit 5414ebc

5 files changed

Lines changed: 43 additions & 46 deletions

File tree

‎pcduino/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
from gpio import *
2+
from adc import analog_read
3+
from pwm import analog_write

‎pcduino/adc.py‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
from exceptions import InvalidChannelException
2-
import os.path
1+
from pinmap import PinMap
32

4-
_ADC_PREFIX = '/proc'
5-
_N_PINS= 6
6-
_ADC_PINS = set(('adc%s' % i for i in xrange(_N_PINS)))
3+
pins = PinMap('/proc', 'adc', 6)
74

5+
def analog_read(channel):
6+
"""Return the integer value of an adc pin.
87
9-
def _get_path(id_):
10-
if id_ in _ADC_PINS:
11-
return os.path.join(_ADC_PREFIX, id_)
12-
raise InvalidChannelException
8+
adc0 and adc1 have 6 bit resolution.
9+
adc2 through adc5 have 12 bit resolution.
1310
14-
def analog_read(channel):
15-
with open(_get_path(channel), 'r') as f:
11+
"""
12+
with open(pins.get_path(channel), 'r') as f:
1613
return int(f.read(32).split(':')[1].strip())

‎pcduino/exceptions.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
class InvalidChannelException(ValueError):
2-
"""The channel sent was invalid."""
3-
pass

‎pcduino/gpio.py‎

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
1-
#!/usr/bin/env python
2-
#
3-
# io test code for pcDuino ( http://www.pcduino.com )
4-
#
5-
from exceptions import InvalidChannelException
1+
from pinmap import PinMap
2+
3+
64
__all__ = ['HIGH', 'LOW', 'INPUT', 'OUTPUT','digital_write', 'digital_read',
75
"pin_mode"]
86

9-
_GPIO_PINS = set(('gpio%s' % i for i in xrange(20)))
10-
11-
_PIN_FD_PATH = '/sys/devices/virtual/misc/gpio/pin/%s'
12-
_MODE_FD_PATH = '/sys/devices/virtual/misc/gpio/mode/%s'
137
HIGH = 1
148
LOW = 0
159
INPUT = 0
1610
OUTPUT = 1
1711

18-
def _get_valid_id(channel):
19-
if channel in _GPIO_PINS:
20-
return channel
21-
else:
22-
raise InvalidChannelException
12+
gpio_pins = PinMap(
13+
'/sys/devices/virtual/misc/gpio/pin',
14+
'gpio',
15+
20
16+
)
2317

2418
def digital_write(channel, value):
2519
"""Write to a GPIO channel"""
26-
id = _get_valid_id(channel)
27-
with open(_PIN_FD_PATH % id, 'w') as f:
20+
path = gpio_pins.get_path(channel)
21+
with open(path, 'w') as f:
2822
f.write('1' if value == HIGH else '0')
2923

3024
def digital_read(channel):
3125
"""Read from a GPIO channel"""
32-
id = _get_valid_id(channel)
33-
with open(_PIN_FD_PATH % id, 'r') as f:
26+
path = gpio_pins.get_path(channel)
27+
with open(path, 'r') as f:
3428
return f.read(1) == '1'
3529

3630
def pin_mode(channel, mode):
3731
""" Set Mode of a GPIO channel """
38-
id = _get_valid_id(channel)
39-
with open(_MODE_FD_PATH % id, 'w') as f:
32+
path = gpio_pins.get_path(channel, '/sys/devices/virtual/misc/gpio/mode/')
33+
with open(path, 'w') as f:
4034
f.write('0' if mode == INPUT else '1')

‎pcduino/pwm.py‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
MAX_PWM_NUM = 5
2-
PWM_IF_PREFIX = "/sys/class/leds/pwm"
3-
PWM_IF_MAX = "max_brightness"
4-
PWM_IF = "brightness"
1+
import os.path
2+
3+
from pinmap import PinMap
54

6-
MAX_PWM_LEVEL = 255
75

8-
def _max_value(pin):
9-
with open("%s%d/%s" % (PWM_IF_PREFIX, pin, PWM_IF_MAX)) as f:
10-
return int(f.read())
6+
pins = PinMap('/sys/class/leds', 'pwm', 5)
7+
8+
MAX_PWM_LEVEL = 255
119

1210
def analog_write(pin, value):
13-
if (pin < 0 or pin > MAX_PWM_NUM or
14-
value < 0 or value > MAX_PWM_LEVEL):
11+
"""Write to one of the pwm pins.
12+
13+
value can be an number between 0 and 255.
14+
15+
"""
16+
path = pins.get_path(pin)
17+
18+
with open(os.path.join(path, 'max_brightness')) as f:
19+
max_value = int(f.read())
20+
21+
if value < 0 or value > MAX_PWM_LEVEL:
1522
raise ValueError(
1623
"pin must be between 0 and %s. value must be between 0 and %s" % (
1724
MAX_PWM_NUM, MAX_PWM_LEVEL))
1825

19-
map_level = (_max_value(pin) * value) / MAX_PWM_LEVEL
20-
with open("%s%d/%s" % (PWM_IF_PREFIX, pin, PWM_IF), 'w+') as f:
26+
map_level = (max_value * value) / MAX_PWM_LEVEL
27+
with open(os.path.join(path, 'brightness'), 'w+') as f:
2128
f.write("%d\n" % map_level)

0 commit comments

Comments
 (0)