Adding interface for serial communication from PC to Arduino board.

This commit is contained in:
David A. Mellis 2006-07-08 10:43:47 +00:00
parent f6f4fe59c5
commit 77ecc6476c
3 changed files with 119 additions and 72 deletions

View File

@ -1399,6 +1399,7 @@ public class Editor extends JFrame
buttons.activate(EditorButtons.SERIAL);
serialPort = new Serial(true);
debugging = true;
status.serial("Serial message:");
} else {
doStop();
}
@ -1421,6 +1422,7 @@ public class Editor extends JFrame
public void doStop() {
//if (runtime != null) runtime.stop();
if (debugging) {
status.unserial();
serialPort.dispose();
debugging = false;
}

View File

@ -39,11 +39,13 @@ public class EditorStatus extends JPanel implements ActionListener {
static final int ERR = 1;
static final int PROMPT = 2;
static final int EDIT = 3;
static final int SERIAL = 4;
static final int YES = 1;
static final int NO = 2;
static final int CANCEL = 3;
static final int OK = 4;
static final int SEND = 5;
static final String NO_MESSAGE = "";
@ -64,6 +66,7 @@ public class EditorStatus extends JPanel implements ActionListener {
JButton noButton;
JButton cancelButton;
JButton okButton;
JButton sendButton;
JTextField editField;
//Thread promptThread;
@ -75,7 +78,7 @@ public class EditorStatus extends JPanel implements ActionListener {
empty();
if (bgcolor == null) {
bgcolor = new Color[4];
bgcolor = new Color[5];
// Arduino 0003 switched to a blue color scheme to visually distinguish
// itself from Processing. Because the image files for certain interface
// elements (e.g. buttons and tabs) are distributed with the application
@ -92,12 +95,14 @@ public class EditorStatus extends JPanel implements ActionListener {
bgcolor[1] = Preferences.getColor("status.error.bgcolor");
bgcolor[2] = Preferences.getColor("status.prompt.bgcolor");
bgcolor[3] = Preferences.getColor("status.prompt.bgcolor");
bgcolor[4] = new Color(0x54, 0x91, 0x9e);
fgcolor = new Color[4];
fgcolor = new Color[5];
fgcolor[0] = Preferences.getColor("status.notice.fgcolor");
fgcolor[1] = Preferences.getColor("status.error.fgcolor");
fgcolor[2] = Preferences.getColor("status.prompt.fgcolor");
fgcolor[3] = Preferences.getColor("status.prompt.fgcolor");
fgcolor[4] = Preferences.getColor("status.notice.fgcolor");
}
}
@ -174,6 +179,26 @@ public class EditorStatus extends JPanel implements ActionListener {
empty();
}
public void serial(String message)
{
mode = SERIAL;
this.message = message;
sendButton.setVisible(true);
editField.setVisible(true);
editField.setText("");
editField.requestFocus();
repaint();
}
public void unserial()
{
sendButton.setVisible(false);
editField.setVisible(false);
empty();
}
/*
public void update() {
@ -249,6 +274,7 @@ public class EditorStatus extends JPanel implements ActionListener {
noButton = new JButton(Preferences.PROMPT_NO);
cancelButton = new JButton(Preferences.PROMPT_CANCEL);
okButton = new JButton(Preferences.PROMPT_OK);
sendButton = new JButton(Preferences.PROMPT_SEND);
// !@#(* aqua ui #($*(( that turtle-neck wearing #(** (#$@)(
// os9 seems to work if bg of component is set, but x still a bastard
@ -257,6 +283,7 @@ public class EditorStatus extends JPanel implements ActionListener {
noButton.setBackground(bgcolor[PROMPT]);
cancelButton.setBackground(bgcolor[PROMPT]);
okButton.setBackground(bgcolor[PROMPT]);
sendButton.setBackground(bgcolor[SERIAL]);
}
setLayout(null);
@ -264,16 +291,19 @@ public class EditorStatus extends JPanel implements ActionListener {
noButton.addActionListener(this);
cancelButton.addActionListener(this);
okButton.addActionListener(this);
sendButton.addActionListener(this);
add(yesButton);
add(noButton);
add(cancelButton);
add(okButton);
add(sendButton);
yesButton.setVisible(false);
noButton.setVisible(false);
cancelButton.setVisible(false);
okButton.setVisible(false);
sendButton.setVisible(false);
editField = new JTextField();
editField.addActionListener(this);
@ -297,6 +327,7 @@ public class EditorStatus extends JPanel implements ActionListener {
// KeyEvent.VK_SPACE);
int c = event.getKeyChar();
if (mode == EDIT) {
if (c == KeyEvent.VK_ENTER) { // accept the input
String answer = editField.getText();
editor.sketch.nameCode(answer);
@ -368,6 +399,14 @@ public class EditorStatus extends JPanel implements ActionListener {
event.consume();
//System.out.println("code is " + code + " char = " + c);
}
} else { // mode != EDIT (i.e. mode == SERIAL)
if (c == KeyEvent.VK_ENTER) { // accept the input
String answer = editField.getText();
editor.serialPort.write(answer + "\n");
event.consume();
editField.setText("");
}
}
//System.out.println("code is " + code + " char = " + c);
}
});
@ -390,11 +429,13 @@ public class EditorStatus extends JPanel implements ActionListener {
cancelButton.setLocation(cancelLeft, top);
editField.setLocation(yesLeft - Preferences.BUTTON_WIDTH, top);
okButton.setLocation(noLeft, top);
sendButton.setLocation(cancelLeft, top);
yesButton.setSize( Preferences.BUTTON_WIDTH, Preferences.BUTTON_HEIGHT);
noButton.setSize( Preferences.BUTTON_WIDTH, Preferences.BUTTON_HEIGHT);
cancelButton.setSize(Preferences.BUTTON_WIDTH, Preferences.BUTTON_HEIGHT);
okButton.setSize( Preferences.BUTTON_WIDTH, Preferences.BUTTON_HEIGHT);
sendButton.setSize( Preferences.BUTTON_WIDTH, Preferences.BUTTON_HEIGHT);
editField.setSize( 2*Preferences.BUTTON_WIDTH, Preferences.BUTTON_HEIGHT);
}
@ -437,6 +478,9 @@ public class EditorStatus extends JPanel implements ActionListener {
//editor.handleSaveAs2(answer);
editor.sketch.nameCode(answer);
unedit();
} else if (e.getSource() == sendButton) {
editor.serialPort.write(editField.getText());
editField.setText("");
}
}
}

View File

@ -71,6 +71,7 @@ public class Preferences {
static final String PROMPT_NO = "No";
static final String PROMPT_CANCEL = "Cancel";
static final String PROMPT_OK = "OK";
static final String PROMPT_SEND = "Send";
static final String PROMPT_BROWSE = "Browse";
/**