Skip to content

XBee automatic reset #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
5 changes: 4 additions & 1 deletion app/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
encoding="UTF-8"
includeAntRuntime="false"
debug="true"
classpath="../core/core.jar; ${env.JAVA_HOME}/lib/tools.jar; lib/ant.jar; lib/ant-launcher.jar; lib/apple.jar; lib/ecj.jar; lib/jna.jar; lib/RXTXcomm.jar" />
classpath="../core/core.jar; ${env.JAVA_HOME}/lib/tools.jar; lib/ant.jar; lib/ant-launcher.jar; lib/apple.jar; lib/ecj.jar; lib/jna.jar; lib/RXTXcomm.jar">
<!-- <compilerarg value="-Xlint:unchecked"/>
<compilerarg value="-Xlint:deprecation"/> -->
</javac>
<copy todir="bin" overwrite="true" verbose="true">
<fileset dir="src" includes="**/*.properties" />
</copy>
Expand Down
4 changes: 4 additions & 0 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

import javax.swing.*;

import processing.app.debug.ZigBee;

import processing.app.debug.Compiler;
import processing.app.debug.Target;
import processing.core.*;
Expand Down Expand Up @@ -793,6 +795,7 @@ public boolean handleClose(Editor editor) {

// This will store the sketch count as zero
editors.remove(editor);
ZigBee.close();
Editor.serialMonitor.closeSerialPort();
storeSketches();

Expand Down Expand Up @@ -831,6 +834,7 @@ public boolean handleQuit() {
// If quit is canceled, this will be replaced anyway
// by a later handleQuit() that is not canceled.
storeSketches();
ZigBee.close();
Editor.serialMonitor.closeSerialPort();

if (handleQuitEach()) {
Expand Down
20 changes: 17 additions & 3 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ protected void selectSerialPort(String name) {
return;
}
JCheckBoxMenuItem selection = null;
for (int i = 0; i < serialMenu.getItemCount(); i++) {
for (int i = 2; i < serialMenu.getItemCount(); i++) {
JCheckBoxMenuItem item = ((JCheckBoxMenuItem)serialMenu.getItem(i));
if (item == null) {
System.out.println(_("name is null"));
Expand All @@ -940,6 +940,8 @@ protected void selectSerialPort(String name) {
if (selection != null) selection.setState(true);
//System.out.println(item.getLabel());
Preferences.set("serial.port", name);
ZigBee.close();
XBeePanel.onPortChange();
serialMonitor.closeSerialPort();
serialMonitor.setVisible(false);
serialMonitor = new SerialMonitor(Preferences.get("serial.port"));
Expand All @@ -956,6 +958,15 @@ protected void populateSerialMenu() {

serialMenu.removeAll();
boolean empty = true;
rbMenuItem = new JMenuItem("XBee");
rbMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Editor.serialMonitor.closeSerialPort();
new XBeePanel();
}
} );
serialMenu.add(rbMenuItem);
serialMenu.addSeparator();

try
{
Expand Down Expand Up @@ -2284,10 +2295,10 @@ public boolean handleSaveAs() {


public boolean serialPrompt() {
int count = serialMenu.getItemCount();
int count = serialMenu.getItemCount()-2;
Object[] names = new Object[count];
for (int i = 0; i < count; i++) {
names[i] = ((JCheckBoxMenuItem)serialMenu.getItem(i)).getText();
names[i] = ((JCheckBoxMenuItem)serialMenu.getItem(i+2)).getText();
}

String result = (String)
Expand Down Expand Up @@ -2338,6 +2349,7 @@ class DefaultExportHandler implements Runnable {
public void run() {

try {
ZigBee.close();
serialMonitor.closeSerialPort();
serialMonitor.setVisible(false);

Expand Down Expand Up @@ -2374,6 +2386,7 @@ class DefaultExportAppHandler implements Runnable {
public void run() {

try {
ZigBee.close();
serialMonitor.closeSerialPort();
serialMonitor.setVisible(false);

Expand Down Expand Up @@ -2444,6 +2457,7 @@ public void handleSerial() {
if (uploading) return;

try {
ZigBee.close();
serialMonitor.openSerialPort();
serialMonitor.setVisible(true);
} catch (SerialException e) {
Expand Down
39 changes: 35 additions & 4 deletions app/src/processing/app/Serial.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,13 @@ synchronized public void serialEvent(SerialPortEvent serialEvent) {
System.arraycopy(buffer, 0, temp, 0, bufferLast);
buffer = temp;
}
//buffer[bufferLast++] = (byte) input.read();
char c = (char) input.read();
if(monitor == true)
System.out.print((char) input.read());
System.out.print(c);
if (this.consumer != null)
this.consumer.message("" + (char) input.read());
this.consumer.message("" + c);
else
buffer[bufferLast++] = (byte) c;

/*
System.err.println(input.available() + " " +
Expand All @@ -266,6 +268,24 @@ synchronized public void serialEvent(SerialPortEvent serialEvent) {
//System.err.println("out of event " + serialEvent.getEventType());
}

public String waitForData(long time) throws SerialException {
for (int i = 0; i < time/10; ++i ) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {}

String str = new String("");
while (available() > 0) {
str = str + readString();
if (available() == 0)
return str;
try {
Thread.sleep(1); // give a bit of time for the next character. XXXXX (should be a function of bps)
} catch (InterruptedException e) {}
}
}
return null;
}

/**
* Returns the number of bytes that have been read from serial
Expand Down Expand Up @@ -488,6 +508,13 @@ public void write(int what) { // will also cover char

public void write(byte bytes[]) {
try {
if (monitor) {
String s = "serial write(" + bytes.length + "):";
for (int i = 0; i < bytes.length; ++i ) {
s = s + " 0x"+Integer.toHexString(bytes[i]);
}
System.out.println(s);
}
output.write(bytes);
output.flush(); // hmm, not sure if a good idea

Expand All @@ -511,7 +538,11 @@ public void write(byte bytes[]) {
* (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
*/
public void write(String what) {
write(what.getBytes());
byte bytes[] = new byte[what.length()];
for (int i = 0; i < what.length(); ++i) {
bytes[i] = what.charAt(i) < 128 ? (byte)what.charAt(i): (byte)(what.charAt(i) - 256);
}
write(bytes);
}

public void setDTR(boolean state) {
Expand Down
Loading