forked from hjc2023/ambit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPAM.cpp
More file actions
1597 lines (1308 loc) · 54 KB
/
Copy pathPAM.cpp
File metadata and controls
1597 lines (1308 loc) · 54 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
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
#include "PAM.h"
#include "nvs1.h"
static const char* TAG = "PAM";
static bool measure_temp = true;
static bool PAM_interrupt(bool, bool);
uint8_t adpd_mode = 0;
adpd_current_config_t adpd_current_config;
adpd_gains_config_t adpd_gains_config;
int serial_read_until(uint8_t target1, uint8_t target2 = 0, uint8_t target3 = 0, uint16_t timeout = 20, bool remove = false);
// use pre-set values
int conf_slow_FR_1(void){
if (adpd_gains_config.init == false) ESP_LOGE(TAG, "Gain preset not initized, use default!");
if (adpd_current_config.init == false) ESP_LOGE(TAG, "Current preset not initized, use default!");
return conf_slow_FR_1(adpd_current_config.I620, adpd_current_config.I720, adpd_current_config.IR, adpd_gains_config.Fluo, adpd_gains_config.FluoRef, adpd_gains_config.Sun, adpd_gains_config.Leaf, adpd_gains_config.IR, adpd_gains_config.IRRef);
}
// Slow measurements with 4 time slots
// @param I620 measuring light current (0 - 127)
// @param I730 IR reflection current (0 - 127)
// @param I_FR Far-red treatment current (0 - 127)
// @param G_Fluor fluorescence signal gain (0 - 5)
// @param G_FluorRef fluorescence rerefence gain (0 - 5)
// @param G_Sun Sun facing PD gain (0 - 5)
// @param G_IR Leaf facing PD gain (0 - 5)
// @param G_FR IR reflection signal gain (0 - 5)
// @param G_FRref IR reflection reference gain (0 - 5)
int conf_slow_FR_1(uint8_t I620, uint8_t I730, uint8_t I_FR, uint8_t G_Fluor, uint8_t G_FluorRef, uint8_t G_Sun, uint8_t G_IR, uint8_t G_FR, uint8_t G_FRref){
int32_t config_result = 0;
// Setup timeslot 1: two ambient light channels, 2 x 3 bytes
adpd.led_config.driver1_current = 0;
adpd.led_config.driver2_current = 0;
adpd.SNR_config.TIA_gain_CH2 = G_IR; // channel 2: leaf IR reflection
adpd.SNR_config.TIA_gain_CH1 = G_Sun; // channel 1: sun vis
config_result = adpd.preset_config_1(0, 4);
if (config_result != jii::adpd6000::kOk) return config_result;
// Setup timeslot 2: Fluor and Ref channels, 4 x 3 bytes
// LED 1A = 620nm
adpd.led_config.driver1_current = I620;
adpd.led_config.led1_channel = LED_A;
// LED 2A = 730nm
adpd.led_config.driver2_current = 0;
adpd.led_config.led2_channel = LED_A;
adpd.SNR_config.TIA_gain_CH1 = G_Fluor;
adpd.SNR_config.TIA_gain_CH2 = G_FluorRef;
config_result = adpd.preset_config_2(1, 1);
if (config_result != jii::adpd6000::kOk) return config_result;
// Setup timeslot 3: IR leave reflection, 2 x 3 bytes
// LED 1A = 620nm
adpd.led_config.driver1_current = 0;
adpd.led_config.led1_channel = LED_A;
// LED 2A = 730nm
adpd.led_config.driver2_current = I730;
adpd.led_config.led2_channel = LED_A;
adpd.SNR_config.TIA_gain_CH1 = G_FR;
adpd.SNR_config.TIA_gain_CH2 = G_FRref;
config_result = adpd.preset_config_3(2, 4);
if (config_result != jii::adpd6000::kOk) return config_result;
// Setup timeslot 4-5-6: Far-red illumination, 0 data
// LED 1A = 620nm
adpd.led_config.driver1_current = 0;
adpd.led_config.led1_channel = LED_A;
// LED 2A = 730nm
adpd.led_config.driver2_current = I_FR;
adpd.led_config.led2_channel = LED_A;
//make 6 time slots, ~ 2 ms without repeats, can get 200 repeats ~ 400ms
for (uint8_t slot = 3; slot <= 8; ++slot){
config_result = adpd.preset_config_4(slot);
if (config_result != jii::adpd6000::kOk) return config_result;
}
adpd_mode = ADPD_CONFIG_MODE::ARRAY_MODE1;
return 0;
}
uint32_t arr_line_parse_type1(uint8_t* line, uint8_t* num1, uint16_t* num2, uint16_t* num3, uint8_t* num4, uint8_t* num5, uint16_t* data_count){
// type 1: Must have measurements: Fluor/Ref
// optional measurements: leaf IR, sun VIs, Reflect/Ref
// 0 = no points, 1 = same freq, 2 = every 8
// data_count 0: fluor, 1: ambient, 2: reflection
uint8_t type = line[0]; // run type 1 = steady state, 0 = skip, 2 = no ir
*num1 = line[1]; // FR on / off
*num2 = line[3] + (line[2] << 8); // sample number
*num3 = line[5] + (line[4] << 8); // frequency
*num4 = line[6]; // actinic
*num5 = line[7]; // sub-sampling factor
if (data_count == NULL) return 0;
if ((type == 1) || (type == 2)){
data_count[0] = *num2;
if (line[7] == 0){ // no ambient
data_count[1] = 0;
data_count[2] = 0;
}else if (line[7] == 1){ // every
data_count[1] = *num2;
data_count[2] = *num2;
}else if (line[7] == 2){ // every 8
data_count[1] = (*num2) / 8;
data_count[2] = (*num2) / 8;
}else{
data_count[1] = 0;
data_count[2] = 0;
}
if (type == 2) data_count[2] = 0;
return data_count[0] * 2 + data_count[1] * 2 + data_count[2] * 2;
}
return 0;
}
int run_preprocess_type1(uint8_t length, uint8_t* arr, uint16_t* data_counter){
uint8_t pc = 0;
uint8_t _type = 0;
uint8_t para1, actinic, subsampling = 0;
uint16_t num_ptx = 0, freq = 0;
uint16_t _data_counter[4] = {0};
while (pc < length){
_type = *(arr + pc * 8);
if ((_type == 1) || (_type == 2)){
arr_line_parse_type1(arr + pc * 8, ¶1, &num_ptx, &freq, &actinic, &subsampling, _data_counter);
ESP_LOGV(TAG, "Run type:%d, number:%d, freq: %d, actinic: %d, subfactor %d", _type, num_ptx, freq, actinic, subsampling);
for (uint8_t i = 0; i < 4; i++) data_counter[i] += _data_counter[i];
}
pc += 1;
}
return 0;
}
// Shared output sink for a completed run: stream the 7 channel buffers over the
// active CONNECTION_TYPE. COMPUTER -> ASCII send_serial; AMBYTE -> binary FSM
// (env retried once; sun/leaf/730 only when present). Extracted verbatim from the
// per-run send block so every run path (sweep #4) shares one copy. Wire-touching:
// re-run HW_CONFORMANCE.md after changing it.
static void pam_send_results(dataclass* d_env, dataclass* d_fluor, dataclass* d_fluoRef,
dataclass* d_sun, dataclass* d_leaf, dataclass* d_730,
dataclass* d_730Ref, dataclass* d_timing, uint8_t subsampling,
bool has_730, bool allow_interrupt){
if (CONNECTION_TYPE == CONNECTION_TYPES::COMPUTER){
d_env->send_serial("env");
d_fluor->send_serial("s_630");
d_fluoRef->send_serial("r_630");
d_sun->send_serial("sun");
d_leaf->send_serial("leaf");
d_730->send_serial("s_730");
d_730Ref->send_serial("r_730");
if (d_timing != NULL) d_timing->send_serial("timing");
Serial.println("Data sent");
}else if(CONNECTION_TYPE == CONNECTION_TYPES::AMBYTE){
// Change 4: one wake per run, then every array streams back-to-back as
// (length header + data + trailer); run_esp.cpp's trailing ESP_CMD_END (240)
// terminates the stream. If the ambyte never wakes, there is nothing to send.
if (ambyte_wake(allow_interrupt) != 1) return;
// Per-array element width / dtype (Change 1 self-describing header, Change 2 widths):
// ENV -> int16 centi-degC (elem_width 2, dtype 1)
// ADC ch -> uint16 (16-bit ADPD regs) (elem_width 2, dtype 0, clamped to 0xFFFF)
// TIMING -> uint32 ticks (elem_width 4, dtype 0)
d_env->fsm_send_array(0, 2, 1);
d_fluor->fsm_send_array(1, 2, 0);
d_fluoRef->fsm_send_array(2, 2, 0);
if (subsampling > 0){
d_sun->fsm_send_array(3, 2, 0);
d_leaf->fsm_send_array(4, 2, 0);
if (has_730){
d_730->fsm_send_array(5, 2, 0);
d_730Ref->fsm_send_array(6, 2, 0);
}
}
if (d_timing != NULL) d_timing->fsm_send_array(7, 4, 0); // Change 3: TIMING block
}
}
int run_arr_type1(uint8_t length, uint8_t* arr, bool led_persist){
return run_arr_type1(length, arr, led_persist, false);
}
// ── Async result holder (parallel trigger/poll/fetch protocol) ──────────────
// Holds the eight result buffers of one retained run plus the metadata
// pam_send_results() needs, until the host FETCHes them. One run at a time.
struct ambit_async_result_t {
dataclass *d_env = NULL, *d_fluor = NULL, *d_fluoRef = NULL, *d_sun = NULL,
*d_leaf = NULL, *d_730 = NULL, *d_730Ref = NULL, *d_timing = NULL;
uint8_t subsampling = 0;
bool has_730 = false;
bool allow_interrupt = false;
uint8_t state = AMBIT_ASYNC_IDLE;
};
static ambit_async_result_t g_async;
void ambit_async_clear(void){
delete g_async.d_env; delete g_async.d_fluor; delete g_async.d_fluoRef;
delete g_async.d_sun; delete g_async.d_leaf; delete g_async.d_730;
delete g_async.d_730Ref; delete g_async.d_timing;
g_async = ambit_async_result_t(); // re-init all pointers to NULL, state IDLE
}
uint8_t ambit_async_get_state(void){ return g_async.state; }
// openJII env channel: fw_new stores leaf temp as centi-degC in the low 16 bits.
// Used only on the json_output path (arrun via the JSON envelope).
static void send_env_json(dataclass* d){
Serial.print("\"env\":[");
if (d->available){
uint16_t n = d->get_length();
for (uint16_t i = 0; i < n; i++){
uint32_t raw = d->pop();
float temp = (int16_t)(raw & 0xFFFF) / 100.0f;
if (i > 0) Serial.print(',');
Serial.printf("{\"temp_c\":%.2f}", temp);
}
}
Serial.print(']');
}
// openJII derived channel: fluo = s_630 / r_630 (signal / reference), one float
// per sample. Computed on-device only for the JSON path because the openJII sink
// consumes the JSON verbatim and does no math. MUST run BEFORE d_fluor / d_fluoRef are
// drained by send_json(): it reads arr[] directly and relies on read_ptr == 0 (nothing
// popped yet). den == 0 -> 0 (calc_signal floors the reference at 0 when dark dominates).
static void send_fluo_json(dataclass* num, dataclass* den){
Serial.print("\"fluo\":[");
if (num->available && den->available){
uint16_t n = num->get_length();
uint16_t m = den->get_length();
if (m < n) n = m; // defensive: emit only paired samples
for (uint16_t i = 0; i < n; i++){
if (i > 0) Serial.print(',');
uint32_t d = den->arr[i];
if (d == 0) Serial.print('0');
else Serial.printf("%.5f", (double)num->arr[i] / (double)d);
}
}
Serial.print(']');
}
int run_arr_type1(uint8_t length, uint8_t* arr, bool led_persist, bool allow_interrupt, bool json_output, bool retain){
if (adpd_mode != ADPD_CONFIG_MODE::ARRAY_MODE1){
conf_slow_FR_1();
ESP_LOGW(TAG, "Run array was not configured!");
}
// set to max possible bytes
uint8_t expected_readout = 8;
uint8_t expected_readout_bytes = expected_readout * 3;
const uint8_t num_integration = 1;
const unsigned int start_t0 = millis();
const int64_t run_tick_begin = esp_timer_get_time(); // Change 3: TIMING - us at run start
// Run protocol preprecess, get storage size
uint16_t data_count[] = {0, 0, 0, 0};
if (run_preprocess_type1(length, arr, data_count) == -1) return -1; // calculate data counts
ESP_LOGV(TAG, "Sample & Ref: %d, optionals: %d", data_count[0], data_count[1]);
dataclass *d_env = new dataclass; //
dataclass *d_fluor = new dataclass; // fluorescence signal
dataclass *d_fluoRef = new dataclass; // fluorescence reference
dataclass *d_sun = new dataclass; // sun-side ambient
dataclass *d_leaf = new dataclass; // leaf-side ambient
dataclass *d_730 = new dataclass; // 730nm reflectance signal
dataclass *d_730Ref = new dataclass; // 730nm reference
dataclass *d_timing = new dataclass; // Change 3: [tick_begin, tick_end] controller ticks (us)
if (!(d_env->init(512))) return -1;
if (!(d_timing->init(2))) return -1;
if (!( (d_fluor->init(data_count[0])) && (d_fluoRef->init(data_count[0])) )) return -1;
if (data_count[1] > 0){
if (!( (d_sun->init(data_count[1])) && (d_leaf->init(data_count[1])) )) return -1;}
if (data_count[2] > 0){
if (!( (d_730->init(data_count[2])) && (d_730Ref->init(data_count[2])) )) return -1;
}
ESP_LOGV(TAG, "Memory allocation completed");
// variables for each trace
uint8_t pc = 0;
uint8_t _type = 0;
uint8_t farred = 0, actinic = 0, subsampling = 0;
uint16_t num_ptx = 0, freq = 0;
// data counter and buffer
// [sun-amb, leaf-ir, lit_leaf-ir, dark_leaf-ir, lit_leaf-ref, dark_leaf-ref]
uint32_t ret[expected_readout] = {0};
uint32_t counter = 0, ploter1 = 0, ploter2 = 0;
uint16_t fifo_c = 0;
uint8_t watch_dog_timer = 0;
int32_t tmp_var = 0;
uint32_t buf_opt[4] = {0};
uint32_t _tmparr = 0;
uint8_t _repeats = 1;
uint32_t light_sleep_time = 1;
float_t leaf_temp = 0.0;
unsigned int env_timer1 = millis();
bool measure_temperature = false;
bool interrupt_run = false;
_tmparr = PAM_get_env(4, start_t0);
d_env->put(_tmparr);
leaf_temp = (int16_t) (_tmparr & 0xFFFF) / 100.0;
adpd.STOP();
while (pc < length){
if (interrupt_run) break;
_type = *(arr + pc * 8); //get line type
if ((_type == 1) || (_type == 2)){ // all channels
arr_line_parse_type1((arr + pc * 8), &farred, &num_ptx, &freq, &actinic, &subsampling, NULL);
adpd.run_freq(freq);
adpd.clear_fifo();
light_sleep_time = (1000/freq);
// JSON path: exactly ONE env temperature per array, sampled once at the start
// (the unconditional d_env->put() before this loop). No in-run 2 s resampling, so
// env is a single {"temp_c":..} for every array — fast or slow. All other paths
// keep the original cadence: that env stream is part of the FROZEN wire contract
// with the datalogger, so its bytes must not change. Runtime branch on
// json_output (was compile-time VARIANT_CLOUD) — same semantics per caller.
if (json_output) measure_temperature = false;
else measure_temperature = (light_sleep_time > 20) && measure_temp && actinic < 50;
if (_type == 1){ // use IR reflect
if (farred == 1){ // whether use actinic IR
adpd.num_ts(9);
_repeats = int(400/freq);
if (freq < 3) _repeats = 250;
if (_repeats == 0) _repeats = 1;
for (uint8_t i = 3; i < 9;i++) adpd.repeats_only(i, 1, _repeats);
}
else{
adpd.num_ts(3);
}
expected_readout = 8;
expected_readout_bytes = expected_readout * 3;
}else{ // NO IR
adpd.num_ts(2);
expected_readout = 6;
expected_readout_bytes = expected_readout * 3;
}
if (actinic > 3){
AS_LED_Current(actinic);
AS_LED_ON();
}else{
AS_LED_OFF();
AS_LED_Current(0);
}
counter = 0;
for (uint8_t i = 0; i < 4; i++) buf_opt[i] = 0;
adpd.RUN();
delay(2);
while (counter < num_ptx){
if (interrupt_run) break;
fifo_c = adpd.fifo_count();
while (fifo_c >= expected_readout_bytes){ // read all bytes from FIFO
adpd.readfifo(expected_readout, 3, ret);
fifo_c -= expected_readout_bytes;
if (counter == num_ptx) break;
// 0: sun-vis; 1: leaf-ir; 2: fluoS_dark; 3: fluoS_lit; 4: fluoR_dark; 5: fluoR_lit; 6: Reflect_signal; 7: reflect_ref
// Apply each stored baseline exactly once at the result boundary.
const uint32_t calibrated_fluor = apply_adpd_calibration(
ambit_calibration::S630, calc_signal(ret[2], ret[3], num_integration));
const uint32_t calibrated_fluo_ref = apply_adpd_calibration(
ambit_calibration::R630, calc_signal(ret[4], ret[5], num_integration));
const uint32_t calibrated_sun = apply_adpd_calibration(ambit_calibration::SUN, ret[0]);
const uint32_t calibrated_leaf = apply_adpd_calibration(ambit_calibration::LEAF, ret[1]);
const uint32_t calibrated_730 = _type == 1
? apply_adpd_calibration(ambit_calibration::S730, ret[6]) : 0;
const uint32_t calibrated_730_ref = _type == 1
? apply_adpd_calibration(ambit_calibration::R730, ret[7]) : 0;
d_fluor->put(calibrated_fluor);
d_fluoRef->put(calibrated_fluo_ref);
// save option data
if (subsampling > 0){
if (subsampling == 1){ // every point
d_sun->put(calibrated_sun);
d_leaf->put(calibrated_leaf);
if (_type == 1){d_730->put(calibrated_730);d_730Ref->put(calibrated_730_ref);} // ir enabled
}else if (subsampling == 2){
buf_opt[0] += calibrated_sun;
buf_opt[1] += calibrated_leaf;
if (_type == 1){
buf_opt[2] += calibrated_730;
buf_opt[3] += calibrated_730_ref;
}
if (counter % 8 == 7){
d_sun->put(buf_opt[0]/8);
d_leaf->put(buf_opt[1]/8);
if(_type == 1){d_730->put(buf_opt[2]/8);d_730Ref->put(buf_opt[3]/8);}
for (uint8_t i = 0; i < 4; i++) buf_opt[i] = 0;
}
}
}
if (CONNECTION_TYPE == CONNECTION_TYPES::PLOTTING){
ploter1 = calibrated_fluor;
ploter2 = calibrated_fluo_ref;
if (ploter2 == 0){
ploter2 = 1;
ploter1 = 0;
}
if (_type == 1) {
Serial.printf("T:%2.3f,F:%3.4f,S:%d,R:%d,7:%d,7R:%d,Sun:%d,L:%d\n", leaf_temp, (float)ploter1/(float)ploter2, ploter1, ploter2, calibrated_730, calibrated_730_ref, calibrated_sun, calibrated_leaf);
}else if (_type == 2){
Serial.printf("T:%2.3f,F:%3.4f,S:%d,R:%d,Sun:%d,L:%d\n", leaf_temp, (float)ploter1/(float)ploter2, ploter1, ploter2, calibrated_sun, calibrated_leaf); }
Serial.flush();
}
counter++;
watch_dog_timer = 0;
}
// do light sleep
//esp_sleep_enable_timer_wakeup(1000);
if (counter + 10 < num_ptx){ // a lot of measurements
// do temperature measurement?
if (measure_temperature && (millis() - env_timer1 > 2000)){
_tmparr = PAM_get_env(4, start_t0);
d_env->put(_tmparr);
leaf_temp = (int16_t) (_tmparr & 0xFFFF) / 100.0;
env_timer1 = millis();
esp_sleep_enable_timer_wakeup(1000);
}
else esp_sleep_enable_timer_wakeup(light_sleep_time * 8000);
}else if (counter + 2 < num_ptx){ // not many
esp_sleep_enable_timer_wakeup(light_sleep_time * 1000);
}else{
esp_sleep_enable_timer_wakeup(1000);
}
// run interrupted by serial input "S"
interrupt_run = PAM_interrupt(allow_interrupt, false);
if (!interrupt_run) esp_light_sleep_start();
interrupt_run = PAM_interrupt(allow_interrupt, true);
}
adpd.STOP();
if (!led_persist) AS_LED_OFF();
digitalWrite(1, LOW);
}
pc += 1;
}
if (interrupt_run){
digitalWrite(STF_FLASH_PIN, LOW);
adpd.STOP();
AS_LED_OFF();
};
if (json_output) { // one JSON object: {"env":[..],"fluo":[..],"s_630":[..],...}
Serial.print('{');
send_env_json(d_env);
// fluo first: it reads d_fluor/d_fluoRef before send_json() drains them.
Serial.print(','); send_fluo_json(d_fluor, d_fluoRef);
Serial.print(','); d_fluor->send_json("s_630");
Serial.print(','); d_fluoRef->send_json("r_630");
if (d_sun->available) { Serial.print(','); d_sun->send_json("sun"); }
if (d_leaf->available) { Serial.print(','); d_leaf->send_json("leaf"); }
if (d_730->available) { Serial.print(','); d_730->send_json("s_730"); }
if (d_730Ref->available) { Serial.print(','); d_730Ref->send_json("r_730"); }
Serial.print('}');
} else {
d_timing->put((uint32_t) run_tick_begin); // Change 3: run start tick (us)
d_timing->put((uint32_t) esp_timer_get_time()); // run end tick (us)
if (retain){
// Parallel protocol: transfer ownership of the buffers to the async holder
// instead of streaming + freeing. The host fetches them later (cmd 24).
g_async.d_env = d_env; g_async.d_fluor = d_fluor; g_async.d_fluoRef = d_fluoRef;
g_async.d_sun = d_sun; g_async.d_leaf = d_leaf; g_async.d_730 = d_730;
g_async.d_730Ref = d_730Ref; g_async.d_timing = d_timing;
g_async.subsampling = subsampling; g_async.has_730 = (data_count[2] > 0);
g_async.allow_interrupt = allow_interrupt;
return 0; // holder owns the buffers now — do NOT delete
}
pam_send_results(d_env, d_fluor, d_fluoRef, d_sun, d_leaf, d_730, d_730Ref,
d_timing, subsampling, data_count[2] > 0, allow_interrupt);
}
delete d_fluor;
delete d_fluoRef;
delete d_sun;
delete d_leaf;
delete d_730;
delete d_730Ref;
delete d_env;
delete d_timing;
return 0;
}
// ── Async run-start / fetch (parallel trigger/poll/fetch protocol) ──────────
// Blocks to completion (like the synchronous run) but retains the results, so
// the host can trigger every sensor back-to-back and collect them afterwards.
int ambit_async_run_start(uint8_t length, uint8_t* arr, bool led_persist, bool allow_interrupt){
ambit_async_clear(); // drop any stale, un-fetched result first
int rc = run_arr_type1(length, arr, led_persist, allow_interrupt, false, true /*retain*/);
if (rc == 0 && g_async.d_env != NULL){
g_async.state = AMBIT_ASYNC_DONE;
} else {
ambit_async_clear(); // partial/failed run — free whatever stuck
g_async.state = AMBIT_ASYNC_ERROR;
}
return rc;
}
// Stream the retained arrays to the ambyte over the existing AMBYTE FSM, then
// free them. Caller (cmd 24) has already written ESP_CMD_DONE and will write
// ESP_CMD_END after this returns.
int ambit_async_fetch(void){
if (g_async.state != AMBIT_ASYNC_DONE || g_async.d_env == NULL) return -1;
uint8_t saved = CONNECTION_TYPE;
CONNECTION_TYPE = CONNECTION_TYPES::AMBYTE;
pam_send_results(g_async.d_env, g_async.d_fluor, g_async.d_fluoRef, g_async.d_sun,
g_async.d_leaf, g_async.d_730, g_async.d_730Ref, g_async.d_timing,
g_async.subsampling, g_async.has_730, g_async.allow_interrupt);
CONNECTION_TYPE = saved;
ambit_async_clear();
return 0;
}
void adpd_trigger(void){
digitalWrite(10, HIGH);
delayMicroseconds(1);
digitalWrite(10, LOW);
}
int run_trigger_spacer(uint16_t length, uint8_t interval, bool change_act, uint8_t act, bool interrrupt){
if (length > 3000) return -1;
adpd.STOP();
if (adpd_mode != ADPD_CONFIG_MODE::ARRAY_MODE1) conf_slow_FR_1();
digitalWrite(10, LOW);
adpd.gpio_config.GPIO0_cfg = 1;
adpd.gpio_config.SYNC_GPIO = 0;
adpd.gpio_config.EXT_SYNC_EN = 1;
adpd.gpio_setup(&(adpd.gpio_config));
adpd_mode = ADPD_CONFIG_MODE::ARRAY_SLOW;
gpio_sleep_set_direction(GPIO_NUM_10, GPIO_MODE_OUTPUT);
gpio_sleep_set_pull_mode(GPIO_NUM_10, GPIO_PULLDOWN_ONLY);
// set to max possible bytes
uint8_t expected_readout = 8;
uint8_t expected_readout_bytes = expected_readout * 3;
const uint32_t _wait_time_ms = interval * 100;
const uint8_t num_integration = 1;
const unsigned int start_t0 = millis();
const int64_t run_tick_begin = esp_timer_get_time(); // Change 3: TIMING - us at run start
// data counter and buffer
// [sun-amb, leaf-ir, lit_leaf-ir, dark_leaf-ir, lit_leaf-ref, dark_leaf-ref]
uint32_t ret[expected_readout] = {0};
uint16_t fifo_c = 0;
uint8_t watch_dog_timer = 0;
int32_t tmp_var = 0;
uint32_t buf_opt[4] = {0};
uint32_t _tmparr = 0;
uint32_t read_fluor, read_fluoRef, read_sun, read_leaf, read_7, read_7Ref;
float_t leaf_temp = 0.0;
unsigned int env_timer1 = millis(), trigger_timer = 0, expected_millis = 0;
int waiting_time = 0;
bool measure_temperature = false;
bool interrupt_run = false;
esp_sleep_enable_timer_wakeup(90000);
dataclass *d_env = new dataclass; //
dataclass *d_fluor = new dataclass; // fluorescence signal
dataclass *d_fluoRef = new dataclass; // fluorescence reference
dataclass *d_sun = new dataclass; // sun-side ambient
dataclass *d_leaf = new dataclass; // leaf-side ambient
dataclass *d_730 = new dataclass; // 730nm reflectance signal
dataclass *d_730Ref = new dataclass; // 730nm reference
dataclass *d_timing = new dataclass; // Change 3: [tick_begin, tick_end] controller ticks (us)
int _func_ret = -1;
if (!(d_env->init(512))) goto del_classes;
if (!(d_timing->init(2))) goto del_classes;
if (!( (d_fluor->init(length)) && (d_fluoRef->init(length)) )) goto del_classes;
if (!( (d_sun->init(length)) && (d_leaf->init(length)) )) goto del_classes;
if (!( (d_730->init(length)) && (d_730Ref->init(length)) )) goto del_classes;
_tmparr = PAM_get_env(4, start_t0);
d_env->put(_tmparr);
leaf_temp = (int16_t) (_tmparr & 0xFFFF) / 100.0;
env_timer1 = millis();
adpd.clear_fifo();
adpd.run_freq(10);
adpd.num_ts(3);
adpd.RUN();
delay(5);
if(change_act){
if (act == 0) AS_LED_OFF();
if (act > 0) {
AS_LED_Current(act);
AS_LED_ON();
}
}
for (uint16_t n = 0; n < length; n++){
fifo_c = 0;
adpd_trigger();
trigger_timer = millis();
expected_millis = trigger_timer + _wait_time_ms;
delay(1);
while (fifo_c != expected_readout_bytes){
fifo_c = adpd.fifo_count();
if (fifo_c >= expected_readout_bytes) break;
if (millis() - trigger_timer > 100) break;
}
if (fifo_c < expected_readout_bytes){
ESP_LOGE(TAG, "NOT ENOUGH IN FIFO");
break;
}
adpd.readfifo(expected_readout, 3, ret);
if (fifo_c > expected_readout_bytes){
adpd.clear_fifo();
ESP_LOGE(TAG, "Extra %d byte in FIFO", fifo_c - expected_readout_bytes);
}
read_fluor = apply_adpd_calibration(ambit_calibration::S630, calc_signal(ret[2], ret[3], num_integration)); d_fluor->put(read_fluor);
read_fluoRef = apply_adpd_calibration(ambit_calibration::R630, calc_signal(ret[4], ret[5], num_integration)); d_fluoRef->put(read_fluoRef);
read_sun = apply_adpd_calibration(ambit_calibration::SUN, ret[0]); d_sun->put(read_sun);
read_leaf = apply_adpd_calibration(ambit_calibration::LEAF, ret[1]); d_leaf->put(read_leaf);
read_7 = apply_adpd_calibration(ambit_calibration::S730, ret[6]); d_730->put(read_7);
read_7Ref = apply_adpd_calibration(ambit_calibration::R730, ret[7]); d_730Ref->put(read_7Ref);
if (CONNECTION_TYPE == CONNECTION_TYPES::PLOTTING){
Serial.printf("T:%2.3f,F:%3.4f,S:%d,R:%d,7:%d,7R:%d,Sun:%d,L:%d\n", leaf_temp, (float)read_fluor/(float)read_fluoRef, read_fluor, read_fluoRef, read_7, read_7Ref, read_sun, read_leaf);
Serial.flush();
}
if (interrupt_run) break;
if (millis() > expected_millis) continue; // overdue
waiting_time = expected_millis - millis();
if ((n % 8 == 7) && (waiting_time > 100)){
if (millis() - env_timer1 > 2000){
_tmparr = PAM_get_env(4, start_t0);
d_env->put(_tmparr);
leaf_temp = (int16_t) (_tmparr & 0xFFFF) / 100.0;
env_timer1 = millis();
}
}
waiting_time = expected_millis - millis();
while (waiting_time > 250){
esp_sleep_enable_timer_wakeup((waiting_time - 50) * 1000);
interrupt_run = PAM_interrupt(interrrupt, false);
if (!interrupt_run) esp_light_sleep_start();
interrupt_run = PAM_interrupt(interrrupt, true);
if (interrupt_run) break;
waiting_time = expected_millis - millis();
}
if (interrupt_run) break;
waiting_time = expected_millis - millis();
if (waiting_time > 1) delay(waiting_time);
} /// End of Loop
adpd.STOP();
adpd.clear_fifo();
// Trigger-spacer always produces the full 7-channel set (subsampling=1, has_730=true),
// sent unconditionally. This path never runs as COMPUTER, so pam_send_results' COMPUTER
// branch is inert here; the AMBYTE branch is identical to the former inline block.
d_timing->put((uint32_t) run_tick_begin); // Change 3: run start tick (us)
d_timing->put((uint32_t) esp_timer_get_time()); // run end tick (us)
pam_send_results(d_env, d_fluor, d_fluoRef, d_sun, d_leaf, d_730, d_730Ref,
d_timing, 1, true, interrrupt);
_func_ret = 0;
del_classes:
delete d_fluor;
delete d_fluoRef;
delete d_sun;
delete d_leaf;
delete d_730;
delete d_730Ref;
delete d_env;
delete d_timing;
adpd.gpio_config.GPIO0_cfg = 0;
adpd.gpio_config.EXT_SYNC_EN = 0;
adpd.gpio_setup(&(adpd.gpio_config));
adpd_mode = ADPD_CONFIG_MODE::ARRAY_MODE1;
return _func_ret;
}
int external_trigger_run(void){
adpd.STOP();
conf_slow_FR_1();
adpd.led_config.driver1_current = adpd_current_config.I620;
adpd.led_config.led1_channel = LED_A;
adpd.led_config.driver2_current = 0;
adpd.led_config.led2_channel = LED_A;
adpd.SNR_config.TIA_gain_CH1 = adpd_gains_config.Fluo;
adpd.SNR_config.TIA_gain_CH2 = adpd_gains_config.FluoRef;
adpd.preset_config_2(1, 4);
digitalWrite(10, LOW);
adpd.gpio_config.GPIO0_cfg = 1;
adpd.gpio_config.SYNC_GPIO = 0;
adpd.gpio_config.EXT_SYNC_EN = 1;
adpd.gpio_setup(&(adpd.gpio_config));
// set to max possible bytes
uint8_t expected_readout = 6, num_integration = 4;
uint8_t expected_readout_bytes = expected_readout * 3;
// data counter and buffer
// [sun-amb, leaf-ir, lit_leaf-ir, dark_leaf-ir, lit_leaf-ref, dark_leaf-ref]
uint32_t ret[expected_readout] = {0};
uint16_t fifo_c = 0;
uint32_t read_fluor, read_fluoRef, read_sun, read_leaf;
float_t leaf_temp = 0.0;
adpd.clear_fifo();
adpd.run_freq(10);
adpd.num_ts(2);
adpd.RUN();
delay(5);
uint8_t unknown_input_counter = 0;
unsigned int watchdog_timer = millis(), trigger_timer = 0, start_timer = millis(), temp_timer = millis();
bool keep_running = true, do_measure = false, change_act = false;
double obj_T, chip_T;
char c, c1, c2;
Serial.println("Run");
while(keep_running){
while (Serial.available() == 0){
if (millis() - watchdog_timer > 30000){
keep_running = false;
break;
}
delayMicroseconds(200);
}
if (!keep_running) break;
while (Serial.available() > 0){
c = Serial.read();
if (c == 'G'){
do_measure = true;
watchdog_timer = millis();
}else if(c == 'E'){
do_measure = false;
keep_running = false;
}else if(c == 'A'){
delay(1);
c1 = 0;
if (Serial.available() > 0){
c1 = Serial.read();
change_act = true;
}
}else if(c == 'T'){
temp_timer = millis();
mlx_measure(&obj_T, &chip_T);
Serial.printf("T:%d,o:%.3f,a:%.3f,d:%d\n", millis() - start_timer, obj_T, chip_T, millis() - temp_timer);
Serial.flush();
watchdog_timer = millis();
continue;
}
else{
unknown_input_counter += 1;
if (unknown_input_counter > 200) keep_running = false;
}
}
if (!keep_running) break;
if (change_act){
change_act = false;
if (c1 > 3){
AS_LED_Current(c1);
AS_LED_ON();
}else{
AS_LED_OFF();
}
}
if (!do_measure) continue;
fifo_c = 0;
adpd_trigger();
do_measure = false;
trigger_timer = millis();
delay(1);
while (fifo_c != expected_readout_bytes){
fifo_c = adpd.fifo_count();
if (fifo_c >= expected_readout_bytes) break;
if (millis() - trigger_timer > 100) break;
}
if (fifo_c < expected_readout_bytes){
Serial.println("NOT ENOUGH IN FIFO");
break;
}
adpd.readfifo(expected_readout, 3, ret);
if (fifo_c > expected_readout_bytes){
adpd.clear_fifo();
Serial.printf("Extra %d byte in FIFO", fifo_c - expected_readout_bytes);
}
read_fluor = apply_adpd_calibration(ambit_calibration::S630, calc_signal(ret[2], ret[3], num_integration));
read_fluoRef = apply_adpd_calibration(ambit_calibration::R630, calc_signal(ret[4], ret[5], num_integration));
read_sun = apply_adpd_calibration(ambit_calibration::SUN, ret[0]);
read_leaf = apply_adpd_calibration(ambit_calibration::LEAF, ret[1]);
Serial.printf("T:%d,S:%d,R:%d,F:%d,B:%d\n", millis() - start_timer, read_fluor, read_fluoRef, read_sun, read_leaf);
Serial.flush();
} /// End of Loop
adpd.STOP();
adpd.clear_fifo();
adpd.gpio_config.GPIO0_cfg = 0;
adpd.gpio_config.EXT_SYNC_EN = 0;
adpd.gpio_setup(&(adpd.gpio_config));
AS_LED_Current(0);
AS_LED_OFF();
Serial.println("Stop");
return 0;
}
int external_trigger_run_Flash(unsigned int gate_time, unsigned int dt, const uint16_t num){
adpd.STOP();
const uint8_t _NUM_TS = 8;
adpd.led_config.driver1_current = 80;
adpd.led_config.led1_channel = LED_A;
// LED 2A = 730nm
adpd.led_config.driver2_current = 0;
adpd.led_config.led2_channel = LED_A;
adpd.SNR_config.TIA_gain_CH1 = 1;
adpd.SNR_config.TIA_gain_CH2 = 5;
for (uint8_t i = 0; i < 12; i++){
adpd.preset_config_ext_fast(i, 2);
}
digitalWrite(STF_FLASH_PIN, LOW);
digitalWrite(10, LOW);
adpd.gpio_config.GPIO0_cfg = 1;
adpd.gpio_config.SYNC_GPIO = 0;
adpd.gpio_config.EXT_SYNC_EN = 1;
adpd.gpio_setup(&(adpd.gpio_config));
AS_LED_OFF();
AS_LED_Current(dt);
// set to max possible bytes
uint8_t expected_readout = _NUM_TS * 4, num_integration = 2;
uint8_t expected_readout_bytes = expected_readout * 3;
// data counter and buffer
uint32_t ret[expected_readout] = {0};
uint16_t fifo_c = 0;
uint32_t read_fluor, read_fluoRef, read_sun, read_leaf;
float_t leaf_temp = 0.0;
adpd.clear_fifo();
adpd.run_freq(10);
adpd.num_ts(_NUM_TS);
adpd.RUN();
delay(5);
uint16_t _counter = 0;
unsigned int watchdog_timer = millis(), trigger_timer = 0, start_timer = millis(), temp_timer = millis();
bool keep_running = true, do_measure = false, change_act = false;
double obj_T, chip_T;
char c, c1, c2;
dataclass *d_fluor = new dataclass; // fluorescence signal
dataclass *d_fluoRef = new dataclass; // fluorescence reference
Serial.println("Run");
unsigned long timer1 = micros();
unsigned long timer2 = micros();
unsigned int flash_duration = expected_readout;
uint16_t _num_sample = num < 20 ? num * _NUM_TS : 20 * _NUM_TS + (num - 20);
if (!( (d_fluor->init(_num_sample + 1)) && (d_fluoRef->init(_num_sample + 1)) )) goto del_classes;
while(keep_running){
_counter += 1;
if (_counter > num) break;
if (!keep_running) break;
fifo_c = 0;
//if ((_counter == 5) && (dt > 0)) AS_LED_ON();
adpd_trigger();
timer1 = micros();
delayMicroseconds(1000);
fifo_c = adpd.fifo_count();
while (fifo_c != expected_readout_bytes){
fifo_c = adpd.fifo_count();
if (fifo_c >= expected_readout_bytes) break;
if (micros() - timer1 > 10000) break;
}
//Serial.printf("%ld: %d\n", micros() - timer1, fifo_c);
if (fifo_c < expected_readout_bytes){
Serial.printf("NOT ENOUGH IN FIFO: %d < %d\n", fifo_c, expected_readout_bytes);
break;
}
if ((gate_time > 0) && (flash_duration > 0) && (_counter > 2)) digitalWrite(STF_FLASH_PIN, HIGH);
//if ((_counter == 15) && (dt > 0)) AS_LED_OFF();
timer1 = micros();
for (uint8_t r = 0; r < expected_readout; r++){
adpd.readfifo(1, 3, ret + r);
if (flash_duration == r) digitalWrite(STF_FLASH_PIN, LOW);
}
//Serial.println(micros() - timer1);
if (_counter <= 20){
for (uint8_t r = 0; r < _NUM_TS; r++){
d_fluor->put(apply_adpd_calibration(ambit_calibration::S630,
calc_signal(ret[0 + r * 4], ret[1 + r * 4], num_integration)));
d_fluoRef->put(apply_adpd_calibration(ambit_calibration::R630,
calc_signal(ret[2 + r * 4], ret[3 + r * 4], num_integration)));
}
}else{
read_fluor = 0; read_fluoRef = 0;
for (uint8_t r = 0; r < _NUM_TS; r++){
read_fluor += calc_signal(ret[0 + r * 4], ret[1 + r * 4], num_integration);
read_fluoRef += calc_signal(ret[2 + r * 4], ret[3 + r * 4], num_integration);
}
d_fluor->put(apply_adpd_calibration(ambit_calibration::S630, read_fluor / _NUM_TS));
d_fluoRef->put(apply_adpd_calibration(ambit_calibration::R630, read_fluoRef / _NUM_TS));