Files
terra/lib/terra.dart
Developer 194033514f init
2026-05-18 17:58:19 +08:00

391 lines
12 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/services.dart';
import 'terra_platform_interface.dart';
class Terra {
Future<String?> getPlatformVersion() {
return TerraPlatform.instance.getPlatformVersion();
}
static const MethodChannel _channel = MethodChannel('terra');
// ==================== 设备管理 ====================
/// 打开所有设备
static Future<int> openAllDevices() async {
var deviceCount = await _channel.invokeMethod("openAllDevices");
return deviceCount;
}
/// 关闭所有设备
static Future<bool> closeAllDevices() async {
return await _channel.invokeMethod("closeAllDevices");
}
/// 关闭当前设备
static Future<bool> closeDevice() async {
return await _channel.invokeMethod("close");
}
/// 获取设备SN
static Future<String> getDeviceSn() async {
return await _channel.invokeMethod("getDeviceSn");
}
/// 获取设备索引
static Future<int> getDeviceIndex() async {
return await _channel.invokeMethod("getIndex");
}
/// 设备是否连接成功
static Future<bool> isConnect() async {
return await _channel.invokeMethod("isConnect");
}
/// 开启 SDK 调试日志
static Future<bool> openDebug() async {
return await _channel.invokeMethod("openDebug");
}
/// 检查 USB 权限
static Future<Map<String, dynamic>> checkUsbPermission() async {
final result = await _channel.invokeMethod("checkUsbPermission");
return Map<String, dynamic>.from(result as Map);
}
// ==================== 参数配置 ====================
/// 设置积分时间
static Future<bool> setIntegrationTime(double integrationTime) async {
Map<String, dynamic> args = {"integrationTime": integrationTime};
return await _channel.invokeMethod("setIntegrationTime", args);
}
/// 设置平均次数
static Future<bool> setAverageTimes(int averageTimes) async {
Map<String, dynamic> args = {"averageTimes": averageTimes};
return await _channel.invokeMethod("setAverageTimes", args);
}
/// 设置触发模式
/// 0: Free Running
/// 1: Hardware Level
/// 2: Synchronous
/// 3: Hardware Edge
/// 4: Edge And Free
static Future<bool> setTriggerMode(int triggerMode) async {
Map<String, dynamic> args = {"triggerMode": triggerMode};
return await _channel.invokeMethod("setTriggerMode", args);
}
// ==================== 光谱数据获取 ====================
/// 获取光谱(获取当前光谱,可能拿到缓存光谱)
static Future<List<double>> getSpectrum() async {
return await _channel.invokeMethod("getSpectrum");
}
/// 获取光谱(获取光谱前会重新设置积分时间)
static Future<List<double>> getResetSpectrum() async {
return await _channel.invokeMethod("getResetSpectrum");
}
/// 获取波长
static Future<List<double>> getWavelength() async {
return await _channel.invokeMethod("getWavelength");
}
/// 获取波数
static Future<List<double>> getWaveNumber() async {
return await _channel.invokeMethod("getWaveNumber");
}
/// 获取处理完的光谱
static Future<List<double>> getRamanSpectrum() async {
List<double> spectrum = await _channel.invokeMethod("getRamanSpectrum");
return spectrum;
}
/// 获取插值 X 轴数据200~3200步长 2
static Future<List<double>> getXvalue() async {
return await _channel.invokeMethod("getXvalue");
}
// ==================== CCD 制冷控制 ====================
/// 开 CCD 制冷
static Future<bool> setCCDTECOn() async {
return await _channel.invokeMethod("setCCDTECOn");
}
/// 关 CCD 制冷
static Future<bool> setCCDTECOff() async {
return await _channel.invokeMethod("setCCDTECOff");
}
/// 设置 CCD 制冷目标温度(-20 ~ 40
static Future<bool> setCCDTECTemperature(int temperature) async {
Map<String, dynamic> args = {"temperature": temperature};
return await _channel.invokeMethod("setCCDTECTemperature", args);
}
/// 获取 CCD 制冷状态(返回 16 字节状态数组)
static Future<List<int>> getCCDTECState() async {
final result = await _channel.invokeMethod("getCCDTECState");
return List<int>.from(result as List);
}
// ==================== 灯泡/脉冲控制 ====================
/// 设置灯泡延迟时间(微秒)
static Future<bool> setLampDelayTime(double delayTime) async {
return await _channel.invokeMethod("setLampDelayTime", {"delayTime": delayTime});
}
/// 设置灯泡脉宽(微秒)
static Future<bool> setLampWidth(double width) async {
return await _channel.invokeMethod("setLampWidth", {"width": width});
}
/// 设置灯泡周期(微秒)
static Future<bool> setLampInterval(double interval) async {
return await _channel.invokeMethod("setLampInterval", {"interval": interval});
}
/// 获取灯泡延迟时间(微秒)
static Future<List<double>> getLampDelayTime() async {
final result = await _channel.invokeMethod("getLampDelayTime");
return List<double>.from(result as List);
}
/// 获取灯泡脉宽(微秒)
static Future<List<double>> getLampWidth() async {
final result = await _channel.invokeMethod("getLampWidth");
return List<double>.from(result as List);
}
/// 获取灯泡周期(微秒)
static Future<List<double>> getLampInterval() async {
final result = await _channel.invokeMethod("getLampInterval");
return List<double>.from(result as List);
}
/// 开关灯泡控制
static Future<bool> setLampEnable(bool enable) async {
return await _channel.invokeMethod("setLampEnable", {"enable": enable});
}
// ==================== 激光控制 ====================
/// 设置激发波长
static Future<bool> setExcitedWaveLength(int excitedWaveLength) async {
return await _channel.invokeMethod("setExcitedWaveLength", {"excitedWaveLength": excitedWaveLength});
}
/// 设置激光功率(范围 0-2000
static Future<bool> setLaserPower(double power) async {
if (power < 0 || power > 2000) {
print('[Terra] setLaserPower 参数超出范围 (0-2000): $power,拒绝执行');
return false;
}
Map<String, dynamic> args = {"power": power};
print('[Terra] setLaserPower 发送: power=$power');
return await _channel.invokeMethod("setLaserPower", args)
.timeout(const Duration(seconds: 5), onTimeout: () {
print('[Terra] setLaserPower 超时,设备可能未正确响应');
return false;
});
}
/// 开激光
static Future<bool> setLaserOn() async {
return await _channel.invokeMethod("setLaserOn");
}
/// 关激光
static Future<bool> setLaserOff() async {
return await _channel.invokeMethod("setLaserOff");
}
/// 开激光器电源
static Future<bool> setLaserPowerOn() async {
return await _channel.invokeMethod("setLaserPowerOn");
}
/// 关激光器电源
static Future<bool> setLaserPowerOff() async {
return await _channel.invokeMethod("setLaserPowerOff");
}
// ==================== 硬件开关 ====================
/// 开 TEC
static Future<bool> setTECOn() async {
return await _channel.invokeMethod("setTECOn");
}
/// 关 TEC
static Future<bool> setTECOff() async {
return await _channel.invokeMethod("setTECOff");
}
/// 开主板
static Future<bool> setMainBoardOn() async {
return await _channel.invokeMethod("setMainBoardOn");
}
/// 关主板
static Future<bool> setMainBoardOff() async {
return await _channel.invokeMethod("setMainBoardOff");
}
/// 主板是否已打开
static Future<bool> isMainBoardOpen() async {
return await _channel.invokeMethod("isMainBoardOpen");
}
/// TEC 是否已打开
static Future<bool> isTECOpen() async {
return await _channel.invokeMethod("isTECOpen");
}
// ==================== 光谱数据处理 ====================
/// 小波平滑
static Future<List<double>> waveletSmooth(List<double> spectrum) async {
return await _channel.invokeMethod("waveletSmooth", {"spectrum": spectrum});
}
/// 消荧光side 为消荧光因子,默认 15
static Future<List<double>> removeFluorescence(List<double> spectrum, {int side = 15}) async {
return await _channel.invokeMethod("removeFluorescence", {"spectrum": spectrum, "side": side});
}
/// 线性插值
static Future<List<double>> lineInterpolation(
List<double> xDataRaw,
List<double> yDataRaw,
List<double> xData,
) async {
return await _channel.invokeMethod("lineInterpolation", {
"xDataRaw": xDataRaw,
"yDataRaw": yDataRaw,
"xData": xData,
});
}
/// 寻峰
static Future<List<int>> findPeaks(
List<double> spectrum,
int minIndicesBetweenPeaks,
double baseline,
) async {
Map<String, dynamic> args = {
"spectrum": spectrum,
"minIndicesBetweenPeaks": minIndicesBetweenPeaks,
"baseline": baseline,
};
return await _channel.invokeMethod("findPeaks", args);
}
// ==================== 校准系数 ====================
/// 获取激光校正系数
static Future<List<double>> getLaserPowerCorrectCoefficient() async {
return await _channel.invokeMethod("getLaserPowerCorrectCoefficient");
}
/// 设置激光校正系数
/// calibrationCoefficient 格式16 位字符串4 个数据,每个数据 4 位
/// 如 "0070030008001400"
static Future<bool> setLaserPowerCorrectCoefficient(String calibrationCoefficient) async {
Map<String, String> args = {"calibrationCoefficient": calibrationCoefficient};
return await _channel.invokeMethod("setLaserPowerCorrectCoefficient", args);
}
/// 获取乙腈校准数据
static Future<List<double>> getYJCorrectCoefficient() async {
return await _channel.invokeMethod("getYJCorrectCoefficient");
}
/// 设置乙腈定标系数
static Future<bool> setYJCorrectCoefficient(List<double> coffes) async {
Map<String, List<double>> args = {"coffes": coffes};
return await _channel.invokeMethod("setYJCorrectCoefficient", args);
}
/// 获取 Y 轴校准系数
static Future<List<double>> getYAxisCorrectCoefficient() async {
final result = await _channel.invokeMethod("getYAxisCorrectCoefficient");
return List<double>.from(result as List);
}
/// 获取探测器非线性校正系数
static Future<List<double>> getCorrectForDetectorNonlinear() async {
final result = await _channel.invokeMethod("getCorrectForDetectorNonlinear");
return List<double>.from(result as List);
}
/// 获取波长校准系数
static Future<List<double>> getWavelengthCalibrationCoefficients() async {
final result = await _channel.invokeMethod("getWavelengthCalibrationCoefficients");
return List<double>.from(result as List);
}
/// 获取 FPGA 版本
static Future<List<double>> getFpgaVersion() async {
final result = await _channel.invokeMethod("getFpgaVersion");
return List<double>.from(result as List);
}
/// 获取狭缝大小
static Future<String> getSlit() async {
return await _channel.invokeMethod("getSlit");
}
/// 获取坏点
static Future<List<int>> getBadPoints() async {
final result = await _channel.invokeMethod("getBadPoints");
return List<int>.from(result as List);
}
// ==================== 算法匹配与校准 ====================
/// 算法注册
static Future<bool> ramanMatchRegister() async {
return await _channel.invokeMethod("ramanMatchRegister");
}
/// 光谱匹配,返回相似度
static Future<double> ramanMatchCalcSimilarity(
List<double> spectrum,
List<double> libSpectrumList,
) async {
Map<String, List<double>> args = {
"spectrum": spectrum,
"libSpectrumList": libSpectrumList,
};
return await _channel.invokeMethod("ramanMatchCalcSimilarity", args);
}
/// 校准
static Future<List<double>> calibration(
List<double> spectrum,
List<double> YJCoeff,
) async {
Map<String, List<double>> args = {
"spectrum": spectrum,
"YJCoeff": YJCoeff,
};
return await _channel.invokeMethod("calibration", args);
}
// ==================== 其他 ====================
/// SN 码 MD5 加密
static Future<String> bytesToMD5(String sn) async {
Map<String, String> args = {"sn": sn};
return await _channel.invokeMethod("bytesToMD5", args);
}
}