-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.c
More file actions
377 lines (251 loc) · 9.36 KB
/
display.c
File metadata and controls
377 lines (251 loc) · 9.36 KB
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
/* +--------------------------------------------+ */
/* | Zenit Multimeter | */
/* | display.c | */
/* | (c)copyright nitram147 [Martin Ersek] 2018 | */
/* +--------------------------------------------+ */
#include <avr/io.h>
#include <stdlib.h>
#include "esp.h" //parser.h require definitions from esp.h
#include "parser.h" //definition of measuring_ranges and global variables
#include "display.h"
#include "lcd.h"
#include "buttons_switches.h"
#include "measure.h"
//global variable containing selected mode of device
DISPLAY_MODE_T display_interface_mode = U_MEASUREMENT;
//global variable used to determine whether show or no labels before measured values
//label example "U="
LABELS_STATE_T labels_state = SHOW_LABELS;
//flag which indicate if we need to rewrie display
//because we don't want to rewrite display each time
uint8_t display_need_to_be_rewrited = 1;
//lcd puts function fix
//because 16x1 display is working like 8x2 display
volatile uint8_t lcd_fix_cursor_position = 0;
void lcd_puts_fixed(char *tmp_string){
char tmp_char;
while((tmp_char = *tmp_string++) != 0x00){
if(lcd_fix_cursor_position == 8) lcd_putc('\n');
lcd_putc(tmp_char);
lcd_fix_cursor_position++;
}
}
//lcd putc function fix
//because 16x1 display is working like 8x2 display
void lcd_putc_fixed(char tmp_char){
if(lcd_fix_cursor_position == 8) lcd_putc('\n');
lcd_putc(tmp_char);
lcd_fix_cursor_position++;
}
//erase display and go to home position
void erase_display(){
lcd_clrscr();lcd_home();
lcd_fix_cursor_position = 0;
}
//print IP address assigned to ESP
void print_ip_address(){
char tmp_buffer[4];
//convert & print first octet
itoa(esp_state.ip_address.first,tmp_buffer,10);
lcd_puts_fixed(tmp_buffer);lcd_putc_fixed('.');
//convert & print second octet
itoa(esp_state.ip_address.second,tmp_buffer,10);
lcd_puts_fixed(tmp_buffer);lcd_putc_fixed('.');
//convert & print third octet
itoa(esp_state.ip_address.third,tmp_buffer,10);
lcd_puts_fixed(tmp_buffer);lcd_putc_fixed('.');
//convert & print fourth octet
itoa(esp_state.ip_address.fourth,tmp_buffer,10);
lcd_puts_fixed(tmp_buffer);
}
//setup lcd position before writing custom char
void lcd_setup_position(uint8_t tmp_x, uint8_t tmp_y){
if(tmp_y == 0) lcd_command(0x80 | tmp_x);
else if(tmp_y == 1) lcd_command(0xC0 | tmp_x);
}
//upload custom char to CGRAM of display driver
void custom_char_wifi_init(){
lcd_command(0x40);
lcd_data(0b00000000);
lcd_data(0b00011111);
lcd_data(0b00010000);
lcd_data(0b00010111);
lcd_data(0b00010100);
lcd_data(0b00010101);
lcd_data(0b00000000);
lcd_data(0b00000000);
}
//show custom char (wifi symbol) on the last display position
void show_custom_wifi_char(){
//display custom char only if we are successfully connected to wifi
if(esp_state.connected_to_wifi && esp_state.wifi_got_ip){
//upload custom char to CGRAM
custom_char_wifi_init();
//setup lcd position before writing custom char
lcd_setup_position(7,1);
//write first custom character stored in CGRAM (our custom wifi symbol)
lcd_data(0);
}
}
//call specific display function based on currently selected mode of device
void display_actual_mode_content(uint8_t tmp_measure_it){
switch(display_interface_mode){
case U_MEASUREMENT: mode_u_measurement(tmp_measure_it); break;
case I_MEASUREMENT: mode_i_measurement(tmp_measure_it); break;
case R_MEASUREMENT: mode_r_measurement(tmp_measure_it); break;
case D_MEASUREMENT: mode_d_measurement(tmp_measure_it); break;
case T_MEASUREMENT: mode_t_measurement(tmp_measure_it); break;
///* reserved for sixth button mode */case (): break;
///* reserved for seventh button mode */case (): break;
case WIFI_MODE: mode_wifi(); break;
}
}
void display_range(MEASURING_RANGES_T tmp_requested, MEASURING_RANGES_T tmp_actual){
//go to desired location
//(fourth character, second line) - because display is behaving like 8x2
lcd_gotoxy(4,1);
if(tmp_requested == RANGE_AUTO) lcd_putc_fixed('A');
else lcd_putc_fixed('M');
//increase range number by one (because we are indexing enum from 0)
//add '0' ascii value to convert number to character
lcd_putc_fixed(tmp_actual+1+'0');
}
void display_number_with_unit_prefix(float tmp_number, uint8_t tmp_precision){
char tmp_unit_prefix = 0;
char tmp_buffer[16];
uint8_t tmp_count_of_decimals;
//in case of negative number
if(tmp_number < 0){
//display negative sign
lcd_putc_fixed('-');
//make positive number from negative
tmp_number = -tmp_number;
//in case of positive number
}else{
//display space
lcd_putc_fixed(' ');
}
//if our number is smaller than one (unit prefix mili)
if(tmp_number < 1.0000){
tmp_unit_prefix = 'm';
if(tmp_number < 0.0100) tmp_count_of_decimals = 4;
else if(tmp_number < 0.1000) tmp_count_of_decimals = 3;
else tmp_count_of_decimals = 2;
tmp_number *= 1000;
//if our number is smaller than thousand (unit prefix none)
}else if(tmp_number < 1000.0){
if(tmp_number < 10.000) tmp_count_of_decimals = 4;
else if(tmp_number < 100.00) tmp_count_of_decimals = 3;
else tmp_count_of_decimals = 2;
//if our number is smaller than milion (unit prefix kilo)
}else if(tmp_number < 1000000.0){
tmp_unit_prefix = 'k';
if(tmp_number < 10000.0) tmp_count_of_decimals = 4;
else if(tmp_number < 100000.0) tmp_count_of_decimals = 3;
else tmp_count_of_decimals = 2;
tmp_number /= 1000;
//if our number is bigger than milion (unit prefix mega)
}else{
tmp_unit_prefix = 'M';
if(tmp_number < 10000000.0) tmp_count_of_decimals = 4;
else if(tmp_number < 100000000.0) tmp_count_of_decimals = 3;
else tmp_count_of_decimals = 2;
tmp_number /= 1000000;
}
//increase or decrease count of decimals in case that function is called with different precision
//default precision is 6, so constants are calculated for that precisions which means
//they need to be changed in case of different precision
tmp_count_of_decimals += (tmp_precision - 6);
//convert number into string with specified precision, output strlen = tmp_precision
dtostrf(tmp_number, tmp_precision, tmp_count_of_decimals, tmp_buffer);
//display number with unit prefix
lcd_puts_fixed(tmp_buffer);
if(tmp_unit_prefix) lcd_putc_fixed(tmp_unit_prefix);
}
void mode_u_measurement(uint8_t tmp_measure_it){
if(tmp_measure_it) measure_voltage();
if(display_need_to_be_rewrited){
clear_measurements_modes_buttons_colors();
change_button_color(FIRST_BUTTON, GREEN);
erase_display();
if(labels_state == SHOW_LABELS) lcd_puts_fixed("U=");
if(!current_measured_values.overflow){
display_number_with_unit_prefix(current_measured_values.voltage, ((labels_state == SHOW_LABELS)?6:8));
lcd_putc_fixed('V');
}else {lcd_puts_fixed(" OverFlow");}
display_need_to_be_rewrited = 0;
display_range(voltage_range_requested, voltage_range_actual);
show_custom_wifi_char();
}
}
void mode_i_measurement(uint8_t tmp_measure_it){
if(tmp_measure_it) measure_current();
if(display_need_to_be_rewrited){
clear_measurements_modes_buttons_colors();
change_button_color(SECOND_BUTTON, GREEN);
erase_display();
if(labels_state == SHOW_LABELS) lcd_puts_fixed("I=");
if(!current_measured_values.overflow){
display_number_with_unit_prefix(current_measured_values.current, ((labels_state == SHOW_LABELS)?6:8));
lcd_putc_fixed('A');
}else {lcd_puts_fixed(" OverFlow");}
display_need_to_be_rewrited = 0;
display_range(current_range_requested, current_range_actual);
show_custom_wifi_char();
}
}
void mode_r_measurement(uint8_t tmp_measure_it){
if(tmp_measure_it) measure_resistance();
if(display_need_to_be_rewrited){
clear_measurements_modes_buttons_colors();
change_button_color(THIRD_BUTTON, GREEN);
erase_display();
if(labels_state == SHOW_LABELS) lcd_puts_fixed("R=");
if(current_measured_values.resistance <= MAX_RESISTANCE){
display_number_with_unit_prefix(current_measured_values.resistance, ((labels_state == SHOW_LABELS)?6:8));
lcd_putc_fixed(OHM_CHARACTER);
}else{
lcd_puts_fixed(" Open");
}
display_need_to_be_rewrited = 0;
show_custom_wifi_char();
}
}
void mode_d_measurement(uint8_t tmp_measure_it){
if(tmp_measure_it) measure_diode();
if(display_need_to_be_rewrited){
clear_measurements_modes_buttons_colors();
change_button_color(FOURTH_BUTTON, GREEN);
erase_display();
if(labels_state == SHOW_LABELS) lcd_puts_fixed("D=");
if(current_measured_values.diode < MAX_DIODE_VOLTAGE){
display_number_with_unit_prefix(current_measured_values.diode, ((labels_state == SHOW_LABELS)?6:8));
lcd_putc_fixed('V');
}else{
lcd_puts_fixed(" OverRange");
}
display_need_to_be_rewrited = 0;
show_custom_wifi_char();
}
}
void mode_t_measurement(uint8_t tmp_measure_it){
if(tmp_measure_it) measure_temperature();
if(display_need_to_be_rewrited){
clear_measurements_modes_buttons_colors();
change_button_color(FIFTH_BUTTON, GREEN);
erase_display();
if(labels_state == SHOW_LABELS) lcd_puts_fixed("T=");
display_number_with_unit_prefix(current_measured_values.temperature, ((labels_state == SHOW_LABELS)?6:8));
lcd_putc_fixed(DEGREE_CHARACTER); lcd_putc_fixed('C');
if(selected_temperature_sensor_type == RTD) lcd_puts_fixed(" RTD");
else lcd_puts_fixed(" NTC");
display_need_to_be_rewrited = 0;
show_custom_wifi_char();
}
}
void mode_wifi(){
if(display_need_to_be_rewrited){
clear_measurements_modes_buttons_colors();
display_need_to_be_rewrited = 0;
}
}