From bcf1eb652b8f51ca36d245464facb8b01ed8bbd0 Mon Sep 17 00:00:00 2001 From: Maksim_Bartoshyk Date: Tue, 1 Oct 2024 18:01:36 +0300 Subject: [PATCH] feat (spectrum-change): use sliding window for spectrum change funtionality if spectrum change is enabled and delta time is 10 seconds, app will always show spectrum for the last 10 seconds allow window up to 5 minutes --- .../fe57/atomspectra/AtomSpectraService.java | 39 ++++++++++++++----- .../fe57/atomspectra/AtomSpectraSettings.java | 8 +++- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/fe57/atomspectra/AtomSpectraService.java b/app/src/main/java/org/fe57/atomspectra/AtomSpectraService.java index b3c3a11..cdae2cf 100644 --- a/app/src/main/java/org/fe57/atomspectra/AtomSpectraService.java +++ b/app/src/main/java/org/fe57/atomspectra/AtomSpectraService.java @@ -109,7 +109,7 @@ public class AtomSpectraService extends Service { //data for spectrum private static final double[] histogram = new double[1024]; public static long[] histogram_all_delta = new long[Constants.NUM_HIST_POINTS]; //array to save delta - public static long[] histogram_all_prev_time = new long[Constants.NUM_HIST_POINTS]; //array to save delta + public static final LinkedList histogram_all_queue = new LinkedList(); //array to save delta window private static final long[] referencePulse = new long[1024]; private static final double[] referenceDoublePulse = new double[1024]; private static final double[] realTimeX = new double[1024]; @@ -1341,7 +1341,9 @@ public void DeleteSpc() { ForegroundSpectrum.initSpectrumData().setSuffix(getString(R.string.hist_suffix)); ForegroundSaveSpectrum.initSpectrumData(); Arrays.fill(histogram_all_delta, 0); - Arrays.fill(histogram_all_prev_time, 0); + synchronized (histogram_all_queue) { + histogram_all_queue.clear(); + } total_pulses = 0; synchronized (windowCps) { windowCps.clear(); @@ -1891,13 +1893,32 @@ public void run() { } } deltaTimeAccumulator++; - if (deltaTimeAccumulator >= (delta_time * 1000 / Constants.UPDATE_PERIOD)) { - deltaTimeAccumulator = 0; - long[] temp = Arrays.copyOf(ForegroundSpectrum.getDataArray(), ForegroundSpectrum.getDataArray().length); - for (int i = 0; i < temp.length; i++) { - histogram_all_delta[i] = temp[i] - histogram_all_prev_time[i]; - histogram_all_prev_time[i] = temp[i]; + if (deltaTimeAccumulator >= (1000 / Constants.UPDATE_PERIOD)) { // each second + if (showDelta && !freeze_update_data) { + long[] currentState = Arrays.copyOf(ForegroundSpectrum.getDataArray(), ForegroundSpectrum.getDataArray().length); + long[] previousState = currentState; + synchronized (histogram_all_queue) { + histogram_all_queue.add(currentState); + while (histogram_all_queue.size() > delta_time + 1) { + histogram_all_queue.remove(); + } + + previousState = histogram_all_queue.peek(); + } + + for (int i = 0; i < currentState.length; i++) { + histogram_all_delta[i] = currentState[i] - previousState[i]; + } + } else { + // cleanup queue + if (!histogram_all_queue.isEmpty()) { + synchronized (histogram_all_queue) { + histogram_all_queue.clear(); + } + } } + + deltaTimeAccumulator = 0; } synchronized (countTimeSync) { if (countTime < periodUpdate) { @@ -2501,7 +2522,7 @@ private void autosaveHist() { if (autosaveSpectrum == null) { autosaveSpectrum = new Spectrum(AtomSpectraService.ForegroundSpectrum); - autosavePair = SpectrumFile.prepareOutputStream(this, sharedPreferences.getString(Constants.CONFIG.CONF_DIRECTORY_SELECTED, null), autosaveSpectrum.getSpectrumDate(), "Spectrogram" + '-' + autosaveSpectrum.getSuffix(), fileNamePrefix,"auto", ".txt", "text/plain", true, true, false); + autosavePair = SpectrumFile.prepareOutputStream(this, sharedPreferences.getString(Constants.CONFIG.CONF_DIRECTORY_SELECTED, null), autosaveSpectrum.getSpectrumDate(), "Spectrogram" + '-' + autosaveSpectrum.getSuffix(), fileNamePrefix, "auto", ".txt", "text/plain", true, true, false); if (autosavePair == null) { new Handler(getMainLooper()).post(() -> Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.perm_no_write_histogram), Toast.LENGTH_LONG).show()); diff --git a/app/src/main/java/org/fe57/atomspectra/AtomSpectraSettings.java b/app/src/main/java/org/fe57/atomspectra/AtomSpectraSettings.java index 3629ae2..8d1ccd5 100644 --- a/app/src/main/java/org/fe57/atomspectra/AtomSpectraSettings.java +++ b/app/src/main/java/org/fe57/atomspectra/AtomSpectraSettings.java @@ -1468,7 +1468,9 @@ public void onClick_Delta_Time_minus(View v) { SharedPreferences settings = getSharedPreferences(Constants.ATOMSPECTRA_PREFERENCES, MODE_PRIVATE); int r = settings.getInt(Constants.CONFIG.CONF_DELTA_TIME, Constants.DEFAULT_DELTA_TIME); - if (r > 10) { + if (r > 60) { + r = r - 30; + } else if (r > 10) { r = r - 5; } else if (r > 1) { r = r - 1; @@ -1493,8 +1495,10 @@ public void onClick_Delta_Time_plus(View v) { r = r + 1; } else if (r < 60) { r = r + 5; + } else if (r < 300) { + r = r + 30; } else { - r = 60; + r = 300; } SharedPreferences.Editor prefEditor = settings.edit();