-
Notifications
You must be signed in to change notification settings - Fork 7
/
NIRS.py
122 lines (91 loc) · 3.83 KB
/
NIRS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# NIRScanner Python Wrapper
# Created by Weiwei Jiang
#
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), "./"))
import atexit
from _NIRScanner import *
import ctypes
class NIRS:
class TYPES:
COLUMN_TYPE = 0
HADAMARD_TYPE = 1
SLEW_TYPE = 2
def __init__(self):
self.nirs_obj = new_NIRScanner()
atexit.register(self._cleanup)
def _cleanup(self):
print("Cleanning up NIRS instance.")
delete_NIRScanner(self.nirs_obj)
def scan_snr(self, scan_type="hadamard"):
if scan_type == "hadamard":
hadamard_flag = True
elif scan_type == "column":
hadamard_flag = False
else:
print("Unknow scan type {}.".format(scan_type))
results_str = NIRScanner_scanSNR(self.nirs_obj, hadamard_flag)
# Convert to Python object and return.
return eval(results_str)
def scan(self, num_repeats=1):
NIRScanner_scan(self.nirs_obj, False, num_repeats)
def get_scan_results(self):
results_dict = {}
results_str = NIRScanner_getScanData(self.nirs_obj)
results_str = results_str.split("\n")
# Deserialization.
for item in results_str:
keyvalue = item.split(":")
if len(keyvalue) == 2:
results_dict[keyvalue[0]] = keyvalue[1]
# Type conversion.
if "valid_length" in results_dict:
length = int(results_dict["valid_length"])
results_dict["valid_length"] = length
# Convert numerical results.
if "wavelength" in results_dict:
results_dict["wavelength"] = [float(item) for item in results_dict["wavelength"].split(",")[:length]]
if "intensity" in results_dict:
results_dict["intensity"] = [int(item) for item in results_dict["intensity"].split(",")[:length]]
if "reference" in results_dict:
results_dict["reference"] = [int(item) for item in results_dict["reference"].split(",")[:length]]
if "temperature_system" in results_dict:
results_dict["temperature_system"] = int(results_dict["temperature_system"]) / 100.0
if "temperature_detector" in results_dict:
results_dict["temperature_detector"] = int(results_dict["temperature_detector"]) / 100.0
if "humidity" in results_dict:
results_dict["humidity"] = int(results_dict["humidity"]) / 100.0
if "pga" in results_dict:
results_dict["pga"] = int(results_dict["pga"])
return results_dict
def display_version(self):
return NIRScanner_readVersion(self.nirs_obj)
def set_hibernate(self, new_value: bool):
return NIRScanner_setHibernate(self.nirs_obj, new_value)
def set_config(self, scanConfigIndex=8, scan_type=1, num_patterns=228, num_repeats=6,
wavelength_start_nm=900, wavelength_end_nm=1700, width_px=7):
return NIRScanner_setConfig(self.nirs_obj, scanConfigIndex, scan_type, num_patterns, num_repeats,
wavelength_start_nm, wavelength_end_nm, width_px)
def set_pga_gain(self, new_value):
return NIRScanner_setPGAGain(self.nirs_obj, new_value)
def set_lamp_on_off(self, new_value):
return NIRScanner_setLampOnOff(self.nirs_obj, new_value)
def clear_error_status(self):
return NIRScanner_resetErrorStatus(self.nirs_obj)
if __name__ == "__main__":
import time
nirs = NIRS()
nirs.display_version()
# Set config.
nirs.set_config(8, NIRS.TYPES.HADAMARD_TYPE, 228, 6, 900, 1700, 7)
# Turn on the lamp.
nirs.set_lamp_on_off(True)
time.sleep(3)
# Scan.
nirs.scan()
results = nirs.get_scan_results()
nirs.scan()
results = nirs.get_scan_results()
# Turn lamp off.
nirs.set_lamp_on_off(False)
pass