Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Instax SP-1 Android Source
package com.Instax.sdk;
import java.io.*;
import java.util.*;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Color;
import android.util.Log;
import com.Instax.sdk.InstaxStatus.ErrCode;
import com.Instax.sdk.InstaxStatus.InstaxState;
class InstaxProtocol {
private NetworkCommunicationParam Param = new NetworkCommunicationParam();
private InstaxStatus State = new InstaxStatus();
private NetworkCommunicationSndImageThread CommSndImageThread = null;
private NetworkCommunicationPrintThread CommPrintThread = null;
private int UID = (int)(System.currentTimeMillis());
private final int EMERGENCY_ERR_LEVEL = 2;
/**
* setNetworkParam
* @param ipAddress
* @param port
*/
void setNetworkParam(String ipAddress, int port) {
InstaxLog.putLogToSD("IP:"+ipAddress);
InstaxLog.putLogToSD("Port:"+String.valueOf(port));
Param.setNetworkParam(ipAddress, port);
}
/**
*
* @param pass
*/
void setSecurityPassword(int pass) {
InstaxLog.putLogToSD("Pass:"+String.valueOf(pass));
Param.setPassword(pass);
}
/**
* InstaxLock
* @param flg
* @return
*/
InstaxStatus InstaxLock(byte flg) {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
int DataSize = 4;
byte[] Data = new byte[DataSize];
short Sid = 0xB3;
ErrCode err = ErrCode.RET_OK;
// ‘—ŽóM
Data[0] = flg;
Data[1] = 0x00;
Data[2] = 0x00;
Data[3] = 0x00;
Frame.CreateSndData( UID, Sid, Data, DataSize, Param.Password );
err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK ) {
State.setInstaxStatus(InstaxState.ST_BUSY, err);
return State;
}
// ŽóMƒf[ƒ^ƒ`ƒFƒbƒN
State = Frame.checkResponseFrame(0);
return State;
}
/**
* getShadingData(0x40)
* @param shadingData
* @param size
* @return
*/
InstaxStatus getShadingData(byte[] shadingData, int size) {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
int DataSize = 4;
byte[] Data = new byte[DataSize];
short Sid = 0x40;
ErrCode err = ErrCode.RET_OK;
for(int i=0; i<3; i++) {
Data[0] = 0x00;
Data[1] = 0x00;
Data[2] = 0x00;
Data[3] = (byte)i;
// ‘—ŽóM
Frame.CreateSndData( UID, Sid, Data, DataSize, Param.Password );
err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK ) {
State.setInstaxStatus(InstaxState.ST_BUSY, err);
return State;
}
// ŽóMƒf[ƒ^ƒ`ƒFƒbƒN
State = Frame.checkResponseFrame( size );
//if( State.ErrCode != ErrCode.RET_OK ) {
if( judgeErrLevel(State) >= EMERGENCY_ERR_LEVEL ) {
return State;
}
byte[] tmpData = new byte[size];
if( Frame.getResponseArrayData(0, tmpData, size) != 0 ) {
err = ErrCode.E_RCV_FRAME;
State.setInstaxStatus(InstaxState.ST_BUSY,err);
break;
}
System.arraycopy(tmpData, 0, shadingData, i*size, size);
}
return State;
}
/**
*
* @param imageData
* @param printWidth
* @param printHeight
* @param sndFinish
* @param shadingData
* @param option
* @return
*/
//InstaxStatus sndImage(Bitmap imageData, int printWidth, int printHeight, InstaxCallBack sndFinish, byte[] shadingData) {
InstaxStatus sndImage(Bitmap imageData, int printWidth, int printHeight, InstaxCallBack sndFinish, byte[] shadingData, String option) {
NetworkCommunication sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
ErrCode err = ErrCode.RET_OK;
int DataSize;
byte[] Data;
short Sid;
if( CommSndImageThread != null ) {
if( CommSndImageThread.isAlive() ) {
// ‘OƒXƒŒƒbƒh‚ª‚Ü‚¾¶‚«‚Ä‚¢‚é
State.setInstaxStatus(InstaxState.ST_BUSY, ErrCode.E_CONNECT);
sndFinish.FinishCallBack(State);
return State;
}
}
// 0x50
DataSize = 0;
Data = new byte[DataSize];
Sid = 0x50;
//Data[0] = 0x00; // RAM page
// ‘—ŽóM
Frame.CreateSndData( UID, Sid, Data, DataSize, Param.Password );
err = sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK ) {
//InstaxLock((byte)0x00);
State.setInstaxStatus(InstaxState.ST_BUSY, err);
sndFinish.FinishCallBack(State);
return State;
}
State = Frame.checkResponseFrame( 0 );
if( State.ErrCode != ErrCode.RET_OK ) {
//InstaxLock((byte)0x00);
sndFinish.FinishCallBack(State);
return State;
}
// ‰æ‘œƒf[ƒ^‰ÁH
int sndMaxSize = Param.MaxFrameSize;
int width = imageData.getWidth(); // ‰æ‘œ•
int height = imageData.getHeight(); // ‰æ‘œ‚‚³
int imageSize = width * height; // ‰æ‘œƒTƒCƒY
int printSize = printWidth * printHeight; // ˆóüƒTƒCƒY
int[] printRGB = new int[printSize];
int[] imageRGB = new int[imageSize];
int x,y;
// ‰Šú‰»
Arrays.fill(printRGB, 0x00FFFFFF); // ”’‰æ–Ê‚ð—pˆÓ
// ƒsƒNƒZƒ‹î•ñGET
imageData.getPixels( imageRGB, 0, width, 0, 0, width, height );
// ƒRƒs[ŠJŽnÀ•WŽZo
x = (printWidth - width)/2;
y = (printHeight - height)/2;
// ƒRƒs[
for(int i=0; i<height; i++) {
// ”’‰æ–ʂɉ摜‚ðƒRƒs[
System.arraycopy(imageRGB, i*width, printRGB, (i+y)*printWidth+x, width);
}
imageRGB = null;
// ‚±‚±‚Ü‚Å‚Å”’–„‚߉摜‚ªo—ˆ‚Ä‚¢‚é
// RBG•Ê‚É•ª‰ð+ƒVƒF[ƒfƒBƒ“ƒO•â³
// R(0,0),R(0,1),...,R(0,479), G(0,0),G(0,1),...,G(0,479), B(0,0),B(0,1),...,B(0,479),
// R(1,0),R(1,1),...
if( shadingData.length < 3*printWidth ) {
// ƒVƒF[ƒfƒBƒ“ƒOƒf[ƒ^Žæ“¾‚µ‚Ä‚¢‚È‚©‚Á‚½‚ç‚Æ‚è‚ ‚¦‚¸rank‚O‚ŕ␳‚©‚¯‚¿‚Ⴄ
shadingData = new byte[3*printWidth];
Arrays.fill(shadingData, (byte)0x00);
}
Data = new byte[printSize*3];
ShadingTableAccess shadingTableAccess = new ShadingTableAccess();
int beforeR, beforeG, beforeB;
for(int i=0; i<printHeight; i++) {
for(int j=0; j<printWidth; j++ ) {
// •â³Œã‚Ì’l‚ð“ü‚ê‚é
int idx = i*printWidth+j;
beforeR = Color.red(printRGB[idx]);
beforeG = Color.green(printRGB[idx]);
beforeB = Color.blue(printRGB[idx]);
// (i*printWidth*3)+j+0*printWidth ¨ (i*3+0)*printWidth+j
Data[(i*3+0)*printWidth+j] = shadingTableAccess.getTableDataR( ((int)shadingData[j+0*printWidth] & 0xFF),beforeR );
Data[(i*3+1)*printWidth+j] = shadingTableAccess.getTableDataG( ((int)shadingData[j+1*printWidth] & 0xFF),beforeG );
Data[(i*3+2)*printWidth+j] = shadingTableAccess.getTableDataB( ((int)shadingData[j+2*printWidth] & 0xFF),beforeB );
}
}
shadingTableAccess = null;
// JPG:ˆ³k
if(Param.IMAGE_Format == 2)
{
final int encodedSizeMax = 150*1024; // 150k
// byteŒ^”z—ñ‚ðintŒ^”z—ñ‚É–ß‚·
int alpha = 0xFF000000; // ƒ¿’l‚ðŽg‚í‚È‚¢‚Æ‚«‚Í0xFF‚ð“ü‚ê‚é
for(int i=0; i<printHeight; i++) {
for(int j=0; j<printWidth; j++ ) {
beforeR = ((((int)Data[(i*3+0)*printWidth+j]) << 16) & 0xFF0000);
beforeG = ((((int)Data[(i*3+1)*printWidth+j]) << 8) & 0x00FF00);
beforeB = ((((int)Data[(i*3+2)*printWidth+j]) << 0) & 0x0000FF);
printRGB[i*printWidth+j] = (alpha | beforeR | beforeG | beforeB);
}
}
// Android‚ÌBitmapƒNƒ‰ƒX‚ð—˜—p‚µ‚ÄJPGˆ³k
//if(Param.Port == 8080)
{
Bitmap bmp = Bitmap.createBitmap(printRGB, printWidth, printHeight, Bitmap.Config.ARGB_8888);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int encodedSize;
int jpgQuality = 100; // ‰Šú’l100
Log.d("jpeg","encodedSizeMax:"+String.valueOf(encodedSizeMax));
while(true) {
bmp.compress(CompressFormat.JPEG, jpgQuality, baos);
encodedSize = baos.size();
Log.d("jpeg","Quality:"+String.valueOf(jpgQuality));
Log.d("jpeg","encodedSize:"+String.valueOf(encodedSize));
if(encodedSize <= encodedSizeMax) {
// ˆ³kŒãƒTƒCƒY‚ªÅ‘å—e—Ê‚æ‚菬‚³‚©‚Á‚½‚甲‚¯‚é
String log = String.format("JPG Quality:%d", jpgQuality);
InstaxLog.putLogToSD(log);
break;
}
baos.reset();
jpgQuality -= 10; // •iŽ¿‚ð—Ž‚Æ‚·
if(jpgQuality < 0) {
// Å’á•iŽ¿‚Å‚àÅ‘å—e—ʈȉº‚É‚¨‚³‚Ü‚ç‚È‚¢‚̂ŃGƒ‰[
encodedSize = 0;
baos.reset();
Log.e("jpeg","encode fail");
break;
}
}
if(encodedSize > 0) {
Data = new byte[encodedSize];
Data = baos.toByteArray();
}
else {
printRGB = null;
State.setInstaxStatus(InstaxState.ST_BUSY, ErrCode.E_MEMORY);
sndFinish.FinishCallBack(State);
return State;
}
}
// MT»‚ÌJPGˆ³k
//else
// {
// // ˆ³k—¦‚̕ύX•û–@•s–¾
// for(int i=0; i<printWidth*printHeight; i++) {
// Data[i*3+0] = (byte)((printRGB[i] >> 16) & 0xFF);
// Data[i*3+1] = (byte)((printRGB[i] >> 8) & 0xFF);
// Data[i*3+2] = (byte)((printRGB[i] >> 0) & 0xFF);
// }
// //Arrays.fill(Data, (byte)0xFF);
// JpegEncode jpg = new JpegEncode(Data);
// JpegEncodeInfo info = new JpegEncodeInfo();
// info.uswNumSamplesPerLine = printWidth;
// info.uswNumLines = printHeight;
// jpg.jpeg_encode(info);
// Data = new byte[(int)info.uinInputFileSize];
// //Log.d("jpeg", "size:"+String.valueOf(info.uinInputFileSize));
// System.arraycopy(info.pbOutputData, 0, Data, 0, (int)info.uinInputFileSize);
// }
}
printRGB = null;
/*
* ƒXƒŒƒbƒh“à‚Ɉړ®
// 0x51
int imgSize = Data.length;
DataSize = 12;
Data = new byte[DataSize];
Sid = 0x51;
Data[0] = (byte)Param.JpgFlg; // ‰æ‘œƒtƒH[ƒ}ƒbƒg
Data[1] = 0x00; // pic opt
Data[2] = (byte)(((int)imgSize >> 24) & 0xFF); // size
Data[3] = (byte)(((int)imgSize >> 16) & 0xFF);
Data[4] = (byte)(((int)imgSize >> 8) & 0xFF);;
Data[5] = (byte)(((int)imgSize >> 0) & 0xFF);;
Data[6] = 0x00; // jpg param
Data[7] = 0x00;
Data[8] = 0x00;
Data[9] = 0x00;
Data[10] = 0x00; // reserve
Data[11] = 0x00; // reserve
// ‘—ŽóM
Frame.CreateSndData( UID, Sid, Data, DataSize );
err = sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK ) {
ModeReset();
State.setInstaxStatus(InstaxState.ST_BUSY, err);
return State;
}
State = Frame.checkResponseFrame( 0 );
if( State.ErrCode != ErrCode.RET_OK ) {
ModeReset();
return State;
}
*/
String log = String.format("IMG Size:%d", Data.length);
InstaxLog.putLogToSD(log);
Param.IMAGE_Option = AnalyzeOption(option);
// 0x51,0x52,0x53
Sid = 0x52;
CommSndImageThread = new NetworkCommunicationSndImageThread(Param, UID, Sid, Data, sndMaxSize, sndFinish);
CommSndImageThread.start();
return State;
}
/*
*
*/
private int AnalyzeOption(String option) {
int ret = 0;
if( option == null ) {
return ret;
}
InstaxLog.putLogToSD("Tag:"+option);
// ‚·‚ׂđ啶Žš‚É•ÏŠ·
option = option.toUpperCase();
// FujifilmOptionŒŸõ
if( option.indexOf("FUJIFILMOPTION") == -1 ) {
InstaxLog.putLogToSD("Tag:"+"0");
return ret;
}
//@CLEARALLŒŸõ
if( option.indexOf("CLEARALL") != -1 ) {
ret |= 0x10;
}
// FORCEPRINTŒŸõ
if( option.indexOf("FORCEPRT") != -1 ) {
ret |= 0x30;
}
// LUTŒŸõ
int index;
if( (index=option.indexOf("LUT")) != -1 ) {
// "LUT"‚ÌŽŸ‚Ì•¶Žš‚𒊏o
String tmp = option.substring(index+3, index+4);
int num = 0;
try {
// ”’l‚É•ÏŠ·
num = Integer.parseInt(tmp);
} catch(NumberFormatException e) {
num = 0;
}
ret |= (0x10+num);
}
String log = String.format("Tag:%02X", ret);
InstaxLog.putLogToSD(log);
return ret;
}
/**
* getSndImageProgress
* @return
*/
int getSndImageProgress() {
if( CommSndImageThread == null || !CommSndImageThread.isAlive() ) {
return 0;
}
return CommSndImageThread.getProgress();
}
/**
* sndImageCancel
* @param cancelFinish
* @return
*/
InstaxStatus sndImageCancel(InstaxCallBack cancelFinish) {
if( CommSndImageThread == null || !CommSndImageThread.isAlive() ) {
State.setInstaxStatus(InstaxState.ST_IDLE,ErrCode.RET_OK);
cancelFinish.FinishCallBack(State);
return State;
}
CommSndImageThread.SndImageCancel(cancelFinish);
State.setInstaxStatus(InstaxState.ST_IDLE,ErrCode.RET_OK);
return State;
}
/**
* startPrint(0xB0)
* @param printFinish
* @return
*/
InstaxStatus startPrint(InstaxCallBack printFinish) {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
int DataSize = 0;
byte[] Data = new byte[DataSize];
short Sid = 0xB0;
ErrCode err = ErrCode.RET_OK;
if( CommPrintThread != null ) {
if( CommPrintThread.isAlive() ) {
// ‘OƒXƒŒƒbƒh‚ª‚Ü‚¾¶‚«‚Ä‚¢‚é
State.setInstaxStatus(InstaxState.ST_BUSY,err);
printFinish.FinishCallBack(State);
return State;
}
}
// ‘—M
Frame.CreateSndData( UID, Sid, Data, DataSize, Param.Password );
err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK) {
State.setInstaxStatus(InstaxState.ST_BUSY,err);
printFinish.FinishCallBack(State);
return State;
}
// ŽóMƒf[ƒ^ƒ`ƒFƒbƒN
State = Frame.checkResponseFrame(4);
if( State.ErrCode != ErrCode.RET_OK ) {
printFinish.FinishCallBack(State);
return State;
}
int printTime = Frame.getResponselongData(0)/10; // 100ms’PˆÊ‚Å‚­‚é‚Ì‚Å1/10‚µ‚Ä•b‚É‚·‚é
CommPrintThread = new NetworkCommunicationPrintThread(Param, UID, (short)0xC3, printTime, printFinish);
CommPrintThread.start();
return State;
}
/**
* getPrintProgress
* @return
*/
int getPrintProgress() {
if( CommPrintThread == null || !CommPrintThread.isAlive() ) {
return 0;
}
return CommPrintThread.getProgress();
}
/**
* printCancel
* @param cancelFinish
* @return
*/
InstaxStatus printCancel(InstaxCallBack cancelFinish) {
if( CommPrintThread == null || !CommPrintThread.isAlive() ) {
State.setInstaxStatus(InstaxState.ST_IDLE,ErrCode.RET_OK);
cancelFinish.FinishCallBack(State);
return State;
}
CommPrintThread.PrintCancel(cancelFinish);
State.setInstaxStatus(InstaxState.ST_IDLE,ErrCode.RET_OK);
return State;
}
/**
* getVersion(0xC0)
* @param info
* @return
*/
InstaxStatus getVersion(InstaxInfo info) {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
ErrCode err = ErrCode.RET_OK;
int DataSize = 0;
byte[] Data = new byte[DataSize];
short Sid = 0xC0;
// ƒf[ƒ^ì¬
Frame.CreateSndData( UID, Sid, Data, DataSize, Param.Password );
// ‘—ŽóM
err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK) {
State.setInstaxStatus(InstaxState.ST_BUSY,err);
return State;
}
// ŽóMƒf[ƒ^Žæ“¾
State = Frame.checkResponseFrame(8);
//if( State.ErrCode != ErrCode.RET_OK ) {
if( judgeErrLevel(State) >= EMERGENCY_ERR_LEVEL ) {
return State;
}
info.BootVer = Frame.getResponseShortData( 0 );
info.FwVer = Frame.getResponseShortData( 2 );
info.FpgaVer = Frame.getResponseShortData( 4 );
return State;
}
/**
* getInstaxParameter(0xC1)
* @param info
* @return
*/
InstaxStatus getInstaxParameter(InstaxInfo info) {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
int DataSize = 0;
byte[] Data = new byte[DataSize];
short Sid = 0xC1;
// ƒf[ƒ^ì¬
Frame.CreateSndData( UID, Sid, Data, DataSize, Param.Password );
// ‘—ŽóM
ErrCode err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK) {
State.setInstaxStatus(InstaxState.ST_BUSY,err);
return State;
}
// ŽóMƒf[ƒ^Žæ“¾
State = Frame.checkResponseFrame(12);
//if( State.ErrCode != ErrCode.RET_OK ) {
if( judgeErrLevel(State) >= EMERGENCY_ERR_LEVEL ) {
return State;
}
int temp;
info.PrintCount = Frame.getResponselongData(0);
//temp = Frame.getResponseSignedShortData( 4 );
//info.InstaxTemp = (float)temp/10;
switch(Frame.getInfoBattery()) {
case 0x00:
case 0x01:
case 0x02:
case 0x03:
temp = Frame.getInfoBattery();
break;
case 0x04:
default:
temp = -1; // DC
break;
}
info.Battery = temp;
info.FilmNum = Frame.getInfoFilm();
if( info.FilmNum > 10 ) {
info.FilmNum = 0;
}
info.FilmMax = 10;
info.ImageHeight = 640;
info.ImageWidth = 480;
return State;
}
/**
* getInstaxName(0xC2)
* @param info
* @return
*/
InstaxStatus getInstaxName(InstaxInfo info) {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
int DataSize = 0;
byte[] Data = new byte[DataSize];
short Sid = 0xC2;
// ƒf[ƒ^ì¬
Frame.CreateSndData( UID, Sid, Data, DataSize, Param.Password );
// ‘—ŽóM
ErrCode err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK) {
State.setInstaxStatus(InstaxState.ST_BUSY,err);
return State;
}
// ŽóMƒf[ƒ^Žæ“¾
State = Frame.checkResponseFrame(0);
//if( State.ErrCode != ErrCode.RET_OK ) {
if( judgeErrLevel(State) >= EMERGENCY_ERR_LEVEL ) {
return State;
}
info.TargetName = Frame.getResponseStringData(0);
return State;
}
/**
* getInstaxStatus
* @return
*/
InstaxStatus getInstaxStatus() {
InstaxInfo info = new InstaxInfo();
State = getInstaxName(info);
return State;
}
/**
* setCommunicationParam
* @param sndTimeOut
* @param rcvTimeOut
* @param retryCnt
*/
void setCommunicationParam(int sndTimeOut, int rcvTimeOut, int retryCnt) {
InstaxLog.putLogToSD("SndTimeOut:"+String.valueOf(sndTimeOut));
InstaxLog.putLogToSD("RcvTimeOut:"+String.valueOf(rcvTimeOut));
InstaxLog.putLogToSD("RetryCount:"+String.valueOf(retryCnt));
Param.setCommunicationParam(sndTimeOut, rcvTimeOut, retryCnt);
}
/**
*
* @param sndMaxSize
* @param delayTime
*/
void setDebugParam(int sndMaxSize, int delayTime, int jpgFlg, int jpgOpt) {
Param.setDebugParam(sndMaxSize, delayTime, jpgFlg, jpgOpt);
}
/**
*
* @param param
*/
void getDebugParam(InstaxPrintSetting param) {
param.MaxFrameSize = this.Param.MaxFrameSize;
param.DelayTime = this.Param.DelayTime;
param.IMAGE_Format = this.Param.IMAGE_Format;
param.IMAGE_Option = this.Param.IMAGE_Option;
}
/**
* setLedElectricAndWhiteMark(0x30)
* @param param
* @return
*/
/*
InstaxStatus setLedElectricAndWhiteMark(InstaxPrintSetting param) {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
int DataSize = 8;
byte[] Data = new byte[DataSize];
short Sid = 0x30;
// ƒf[ƒ^ì¬
Data[0] = (byte)((param.LedRed >> 8) & 0xFF);
Data[1] = (byte)((param.LedRed >> 0) & 0xFF);
Data[2] = (byte)((param.LedGreen >> 8) & 0xFF);
Data[3] = (byte)((param.LedGreen >> 0) & 0xFF);
Data[4] = (byte)((param.LedBlue >> 8) & 0xFF);
Data[5] = (byte)((param.LedBlue >> 0) & 0xFF);
Data[6] = 0x00;
Data[7] = 0x00;
// Data[6] = (byte)((param.BeforeWhite >> 8) & 0xFF);
// Data[7] = (byte)((param.BeforeWhite >> 0) & 0xFF);
// Data[8] = (byte)((param.AfterWhite >> 8) & 0xFF);
// Data[9] = (byte)((param.AfterWhite >> 0) & 0xFF);
Frame.CreateSndData( UID, Sid, Data, DataSize );
// ‘—ŽóM
ErrCode err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK) {
State.setInstaxStatus(InstaxState.ST_BUSY,err);
return State;
}
// ŽóMƒf[ƒ^Žæ“¾
State = Frame.checkResponseFrame(0);
return State;
}
*/
/**
* setLcs(0x31)
* @param param
* @return
*/
/*
InstaxStatus setLcs(InstaxPrintSetting param) {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
int DataSize = 22;
byte[] Data = new byte[DataSize];
short Sid = 0x31;
// ƒf[ƒ^ì¬
Data[0] = (byte)((param.LpCycle >> 8) & 0xFF);
Data[1] = (byte)((param.LpCycle >> 0) & 0xFF);
Data[2] = (byte)((param.ExposureRed >> 8) & 0xFF);
Data[3] = (byte)((param.ExposureRed >> 0) & 0xFF);
Data[4] = (byte)((param.ExposureGreen >> 8) & 0xFF);
Data[5] = (byte)((param.ExposureGreen >> 0) & 0xFF);
Data[6] = (byte)((param.ExposureBlue >> 8) & 0xFF);
Data[7] = (byte)((param.ExposureBlue >> 0) & 0xFF);
Data[8] = (byte)((param.OpenRed >> 8) & 0xFF);
Data[9] = (byte)((param.OpenRed >> 0) & 0xFF);
Data[10] = (byte)((param.OpenGreen >> 8) & 0xFF);
Data[11] = (byte)((param.OpenGreen >> 0) & 0xFF);
Data[12] = (byte)((param.OpenBlue >> 8) & 0xFF);
Data[13] = (byte)((param.OpenBlue >> 0) & 0xFF);
Data[14] = (byte)((param.CloseRed >> 8) & 0xFF);
Data[15] = (byte)((param.CloseRed >> 0) & 0xFF);
Data[16] = (byte)((param.CloseGreen >> 8) & 0xFF);
Data[17] = (byte)((param.CloseGreen >> 0) & 0xFF);
Data[18] = (byte)((param.CloseDummy >> 8) & 0xFF);
Data[19] = (byte)((param.CloseDummy >> 0) & 0xFF);
Data[20] = (byte)((param.ExposureDummy >> 8) & 0xFF);
Data[21] = (byte)((param.ExposureDummy >> 0) & 0xFF);
Frame.CreateSndData( UID, Sid, Data, DataSize );
// ‘—ŽóM
ErrCode err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK) {
State.setInstaxStatus(InstaxState.ST_BUSY,err);
return State;
}
// ŽóMƒf[ƒ^Žæ“¾
State = Frame.checkResponseFrame(0);
return State;
}
*/
/**
* setMovementAndMotorSpeedAndEnergisationMode(0xE1)
* @param param
* @return
*/
/*
InstaxStatus setMovementAndMotorSpeedAndEnergisationMode(InstaxPrintSetting param) {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
int DataSize = 28;
byte[] Data = new byte[DataSize];
short Sid = 0xE1;
// ƒf[ƒ^ì¬
Data[0] = (byte)((param.BeforeWhite >> 8) & 0xFF);
Data[1] = (byte)((param.BeforeWhite >> 0) & 0xFF);
Data[2] = (byte)((param.AfterWhite >> 8) & 0xFF);
Data[3] = (byte)((param.AfterWhite >> 0) & 0xFF);
Data[4] = (byte)((param.StepPerLine >> 8) & 0xFF);
Data[5] = (byte)((param.StepPerLine >> 0) & 0xFF);
Data[6] = (byte)((param.OrgToPiStep >> 8) & 0xFF);
Data[7] = (byte)((param.OrgToPiStep >> 0) & 0xFF);
Data[8] = (byte)((param.SpeedUpStep >> 8) & 0xFF);
Data[9] = (byte)((param.SpeedUpStep >> 0) & 0xFF);
Data[10] = (byte)((param.SpeedDnStep >> 8) & 0xFF);
Data[11] = (byte)((param.SpeedDnStep >> 0) & 0xFF);
Data[12] = (byte)((param.MainStep >> 8) & 0xFF);
Data[13] = (byte)((param.MainStep >> 0) & 0xFF);
Data[14] = (byte)((param.AfterPrintStep >> 8) & 0xFF);
Data[15] = (byte)((param.AfterPrintStep >> 0) & 0xFF);
Data[16] = (byte)((param.ReturnStep >> 8) & 0xFF);
Data[17] = (byte)((param.ReturnStep >> 0) & 0xFF);
Data[18] = (byte)((param.LowSpeed >> 8) & 0xFF);
Data[19] = (byte)((param.LowSpeed >> 0) & 0xFF);
Data[20] = (byte)((param.PrintSpeed >> 8) & 0xFF);
Data[21] = (byte)((param.PrintSpeed >> 0) & 0xFF);
Data[22] = (byte)((param.ReturnSpeed >> 8) & 0xFF);
Data[23] = (byte)((param.ReturnSpeed >> 0) & 0xFF);
Data[24] = (byte)((param.Excitation >> 0) & 0xFF);
Data[25] = (byte)((param.LedOpen >> 0) & 0xFF);
Data[26] = 0x00;
Data[27] = 0x00;
Frame.CreateSndData( UID, Sid, Data, DataSize );
// ‘—ŽóM
ErrCode err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK) {
State.setInstaxStatus(InstaxState.ST_BUSY,err);
return State;
}
// ŽóMƒf[ƒ^Žæ“¾
State = Frame.checkResponseFrame(0);
return State;
}
*/
/**
* getLedElectricAndWhiteMark(0x40)
* @param param
* @return
*/
/*
InstaxStatus getLedElectricAndWhiteMark(InstaxPrintSetting param) {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
ErrCode err = ErrCode.RET_OK;
int DataSize = 0;
byte[] Data = new byte[DataSize];
short Sid = 0x40;
// ƒf[ƒ^ì¬
Frame.CreateSndData( UID, Sid, Data, DataSize );
// ‘—ŽóM
err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK) {
State.setInstaxStatus(InstaxState.ST_BUSY,err);
return State;
}
// ŽóMƒf[ƒ^Žæ“¾
State = Frame.checkResponseFrame(8);
//if( State.ErrCode != ErrCode.RET_OK ) {
if( judgeErrLevel(State) >= EMERGENCY_ERR_LEVEL ) {
return State;
}
param.LedRed = Frame.getResponseShortData( 0 );
param.LedGreen = Frame.getResponseShortData( 2 );
param.LedBlue = Frame.getResponseShortData( 4 );
// param.BeforeWhite = Frame.getResponseShortData( 6 );
// param.AfterWhite = Frame.getResponseShortData( 8 );
return State;
}
*/
/**
* getLcs(0x41)
* @param param
* @return
*/
/*
InstaxStatus getLcs(InstaxPrintSetting param) {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
ErrCode err = ErrCode.RET_OK;
int DataSize = 0;
byte[] Data = new byte[DataSize];
short Sid = 0x41;
// ƒf[ƒ^ì¬
Frame.CreateSndData( UID, Sid, Data, DataSize );
// ‘—ŽóM
err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK) {
State.setInstaxStatus(InstaxState.ST_BUSY,err);
return State;
}
// ŽóMƒf[ƒ^Žæ“¾
State = Frame.checkResponseFrame(22);
//if( State.ErrCode != ErrCode.RET_OK ) {
if( judgeErrLevel(State) >= EMERGENCY_ERR_LEVEL ) {
return State;
}
param.LpCycle = Frame.getResponseShortData( 0 );
param.ExposureRed = Frame.getResponseShortData( 2 );
param.ExposureGreen = Frame.getResponseShortData( 4 );
param.ExposureBlue = Frame.getResponseShortData( 6 );
param.OpenRed = Frame.getResponseShortData( 8 );
param.OpenGreen = Frame.getResponseShortData( 10 );
param.OpenBlue = Frame.getResponseShortData( 12 );
param.CloseRed = Frame.getResponseShortData( 14 );
param.CloseGreen = Frame.getResponseShortData( 16 );
param.CloseDummy = Frame.getResponseShortData( 18 );
param.ExposureDummy = Frame.getResponseShortData( 20 );
return State;
}
*/
/**
* getMovementAndMotorSpeedAndEnergisationMode(0xE2)
* @param param
* @return
*/
/*
InstaxStatus getMovementAndMotorSpeedAndEnergisationMode(InstaxPrintSetting param) {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
ErrCode err = ErrCode.RET_OK;
int DataSize = 0;
byte[] Data = new byte[DataSize];
short Sid = 0xE2;
// ƒf[ƒ^ì¬
Frame.CreateSndData( UID, Sid, Data, DataSize );
// ‘—ŽóM
err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK) {
State.setInstaxStatus(InstaxState.ST_BUSY,err);
return State;
}
// ŽóMƒf[ƒ^Žæ“¾
State = Frame.checkResponseFrame(28);
//if( State.ErrCode != ErrCode.RET_OK ) {
if( judgeErrLevel(State) >= EMERGENCY_ERR_LEVEL ) {
return State;
}
param.BeforeWhite = Frame.getResponseShortData( 0 );
param.AfterWhite = Frame.getResponseShortData( 2 );
param.StepPerLine = Frame.getResponseShortData( 4 );
param.OrgToPiStep = Frame.getResponseShortData( 6 );
param.SpeedUpStep = Frame.getResponseShortData( 8 );
param.SpeedDnStep = Frame.getResponseShortData( 10 );
param.MainStep = Frame.getResponseShortData( 12 );
param.AfterPrintStep = Frame.getResponseShortData( 14 );
param.ReturnStep = Frame.getResponseShortData( 16 );
param.LowSpeed = Frame.getResponseShortData( 18 );
param.PrintSpeed = Frame.getResponseShortData( 20 );
param.ReturnSpeed = Frame.getResponseShortData( 22 );
param.Excitation = Frame.getResponseByteData( 24 );
param.LedOpen = Frame.getResponseByteData( 25 );
return State;
}
*/
/**
* GetFunctionVersion(0xC4)
* @param FuncMap
* @return
*/
//InstaxStatus GetFunctionVersion(int functionId, Integer version) {
InstaxStatus GetFunctionVersion(Map<Integer, Integer> FuncMap) {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
ErrCode err = ErrCode.RET_OK;
int DataSize = 4;
byte[] Data = new byte[DataSize];
short Sid = 0xC4;
for(Integer key : FuncMap.keySet()) {
// ƒf[ƒ^ì¬
Data[0] = 0x00;
Data[1] = 0x00;
Data[2] = (byte)((key.intValue() >> 8) & 0xFF);
Data[3] = (byte)((key.intValue() >> 0) & 0xFF);
Frame.CreateSndData( UID, Sid, Data, DataSize, Param.Password );
// ‘—ŽóM
err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK) {
State.setInstaxStatus(InstaxState.ST_BUSY,err);
return State;
}
// ŽóMƒf[ƒ^Žæ“¾
State = Frame.checkResponseFrame(4);
//if( State.ErrCode != ErrCode.RET_OK ) {
if( judgeErrLevel(State) >= EMERGENCY_ERR_LEVEL ) {
return State;
}
FuncMap.put(key, Integer.valueOf(Frame.getResponseShortData(2)));
}
return State;
}
/**
* InstaxPowerOff(0xB4)
* @return
*/
InstaxStatus InstaxPowerOff() {
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
NetworkCommunicationParam PowerOffParam = new NetworkCommunicationParam();
ErrCode err = ErrCode.RET_OK;
int DataSize = 4;
byte[] Data = new byte[DataSize];
short Sid = 0xB4;
// PowerOff‚̓ŒƒXƒ|ƒ“ƒX‚ð‘Ò‚½‚È‚¢‚Ì‚Å‘¼ƒRƒ}ƒ“ƒh‚Ƃ̓pƒ‰ƒ[ƒ^[‚ð•ÏX‚·‚é
PowerOffParam.setNetworkParam(Param.IpAddress, Param.Port);
PowerOffParam.setCommunicationParam(Param.SndTimeOut, 0, 0);
PowerOffParam.setPassword(Param.Password);
// ƒf[ƒ^ì¬
Data[0] = 0x00;
Data[1] = 0x00;
Data[2] = 0x00;
Data[3] = 0x00;
Frame.CreateSndData( UID, Sid, Data, DataSize, PowerOffParam.Password );
// ‘—M‚ªŠ®—¹‚µ‚½Žž“_‚Å”²‚¯‚邽‚ß‚É‚±‚Ì‘w‚ŃŠƒgƒ‰ƒCˆ—‚ðs‚¤
for(int i=0; i<=Param.RetryCnt; i++) {
// ‘—ŽóM
err = Sock.SndRcvData( PowerOffParam, Frame );
if( err == ErrCode.RET_OK || err == ErrCode.E_RCV_TIMEOUT ) { //ƒ^ƒCƒ€ƒAƒEƒg‚̓Xƒ‹[
State.setInstaxStatus(InstaxState.ST_IDLE, ErrCode.RET_OK);
break;
}
else {
State.setInstaxStatus(InstaxState.ST_BUSY,err);
}
}
// ŽóMƒf[ƒ^Žæ“¾
//State = Frame.checkResponseFrame(0);
return State;
}
/**
*
* @param stat
* @return
*/
int judgeErrLevel(InstaxStatus stat) {
int ret;
// 0:ƒGƒ‰[‚È‚µ
// 1:ˆ—Œp‘±
// 2:ˆ—’†Ž~
//@’萔‚¶‚á‚È‚¢‚Ì‚Åswitch•¶–³—
if( stat.ErrCode == ErrCode.RET_OK ) {
ret = 0;
}
else if( stat.ErrCode == ErrCode.E_FILM_EMPTY
|| stat.ErrCode == ErrCode.E_BATTERY_EMPTY
|| stat.ErrCode == ErrCode.E_CAM_POINT
|| stat.ErrCode == ErrCode.E_MOTOR
|| stat.ErrCode == ErrCode.E_COVER_OPEN
|| stat.ErrCode == ErrCode.E_PI_SENSOR) {
ret = 1;
}
else {
ret = 2;
}
return ret;
}
/**
*
* @param newPassword
* @param oldPassword
* @return
*/
InstaxStatus InstaxChangePassword(int newPassword, int oldPassword) {
InstaxLog.putLogToSD("NewPass:"+String.valueOf(newPassword));
InstaxLog.putLogToSD("OldPass:"+String.valueOf(oldPassword));
// ‹ŒƒpƒXƒ[ƒh‚ª•sˆê’v
//if( Param.Password != oldPassword && Param.Password != -1 ) {
// State.setInstaxStatus(InstaxState.ST_BUSY,ErrCode.E_UNMATCH_PASS);
// return State;
//}
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
ErrCode err = ErrCode.RET_OK;
int DataSize = 4;
byte[] Data = new byte[DataSize];
short Sid = 0xB6;
// ƒf[ƒ^ì¬
Data[0] = 0x00;
Data[1] = 0x00;
Data[2] = (byte)((newPassword >> 8) & 0xFF);
Data[3] = (byte)((newPassword >> 0) & 0xFF);
Frame.CreateSndData( UID, Sid, Data, DataSize, oldPassword );
// ‘—ŽóM
err = Sock.SndRcvData( Param, Frame );
if( err != ErrCode.RET_OK) {
State.setInstaxStatus(InstaxState.ST_BUSY,err);
return State;
}
// ŽóMƒf[ƒ^ƒ`ƒFƒbƒN
State = Frame.checkResponseFrame(0);
if( judgeErrLevel(State) >= EMERGENCY_ERR_LEVEL ) {
return State;
}
// ƒpƒXƒ[ƒh•ÏX
Param.setPassword(newPassword);
return State;
}
/**
*
* @param filePath
* @return
*/
boolean InstaxSetOutputLog(String filePath) {
return InstaxLog.SetLogPath(filePath);
}
}
package com.Instax.sdk;
class NetworkCommunicationParam {
static final String def_IpAddress = "192.168.0.251";
static final int def_Port = 8080;
static final int def_SndTimeOut = 3000;
static final int def_RcvTimeOut =3000;
static final int def_RetryCnt = 3;
static final int def_MaxFrameSize = 480;
static final int def_DelayTime = 0;
static final int def_IMAGE_Format = 2; // default JPG
static final int def_IMAGE_Option = 0;
static final int def_Password = 1111;
String IpAddress;
int Port;
int SndTimeOut;
int RcvTimeOut;
int RetryCnt;
int MaxFrameSize;
int DelayTime;
int IMAGE_Format;
int IMAGE_Option;
int Password;
NetworkCommunicationParam() {
IpAddress = def_IpAddress;
Port = def_Port;
SndTimeOut = def_SndTimeOut;
RcvTimeOut = def_RcvTimeOut;
RetryCnt = def_RetryCnt;
MaxFrameSize = def_MaxFrameSize;
DelayTime = def_DelayTime;
IMAGE_Format = def_IMAGE_Format;
IMAGE_Option = def_IMAGE_Option;
Password = def_Password;
}
void setPassword(int pass) {
if( -1 <= pass && pass <= 9999 ) {
this.Password = pass;
}
else {
this.Password = def_Password;
}
}
/**
* setNetworkParam
* @param ipAddress
* @param port
*/
void setNetworkParam(String ipAddress, int port) {
if( ipAddress != null ) {
this.IpAddress = ipAddress;
}
if( 0 <= port && port <= 65535 ) {
this.Port = port;
}
else {
this.Port = def_Port; // ”͈͊O‚͏‰Šú’l‚ð“ü‚ê‚é
}
}
/**
* setCommunicationParam
* @param sndTimeout
* @param rcvTimeout
* @param retryCnt
*/
void setCommunicationParam(int sndTimeout, int rcvTimeout, int retryCnt) {
if(sndTimeout < 0 || rcvTimeout < 0 || retryCnt < 0) {
this.SndTimeOut = def_SndTimeOut;
this.RcvTimeOut = def_RcvTimeOut;
this.RetryCnt = def_RetryCnt;
}
else {
this.SndTimeOut = sndTimeout;
this.RcvTimeOut = rcvTimeout;
this.RetryCnt = retryCnt;
}
}
/**
*
* @param sndMaxSize
* @param delayTime
*/
void setDebugParam(int maxDataSize, int delayTime, int jpgFlg, int jpgOpt) {
final int SND_BUF_SIZE = 1024*64;
if( maxDataSize > SND_BUF_SIZE-1-4-NetworkFrameData.MIN_REQ_FRAME_SIZE ) {
this.MaxFrameSize = SND_BUF_SIZE-1-4-NetworkFrameData.MIN_REQ_FRAME_SIZE;
}
else if( maxDataSize < 1 ) {
this.MaxFrameSize = def_MaxFrameSize;
}
else {
this.MaxFrameSize = maxDataSize;
}
if( delayTime < 0 ) {
this.DelayTime = def_DelayTime;
}
else {
this.DelayTime = delayTime;
}
if( jpgFlg == 1 || jpgFlg == 2 ) {
this.IMAGE_Format = jpgFlg;
}
else {
this.IMAGE_Format = def_IMAGE_Format;
}
// if( jpgOpt > 0x0B && jpgOpt != 0x10 ) {
// this.IMAGE_Option = 0x00;
// }
// else {
//this.IMAGE_Option = jpgOpt;
// }
}
}
package com.Instax.sdk;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.channels.IllegalBlockingModeException;
import java.util.Arrays;
import android.util.Log;
import com.Instax.sdk.InstaxStatus.ErrCode;
import com.Instax.sdk.InstaxStatus.InstaxState;
class NetworkCommunicationSndImageThread extends Thread {
private NetworkCommunicationParam param;
private byte[] AllSndData; // ‘—M‚·‚é‘Sƒf[ƒ^
private InstaxCallBack sndCallback; // ‘—MŠ®—¹ƒR[ƒ‹ƒoƒbƒN
private InstaxCallBack cancelCallback; // ‘—M’†Ž~ƒR[ƒ‹ƒoƒbƒN
private int SndMaxSize; // ‚P‰ñ‚É‘—‚éÅ‘åƒTƒCƒY
private short Sid;
private int UID;
volatile private int Prog; // i’»
volatile private boolean cancelFlg; // ƒLƒƒƒ“ƒZƒ‹ƒtƒ‰ƒO
private int DelayTime;
/**
*
* @param param
* @param sid
* @param sndData
* @param length
* @param sndMaxSize
* @param sndFinish
*/
NetworkCommunicationSndImageThread(NetworkCommunicationParam param, int uid, short sid, byte[] sndData, int sndMaxSize, InstaxCallBack sndFinish) {
this.param = param;
this.AllSndData= sndData;
this.sndCallback = sndFinish;
this.SndMaxSize = sndMaxSize;
this.Sid = sid;
this.Prog = 0;
this.cancelFlg = false;
this.cancelCallback = null;
this.UID = uid;
this.DelayTime = param.DelayTime; // ms
}
/**
* getProgress
* @return
*/
int getProgress() {
return Prog;
}
/**
*
* @param cancelFinish
*/
void SndImageCancel(InstaxCallBack cancelFinish) {
cancelCallback = cancelFinish;
cancelFlg = true;
}
/**
*
* @return
*/
/*
private InstaxStatus ModeReset() {
InstaxStatus state = new InstaxStatus();
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
int DataSize = 1;
byte[] Data = new byte[DataSize];
short Sid = 0xB3;
ErrCode err = ErrCode.RET_OK;
// ‘—ŽóM
Data[0] = 0;
Frame.CreateSndData( UID, Sid, Data, DataSize );
err = Sock.SndRcvData( param, Frame );
if( err != ErrCode.RET_OK ) {
state.setInstaxStatus(InstaxState.ST_BUSY, err);
return state;
}
// ŽóMƒf[ƒ^ƒ`ƒFƒbƒN
state = Frame.checkResponseFrame(0);
return state;
}
*/
// 0x51
private InstaxStatus TransferStart() {
InstaxStatus state = new InstaxStatus();
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
int DataSize = 12;
byte[] Data = new byte[DataSize];
int imgSize = AllSndData.length;
short Sid = 0x51;
ErrCode err = ErrCode.RET_OK;
Data[0] = (byte)param.IMAGE_Format; // ‰æ‘œƒtƒH[ƒ}ƒbƒg
Data[1] = (byte)param.IMAGE_Option; // pic opt
Data[2] = (byte)(((int)imgSize >> 24) & 0xFF); // size
Data[3] = (byte)(((int)imgSize >> 16) & 0xFF);
Data[4] = (byte)(((int)imgSize >> 8) & 0xFF);;
Data[5] = (byte)(((int)imgSize >> 0) & 0xFF);;
Data[6] = 0x00; // jpg param
Data[7] = 0x00;
Data[8] = 0x00;
Data[9] = 0x00;
Data[10] = 0x00; // reserve
Data[11] = 0x00; // reserve
// ‘—ŽóM
Frame.CreateSndData( UID, Sid, Data, DataSize, param.Password );
err = Sock.SndRcvData( param, Frame );
if( err != ErrCode.RET_OK ) {
state.setInstaxStatus(InstaxState.ST_BUSY, err);
return state;
}
state = Frame.checkResponseFrame( 0 );
return state;
}
// 0x53
private InstaxStatus TransferEnd() {
InstaxStatus state = new InstaxStatus();
NetworkCommunication Sock = new NetworkCommunication();
NetworkFrameData Frame = new NetworkFrameData();
int DataSize = 0;
byte[] Data = new byte[DataSize];
short Sid = 0x53;
ErrCode err = ErrCode.RET_OK;
// ‘—ŽóM
Frame.CreateSndData( UID, Sid, Data, DataSize, param.Password );
err = Sock.SndRcvData( param, Frame );
if( err != ErrCode.RET_OK ) {
state.setInstaxStatus(InstaxState.ST_BUSY, err);
return state;
}
//state = Frame.checkResponseFrame( 4 );
state = Frame.checkResponseFrame( 0 );
//if( state.ErrCode != ErrCode.RET_OK ) {
// return state;
//}
// ‰æ‘œƒf[ƒ^ƒ`ƒFƒbƒNƒTƒ€‚È‚ñ‚Ä‚à‚Ì‚Í‚È‚©‚Á‚½
//int picSum = 0;
//for(int i=0; i<AllSndData.length; i++) {
// picSum += ((int)AllSndData[i] & 0xFF);
//}
//picSum = picSum & 0xFFFFFFFF;
//if( Frame.getResponselongData(0) != picSum ) {
// // ‰æ‘œƒf[ƒ^ƒ`ƒFƒbƒNƒTƒ€•sˆê’v
// state.setInstaxStatus(InstaxState.ST_BUSY, ErrCode.E_RCV_FRAME);
// Log.e("thread", "picSum Error:"+String.valueOf(Frame.getResponseShortData(0)));
// Log.e("thread", "picSum Error:"+String.valueOf(picSum));
//}
return state;
}
@Override
public void run() {
Log.d("thread", "thread start");
InstaxStatus state = new InstaxStatus();
NetworkFrameData Frame = new NetworkFrameData();
Socket socket;
OutputStream os;
InputStream is;
int i, loop, lastSize, seqCnt;
byte[] data = new byte[SndMaxSize+4]; // ƒV[ƒPƒ“ƒXƒf[ƒ^•ª+4
boolean finishFlg = true;
ErrCode err = ErrCode.RET_OK;
boolean RcvFlg = false;
loop = (int)(AllSndData.length/SndMaxSize);
lastSize = (int)(AllSndData.length%SndMaxSize);
Log.d("thread","AllSndDataLength:"+String.valueOf(AllSndData.length));
Log.d("thread","SndMaxSize:"+String.valueOf(SndMaxSize));
Log.d("thread","loop:"+String.valueOf(loop));
Log.d("thread","lastSize:"+String.valueOf(lastSize));
Log.d("thread", "SendTimeout:"+String.valueOf(param.SndTimeOut));
Log.d("thread", "RecvTimeout:"+String.valueOf(param.RcvTimeOut));
Log.d("thread", "Retry:"+String.valueOf(param.RetryCnt));
seqCnt = 0;
Prog = 0;
// ‘—MƒLƒƒƒ“ƒZƒ‹
if( cancelFlg == true && cancelCallback != null )
{
Log.d("thread", "SndImageCanncel");
state.setInstaxStatus(InstaxState.ST_IDLE, ErrCode.RET_OK);
cancelCallback.FinishCallBack(state);
//ModeReset();
return;
}
// ˜A‘±‘—M
// Nullƒ`ƒFƒbƒN
if( param == null || Frame == null ) {
Log.e("thread", "NullObject");
state.setInstaxStatus(InstaxState.ST_BUSY, ErrCode.E_CONNECT);
sndCallback.FinishCallBack(state);
//ModeReset();
return;
}
// 0x51 ‘—M
state = TransferStart();
if( state.InstaxState != InstaxState.ST_IDLE || state.ErrCode != ErrCode.RET_OK ) {
sndCallback.FinishCallBack(state);
//ModeReset();
return;
}
try {
// ƒ\ƒPƒbƒgì¬
socket = new Socket();
// Ú‘±
socket.connect(new InetSocketAddress(param.IpAddress,param.Port),param.SndTimeOut);
os = socket.getOutputStream();
is = socket.getInputStream();
Log.d("thread", "Connect Socket");
// 0x52‘—M
for( i=0; i<loop; i++) {
if( cancelFlg == true ) {
Log.d("thread", "SndImageCanncel");
break;
}
data[0] = (byte)((seqCnt >> 24) & 0xFF);
data[1] = (byte)((seqCnt >> 16) & 0xFF);
data[2] = (byte)((seqCnt >> 8) & 0xFF);
data[3] = (byte)((seqCnt >> 0) & 0xFF);
System.arraycopy(AllSndData, i*SndMaxSize, data, 4, SndMaxSize);
Frame.CreateSndData(UID, Sid, data, SndMaxSize+4, param.Password);
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// ŠÖ”‰»‚·‚é‚Æsocket’ʐM‚ª‚¤‚Ü‚­‚¢‚©‚È‚­‚È‚é‚̂Ńxƒ^‘‚«‚·‚é
for( int retry=0; retry<=param.RetryCnt; retry++) {
if( cancelFlg == true ) {
Log.d("thread", "SndImageCanncel");
break;
}
// ƒf[ƒ^‘—M
if( !socket.isConnected() ) {
Log.d("thread", "socket disconnect");
// ƒ\ƒPƒbƒgì¬
socket = new Socket();
// Ú‘±
socket.connect(new InetSocketAddress(param.IpAddress,param.Port),param.SndTimeOut);
os = socket.getOutputStream();
is = socket.getInputStream();
}
os.write(Frame.getSndData(), 0, Frame.getSndSize());
os.flush();
// “ǂݍž‚Þ‚Ü‚Å‘Ò‹@
long s = System.currentTimeMillis();
while(true) {
if( is.available() != 0 ) {
// ŽóM
RcvFlg = true;
break;
}
if( (System.currentTimeMillis() - s) >= param.RcvTimeOut ) {
// ŽóMƒ^ƒCƒ€ƒAƒEƒg
RcvFlg = false;
os.close();
is.close();
socket.close();
socket = new Socket();
// ÄÚ‘±
socket.connect(new InetSocketAddress(param.IpAddress,param.Port),param.SndTimeOut);
os = socket.getOutputStream();
is = socket.getInputStream();
break;
}
}
if( RcvFlg == true ) {
// “ǂݍž‚ñ‚¾‚à‚Ì‚ð‘‚«‚¾‚·
Frame.setRcvSize(is.available());
Frame.setRcvData(new byte[Frame.getRcvSize()]);
is.read(Frame.getRcvData(), 0, Frame.getRcvSize());
}
if( RcvFlg == false ) {
Log.e("SndRcvData", "RcvTimeOut");
err = ErrCode.E_RCV_TIMEOUT;
continue;
}
err = ErrCode.RET_OK;
break;
}// retry
///////////////////////////////////////////////////////////////////////////////////////////////////////////
if( err != ErrCode.RET_OK ) {
// ‘—ŽóMƒGƒ‰[
finishFlg = false;
state.setInstaxStatus(InstaxState.ST_BUSY, err);
Log.e("thread", "SndRcvData Error:"+err.toString());
break;
}
state = Frame.checkResponseFrame(4);
if( state.ErrCode != ErrCode.RET_OK ) {
// ƒŒƒXƒ|ƒ“ƒXƒGƒ‰[
finishFlg = false;
state.setInstaxStatus(InstaxState.ST_BUSY, state.ErrCode);
Log.e("thread", "Response Error:"+state.ErrCode.toString());
break;
}
if( Frame.getResponselongData(0) != seqCnt ) {
// ƒV[ƒPƒ“ƒX•sˆê’v
finishFlg = false;
state.setInstaxStatus(InstaxState.ST_BUSY, ErrCode.E_RCV_FRAME);
Log.e("thread", "Sequence Error:"+String.valueOf(seqCnt));
break;
}
//Log.d("thread", "Sequence:"+String.valueOf(seqCnt));
seqCnt++;
if( lastSize > 0 ) {
Prog = (i*100)/(loop+1); // i’»—¦ŒvŽZ
}
else {
Prog = (i*100)/(loop);
}
if( DelayTime > 0 ) {
Thread.sleep(DelayTime);
}
}// loop
// —]‚è‘—M
if( finishFlg == true && lastSize > 0 && cancelFlg == false ) {
Arrays.fill(data, (byte)0x00);
data[0] = (byte)((seqCnt >> 24) & 0xFF);
data[1] = (byte)((seqCnt >> 16) & 0xFF);
data[2] = (byte)((seqCnt >> 8) & 0xFF);
data[3] = (byte)((seqCnt >> 0) & 0xFF);
System.arraycopy(AllSndData, i*SndMaxSize, data, 4, lastSize);
//Frame.CreateSndData(UID, Sid, data, lastSize+4);
// ’[”‚ª‚ ‚Á‚½ê‡‚Í’[”•ª‚¾‚¯‘—‚Á‚Ä‚¢‚½‚ª0–„‚߂ŃtƒŒ[ƒ€ƒTƒCƒY‚ð‡‚í‚¹‚邱‚Æ‚É‚È‚Á‚½
Frame.CreateSndData(UID, Sid, data, SndMaxSize+4, param.Password);
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// ŠÖ”‰»‚·‚é‚Æsocket’ʐM‚ª‚¤‚Ü‚­‚¢‚©‚È‚­‚È‚é‚̂Ńxƒ^‘‚«‚·‚é
for( int retry=0; retry<=param.RetryCnt; retry++) {
if( cancelFlg == true ) {
Log.d("thread", "SndImageCanncel");
break;
}
// ƒf[ƒ^‘—M
if( !socket.isConnected() ) {
// ƒ\ƒPƒbƒgì¬
socket = new Socket();
// Ú‘±
socket.connect(new InetSocketAddress(param.IpAddress,param.Port),param.SndTimeOut);
os = socket.getOutputStream();
is = socket.getInputStream();
}
os.write(Frame.getSndData(), 0, Frame.getSndSize());
os.flush();
// “ǂݍž‚Þ‚Ü‚Å‘Ò‹@
long s = System.currentTimeMillis();
while(true) {
if( is.available() != 0 ) {
// ŽóM
RcvFlg = true;
break;
}
if( (System.currentTimeMillis() - s) >= param.RcvTimeOut ) {
// ŽóMƒ^ƒCƒ€ƒAƒEƒg
RcvFlg = false;
os.close();
is.close();
socket.close();
socket = new Socket();
// ÄÚ‘±
socket.connect(new InetSocketAddress(param.IpAddress,param.Port),param.SndTimeOut);
os = socket.getOutputStream();
is = socket.getInputStream();
break;
}
}
if( RcvFlg == true ) {
// “ǂݍž‚ñ‚¾‚à‚Ì‚ð‘‚«‚¾‚·
Frame.setRcvSize(is.available());
Frame.setRcvData(new byte[Frame.getRcvSize()]);
is.read(Frame.getRcvData(), 0, Frame.getRcvSize());
}
if( RcvFlg == false ) {
Log.e("SndRcvData", "RcvTimeOut");
err = ErrCode.E_RCV_TIMEOUT;
continue;
}
err = ErrCode.RET_OK;
break;
}// retry
///////////////////////////////////////////////////////////////////////////////////////////////////////////
if( err != ErrCode.RET_OK ) {
// ‘—ŽóMƒGƒ‰[
finishFlg = false;
state.setInstaxStatus(InstaxState.ST_BUSY, err);
Log.e("thread", "SndRcvData Error:"+err.toString());
}
state = Frame.checkResponseFrame(4);
if( state.ErrCode != ErrCode.RET_OK ) {
// ƒŒƒXƒ|ƒ“ƒXƒGƒ‰[
finishFlg = false;
state.setInstaxStatus(InstaxState.ST_BUSY, state.ErrCode);
Log.e("thread", "Response Error:"+state.ErrCode.toString());
}
if( Frame.getResponselongData(0) != seqCnt ) {
// ƒV[ƒPƒ“ƒX•sˆê’v
finishFlg = false;
state.setInstaxStatus(InstaxState.ST_BUSY, ErrCode.E_RCV_FRAME);
Log.e("thread", "Sequence Error:"+String.valueOf(seqCnt));
}
if( DelayTime > 0 ) {
Thread.sleep(DelayTime);
}
}// 0x52‘—M
// Ø’f
os.close();
is.close();
socket.close();
// 0x53 ‘—M
if( finishFlg == true && cancelFlg == false ) {
state = TransferEnd();
if( state.InstaxState != InstaxState.ST_IDLE || state.ErrCode != ErrCode.RET_OK ) {
finishFlg = false;
}
}
} catch (SocketTimeoutException e) { // ƒ\ƒPƒbƒgì¬Ž¸”s
InstaxLog.putLogToSD("SocketTimeoutException");
Log.e("thread", "SocketTimeOut");
state.setInstaxStatus(InstaxState.ST_BUSY, ErrCode.E_MAKE_SOCKET);
finishFlg = false;
} catch (IllegalBlockingModeException e) { // ƒCƒŠ[ƒKƒ‹ƒGƒ‰[
InstaxLog.putLogToSD("IllegalBlockingModeException");
Log.e("thread","IllegalBlockingMode");
state.setInstaxStatus(InstaxState.ST_BUSY, ErrCode.E_CONNECT);
finishFlg = false;
} catch (IllegalArgumentException e) {
InstaxLog.putLogToSD("IllegalArgumentException");
Log.e("thread", "IllegalArgument"); // ˆø”ˆÙí
state.setInstaxStatus(InstaxState.ST_BUSY, ErrCode.E_CONNECT);
finishFlg = false;
} catch (UnknownHostException e) {
InstaxLog.putLogToSD("UnknownHostException");
Log.e("thread", "UnknownHost"); // ƒzƒXƒg‚ªŒ©‚‚©‚ç‚È‚¢
state.setInstaxStatus(InstaxState.ST_BUSY, ErrCode.E_CONNECT);
finishFlg = false;
} catch (InterruptedException e) {
InstaxLog.putLogToSD("InterruptedException");
Log.e("thread", "InterruptedException"); // ƒzƒXƒg‚ªŒ©‚‚©‚ç‚È‚¢
state.setInstaxStatus(InstaxState.ST_BUSY, ErrCode.E_CONNECT);
finishFlg = false;
} catch (IOException e) { // ‚»‚Ì‘¼
InstaxLog.putLogToSD("IOException " + e.getMessage());
Log.e("thread", "IOException " + e.getMessage());
state.setInstaxStatus(InstaxState.ST_BUSY, ErrCode.E_CONNECT);
finishFlg = false;
}
// ƒXƒe[ƒ^ƒXƒZƒbƒg
if( cancelFlg == true && cancelCallback != null ) {
Prog = 0;
state.setInstaxStatus(InstaxState.ST_IDLE, ErrCode.RET_OK);
cancelCallback.FinishCallBack(state);
Log.d("thread", "SndImageCanncel");
//ModeReset();
return;
}
if( finishFlg == true ) {
Prog = 100;
state.setInstaxStatus(InstaxState.ST_IDLE, ErrCode.RET_OK);
}
else {
Prog = 0;
//ModeReset();
}
// ƒR[ƒ‹ƒoƒbƒN
sndCallback.FinishCallBack( state );
Log.d("thread", "thread end");
}
}
@AcmonCercopes

This comment has been minimized.

Copy link

@AcmonCercopes AcmonCercopes commented Feb 28, 2016

Hello,

I try to create a photobooth using rasberry Pi 2 and the Share SP-1, so i tried your code in order to understand how the protocol works, but sadly several classes are missing.

Do i need to decompile the official instax Application to get this classes?

Thx, in advance ;)

@henryww

This comment has been minimized.

Copy link

@henryww henryww commented May 10, 2016

Hello Contra
I am also working on a similar app for the Instax and it would be great if you can share the additional classes
com.Instax.sdk.InstaxStatus.ErrCode
com.Instax.sdk.InstaxStatus.InstaxState

That will certainly help me a lot ... Thank you soooooooooo much advance

Henry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment