forked from Alex20129/eeprom_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.c
More file actions
373 lines (317 loc) · 9.48 KB
/
Copy pathui.c
File metadata and controls
373 lines (317 loc) · 9.48 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
#include "ui.h"
#include "eeprom_structure.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
// ═══════════════════════════════════════════════════════════════
// Formatted Output Functions
// ═══════════════════════════════════════════════════════════════
void ui_print_success(const char *format, ...)
{
va_list args;
va_start(args, format);
printf(TERM_GREEN "✓ " TERM_RESET);
vprintf(format, args);
printf("\n");
va_end(args);
}
void ui_print_error(const char *format, ...)
{
va_list args;
va_start(args, format);
printf(TERM_RED "✗ Error: " TERM_RESET);
vprintf(format, args);
printf("\n");
va_end(args);
}
void ui_print_warning(const char *format, ...)
{
va_list args;
va_start(args, format);
printf(TERM_YELLOW "⚠ Warning: " TERM_RESET);
vprintf(format, args);
printf("\n");
va_end(args);
}
void ui_print_info(const char *format, ...)
{
va_list args;
va_start(args, format);
printf(TERM_CYAN "ℹ " TERM_RESET);
vprintf(format, args);
printf("\n");
va_end(args);
}
void ui_print_header(const char *title)
{
printf("\n");
printf(TERM_BOLD TERM_CYAN "%s" TERM_RESET "\n", title);
printf(TERM_DIM "════════════════════════════════════════════════════════════════\n" TERM_RESET);
}
void ui_print_separator(void)
{
printf(TERM_DIM "────────────────────────────────────────────────────────────────\n" TERM_RESET);
}
void ui_print_category_header(const char *category)
{
printf("\n");
printf(TERM_BOLD TERM_BLUE "── %s " TERM_RESET, category);
printf(TERM_DIM "────────────────────────────────────────────────────────\n" TERM_RESET);
}
// ═══════════════════════════════════════════════════════════════
// Input Functions with Validation
// ═══════════════════════════════════════════════════════════════
bool ui_input_uint8(const char *prompt, uint8_t *value, uint8_t min, uint8_t max)
{
int input;
printf("%s [%u-%u]: ", prompt, min, max);
if (scanf("%d", &input) != 1)
{
while (getchar() != '\n'); // Clear input buffer
ui_print_error("Invalid input");
return false;
}
if (input < min || input > max)
{
ui_print_error("Value out of range (%u-%u)", min, max);
return false;
}
*value = (uint8_t)input;
return true;
}
bool ui_input_uint16(const char *prompt, uint16_t *value, uint16_t min, uint16_t max)
{
int input;
printf("%s [%u-%u]: ", prompt, min, max);
if (scanf("%d", &input) != 1)
{
while (getchar() != '\n');
ui_print_error("Invalid input");
return false;
}
if (input < min || input > max)
{
ui_print_error("Value out of range (%u-%u)", min, max);
return false;
}
*value = (uint16_t)input;
return true;
}
bool ui_input_int8(const char *prompt, int8_t *value, int8_t min, int8_t max)
{
int input;
printf("%s [%d-%d]: ", prompt, min, max);
if (scanf("%d", &input) != 1)
{
while (getchar() != '\n');
ui_print_error("Invalid input");
return false;
}
if (input < min || input > max)
{
ui_print_error("Value out of range (%d-%d)", min, max);
return false;
}
*value = (int8_t)input;
return true;
}
bool ui_input_string(const char *prompt, char *buffer, size_t maxlen)
{
printf("%s (max %zu chars): ", prompt, maxlen - 1);
if (fgets(buffer, maxlen, stdin) == NULL)
{
ui_print_error("Failed to read input");
return false;
}
size_t len = strlen(buffer);
if (len > 0 && buffer[len-1] == '\n')
{
buffer[len-1] = '\0';
}
return true;
}
// ═══════════════════════════════════════════════════════════════
// EEPROM Display Functions
// ═══════════════════════════════════════════════════════════════
void ui_print_field(const void *base, const FieldMetadata *field)
{
const uint8_t *ptr = (const uint8_t*)base + field->offset;
char value_buf[256];
value_buf[0] = '\0';
switch (field->type)
{
case FIELD_TYPE_UINT8:
{
uint8_t value = *(const uint8_t*)ptr;
snprintf(value_buf, sizeof(value_buf), field->format, value);
break;
}
case FIELD_TYPE_UINT16:
{
uint16_t value = *(const uint16_t*)ptr;
if (field->unit)
{
snprintf(value_buf, sizeof(value_buf), "%u %s", value, field->unit);
}
else
{
snprintf(value_buf, sizeof(value_buf), "%u", value);
}
break;
}
case FIELD_TYPE_INT8:
{
int8_t value = *(const int8_t*)ptr;
if (field->unit)
{
snprintf(value_buf, sizeof(value_buf), "%d %s", value, field->unit);
}
else
{
snprintf(value_buf, sizeof(value_buf), "%d", value);
}
break;
}
case FIELD_TYPE_STRING:
{
snprintf(value_buf, sizeof(value_buf), field->format, (const char*)ptr);
break;
}
case FIELD_TYPE_HEX8:
{
uint8_t value = *(const uint8_t*)ptr;
snprintf(value_buf, sizeof(value_buf), "0x%02X", value);
break;
}
case FIELD_TYPE_HEX16:
{
uint16_t value = *(const uint16_t*)ptr;
snprintf(value_buf, sizeof(value_buf), "0x%04X", value);
break;
}
case FIELD_TYPE_VOLTAGE:
{
uint16_t value = *(const uint16_t*)ptr;
snprintf(value_buf, sizeof(value_buf), "%.2f V", value / 100.0f);
break;
}
case FIELD_TYPE_HASHRATE:
{
uint16_t value = *(const uint16_t*)ptr;
snprintf(value_buf, sizeof(value_buf), field->format, value / 100.0f);
break;
}
case FIELD_TYPE_ARRAY_UINT8:
{
if (strcmp(field->name, "ASIC Frequencies") == 0)
{
uint16_t freq_base;
uint8_t freq_step;
size_t array_size = field->size;
const uint8_t *array = (const uint8_t*)ptr;
// Определяем версию EEPROM по offset
if (field->offset == offsetof(EEPROMStructure_v1, sweep_data.sweep_level))
{
// EEPROM v1 (S21+)
const EEPROMStructure_v1 *eeprom = (const EEPROMStructure_v1*)base;
freq_base = eeprom->sweep_data.sweep_freq_base;
freq_step = eeprom->sweep_data.sweep_freq_step;
}
else
{
// EEPROM v4/v5/v6 (S19/S19+)
const EEPROMStructure *eeprom = (const EEPROMStructure*)base;
freq_base = eeprom->sweep_data.sweep_freq_base;
freq_step = eeprom->sweep_data.sweep_freq_step;
}
printf(" " TERM_BOLD "%-27s" TERM_RESET ":", field->name);
if (field->read_only)
{
printf(TERM_DIM " [RO]" TERM_RESET);
}
printf("\n");
// Выводим частоты (каждый байт содержит 2 частоты по 4 бита)
for (size_t i = 0; i < array_size; i++)
{
if (i % 8 == 0)
{
printf(" ");
}
uint8_t v0 = array[i] >> 4; // Старшие 4 бита
uint8_t v1 = array[i] & 0x0F; // Младшие 4 бита
uint16_t freq0 = v0 * freq_step + freq_base;
uint16_t freq1 = v1 * freq_step + freq_base;
printf(" %4u %4u", freq0, freq1);
if ((i % 8) == 7)
{
printf("\n");
}
}
return;
}
else
{
const uint8_t *array = (const uint8_t*)ptr;
int pos = 0;
pos += snprintf(value_buf + pos, sizeof(value_buf) - pos, "[");
for (size_t i = 0; i < field->size && i < 16; i++)
{
if (i > 0) pos += snprintf(value_buf + pos, sizeof(value_buf) - pos, " ");
pos += snprintf(value_buf + pos, sizeof(value_buf) - pos, "0x%02X", array[i]);
}
if (field->size > 16)
{
pos += snprintf(value_buf + pos, sizeof(value_buf) - pos, " ...");
}
snprintf(value_buf + pos, sizeof(value_buf) - pos, "]");
}
break;
}
default:
snprintf(value_buf, sizeof(value_buf), "<unknown type>");
break;
}
printf(" " TERM_BOLD "%-27s" TERM_RESET ": %s", field->name, value_buf);
if (field->read_only)
{
printf(TERM_DIM " [RO]" TERM_RESET);
}
printf("\n");
}
void ui_print_eeprom(const void *eeprom_struct, EEPROMVersion version)
{
size_t field_count;
const FieldMetadata *fields = eeprom_get_fields(version, &field_count);
if (!fields || field_count == 0)
{
ui_print_error("No field metadata for EEPROM version %d", version);
return;
}
char title[64];
if (version == EEPROM_VERSION_V1)
{
snprintf(title, sizeof(title), "EEPROM v1 Structure (Antminer S21+ Series)");
}
else if (version == EEPROM_VERSION_V17)
{
snprintf(title, sizeof(title), "EEPROM v17 Structure (Antminer L Series)");
}
else
{
snprintf(title, sizeof(title), "EEPROM v%d Structure (Antminer S Series)", version);
}
ui_print_header(title);
const char *current_category = NULL;
for (size_t i = 0; i < field_count; i++)
{
const FieldMetadata *field = &fields[i];
if (current_category == NULL || strcmp(current_category, field->category) != 0)
{
ui_print_category_header(field->category);
current_category = field->category;
}
ui_print_field(eeprom_struct, field);
}
printf("\n");
}