package com.example.displaydemo;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.displaydemo.base.BaseActivity;
import com.example.displaydemo.utils.ToastUtils;
import com.pavolibrary.commands.Constant;
import com.pavolibrary.commands.Dsp280API;
import com.pavolibrary.utils.FileChooseUtils;
import com.pavolibrary.utils.LogUtils;
import com.serialport.api.SerialPortFinder;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;


public class PD280Activity extends BaseActivity implements View.OnClickListener, Dsp280API.UploadListener {
    private static final String TAG = "PD280Activity";

    private static final int REQUEST_CHOOSEFILE = 1;

    private Button btn_display;
    private Button btn_display_upperlinescroll;
    private Button btn_display_at;
    private Button btn_setlines;
    private Button btn_setbrightness;
    private Button btn_setcommand;
    private Button btn_setcharset;
    private Button btn_setcodepage;
    private Button btn_clearline;
    private Button btn_clearscreen;
    private Button btn_cursorleft;
    private Button btn_cursorright;
    private Button btn_cursordown;
    private Button btn_cursorup;
    private Button btn_cursorendleft;
    private Button btn_cursorendright;
    private Button btn_cursorhome;
    private Button btn_cursorend;
    private Button btn_movecursor;


    private Button btn_restart;
    private Button btn_init;

    private Button btn_price;
    private Button btn_total;
    private Button btn_collect;
    private Button btn_change;

    private Button btn_showqrcode;
    private Button btn_showtext;
    private Button btn_showpicture;

    private Button btn_uploadpicture;
    private Button btn_selectpicture;

    private Button btn_connect;
    private Button btn_back;

    private EditText edt_amount;
    private EditText edt_text;
    private EditText edt_qrcode_size;
    private EditText edt_qrcode_x;
    private EditText edt_qrcode_y;
    private EditText edt_font_size;
    private EditText edt_font_x;
    private EditText edt_font_y;
    private EditText edt_picture_x;
    private EditText edt_picture_y;

    private Spinner sp_port;
    private Spinner sp_baudrate;
    private Spinner sp_lines;
    private Spinner sp_row;
    private Spinner sp_column;

    private Spinner sp_row_display;
    private Spinner sp_column_display;
    private Spinner sp_brightness;
    private Spinner sp_commands;
    private Spinner sp_charset;
    private Spinner sp_codepage;
    private Spinner sp_picturename;

    private TextView tv_picturefilename;
    private TextView tv_model;
    private TextView tv_firmwareVersion;

    private SerialPortFinder mSerialPortFinder;

    private Dsp280API mDsp;

    private String serialPort = "USB";  //定义串口号
    private int serialBaudrate = 9600;  //定义波特率
    private int serialFlag = 0;

    private Runnable runnable;
    private ProgressDialog progressDialog;

    @Override
    public int getContentViewId() {
        return R.layout.activity_pd280;
    }

