-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathArducam.py
184 lines (145 loc) · 7.01 KB
/
Arducam.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import threading
import ArducamSDK
from utils import *
class ArducamCamera(object):
def __init__(self):
self.isOpened = False
self.running_ = False
self.signal_ = threading.Condition()
pass
def openCamera(self, fname, index=0):
self.isOpened, self.handle, self.cameraCfg, self.color_mode = camera_initFromFile(
fname, index)
return self.isOpened
def start(self):
if not self.isOpened:
raise RuntimeError("The camera has not been opened.")
self.running_ = True
ArducamSDK.Py_ArduCam_setMode(self.handle, ArducamSDK.CONTINUOUS_MODE)
self.capture_thread_ = threading.Thread(target=self.capture_thread)
self.capture_thread_.daemon = True
self.capture_thread_.start()
def read(self, timeout=1500):
if not self.running_:
raise RuntimeError("The camera is not running.")
if ArducamSDK.Py_ArduCam_availableImage(self.handle) <= 0:
with self.signal_:
self.signal_.wait(timeout/1000.0)
if ArducamSDK.Py_ArduCam_availableImage(self.handle) <= 0:
return (False, None, None)
ret, data, cfg = ArducamSDK.Py_ArduCam_readImage(self.handle)
ArducamSDK.Py_ArduCam_del(self.handle)
size = cfg['u32Size']
if ret != 0 or size == 0:
return (False, data, cfg)
return (True, data, cfg)
def stop(self):
if not self.running_:
raise RuntimeError("The camera is not running.")
self.running_ = False
self.capture_thread_.join()
def closeCamera(self):
if not self.isOpened:
raise RuntimeError("The camera has not been opened.")
if (self.running_):
self.stop()
self.isOpened = False
ArducamSDK.Py_ArduCam_close(self.handle)
self.handle = None
def capture_thread(self):
ret = ArducamSDK.Py_ArduCam_beginCaptureImage(self.handle)
if ret != 0:
self.running_ = False
raise RuntimeError("Error beginning capture, Error : {}".format(GetErrorString(ret)))
print("Capture began, Error : {}".format(GetErrorString(ret)))
while self.running_:
ret = ArducamSDK.Py_ArduCam_captureImage(self.handle)
if ret > 255:
print("Error capture image, Error : {}".format(GetErrorString(ret)))
if ret == ArducamSDK.USB_CAMERA_USB_TASK_ERROR:
break
elif ret > 0:
with self.signal_:
self.signal_.notify()
self.running_ = False
ArducamSDK.Py_ArduCam_endCaptureImage(self.handle)
def setCtrl(self, func_name, val):
return ArducamSDK.Py_ArduCam_setCtrl(self.handle, func_name, val)
def dumpDeviceInfo(self):
USB_CPLD_I2C_ADDRESS=0x46
cpld_info={}
ret, version = ArducamSDK.Py_ArduCam_readReg_8_8(
self.handle, USB_CPLD_I2C_ADDRESS, 0x00)
ret, year = ArducamSDK.Py_ArduCam_readReg_8_8(
self.handle, USB_CPLD_I2C_ADDRESS, 0x05)
ret, mouth = ArducamSDK.Py_ArduCam_readReg_8_8(
self.handle, USB_CPLD_I2C_ADDRESS, 0x06)
ret, day = ArducamSDK.Py_ArduCam_readReg_8_8(
self.handle, USB_CPLD_I2C_ADDRESS, 0x07)
cpld_info["version"] = "v{}.{}".format(version>>4, version & 0x0F)
cpld_info["year"] = year
cpld_info["mouth"] = mouth
cpld_info["day"] = day
print(cpld_info)
ret, data = ArducamSDK.Py_ArduCam_getboardConfig(
self.handle, 0x80, 0x00, 0x00, 2
)
usb_info={}
usb_info["fw_version"] = "v{}.{}".format((data[0] & 0xFF), (data[1] & 0xFF))
usb_info["interface"] = 2 if self.cameraCfg["usbType"] == 4 else 3
usb_info["device"] = 3 if self.cameraCfg["usbType"] == 3 or self.cameraCfg["usbType"] == 4 else 2
print(usb_info)
def getCamInformation(self):
self.version = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 00)[1]
self.year = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 5)[1]
self.mouth = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 6)[1]
self.day = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 7)[1]
cpldVersion = "V{:d}.{:d}\t20{:0>2d}/{:0>2d}/{:0>2d}".format(self.version >> 4, self.version & 0x0F, self.year,
self.mouth, self.day)
return cpldVersion
def getMipiDataInfo(self):
mipiData = {"mipiDataID": "",
"mipiDataRow": "",
"mipiDataCol": "",
"mipiDataClk": "",
"mipiWordCount": "",
"mFramerateValue": ""}
self.getCamInformation()
cpld_version = self.version & 0xF0
date = (self.year * 1000 + self.mouth * 100 + self.day)
if cpld_version not in [0x20, 0x30]:
return None
if cpld_version == 0x20 and date < (19 * 1000 + 7 * 100 + 8):
return None
elif cpld_version == 0x30 and date < (19 * 1000 + 3 * 100 + 22):
return None
mipiDataID = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 0x1E)[1]
mipiData["mipiDataID"] = hex(mipiDataID)
rowMSB = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 0x21)[1]
rowLSB = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 0x22)[1]
mipiDataRow = ((rowMSB & 0xFF) << 8) | (rowLSB & 0xFF)
mipiData["mipiDataRow"] = str(mipiDataRow)
colMSB = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 0x1F)[1]
colLSB = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 0x20)[1]
mipiDataCol = ((colMSB & 0xFF) << 8) | (colLSB & 0xFF)
mipiData["mipiDataCol"] = str(mipiDataCol)
# after 2020/06/22
if cpld_version == 0x20 and date < (20 * 1000 + 6 * 100 + 22):
return mipiData
elif cpld_version == 0x30 and date < (20 * 1000 + 6 * 100 + 22):
return mipiData
mipiDataClk = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 0x27)[1]
mipiData["mipiDataClk"] = str(mipiDataClk)
if (cpld_version == 0x30 and date >= (21 * 1000 + 3 * 100 + 1)) or (
cpld_version == 0x20 and date >= (21 * 1000 + 9 * 100 + 6)):
wordCountMSB = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 0x25)[1]
wordCountLSB = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 0x26)[1]
mipiWordCount = ((wordCountMSB & 0xFF) << 8) | (wordCountLSB & 0xFF)
mipiData["mipiWordCount"] = str(mipiWordCount)
if date >= (21 * 1000 + 6 * 100 + 22):
fpsMSB = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 0x2A)[1]
fpsLSB = ArducamSDK.Py_ArduCam_readReg_8_8(self.handle, 0x46, 0x2B)[1]
fps = (fpsMSB << 8 | fpsLSB) / 4.0
fpsResult = "{:.1f}".format(fps)
mipiData["mFramerateValue"] = fpsResult
return mipiData