Skip to content

Commit ebe031a

Browse files
committed
WIP analogRead
1 parent 6499153 commit ebe031a

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎pcduino/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from gpio import *

‎pcduino/gpio.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
#
3+
# io test code for pcDuino ( http://www.pcduino.com )
4+
#
5+
__all__ = ['HIGH', 'LOW', 'INPUT', 'OUTPUT','digitalWrite', 'digitalRead', "pinMode"]
6+
7+
_GPIO_PINS = ('gpio0','gpio1','gpio2','gpio3','gpio4','gpio5','gpio6','gpio7',
8+
'gpio8', 'gpio9', 'gpio10', 'gpio11', 'gpio12', 'gpio13',
9+
'gpio14', 'gpio15', 'gpio16', 'gpio17', 'gpio18', 'gpio19')
10+
11+
_PIN_FD_PATH = '/sys/devices/virtual/misc/gpio/pin/%s'
12+
_MODE_FD_PATH = '/sys/devices/virtual/misc/gpio/mode/%s'
13+
HIGH = 1
14+
LOW = 0
15+
INPUT = 0
16+
OUTPUT = 1
17+
18+
class InvalidChannelException(Exception):
19+
"""The channel sent is invalid on pcDuino board """
20+
pass
21+
22+
def _GetValidId(channel):
23+
if channel in _GPIO_PINS:
24+
return channel
25+
else:
26+
raise InvalidChannelException
27+
28+
def digitalWrite(channel, value):
29+
"""Write to a GPIO channel"""
30+
id = _GetValidId(channel)
31+
with open(_PIN_FD_PATH % id, 'w') as f:
32+
f.write('1' if value == HIGH else '0')
33+
34+
def digitalRead(channel):
35+
"""Read from a GPIO channel"""
36+
id = _GetValidId(channel)
37+
with open(_PIN_FD_PATH % id, 'r') as f:
38+
return f.read(1) == '1'
39+
40+
def pinMode(channel, mode):
41+
""" Set Mode of a GPIO channel """
42+
id = _GetValidId(channel)
43+
with open(_MODE_FD_PATH % id, 'w') as f:
44+
f.write('0' if mode == INPUT else '1')

‎pcduino/pwm.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
_ADC_PREFIX = '/proc/adc'
2+
_N_PINS= 6
3+
4+
5+
def analogRead(channel):
6+
with open(_ADC_PREFIX % id, 'r') as f:
7+
return f.read(1)

0 commit comments

Comments
 (0)