-
Notifications
You must be signed in to change notification settings - Fork 7
/
USBHardware.py
349 lines (321 loc) · 12.2 KB
/
USBHardware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/python
## This driver is based is based on reverse engineering of HeavyWeather 2800 v 1.54
## All copyright goes to La Crosse Technology (c) 2008
## Python port by Eddi De Pieri <[email protected]>
## Use this software as your own risk.
## Me and La Crosse Technology is not responsable for any damage using this software
import logging
import datetime
import CWeatherTraits
CWeatherTraits = CWeatherTraits.CWeatherTraits()
class USBHardware(object):
def __init__(self):
self.logger = logging.getLogger('ws28xx.USBHardware')
def IsOFL2(self, buffer, start, startOnLowNibble):
if ( startOnLowNibble ):
result = (buffer[0][start+0] & 0xF) == 15 \
or (buffer[0][start+0] >> 4) == 15
else:
result = (buffer[0][start+0] >> 4) == 15 \
or (buffer[0][start+1] & 0xF) == 15
return result
def IsOFL3(self, buffer, start, startOnLowNibble):
if ( startOnLowNibble ):
result = (buffer[0][start+0] & 0xF) == 15 \
or (buffer[0][start+0] >> 4) == 15 \
or (buffer[0][start+1] & 0xF) == 15
else:
result = (buffer[0][start+0] >> 4) == 15 \
or (buffer[0][start+1] & 0xF) == 15 \
or (buffer[0][start+1] >> 4) == 15
return result;
def IsOFL5(self, buffer, start, startOnLowNibble):
if ( startOnLowNibble ):
result = (buffer[0][start+0] & 0xF) == 15 \
or (buffer[0][start+0] >> 4) == 15 \
or (buffer[0][start+1] & 0xF) == 15 \
or (buffer[0][start+1] >> 4) == 15 \
or (buffer[0][start+2] & 0xF) == 15
else:
result = (buffer[0][start+0] >> 4) == 15 \
or (buffer[0][start+1] & 0xF) == 15 \
or (buffer[0][start+1] >> 4) == 15 \
or (buffer[0][start+2] & 0xF) == 15 \
or (buffer[0][start+2] >> 4) == 15
return result
def IsErr2(self,buffer,start,startOnLowNibble):
if ( startOnLowNibble ):
result = (buffer[0][start+0] & 0xF) >= 10 \
and (buffer[0][start+0] & 0xF) != 15 \
or (buffer[0][start+0] >> 4) >= 10 \
and (buffer[0][start+0] >> 4) != 15
else:
result = (buffer[0][start+0] >> 4) >= 10 \
and (buffer[0][start+0] >> 4) != 15 \
or (buffer[0][start+1] & 0xF) >= 10 \
and (buffer[0][start+1] & 0xF) != 15
return result
def IsErr3(self,buffer,start,startOnLowNibble):
if ( startOnLowNibble ):
result = (buffer[0][start+0] & 0xF) >= 10 \
and (buffer[0][start+0] & 0xF) != 15 \
or (buffer[0][start+0] >> 4) >= 10 \
and (buffer[0][start+0] >> 4) != 15 \
or (buffer[0][start+1] & 0xF) >= 10 \
and (buffer[0][start+1] & 0xF) != 15
else:
result = (buffer[0][start+0] >> 4) >= 10 \
and (buffer[0][start+0] >> 4) != 15 \
or (buffer[0][start+1] & 0xF) >= 10 \
and (buffer[0][start+1] & 0xF) != 15 \
or (buffer[0][start+1] >> 4) >= 10 \
and (buffer[0][start+1] >> 4) != 10
return result
def IsErr5(self,buffer,start,startOnLowNibble):
if ( startOnLowNibble ):
result = (buffer[0][start+0] & 0xF) >= 10 \
and (buffer[0][start+0] & 0xF) != 15 \
or (buffer[0][start+0] >> 4) >= 10 \
and (buffer[0][start+0] >> 4) != 15 \
or (buffer[0][start+1] & 0xF) >= 10 \
and (buffer[0][start+1] & 0xF) != 15 \
or (buffer[0][start+1] >> 4) >= 10 \
and (buffer[0][start+1] >> 4) != 15 \
or (buffer[0][start+2] & 0xF) >= 10 \
and (buffer[0][start+2] & 0xF) != 15
else:
result = (buffer[0][start+0] >> 4) >= 10 \
and (buffer[0][start+0] >> 4) != 15 \
or (buffer[0][start+1] & 0xF) >= 10 \
and (buffer[0][start+1] & 0xF) != 15 \
or (buffer[0][start+1] >> 4) >= 10 \
and (buffer[0][start+1] >> 4) != 15 \
or (buffer[0][start+2] & 0xF) >= 10 \
and (buffer[0][start+2] & 0xF) != 15 \
or (buffer[0][start+2] >> 4) >= 10 \
and (buffer[0][start+2] >> 4) != 15
return result
def ToCurrentTempBytes(self,bufer,c, d):
self.logger.debug("")
def To2Pre(self,buffer, start, startOnLowNibble):
self.logger.debug("")
if startOnLowNibble:
rawpre = (buffer[0][start+0] & 0xf)* 1 \
+ (buffer[0][start+0] >> 4)* 10
else:
rawpre = (buffer[0][start+0] >> 4)* 1 \
+ (buffer[0][start+1] & 0xf)* 10
return rawpre
def ToRainAlarmBytes(buffer,alarm):
self.logger.debug("")
def ToDateTime(self, buffer, start, startOnLowNibble):
self.logger.debug("")
#print "buffer[0]", buffer[0]
#ATL::COleDateTime::COleDateTime(&invalidDate);
#ATL::COleDateTime::SetStatus(&invalidDate, partial);
if ( self.IsErr2(buffer, start+0, startOnLowNibble)
or self.IsErr2(buffer, start+1, startOnLowNibble)
or self.IsErr2(buffer, start+2, startOnLowNibble)
or self.IsErr2(buffer, start+3, startOnLowNibble)
or self.IsErr2(buffer, start+4, startOnLowNibble)
or self.To2Pre(buffer, start+3, startOnLowNibble) > 12): #fix since I bugget my ws with corrupted date..
#*(_QWORD *)&result->m_dt = (_QWORD)invalidDate.m_dt;
#result->m_status = invalidDate.m_status;
#return datetime.datetime.now()
return datetime.datetime(1900, 01, 01, 00, 00) #fake too old date means invalid date
else:
minutes = self.To2Pre(buffer, start+0, startOnLowNibble)
hours = self.To2Pre(buffer, start+1, startOnLowNibble)
days = self.To2Pre(buffer, start+2, startOnLowNibble)
month = self.To2Pre(buffer, start+3, startOnLowNibble)
year = self.To2Pre(buffer, start+4, startOnLowNibble) + 2000;
result = datetime.datetime(year, month, days, hours, minutes)
# *(_QWORD *)&result->m_dt = (_QWORD)dt.m_dt;
# result->m_status = dt.m_status;
return result
#return datetime.datetime.now()
def ToHumidity(self,buffer,start,startOnLowNibble):
self.logger.debug("")
if ( self.IsErr2(buffer, start+0, startOnLowNibble) ):
result = CWeatherTraits.HumidityNP();
else:
if ( self.IsOFL2(buffer, start+0, startOnLowNibble) ):
result = CWeatherTraits.HumidityOFL()
else:
result = self.To2Pre(buffer, start, startOnLowNibble);
return result;
def ToTemperature(self,buffer, start, startOnLowNibble):
self.logger.debug("")
if ( self.IsErr5(buffer, start+0, startOnLowNibble) ):
result = CWeatherTraits.TemperatureNP()
else:
if ( self.IsOFL5(buffer, start+0, startOnLowNibble) ):
result = CWeatherTraits.TemperatureOFL()
else:
if startOnLowNibble:
rawtemp = (buffer[0][start+0] & 0xf)* 0.001 \
+ (buffer[0][start+0] >> 4)* 0.01 \
+ (buffer[0][start+1] & 0xf)* 0.1 \
+ (buffer[0][start+1] >> 4)* 1 \
+ (buffer[0][start+2] & 0xf)* 10
else:
rawtemp = (buffer[0][start+0] >> 4)* 0.001 \
+ (buffer[0][start+1] & 0xf)* 0.01 \
+ (buffer[0][start+1] >> 4)* 0.1 \
+ (buffer[0][start+2] & 0xf)* 1 \
+ (buffer[0][start+2] >> 4)* 10
result = rawtemp - CWeatherTraits.TemperatureOffset()
return result;
def To4Pre3Post(self,buffer,start):
self.logger.debug("")
if ( self.IsErr5(buffer, start+0, 1) or self.IsErr2(buffer, start+2, 0) ):
result = CWeatherTraits.RainNP()
else:
if ( self.IsOFL5(buffer, start+1, 1) or self.IsOFL2(buffer, start+2, 0) ):
result = CWeatherTraits.RainOFL()
else:
result = (buffer[0][start+0] & 0xf)* 0.001 \
+ (buffer[0][start+0] >> 4)* 0.01 \
+ (buffer[0][start+1] & 0xf)* 0.1 \
+ (buffer[0][start+1] >> 4)* 1 \
+ (buffer[0][start+2] & 0xf)* 10 \
+ (buffer[0][start+2] >> 4)* 100 \
+ (buffer[0][start+3] & 0xf)*1000
return result
def To4Pre2Post(self,buffer,start):
self.logger.debug("")
if ( self.IsErr2(buffer,start+0,1) or self.IsErr2(buffer,start+1, 1) or self.IsErr2(buffer, start+2, 1) ):
result = CWeatherTraits.RainNP();
else:
if ( self.IsOFL2(buffer,start+0, 1) or self.IsOFL2(buffer, start+1, 1) or self.IsOFL2(buffer, start+2, 1) ):
result = CWeatherTraits.RainOFL()
else:
result = (buffer[0][start+0] & 0xf)* 0.01 \
+ (buffer[0][start+0] >> 4)* 0.1 \
+ (buffer[0][start+1] & 0xf)* 1 \
+ (buffer[0][start+1] >> 4)* 10 \
+ (buffer[0][start+2] & 0xf)* 100 \
+ (buffer[0][start+2] >> 4)*1000
return result
def ToWindspeed(self,buffer,start): #m/s
self.logger.debug("")
val = self.ByteToFloat(buffer, start, 1, 16, 6);
val = val / 256.0;
val = val / 100.0; #km/h
val = val / 3.599999904632568; #m/s
return val
def ByteToFloat(self,buffer, start,startOnLowNibble, base, pre):
self.logger.debug("")
lowNibble = startOnLowNibble;
val = 0;
byteCounter = 0;
i = 0;
#for i in xrange(0,pre):
while i < pre:
if ( pre > 0 ):
digit = 0;
if ( lowNibble ):
digit = buffer[0][start+byteCounter] & 0xF;
else:
digit = buffer[0][start+byteCounter] >> 4;
if ( not lowNibble ):
byteCounter += 1
if lowNibble == 0:
lowNibble=1
else:
lowNibble=0
power = base**i
val += digit * power
i += 1
return val
def ReverseByteOrder(self,buf,start,Count):
self.logger.debug("")
nbuf=buf[0]
#print nbuf
for i in xrange(0, Count >> 1):
tmp = nbuf[start + i]
nbuf[start + i] = nbuf[start + Count - i - 1]
nbuf[start + Count - i - 1 ] = tmp
buf[0]=nbuf
def ReadWindDirectionShared(self,buffer,start):
self.logger.debug("")
return (buffer[0][0+start] & 0xf, buffer[0][start] >> 4)
def ReadPressureShared(self,buffer,start):
self.logger.debug("")
return ( self.ToPressure(buffer,start,1) , self.ToPressureInhg(buffer,start+2,0))
def ToPressure(self,buffer,start,startOnLowNibble):
if ( self.IsErr5(buffer, start+0, startOnLowNibble) ):
result = CWeatherTraits.PressureNP();
else:
if ( self.IsOFL5(buffer, start+0, startOnLowNibble) ):
result = CWeatherTraits.PressureOFL();
else:
if ( startOnLowNibble ):
rawresult = (buffer[0][start+2] & 0xF)* 1000 \
+ (buffer[0][start+1] >> 4)* 100 \
+ (buffer[0][start+1] & 0xF)* 10 \
+ (buffer[0][start+0] >> 4)* 1 \
+ (buffer[0][start+0] & 0xF)* 0.1
else:
rawresult = (buffer[0][start+2] >> 4)* 1000 \
+ (buffer[0][start+2] & 0xF)* 100 \
+ (buffer[0][start+1] >> 4)* 10 \
+ (buffer[0][start+1] & 0xF)* 1 \
+ (buffer[0][start+0] >> 4)* 0.1
result = rawresult
return result
def ToPressureInhg(self,buffer,start,startOnLowNibble):
if ( self.IsErr5(buffer, start+0, startOnLowNibble) ):
rawresult = CWeatherTraits.PressureNP();
else:
if ( self.IsOFL5(buffer, start+0, startOnLowNibble) ):
rawresult = CWeatherTraits.PressureOFL()
else:
if ( startOnLowNibble ):
rawresult = (buffer[0][start+2] & 0xF)* 100 \
+ (buffer[0][start+1] >> 4)* 10 \
+ (buffer[0][start+1] & 0xF)* 1 \
+ (buffer[0][start+0] >> 4)* 0.1 \
+ (buffer[0][start+0] & 0xF)* 0.01
else:
rawresult = (buffer[0][start+2] >> 4)* 100 \
+ (buffer[0][start+2] & 0xF)* 10 \
+ (buffer[0][start+1] >> 4)* 1 \
+ (buffer[0][start+0] & 0xF)* 0.1 \
+ (buffer[0][start+0] >> 4)* 0.01
result = rawresult
return result
def ToTemperatureRingBuffer(self,buffer,start,startOnLowNibble):
if ( self.IsErr3(buffer, start+0, startOnLowNibble) ):
result = CWeatherTraits.TemperatureNP()
else:
if ( self.IsOFL3(buffer, start+0, startOnLowNibble) ):
result = CWeatherTraits.TemperatureOFL()
else:
if ( startOnLowNibble ):
#rawtemp = (buffer[0][start+0] & 0xF)* 10 \
# + (buffer[0][start+0] >> 4)* 1 \
# + (buffer[0][start+1] & 0xF)* 0.1
rawtemp = (buffer[0][start+0] & 0xF)* 0.1 \
+ (buffer[0][start+0] >> 4)* 1 \
+ (buffer[0][start+1] & 0xF)* 10
else:
#rawtemp = (buffer[0][start+0] >> 4)* 10 \
# + (buffer[0][start+1] & 0xF)* 1 \
# + (buffer[0][start+1] >> 4)* 0.1
rawtemp = (buffer[0][start+0] >> 4)* 0.1 \
+ (buffer[0][start+1] & 0xF)* 1 \
+ (buffer[0][start+1] >> 4)* 10
result = rawtemp - CWeatherTraits.TemperatureOffset()
return result
def ToWindspeedRingBuffer(self,buffer,start):
if ( buffer[0][start+0] != 254 or (buffer[0][start+1] & 0xF) != 1 ):
if ( buffer[0][start+0] != 255 or (buffer[0][start+1] & 0xF) != 1 ):
val = self.ByteToFloat(buffer, start, 1, 16, 3);
val = val / 10.0;
result = val;
else:
result = CWeatherTraits.WindOFL();
else:
result = CWeatherTraits.WindNP();
return result