    @Override
    public void initView() {
        btn_display = findViewById(R.id.btn_display);
        btn_display_upperlinescroll = findViewById(R.id.btn_display_upperlinescroll);
        btn_display_at = findViewById(R.id.btn_display_at);
        btn_clearline = findViewById(R.id.btn_clearline);
        btn_clearscreen = findViewById(R.id.btn_clearscreen);
        btn_cursorleft = findViewById(R.id.btn_cursorleft);
        btn_cursorright = findViewById(R.id.btn_cursorright);
        btn_cursordown = findViewById(R.id.btn_cursordown);
        btn_cursorup = findViewById(R.id.btn_cursorup);
        btn_cursorendleft = findViewById(R.id.btn_cursorendleft);
        btn_cursorendright = findViewById(R.id.btn_cursorendright);
        btn_cursorhome = findViewById(R.id.btn_cursorhome);
        btn_cursorend = findViewById(R.id.btn_cursorend);
        btn_movecursor = findViewById(R.id.btn_movecursor);
        btn_setlines = findViewById(R.id.btn_setlines);
        btn_setbrightness = findViewById(R.id.btn_SetBrightness);
        btn_setcommand = findViewById(R.id.btn_SetCommand);
        btn_setcharset = findViewById(R.id.btn_setcharset);
        btn_setcodepage = findViewById(R.id.btn_setcodepage);


        btn_restart = findViewById(R.id.btn_restart);
        btn_init = findViewById(R.id.btn_init);

        btn_price = findViewById(R.id.btn_price);
        btn_total = findViewById(R.id.btn_total);
        btn_collect = findViewById(R.id.btn_collect);
        btn_change = findViewById(R.id.btn_change);
        btn_showqrcode = findViewById(R.id.btn_showqrcode);
        btn_showtext = findViewById(R.id.btn_showtext);
        btn_showpicture = findViewById(R.id.btn_showpicture);

        btn_uploadpicture = findViewById(R.id.btn_uploadpicture);
        btn_selectpicture = findViewById(R.id.btn_selectpicture);

        btn_connect = findViewById(R.id.btn_connect);
        btn_back = findViewById(R.id.btn_back);

        edt_amount = findViewById(R.id.edt_amount);
        edt_text = findViewById(R.id.edt_text);
        edt_qrcode_size = findViewById(R.id.edt_qrcode_size);
        edt_qrcode_x = findViewById(R.id.edt_qrcode_x);
        edt_qrcode_y = findViewById(R.id.edt_qrcode_y);
        edt_font_size = findViewById(R.id.edt_font_size);
        edt_font_x = findViewById(R.id.edt_font_x);
        edt_font_y = findViewById(R.id.edt_font_y);
        edt_picture_x = findViewById(R.id.edt_picture_x);
        edt_picture_y = findViewById(R.id.edt_picture_y);

        sp_lines = findViewById(R.id.spinner_lines);
        sp_row = findViewById(R.id.spinner_row);
        sp_column = findViewById(R.id.spinner_column);
        sp_row_display = findViewById(R.id.spinner_row_display);
        sp_column_display = findViewById(R.id.spinner_column_display);
        sp_picturename = findViewById(R.id.spinner_picturename);
        sp_brightness = findViewById(R.id.spinner_Brightness);
        sp_commands = findViewById(R.id.spinner_Commands);
        sp_charset = findViewById(R.id.spinner_charset);
        sp_codepage = findViewById(R.id.spinner_codepage);

        tv_picturefilename = findViewById(R.id.textPictureFileName);
        tv_model = findViewById(R.id.textModel);
        tv_firmwareVersion = findViewById(R.id.textFirmwareVersion);

        sp_port = findViewById(R.id.spinner_port);
        sp_baudrate = findViewById(R.id.spinner_baudrate);

        //serial port list
        mSerialPortFinder = new SerialPortFinder();
        String[] entryValues = mSerialPortFinder.getAllDevicesPath();
        List<String> allDevices = new ArrayList<String>();
        allDevices.add("USB");
        for (int i = 0; i < entryValues.length; i++) {
            allDevices.add(entryValues[i]);
        }
        ArrayAdapter<String> aspnDevices = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, allDevices);
        aspnDevices.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_port.setAdapter(aspnDevices);

