?? adcm1670_library.m
字號(hào):
function status = adcm1670_library(command);
%Agilent ADCM-1670 Camera Module Library.
% STATUS = ADCM1670_LIBRARY(COMMAND);
%
% Input Parameters:
% ================
%
% command -----------> Library function (string):
% 'open' - Open communications session.
% 'init' - Initialize camera module.
% 'frame' - Capture current frame.
% 'close' - Close communications session.
%
% Output Parameters:
% =================
%
% status ------------> Success status (-1 = error, +1 = success).
%
% See also N/A.
% Stephan Hengstler
% Stanford Wireless Sensor Networks Lab
% December 22, 2005
%
% Last modified: 02-09-2005
%************************ Agilent ADCM-1670 Camera Module Library *************************
%--- Section: Global Variables ------------------------------------------------------------
global colorm; % color map
global handle; % device handle
global imager; % image array
global PORT; % communications port
%--- Section: System Parameters -----------------------------------------------------------
CM_CFG_JPEG_GRAY = ... % Configuration: JPEG downsampling gray.
bin2dec('0010000000000000');
CM_CFG_JPEG_444 = ... % Configuration: JPEG downsampling 444.
bin2dec('0010000001000000');
CM_CFG_JPEG_422 = ... % Configuration: JPEG downsampling 442.
bin2dec('0010000010000000');
CM_INT_BAUDRATE = 115200; % Interface: serial baud rate.
CM_MSK_AEWBFB = ... % Bit mask: Auto exposure/white balance convergence
hex2dec('00C0'); % and frame buffer content.
CM_MSK_IA = ... % Bit mask: Image abort.
hex2dec('1000');
CM_MSK_JPEG_CFG = ... % Bit mask: JPEG configuration.
bin2dec('0010000011000000');
CM_MSK_SNAP = ... % Bit mask: Capture snapshot.
hex2dec('0001');
CM_REG_REV = '0000'; % Register: IC revision number.
CM_REG_CMD_1 = '0002'; % Register: Main command register 1.
CM_REG_CMD_2 = '0004'; % Register: Main command register 2.
CM_REG_VIDEO_CONFIG = '000C'; % Register: Video configuration register.
CM_REG_STILL_CONFIG = '000E'; % Register: Still configuration register.
CM_REG_UART_CREDITS = '0024'; % Register: UART credits.
CM_REG_SZR_OUT_W_STL = '0032'; % Register: Sizer output width, still mode.
CM_REG_SZR_OUT_H_STL = '0034'; % Register: Sizer output height, still mode.
CM_REG_STATUS_FLAGS = '0074'; % Register: General status flags.
CM_SEQ_BAUD = ['55';'55']; % Sequence: baud rate auto detection.
CM_SEQ_INIT = ... % Sequence: image pipeline initialization.
['00';'00';'20';'04';'00';'02';... % UART_PCKT_SIZE The size of one packet.
'00';'00';'24';'00';'00';'02';... % UART_CREDITS Number of current UART credits.
'00';'00';'0C';'B9';'40';'02';... % VIDEO_CONFIG Video configuration.
'00';'00';'0E';'B9';'40';'02';... % STILL_CONFIG Still configuration.
'00';'00';'26';'01';'60';'02';... % SZR_IN_W_VID Sizer input width, video mode.
'00';'00';'28';'01';'60';'02';... % SZR_IN_H_VID Sizer input height, video mode.
'00';'00';'2A';'01';'60';'02';... % SZR_OUT_W_VID Sizer output width, video mode.
'00';'00';'2C';'01';'60';'02';... % SZR_OUT_H_VID Sizer output width, video mode.
'00';'00';'2E';'01';'60';'02';... % SZR_IN_W_STL Sizer input width, still mode.
'00';'00';'30';'01';'60';'02';... % SZR_IN_H_STL Sizer input height, still mode.
'00';'00';'32';'01';'60';'02';... % SZR_OUT_W_STL Sizer output width, still mode.
'00';'00';'34';'01';'60';'02';... % SZR_OUT_H_STL Sizer output height, still mode.
'00';'00';'3A';'00';'00';'02';... % UFL_LIMIT_VID JPEG underflow limit, video mode.
'00';'00';'3E';'03';'C0';'02';... % OFL_TARGET_VID JPEG overflow target, video mode.
'00';'00';'40';'03';'C0';'02';... % OFL_LIMIT_VID JPEG overflow limit, video mode.
'00';'00';'42';'00';'00';'02';... % UFL_LIMIT_STL JPEG underflow limit, still mode.
'00';'00';'46';'03';'C0';'02';... % OFL_TARGET_STL JPEG overflow target, still mode.
'00';'00';'48';'03';'C0';'02';... % OFL_LIMIT_STL JPEG overflow limit, still mode.
'00';'01';'08';'00';'00';'02']; % T_DGEN_M Test data generator mode.
CM_SOP_VIEW_GRAY = hex2dec('2D'); % Start of packet viewfinder: Y only - grayscale.
CM_SOP_VIEW_JPEG = hex2dec('30'); % Start of packet viewfinder: JPEG.
CM_SOP_SNAP_GRAY = hex2dec('4D'); % Start of packet snapshot: Y only - grayscale.
CM_SOP_SNAP_JPEG = hex2dec('50'); % Start of packet snapshot: JPEG.
CM_SOB_CODE = hex2dec('11'); % Start of block code.
CM_EOB_CODE = hex2dec('04'); % End of block code.
CM_EOP_CODE = hex2dec('02'); % End of packet code.
%--- Command: Open Communications Session -------------------------------------------------
if (~isempty(strmatch('open',command,'exact')))
% open serial port to camera module
handle = com_open(PORT,CM_INT_BAUDRATE);
if (handle < 0), status = -1, return, end
% perform autobaud detection
packet = char(hex2dec(CM_SEQ_BAUD)');
status = com_send(handle,packet);
if (status < 0), return, end
% return status
status = 1;
%--- Command: Initialize Camera Module ----------------------------------------------------
elseif (~isempty(strmatch('init',command,'exact')))
% initialize camera module
packet = char(hex2dec(CM_SEQ_INIT)');
status = com_send(handle,packet);
if (status < 0), return, end
% configure image output format
% status = cm_write_register(handle,CM_REG_VIDEO_CONFIG,'9D80');
% status = cm_write_register(handle,CM_REG_STILL_CONFIG,'9D80');
% enable viewfinder mode
status = cm_write_register(handle,CM_REG_CMD_1,'0001');
if (status < 0), return, end
% get camera revision id
camera_id = cm_read_register(handle,CM_REG_REV)
% return status
status = 1;
%--- Command: Capture Current Frame -------------------------------------------------------
elseif (~isempty(strmatch('frame',command,'exact')))
% repeat until good image (TODO: temporary workaround for corrupt jpeg data)
while (1)
% wait until image abort is complete
while (bitand(hex2dec(cm_read_register(handle,CM_REG_CMD_2)),CM_MSK_IA))
end
% check auto exposure/white balance convergence and frame buffer content
while (~bitand(hex2dec(cm_read_register(handle,CM_REG_STATUS_FLAGS)),CM_MSK_AEWBFB))
end
% capture snapshot
status = cm_write_register(handle,CM_REG_CMD_2,dec2hex(CM_MSK_SNAP,4));
if (status < 0), return, end
% get image type and size
type = hex2dec(cm_read_register(handle,CM_REG_STILL_CONFIG));
width = hex2dec(cm_read_register(handle,CM_REG_SZR_OUT_W_STL));
height = hex2dec(cm_read_register(handle,CM_REG_SZR_OUT_H_STL));
% initialize image array
imager = [];
% read image data until EOP
while (1)
% set current uart credits to 1
status = cm_write_register(handle,CM_REG_UART_CREDITS,'0001');
if (status < 0), return, end
% read-out image data
packet = [];
timeout = 0;
while (isempty(packet) & (timeout < 10))
pause(0.010)
packet = com_recv(handle,1027);
timeout = timeout + 1;
end
% check for error
if (isempty(packet))
status = -1;
return;
end
% convert response packet
packet = dec2hex(bitand(double(packet),255),2);
% check response packet prefix
switch (hex2dec(packet(1,:)))
% start of block (SOB)?
case CM_SOB_CODE
offset = 2;
% disp('SOB')
% start of packet (SOP)?
case {CM_SOP_VIEW_GRAY,CM_SOP_VIEW_JPEG,CM_SOP_SNAP_GRAY,CM_SOP_SNAP_JPEG}
SOP = hex2dec(packet(1,:));
offset = 1;
% disp(['SOP 0x',packet(1,:)])
end
% build image array
imager = [imager uint8(hex2dec(packet(offset:end-2,:)))'];
% check response packet suffix
switch (hex2dec(packet(end-1,:)))
% end of packet (EOP)?
case CM_EOP_CODE
% disp('EOP')
break;
% end of block (EOB)?
case CM_EOB_CODE
% disp('EOB')
end
end
% discard current still frame to avoid double buffering
status = cm_write_register(handle,CM_REG_CMD_2,dec2hex(CM_MSK_IA,4));
if (status < 0), return, end
% typecast image array to double
imager = double(imager);
% reset warning messages
lastwarn('');
% decode image array according to output format
switch (SOP)
% output format: jpeg
case {CM_SOP_VIEW_JPEG,CM_SOP_SNAP_JPEG}
% get jpeg downsampling
type = bitand(type,CM_MSK_JPEG_CFG);
% add jpeg header to image array
jpeg = cmJpgHdrBuild(imager,type,width,height);
% write jpeg image to file
fid = fopen(['frame',PORT(end:end),'.jpg'],'w');
count = fwrite(fid,jpeg,'uchar');
status = fclose(fid);
if (status < 0), return, end
% read jpeg image from file
[imager,colorm] = imread(['frame',PORT(end:end),'.jpg'],'JPEG');
% output format: y only - grayscale
case {CM_SOP_VIEW_GRAY,CM_SOP_SNAP_GRAY}
% reshape image array accoding to image size
imager = (reshape(imager(1:end-1),width,height))';
end
% check for good image data
if (isempty(lastwarn))
break;
end
end
% typecast image array to double
imager = double(imager);
% return status
status = 1;
%--- Command: Close Communications Session ------------------------------------------------
elseif (~isempty(strmatch('close',command,'exact')))
% close serial port
com_close(handle);
% return status
status = 1;
%--- Command: Undefined -------------------------------------------------------------------
else
% return status
status = -1;
end
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -