-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfineoffset.h
275 lines (242 loc) · 8.58 KB
/
fineoffset.h
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
#include <usbhid.h>
#include <hiduniversal.h>
#include <stdio.h>
#include <string.h>
// #include <SSD1306.h>
// #ifndef config
// #include "config.h"
// #endif
#define IN_BUFFER_SIZE 32
//Tratare de implementar WH1080_WORK en mi implementación
// define the readings collector
//Definiciones agregadas para poder adaptar las funciones
#define ERROR 1
#define OK 0
#define ERROR_ABORT 2
#define ARCHIVE_VALUE_NULL -100000
/* WH1080 <vendorid, productid> */
#define WH1080_VENDOR_ID 0x1941
#define WH1080_PRODUCT_ID 0x8021
// Define the rain rate acuumulator period (minutes):
#define WH1080_RAIN_RATE_PERIOD 5
// Weather Station buffer parameters:
#define WH1080_RAIN_MAX 0x10000 // Wrap value for rain counter
#define WH1080_BUFFER_START 0x100 // Size of fixed block
// start of buffer records
#define WH1080_BUFFER_CHUNK 0x20 // Size of chunk received over USB
// Weather Station record memory positions:
#define WH1080_DELAY 0 // Position of delay parameter
#define WH1080_HUMIDITY_IN 1 // Position of inside humidity parameter
#define WH1080_TEMPERATURE_IN 2 // Position of inside temperature parameter
#define WH1080_HUMIDITY_OUT 4 // Position of outside humidity parameter
#define WH1080_TEMPERATURE_OUT 5 // Position of outside temperature parameter
#define WH1080_ABS_PRESSURE 7 // Position of absolute pressure parameter
#define WH1080_WIND_AVE 9 // Position of wind direction parameter
#define WH1080_WIND_GUST 10 // Position of wind direction parameter
#define WH1080_WIND_DIR 12 // Position of wind direction parameter
#define WH1080_RAIN 13 // Position of rain parameter
#define WH1080_STATUS 15 // Position of status parameter
// Control block offsets:
#define WH1080_SAMPLING_INTERVAL 16 // Position of sampling interval
#define WH1080_DATA_COUNT 27 // Position of data_count parameter
#define WH1080_CURRENT_POS 30 // Position of current_pos parameter
//Numero de lectura
int numLectura = 0;
//Posicion en memoria
int currentPos;
//the wview daemon work area
typedef struct
{
pid_t myPid;
//WVIEW_MEDIUM medium;
void* stationData; // station-specific data store
int runningFlag;
//RAD_THREAD_ID threadId; // Non-NULL if station uses threads
//int stationGeneratesArchives;
//char pidFile[WVIEW_MAX_PATH];
//char fifoFile[WVIEW_MAX_PATH];
//char statusFile[WVIEW_MAX_PATH];
char stationType[64];
char stationInterface[16];
//char stationDevice[WVIEW_MAX_PATH];
char stationHost[256];
int stationPort;
int stationIsWLIP;
int stationToggleDTR;
int stationRainSeasonStart;
float stationRainStormTrigger;
int stationRainStormIdleHours;
float stationRainYTDPreset;
float stationETYTDPreset;
int stationRainETPresetYear;
uint16_t archiveInterval;
int16_t latitude;
int16_t longitude;
int16_t elevation;
time_t archiveDateTime;
time_t nextArchiveTime; // detect system clock changes
// TIMER_ID archiveTimer;
// TIMER_ID cdataTimer;
// TIMER_ID pushTimer;
// TIMER_ID syncTimer;
// TIMER_ID ifTimer;
uint32_t cdataInterval;
uint32_t pushInterval;
//SENSOR_STORE sensors;
//LOOP_PKT loopPkt;
//LOOP_PKT lastLoopPkt;
int numReadRetries;
int archiveRqstPending;
// Calibration:
float calMBarometer;
float calCBarometer;
float calMPressure;
float calCPressure;
float calMAltimeter;
float calCAltimeter;
float calMInTemp;
float calCInTemp;
float calMOutTemp;
float calCOutTemp;
float calMInHumidity;
float calCInHumidity;
float calMOutHumidity;
float calCOutHumidity;
float calMWindSpeed;
float calCWindSpeed;
float calMWindDir;
float calCWindDir;
float calMRain;
float calCRain;
float calMRainRate;
float calCRainRate;
// Alert Emails:
int IsAlertEmailsEnabled;
//char alertEmailFromAdrs[WVIEW_STRING1_SIZE];
//char alertEmailToAdrs[WVIEW_STRING1_SIZE];
int IsTestEmailEnabled;
int showStationIF;
// Debug:
int DebugStationInput;
int DebugStationOutput;
int DebugStationByteCount;
int UsbRawBytes;
int StreamBytes;
int PacketBytes;
int ChecksumBytes;
int BadLengthBytes;
int StatCount;
int UnknownPacketType;
int exiting;
} WVIEWD_WORK;
// Types for decoding raw weather station data.
// ub unsigned byte
// sb signed byte
// us unsigned short
// ss signed short
// dt date time bcd yymmddhhmm
// tt time bcd hhmm
// pb status - bit 6 lost contact - bit 7 rain counter overflow
// wa wind average low bits puls lower bits of address +2
// wg wind gust low bits plus upper bits of address +1
// wd wind direction
enum ws_types
{
ub,
sb,
us,
ss,
dt,
tt,
pb,
wa,
wg,
wd
};
#define WH1080_NUM_SENSORS 11
// Define the decoder type structure:
typedef struct _decodeType
{
int pos;
enum ws_types ws_type;
float scale;
float *var;
} WH1080_DECODE_TYPE;
// define the readings collector
typedef struct
{
float delay;
float inhumidity;
float intemp;
float outhumidity;
float outtemp;
float pressure;
float windAvgSpeed;
float windGustSpeed;
float windDir;
float rain;
float status;
} WH1080_DATA;
// define the work area
typedef struct
{
WH1080_DATA sensorData;
uint8_t controlBlock[WH1080_BUFFER_CHUNK];
uint8_t recordBlock[WH1080_BUFFER_CHUNK];
int lastRecord;
} WH1080_WORK;
typedef struct
{
int secondsInAccumulator;
float sum;
} WV_ACCUM, *WV_ACCUM_ID;
// define WH1080-specific interface data here
typedef struct
{
int elevation;
float latitude;
float longitude;
int archiveInterval;
WH1080_DATA wh1080Readings;
float totalRain; // to track cumulative changes
//WV_ACCUM_ID rainRateAccumulator; // to compute rain rate
} WH1080_IF_DATA;
class DeviceReader
{
public:
//int PERADDR = 0xe0;
DeviceReader();
int ciclos = 0;
int vendor_id = 1941;
int product_id = 8021;
uint8_t endPoint = 0; //81 en el descriptor de usb y script 0
//int usb_endpoint = 81; // revisar para que sirve, evitar confusión
const uint8_t usb_addr = 1; //Dirección en USB_desc 1 dirección del dispositivo en el hub???
//int address = 1; //Direccion en memoria de la estacion
const uint16_t kBufferSize = 32; //Esto estaba definido como 10, pero luego lo cambié a 16 observando la script de getData.py
//Abrimos el puerto y lo dejamos disponible, basicamente llamamos a openPort()
void openPort(USB Usb);
//Se supone que esta función buscaría el puerto y direccion, pero al ser un solo adaptador se puede obviar (creo), dejando
//las direcciones predeterminadas, revisa que esté listo el USB
void find_device(USB Usb);
//Llamada a uno de los metodos mas basicos de acceso a la estacion
//Hace un control transfer y un intransfer para obtener datos desde USB,
//Retorna un arreglo de 16 bytes, que debe ser la lectura
//uint8_t *read_usb_block(USB Usb, int usb_address);
//Funcion que debería llamar a read_usb_block
uint8_t *read_block(USB Usb, int offset, uint8_t* block);
//Funcion siguiente read_block, lee de 20 en 20 retorna un arreglo
//cuando está correcto, usa los magic_numbers para validar
//Ptr se llama block en el ejemplo de C, pero ptr en el ejemplo de python
uint8_t *read_fixed_block(USB Usb, uint8_t* block);
//Función adaptada de wview
int readStationData(USB Usb, WVIEWD_WORK* work, int currentPosition);
//Se prepara para comenzar las lecturas o envíos, es donde marcamos el indice de currentPos
int setLastSentRecord(int readcount, int stmempos);
//Obtenemos la posicion actual del buffer circular de la estacion
int getCurrentPosition(USB Usb);
//Obtenemos los datos entre dos espacios de memoria
int getData(int lastPosition, int currentPosition);
//Obtenemos los datos de toda la memoria
int getAllData(USB Usb);
};