-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathad8317_powermeter.ino
342 lines (285 loc) · 8.47 KB
/
ad8317_powermeter.ino
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
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>
#define DEFAULT_MODE 0
#define DEFAULT_AVERAGING 6
#define DEFAULT_BAND 3
#define AD8317_INPUT A0
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define AVG 0
#define MAX 1
#define MIN 2
#define BTN_SET 10
#define BTN_SELECT 9
#define BTN_GROUND 8
#define CAL_LOW -40
#define CAL_UP -10
#define OLED_RESET -1
#define MAX_ATT 70
#define MODES 3
char *mode_labels[] = { "AVG", "MAX", "MIN" };
#define BANDS 9
char *band_labels[] = { "10M", "20M", "50M", "144", "430", "1G2", "2G4", "5G", "10G" };
// following arrays are filled in by prepare_calib()
uint16_t cal_lower[BANDS], cal_upper[BANDS];
double cal_a[BANDS], cal_b[BANDS];
#define AVERAGE_MODES 10
uint16_t avg_values[] = { 1, 5, 10, 50, 100, 500, 1000, 2000, 5000, 10000};
#define SELECTIONS_AVAIL 6
enum btn_select{SEL_UNITS, SEL_BAND, SEL_MODE, SEL_AVG, SEL_ATT, SEL_NONE, SEL_SAVE, SEL_CAL_LEVEL};
#define SELECTIONS_CAL 5
int calibration_selections[] = { SEL_BAND, SEL_MODE, SEL_AVG, SEL_CAL_LEVEL, SEL_SAVE };
#define UNITS_AVAIL 4
enum measure_units{DBM, RAW, WATT, MWATT};
char *units_labels[] = { "dBm", "RAW", "W", "mW"};
int mode=DEFAULT_MODE, averaging=DEFAULT_AVERAGING, band=DEFAULT_BAND, current_sel=SEL_NONE, units=SEL_UNITS;
int lowest, highest, r, att, display_value, calibration_mode=0, current_sel_cal=SEL_BAND, cal_level=0;
uint32_t cumulative;
char k;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
// power to the OLED @ pin D2
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
delay(100);
// start the ADC
analogReference(EXTERNAL);
Serial.begin(115200);
Serial.println();
// configure buttons
pinMode(BTN_SET, INPUT_PULLUP);
pinMode(BTN_SELECT, INPUT_PULLUP);
// ground for buttons
pinMode(BTN_GROUND, OUTPUT);
digitalWrite(BTN_GROUND, LOW);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
// read eeprom, fill in calibration tables
prepare_calib();
// if booting with any of the buttons pressed, go into CALIB mode
if(!digitalRead(BTN_SET) || !digitalRead(BTN_SELECT)) {
calibration_mode = 1;
units = RAW;
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(4);
display.print("CAL");
display.display();
delay(3000);
}
}
// read values from EEPROM and prepare "a" & "b" coefficients for calibration
void prepare_calib() {
for(int i=0; i<BANDS; i++) {
cal_lower[i] = (EEPROM.read(i*4) << 8) + EEPROM.read(i*4+1); // 10-bit values - split into 2 eeprom bytes
cal_upper[i] = (EEPROM.read(i*4+2) << 8) + EEPROM.read(i*4+3);
cal_a[i] = (float)(CAL_LOW-CAL_UP)/(cal_lower[i]-cal_upper[i]);
cal_b[i] = -((double)cal_lower[i]*cal_a[i]-CAL_LOW);
}
}
// return value in dBm incl. calibration
double get_dbm(int readout) {
return cal_a[band]*readout + cal_b[band] + att;
}
double get_milliwatt(double dbm) {
return pow(10, dbm/10);
}
void invert_text_if_selected(uint8_t value) {
if(current_sel == value) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // invert text color
}
}
void reset_text_color() {
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK); // regular white-on-black
}
void display_all() {
display.clearDisplay();
// print measured value
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
switch(units) {
case DBM:
display.print(get_dbm(display_value), 1);
break;
case RAW:
display.print(display_value);
break;
case WATT:
case MWATT:
double watt_val = get_milliwatt(get_dbm(display_value));
if(units == WATT) watt_val /= 1000;
int val_len = log10(watt_val);
if(val_len < 0) val_len = 0;
display.print(watt_val, 3-val_len);
break;
}
// print units
display.setTextSize(2);
display.setCursor(90, 0);
invert_text_if_selected(SEL_UNITS);
display.print(units_labels[units]);
reset_text_color();
// print band label
display.setTextSize(1);
display.setCursor(110, 16);
invert_text_if_selected(SEL_BAND);
display.print(band_labels[band]);
reset_text_color();
// print measurement mode
display.setCursor(0, 24);
invert_text_if_selected(SEL_MODE);
display.print(mode_labels[mode]);
reset_text_color();
display.print(":");
// print dataset size
display.setCursor(25, 24);
invert_text_if_selected(SEL_AVG);
if(avg_values[averaging] < 1000) {
display.print(avg_values[averaging]);
}
else {
display.print(avg_values[averaging]/1000);
display.print("K");
}
reset_text_color();
// hide ATT when in CALIB mode
if(!calibration_mode) {
// print attenuator value
display.setCursor(48, 24);
display.print("ATT: ");
invert_text_if_selected(SEL_ATT);
display.print(att);
reset_text_color();
}
if(calibration_mode) {
// change calibration level
display.setCursor(48, 24);
invert_text_if_selected(SEL_CAL_LEVEL);
display.print("LV: ");
display.print(cal_level == 0 ? CAL_LOW : CAL_UP);
reset_text_color();
// save calibration
display.setCursor(104, 24);
invert_text_if_selected(SEL_SAVE);
display.print("SAVE");
reset_text_color();
}
display.display();
}
// save current RAW readout to EEPROM
void save_calibration() {
EEPROM.write(band*4 + cal_level*2, (display_value>>8));
EEPROM.write(band*4 + cal_level*2 + 1, (display_value&0xff));
}
// store default calibration values to EEPROM
void store_default_calibration() {
uint16_t default_cal_lower[BANDS] = {631, 629, 630, 628, 625, 610, 580, 563, 563};
uint16_t default_cal_upper[BANDS] = {298, 296, 298, 293, 298, 276, 248, 232, 232};
for(int i=0; i<BANDS; i++) {
EEPROM.write(i*4, default_cal_lower[i] >> 8);
EEPROM.write(i*4+1, default_cal_lower[i] & 0xFF);
EEPROM.write(i*4+2, default_cal_upper[i] >> 8);
EEPROM.write(i*4+3, default_cal_upper[i] & 0xFF);
}
Serial.println("Default calibration values have been loaded.");
prepare_calib();
}
// SET button
void set_action() {
if(current_sel == SEL_MODE) mode = (mode+1)%MODES;
if(current_sel == SEL_BAND) band = (band+1)%BANDS;
if(current_sel == SEL_AVG) averaging = (averaging+1)%AVERAGE_MODES;
if(current_sel == SEL_ATT) att = (att + 10)%MAX_ATT;
if(current_sel == SEL_UNITS) units = (units+1)%UNITS_AVAIL;
if(current_sel == SEL_CAL_LEVEL) cal_level = (cal_level+1)%2;
if(current_sel == SEL_SAVE) save_calibration();
}
// SELECT button
void select_action() {
if(calibration_mode) {
current_sel_cal = (current_sel_cal+1)%SELECTIONS_CAL;
current_sel = calibration_selections[current_sel_cal];
}
else {
current_sel = (current_sel + 1) % SELECTIONS_AVAIL;
}
}
void print_calibration() {
Serial.println("Calibration...");
Serial.print("Lower point: ");
Serial.println(CAL_LOW);
Serial.print("Upper point: ");
Serial.println(CAL_UP);
for(uint8_t i = 0; i<BANDS; i++) {
Serial.print(band_labels[i]);
Serial.print(" L: ");
Serial.print(cal_lower[i]);
Serial.print(" H: ");
Serial.println(cal_upper[i]);
}
}
void serial_parse() {
if(!Serial.available()) { return; }
k = Serial.read();
if(k=='m') mode = (mode+1)%MODES;
if(k=='b') band = (band+1)%BANDS;
if(k=='a') averaging = (averaging+1)%AVERAGE_MODES;
if(k=='t') att = (att + 10)%MAX_ATT;
if(k=='p') print_calibration();
if(k=='d') store_default_calibration();
// simulation of physical buttons
if(k==',') {
select_action();
}
if(k=='.') {
set_action();
}
}
uint8_t handle_buttons() {
if(!digitalRead(BTN_SET)) {
set_action();
while(!digitalRead(BTN_SET)) {}
delay(5);
}
else if(!digitalRead(BTN_SELECT)) {
select_action();
while(!digitalRead(BTN_SELECT)) {}
delay(5);
}
else {
return 0; // return 0 if no key was pressed
}
return 1; // return 1 - key was pressed
}
void loop() {
cumulative = 0;
lowest = 1024;
highest = 0;
// read ADC samples, capture min&max
for(int x=0; x<avg_values[averaging]; x++) {
r = analogRead(AD8317_INPUT);
cumulative += r;
if(r < lowest) lowest = r;
if(r > highest) highest = r;
if(x&0xff == 0xff || x == 0) { // check serial commands + buttons every 256 samples - reduce lag
serial_parse();
if(handle_buttons()) display_all();
}
}
switch(mode) {
case AVG:
display_value = cumulative/avg_values[averaging];
break;
case MAX:
display_value = lowest;
break;
case MIN:
display_value = highest;
break;
}
display_all();
}