forked from joelowney/PythonForPicam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.py
246 lines (191 loc) · 7.9 KB
/
Main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""
PythonForPicam is a Python ctypes interface to the Princeton Instruments PICAM Library
Copyright (C) 2013 Joe Lowney. The copyright holder can be reached at [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
"""Test for talking to Picam"""
import ctypes as ctypes
""" Import standard type definitions from PiTypes.py """
from PiTypes import *
""" Import non-standard type definitions from PiTypesMore.py """
from PiTypesMore import *
""" Import function definitions from PiFunctions.py """
""" This should contian all of the function from picam.h """
from PiFunctions import *
""" Import parameter lookup from PiParameterLookup.py """
""" This file includes a function PI_V and a lookup table to return the code
for different Picam Parameters described in chapter 4 """
from PiParameterLookup import *
############################
##### Custom Functions #####
############################
def pointer(x):
"""Returns a ctypes pointer"""
ptr = ctypes.pointer(x)
return ptr
def load(x):
"""Loads a library where argument is location of library"""
x = ctypes.cdll.LoadLibrary(x)
return x
#########################
##### Main Routine #####
#########################
if __name__ == '__main__':
""" Load the libpicam.so library """
# picamDll = 'C:/Users/becgroup/Documents/Python/DriverTest/Princeton Instruments/Picam/Runtime/Picam.dll'
picamLibrary = 'libpicam.so'
picam = load(picamLibrary)
print 'Initialize Camera.',Picam_InitializeLibrary()
print '\n'
""" Print version of PICAM """
major = piint()
minor = piint()
distribution = piint()
release = piint()
print 'Check Software Version. ',Picam_GetVersion(pointer(major),pointer(minor),pointer(distribution),pointer(release))
print 'Picam Version ',major.value,'.',minor.value,'.',distribution.value,' Released: ',release.value
print '\n'
## Test Routine to connect a demo camera
## p23
print 'Preparing to connect Demo Camera'
model = ctypes.c_int(428)
serial_number = ctypes.c_char_p('12345')
PicamID = PicamCameraID()
"""
PICAM_API Picam_ConnectDemoCamera(
PicamModel model,
const pichar* serial_number,
PicamCameraID* id );
"""
print 'Demo camera connected with return value = ',Picam_ConnectDemoCamera(model, serial_number, pointer(PicamID))
print '\n'
print 'Camera model is ',PicamID.model
print 'Camera computer interface is ',PicamID.computer_interface
print 'Camera sensor_name is ', PicamID.sensor_name
print 'Camera serial number is', PicamID.serial_number
print '\n'
## Test routine to open first camera
## p20
"""
PICAM_API Picam_OpenFirstCamera( PicamHandle* camera );
"""
camera = PicamHandle()
print 'Opening First Camera'
#print Picam_OpenFirstCamera(ctypes.addressof(camera))
print Picam_OpenFirstCamera(pointer(camera))
## Set ADC rate to high
# error = Picam_SetParameterFloatingPointValue(
# camera,
# PicamParameter_AdcSpeed,
# 4.0 );
# PrintError( error );
print Picam_SetParameterFloatingPointValue(camera, ctypes.c_int(PicamParameter_AdcSpeed), pi32f(4.0))
## Commit parameters:
failed_parameters = ctypes.c_int() # not sure this is "the right thing" but it seems to work
failed_parameters_count = piint()
print Picam_CommitParameters(camera, ctypes.byref(failed_parameters), ctypes.byref(failed_parameters_count))
print "Cleaning up..."
print Picam_DestroyParameters(failed_parameters)
## Test routine to acquire image
## p73
"""
PICAM_API Picam_Acquire(
PicamHandle camera,
pi64s readout_count,
piint readout_time_out,
PicamAvailableData* available,
PicamAcquisitionErrorsMask* errors );
"""
readoutstride = piint(0);
print "Getting readout stride. ", Picam_GetParameterIntegerValue( camera, ctypes.c_int(PicamParameter_ReadoutStride), ctypes.byref(readoutstride) );
"""
Prototype
PICAM_API Picam_Acquire(
PicamHandle camera,
pi64s readout_count,
piint readout_time_out,
PicamAvailableData* available,
PicamAcquisitionErrorsMask* errors );
"""
"""
typedef struct PicamAvailableData
{
void* initial_readout;
pi64s readout_count;
} PicamAvailableData;
"""
readout_count = pi64s(1)
readout_time_out = piint(-1) # same as NO_TIMEOUT?
available = PicamAvailableData()
""" Print Debug Information on initial readout """
print '\n'
print "available.initial_readout: ",available.initial_readout
print "Initial readout type is", type(available.initial_readout)
errors = PicamAcquisitionErrorsMask()
"""
Prototype
PICAM_API Picam_Acquire(
PicamHandle camera,
pi64s readout_count,
piint readout_time_out,
PicamAvailableData* available,
PicamAcquisitionErrorsMask* errors );
"""
Picam_Acquire.argtypes = PicamHandle, pi64s, piint, ctypes.POINTER(PicamAvailableData), ctypes.POINTER(PicamAcquisitionErrorsMask)
Picam_Acquire.restype = piint
print '\nAcquiring... '
print Picam_Acquire(camera, readout_count, readout_time_out, ctypes.byref(available), ctypes.byref(errors))
print '\n'
print 'step a'
print "available.initial_readout: ",available.initial_readout
print "Initial readout type is", type(available.initial_readout)
print '\n'
print '\nAcquiring again... '
print Picam_Acquire(camera, readout_count, readout_time_out, ctypes.byref(available), ctypes.byref(errors))
print '\n'
print 'step b'
print "available.initial_readout: ",available.initial_readout
print "Initial readout type is", type(available.initial_readout)
print '\n'
""" Close out Library Resources """
## Disconnected the above cameras
print 'Disconnecting demo camera...'
print Picam_DisconnectDemoCamera(pointer(PicamID))
""" Test Routine to Access Data """
""" Create an array type to hold 1300x400 16bit integers """
DataArrayType = pi16u*520000
""" Create pointer type for the above array type """
DataArrayPointerType = ctypes.POINTER(pi16u*520000)
""" Create an instance of the pointer type, and point it to initial readout contents (memory address?) """
DataPointer = ctypes.cast(available.initial_readout,DataArrayPointerType)
""" Create a separate array with readout contents """
data = DataPointer.contents
""" Write contents of Data to binary file"""
libc = ctypes.CDLL('libc.so.6')
fopen = libc.fopen
fopen.argtypes = ctypes.c_char_p, ctypes.c_char_p
fopen.restype = ctypes.c_void_p
fwrite = libc.fwrite
fwrite.argtypes = ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t, ctypes.c_void_p
fwrite.restype = ctypes.c_size_t
fclose = libc.fclose
fclose.argtypes = ctypes.c_void_p,
fclose.restype = ctypes.c_int
fp = fopen('PythonBinOutput.raw', 'wb')
print 'fwrite returns: ',fwrite(data, readoutstride.value, 1, fp)
fclose(fp)
## Close camera
print "Closing camera..."
print Picam_CloseCamera(camera)
## Close down library
print 'Uninitializing...'
print Picam_UninitializeLibrary()