Yet Another WebIOPi+
Main Page
Related Pages
Packages
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Macros
Pages
python
webiopi
devices
digital
__init__.py
Go to the documentation of this file.
1
# Copyright 2012-2013 Eric Ptak - trouch.com
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
# http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
15
from
webiopi.decorators.rest
import
request, response
16
from
webiopi.utils.types
import
M_JSON
17
18
class
GPIOPort
():
19
IN = 0
20
OUT = 1
21
22
LOW =
False
23
HIGH =
True
24
25
def
__init__
(self, channelCount, bankCount=0):
26
self.
digitalChannelCount
= channelCount
27
self.
checkDigitalBanks
(bankCount)
28
self.
digitalBanksCount
= bankCount
29
30
def
checkDigitalChannel
(self, channel):
31
if
not
0 <= channel < self.
digitalChannelCount
:
32
raise
ValueError(
"Channel %d out of range [%d..%d]"
% (channel, 0, self.
digitalChannelCount
-1))
33
34
def
checkDigitalValue
(self, value):
35
if
not
(value == 0
or
value == 1):
36
raise
ValueError(
"Value %d not in {0, 1}"
)
37
38
def
checkDigitalBanks
(self, banks):
39
if
not
0 <= banks < self.
digitalChannelCount
:
40
raise
ValueError(
"Banks %d out of range [%d..%d]"
% (banks, 0, self.
digitalChannelCount
))
41
42
@
request
(
"GET"
,
"count"
)
43
@
response
(
"%d"
)
44
def
digitalCount
(self):
45
return
self.
digitalChannelCount
46
47
@
request
(
"GET"
,
"banks"
)
48
@
response
(
"%d"
)
49
def
bankCount
(self):
50
return
self.
digitalBanksCount
51
52
def
__family__
(self):
53
return
"GPIOPort"
54
55
def
__getFunction__
(self, channel):
56
raise
NotImplementedError
57
58
def
__setFunction__
(self, channel, func):
59
raise
NotImplementedError
60
61
def
__digitalRead__
(self, chanel):
62
raise
NotImplementedError
63
64
def
__portRead__
(self):
65
raise
NotImplementedError
66
67
def
__digitalWrite__
(self, chanel, value):
68
raise
NotImplementedError
69
70
def
__portWrite__
(self, value):
71
raise
NotImplementedError
72
73
def
getFunction
(self, channel):
74
self.
checkDigitalChannel
(channel)
75
return
self.
__getFunction__
(channel)
76
77
@
request
(
"GET"
,
"%(channel)d/function"
)
78
def
getFunctionString
(self, channel):
79
func = self.
getFunction
(channel)
80
if
func == self.
IN
:
81
return
"IN"
82
elif
func == self.
OUT
:
83
return
"OUT"
84
# elif func == GPIO.PWM:
85
# return "PWM"
86
else
:
87
return
"UNKNOWN"
88
89
def
setFunction
(self, channel, value):
90
self.
checkDigitalChannel
(channel)
91
self.
__setFunction__
(channel, value)
92
return
self.
getFunction
(channel)
93
94
@
request
(
"POST"
,
"%(channel)d/function/%(value)s"
)
95
def
setFunctionString
(self, channel, value):
96
value = value.lower()
97
if
value ==
"in"
:
98
self.
setFunction
(channel, self.
IN
)
99
elif
value ==
"out"
:
100
self.
setFunction
(channel, self.
OUT
)
101
# elif value == "pwm":
102
# self.setFunction(channel, GPIO.PWM)
103
else
:
104
raise
ValueError(
"Bad Function"
)
105
return
self.
getFunctionString
(channel)
106
107
@
request
(
"GET"
,
"%(channel)d/value"
)
108
@
response
(
"%d"
)
109
def
digitalRead
(self, channel):
110
self.
checkDigitalChannel
(channel)
111
return
self.
__digitalRead__
(channel)
112
113
@
request
(
"GET"
,
"*"
)
114
@
response
(contentType=M_JSON)
115
def
wildcard
(self, compact=False):
116
if
compact:
117
f =
"f"
118
v =
"v"
119
else
:
120
f =
"function"
121
v =
"value"
122
123
values = {}
124
for
i
in
range(self.
digitalChannelCount
):
125
if
compact:
126
func = self.
getFunction
(i)
127
else
:
128
func = self.
getFunctionString
(i)
129
values[i] = {f: func, v: int(self.
digitalRead
(i))}
130
return
values
131
132
@
request
(
"GET"
,
"*/integer"
)
133
@
response
(
"%d"
)
134
def
portRead
(self):
135
return
self.
__portRead__
()
136
137
@
request
(
"POST"
,
"%(channel)d/value/%(value)d"
)
138
@
response
(
"%d"
)
139
def
digitalWrite
(self, channel, value):
140
self.
checkDigitalChannel
(channel)
141
self.
checkDigitalValue
(value)
142
self.
__digitalWrite__
(channel, value)
143
return
self.
digitalRead
(channel)
144
145
@
request
(
"POST"
,
"*/integer/%(value)d"
)
146
@
response
(
"%d"
)
147
def
portWrite
(self, value):
148
self.
__portWrite__
(value)
149
return
self.
portRead
()
150
151
DRIVERS = {}
152
DRIVERS[
"mcp23XXX"
] = [
"MCP23008"
,
"MCP23009"
,
"MCP23017"
,
"MCP23018"
,
"MCP23S08"
,
"MCP23S09"
,
"MCP23S17"
,
"MCP23S18"
]
153
DRIVERS[
"pcf8574"
] = [
"PCF8574"
,
"PCF8574A"
]
154
DRIVERS[
"ds2408"
] = [
"DS2408"
]
155
DRIVERS[
"pca9555"
] = [
"PCA9555"
,
"PCA9535"
]
webiopi.utils.types
Definition:
types.py:1
webiopi.devices.digital.GPIOPort.portWrite
def portWrite
Definition:
__init__.py:147
webiopi.devices.digital.GPIOPort.checkDigitalChannel
def checkDigitalChannel
Definition:
__init__.py:30
webiopi.devices.digital.GPIOPort.getFunctionString
def getFunctionString
Definition:
__init__.py:78
webiopi.devices.digital.GPIOPort.setFunctionString
def setFunctionString
Definition:
__init__.py:95
webiopi.devices.digital.GPIOPort.digitalChannelCount
digitalChannelCount
Definition:
__init__.py:26
coap-client.response
tuple response
Definition:
coap-client.py:9
webiopi.devices.digital.GPIOPort.digitalWrite
def digitalWrite
Definition:
__init__.py:139
webiopi.devices.digital.GPIOPort.IN
int IN
Definition:
__init__.py:19
webiopi.devices.digital.GPIOPort.portRead
def portRead
Definition:
__init__.py:134
webiopi.devices.digital.GPIOPort.digitalRead
def digitalRead
Definition:
__init__.py:109
webiopi.devices.digital.GPIOPort.getFunction
def getFunction
Definition:
__init__.py:73
webiopi.devices.digital.GPIOPort.OUT
int OUT
Definition:
__init__.py:20
webiopi.devices.digital.GPIOPort.__getFunction__
def __getFunction__
Definition:
__init__.py:55
webiopi.devices.digital.GPIOPort.digitalCount
def digitalCount
Definition:
__init__.py:44
webiopi.devices.digital.GPIOPort.setFunction
def setFunction
Definition:
__init__.py:89
webiopi.devices.digital.GPIOPort.bankCount
def bankCount
Definition:
__init__.py:49
webiopi.devices.digital.GPIOPort.wildcard
def wildcard
Definition:
__init__.py:115
webiopi.decorators.rest
Definition:
rest.py:1
webiopi.devices.digital.GPIOPort.digitalBanksCount
digitalBanksCount
Definition:
__init__.py:28
webiopi.devices.digital.GPIOPort.__digitalWrite__
def __digitalWrite__
Definition:
__init__.py:67
webiopi.decorators.rest.request
def request
Definition:
rest.py:1
webiopi.devices.digital.GPIOPort.checkDigitalBanks
def checkDigitalBanks
Definition:
__init__.py:38
webiopi.devices.digital.GPIOPort.checkDigitalValue
def checkDigitalValue
Definition:
__init__.py:34
webiopi.devices.digital.GPIOPort
Definition:
__init__.py:18
webiopi.devices.digital.GPIOPort.__setFunction__
def __setFunction__
Definition:
__init__.py:58
webiopi.devices.digital.GPIOPort.__portWrite__
def __portWrite__
Definition:
__init__.py:70
webiopi.devices.digital.GPIOPort.__family__
def __family__
Definition:
__init__.py:52
webiopi.devices.digital.GPIOPort.__init__
def __init__
Definition:
__init__.py:25
webiopi.devices.digital.GPIOPort.__digitalRead__
def __digitalRead__
Definition:
__init__.py:61
webiopi.devices.digital.GPIOPort.__portRead__
def __portRead__
Definition:
__init__.py:64
Generated on Sat Sep 10 2016 09:36:44 for Yet Another WebIOPi+ by
1.8.8