        //charset list
        List<String> allCharset_names = new ArrayList<String>();
        for (int i = 0; i < Constant.international_name.length; i++) {
            allCharset_names.add(Constant.international_name[i]);
        }
        ArrayAdapter<String> aspnCharset_names = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, allCharset_names);
        aspnCharset_names.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_charset.setAdapter(aspnCharset_names);

        //code page list
        List<String> allCodepage_names = new ArrayList<String>();
        for (int i = 0; i < Constant.codepage_name.length; i++) {
            allCodepage_names.add(Constant.codepage_name[i]);
        }
        ArrayAdapter<String> aspnCodepage_names = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, allCodepage_names);
        aspnCodepage_names.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_codepage.setAdapter(aspnCodepage_names);

        //picture list
        List<String> allPicture_names = new ArrayList<String>();
        for (int i = 0; i < Constant.picture_name_pd280.length; i++) {
            allPicture_names.add(Constant.picture_name_pd280[i]);
        }
        ArrayAdapter<String> aspnPicture_names = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, allPicture_names);
        aspnPicture_names.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_picturename.setAdapter(aspnPicture_names);
    }

    @Override
    public void initData() {
        mDsp = new Dsp280API(this);

        btn_display.setEnabled(false);
        btn_display_upperlinescroll.setEnabled(false);
        btn_display_at.setEnabled(false);
        btn_clearline.setEnabled(false);
        btn_clearscreen.setEnabled(false);
        btn_cursorleft.setEnabled(false);
        btn_cursorright.setEnabled(false);
        btn_cursordown.setEnabled(false);
        btn_cursorup.setEnabled(false);
        btn_cursorendleft.setEnabled(false);
        btn_cursorendright.setEnabled(false);
        btn_cursorhome.setEnabled(false);
        btn_cursorend.setEnabled(false);
        btn_movecursor.setEnabled(false);

        btn_restart.setEnabled(false);
        btn_init.setEnabled(false);
        btn_price.setEnabled(false);
        btn_total.setEnabled(false);
        btn_collect.setEnabled(false);
        btn_change.setEnabled(false);

        btn_showqrcode.setEnabled(false);
        btn_showtext.setEnabled(false);
        btn_showpicture.setEnabled(false);

        btn_setlines.setEnabled(false);
        btn_setbrightness.setEnabled(false);
        btn_setcommand.setEnabled(false);
        btn_setcharset.setEnabled(false);
        btn_setcodepage.setEnabled(false);
        btn_uploadpicture.setEnabled(false);
        btn_selectpicture.setEnabled(false);
    }

    @Override
    protected void initListener() {
        btn_display.setOnClickListener(this);
        btn_display_upperlinescroll.setOnClickListener(this);
        btn_display_at.setOnClickListener(this);
        btn_clearline.setOnClickListener(this);
        btn_clearscreen.setOnClickListener(this);
        btn_cursorleft.setOnClickListener(this);
        btn_cursorright.setOnClickListener(this);
        btn_cursordown.setOnClickListener(this);
        btn_cursorup.setOnClickListener(this);
        btn_cursorendleft.setOnClickListener(this);
        btn_cursorendright.setOnClickListener(this);
        btn_cursorhome.setOnClickListener(this);
        btn_cursorend.setOnClickListener(this);
        btn_movecursor.setOnClickListener(this);

        btn_restart.setOnClickListener(this);
        btn_init.setOnClickListener(this);

        btn_price.setOnClickListener(this);
        btn_total.setOnClickListener(this);
        btn_collect.setOnClickListener(this);
        btn_change.setOnClickListener(this);

        btn_showqrcode.setOnClickListener(this);
        btn_showtext.setOnClickListener(this);
        btn_showpicture.setOnClickListener(this);

        btn_setlines.setOnClickListener(this);
        btn_setbrightness.setOnClickListener(this);
        btn_setcommand.setOnClickListener(this);
        btn_setcharset.setOnClickListener(this);
        btn_setcodepage.setOnClickListener(this);
        btn_uploadpicture.setOnClickListener(this);
        btn_selectpicture.setOnClickListener(this);

        btn_connect.setOnClickListener(this);
        btn_back.setOnClickListener(this);

        mDsp.setUploadListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mDsp != null) {
            mDsp.disconnect();
            mDsp = null;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            /*case R.id.btn_display:
                String text1 = edt_text.getText().toString().trim();
                if (mDsp != null && mDsp.isConnect() && !text1.isEmpty()) {
                    mDsp.DSP_Dispay(text1,
                            "cp936");
                }
                break;
            case R.id.btn_display_upperlinescroll:
                String text2 = edt_text.getText().toString().trim();
                if (mDsp != null && mDsp.isConnect() && !text2.isEmpty()) {
                    mDsp.DSP_Dispay_UpperLineScroll(text2,
                            "cp936");
                }
                break;
            case R.id.btn_display_at:
                String text3 = edt_text.getText().toString().trim();
                if (mDsp != null && mDsp.isConnect() && !text3.isEmpty()) {
                    mDsp.DSP_Dispay_At(text3,
                            "cp936",
                            sp_row_display.getSelectedItemPosition() + 1,
                            sp_column_display.getSelectedItemPosition() + 1);
                }
                break;
            case R.id.btn_clearline:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_ClearLine();
                }
                break;
            case R.id.btn_clearscreen:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_ClearScreen();
                }
                break;
            case R.id.btn_cursorleft:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_MoveCursorLeft();
                }
                break;
            case R.id.btn_cursorright:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_MoveCursorRight();
                }
                break;
            case R.id.btn_cursordown:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_MoveCursorDown();
                }
                break;
            case R.id.btn_cursorup:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_MoveCursorUp();
                }
                break;
            case R.id.btn_cursorendleft:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_MoveCursorEndLeft();
                }
                break;
            case R.id.btn_cursorendright:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_MoveCursorEndRight();
                }
                break;
            case R.id.btn_cursorend:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_MoveCursorEnd();
                }
                break;
            case R.id.btn_cursorhome:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_MoveCursorHome();
                }
                break;
            case R.id.btn_movecursor:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_MoveCursor(sp_row.getSelectedItemPosition() + 1,
                            sp_column.getSelectedItemPosition() + 1);
                }
                break;
            case R.id.btn_setlines:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_SetLines(sp_lines.getSelectedItemPosition() + 1);
                }
                break;
            case R.id.btn_SetBrightness:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_SetBrightness(sp_brightness.getSelectedItemPosition() + 1);
                }
                break;
            case R.id.btn_SetCommand:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_SetCommandSet(sp_commands.getSelectedItemPosition());
                }
                break;
            case R.id.btn_setcharset:
                if (mDsp != null && mDsp.isConnect()) {
                    int i = sp_charset.getSelectedItemPosition();
                    mDsp.DSP_SetCharset(Constant.international_no[i]);
                }
                break;
            case R.id.btn_setcodepage:
                if (mDsp != null && mDsp.isConnect()) {
                    int i = sp_codepage.getSelectedItemPosition();
                    mDsp.DSP_SetCodepage(Constant.codepage_no[i]);
                }
                break;

                */


            case R.id.btn_restart:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_Restart();
                }
                break;
            case R.id.btn_init:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_Init();
                }
                break;

            case R.id.btn_price:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.LED_Display(edt_amount.getText().toString(), 1);
                }
                break;
            case R.id.btn_total:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.LED_Display(edt_amount.getText().toString(), 2);
                }
                break;
            case R.id.btn_collect:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.LED_Display(edt_amount.getText().toString(), 3);
                }
                break;
            case R.id.btn_change:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.LED_Display(edt_amount.getText().toString(), 4);
                }
                break;
            case R.id.btn_showqrcode:
                String qrtext = edt_text.getText().toString().trim();
                if (mDsp != null && mDsp.isConnect() && !qrtext.isEmpty()) {
                    mDsp.DSP_ShowQrCode(qrtext,
                            Integer.parseInt(edt_qrcode_size.getText().toString()),
                            Integer.parseInt(edt_qrcode_x.getText().toString()),
                            Integer.parseInt(edt_qrcode_y.getText().toString()));
                }
                break;
            case R.id.btn_showtext:
                String text = edt_text.getText().toString().trim();
                if (mDsp != null && mDsp.isConnect() && !text.isEmpty()) {
                    mDsp.DSP_ShowText(text,
                            Integer.parseInt(edt_font_size.getText().toString()),
                            Integer.parseInt(edt_font_x.getText().toString()),
                            Integer.parseInt(edt_font_y.getText().toString()));
                }
                break;
            case R.id.btn_showpicture:
                String picname = sp_picturename.getSelectedItem().toString().trim();
                if (mDsp != null && mDsp.isConnect() && !picname.isEmpty()) {
                    mDsp.DSP_ShowPicture(picname,
                            Integer.parseInt(edt_picture_x.getText().toString()),
                            Integer.parseInt(edt_picture_y.getText().toString()));
                }
                break;
            case R.id.btn_selectpicture:
                //选择文件【调用系统的文件管理】
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                //intent.setType("image/*");//选择图片
                //intent.setType("audio/*"); //选择音频
                //intent.setType("video/*"); //选择视频 （mp4 3gp 是android支持的视频格式）
                //intent.setType("video/*;image/*");//同时选择视频和图片
                intent.setType("*/*");//无类型限制
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent, REQUEST_CHOOSEFILE);
                break;
            case R.id.btn_uploadpicture:
                if (tv_picturefilename.getText().toString().isEmpty()) return;
                if (mDsp != null && mDsp.isConnect()) {
                    progressUpdate();
                    runnable = new Runnable() {
                        @Override
                        public void run() {
                            mDsp.DSP_UploadPicture(
                                    sp_picturename.getSelectedItem().toString(),
                                    tv_picturefilename.getText().toString(),
                                    320,
                                    240
                            );
                        }
                    };
                    new Thread(runnable).start();
                }
                break;
            case R.id.btn_connect:
                this.serialPort = sp_port.getSelectedItem().toString();
                this.serialBaudrate = Integer.parseInt(sp_baudrate.getSelectedItem().toString());
                connect();
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.DSP_SetDefaultStyle(3);
                    mDsp.DSP_SwitchStyle(3);
                }
                break;
            case R.id.btn_back:
                if (mDsp != null && mDsp.isConnect()) {
                    mDsp.disconnect();
                }
                mDsp = null;
                this.finish();
                break;
        }
    }

    /**
     * 连接
     */
    private void connect() {
        if (mDsp != null) {
            mDsp.clearbuffer();
            mDsp.disconnect();
        }
        runnable = new Runnable() {
            @Override
            public void run() {

                if (mDsp.SUCCESS == mDsp.connect(serialPort, serialBaudrate, serialFlag)) {
                    showHandler.sendEmptyMessage(0);
                } else {
                    showHandler.sendEmptyMessage(1);
                }
            }
        };
        new Thread(runnable).start();

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {//选择文件返回
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CHOOSEFILE:
                    Uri uri = data.getData();
                    String chooseFilePath = FileChooseUtils.getInstance(this).getChooseFileResultPath(uri);
                    LogUtils.i(TAG, "选择文件返回：" + chooseFilePath);
                    tv_picturefilename.setText(chooseFilePath);
                    break;
            }
        }
    }

    private void progressRun(String title, String message) {
        progressDialog = new ProgressDialog(PD280Activity.this);
        progressDialog.setTitle(title);
        progressDialog.setMessage(message);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条对话框//样式（旋转）
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    private void progressUpdate() {
        progressDialog = new ProgressDialog(PD280Activity.this);
        progressDialog.setTitle("Upload File");
        progressDialog.setMessage("Uploading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置进度条对话框//样式（水平）
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    public void onUpload(int progress, int code) {
        if (code == 0) {
            if (progressDialog.isShowing())
                progressDialog.setProgress(progress);
            if (progress == 100) {
                if (progressDialog.isShowing())
                    progressDialog.dismiss();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtils.showToast("Upload Complete");
                    }
                });
            }
        } else if (code == -1) {
            if (progressDialog.isShowing())
                progressDialog.dismiss();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.showToast("Upload failed (Read Error)");
                }
            });
        } else if (code == -2) {
            if (progressDialog.isShowing())
                progressDialog.dismiss();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.showToast("Upload failed (Write Error)");
                }
            });
        } else if (code == -3) {
            if (progressDialog.isShowing())
                progressDialog.dismiss();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.showToast("Upload failed (CRC Error)");
                }
            });
        }
    }

    private static class NoLeakHandler extends Handler {
        WeakReference<PD280Activity> wf;

        private NoLeakHandler(PD280Activity activity) {
            wf = new WeakReference(activity);
        }
    }

    @SuppressLint("HandlerLeak")
    Handler showHandler = new PD280Activity.NoLeakHandler(PD280Activity.this) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    ToastUtils.showToast(serialPort + " Connect successful");

                    String model = mDsp.DSP_GetModel();
                    int version = mDsp.DSP_GetFirmwareVersion();
                    if (!model.isEmpty())
                        tv_model.setText("Model: " + model);
                    else
                        tv_model.setText("Model: unknown");

                    if (version > 0)
                        tv_firmwareVersion.setText("Firmware Version: " + version);
                    else
                        tv_firmwareVersion.setText("Firmware Version: unknown");

                    btn_display.setEnabled(true);
                    btn_display_upperlinescroll.setEnabled(true);
                    btn_display_at.setEnabled(true);
                    btn_clearline.setEnabled(true);
                    btn_clearscreen.setEnabled(true);
                    btn_cursorleft.setEnabled(true);
                    btn_cursorright.setEnabled(true);
                    btn_cursordown.setEnabled(true);
                    btn_cursorup.setEnabled(true);
                    btn_cursorendleft.setEnabled(true);
                    btn_cursorendright.setEnabled(true);
                    btn_cursorhome.setEnabled(true);
                    btn_cursorend.setEnabled(true);
                    btn_movecursor.setEnabled(true);

                    btn_restart.setEnabled(true);
                    btn_init.setEnabled(true);
                    btn_price.setEnabled(true);
                    btn_total.setEnabled(true);
                    btn_collect.setEnabled(true);
                    btn_change.setEnabled(true);

                    btn_showqrcode.setEnabled(true);
                    btn_showtext.setEnabled(true);
                    btn_showpicture.setEnabled(true);

                    btn_setlines.setEnabled(true);
                    btn_setbrightness.setEnabled(true);
                    btn_setcommand.setEnabled(true);
                    btn_setcharset.setEnabled(true);
                    btn_setcodepage.setEnabled(true);
                    btn_uploadpicture.setEnabled(true);
                    btn_selectpicture.setEnabled(true);
                    break;
                case 1:
                    ToastUtils.showToast(serialPort + " Connect failed");

                    btn_display.setEnabled(false);
                    btn_display_at.setEnabled(false);
                    btn_clearline.setEnabled(false);
                    btn_clearscreen.setEnabled(false);
                    btn_cursorleft.setEnabled(false);
                    btn_cursorright.setEnabled(false);
                    btn_cursordown.setEnabled(false);
                    btn_cursorup.setEnabled(false);
                    btn_cursorendleft.setEnabled(false);
                    btn_cursorendright.setEnabled(false);
                    btn_cursorhome.setEnabled(false);
                    btn_cursorend.setEnabled(false);
                    btn_movecursor.setEnabled(false);

                    btn_restart.setEnabled(false);
                    btn_init.setEnabled(false);
                    btn_price.setEnabled(false);
                    btn_total.setEnabled(false);
                    btn_collect.setEnabled(false);
                    btn_change.setEnabled(false);

                    btn_showqrcode.setEnabled(false);
                    btn_showtext.setEnabled(false);
                    btn_showpicture.setEnabled(false);

                    btn_setlines.setEnabled(false);
                    btn_setbrightness.setEnabled(false);
                    btn_setcommand.setEnabled(false);
                    btn_setcharset.setEnabled(false);
                    btn_setcodepage.setEnabled(false);
                    btn_uploadpicture.setEnabled(false);
                    btn_selectpicture.setEnabled(false);
                    break;
            }
        }
    };
}
