15
15
from myDevices .decorators .rest import request , response
16
16
from myDevices .utils .types import toint , M_JSON
17
17
from myDevices .devices import instance
18
+ from myDevices .utils .logger import info
19
+
18
20
19
21
class ADC ():
20
22
def __init__ (self , channelCount , resolution , vref ):
@@ -34,79 +36,72 @@ def checkAnalogValue(self, value):
34
36
if not 0 <= value <= self ._analogMax :
35
37
raise ValueError ("Value %d out of range [%d..%d]" % (value , 0 , self ._analogMax ))
36
38
37
- #@request("GET", "analog/count")
38
39
@response ("%d" )
39
40
def analogCount (self ):
40
41
return self ._analogCount
41
42
42
- #@request("GET", "analog/resolution")
43
43
@response ("%d" )
44
44
def analogResolution (self ):
45
45
return self ._analogResolution
46
46
47
- #@request("GET", "analog/max")
48
47
@response ("%d" )
49
48
def analogMaximum (self ):
50
49
return int (self ._analogMax )
51
50
52
- #@request("GET", "analog/vref")
53
51
@response ("%.2f" )
54
52
def analogReference (self ):
55
53
return self ._analogRef
56
54
57
55
def __analogRead__ (self , channel , diff ):
58
56
raise NotImplementedError
59
57
60
- #@request("GET", "analog/%(channel)d/integer")
61
58
@response ("%d" )
62
59
def analogRead (self , channel , diff = False ):
63
60
self .checkAnalogChannel (channel )
64
61
return self .__analogRead__ (channel , diff )
65
62
66
- #@request("GET", "analog/%(channel)d/float")
67
63
@response ("%.2f" )
68
64
def analogReadFloat (self , channel , diff = False ):
69
65
return self .analogRead (channel , diff ) / float (self ._analogMax )
70
66
71
- #@request("GET", "analog/%(channel)d/volt")
72
67
@response ("%.2f" )
73
68
def analogReadVolt (self , channel , diff = False ):
74
69
if self ._analogRef == 0 :
75
70
raise NotImplementedError
76
71
return self .analogReadFloat (channel , diff ) * self ._analogRef
77
72
78
- #@request("GET", "analog/*/integer")
79
73
@response (contentType = M_JSON )
80
74
def analogReadAll (self ):
81
75
values = {}
82
76
for i in range (self ._analogCount ):
83
77
values [i ] = self .analogRead (i )
84
78
return values
85
79
86
- #@request("GET", "analog/*/float")
87
80
@response (contentType = M_JSON )
88
81
def analogReadAllFloat (self ):
89
82
values = {}
90
83
for i in range (self ._analogCount ):
91
84
values [i ] = float ("%.2f" % self .analogReadFloat (i ))
92
85
return values
93
86
94
- #@request("GET", "analog/*/volt")
95
87
@response (contentType = M_JSON )
96
88
def analogReadAllVolt (self ):
97
89
values = {}
98
90
for i in range (self ._analogCount ):
99
91
values [i ] = float ("%.2f" % self .analogReadVolt (i ))
100
92
return values
101
93
102
- def read (self , channel , diff = False ):
103
- return self .analogRead (channel , diff )
94
+ def read (self , channel , value_type = None , diff = False ):
95
+ read_functions = {'float' : self .analogReadFloat , 'volt' : self .analogReadVolt }
96
+ read_function = read_functions .get (value_type , self .analogRead )
97
+ return read_function (channel , diff )
104
98
105
99
def readFloat (self , channel , diff = False ):
106
- return self .analogReadFloat (channel , diff )
100
+ return self .analogReadFloat (channel , diff )
107
101
108
102
def readVolt (self , channel , diff = False ):
109
- return self .analogReadVolt (channel , diff )
103
+ return self .analogReadVolt (channel , diff )
104
+
110
105
111
106
class DAC (ADC ):
112
107
def __init__ (self , channelCount , resolution , vref ):
@@ -118,35 +113,35 @@ def __family__(self):
118
113
def __analogWrite__ (self , channel , value ):
119
114
raise NotImplementedError
120
115
121
- #@request("POST", "analog/%(channel)d/integer/%(value)d")
122
116
@response ("%d" )
123
117
def analogWrite (self , channel , value ):
124
118
self .checkAnalogChannel (channel )
125
119
self .checkAnalogValue (value )
126
120
self .__analogWrite__ (channel , value )
127
121
return self .analogRead (channel )
128
122
129
- #@request("POST", "analog/%(channel)d/float/%(value)f")
130
123
@response ("%.2f" )
131
124
def analogWriteFloat (self , channel , value ):
132
125
self .analogWrite (channel , int (value * self ._analogMax ))
133
126
return self .analogReadFloat (channel )
134
127
135
- #@request("POST", "analog/%(channel)d/volt/%(value)f")
136
128
@response ("%.2f" )
137
129
def analogWriteVolt (self , channel , value ):
138
130
self .analogWriteFloat (channel , value / self ._analogRef )
139
131
return self .analogReadVolt (channel )
140
132
141
- def write (self , channel , value ):
142
- return self .analogWrite (channel , value )
133
+ def write (self , channel , value , value_type = None ):
134
+ write_functions = {'float' : self .analogWriteFloat , 'volt' : self .analogWriteVolt }
135
+ write_function = write_functions .get (value_type , self .analogWrite )
136
+ return write_function (channel , value )
143
137
144
138
def writeFloat (self , channel , value ):
145
139
return self .analogWriteFloat (channel , value )
146
140
147
141
def writeVolt (self , channel , value ):
148
142
return self .analogWriteVolt (channel , value )
149
143
144
+
150
145
class PWM ():
151
146
def __init__ (self , channelCount , resolution , frequency ):
152
147
self ._pwmCount = channelCount
@@ -179,54 +174,51 @@ def __pwmRead__(self, channel):
179
174
def __pwmWrite__ (self , channel , value ):
180
175
raise NotImplementedError
181
176
182
- #@request("GET", "pwm/count")
183
177
@response ("%d" )
184
178
def pwmCount (self ):
185
179
return self ._pwmCount
186
180
187
- #@request("GET", "pwm/resolution")
188
181
@response ("%d" )
189
182
def pwmResolution (self ):
190
183
return self ._pwmResolution
191
184
192
- #@request("GET", "pwm/max")
193
185
@response ("%d" )
194
186
def pwmMaximum (self ):
195
187
return int (self ._pwmMax )
196
188
197
- #@request("GET", "pwm/%(channel)d/integer")
198
189
@response ("%d" )
199
190
def pwmRead (self , channel ):
200
191
self .checkPWMChannel (channel )
201
192
return self .__pwmRead__ (channel )
202
193
203
- #@request("GET", "pwm/%(channel)d/float")
204
194
@response ("%.2f" )
205
195
def pwmReadFloat (self , channel ):
206
196
return self .pwmRead (channel ) / float (self ._pwmMax )
207
197
208
- #@request("POST", "pwm/%(channel)d/integer/%(value)d")
209
198
@response ("%d" )
210
199
def pwmWrite (self , channel , value ):
211
200
self .checkPWMChannel (channel )
212
201
self .checkPWMValue (value )
213
202
self .__pwmWrite__ (channel , value )
214
203
return self .pwmRead (channel )
215
204
216
- #@request("POST", "pwm/%(channel)d/float/%(value)f")
217
205
@response ("%.2f" )
218
206
def pwmWriteFloat (self , channel , value ):
219
207
self .pwmWrite (channel , int (value * self ._pwmMax ))
220
208
return self .pwmReadFloat (channel )
221
209
222
- def read (self , channel ):
223
- return self .pwmRead (channel )
210
+ def read (self , channel , value_type = None ):
211
+ read_functions = {'float' : self .pwmReadFloat , 'angle' : self .pwmReadAngle }
212
+ read_function = read_functions .get (value_type , self .pwmRead )
213
+ return read_function (channel )
224
214
225
215
def readFloat (self , channel ):
226
216
return self .pwmReadFloat (channel )
227
217
228
- def write (self , channel , value ):
229
- return self .pwmWrite (channel , value )
218
+ def write (self , channel , value , value_type = None ):
219
+ write_functions = {'float' : self .pwmWriteFloat , 'angle' : self .pwmWriteAngle }
220
+ write_function = write_functions .get (value_type , self .pwmWrite )
221
+ return write_function (channel , value )
230
222
231
223
def writeFloat (self , channel , value ):
232
224
return self .pwmWriteFloat (channel , value )
@@ -256,7 +248,6 @@ def AngleToRatio(self, value):
256
248
f /= self .period
257
249
return f
258
250
259
- #@request("GET", "pwm/%(channel)d/angle")
260
251
@response ("%.2f" )
261
252
def pwmReadAngle (self , channel ):
262
253
f = self .pwmReadFloat (channel )
@@ -267,7 +258,6 @@ def pwmReadAngle(self, channel):
267
258
f = f
268
259
return f
269
260
270
- #@request("POST", "pwm/%(channel)d/angle/%(value)f")
271
261
@response ("%.2f" )
272
262
def pwmWriteAngle (self , channel , value ):
273
263
if self .reverse [channel ]:
@@ -278,7 +268,6 @@ def pwmWriteAngle(self, channel, value):
278
268
self .pwmWriteFloat (channel , f )
279
269
return self .pwmReadAngle (channel )
280
270
281
- #@request("GET", "pwm/*")
282
271
@response (contentType = M_JSON )
283
272
def pwmWildcard (self ):
284
273
values = {}
@@ -300,9 +289,7 @@ def writeAngle(self, channel, value):
300
289
DRIVERS = {}
301
290
DRIVERS ["helper" ] = ["AnalogSensor" , "AnalogActuator" , "ServoMotor" , "Thermistor" , "Photoresistor" , "LoadSensor" , "DistanceSensor" , "LightDimmer" ]
302
291
DRIVERS ["ads1x1x" ] = ["ADS1014" , "ADS1015" , "ADS1114" , "ADS1115" ]
303
- DRIVERS ["mcp3x0x" ] = ["MCP3002" , "MCP3004" , "MCP3008" , "MCP3204" , "MCP3208" ]
304
292
DRIVERS ["mcp4725" ] = ["MCP4725" ]
305
293
DRIVERS ["mcp48XX" ] = ["MCP4802" , "MCP4812" , "MCP4822" ]
306
294
DRIVERS ["mcp492X" ] = ["MCP4921" , "MCP4922" ]
307
- DRIVERS ["pca9685" ] = ["PCA9685" ]
308
295
DRIVERS ["pcf8591" ] = ["PCF8591" ]
0 commit comments