Commit 5d41fb83 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

9781565926929

0 → 120000
+1 −0
Changes for 9781565926929: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
9781565926929
 No newline at end of file

README.md

0 → 100644
+15 −0
Changes for README.md: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Example files for the title:  
	  
# The Unofficial Guide to LEGO MINDSTORMS Robots, by Jonathan Knudsen
	  
[![The Unofficial Guide to LEGO MINDSTORMS Robots, by Jonathan Knudsen](http://akamaicovers.oreilly.com/images/9781565926929/cat.gif)](https://www.safaribooksonline.com/)
	  
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
	  
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
	  
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
	  
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
	  
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.

lmstorms/Download.java

0 → 100644
+173 −0
Changes for lmstorms/Download.java: 173 added lines, 0 removed lines.
Original line number Diff line number Diff line
import java.io.*;
import java.util.*;

import javax.comm.*;

public class Download {
  public static void main(String[] args) {
    String filename = args[args.length - 1];
    String portName = "COM1";
  
    for (int i = 0; i < args.length - 2; i++) {
      if (args[i].equals("-port")) portName = args[i + 1];
    }

    try { new Download(filename, portName); }
    catch (NoSuchPortException nspe) {
      System.out.println("Sorry, I don't know about the " +
          portName + " port.");
    }
    catch (PortInUseException piue) {
      System.out.println("Sorry, somebody else is using " +
          portName + ".");
    }
    catch (UnsupportedCommOperationException ucoe) {
      System.out.println(ucoe);
    }
    catch (IOException ioe) {
      System.out.println("An IOException occurred: " + ioe);
    }
  }
  
  private SerialPort mPort;
  private Reader mFileIn;
  private Writer mOut;
  private PortListener mPortListener;
  
  private static final int kCharSleep = 20;
  private static final int kTimeOut = 800;

  public Download(String filename, String portName)
      throws NoSuchPortException, PortInUseException,
      UnsupportedCommOperationException, IOException {
    initialize(portName);
    mFileIn = new FileReader(filename);
    run();
  }
    
  protected void initialize(String portName)
      throws NoSuchPortException, PortInUseException,
      UnsupportedCommOperationException, IOException {
    CommPortIdentifier id =
        CommPortIdentifier.getPortIdentifier(portName);
    mPort = (SerialPort)id.open("Download", 1000);
    mPort.setSerialPortParams(
        2400,
        SerialPort.DATABITS_8,
        SerialPort.STOPBITS_1,
        SerialPort.PARITY_NONE
    );
    Reader in = new InputStreamReader(mPort.getInputStream());
    mPortListener = new PortListener(in);
    mOut = new OutputStreamWriter(mPort.getOutputStream());
  }

  public void run() {
    int c, n = 1;
    try {
      sendReturn();
      sendReturn();
      while ((c = mFileIn.read()) != -1) {
        if (c == '\r') {
          sendReturn();
          if (mPortListener.isComplete() == false) {
            throw new DownloadException("Timed out waiting " +
                "for a response from the RCX.");
          }
          else if (mPortListener.isError() == true) {
            throw new DownloadException("Error: " + 
                mPortListener.getLastLine());
          }
          System.out.print(".");
          n++;
        }
        else if (c != '\n') {
          mPortListener.reset();
          mOut.write(c);
          mOut.flush();
          Thread.sleep(kCharSleep);
        }
      }
      sendReturn();
      sendReturn();
    }
    catch (InterruptedException ie) { System.out.println(ie); }
    catch (DownloadException de) {
      System.out.println();
      System.out.println("Line " + n + ":");
      System.out.println("  " + de.getMessage());
    }
    catch (IOException ioe) { System.out.println(ioe); }
    
    // Regardless of what happened, try to clean up.
    try {
      mPortListener.stop();
      mFileIn.close();
      mOut.close();
      mPort.close();
    }
    catch (IOException ioe) {}
    System.exit(0);
  }
  
  protected void sendReturn() throws IOException, InterruptedException {
    mOut.write('\r');
    mOut.flush();
    Thread.sleep(kCharSleep);
    // Wait for response, or time out.
    long savedTime = System.currentTimeMillis();
    boolean trucking = true;
    while (trucking) {
      if (mPortListener.isComplete()) trucking = false;
      long currentTime = System.currentTimeMillis();
      if (currentTime - savedTime > kTimeOut) trucking = false;
      Thread.sleep(20);
    }
  }
  
  public class PortListener
      implements Runnable {
    private Thread mThread;
    private BufferedReader mIn;
    private boolean mComplete = false;
    private boolean mError = false;
    private String mLastLine;

    public PortListener(Reader in) {
      mIn = new BufferedReader(in);
      mThread = new Thread(this);
      mThread.start();
    }

    public void run() {
      String line;
      try {
        while((line = mIn.readLine()) != null) {
          line = line.trim();
          mLastLine = line;
          if (line.indexOf("ok") != -1) mComplete = true;
          if (line.indexOf("redefine") != -1) mComplete = true;
          if (line.indexOf("undefined") != -1) mComplete = mError = true;
          if (line.length() == 0) mComplete = true;
        }
      }
      catch (IOException ioe) {
        System.out.println("PortListener: ioe " + ioe);
      }
    }
    
    public void stop() throws IOException {
      mThread.interrupt();
    }
    
    public void reset() { mComplete = false; mError = false; }
    public boolean isComplete() { return mComplete; }
    public boolean isError() { return mError; }
    public String getLastLine() { return mLastLine; }
  }
  
  public class DownloadException
      extends IOException {
    public DownloadException(String message) { super(message); }
  }
}
 No newline at end of file

lmstorms/Hank.nqc

0 → 100644
+27 −0
Changes for lmstorms/Hank.nqc: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
#define BACK_TIME 50
#define TURN_TIME 80

task main() {
  SetSensor(SENSOR_1, SENSOR_TOUCH);
  SetSensor(SENSOR_3, SENSOR_TOUCH);
  OnFwd(OUT_A + OUT_C);
  while (true) {
    if (SENSOR_1 == 1) {
      PlayTone(440, 50);
      OnRev(OUT_A + OUT_C);
      Wait(BACK_TIME);
      OnFwd(OUT_A);
      Wait(TURN_TIME);
      OnFwd(OUT_C);
    }
    if (SENSOR_3 == 1) {
      PlayTone(880, 50);
      OnRev(OUT_A + OUT_C);
      Wait(BACK_TIME);
      OnFwd(OUT_C);
      Wait(TURN_TIME);
      OnFwd(OUT_A);
    }
  }
}

lmstorms/Hank.prg

0 → 100644
+2.83 KiB

File added.

No diff preview for this file type.

Loading