-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.h
489 lines (340 loc) · 13.4 KB
/
main.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Main.h - Copyright 2016-2019, HZB, ILL/SANE & ISIS
#ifndef MAIN_H
#define MAIN_H
#include <stdint.h>
#include "config.h"
#include "module_globals.h"
#ifdef ili9341
#include "disp/ili9341_driver.h"
#endif
#ifdef DISP_3000
#include "disp/DISP_3000_driver.h"
#endif
extern EWindowOrientation Orientation;
extern EWindowOrientation ili_Orientation;
#ifdef DISP_3000
#include "StringPixelCoordTable.h"
#endif
#ifdef ili9341
#include "StringPixelCoordTable_ili9341.h"
//#include "StringPixelCoordTable.h"
#endif
#define arr_size(x) (sizeof(x)/sizeof(x[0]))
// Proportional Factor to calculate the Set_Current (OCR2A = Set_Current * SET_CURRENT_FACTOR + SET_CURRENT_OFFSET)
#define SET_CURRENT_FACTOR 0.8719
#define SET_CURRENT_OFFSET 21.77
// Device position constants
#define END 0x7E // '~' Start and end marker for ranges array
#define SEP 0x2F // '/' Separator marker
#define DEL 0x3B // ';' Delimiter
#define FILL_RANGES_COUNT 40
#define FILL_RANGES_LENGTH 30
#define FILL_LETTERS_LENGTH 5 //4 chars + \0 !
#define AUTO_FILL_TURNED_ON !(PINC & (1<<PC1)) // PC1 = VCC means auto-fill OFF
#define START_AUTO_FILL PORTB |=(1<<PB0); // Set Pin B0
#define STOP_AUTO_FILL PORTB &=~(1<<PB0); // Clear Pin B0
//#define DO_MEASUREMENT !(PINA & (1<<PA5)) // Low trigger on PA5
#define SHUTDOWN PORTB &=~(1<<PB1); // Shutdown - set PB1 at 0
#define MEASBUFFER_LENGTH 25
//#define COM_TIMEOUT_TIME 10 //s
#define WAIT_TIMEOUT_TIME 500 //ms
#define DISP_TIMEOUT_TIME 20 //s
#define OPT_TIMEOUT_TIME 60 //s
#define POS_TIMEOUT_TIME 20
#define SAVE_PLUS_PARAMS_TIMEOUT_TIME 20
#define OPT_CHANGED_TIMEOUT_TIME 20
#define SHUTDOWN_TIMEOUT_TIME 20
#define LOGIN_CONNECT_TIMEOUT_TIME 20
#define STOP_FILL_TIMEOUT_TIME 20
#define CALIBRATE_TIMEOUT_TIME 20
//#define ST_OVERFLOW_HRS 791
//#define ST_OVERFLOW_MIN (13.2)
#define OPTIONS_PASSWORD 0
#define NUMBER_OPTIONS_BYTES 37
#define NUMBER_LOGIN_BYTES 41
// Database server errors
#define HE_LEVEL_ERROR 1111
// XBee network constants
#define TRANSMIT_SLOW_DEF 5 // Time interval in hour(s) between regular network connections
#define TRANSMIT_SLOW_MIN 10 // Minimum allowed value for time interval (minutes)
#define TRANSMIT_SLOW_MAX 24 // Maximum allowed value for time interval (hours)
#define TRANSMIT_FAST_DEF 1 // Reduced time interval in minute(s) between network connections while filling
#define TRANSMIT_FAST_MIN 30 // Minimum allowed value for reduced time interval (seconds)
#define TRANSMIT_FAST_MAX 10 // Maximum allowed value for reduced time interval (minutes)
// Helium probe constants
#define RES_MIN_DEF (0) // Minimum resistance of He probe (user-defined)
#define RES_MIN_MIN (0) // Minimum allowed value for min resistance of He probe
#define RES_MIN_MAX (200) // Maximum allowed value for min resistance of He probe
#define RES_MAX_DEF (100) // Maximum resistance of He probe (user-defined)
#define RES_MAX_MIN (1) // Minimum allowed value for max resistance of He probe
#define RES_MAX_MAX (999.9) // Maximum allowed value for max resistance of He probe
#define QUENCH_TIME_DEF (2) //
#define QUENCH_TIME_MIN (0.0) //
#define QUENCH_TIME_MAX (9.9) //
#define QUENCH_CURRENT_DEF (120) // Current in mA
#define QUENCH_CURRENT_MIN (20) // Minimum allowed value
#define QUENCH_CURRENT_MAX (250) // Maximum allowed value
#define WAIT_TIME_DEF (2) // Waiting time between end of quench and beginning of measurement of He probe (JG)
#define WAIT_TIME_MIN (0.1) // Minimum allowed value
#define WAIT_TIME_MAX (30) // Maximum allowed value
#define MEAS_CURRENT_DEF (80) // Current in mA
#define MEAS_CURRENT_MIN (20) // Minimum allowed value
#define MEAS_CURRENT_MAX (150) // Maximum allowed value
#define TOTAL_VOL_DEF (100) // Total volume in liter
#define TOTAL_VOL_MIN (1) // Minimum allowed value
#define TOTAL_VOL_MAX (1000) // Maximum allowed value
#define MEASUREMENT_CYCLES_DEF 20 // Number of measuring cycles for He probe
#define MEASUREMENT_CYCLES_MIN 1 // Minimum allowed value for number of measuring cycles for He probe
#define MEASUREMENT_CYCLES_MAX 50 // Maximum allowed value for number of measuring cycles for He probe
// He probe calibration
#define R_SPAN_DEF 1 // ???
#define R_ZERO_DEF 0 //
// Helium filling constants
#define FILLING_TIMEOUT_DEF 30 // Timeout while filling in minutes
#define MIN_FILLING_TIMEOUT 10
#define MAX_FILLING_TIMEOUT 90
#define AUTO_FILL_HE_DEF 10 // in %
#define MIN_AUTO_FILL_HE 5
#define MAX_AUTO_FILL_HE 80
#define FILL_WAITING_LABEL "Filling time: %d min"
// Pressure constants
#define ZERO_DEF 0 // Used to calculate pressure level (see map_to_pres())
#define MIN_ZERO (-2000)
#define MAX_ZERO 2000
// #define ZERO_NULLPUNKT 5000 // nur positive zahlen! oder code aendern
#define SPAN_DEF (1.5) //
#define MIN_SPAN 0.1
#define MAX_SPAN 2000
// Battery constants
#define BATT_MIN_DEF 7 // Minimum voltage of battery (user-defined)
#define BATT_MIN_MIN 6 // Minimum allowed value for minimum voltage of battery
#define BATT_MIN_MAX 12 // Maximum allowed value for minimum voltage of battery
#define BATT_MAX_DEF 8.3 // Maximum voltage of battery (user-defined)
#define BATT_MAX_MIN 7 // Minimum allowed value for maximum voltage of battery
#define BATT_MAX_MAX 13 // Maximum allowed value for maximum voltage of battery
#define CRITICAL_BATT_DEF 10 // Critical percentage of battery (user-defined)
#define CRITICAL_BATT_MIN 0
#define CRITICAL_BATT_MAX 100
#define DEV_ID_CHARS_DEF 3
#define DEV_ID_CHARS_MAX 6
#define DEV_ID_CHARS_MIN 3
#define DEVICE_ID_UI_MAX_LEN 3
#define DEVICE_ID_ALPHANUMERIC false
#define MESSAGEFOPRMAT_IDENTIFIER 128 // Identifier used, to differentiate between old and new messageformat
#define OPTIONS_PW_DEF 1
#define OPTIONS_PW_MAX 999
#define OPTIONS_PW_MIN 0
// ILL Display Orientation
#ifdef ILL_DISP_CONFIG
#define DISPLAY_CONFIG Orientation = (LVM.options->display_reversed)? Landscape : Landscape180;
#endif // ILL
// HZB Display Orientation
#ifdef DEFAULT_DISP_CONFIG
#define DISPLAY_CONFIG Orientation = (!LVM.options->display_reversed)? Landscape : Landscape180;
#endif // HZB
#define POST_PULSE_SLEEP_TIME 60 //after a remote Pulse the Levelmter has a number of sleeep cycles that are shorter than usual to facilitate faster chaining of multiple remote pulses
#define NUMBER_POST_PULSE_WAKES 10
#define EEPROM_CHANGED_DEF 0
// Configuration
#define BUFFER_LENGTH 10 // maximum frames
#define DATA_LENGTH 256-16 // maximum data in frame ATTENTION if changed then change SINGLE_FRAME_LENGTH in xbee.h as well
#define DATA_LENGTH_MEAS 100 // maximum data in frame for level measurement (#3 #6 #7 #8 #10)
#define SINGLE_FRAME_LENGTH 256 // Full length of one frame ATTENTION if changed then change DATA_LENGTH in xbee_utilities.h as well
//#define BYTE unsigned char
#define DEVICE_ID_STRING_LEN 6
#define ALLOW_COM // Allow the possibility to receive answer from XBee module
// #define ALLOW_DEBUG
#define ALLOW_DISPLAY_TIMER
#define ALLOW_LOGIN
#define ALLOW_XBEE_SLEEP // Allow sleeping mode for XBee module
#define ALLOW_EEPROM_SAVING
//#define ALLOW_DOUBLE_CLICK // Define to allow double click: wake up display and execute the pressed key
//==============================================================
// List of error codes
//==============================================================
#define CHECK_BOUNDS(VAR,MIN,MAX,DEF,FLAG) if((VAR < MIN) || (VAR > MAX || isnan(VAR))){VAR = DEF; FLAG = 1;};
//====================================================================
// OPTIONS
//====================================================================
typedef struct{
double r_span;
double r_zero;
uint16_t transmit_slow;
_Bool transmit_slow_min;
uint16_t transmit_fast;
_Bool transmit_fast_sec;
double quench_time;
double quench_current;
double wait_time;
double meas_current;
uint8_t meas_cycles;
uint8_t fill_timeout;
uint8_t he_min;
double res_min;
double res_max;
double span;
double zero;
uint8_t enable_pressure;
double batt_min;
double batt_max;
uint8_t critical_batt;
double total_volume;
uint8_t display_reversed;
_Bool Dev_ID_alpahnum;
uint8_t Dev_ID_Max_len;
uint16_t options_pw;
uint16_t SC_mask;
uint8_t SC_mask_alerady_received;
}optionsType;
//====================================================================
// POSITION
//====================================================================
typedef struct {
char Strings[FILL_RANGES_COUNT][FILL_LETTERS_LENGTH];
uint8_t RangeNums[FILL_RANGES_COUNT][FILL_RANGES_LENGTH];
uint8_t StrLen[FILL_RANGES_COUNT];
int8_t active_Pos_Str;
int8_t active_Pos_Str_last;
int8_t active_range_Number;
int8_t active_range_Number_last;
_Bool digit_on;
_Bool digit_on_last;
_Bool letters_and_numbers ;
}posType;
//====================================================================
// Message from Server
//====================================================================
typedef struct
{
char Str[19];
uint8_t type;
_Bool Received;
}LCD_MessageType;
//====================================================================
// unsent MEASUREMENT
//====================================================================
// Buffer for old measurements that could not be transmitted, see also typedef "frame" in xbee_utilities
typedef struct {
uint8_t type;
uint8_t data_len;
uint8_t data[DATA_LENGTH_MEAS];
} savedMeasType;
//====================================================================
// SAVED MEASUREMENTS RINGBUFFER
//====================================================================
typedef struct {
savedMeasType measurements[MEASBUFFER_LENGTH];
uint8_t firstMeas; // index of the first entry
uint8_t nextfreeEntry; // index of the next free entry
uint8_t numberStored; // number of stored measurements
}MeasBufferType;
//====================================================================
// TEMPORARY BUFFERS
//====================================================================
typedef struct {
char string[SINGLE_FRAME_LENGTH];
uint8_t buffer[SINGLE_FRAME_LENGTH];
uint8_t databuffer[DATA_LENGTH_MEAS];
char infostr[30];
}Temp_buffersType;
//====================================================================
// VARS
//====================================================================
typedef struct{
double r_val_last_Meas;
double he_level; // Init and clear Helium level
double last_he_level;
double batt_level; // Init and clear battery level
double pressure_level; // Init and clear pressure value
uint16_t eeprom_changed;
_Bool very_low_He_level; // Flag for very low Helium level (< level min)
_Bool auto_fill_enabled;
_Bool auto_fill_started;
_Bool transmit_slow_changed; // Set true if time interval between regular network connections has been changed
_Bool transmit_fast_changed;
// Device position (instruments or labs)
char device_pos[5];
uint8_t n_pulse_wakes;
//diag pages
uint16_t entered_options_pw;
uint8_t fill_meas_counter;
char Device_ID_Str[DEVICE_ID_STRING_LEN+1];
}VarsType;
typedef struct
{
optionsType eeOptions;
uint16_t eeprom_changed;
}eememType;
typedef struct{
VarsType *const vars;
optionsType *const options;
posType *const pos;
LCD_MessageType *const message;
MeasBufferType *const measbuff;
Temp_buffersType *const temp;
VersionType *const version;
eememType *const eeprom;
}LVM_ModelType;
// diag pulse Model
typedef struct
{
uint32_t elapsed_t; //elapsed time in timer overflows, --> conversion is needed to get ms
uint32_t elapsed_t_last;
uint32_t delta_t_timer_steps;
uint32_t t_end_quench;
uint32_t t_end_wait;
uint32_t t_end_meas;
uint8_t meastime_factor;
_Bool draw_all;
_Bool headless;
_Bool quench_on;
uint16_t * top_unit;
double heat_time;
double quench_current;
double meas_current;
double wait_time;
double meas_time;
double r_span;
double r_zero;
int16_t active_point;
uint16_t u[DP_NUMBER_OF_POINTS_140];
uint16_t i[DP_NUMBER_OF_POINTS_140];
uint16_t r[DP_NUMBER_OF_POINTS_140];
uint16_t u_point[DP_NUMBER_OF_POINTS_140];
uint16_t i_point[DP_NUMBER_OF_POINTS_140];
uint16_t r_point[DP_NUMBER_OF_POINTS_140];
uint16_t color[DP_NUMBER_OF_POINTS_140];
uint8_t y_maxpixels;
uint16_t cursor_len;
uint8_t x_fact;
uint16_t quench_end_position;
double delta_t_points;
uint16_t top_zero;
uint16_t bot_zero;
uint16_t u_max;
uint16_t i_max;
uint16_t r_max;
double u_avg;
double i_avg;
uint16_t n_for_avg;
double I_increment;
double diag_res;
uint8_t pulse_type;
uint16_t points_in_plot;
uint16_t halfway_point;
}diag_pulseType;
#define DRAW_U 1
#define DRAW_R 2
//#define CHECK_ERROR(LETTERS_ERROR) (status_byte & (1<<6))
//==============================================================
// Add the following commands
//==============================================================
//_Bool auto_fill_pin_on(void); // Check auto fill pin
void diag_pulse_move_cursor(diag_pulseType *dp,int8_t direction);
//int main(void);
int I2C_ClearBus(void);
uint8_t collect_and_send_MeasData(uint8_t *meas_buffer,uint8_t Message_Code);
_Bool auto_fill_pin_on(void);
void write_opts_to_EEPROM(void);
void write_DEFS_to_EEPROM(void);
#endif // main.h