添加项目文件。
This commit is contained in:
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
#ifndef ARIS_H
|
||||
#define ARIS_H
|
||||
|
||||
#include "aris_global.h"
|
||||
|
||||
class ARIS_EXPORT Aris
|
||||
{
|
||||
public:
|
||||
Aris();
|
||||
};
|
||||
|
||||
#endif // ARIS_H
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
#ifndef ARIS_GLOBAL_H
|
||||
#define ARIS_GLOBAL_H
|
||||
|
||||
#if defined(_MSC_VER) || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
|
||||
# define Q_DECL_EXPORT __declspec(dllexport)
|
||||
# define Q_DECL_IMPORT __declspec(dllimport)
|
||||
#else
|
||||
# define Q_DECL_EXPORT __attribute__((visibility("default")))
|
||||
# define Q_DECL_IMPORT __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
#if defined(ARIS_LIBRARY)
|
||||
# define ARIS_EXPORT Q_DECL_EXPORT
|
||||
#else
|
||||
# define ARIS_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
#endif // ARIS_GLOBAL_H
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
#ifndef BYTECONVERTER_H
|
||||
#define BYTECONVERTER_H
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <type_traits>
|
||||
#include <cstring>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
|
||||
class ByteConverter {
|
||||
public:
|
||||
static uint32_t bswap32(uint32_t v) {
|
||||
return ((v & 0x000000FFu) << 24) |
|
||||
((v & 0x0000FF00u) << 8) |
|
||||
((v & 0x00FF0000u) >> 8) |
|
||||
((v & 0xFF000000u) >> 24);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static T fromLittleEndian(const uint8_t* data, size_t offset = 0) {
|
||||
T value = 0;
|
||||
for (size_t i = 0; i < sizeof(T); ++i) {
|
||||
value |= static_cast<T>(data[offset + i]) << (i * 8);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
template<typename T>
|
||||
static T convert(const std::vector<uint8_t>& data, size_t offset = 0, bool is_little_endian = true) {
|
||||
if (data.size() < offset + sizeof(T)) {
|
||||
throw std::runtime_error("Data buffer overflow");
|
||||
}
|
||||
T value;
|
||||
memcpy(&value, data.data() + offset, sizeof(T));
|
||||
if (is_little_endian) {
|
||||
return value; // 假设当前平台是小端序
|
||||
} else {
|
||||
// 大端序转小端序(根据平台实现)
|
||||
if (sizeof(T) == 4) {
|
||||
uint32_t v32 = 0;
|
||||
std::memcpy(&v32, &value, sizeof(uint32_t));
|
||||
v32 = bswap32(v32);
|
||||
std::memcpy(&value, &v32, sizeof(uint32_t));
|
||||
return value;
|
||||
}
|
||||
// 其他类型处理...
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// 字符串解析(增强版)
|
||||
static std::string convert_str(
|
||||
const std::vector<uint8_t>& data,
|
||||
size_t offset = 0,
|
||||
size_t max_length = SIZE_MAX,
|
||||
bool trim_leading_null = true,
|
||||
bool trim_trailing_null = true
|
||||
);
|
||||
|
||||
private:
|
||||
static void validate_size(const std::vector<uint8_t>& data, size_t offset, size_t required);
|
||||
};
|
||||
|
||||
#endif // BYTECONVERTER_H
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
// libusbinterface.h
|
||||
#ifndef LIBUSB_INTERFACE_H
|
||||
#define LIBUSB_INTERFACE_H
|
||||
|
||||
#include <libusb-1.0/libusb.h>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
#include <stdexcept>
|
||||
|
||||
class LibusbInterface {
|
||||
public:
|
||||
LibusbInterface(uint16_t vendorId, uint16_t productId, uint8_t busNumber, uint8_t deviceAddress);
|
||||
~LibusbInterface();
|
||||
|
||||
void open();
|
||||
void close();
|
||||
bool isOpen() const;
|
||||
void write(const std::vector<unsigned char>& data);
|
||||
std::vector<unsigned char> read(int maxLength);
|
||||
|
||||
static std::vector<std::tuple<uint8_t, uint8_t>> findDevices(uint16_t vendorId, uint16_t productId);
|
||||
|
||||
private:
|
||||
libusb_device_handle* mDevHandle;
|
||||
uint16_t mVendorId;
|
||||
uint16_t mProductId;
|
||||
uint8_t mBusNumber;
|
||||
uint8_t mDeviceAddress;
|
||||
bool mOpenFlag = false;
|
||||
uint8_t mEndpointOut;
|
||||
uint8_t mEndpointIn;
|
||||
|
||||
static libusb_context* sUsbContext;
|
||||
static int sInstanceCount;
|
||||
|
||||
static void initUsbContext();
|
||||
static void cleanupUsbContext();
|
||||
void setupEndpoints(libusb_device* device);
|
||||
void configureDevice();
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // LIBUSB_INTERFACE_H
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
#ifndef SPECTROMETERCALLBACK_H
|
||||
#define SPECTROMETERCALLBACK_H
|
||||
|
||||
|
||||
#include "aris_global.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
|
||||
class ARIS_EXPORT SpectrometerCallback
|
||||
{
|
||||
public:
|
||||
virtual ~SpectrometerCallback() = default;
|
||||
|
||||
// 回调函数,当光谱数据准备好时被调用
|
||||
virtual void onDataReceived(std::string serialNumber,const std::vector<float> &wavelength,
|
||||
const std::vector<float>& spectrum) = 0;
|
||||
|
||||
// 回调函数,当光谱数据准备好时被调用
|
||||
virtual void onDataReceivedBanch(std::string serialNumber,const std::vector<float> &wavelength,
|
||||
const std::vector<std::vector<float>>& spectrum) = 0;
|
||||
|
||||
// 回调函数,当发生错误时被调用
|
||||
virtual void onError(std::string serialNumber,
|
||||
const std::string& error) = 0;
|
||||
};
|
||||
|
||||
#endif // SPECTROMETERCALLBACK_H
|
||||
+215
@@ -0,0 +1,215 @@
|
||||
#ifndef SPECTROMETERDEVICE_H
|
||||
#define SPECTROMETERDEVICE_H
|
||||
|
||||
|
||||
#include "aris_global.h"
|
||||
#include "libusbinterface.h"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
|
||||
|
||||
// 确保结构体紧凑排列(1字节对齐)
|
||||
#pragma pack(push, 1)
|
||||
struct CommandWithoutParam {
|
||||
uint32_t code;
|
||||
};
|
||||
struct CommandWithParam {
|
||||
uint32_t code;
|
||||
uint32_t param;
|
||||
};
|
||||
struct TriggerConfig {
|
||||
uint8_t mode = 0x00; // Free-Running(曝光结束后触发)
|
||||
uint8_t edge = 0x00; // 上升沿(此模式下无实际作用,可忽略)
|
||||
uint16_t reserved = 0; // 必须为0
|
||||
};
|
||||
// 添加与C#对应的SpectrumHeader结构体
|
||||
#pragma pack(push, 1)
|
||||
struct SpectrumHeader {
|
||||
uint32_t ExposureTime; // 曝光时间(微秒)
|
||||
uint32_t Averaging; // 平均次数
|
||||
uint32_t Time; // 时间戳(0.1ms)
|
||||
uint32_t Date; // 日期戳
|
||||
float LoadLevel; // 负载水平
|
||||
float SensorTemperature; // 传感器温度
|
||||
uint16_t PixelCount; // 像素数量
|
||||
uint8_t DataFormat; // 数据格式
|
||||
uint8_t VersionNumber; // 版本号
|
||||
uint16_t ExposureSettings; // 曝光设置
|
||||
uint16_t AppliedProcessing; // 应用的处理标志位(关键字段)
|
||||
char Name[24]; // 名称字符串
|
||||
float DarkAvg; // 暗电流平均值
|
||||
float ReadoutNoise; // 读出噪声
|
||||
};
|
||||
#pragma pack(pop)
|
||||
#pragma pack(pop)
|
||||
|
||||
enum class TriggerMode {
|
||||
SOFTWARE,
|
||||
EXTERNAL
|
||||
};
|
||||
|
||||
// Spectrometer status
|
||||
enum SpectrStatus {
|
||||
Idle = 0x00,
|
||||
WaitingForTrigger = 0x01,
|
||||
TakingSpectrum = 0x02,
|
||||
WaitingForTemperature = 0x03,
|
||||
PoweredOff = 0x08,
|
||||
SleepMode = 0x09,
|
||||
NotConnected = 0x0A
|
||||
};
|
||||
|
||||
struct StatusInfo {
|
||||
SpectrStatus status = SpectrStatus::NotConnected;
|
||||
uint16_t availableSpectra = 0;
|
||||
};
|
||||
|
||||
struct SpectrometerParameters {
|
||||
std::string serialNumber;
|
||||
int startPixel; // 起始像素
|
||||
int endPixel; // 结束像素
|
||||
size_t pixelCount; // 像素数
|
||||
std::vector<float> wavelengths; // 波长列表
|
||||
|
||||
};
|
||||
|
||||
|
||||
// 定义版本结构体
|
||||
struct FirmwareVersion {
|
||||
uint8_t major;
|
||||
uint8_t minor;
|
||||
uint8_t build;
|
||||
uint8_t revision;
|
||||
|
||||
// 构造函数
|
||||
FirmwareVersion(uint8_t maj = 0, uint8_t min = 0, uint8_t bld = 0, uint8_t rev = 0)
|
||||
: major(maj), minor(min), build(bld), revision(rev) {}
|
||||
|
||||
// 版本比较运算符
|
||||
bool operator>=(const FirmwareVersion& other) const {
|
||||
if (major != other.major) return major > other.major;
|
||||
if (minor != other.minor) return minor > other.minor;
|
||||
if (build != other.build) return build > other.build;
|
||||
return revision >= other.revision;
|
||||
}
|
||||
|
||||
// 调试输出
|
||||
std::string toString() const {
|
||||
return std::to_string(major) + "." + std::to_string(minor) + "." +
|
||||
std::to_string(build) + "." + std::to_string(revision);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class ARIS_EXPORT SpectrometerDevice
|
||||
{
|
||||
public:
|
||||
SpectrometerDevice();
|
||||
|
||||
SpectrometerDevice(std::unique_ptr<LibusbInterface> device);
|
||||
|
||||
std::string m_serialNumber;
|
||||
|
||||
// 获取设备的基本参数
|
||||
SpectrometerParameters getParameters() const;
|
||||
|
||||
|
||||
void disconnect();
|
||||
|
||||
void setExposureParams(int exposureTime, int averaging);
|
||||
|
||||
void setTriggerMode(TriggerMode mode);
|
||||
|
||||
// 查询触发模式
|
||||
TriggerConfig getTriggerMode();
|
||||
|
||||
void startMeasure();
|
||||
void startMeasure(int exposureCount);
|
||||
void standBy();
|
||||
|
||||
SpectrStatus getStatus();
|
||||
|
||||
StatusInfo getStatusInfo();
|
||||
|
||||
std::vector<float> getSpectrum();
|
||||
std::vector<float> getSpectrum(uint32_t& time01ms, uint32_t& dateDays);
|
||||
std::vector<float> getSpectrum(uint32_t& time01ms, uint32_t& dateDays, uint32_t& exposureTimeUs, uint32_t& averaging);
|
||||
std::vector<std::vector<float>> getAllSpectrumData();
|
||||
void discardSpectrum();
|
||||
|
||||
std::vector<float> getWavelength();
|
||||
|
||||
void stopMeasure();
|
||||
|
||||
// 新增:暗背景相关方法
|
||||
void takeDarkSpectrum();
|
||||
void setSubtractDark(bool enable);
|
||||
// 设置增益:0=低增益,1=高增益
|
||||
void setGain(int gain=0);
|
||||
|
||||
void setDelayParams(int delay = 0);
|
||||
|
||||
void cancelExposure();
|
||||
private:
|
||||
|
||||
void initialize();
|
||||
// 获取设备的各项参数
|
||||
std::string getSerialNumber();
|
||||
|
||||
int getStartPixel();
|
||||
|
||||
int getEndPixel();
|
||||
|
||||
int getPixelCount();
|
||||
|
||||
std::vector<float> readWavelengthsFromDevice();
|
||||
|
||||
|
||||
|
||||
|
||||
int m_startPixel;
|
||||
int m_endPixel;
|
||||
size_t m_pixelCount;
|
||||
std::vector<float> m_wavelengths;
|
||||
|
||||
std::unique_ptr<LibusbInterface> m_device; // 设备硬件接口
|
||||
int m_deviceIndex; // 设备索引,用于命令识别
|
||||
|
||||
static constexpr int MAX_RX_LENGTH = 16492;
|
||||
|
||||
|
||||
bool m_subtractDark; // 是否启用暗背景扣除
|
||||
bool m_hasDarkSpectrum; // 是否已获取暗背景
|
||||
mutable std::mutex m_darkSpectrumMutex; // 暗背景数据的线程安全保护
|
||||
|
||||
std::vector<unsigned char> sendBulkReceiveMessage(const std::vector<unsigned char>& txData) ;
|
||||
std::vector<uint8_t> sendReceiveMessage(uint32_t command_code);
|
||||
std::vector<uint8_t> sendReceiveMessage(uint32_t command_code, uint32_t parameter);
|
||||
std::vector<uint8_t> sendReceiveMessage(uint32_t command_code,
|
||||
const std::vector<uint8_t>& payload);
|
||||
std::vector<uint8_t> readBulkData(size_t expectedSize);
|
||||
|
||||
void setFreeRunningTrigger();
|
||||
|
||||
void setHardwareRisingEdgeTrigger();
|
||||
|
||||
|
||||
void saveSpectrumWithDarkSubtractionCSV(const std::vector<float>& spectrum);
|
||||
|
||||
std::string generateRandomFilename(const std::string& prefix,
|
||||
const std::string& ext);
|
||||
|
||||
|
||||
|
||||
|
||||
FirmwareVersion softwareVersion;
|
||||
std::chrono::system_clock::time_point nextTimeUpdate;
|
||||
FirmwareVersion readSoftwareVersion();
|
||||
void updateTimeInDevice();
|
||||
void ensureIdleForConfiguration();
|
||||
};
|
||||
|
||||
#endif // SPECTROMETERDEVICE_H
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
#ifndef ZIOLINK_PROTOCOL_H
|
||||
#define ZIOLINK_PROTOCOL_H
|
||||
|
||||
|
||||
#include "aris_global.h" // 在文件顶部添加
|
||||
#include "libusbinterface.h"
|
||||
#include <cstring>
|
||||
#include "spectrometercallback.h"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include "byteconverter.h"
|
||||
#include <unordered_map>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <functional> // 用于回调函数
|
||||
#include <future>
|
||||
#include <deque>
|
||||
#include "spectrometerdevice.h"
|
||||
|
||||
|
||||
|
||||
|
||||
class ARIS_EXPORT ZioLinkProtocol { // 添加导出宏
|
||||
public:
|
||||
ZioLinkProtocol();
|
||||
|
||||
~ZioLinkProtocol();
|
||||
|
||||
|
||||
|
||||
void connectDevice();
|
||||
|
||||
void disconnectDevice();
|
||||
|
||||
|
||||
// 获取已连接设备数量
|
||||
size_t getConnectedDeviceCount() const;
|
||||
|
||||
|
||||
// 设置触发模式
|
||||
void setTriggerMode(TriggerMode mode);
|
||||
|
||||
// 获取触发模式
|
||||
TriggerConfig getTriggerMode(int deviceIndex );
|
||||
|
||||
// 启动测量并传入回调函数
|
||||
void startMeasurement(SpectrometerCallback* callback, int exposureTime, int averaging = 1, int delayUs = 0, int measureCount = 1);
|
||||
void startBatchMeasurement(SpectrometerCallback* callback, int exposureTime, int collections, int averaging = 1, int delayUs = 0);
|
||||
// 停止测量
|
||||
void stopMeasurement();
|
||||
|
||||
std::unique_ptr<SpectrometerDevice>& getDevice(int deviceKey);
|
||||
|
||||
|
||||
|
||||
// 新增:暗背景管理方法
|
||||
void takeDarkSpectrum(int exposureTime, int averaging = 1, int deviceIndex = -1); // -1表示所有设备
|
||||
|
||||
void setSubtractDark(bool enable, int deviceIndex) ;
|
||||
private:
|
||||
static const uint16_t TARGET_VID ;
|
||||
static const uint16_t TARGET_PID;
|
||||
|
||||
|
||||
template<typename T>
|
||||
void pack_le(uint8_t* dest, T value) {
|
||||
for (size_t i = 0; i < sizeof(T); ++i) {
|
||||
dest[i] = (value >> (i * 8)) & 0xFF; // 将每一字节存储到数组中
|
||||
}
|
||||
}
|
||||
|
||||
// 设置触发模式为 Free-Running(曝光结束后触发)
|
||||
void setFreeRunningTrigger(int deviceIndex);
|
||||
|
||||
// 设置硬件上升沿触发模式
|
||||
void setHardwareRisingEdgeTrigger(int deviceIndex);
|
||||
|
||||
|
||||
void checkStatusAndNotify(SpectrometerCallback* callback,
|
||||
int deviceIndex,
|
||||
std::chrono::steady_clock::time_point startTime,
|
||||
std::chrono::system_clock::time_point startWallTime,
|
||||
int exposureTimeUs,
|
||||
int averaging);
|
||||
|
||||
std::vector<std::tuple<uint8_t, uint8_t>> mFoundDevices;
|
||||
|
||||
|
||||
int mCurrentDeviceIndex ;
|
||||
void getSpectrometerStatus( int deviceIndex);
|
||||
|
||||
// 设置测量参数
|
||||
void setExposureParams(int exposureTime = 30000, int averaging = 1, int deviceIndex=0);
|
||||
|
||||
|
||||
// 停止测量
|
||||
void stopDeviceMeasurement(int deviceIndex);
|
||||
|
||||
// 关闭当前设备
|
||||
void disconnectCurrentDevice( int deviceIndex);
|
||||
|
||||
// 线程安全状态管理
|
||||
std::mutex deviceMutex;
|
||||
|
||||
// 设备连接池,保存每个设备的独立连接
|
||||
std::vector<std::unique_ptr<SpectrometerDevice>> mDevicePool;
|
||||
|
||||
std::atomic<bool> isStop{true};
|
||||
|
||||
|
||||
|
||||
std::deque<std::unique_ptr<std::atomic<bool>>> deviceMeasurementStatus;
|
||||
|
||||
|
||||
std::vector<std::unique_ptr<std::mutex>> mDeviceMutexes;
|
||||
std::vector<std::thread> measurementThreads;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // ZIOLINK_PROTOCOL_H
|
||||
Reference in New Issue
Block a user