123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- #ifndef IMAGE_TEST_H
- #define IMAGE_TEST_H
- #include <opencv4/opencv2/opencv.hpp>
- #include <string>
- #include <sys/time.h>
- #include <chrono>
- #include <fstream>
- #include <fstream>
- #include <atomic>
- #include <thread>
- #include <mutex>
- namespace UsbTest
- namespace UsbTest
- {
- void saveImageFromData(unsigned char *data, int width, int height, const std::string &filename = "./output.png");
- class HighResolutionTimer
- {
- public:
- // 开始计时
- void start()
- {
- start_time = std::chrono::high_resolution_clock::now();
- }
- // 停止计时
- void stop()
- {
- end_time = std::chrono::high_resolution_clock::now();
- }
- // 获取经过的时间(以秒为单位)
- double elapsed() const
- {
- return std::chrono::duration<double>(end_time - start_time).count();
- }
- // 获取经过的时间(以毫秒为单位)
- double elapsed_milliseconds() const
- {
- return std::chrono::duration<double, std::milli>(end_time - start_time).count();
- }
- // 获取经过的时间(以微秒为单位)
- double elapsed_microseconds() const
- {
- return std::chrono::duration<double, std::micro>(end_time - start_time).count();
- }
- protected:
- std::chrono::high_resolution_clock::time_point start_time;
- std::chrono::high_resolution_clock::time_point end_time;
- };
- class GlobalResolutionTimer: public HighResolutionTimer
- {
- public:
- static GlobalResolutionTimer& getInstance()
- {
- static GlobalResolutionTimer instance;
- return instance;
- }
- void start()
- {
- start_time = std::chrono::high_resolution_clock::now();
- }
- std::string pressStopWatchString()
- {
- end_time = std::chrono::high_resolution_clock::now();
- return "Elapsed Time: " + std::to_string(elapsed_milliseconds()) + " ms";
- }
- };
- class SimulateTrigger
- {
- public:
- static SimulateTrigger& getInstance()
- {
- static SimulateTrigger instance;
- return instance;
- }
- void startTrigger()
- {
- if (!triggering)
- {
- triggering = true;
- triggerStatus.resize(3);
- triggerStatus[0]=false;
- triggerStatus[1]=false;
- triggerStatus[2]=false;
- triggerThread = std::thread(&SimulateTrigger::triggerLoop, this);
- }
- }
- void stopTrigger()
- {
- if (triggering)
- {
- triggering = false;
- if (triggerThread.joinable())
- {
- triggerThread.join();
- }
- }
- }
- bool isTriggering(int index)
- {
- bool status = triggerStatus[index];
- std::lock_guard<std::mutex> lock(triggerMutex);
- triggerStatus[index] = false;
- return status;
- }
- private:
- SimulateTrigger() : triggering(false) {}
- ~SimulateTrigger()
- {
- stopTrigger();
- }
- // Delete copy constructor and assignment operator to prevent copying
- SimulateTrigger(const SimulateTrigger&) = delete;
- SimulateTrigger& operator=(const SimulateTrigger&) = delete;
- void triggerLoop()
- {
- while (triggering)
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(188));
- if (triggering)
- {
- // Simulate the trigger signal
- std::lock_guard<std::mutex> lock(triggerMutex);
- triggerStatus[0]=true;
- triggerStatus[1]=true;
- triggerStatus[2]=true;
- }
- }
- }
- std::atomic<bool> triggering;
- std::thread triggerThread;
- std::mutex triggerMutex;
- std::vector<bool> triggerStatus;
- };
- class TimeRecorder
- {
- public:
- static TimeRecorder& getInstance()
- {
- static TimeRecorder instance;
- return instance;
- }
- bool init()
- {
- log_file = std::fstream("TimeLog.txt", std::ios::app);
- }
-
- void recordTime(std::string &message)
- {
- if (log_file.is_open())
- {
- log_file << message << std::endl;
- }
- }
- private:
- TimeRecorder() {}
- ~TimeRecorder()
- {
- if (log_file.is_open())
- {
- log_file.close();
- }
- }
- std::fstream log_file;
- };
- }
- #endif // IMAGE_TEST_H
|