Skip to content

Commit 7ac2cf6

Browse files
committed
machine: Add Pin class implementation for Linux.
Tested only for output so far.
1 parent 6ec58b6 commit 7ac2cf6

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

machine/machine/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .timer import *
2+
from .pin import *
23

34
def unique_id():
45
return b"upy-non-unique"

machine/machine/pin.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import umachine
2+
3+
class Pin(umachine.PinBase):
4+
5+
IN = "in"
6+
OUT = "out"
7+
8+
def __init__(self, no, dir=IN):
9+
pref = "/sys/class/gpio/gpio{}/".format(no)
10+
dirf = pref + "direction"
11+
try:
12+
f = open(dirf, "w")
13+
except OSError:
14+
with open("/sys/class/gpio/export", "w") as f:
15+
f.write(str(no))
16+
f = open(dirf, "w")
17+
f.write(dir)
18+
self.f = open(pref + "value", "rw")
19+
20+
def value(self, v=None):
21+
if v is None:
22+
return self.f.read(1) == "1"
23+
self.f.write(str(v))
24+
25+
def deinit(self):
26+
self.f.close()

0 commit comments

Comments
 (0)