DELL
2024-08-30 9792926a4a3054f1b2f72e6fc8c810103bb81c16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main;
 
import javax.swing.JPanel;
 
import java.awt.BorderLayout;
 
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.JTextField;
 
import java.awt.GridLayout;
 
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.JLabel;
import javax.swing.DefaultComboBoxModel;
 
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.awt.Font;
 
import javax.swing.ImageIcon;
 
import com.dev.bybb.CommBYBB;
import com.dev.fbs9600_mon.SPCommMon;
import com.dev.ntm.SPCommNTM;
 
public class page_dfu extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JTextField tf_updatefile;
    private JTextField tf_dfu_addr_start;
    private JTextField tf_dfu_addr_end;
    private JButton btn_update_stop;
    private JButton btn_update_start;
    public JTextArea ta_dfu_inf;
    private JComboBox<Object> cb_dfu_bitrate;
    public JCheckBox ckc_read_check;
    private JFileChooser parseDir;
    private JButton btn_openfile;
    private int mDevType = 0;
    private SPCommMon SerialComm_Mon = null;
    private SPCommNTM SPComm_NTM = null;
    private CommBYBB SPComm_BYBB = null;
 
    /**
     * Create the panel.
     */
    public page_dfu() {
        
        setLayout(new BorderLayout(0, 0));
        
        JPanel panel = new JPanel();
        add(panel, BorderLayout.NORTH);
        panel.setLayout(new BorderLayout(0, 0));
        
        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new TitledBorder(null, " \u7A0B\u5E8F\u6587\u4EF6\u8DEF\u5F84", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel.add(panel_1, BorderLayout.NORTH);
        panel_1.setLayout(new BorderLayout(0, 0));
        
        tf_updatefile = new JTextField();
        panel_1.add(tf_updatefile, BorderLayout.CENTER);
        tf_updatefile.setColumns(10);
        
        JPanel panel_2 = new JPanel();
        panel.add(panel_2, BorderLayout.SOUTH);
        panel_2.setLayout(new GridLayout(0, 7, 4, 0));
        
        btn_openfile = new JButton("\u5BFC\u5165\u6587\u4EF6");
        btn_openfile.setIcon(new ImageIcon(page_dfu.class.getResource("/png_32x32/Button Add.png")));
        btn_openfile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(null == parseDir) {
                    parseDir = new JFileChooser();
                }
                
                parseDir.setFileSelectionMode(JFileChooser.FILES_ONLY);
                 parseDir.setFileFilter(new FileFilter() {
                     @Override
                     public boolean accept(File f) {
                         if (f.isDirectory())
                             return true;
                         
                         if((SPCommMon.DEV_TYPE_MON_SAMD09 == mDevType)
                             ||(SPCommMon.DEV_TYPE_MON_SWM == mDevType)
                             || (SPCommMon.DEV_TYPE_NTM == mDevType)) {
                             return (f.getName().toUpperCase().endsWith(".CIM")
                                  || f.getName().toUpperCase().endsWith(".SAM"));
                         } else if(SPCommMon.DEV_TYPE_MON_MSP430 == mDevType) {
                             return f.getName().toUpperCase().endsWith(".SPM");
                         } else {
                             return f.getName().toUpperCase().endsWith(".BIN");
                         }
                     }
                     
                     @Override
                     public String getDescription() {
                         if((SPCommMon.DEV_TYPE_MON_SAMD09 == mDevType)
                             ||(SPCommMon.DEV_TYPE_MON_SWM == mDevType)
                             || (SPCommMon.DEV_TYPE_NTM == mDevType)) {
                             return "*.CIM;*.SAM";
                         } else if(SPCommMon.DEV_TYPE_MON_MSP430 == mDevType) {
                             return "*.SPM";
                         } else {
                             return "*.BIN";
                         }
                     }
                 });
                 parseDir.setDialogTitle("´ò¿ªÉý¼¶Îļþ!");
                 if(JFileChooser.APPROVE_OPTION == parseDir.showOpenDialog(null)) {
                     //String file_n = parseDir.getSelectedFile().getAbsolutePath();
                     //File f = new File(file_n);
                    //if(f.length() < ((1024*16-256-4096)+64)) {
                        tf_updatefile.setText(parseDir.getSelectedFile().getAbsolutePath());
                    //} else {
                    //    JOptionPane.showMessageDialog(frmSerialport, "Îļþ³¤¶È³¬¹ýоƬ´æ´¢¿Õ¼ä, ÇëÑ¡ÔñÕýÈ·µÄÎļþ!");
                    //}
                 }
            }
        });
        panel_2.add(btn_openfile);
        
        JPanel panel_3 = new JPanel();
        panel_3.setBorder(new TitledBorder(null, "DFU\u6CE2\u7279\u7387", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel_2.add(panel_3);
        panel_3.setLayout(new BorderLayout(0, 0));
        
        cb_dfu_bitrate = new JComboBox<Object>();
        cb_dfu_bitrate.setModel(new DefaultComboBoxModel<Object>(new String[] {"4800", "9600", "19200", "38400", "57600", "115200"}));
        panel_3.add(cb_dfu_bitrate);
        
        JPanel panel_4 = new JPanel();
        panel_4.setBorder(new TitledBorder(null, "\u8BFB\u53D6\u6821\u9A8C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel_2.add(panel_4);
        panel_4.setLayout(new BorderLayout(0, 0));
        
        ckc_read_check = new JCheckBox("\u542F\u7528");
        panel_4.add(ckc_read_check, BorderLayout.CENTER);
        
        JPanel panel_5 = new JPanel();
        panel_5.setBorder(new TitledBorder(null, "DFU\u8D77\u59CB\u5355\u4F53\u5730\u5740", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel_2.add(panel_5);
        panel_5.setLayout(new BorderLayout(0, 0));
        
        tf_dfu_addr_start = new JTextField();
        panel_5.add(tf_dfu_addr_start);
        tf_dfu_addr_start.setColumns(10);
        
        JPanel panel_6 = new JPanel();
        panel_6.setBorder(new TitledBorder(null, "DFU\u7ED3\u675F\u5355\u4F53\u5730\u5740", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel_2.add(panel_6);
        panel_6.setLayout(new BorderLayout(0, 0));
        
        tf_dfu_addr_end = new JTextField();
        panel_6.add(tf_dfu_addr_end);
        tf_dfu_addr_end.setColumns(10);
        
        btn_update_start = new JButton("\u542F\u52A8");
        btn_update_start.setIcon(new ImageIcon(page_dfu.class.getResource("/png_32x32/Button Play.png")));
        btn_update_start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(tf_updatefile.getText().trim().length() < 3) {
                    JOptionPane.showMessageDialog(null, "ÇëÏÈÊäÈëÉý¼¶³ÌÐòÎļþ!");
                    return;
                }
                
                if(null != SerialComm_Mon) {
                    //System.out.println("SerialComm_Mon");
                    //-------------------------------------------------------------//
                    boolean pwd_ch = true;
                    for(int len=0; len<SerialComm_Mon.dt_dfu_pwd.length; len++) {
                        if(SerialComm_Mon.dt_dfu_pwd_auto[len] != SerialComm_Mon.dt_dfu_pwd[len]) {
                            pwd_ch = false;
                            break;
                        }
                    }
                    //-------------------------------------------------------------//
                    if(false == pwd_ch) {
                        JPasswordField pw = new JPasswordField();
                        JOptionPane.showMessageDialog(null, pw, "ÇëÊäÈëÊÚȨÃÜÂë!", JOptionPane.PLAIN_MESSAGE);
                        char[] pass = pw.getPassword();
                        if(pass.length < 1) {
                            return;
                        }
                        if(pass.length != SerialComm_Mon.dt_dfu_pwd.length) {
                            JOptionPane.showMessageDialog(null, "ÃÜÂ볤¶È´íÎó!");
                            return;
                        }
                        pwd_ch = true;
                        for(int len=0; len<pass.length; len++) {
                            SerialComm_Mon.dt_dfu_pwd[len] = pass[len];
                            if(SerialComm_Mon.dt_dfu_pwd[len] != SerialComm_Mon.dt_dfu_pwd_auto[len]) {
                                pwd_ch = false;
                                break;
                            }
                        }
                        if(false == pwd_ch) {
                            for(int len=0; len<SerialComm_Mon.dt_dfu_pwd.length; len++) {
                                SerialComm_Mon.dt_dfu_pwd[len] = 0;
                            }
                            JOptionPane.showMessageDialog(null, "ÃÜÂë´íÎó!");
                            return;
                        }
                    }
                    //-------------------------------------------------------------//
                    
                    int dfu_addr_start = 0;
                    int dfu_addr_end = 0;
                    try {
                        dfu_addr_start = Integer.parseInt(tf_dfu_addr_start.getText());
                        dfu_addr_end = Integer.parseInt(tf_dfu_addr_end.getText());
                    } catch (Exception e1) {
                        JOptionPane.showMessageDialog(null, "ÇëÏÈÊäÈëÄ¿±êÄ£¿éÆðʼµØÖ·ºÍ½áÊøµØÖ·!");
                        return;
                    }
                    
                    if(dfu_addr_start > 255) {
                        dfu_addr_start = 255;
                        tf_dfu_addr_start.setText(String.valueOf(dfu_addr_start));
                    }
                    if(dfu_addr_end > 255) {
                        dfu_addr_end = 255;
                        tf_dfu_addr_end.setText(String.valueOf(dfu_addr_end));
                    }
                    
                    SerialComm_Mon.setDfuCommAddr(dfu_addr_start, dfu_addr_end);
                    SerialComm_Mon.setUpdateCmd(true, ckc_read_check.isSelected(), parseDir.getSelectedFile().getAbsolutePath(), 
                                                Integer.parseInt((String) cb_dfu_bitrate.getSelectedItem()));
                    SerialComm_Mon.setCommCmd(SPCommMon.CMD_TYPE_WRITE, SPCommMon.UART_MonomerDFU_RegAddr, 
                                                (short)0x55AA, (short)0xAA55, (short)0x7F7F);
                } else if(null != SPComm_NTM) {
                    SPComm_NTM.setUpdateCmd(true, parseDir.getSelectedFile().getAbsolutePath());
                    //System.out.println("SPComm_NTM");
                } else if(null != SPComm_BYBB) {
                    if(JOptionPane.NO_OPTION == JOptionPane.showConfirmDialog(null, "È·¶¨ÒªÉý¼¶³ÌÐòÂð?", 
                                                                "²Ù×÷È·ÈÏ", JOptionPane.YES_NO_OPTION)) {
                        return;
                    }
                    
                    SPComm_BYBB.setCommCmd_DFU(true, parseDir.getSelectedFile().getAbsolutePath(), ta_dfu_inf);
                }
            }
        });
        panel_2.add(btn_update_start);
        
        btn_update_stop = new JButton("\u505C\u6B62");
        btn_update_stop.setIcon(new ImageIcon(page_dfu.class.getResource("/png_32x32/Button Stop.png")));
        btn_update_stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(null != SerialComm_Mon) {
                    SerialComm_Mon.setUpdateCmd(false, false, "",  
                                                Integer.parseInt((String) cb_dfu_bitrate.getSelectedItem()));
                } else if(null != SPComm_NTM) {
                    SPComm_NTM.setUpdateCmd(false, "");
                } else if(null != SPComm_BYBB) {
                    SPComm_BYBB.setCommCmd_DFU(false, "", ta_dfu_inf);
                }
            }
        });
        panel_2.add(btn_update_stop);
        
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollPane, BorderLayout.CENTER);
        
        ta_dfu_inf = new JTextArea();
        ta_dfu_inf.setFont(new Font("Consolas", Font.PLAIN, 18));
        scrollPane.setViewportView(ta_dfu_inf);
        
        JLabel lblNewLabel = new JLabel("\u5347\u7EA7\u4FE1\u606F");
        scrollPane.setColumnHeaderView(lblNewLabel);
        
        setButttonState(false);
    }
    
    public void setDfu_Mon_Addr(String start_addr, String end_addr) {
        tf_dfu_addr_start.setText(start_addr);
        tf_dfu_addr_end.setText(end_addr);
    }
    
    public void setButttonState(boolean state) {
        btn_openfile.setEnabled(state);
        btn_update_start.setEnabled(state);
        btn_update_stop.setEnabled(state);
    }
    
    public void set_SerialComm_Mon(SPCommMon sp_comm) {
        SerialComm_Mon = sp_comm;
        if(null != SerialComm_Mon) {
            mDevType = SerialComm_Mon.m_DEV_TYPE;
        }
    }
    
    public void setComm_NTM(SPCommNTM sp_comm) {
        SPComm_NTM = sp_comm;
        mDevType = SPCommMon.DEV_TYPE_NTM;
    }
    
    public void setComm_BYBB(CommBYBB sp_comm) {
        SPComm_BYBB = sp_comm;
        mDevType = SPCommMon.DEV_TYPE_MON_SAMD09;
    }
}