16
16
from myDevices .utils .types import toint , M_JSON
17
17
from myDevices .devices import instance
18
18
19
+
19
20
class ADC ():
20
21
def __init__ (self , channelCount , resolution , vref ):
21
22
self ._analogCount = channelCount
@@ -34,79 +35,73 @@ def checkAnalogValue(self, value):
34
35
if not 0 <= value <= self ._analogMax :
35
36
raise ValueError ("Value %d out of range [%d..%d]" % (value , 0 , self ._analogMax ))
36
37
37
- #@request("GET", "analog/count")
38
38
@response ("%d" )
39
39
def analogCount (self ):
40
40
return self ._analogCount
41
41
42
- #@request("GET", "analog/resolution")
43
42
@response ("%d" )
44
43
def analogResolution (self ):
45
44
return self ._analogResolution
46
45
47
- #@request("GET", "analog/max")
48
46
@response ("%d" )
49
47
def analogMaximum (self ):
50
48
return int (self ._analogMax )
51
49
52
- #@request("GET", "analog/vref")
53
50
@response ("%.2f" )
54
51
def analogReference (self ):
55
52
return self ._analogRef
56
53
57
54
def __analogRead__ (self , channel , diff ):
58
55
raise NotImplementedError
59
56
60
- #@request("GET", "analog/%(channel)d/integer")
61
57
@response ("%d" )
62
58
def analogRead (self , channel , diff = False ):
63
59
self .checkAnalogChannel (channel )
64
60
return self .__analogRead__ (channel , diff )
65
61
66
- #@request("GET", "analog/%(channel)d/float")
67
62
@response ("%.2f" )
68
63
def analogReadFloat (self , channel , diff = False ):
69
64
return self .analogRead (channel , diff ) / float (self ._analogMax )
70
65
71
- #@request("GET", "analog/%(channel)d/volt")
72
66
@response ("%.2f" )
73
67
def analogReadVolt (self , channel , diff = False ):
74
68
if self ._analogRef == 0 :
75
69
raise NotImplementedError
76
70
return self .analogReadFloat (channel , diff ) * self ._analogRef
77
71
78
- #@request("GET", "analog/*/integer")
79
72
@response (contentType = M_JSON )
80
73
def analogReadAll (self ):
81
74
values = {}
82
75
for i in range (self ._analogCount ):
83
76
values [i ] = self .analogRead (i )
84
77
return values
85
78
86
- #@request("GET", "analog/*/float")
87
79
@response (contentType = M_JSON )
88
80
def analogReadAllFloat (self ):
89
81
values = {}
90
82
for i in range (self ._analogCount ):
91
83
values [i ] = float ("%.2f" % self .analogReadFloat (i ))
92
84
return values
93
85
94
- #@request("GET", "analog/*/volt")
95
86
@response (contentType = M_JSON )
96
87
def analogReadAllVolt (self ):
97
88
values = {}
98
89
for i in range (self ._analogCount ):
99
90
values [i ] = float ("%.2f" % self .analogReadVolt (i ))
100
91
return values
101
92
102
- def read (self , channel , diff = False ):
103
- return self .analogRead (channel , diff )
93
+ def read (self , channel , data_type = None , diff = False ):
94
+ read_functions = {'float' : self .analogReadFloat ,'f' : self .analogReadFloat ,
95
+ 'volt' : self .analogReadVolt ,'v' : self .analogReadVolt }
96
+ read_function = read_functions .get (data_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,21 +113,18 @@ 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 )
@@ -147,6 +139,7 @@ def writeFloat(self, channel, value):
147
139
def writeVolt (self , channel , value ):
148
140
return self .analogWriteVolt (channel , value )
149
141
142
+
150
143
class PWM ():
151
144
def __init__ (self , channelCount , resolution , frequency ):
152
145
self ._pwmCount = channelCount
@@ -179,41 +172,34 @@ def __pwmRead__(self, channel):
179
172
def __pwmWrite__ (self , channel , value ):
180
173
raise NotImplementedError
181
174
182
- #@request("GET", "pwm/count")
183
175
@response ("%d" )
184
176
def pwmCount (self ):
185
177
return self ._pwmCount
186
178
187
- #@request("GET", "pwm/resolution")
188
179
@response ("%d" )
189
180
def pwmResolution (self ):
190
181
return self ._pwmResolution
191
182
192
- #@request("GET", "pwm/max")
193
183
@response ("%d" )
194
184
def pwmMaximum (self ):
195
185
return int (self ._pwmMax )
196
186
197
- #@request("GET", "pwm/%(channel)d/integer")
198
187
@response ("%d" )
199
188
def pwmRead (self , channel ):
200
189
self .checkPWMChannel (channel )
201
190
return self .__pwmRead__ (channel )
202
191
203
- #@request("GET", "pwm/%(channel)d/float")
204
192
@response ("%.2f" )
205
193
def pwmReadFloat (self , channel ):
206
194
return self .pwmRead (channel ) / float (self ._pwmMax )
207
195
208
- #@request("POST", "pwm/%(channel)d/integer/%(value)d")
209
196
@response ("%d" )
210
197
def pwmWrite (self , channel , value ):
211
198
self .checkPWMChannel (channel )
212
199
self .checkPWMValue (value )
213
200
self .__pwmWrite__ (channel , value )
214
201
return self .pwmRead (channel )
215
202
216
- #@request("POST", "pwm/%(channel)d/float/%(value)f")
217
203
@response ("%.2f" )
218
204
def pwmWriteFloat (self , channel , value ):
219
205
self .pwmWrite (channel , int (value * self ._pwmMax ))
@@ -256,7 +242,6 @@ def AngleToRatio(self, value):
256
242
f /= self .period
257
243
return f
258
244
259
- #@request("GET", "pwm/%(channel)d/angle")
260
245
@response ("%.2f" )
261
246
def pwmReadAngle (self , channel ):
262
247
f = self .pwmReadFloat (channel )
@@ -267,7 +252,6 @@ def pwmReadAngle(self, channel):
267
252
f = f
268
253
return f
269
254
270
- #@request("POST", "pwm/%(channel)d/angle/%(value)f")
271
255
@response ("%.2f" )
272
256
def pwmWriteAngle (self , channel , value ):
273
257
if self .reverse [channel ]:
@@ -278,7 +262,6 @@ def pwmWriteAngle(self, channel, value):
278
262
self .pwmWriteFloat (channel , f )
279
263
return self .pwmReadAngle (channel )
280
264
281
- #@request("GET", "pwm/*")
282
265
@response (contentType = M_JSON )
283
266
def pwmWildcard (self ):
284
267
values = {}
0 commit comments