Exemplo n.º 1
0
 public double[] GetSpectrum()
 {
     return(wrapper.getSpectrum(index));
 }
Exemplo n.º 2
0
        ////////////////////////////////////////////////////////////////////////
        // Background Worker
        ////////////////////////////////////////////////////////////////////////

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            int specIndex = (int)e.Argument;

            logger.log("[DoWork:{0}] starting...", specIndex);
            if (specIndex < 0 || wrapper == null)
            {
                logger.push("[DoWork:{0}] ERROR: can't start acquisition (missing spectrometer or OmniDriver)", specIndex);
                return;
            }

            BackgroundWorker worker = sender as BackgroundWorker;

            scanCount = 0;

            while (true)
            {
                scanCount++;
                double[] newRaw = null;

                try
                {
                    lastScanTimes[specIndex] = DateTime.Now;
                    newRaw = wrapper.getSpectrum(specIndex);

                    // logger.log("  spectrum {0} valid", 0 == wrapper.isSpectrumValid(specIndex) ? "is" : "is NOT");

                    if (wavelengths[specIndex] == null)
                    {
                        newRaw = null;
                    }

                    if (savingAll)
                    {
                        Tuple <DateTime, double[]> entry = new Tuple <DateTime, double[]>(DateTime.Now, newRaw);
                        bufferedSpectra[specIndex].Add(entry);
                    }
                    else if (newRaw != null)
                    {
                        // only log this if we're not in high-speed mode
                        double min = 999999;
                        double max = -99999;
                        for (int i = 0; i < newRaw.Length; i++)
                        {
                            min = newRaw[i] < min ? newRaw[i] : min;
                            max = newRaw[i] > max ? newRaw[i] : max;
                        }
                        logger.log("  spectrum {0} has {1} pixels ({2}, {3})", scanCount, newRaw.Length, min, max);
                    }
                    else
                    {
                        logger.log("  spectrum is NULL!");
                    }
                }
                catch (Exception ex)
                {
                    logger.push("[DoWork:{0}] Caught exception during acquisition: {1}", specIndex, ex);
                    break;
                }

                if (newRaw == null)
                {
                    logger.push("[DoWork:{0}] Error taking acquisition (newRaw null)", specIndex);
                    break;
                }

                if (newRaw.Length != pixels[specIndex])
                {
                    logger.push("[DoWork:{0}] Error taking acquisition (length {1} != pixels {2})", specIndex, newRaw.Length, pixels[specIndex]);
                    break;
                }

                // copy to graphable buffer

                mut.WaitOne();
                Array.Copy(newRaw, newestSpectrum[specIndex], pixels[specIndex]);
                mut.ReleaseMutex();

                // report progress
                worker.ReportProgress(scanCount, specIndex);

                // quit if we've completed our scan count
                if (maxScans > 0 && scanCount >= maxScans)
                {
                    logger.log("[DoWork:{0}] Stopping after {0} scans", specIndex, scanCount);
                    break;
                }

                // end thread if we've been asked to cancel
                if (worker.CancellationPending)
                {
                    logger.log("[DoWork:{0}] closing", specIndex);
                    e.Cancel = true;
                    break;
                }
            }

            // this worker is done
            if (scanning.Contains(specIndex))
            {
                scanning.Remove(specIndex);
            }

            logger.log("[DoWork:{0}] done", specIndex);
        }