domingo, 1 de marzo de 2015

TTL Serial JPEG Camera with Matlab



% This example take a picture using an Arduino hacked to be interfaced as serial conection to the camera

% THIS CODE WORKS PROPERLY NOW.

% Used the LinkSprite JPEG Color Camera TTL Interface from Adafruit. 

% rjuarez7@gmail.com      26th of feb 2015

s=serial('com4','baudrate',38400);
set(s,'timeout',10);
%Open serial port
fopen(s);

%Image size. I can select what size I want, in this case I select 640x480
%by puting a '00' at the end of this datagram
tam=hex2dec([ '56' ; '00'; '31'; '05'; '04' ; '00'; '19'; '00']);
fwrite(s,tam,'uint8');
retorno_tam=fread(s,5)

%reset
res=hex2dec([ '56' ; '00'; '26'; '00']);
fwrite(s,res,'uint8');
retorno_res=fread(s,4)

pause(4);

%Take a picture
protocolsign=hex2dec('56');
serialnumber=hex2dec('00');
command=hex2dec('36');
datalength=hex2dec('01');
data=hex2dec('00');

fwrite(s,protocolsign,'uint8');
fwrite(s,serialnumber,'uint8');
fwrite(s,command,'uint8');
fwrite(s,datalength,'uint8');
fwrite(s,data,'uint8');
retorno_getpicture=fread(s,5)

%Stop taking pictures
protocolsign=hex2dec('56');
serialnumber=hex2dec('00');
command=hex2dec('36');
datalength=hex2dec('01');
data=hex2dec('03');

fwrite(s,protocolsign,'uint8');
fwrite(s,serialnumber,'uint8');
fwrite(s,command,'uint8');
fwrite(s,datalength,'uint8');
fwrite(s,data,'uint8');
retorno_stop_picture=fread(s,5)


%Get Fbuffer_Length or read the picture length it is not constant due to
%light....
protocolsign=hex2dec('56');
serialnumber=hex2dec('00');
command=hex2dec('34');
datalength=hex2dec('01');
data=hex2dec('00');

fwrite(s,protocolsign,'uint8');
fwrite(s,serialnumber,'uint8');
fwrite(s,command,'uint8');
fwrite(s,datalength,'uint8');
fwrite(s,data,'uint8');


taille = fread(s,9)%Reads the first 9 bytes

longitud=hex2dec(dec2hex(256*taille(8)+taille(9))) %It Calculates the value in decimal of the picture  256xMSB+LSB


%Close serial port
fclose(s);

%Adjust Input buffer size of the serial object in Matlab to the image number of bytes or length
set(s,'inputbuffersize',longitud);

%Reopen Serial port
fopen(s);
[color=#0000FF]% Thanks to this delay , with the function "pause()", the port in Matlab is open and ready to send the command to the camera to read the image completely in the next lines.[/color]

[b]pause(5); [/b]

[b]%Read picture  or Read FBUF[/b]
protocolsign=hex2dec('56');
serialnumber=hex2dec('00');
command=hex2dec('32');
datalength=hex2dec('0c');
dirfinMSB=dec2hex(taille(8));
dirfinLSB=dec2hex(taille(9));
data=hex2dec([ '00' ; '0a'; '00'; '00'; '00'; '00'; '00'; '00'; dirfinMSB; dirfinLSB; 'ff' ;'ff']);
%To send the command  to read the image by writing on the serial object   s 
fwrite(s,protocolsign,'uint8');
fwrite(s,serialnumber,'uint8');
fwrite(s,command,'uint8');
fwrite(s,datalength,'uint8');
fwrite(s,data,'uint8');

B=fread(s,5) %getting response from camera
%pause(2);
if B(4)==0 % If status byte is "0"="success"
'yeah'; %be happy :)

[b]%read the bytes of the image and stores them in the variable buffer_cam
buffer_cam=fread(s,longitud); [/b]

% just for debugging gives the number of bytes in the varible tamano_buffer_cam
tamano_buffer_cam=size(buffer_cam) 

end


%Resume FBUFFER
protocolsign=hex2dec('56');
serialnumber=hex2dec('00');
command=hex2dec('36');
datalength=hex2dec('01');
data=hex2dec('02');
fwrite(s,protocolsign,'uint8');
fwrite(s,serialnumber,'uint8');
fwrite(s,command,'uint8');
fwrite(s,datalength,'uint8');
fwrite(s,data,'uint8');
fread(s,5)


fclose(s);

[color=#0000BF]%Creates the jpg file with the next code.

fid = fopen('foto.jpg', 'w');
fwrite(fid, buffer_cam, 'uint8');
fclose(fid);[/color]