|
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 | + |
6 | 4 | __all__ = ['HIGH', 'LOW', 'INPUT', 'OUTPUT','digital_write', 'digital_read', |
7 | 5 | "pin_mode"] |
8 | 6 |
|
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' |
13 | 7 | HIGH = 1 |
14 | 8 | LOW = 0 |
15 | 9 | INPUT = 0 |
16 | 10 | OUTPUT = 1 |
17 | 11 |
|
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 | +) |
23 | 17 |
|
24 | 18 | def digital_write(channel, value): |
25 | 19 | """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: |
28 | 22 | f.write('1' if value == HIGH else '0') |
29 | 23 |
|
30 | 24 | def digital_read(channel): |
31 | 25 | """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: |
34 | 28 | return f.read(1) == '1' |
35 | 29 |
|
36 | 30 | def pin_mode(channel, mode): |
37 | 31 | """ 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: |
40 | 34 | f.write('0' if mode == INPUT else '1') |
0 commit comments