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
@ -88,16 +91,18 @@ public class EditorStatus extends JPanel implements ActionListener {
// however, it obviates the need to provide for version-specific
// preferences.
//bgcolor[0] = Preferences.getColor("status.notice.bgcolor");
bgcolor[0] = new Color(0x54, 0x91, 0x9e);
bgcolor[0] = new Color(0x54, 0x91, 0x9e);
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");
}
}
@ -173,6 +178,26 @@ public class EditorStatus extends JPanel implements ActionListener {
editField.setVisible(false);
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();
}
/*
@ -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);
@ -296,77 +326,86 @@ public class EditorStatus extends JPanel implements ActionListener {
//System.out.println("got event " + event + " " +
// KeyEvent.VK_SPACE);
int c = event.getKeyChar();
if (c == KeyEvent.VK_ENTER) { // accept the input
String answer = editField.getText();
editor.sketch.nameCode(answer);
unedit();
event.consume();
// easier to test the affirmative case than the negative
} else if ((c == KeyEvent.VK_BACK_SPACE) ||
(c == KeyEvent.VK_DELETE) ||
(c == KeyEvent.VK_RIGHT) ||
(c == KeyEvent.VK_LEFT) ||
(c == KeyEvent.VK_UP) ||
(c == KeyEvent.VK_DOWN) ||
(c == KeyEvent.VK_HOME) ||
(c == KeyEvent.VK_END) ||
(c == KeyEvent.VK_SHIFT)) {
//System.out.println("nothing to see here");
//noop();
} else if (c == KeyEvent.VK_ESCAPE) {
unedit();
editor.buttons.clear();
event.consume();
} else if (c == KeyEvent.VK_SPACE) {
//System.out.println("got a space");
// if a space, insert an underscore
//editField.insert("_", editField.getCaretPosition());
/* tried to play nice and see where it got me
editField.dispatchEvent(new KeyEvent(editField,
KeyEvent.KEY_PRESSED,
System.currentTimeMillis(),
0, 45, '_'));
*/
//System.out.println("start/end = " +
// editField.getSelectionStart() + " " +
// editField.getSelectionEnd());
String t = editField.getText();
//int p = editField.getCaretPosition();
//editField.setText(t.substring(0, p) + "_" + t.substring(p));
//editField.setCaretPosition(p+1);
int start = editField.getSelectionStart();
int end = editField.getSelectionEnd();
editField.setText(t.substring(0, start) + "_" +
t.substring(end));
editField.setCaretPosition(start+1);
//System.out.println("consuming event");
event.consume();
} else if ((c == '_') || (c == '.') || // allow .pde and .java
((c >= 'A') && (c <= 'Z')) ||
((c >= 'a') && (c <= 'z'))) {
// everything fine, catches upper and lower
//noop();
} else if ((c >= '0') && (c <= '9')) {
// getCaretPosition == 0 means that it's the first char
// and the field is empty.
// getSelectionStart means that it *will be* the first
// char, because the selection is about to be replaced
// with whatever is typed.
if ((editField.getCaretPosition() == 0) ||
(editField.getSelectionStart() == 0)) {
// number not allowed as first digit
//System.out.println("bad number bad");
if (mode == EDIT) {
if (c == KeyEvent.VK_ENTER) { // accept the input
String answer = editField.getText();
editor.sketch.nameCode(answer);
unedit();
event.consume();
// easier to test the affirmative case than the negative
} else if ((c == KeyEvent.VK_BACK_SPACE) ||
(c == KeyEvent.VK_DELETE) ||
(c == KeyEvent.VK_RIGHT) ||
(c == KeyEvent.VK_LEFT) ||
(c == KeyEvent.VK_UP) ||
(c == KeyEvent.VK_DOWN) ||
(c == KeyEvent.VK_HOME) ||
(c == KeyEvent.VK_END) ||
(c == KeyEvent.VK_SHIFT)) {
//System.out.println("nothing to see here");
//noop();
} else if (c == KeyEvent.VK_ESCAPE) {
unedit();
editor.buttons.clear();
event.consume();
} else if (c == KeyEvent.VK_SPACE) {
//System.out.println("got a space");
// if a space, insert an underscore
//editField.insert("_", editField.getCaretPosition());
/* tried to play nice and see where it got me
editField.dispatchEvent(new KeyEvent(editField,
KeyEvent.KEY_PRESSED,
System.currentTimeMillis(),
0, 45, '_'));
*/
//System.out.println("start/end = " +
// editField.getSelectionStart() + " " +
// editField.getSelectionEnd());
String t = editField.getText();
//int p = editField.getCaretPosition();
//editField.setText(t.substring(0, p) + "_" + t.substring(p));
//editField.setCaretPosition(p+1);
int start = editField.getSelectionStart();
int end = editField.getSelectionEnd();
editField.setText(t.substring(0, start) + "_" +
t.substring(end));
editField.setCaretPosition(start+1);
//System.out.println("consuming event");
event.consume();
} else if ((c == '_') || (c == '.') || // allow .pde and .java
((c >= 'A') && (c <= 'Z')) ||
((c >= 'a') && (c <= 'z'))) {
// everything fine, catches upper and lower
//noop();
} else if ((c >= '0') && (c <= '9')) {
// getCaretPosition == 0 means that it's the first char
// and the field is empty.
// getSelectionStart means that it *will be* the first
// char, because the selection is about to be replaced
// with whatever is typed.
if ((editField.getCaretPosition() == 0) ||
(editField.getSelectionStart() == 0)) {
// number not allowed as first digit
//System.out.println("bad number bad");
event.consume();
}
} else {
event.consume();
//System.out.println("code is " + code + " char = " + c);
}
} else {
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";
/**