Created
September 18, 2017 00:49
-
-
Save edwardinubuntu/69b1f23cbee8c69f526050832ff7cd37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tw.soleil.tscdemo; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import com.example.tscdll.TscWifiActivity; | |
import java.io.UnsupportedEncodingException; | |
import java.util.LinkedList; | |
public class TSCExampleActivity extends AppCompatActivity { | |
public static final String TAG = "TSCApp"; | |
private TscWifiActivity tscWifiActivity = new TscWifiActivity(); | |
protected EditText ipAddressEditText; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_tsc_example); | |
} | |
@Override | |
protected void onPostCreate(@Nullable Bundle savedInstanceState) { | |
super.onPostCreate(savedInstanceState); | |
final TextView statusTextView = (TextView) findViewById(R.id.status_text_view); | |
final TextView commandTextView = (TextView) findViewById(R.id.command_text_view); | |
final EditText adjustPrintYEditText = (EditText) findViewById(R.id.adjust_print_y_edit_text); | |
ipAddressEditText = (EditText) findViewById(R.id.ip_address_edit_text); | |
final TSCSetup tscSetup = new TSCSetup(ipAddressEditText.getText().toString(), 9100); | |
adjustPrintYEditText.setText(String.valueOf(tscSetup.getLeftPrintY())); | |
final EditText numberOfLabelEditText = (EditText) findViewById(R.id.number_of_label_edit_text); | |
findViewById(R.id.multi_print_button).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (ipAddressEditText.getText() != null && ipAddressEditText.getText().toString().length() > 0) { | |
tscSetup.setIp(ipAddressEditText.getText().toString()); | |
} | |
if (adjustPrintYEditText.getText() != null && adjustPrintYEditText.getText().toString().length() > 0) { | |
tscSetup.setLeftPrintY(Integer.parseInt(adjustPrintYEditText.getText().toString())); | |
} | |
if (numberOfLabelEditText.getText() != null && numberOfLabelEditText.getText().length() > 0) { | |
StringBuilder printCommandBuffer = new StringBuilder(); | |
Log.d(TAG, "TSC setup: " + tscSetup.toString()); | |
int numberToPrint = Integer.parseInt(numberOfLabelEditText.getText().toString()); | |
for (int index = 0; index < numberToPrint; index++) { | |
final LinkedList<TSCSetup.TextCommand> textCommandLinkedList = new LinkedList<>(); | |
textCommandLinkedList.add(new TSCSetup.TextCommand(TSCSetup.TextAlignment.RIGHT, "0001("+ (index + 1) +"/"+ (numberToPrint) +")")); | |
textCommandLinkedList.add(new TSCSetup.TextCommand(TSCSetup.TextAlignment.LEFT, "檸檬汁(中)")); | |
textCommandLinkedList.add(new TSCSetup.TextCommand(TSCSetup.TextAlignment.LEFT, "")); | |
textCommandLinkedList.add(new TSCSetup.TextCommand(TSCSetup.TextAlignment.LEFT, "售價$45 (外帶)")); | |
textCommandLinkedList.add(new TSCSetup.TextCommand(TSCSetup.TextAlignment.LEFT, "一小時內飲用最佳")); | |
textCommandLinkedList.add(new TSCSetup.TextCommand(TSCSetup.TextAlignment.LEFT, "日傑茶坊 TEL:0000-0000")); | |
printCommandBuffer.append(tscSetup.printTextCommand(textCommandLinkedList)); | |
sendCommandToTSC(tscSetup, textCommandLinkedList, statusTextView); | |
} | |
commandTextView.setText(printCommandBuffer.toString()); | |
Log.d(TAG, "Command: \n" + printCommandBuffer.toString()); | |
} | |
} | |
}); | |
findViewById(R.id.action_button).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (adjustPrintYEditText.getText() != null && adjustPrintYEditText.getText().toString().length() > 0) { | |
tscSetup.setLeftPrintY(Integer.parseInt(adjustPrintYEditText.getText().toString())); | |
} | |
final LinkedList<TSCSetup.TextCommand> textCommandLinkedList = new LinkedList<>(); | |
textCommandLinkedList.add(new TSCSetup.TextCommand(TSCSetup.TextAlignment.RIGHT, "0001(10/10)")); | |
textCommandLinkedList.add(new TSCSetup.TextCommand(TSCSetup.TextAlignment.LEFT, "檸檬汁(中)")); | |
textCommandLinkedList.add(new TSCSetup.TextCommand(TSCSetup.TextAlignment.LEFT, "")); | |
textCommandLinkedList.add(new TSCSetup.TextCommand(TSCSetup.TextAlignment.LEFT, "售價$45 (外帶)")); | |
textCommandLinkedList.add(new TSCSetup.TextCommand(TSCSetup.TextAlignment.LEFT, "一小時內飲用最佳")); | |
textCommandLinkedList.add(new TSCSetup.TextCommand(TSCSetup.TextAlignment.LEFT, "日傑茶坊 TEL:0000-0000")); | |
sendCommandToTSC(tscSetup, textCommandLinkedList, statusTextView); | |
String printTextCommand = tscSetup.printTextCommand(textCommandLinkedList); | |
commandTextView.setText(printTextCommand.toString()); | |
Log.d(TAG, "Command: \n" + printTextCommand.toString()); | |
Log.d(TAG, "TSC setup: " + tscSetup.toString()); | |
} | |
}); | |
} | |
@NonNull | |
private void sendCommandToTSC(TSCSetup tscSetup, LinkedList<TSCSetup.TextCommand> textCommandLinkedList, TextView statusTextView) { | |
// Open port | |
tscWifiActivity.openport(tscSetup.getIp(),tscSetup.getPort()); | |
// Setup | |
tscWifiActivity.setup(tscSetup.getLabelWidthInMM(), tscSetup.getLabelHeightInMM(), 1, 4, 0, 0, 0); | |
tscWifiActivity.clearbuffer(); | |
tscWifiActivity.sendcommand("SET TEAR ON\n"); | |
String printTextCommand = tscSetup.printTextCommand(textCommandLinkedList); | |
try { | |
tscWifiActivity.sendcommand(printTextCommand.getBytes("big5")); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
Log.e(TAG, "Error on: " + e.getLocalizedMessage()); | |
} | |
statusTextView.setText(tscWifiActivity.status()); | |
tscWifiActivity.printlabel(1, 1); | |
// Close port | |
tscWifiActivity.closeport(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tw.soleil.tscdemo; | |
import java.util.LinkedList; | |
/** | |
* Created by edwardchiang on 28/06/2017. | |
*/ | |
public class TSCSetup { | |
private String ip = "10.0.1.42"; | |
private int port = 9100; | |
private int labelWidthInMM = 35; | |
private int labelHeightInMM = 25; | |
private int leftPrintX = 25; | |
private int leftPrintY = 25; | |
private int rightPrintX = 290; | |
private int textHeight = 28; | |
public TSCSetup(String ip, int port) { | |
this.ip = ip; | |
this.port = port; | |
} | |
public static class TextCommand { | |
private TextAlignment textAlignment; | |
private String text; | |
public TextCommand(TextAlignment textAlignment, String text) { | |
this.textAlignment = textAlignment; | |
this.text = text; | |
} | |
public TextAlignment getTextAlignment() { | |
return textAlignment; | |
} | |
public void setTextAlignment(TextAlignment textAlignment) { | |
this.textAlignment = textAlignment; | |
} | |
public String getText() { | |
return text; | |
} | |
public void setText(String text) { | |
this.text = text; | |
} | |
} | |
public enum TextAlignment { | |
LEFT, RIGHT | |
} | |
public String printTextCommand(LinkedList<TextCommand> textCommandLinkedList) { | |
StringBuilder command = new StringBuilder(); | |
int printTextY = getLeftPrintY(); | |
for (TextCommand textCommand : textCommandLinkedList) { | |
TextAlignment textAlignment = textCommand.getTextAlignment(); | |
switch (textAlignment) { | |
case LEFT: | |
command.append("TEXT ") | |
.append(getLeftPrintX()) | |
.append(",") | |
.append(printTextY) | |
.append(",\"TST24.BF2\",0,1,1,\"") | |
.append(textCommand.getText()) | |
.append("\"\n"); | |
break; | |
case RIGHT: | |
command.append("TEXT ") | |
.append(getRightPrintX()) | |
.append(",") | |
.append(printTextY) | |
.append(",\"TST24.BF2\",0,1,1,3,\"") | |
.append(textCommand.getText()) | |
.append("\"\n"); | |
break; | |
} | |
printTextY += getTextHeight(); | |
} | |
return command.toString(); | |
} | |
public int getLabelWidthInMM() { | |
return labelWidthInMM; | |
} | |
public int getLabelHeightInMM() { | |
return labelHeightInMM; | |
} | |
public int getLeftPrintX() { | |
return leftPrintX; | |
} | |
public int getLeftPrintY() { | |
return leftPrintY; | |
} | |
public int getRightPrintX() { | |
return rightPrintX; | |
} | |
public int getTextHeight() { | |
return textHeight; | |
} | |
public void setLeftPrintY(int leftPrintY) { | |
this.leftPrintY = leftPrintY; | |
} | |
public String getIp() { | |
return ip; | |
} | |
public void setIp(String ip) { | |
this.ip = ip; | |
} | |
public int getPort() { | |
return port; | |
} | |
public void setPort(int port) { | |
this.port = port; | |
} | |
@Override | |
public String toString() { | |
return "TSCSetup{" + | |
"ip='" + ip + '\'' + | |
", port=" + port + | |
", labelWidthInMM=" + labelWidthInMM + | |
", labelHeightInMM=" + labelHeightInMM + | |
", leftPrintX=" + leftPrintX + | |
", leftPrintY=" + leftPrintY + | |
", rightPrintX=" + rightPrintX + | |
", textHeight=" + textHeight + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.append(","TST24.BF2",0,1,1,"") > 字體大小 .append(","TST24.BF2",0,1,2,"") 檸檬汁(中) 用 2