-
Notifications
You must be signed in to change notification settings - Fork 36
/
opengamma_pico.ino
1223 lines (966 loc) · 36.1 KB
/
opengamma_pico.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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Open Gamma Detector Sketch
Only works on the Raspberry Pi Pico with arduino-pico!
Triggers on newly detected pulses and measures their energy.
2022, NuclearPhoenix. Open Gamma Project.
https://github.com/OpenGammaProject/Open-Gamma-Detector
## NOTE:
## Only change the highlighted USER SETTINGS below
## except you know exactly what you are doing!
## Flash with default settings and
## Flash Size: "2MB (Sketch: 1984KB, FS: 64KB)"
TODO: (?) Adafruit TinyUSB lib: WebUSB support
TODO: Optimize for power usage (How?)
TODO: cps bar graph while in Geiger Mode instead of empty spectrum
*/
//#include <ADCInput.h> // Special SiPM readout utilizing the ADC FIFO and Round Robin
#include "Helper.h" // Misc helper functions
#include <SimpleShell_Enhanced.h> // Serial Commands/Console
#include <ArduinoJson.h> // Load and save the settings file
#include <LittleFS.h> // Used for FS, stores the settings and debug files
#include <Statistical.h> // Used to get the median for baseline subtraction
/*
BEGIN USER SETTINGS
*/
// These are the default settings that can only be changed by reflashing the Pico
#define SCREEN_TYPE SCREEN_SSD1306 // Display type: Either SCREEN_SSD1306 or SCREEN_SH1106
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SCREEN_ADDRESS 0x3C // See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
#define PH_RESET 1000 // Microseconds after which the P&H circuit will be reset once
#define EVENT_BUFFER 50000 // Buffer this many events for Serial.print
#define TRNG_BITS 8 // Number of bits for each random number, max 8
#define BASELINE_NUM 100 // Number of measurements taken to determine the DC baseline
#define CONFIG_FILE "/config.json" // File to store the settings
#define DEBUG_FILE "/debug.json" // File to store some misc debug info
#define DISPLAY_REFRESH 1000 // Milliseconds between display refreshs
#define BUZZER_PIN 9 // Digital PWM pin the buzzer is connected to
#define BUZZER_FREQ 3000 // Frequency used for the buzzer PWM
#define TICK_RATE 10 // Buzzer clicks once every TICK_RATE counts
struct Config {
// These are the default settings that can also be changes via the serial commands
bool ser_output = true; // Wheter data should be Serial.println'ed
bool geiger_mode = false; // Measure only cps, not energy
bool print_spectrum = false; // Print the finishes spectrum, not just chronological events
size_t meas_avg = 5; // Number of meas. averaged each event, higher=longer dead time
bool enable_display = false; // Enable I2C Display, see settings above
bool trng_enabled = false; // Enable the True Random Number Generator
bool subtract_baseline = false; // Subtract the DC bias from each pulse
bool cps_correction = true; // Correct the cps for the DNL compensation
uint8_t buzzer_tick = 0; // Ticker on-time for one pulse in ms
// Do NOT modify this function:
bool operator==(const Config &other) const {
return (buzzer_tick == other.buzzer_tick && cps_correction == other.cps_correction && ser_output == other.ser_output && geiger_mode == other.geiger_mode && print_spectrum == other.print_spectrum && meas_avg == other.meas_avg && enable_display == other.enable_display && trng_enabled == other.trng_enabled && subtract_baseline == other.subtract_baseline);
}
};
/*
END USER SETTINGS
*/
const String FWVERS = "3.4.0"; // Firmware Version Code
const uint8_t GND_PIN = A2; // GND meas pin
const uint8_t VSYS_MEAS = A3; // VSYS/3
const uint8_t VBUS_MEAS = 24; // VBUS Sense Pin
const uint8_t PS_PIN = 23; // SMPS power save pin
const uint8_t AIN_PIN = A1; // Analog input pin
const uint8_t AMP_PIN = A0; // Preamp (baseline) meas pin
const uint8_t INT_PIN = 16; // Signal interrupt pin
const uint8_t RST_PIN = 22; // Peak detector MOSFET reset pin
const uint8_t LED = 25; // LED on GP25
const uint16_t EVT_RESET_C = 3000; // Number of counts after which the OLED stats will be reset
const uint16_t OUT_REFRESH = 1000; // Milliseconds between serial data outputs
const float VREF_VOLTAGE = 3.0; // ADC reference voltage, defaults 3.3, with reference 3.0
const uint8_t ADC_RES = 12; // Use 12-bit ADC resolution
volatile uint32_t spectrum[uint16_t(pow(2, ADC_RES))]; // Holds the output histogram (spectrum)
volatile uint32_t display_spectrum[uint16_t(pow(2, ADC_RES))]; // Holds the display histogram (spectrum)
volatile uint16_t events[EVENT_BUFFER]; // Buffer array for single events
volatile uint32_t event_position = 0; // Target index in events array
volatile unsigned long start_time = 0; // Time in ms when the spectrum collection has started
volatile unsigned long last_time = 0; // Last time the display has been refreshed
volatile uint32_t last_total = 0; // Last total pulse count for display
volatile unsigned long trng_stamps[3]; // Timestamps for True Random Number Generator
volatile uint8_t random_num = 0b00000000; // Generated random bits that form a byte together
volatile uint8_t bit_index = 0; // Bit index for the generated number
volatile uint32_t trng_nums[1000]; // TRNG number output array
volatile uint16_t number_index = 0; // Amount of saved numbers to the TRNG array
volatile unsigned long dt_sum = 0; // Total detector dead time in µs
volatile uint32_t dt_sum_num = 0; // Number of dead time measurements
uint16_t baselines[BASELINE_NUM]; // Array of a number of baseline (DC bias) measurements at the SiPM input
uint16_t current_baseline = 0; // Median value of the input baseline voltage
volatile bool adc_lock = false; // Locks the ADC if it's currently in use
// Stores 5 * DISPLAY_REFRESH worth of "current" cps to calculate an average cps value in a ring buffer config
float counts_buffer[5] = {};
Config conf; // Configuration object
//ADCInput sipm(AMP_PIN);
// Check for the right display type
#if (SCREEN_TYPE == SCREEN_SH1106)
#include <Adafruit_SH110X.h>
#define DISPLAY_WHITE SH110X_WHITE
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#elif (SCREEN_TYPE == SCREEN_SSD1306)
#include <Adafruit_SSD1306.h>
#define DISPLAY_WHITE SSD1306_WHITE
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#endif
void resetSampleHold(uint8_t time = 2) { // Reset sample and hold circuit
digitalWriteFast(RST_PIN, HIGH);
delayMicroseconds(time); // Discharge for (default) 2 µs -> ~99% discharge time for 1 kOhm and 470 pF
digitalWriteFast(RST_PIN, LOW);
}
void setSerialOutMode(String *args) {
String command = *args;
command.replace("set out", "");
command.trim();
if (command == "spectrum") {
conf.ser_output = true;
conf.print_spectrum = true;
println("Set serial output mode to spectrum histogram.");
} else if (command == "events") {
conf.ser_output = true;
conf.print_spectrum = false;
println("Set serial output mode to events.");
} else if (command == "disable") {
conf.ser_output = false;
conf.print_spectrum = false;
println("Disabled serial outputs.");
} else {
println("Invalid input '" + command + "'.", true);
println("Must be 'spectrum', 'events' or 'disable'.", true);
return;
}
saveSettings();
}
void toggleDisplay(String *args) {
String command = *args;
command.replace("set display", "");
command.trim();
if (command == "enable") {
conf.enable_display = true;
println("Enabled display output. You might need to reboot the device.");
} else if (command == "disable") {
conf.enable_display = false;
println("Disabled display output. You might need to reboot the device.");
} else {
println("Invalid input '" + command + "'.", true);
println("Must be 'enable' or 'disable'.", true);
return;
}
saveSettings();
}
void setTickerTime(String *args) {
String command = *args;
command.replace("set ticker", "");
command.trim();
const long number = command.toInt();
if (number >= 0) {
conf.buzzer_tick = number;
println("Set ticker output to " + String(number) + ".");
println("You might need to reboot the device.");
} else {
println("Invalid input '" + command + "'.", true);
println("Parameter must be a number >= 0.", true);
return;
}
saveSettings();
}
void setMode(String *args) {
String command = *args;
command.replace("set mode", "");
command.trim();
if (command == "geiger") {
conf.geiger_mode = true;
event_position = 0;
println("Enabled geiger mode.");
} else if (command == "energy") {
conf.geiger_mode = false;
event_position = 0;
println("Enabled energy measuring mode.");
} else {
println("Invalid input '" + command + "'.", true);
println("Must be 'geiger' or 'energy'.", true);
return;
}
resetSampleHold();
saveSettings();
}
void toggleTRNG(String *args) {
String command = *args;
command.replace("set trng", "");
command.trim();
if (command == "enable") {
conf.trng_enabled = true;
println("Enabled True Random Number Generator output.");
} else if (command == "disable") {
conf.trng_enabled = false;
println("Disabled True Random Number Generator output.");
} else {
println("Invalid input '" + command + "'.", true);
println("Must be 'enable' or 'disable'.", true);
return;
}
saveSettings();
}
void toggleBaseline(String *args) {
String command = *args;
command.replace("set baseline", "");
command.trim();
if (command == "enable") {
conf.subtract_baseline = true;
println("Enabled automatic DC bias subtraction.");
} else if (command == "disable") {
conf.subtract_baseline = false;
current_baseline = 0; // Reset baseline back to zero
println("Disabled automatic DC bias subtraction.");
} else {
println("Invalid input '" + command + "'.", true);
println("Must be 'enable' or 'disable'.", true);
return;
}
saveSettings();
}
void toggleCPSCorrection(String *args) {
String command = *args;
command.replace("set correction", "");
command.trim();
if (command == "enable") {
conf.cps_correction = true;
println("Enabled CPS correction.");
} else if (command == "disable") {
conf.cps_correction = false;
println("Disabled CPS correction.");
} else {
println("Invalid input '" + command + "'.", true);
println("Must be 'enable' or 'disable'.", true);
return;
}
saveSettings();
}
void setMeasAveraging(String *args) {
String command = *args;
command.replace("set averaging", "");
command.trim();
const long number = command.toInt();
if (number > 0) {
conf.meas_avg = number;
println("Set measurement averaging to " + String(number) + ".");
} else {
println("Invalid input '" + command + "'.", true);
println("Parameter must be a number > 0.", true);
return;
}
saveSettings();
}
void deviceInfo([[maybe_unused]] String *args) {
File debugFile = LittleFS.open(DEBUG_FILE, "r");
DynamicJsonDocument doc(512);
DeserializationError error = deserializeJson(doc, debugFile);
uint32_t power_cycle, power_on;
if (!debugFile || error) {
power_cycle = 0;
power_on = 0;
} else {
power_cycle = doc["power_cycle_count"];
power_on = doc["power_on_hours"];
}
debugFile.close();
println("=========================");
println("-- Open Gamma Detector --");
println("By NuclearPhoenix, Open Gamma Project");
println("2023. https://github.com/OpenGammaProject");
println("Firmware Version: " + FWVERS);
println("=========================");
println("Runtime: " + String(millis() / 1000.0) + " s");
print("Average Dead Time: ");
if (dt_sum_num == 0) {
cleanPrint("no impulses");
} else {
cleanPrint(String(round(float(dt_sum) / float(dt_sum_num)), 0));
}
cleanPrintln(" µs");
const float deadtime_frac = float(dt_sum) / 1000.0 / float(millis()) * 100.0;
println("Total Dead Time: " + String(deadtime_frac) + " %");
println("Total Number Of Impulses: " + String(dt_sum_num));
println("CPU Frequency: " + String(rp2040.f_cpu() / 1e6) + " MHz");
println("Used Heap Memory: " + String(rp2040.getUsedHeap() / 1000.0) + " kB");
println("Free Heap Memory: " + String(rp2040.getFreeHeap() / 1000.0) + " kB");
println("Total Heap Size: " + String(rp2040.getTotalHeap() / 1000.0) + " kB");
println("Temperature: " + String(round(readTemp() * 10.0) / 10.0, 1) + " °C");
println("USB Connection: " + String(digitalRead(VBUS_MEAS)));
const float v = 3.0 * analogRead(VSYS_MEAS) * VREF_VOLTAGE / (pow(2, ADC_RES) - 1);
println("Supply Voltage: " + String(round(v * 10.0) / 10.0, 1) + " V");
print("Power Cycle Count: ");
if (power_cycle == 0) {
cleanPrintln("n/a");
} else {
cleanPrintln(power_cycle);
}
print("Power-on hours: ");
if (power_on == 0) {
cleanPrintln("n/a");
} else {
cleanPrintln(power_on);
}
}
void fsInfo([[maybe_unused]] String *args) {
FSInfo fsinfo;
LittleFS.info(fsinfo);
println("Total Size: " + String(fsinfo.totalBytes / 1000.0) + " kB");
print("Used Size: " + String(fsinfo.usedBytes / 1000.0) + " kB");
cleanPrintln(" / " + String(float(fsinfo.usedBytes) / fsinfo.totalBytes * 100) + " %");
println("Block Size: " + String(fsinfo.blockSize / 1000.0) + " kB");
println("Page Size: " + String(fsinfo.pageSize) + " B");
println("Max Open Files: " + String(fsinfo.maxOpenFiles));
println("Max Path Length: " + String(fsinfo.maxPathLength));
}
void getSpectrumData([[maybe_unused]] String *args) {
for (size_t i = 0; i < pow(2, ADC_RES); i++) {
cleanPrint(String(spectrum[i]) + ";");
}
cleanPrintln();
}
void clearSpectrumData([[maybe_unused]] String *args) {
println("Resetting spectrum...");
clearSpectrum();
//clearSpectrumDisplay();
println("Successfully reset spectrum!");
}
void readSettings([[maybe_unused]] String *args) {
File saveFile = LittleFS.open(CONFIG_FILE, "r");
if (!saveFile) {
println("Could not open save file!", true);
return;
}
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, saveFile);
saveFile.close();
if (error) {
print("Could not load config from json file: ", true);
cleanPrintln(error.f_str());
return;
}
serializeJsonPretty(doc, Serial);
cleanPrintln();
println("Read settings file successfully.");
}
void resetSettings([[maybe_unused]] String *args) {
Config defaultConf; // New Config object with all default parameters
conf = defaultConf;
println("Applied default settings.");
println("You might need to reboot for all changes to take effect.");
saveSettings();
}
void rebootNow([[maybe_unused]] String *args) {
println("You might need to reconnect after reboot.");
println("Rebooting now...");
delay(1000);
rp2040.reboot();
}
void clearSpectrum() {
for (size_t i = 0; i < pow(2, ADC_RES); i++) {
spectrum[i] = 0;
}
}
void clearSpectrumDisplay() {
for (size_t i = 0; i < pow(2, ADC_RES); i++) {
display_spectrum[i] = 0;
}
start_time = millis(); // Spectrum pulse collection has started
last_time = millis();
last_total = 0; // Remove old values
}
void readDebugFile() {
File debugFile = LittleFS.open(DEBUG_FILE, "r");
if (!debugFile) {
println("Could not open debug file!", true);
return;
}
DynamicJsonDocument doc(512);
DeserializationError error = deserializeJson(doc, debugFile);
debugFile.close();
if (error) {
print("Could not load debug info from json file: ", true);
cleanPrintln(error.f_str());
return;
}
serializeJsonPretty(doc, Serial);
cleanPrintln();
println("Read debug file successfully.");
}
void writeDebugFile(bool new_start = false, bool increment = true) {
File debugFile = LittleFS.open(DEBUG_FILE, "r"); // Open read and write
DynamicJsonDocument doc(512);
DeserializationError error = deserializeJson(doc, debugFile);
if (!debugFile || error) {
//println("Could not open debug file!", true);
print("Could not load debug info from json file: ", true);
cleanPrintln(error.f_str());
doc["power_cycle_count"] = 0;
doc["power_on_hours"] = 0;
}
debugFile.close();
if (new_start) {
uint32_t temp = 0;
if (doc.containsKey("power_cycle_count")) {
temp = doc["power_cycle_count"];
}
doc["power_cycle_count"] = ++temp;
}
if (increment) {
uint32_t temp = 0;
if (doc.containsKey("power_on_hours")) {
temp = doc["power_on_hours"];
}
doc["power_on_hours"] = ++temp;
}
debugFile = LittleFS.open(DEBUG_FILE, "w"); // Open read and write
serializeJson(doc, debugFile);
debugFile.close();
}
Config loadSettings(bool msg = true) {
Config new_conf;
File saveFile = LittleFS.open(CONFIG_FILE, "r");
if (!saveFile) {
println("Could not open save file! Creating a fresh file...", true);
writeSettingsFile(); // Force creation of a new file
return new_conf;
}
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, saveFile);
saveFile.close();
if (error) {
print("Could not load config from json file: ", true);
cleanPrintln(error.f_str());
return new_conf;
}
if (doc.containsKey("ser_output")) {
new_conf.ser_output = doc["ser_output"];
}
if (doc.containsKey("geiger_mode")) {
new_conf.geiger_mode = doc["geiger_mode"];
}
if (doc.containsKey("print_spectrum")) {
new_conf.print_spectrum = doc["print_spectrum"];
}
if (doc.containsKey("meas_avg")) {
new_conf.meas_avg = doc["meas_avg"];
}
if (doc.containsKey("enable_display")) {
new_conf.enable_display = doc["enable_display"];
}
if (doc.containsKey("trng_enabled")) {
new_conf.trng_enabled = doc["trng_enabled"];
}
if (doc.containsKey("subtract_baseline")) {
new_conf.subtract_baseline = doc["subtract_baseline"];
}
if (doc.containsKey("cps_correction")) {
new_conf.cps_correction = doc["cps_correction"];
}
if (doc.containsKey("buzzer_tick")) {
new_conf.buzzer_tick = doc["buzzer_tick"];
}
if (msg) {
println("Successfuly loaded settings from flash.");
}
return new_conf;
}
bool writeSettingsFile() {
File saveFile = LittleFS.open(CONFIG_FILE, "w");
if (!saveFile) {
println("Could not open save file!", true);
return false;
}
DynamicJsonDocument doc(1024);
doc["ser_output"] = conf.ser_output;
doc["geiger_mode"] = conf.geiger_mode;
doc["print_spectrum"] = conf.print_spectrum;
doc["meas_avg"] = conf.meas_avg;
doc["enable_display"] = conf.enable_display;
doc["trng_enabled"] = conf.trng_enabled;
doc["subtract_baseline"] = conf.subtract_baseline;
doc["cps_correction"] = conf.cps_correction;
doc["buzzer_tick"] = conf.buzzer_tick;
serializeJson(doc, saveFile);
saveFile.close();
return true;
}
bool saveSettings() {
Config read_conf = loadSettings(false);
if (read_conf == conf) {
//println("Settings did not change... not writing to flash.");
return false;
}
//println("Successfuly written config to flash.");
return writeSettingsFile();
}
void serialEvent() {
Shell.handleEvent(); // Handle the serial input for the USB Serial
}
void serialEvent2() {
Shell.handleEvent(); // Handle the serial input for the Hardware Serial
}
float readTemp() {
adc_lock = true; // Flag this, so that nothing else uses the ADC in the mean time
delayMicroseconds(conf.meas_avg * 100); // Wait for an already-executing interrupt
const float temp = analogReadTemp(VREF_VOLTAGE);
adc_lock = false;
return temp;
}
void drawSpectrum() {
const uint16_t BINSIZE = floor(pow(2, ADC_RES) / SCREEN_WIDTH);
uint32_t eventBins[SCREEN_WIDTH];
uint16_t offset = 0;
uint32_t max_num = 0;
uint32_t total = 0;
for (uint16_t i = 0; i < SCREEN_WIDTH; i++) {
uint32_t totalValue = 0;
for (uint16_t j = offset; j < offset + BINSIZE; j++) {
totalValue += display_spectrum[j];
}
offset += BINSIZE;
eventBins[i] = totalValue;
if (totalValue > max_num) {
max_num = totalValue;
}
total += totalValue;
}
float scale_factor = 0.0;
if (max_num > 0) { // No events accumulated, catch divide by zero
scale_factor = float(SCREEN_HEIGHT - 11) / float(max_num);
}
uint32_t new_total = total - last_total;
last_total = total;
if (millis() < last_time) { // Catch Millis() Rollover
last_time = millis();
return;
}
unsigned long time_delta = millis() - last_time;
last_time = millis();
if (time_delta == 0) { // Catch divide by zero
time_delta = 1000;
}
display.clearDisplay();
display.setCursor(0, 0);
static uint8_t buffer_index = 0;
const uint8_t BUFFER_SIZE = sizeof(counts_buffer) / sizeof(counts_buffer[0]);
counts_buffer[buffer_index] = new_total * 1000.0 / time_delta;
if (buffer_index + 1 >= BUFFER_SIZE) {
buffer_index = 0;
} else {
buffer_index++;
}
float avg_cps = 0;
for (uint8_t i = 0; i < BUFFER_SIZE; i++) {
avg_cps += counts_buffer[i];
}
avg_cps /= BUFFER_SIZE;
display.print(avg_cps, 1);
display.print(" cps");
static int16_t temp = round(readTemp());
if (!adc_lock) { // Only update if ADC is free atm
temp = round(readTemp());
}
if (temp < 0) {
display.setCursor(SCREEN_WIDTH - 36, 0);
} else {
display.setCursor(SCREEN_WIDTH - 30, 0);
}
display.print(temp);
display.print(" ");
display.print((char)247);
display.println("C");
const unsigned long seconds_running = round((millis() - start_time) / 1000.0);
const uint8_t char_offset = floor(log10(seconds_running));
display.setCursor(SCREEN_WIDTH - 18 - char_offset * 6, 8);
display.print(seconds_running);
display.println(" s");
for (uint16_t i = 0; i < SCREEN_WIDTH; i++) {
uint32_t val = round(eventBins[i] * scale_factor);
display.drawFastVLine(i, SCREEN_HEIGHT - val - 1, val, DISPLAY_WHITE);
}
display.drawFastHLine(0, SCREEN_HEIGHT - 1, SCREEN_WIDTH, DISPLAY_WHITE);
display.display();
if (total > EVT_RESET_C) {
clearSpectrumDisplay();
}
}
void drawGeigerCounts() {
uint32_t total = 0;
for (uint16_t i = 0; i < pow(2, ADC_RES); i++) {
total += display_spectrum[i];
}
uint32_t new_total = total - last_total;
last_total = total;
if (millis() < last_time) { // Catch Millis() Rollover
last_time = millis();
return;
}
unsigned long time_delta = millis() - last_time;
last_time = millis();
if (time_delta == 0) { // Catch divide by zero
time_delta = 1000;
}
static uint8_t buffer_index = 0;
const uint8_t BUFFER_SIZE = sizeof(counts_buffer) / sizeof(counts_buffer[0]);
counts_buffer[buffer_index] = new_total * 1000.0 / time_delta;
if (buffer_index + 1 >= BUFFER_SIZE) {
buffer_index = 0;
} else {
buffer_index++;
}
float avg_cps = 0;
for (uint8_t i = 0; i < BUFFER_SIZE; i++) {
avg_cps += counts_buffer[i];
}
avg_cps /= BUFFER_SIZE;
static float max_cps = -1;
static float min_cps = -1;
for (uint8_t i = 0; i < BUFFER_SIZE; i++) {
const float cps = counts_buffer[i];
if (max_cps == -1 || cps > max_cps) {
max_cps = cps;
}
if (min_cps <= 0 || cps < min_cps) {
min_cps = cps;
}
}
display.clearDisplay();
display.setCursor(0, 0);
display.print("Min: ");
display.println(min_cps, 1);
display.print("Max: ");
display.println(max_cps, 1);
static int16_t temp = round(readTemp());
if (!adc_lock) { // Only update if ADC is free atm
temp = round(readTemp());
}
if (temp < 0) {
display.setCursor(SCREEN_WIDTH - 36, 0);
} else {
display.setCursor(SCREEN_WIDTH - 30, 0);
}
display.print(temp);
display.print(" ");
display.print((char)247);
display.println("C");
display.setCursor(0, 0);
display.setTextSize(2);
display.drawFastHLine(0, 18, SCREEN_WIDTH, DISPLAY_WHITE);
display.setCursor(0, 22);
display.print(avg_cps, 1);
display.println(" cps");
display.setTextSize(1);
display.display();
}
void eventInt() {
// Disable interrupt generation for this pin ASAP.
// Directly uses Core0 IRQ Ctrl (core1 does not set the interrupt).
// Thanks a lot to all the replies at
// https://github.com/earlephilhower/arduino-pico/discussions/1397!
static io_rw_32 *addr = &(iobank0_hw->proc0_irq_ctrl.inte[INT_PIN / 8]);
static uint32_t mask1 = 0b1111 << (INT_PIN % 8) * 4u;
hw_clear_bits(addr, mask1);
const unsigned long start = micros();
digitalWriteFast(LED, HIGH); // Enable activity LED
const unsigned long start_millis = millis();
static unsigned long last_tick = start_millis; // Last buzzer tick in ms, not needed with tone()
static uint8_t count = 0;
// Check if ticker is enabled, currently not "ticking" and also catch the millis() overflow
if (conf.buzzer_tick > 0 && (start_millis - last_tick > conf.buzzer_tick || start_millis < last_tick)) {
if (count >= TICK_RATE - 1) { // Only click at every 10th count
tone(BUZZER_PIN, BUZZER_FREQ, conf.buzzer_tick); // Worse at higher cps
last_tick = start_millis;
count = 0;
} else {
count++;
}
}
uint16_t mean = 0;
if (!conf.geiger_mode && !adc_lock) {
uint32_t sum = 0;
uint8_t num = 0;
for (size_t i = 0; i < conf.meas_avg; i++) {
const uint16_t m = analogRead(AIN_PIN);
// Pico-ADC DNL issues, see https://pico-adc.markomo.me/INL-DNL/#dnl
// Discard channels 512, 1536, 2560, and 3584. For now.
// See RP2040 datasheet Appendix B: Errata
if (m == 511 || m == 1535 || m == 2559 || m == 3583) {
//continue; // Discard
break;
}
sum += m;
num++;
}
float avg = 0.0; // Use median instead of average?
if (num > 0) {
avg = float(sum) / float(num);
}
if (current_baseline <= avg) { // Catch negative numbers
// Subtract DC bias from pulse avg and then convert float --> uint16_t ADC channel
mean = round(avg - current_baseline);
}
resetSampleHold();
}
if ((conf.ser_output || conf.enable_display) && (conf.cps_correction || mean != 0 || conf.geiger_mode)) {
events[event_position] = mean;
spectrum[mean] += 1;
display_spectrum[mean] += 1;
if (event_position >= EVENT_BUFFER - 1) { // Increment if memory available, else overwrite array
event_position = 0;
} else {
event_position++;
}
}
if (conf.trng_enabled) {
static uint8_t trng_index = 0; // Timestamp index for True Random Number Generator
// Calculations for the TRNG
trng_stamps[trng_index] = micros();
if (trng_index < 2) {
trng_index++;
} else {
// Catch micros() overflow
if (trng_stamps[1] > trng_stamps[0] && trng_stamps[2] > trng_stamps[1]) {
const uint32_t delta0 = trng_stamps[1] - trng_stamps[0];
const uint32_t delta1 = trng_stamps[2] - trng_stamps[1];
if (delta0 < delta1) {
bitWrite(random_num, bit_index, 0);
} else {
bitWrite(random_num, bit_index, 1);
}
if (bit_index < TRNG_BITS - 1) {
bit_index++;
} else {
trng_nums[number_index] = random_num;
if (number_index < 999) {
number_index++;
} else {
number_index = 0; // Catch overflow
}
random_num = 0b00000000; // Clear number
bit_index = 0;
}
}
trng_index = 0;
}
}
digitalWriteFast(LED, LOW); // Disable activity LED
const unsigned long end = micros();
if (end >= start) { // Catch micros() overflow
// Compute Detector Dead Time
dt_sum += end - start;
dt_sum_num++;
if (dt_sum_num >= 4294967294) { // Catch dead time number overflow 2^32 - 2
dt_sum_num = 0;
dt_sum = 0;
}
}
// Re-enable interrupts
static uint32_t mask2 = 0b0100 << (INT_PIN % 8) * 4u;
hw_set_bits(addr, mask2);
// Clear all interrupts on the executing core
irq_clear(15); // IRQ 15 = SIO_IRQ_PROC0
}
/*
SETUP FUNCTIONS
*/
void setup() {
pinMode(AMP_PIN, INPUT);
pinMode(INT_PIN, INPUT);
pinMode(RST_PIN, OUTPUT_12MA);
pinMode(AIN_PIN, INPUT);
pinMode(LED, OUTPUT);
analogReadResolution(ADC_RES);
resetSampleHold(5); // Reset before enabling the interrupts to avoid jamming
//sipm.setBuffers(4, 64);
//sipm.begin(1000000);
attachInterrupt(digitalPinToInterrupt(INT_PIN), eventInt, FALLING);
start_time = millis(); // Spectrum pulse collection has started
}
void setup1() {
rp2040.wdt_begin(5000); // Enable hardware watchdog to check every 5s
// Disable "Power-Saving" power supply option.
// -> does not actually significantly save power, but output is much less noisy in HIGH!
// -> Also with PS_PIN LOW I have experiences high-pitched (~ 15 kHz range) coil whine!
pinMode(PS_PIN, OUTPUT_4MA);
digitalWrite(PS_PIN, HIGH);