MCPcopy Create free account
hub / github.com/curiores/SerialAnalyzer / addData

Function addData

src/Utils/SerialData.js:136–204  ·  view source on GitHub ↗
(data)

Source from the content-addressed store, hash-verified

134 var onVal = parser.on('data', addData)
135
136 function addData(data) {
137 // If there is decimation, don't add the data until the decimation index is reached
138 if (decIndex >= GlobalSettings.global.decimation) {
139 decIndex = 1;
140 } else {
141 decIndex += 1;
142 return; // Dont add the data if the user is decimating
143 }
144 //--------------------- regular update --------------------- //
145 SerialDataObject.Iter += 1;// Iterate
146 // Computations for the sample rate (only do this once every NsampleRateUpdate samples)
147 if (SerialDataObject.Iter % SerialDataObject.NsampleRateUpdate === 0) {
148 if (SerialDataObject.sampleHistory.length > 1) {
149 var deltaT = SerialDataObject.timeHistory[SerialDataObject.timeHistory.length - 1] - SerialDataObject.timeHistory[0];
150 var samples = SerialDataObject.sampleHistory[SerialDataObject.sampleHistory.length - 1] - SerialDataObject.sampleHistory[0];
151
152 SerialDataObject.sampleRate = samples / deltaT * 1000;
153 }
154 }
155
156 // Push the raw data (unless its NaN)
157 SerialDataObject.rawData.push(data);
158 if (SerialDataObject.rawData.length >= SerialDataObject.bufferSize) {
159 // If the buffer is full, remove the first line of the raw data
160 SerialDataObject.rawData.shift();
161 }
162
163 // If we're recording, write each data row to the file
164 writeDataIfRecording(data);
165
166 // Now parse the numeric data
167 var splitData = data.split(/\s+|,\s+/);
168 var nums = splitData.map(parseFloat);
169 var t = 0;
170 if (GlobalSettings.global.firstColumnTime) {
171 t = nums[0];
172 nums = nums.slice(1, nums.length);
173 }
174 else {
175 t = performance.now();
176 }
177
178 // Push the data into the data array (if there arent any NaNs)
179 // also push the sample/time history
180 if (nums.every((value) => { return !isNaN(value) })) {
181 SerialDataObject.data.push(nums);
182 SerialDataObject.sampleHistory.push(SerialDataObject.Iter);
183 SerialDataObject.timeHistory.push(t);
184 }
185
186 var n = SerialDataObject.data.length;
187 if (n > SerialDataObject.bufferSize) {
188 // If the data has filled the buffer, resize it
189 const resize = (v) => {
190 return v.slice(v.length - SerialDataObject.bufferSize, v.length);
191 }
192 SerialDataObject.data = resize(SerialDataObject.data);
193 SerialDataObject.sampleHistory = resize(SerialDataObject.sampleHistory);

Callers

nothing calls this directly

Calls 3

writeDataIfRecordingFunction · 0.90
resizeFunction · 0.85
idxDataFunction · 0.85

Tested by

no test coverage detected