STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
sense_binary_cali copy 2.cpp
Go to the documentation of this file.
1// #include <ADS126X.h>
2// #include <SPI.h>
3// #include <Arduino.h>
4// #include "board_pins.h"
5// #include "adcs.h"
6
7// // 1 = ASCII voltages for the Python calibration script
8// // 0 = packed binary records (original behavior)
9// #define TEXT_OUTPUT 0
10// #define PT_NUM_START 0
11// #define NUM_PTS 10
12
13// // --- Buffer settings ---
14// static const size_t PKT_MAX = 1000; // packet size in bytes
15// static const uint32_t FLUSH_MS = 10; // flush interval in ms
16
17// static uint8_t pktA[PKT_MAX];
18// static uint8_t pktB[PKT_MAX];
19// static uint8_t* active = pktA;
20// static uint8_t* standby = pktB;
21// static size_t used = 0;
22// static uint32_t lastFlushMs = 0;
23// inline void flushPacketIfNeeded(bool force);
24
25// ADS126X& adc = adcs::device();
26
27// int pos_pin = 0; // AIN0
28// int neg_pin = 0xA; // AINCOM
29
30// #pragma pack(push, 1)
31// struct SampleRecord {
32// uint32_t t_us;
33// uint8_t channel;
34// int32_t volt_reader; // Raw ADC code (signed)
35// float voltage; // Volts
36// uint32_t read_time_us;
37// float samples_per_second;
38// uint32_t sent_us;
39// };
40// #pragma pack(pop)
41
42// void setup() {
43// Serial.begin(115200);
44// SPI.begin(SCLK, MISO, MOSI, CS);
45
46// if (!adcs::init()) {
47// while (1) {delay(1000);}
48// }
49
50// if (!adcs::configure()) {
51// while (1) {delay(1000);}
52// }
53
54// adc.startADC1();
55// }
56
57// void loop() {
58// float voltages[NUM_PTS];
59
60// for (uint8_t ch = 0; ch < NUM_PTS; ch++) {
61
62// long raw_wase = adc.readADC1(ch, neg_pin);
63
64// // Wait until DRDY is low
65// for (int j = 0; j < 4; j++) {
66// while (digitalRead(DRDY_PIN) == HIGH) {
67// // wait
68// }
69// }
70
71// uint32_t t_start = micros();
72
73// long raw = adc.readADC1(ch, neg_pin);
74// uint32_t read_time = micros() - t_start;
75
76// // Convert to volts. Signed 32-bit full-scale assumed, Vref = 2.5 V
77// float volts = (float)raw * 2.5f / 2147483648.0f;
78// voltages[ch] = volts;
79
80// if (!TEXT_OUTPUT) {
81// float sps = (read_time > 0) ? (1000000.0f / (float)read_time) : 0.0f;
82
83// SampleRecord rec;
84// rec.t_us = t_start;
85// rec.channel = ch;
86// rec.volt_reader = (int32_t)raw;
87// rec.voltage = volts;
88// rec.read_time_us = read_time;
89// rec.samples_per_second = sps;
90// rec.sent_us = 0;
91
92// // Serial.write((uint8_t *)&rec, sizeof(rec));
93
94// // Buffer packet
95// // if this record won’t fit, flush first
96// if (used + sizeof(rec) > PKT_MAX) {
97// flushPacketIfNeeded(true);
98// }
99
100// // copy record into buffer
101// memcpy(active + used, &rec, sizeof(rec));
102// used += sizeof(rec);
103
104// // flush periodically (time-based)
105// flushPacketIfNeeded(false);
106
107// }
108// }
109
110// if (TEXT_OUTPUT) {
111// for (int i = 0; i < NUM_PTS; i++) {
112// Serial.print(voltages[i], 6);
113// if (i < NUM_PTS - 1) Serial.print(" ");
114// }
115// Serial.print("\r\n"); // CRLF as expected by the Python script
116// }
117// }
118
119// inline void flushPacketIfNeeded(bool force = false) {
120// uint32_t now = millis();
121// if (force || (used > 0 && (now - lastFlushMs >= FLUSH_MS))) {
122// uint32_t send_ts = micros();
123// if (used > 0) {
124// size_t count = used / sizeof(SampleRecord);
125// SampleRecord* recs = reinterpret_cast<SampleRecord*>(active);
126// for (size_t i = 0; i < count; ++i) {
127// recs[i].sent_us = send_ts;
128// }
129// }
130// Serial.write(active, used);
131// Serial.println(); // optional delimiter
132// uint8_t* tmp = active;
133// active = standby;
134// standby = tmp;
135// used = 0;
136// lastFlushMs = now;
137// }
138// }