|
Message
From: cvs at opencores.org<cvs@o...>
Date: Tue Jun 5 17:36:36 CEST 2007
Subject: [cvs-checkins] MODIFIED: jop ...
Date: 00/07/06 05:17:36 Added: jop/java/target/src/app/lego AudioPlayback.java AudioTest.java BackEMFAvg.java BackEMFAvgDeviation.java BackEMFTest.java BeQuiet.java ButtonsLedsTest.java CrashAvoider2.java Demo1.java Demo2.java FlashProgrammer.java InputTest.java InputTestBinary.java IsAliveTest.java KnightRider.java LegoBoardTest.java LineFollower.java ReadTrivialFS.java sound1.java sound2.java sound4.java Log: Adding the Lego Java Programs Revision Changes Path 1.1 jop/java/target/src/app/lego/AudioPlayback.java http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/AudioPlayback.java?rev=1.1&content-type=text/x-cvsweb-markup Index: AudioPlayback.java =================================================================== package lego; import lego.utils.TrivialFS; public class AudioPlayback { // static final int SAMPLERATE = 22050; // static final int SAMPLERATE = 48000; static final int SAMPLERATE = 44100; static final int US_PER_SAMPLE = 23; // more correct for 44100 Hz //1000000/SAMPLERATE; /** * @param args */ public static void main(String[] args) { // new RtThread(10, 1000000/SAMPLERATE) // { // public void run() { System.out.print("Samplerate: "); System.out.println(SAMPLERATE); System.out.print("us/sample: "); System.out.println(US_PER_SAMPLE); while (true) { int count = TrivialFS.getFileCount(); for (int i = 0; i < count; i++) lego.utils.AudioPlayback.playFromTrivialFS(i, US_PER_SAMPLE); } // } // }; // RtThread.startMission(); // } } } } 1.3 jop/java/target/src/app/lego/AudioTest.java http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/AudioTest.java.diff?r1=1.2&r2=1.3 (In the diff below, changes in quantity of whitespace are not shown.) Index: AudioTest.java =================================================================== RCS file: AudioTest.java diff -N AudioTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ AudioTest.java 5 Jun 2007 15:36:36 -0000 1.3 @@ -0,0 +1,86 @@ +package lego; + +import lego.lib.*; +import joprt.RtThread; + +public class AudioTest +{ + static int pcm = 0; + static int logFrequency = 0; + + public static void main(String[] args) + { + new RtThread(10, 61) + { + public void run() + { + int counter = 0; + boolean toggle = false; + + while (true) + { + if (((((1<<(13-logFrequency)))-1) & counter) == 0)
+ {
+ Speaker.write(toggle ? pcm : 0);
+ toggle = !toggle;
+ }
+
+ waitForNextPeriod();
+ counter++;
+ }
+ }
+ };
+
+ new RtThread(10, 100*1000)
+ {
+ public void run()
+ {
+ int lastpcmPercentage = pcm;
+ int lastlogFrequency = logFrequency;
+
+ while(true)
+ {
+ if (Buttons.getButton(1))
+ {
+ pcm += 5;
+ }
+ if (Buttons.getButton(0))
+ {
+ pcm -= 5;
+ }
+
+ if (Buttons.getButton(3))
+ {
+ logFrequency += 1;
+ }
+ if (Buttons.getButton(2))
+ {
+ logFrequency -= 1;
+ }
+
+ logFrequency = Math.max(0, Math.min(13, logFrequency));
+ pcm = Math.max(0, Math.min(255, pcm));
+
+ if (lastpcmPercentage != pcm)
+ {
+ System.out.print("PCM: ");
+ System.out.println(pcm);
+ }
+ lastpcmPercentage = pcm;
+
+ if (lastlogFrequency!= logFrequency)
+ {
+ System.out.print("Frequency: ");
+ System.out.println(((((1<<(13-logFrequency)))-1)));
+ System.out.println(logFrequency);
+ }
+ lastlogFrequency = logFrequency;
+
+ waitForNextPeriod();
+ }
+ }
+ };
+
+ RtThread.startMission();
+ }
+}
1.3 jop/java/target/src/app/lego/BackEMFAvg.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/BackEMFAvg.java.diff?r1=1.2&r2=1.3
(In the diff below, changes in quantity of whitespace are not shown.)
Index: BackEMFAvg.java
===================================================================
RCS file: BackEMFAvg.java
diff -N BackEMFAvg.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ BackEMFAvg.java 5 Jun 2007 15:36:36 -0000 1.3
@@ -0,0 +1,77 @@
+package lego;
+
+import lego.lib.*;
+
+import java.io.*;
+
+import joprt.RtThread;
+
+public class BackEMFAvg
+{
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws IOException
+ {
+ System.out.println("Starting...");
+
+ Motor m0 = new Motor(0);
+ m0.setMotor(Motor.STATE_FORWARD, true, 0);
+ int pwmValue = 0;
+
+ final int VALUE_COUNT = 10;
+ int[][] values = new int[VALUE_COUNT][2];
+ int index = 0;
+
+ while (true)
+ {
+ if (Buttons.getButton(0))
+ pwmValue = Math.max(0, pwmValue-1);
+ if (Buttons.getButton(1))
+ pwmValue = Math.min(0x3f, pwmValue+1);
+ if (Buttons.getButton(3))
+ m0.setMeasure(true);
+ if (Buttons.getButton(2))
+ m0.setMeasure(false);
+
+ m0.setDutyCycle((pwmValue * Motor.MAX_DUTYCYCLE) / 0x3f);
+ Leds.setLeds(pwmValue >> 2);
+
+ RtThread.busyWait(25 * 1000);
+
+ int backEMF[] = m0.readBackEMF();
+
+ values[index] = backEMF;
+
+ int avg[] = new int[] {0,0};
+ for (int i = 0; i < VALUE_COUNT; i++)
+ for (int j = 0; j < 2; j++)
+ avg[j] += values[i][j];
+
+ if (index == VALUE_COUNT-1)
+ {
+ System.out.print("DC: ");
+ System.out.print(pwmValue);
+ System.out.print(" bEMF: ");
+ }
+ for (int j = 0; j < 2; j++)
+ {
+ avg[j] /= VALUE_COUNT;
+ if (index == VALUE_COUNT-1)
+ {
+ System.out.print(avg[j] - 0x100);
+ System.out.print(" ");
+ }
+ }
+ if (index == VALUE_COUNT-1)
+ {
+ System.out.println();
+ }
+
+
+ index = (index + 1) % VALUE_COUNT;
+ }
+ }
+
+}
1.3 jop/java/target/src/app/lego/BackEMFAvgDeviation.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/BackEMFAvgDeviation.java.diff?r1=1.2&r2=1.3
(In the diff below, changes in quantity of whitespace are not shown.)
Index: BackEMFAvgDeviation.java
===================================================================
RCS file: BackEMFAvgDeviation.java
diff -N BackEMFAvgDeviation.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ BackEMFAvgDeviation.java 5 Jun 2007 15:36:36 -0000 1.3
@@ -0,0 +1,77 @@
+package lego;
+
+import lego.lib.*;
+
+import java.io.*;
+
+import joprt.RtThread;
+
+/*
+ * Typical deviations @ full speed
+ * 450 450 450 452 450 553 ~450
+ * 300 440 360 405 339 290 160-467
+ *
+ * Deviations | Avg @ 0 speed
+ * 3500
+ * 3500
+ *
+ * Deviations | Avg @ full speed
+ * 4500
+ * 3600-4250
+ *
+ * Longer buffer:
+ *
+ * Deviations | Avg @ 1 speed (bug w/ 0)
+ * 0 -16
+ * 4801
+ *
+ * Deviations | Avg @ full speed
+ * 0 -6
+ * 0 -6
+ */
+public class BackEMFAvgDeviation
+{
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws IOException
+ {
+ while (true)
+ {
+ System.out.println("Starting...");
+
+ Motor m0 = new Motor(0);
+ m0.setMotorPercentage(Motor.STATE_FORWARD, true, 1);
+
+ RtThread.busyWait(10000 * 1000);
+
+ final int VALUE_COUNT = 1000;
+ int[][] values = new int[VALUE_COUNT][2];
+
+ for (int i = 0; i < VALUE_COUNT; i++)
+ {
+ RtThread.busyWait(10000);
+ values[i] = m0.readNormalizedBackEMF();
+ }
+
+ int avg[] = new int[] {0,0};
+ for (int i = 0; i < VALUE_COUNT; i++)
+ for (int j = 0; j < 2; j++)
+ avg[j] += values[i][j];
+
+ for (int j = 0; j < 2; j++)
+ avg[j] /= VALUE_COUNT;
+
+ int dev[] = new int[] {0,0};
+ for (int i = 0; i < VALUE_COUNT; i++)
+ for (int j = 0; j < 2; j++)
+ dev[j] += Math.abs(values[i][j] - avg[j]);
+
+ for (int j = 0; j < 2; j++)
+ System.out.println("Sum of deviations: " + dev[j] + " Avg: " + avg[j]);
+
+ m0.setState(Motor.STATE_OFF);
+ }
+ }
+}
1.3 jop/java/target/src/app/lego/BackEMFTest.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/BackEMFTest.java.diff?r1=1.2&r2=1.3
(In the diff below, changes in quantity of whitespace are not shown.)
Index: BackEMFTest.java
===================================================================
RCS file: BackEMFTest.java
diff -N BackEMFTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ BackEMFTest.java 5 Jun 2007 15:36:36 -0000 1.3
@@ -0,0 +1,40 @@
+package lego;
+
+import joprt.RtThread;
+import lego.lib.*;
+
+public class BackEMFTest
+{
+ /**
+ * @param args
+ */
+ public static void main(String[] args)
+ {
+ new RtThread(10, 100*1000) {
+ public void run()
+ {
+ Motor m0 = new Motor(0);
+ Motor m1 = new Motor(1);
+
+ m0.setMeasure(true);
+ m1.setMeasure(true);
+
+ while (true)
+ {
+ Motor.synchronizedReadBackEMF();
+ int[] sBackEMF0 = m0.getSynchronizedBackEMF();
+ int[] sBackEMF1 = m1.getSynchronizedBackEMF();
+
+ System.out.println(
+ sBackEMF0[0] + ", " + sBackEMF0[1] + "; " + sBackEMF1[0] + ", " + sBackEMF1[1]);
+
+ waitForNextPeriod();
+ }
+ }
+
+ };
+
+ RtThread.startMission();
+ }
+
+}
1.3 jop/java/target/src/app/lego/BeQuiet.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/BeQuiet.java.diff?r1=1.2&r2=1.3
(In the diff below, changes in quantity of whitespace are not shown.)
Index: BeQuiet.java
===================================================================
RCS file: BeQuiet.java
diff -N BeQuiet.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ BeQuiet.java 5 Jun 2007 15:36:36 -0000 1.3
@@ -0,0 +1,14 @@
+package lego;
+
+public class BeQuiet
+{
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args)
+ {
+ // remain quiet
+ }
+
+}
1.3 jop/java/target/src/app/lego/ButtonsLedsTest.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/ButtonsLedsTest.java.diff?r1=1.2&r2=1.3
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ButtonsLedsTest.java
===================================================================
RCS file: ButtonsLedsTest.java
diff -N ButtonsLedsTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ButtonsLedsTest.java 5 Jun 2007 15:36:36 -0000 1.3
@@ -0,0 +1,19 @@
+package lego;
+
+import lego.lib.*;
+
+public class ButtonsLedsTest
+{
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args)
+ {
+ while (true)
+ {
+ Leds.setLeds(Buttons.getButtons());
+ }
+ }
+
+}
1.1 jop/java/target/src/app/lego/CrashAvoider2.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/CrashAvoider2.java?rev=1.1&content-type=text/x-cvsweb-markup
Index: CrashAvoider2.java
===================================================================
package lego;
import com.jopdesign.sys.Const;
import com.jopdesign.sys.Native;
import joprt.RtThread;
import lego.lib.*;
/**
* This program had been written for a specific robot we built for demonstration purposes
*
*/
public class CrashAvoider2
{
// configuration
public static final boolean READ_BUTTON = true;
public static final boolean READ_IR = false;
// implementation
public static final int IR_SENSOR = 2;
public static final int IR_SENSOR_THRESHOLD = 10;
public static Motor[] MOTORS;
public static final int MOTOR_LEFT = 0;
public static final int MOTOR_RIGHT = 1;
public static final int DIFFERENCE_BUFFER_LENGTH = 3;
/**
* @param args
*/
public static void main(String[] args)
{
// int[] difference = new int[DIFFERENCE_BUFFER_LENGTH];
// int differenceIndex = 0;
new RtThread(10, 100*1000)
{
public void run()
{
System.out.println("Ready.");
Leds.setLeds(~0);
MOTORS = new Motor[] { new Motor(0), new Motor(1) };
int freeValue;
while (Buttons.getButtons() == 0)
{
System.out.println(Sensors.readSensor(IR_SENSOR));
}
freeValue = Sensors.readSensor(IR_SENSOR);
boolean stop = true;
System.out.println("Starting...");
Leds.setLeds(0);
while (true)
{
// if (Buttons.getButton(0))
// {
// freeValue = Sensors.readSensor(IR_SENSOR);
// stop = true;
// System.out.println(freeValue);
// }
// if (Buttons.getButton(3))
// {
// if (stop)
// System.out.println("Restarting...");
// stop = false;
// }
stop = !DigitalInputs.getDigitalInput(2);
if (stop)
freeValue = Sensors.readSensor(IR_SENSOR);
for (int i = 0; i < 2; i++)
MOTORS[i].setMotorPercentage(stop ? Motor.STATE_OFF
: Motor.STATE_FORWARD, false, 70);
// difference[differenceIndex] = Sensors.readSensor(IR_SENSOR) - freeValue;
// differenceIndex = (differenceIndex+1) % DIFFERENCE_BUFFER_LENGTH;
// int totalDifference = 0;
// for (int i = 0; i < DIFFERENCE_BUFFER_LENGTH; i++)
// totalDifference += Math.abs(difference[i]);
if (!stop)
{
int totalDifference = Sensors.readSensor(IR_SENSOR) - freeValue;
boolean dirRight = false;
if ((READ_BUTTON && DigitalInputs.getDigitalInput(1))
|| (READ_IR && (Math.abs(totalDifference) >= IR_SENSOR_THRESHOLD)))
{
dirRight = (Native.rd(Const.IO_US_CNT) & 1) != 0;
Leds.setLeds(dirRight ? 0x1 : 0x8);
}
if (READ_BUTTON && DigitalInputs.getDigitalInput(1))
{
for (int i = 0; i < 2; i++)
MOTORS[i].setMotorPercentage(Motor.STATE_BACKWARD,
false, 100);
RtThread.sleepMs(300);
MOTORS[dirRight ? MOTOR_LEFT : MOTOR_RIGHT].setState(Motor.STATE_OFF);
MOTORS[dirRight ? MOTOR_RIGHT : MOTOR_LEFT].setMotorPercentage(Motor.STATE_BACKWARD, false, 100);
RtThread.sleepMs(500);
for (int i = 0; i < 2; i++)
MOTORS[i].setState(Motor.STATE_OFF);
RtThread.sleepMs(2000);
}
else if (READ_IR && (Math.abs(totalDifference) >= IR_SENSOR_THRESHOLD))
{
for (int i = 0; i < 2; i++)
MOTORS[i].setMotorPercentage(Motor.STATE_BACKWARD,
false, 80);
RtThread.sleepMs(500);
MOTORS[dirRight ? MOTOR_LEFT : MOTOR_RIGHT].setMotorPercentage(Motor.STATE_BACKWARD, false, 100);
MOTORS[dirRight ? MOTOR_RIGHT : MOTOR_LEFT].setState(Motor.STATE_OFF);
RtThread.sleepMs(500);
}
continue;
}
waitForNextPeriod();
}
}
};
RtThread.startMission();
}
}
1.1 jop/java/target/src/app/lego/Demo1.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/Demo1.java?rev=1.1&content-type=text/x-cvsweb-markup
Index: Demo1.java
===================================================================
package lego;
import _legonotforrelease.MyRtThread;
import com.jopdesign.sys.Const;
import com.jopdesign.sys.Native;
import joprt.RtThread;
import lego.lib.*;
/**
* This program had been written for a specific robot we built for demonstration purposes
*
*/
public class Demo1
{
// configuration
public static final boolean READ_BUTTON = true;
public static final boolean READ_IR = false;
// implementation
public static final int IR_SENSOR = 2;
public static final int IR_SENSOR_THRESHOLD = 10;
public static Motor[] MOTORS;
public static final int MOTOR_LEFT = 0;
public static final int MOTOR_RIGHT = 1;
public static final int DIFFERENCE_BUFFER_LENGTH = 3;
public static final int STATE_OFF = 0;
public static final int STATE_LINEFOLLOWER = 1;
public static final int STATE_TOUCHSENSOR = 2;
public static final int STATE_IRSENSOR = 3;
public static final int STATE_ALL = 4;
public static int state = STATE_OFF;
// Knight Rider
public static final int LED0 = 1<<1;
public static final int LED1 = 1<<3;
public static final int LED2 = 1<<5;
public static final int LED3 = 1<<7;
static int val, counter, value, counter1, freeValue;
static boolean up, flag, speaker_up, stop;
// Sound1
public static int speed = 0x100;
/**
* @param args
*/
public static void main(String[] args)
{
new MyRtThread(10, 100*1000) {
public void run() {
val = LED0;
up = true;
while (true)
{
if (state == STATE_ALL)
{
if (up){
switch (val) {
case LED0: val = LED1; break;
case LED1: val = LED2; break;
case LED2: val = LED3; break;
case LED3: {
up = false;
val = LED2;
break;
}
default: val = LED0; break;
}
} else {
switch (val) {
case LED0: {
up = true;
val = LED1;
break;
}
case LED1: val = LED0; break;
case LED2: val = LED1; break;
default: val = LED0; break;
}
}
}
else
{
val = 0;
}
FutureUse.writePins(val);
waitForNextPeriod();
}
}
};
new MyRtThread(10, 1*100)
{
public void run() {
up = true;
flag = false;
value = 10;
speed = 0x100;
while (true)
{
if (state == STATE_ALL)
{
counter++;
if ((counter % value) == 0) {
if (flag) {
flag = false;
} else {
flag = true;
}
}
counter1++;
if ((counter1 % speed) == 0) {
if (speaker_up) {
value++;
if (value >= 50) {
speaker_up = false;
value--;
}
} else
{
value--;
if (value <= 10) {
speaker_up = true;
value++;
}
}
}
}
else
{
flag = false;
}
if (flag) {
Speaker.write(true);
} else
Speaker.write(false);
waitForNextPeriod();
}
}
};
new MyRtThread(10, 100*1000)
{
public void run()
{
System.out.println("Main thread ready.");
MOTORS = new Motor[] { new Motor(0), new Motor(1) };
freeValue = 0;
stop = true;
while (true)
{
for (int i = 0; i < 4; i++)
if (Buttons.getButton(i))
{
state = i;
Leds.setLed(1, (state & 1) != 0);
Leds.setLed(2, (state & 2) != 0);
break;
}
stop = !DigitalInputs.getDigitalInput(2);
if ((Buttons.getButtons() != 0) || stop)
freeValue = Sensors.readSensor(IR_SENSOR);
switch (state)
{
case STATE_OFF:
{
for (int i = 0; i < 2; i++)
MOTORS[i].setState(Motor.STATE_OFF);
break;
}
case STATE_LINEFOLLOWER:
{
break;
}
case STATE_TOUCHSENSOR:
case STATE_IRSENSOR:
case STATE_ALL:
{
for (int i = 0; i < 2; i++)
MOTORS[i].setMotorPercentage(stop ? Motor.STATE_OFF
: Motor.STATE_FORWARD, false, 70);
if (!stop)
{
int totalDifference = Sensors.readSensor(IR_SENSOR) - freeValue;
if (((state == STATE_IRSENSOR)||(state == STATE_ALL)) && (Math.abs(totalDifference) >= IR_SENSOR_THRESHOLD))
{
turnback(false);
}
else if ((state == STATE_TOUCHSENSOR)&&(DigitalInputs.getDigitalInput(1)))
{
turnback(true);
}
}
break;
}
}
waitForNextPeriod();
}
}
void turnback(boolean fast) {
int turnspeed, turnsleep;
boolean dirRight = false;
if (fast)
{
turnspeed = 80;
turnsleep = 500;
} else
{
turnspeed = 70;
turnsleep = 1000;
}
speed += 0x50;
dirRight = (Native.rd(Const.IO_US_CNT) & 1) != 0;
Leds.setLed(0, dirRight);
Leds.setLed(3, !dirRight);
for (int i = 0; i < 2; i++)
MOTORS[i].setMotorPercentage(Motor.STATE_BACKWARD,
false, turnspeed);
rtSleep(turnsleep);
MOTORS[dirRight ? MOTOR_LEFT : MOTOR_RIGHT].setMotorPercentage(Motor.STATE_BACKWARD, false, 100);
MOTORS[dirRight ? MOTOR_RIGHT : MOTOR_LEFT].setState(Motor.STATE_OFF);
rtSleep(500);
speed -= 0x50;
Leds.setLed(0, false);
Leds.setLed(3, false);
}
};
RtThread.startMission();
}
}
1.1 jop/java/target/src/app/lego/Demo2.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/Demo2.java?rev=1.1&content-type=text/x-cvsweb-markup
Index: Demo2.java
===================================================================
package lego;
import _legonotforrelease.MyRtThread;
import com.jopdesign.sys.Const;
import com.jopdesign.sys.Native;
import joprt.RtThread;
import lego.lib.*;
import lego.lib.Motor;
/**
* This program had been written for a specific robot we built for demonstration purposes
*
*/
public class Demo2
{
// configuration
public static final boolean READ_BUTTON = true;
public static final boolean READ_IR = false;
// implementation
public static final int IR_SENSOR = 2;
public static final int IR_SENSOR_THRESHOLD = 10;
public static Motor[] MOTORS;
public static final int MOTOR_LEFT = 0;
public static final int MOTOR_RIGHT = 1;
public static final int DIFFERENCE_BUFFER_LENGTH = 3;
public static final int STATE_OFF = 0;
public static final int STATE_LINEFOLLOWER = 1;
public static final int STATE_TOUCHSENSOR = 2;
public static final int STATE_IRSENSOR = 3;
public static final int STATE_ALL = 4;
public static int state = STATE_OFF;
// Knight Rider
public static final int LED0 = 1<<1;
public static final int LED1 = 1<<3;
public static final int LED2 = 1<<5;
public static final int LED3 = 1<<7;
static int val, counter, value, counter1, freeValue;
static boolean up, flag, speaker_up, stop;
// Sound1
public static int speed = 0x100;
/**
* @param args
*/
public static void main(String[] args)
{
new MyRtThread(10, 1*100)
{
public void run() {
up = true;
flag = false;
value = 10;
speed = 0x400;
while (true)
{
if (state == STATE_ALL)
{
counter++;
if ((counter % value) == 0) {
if (flag) {
flag = false;
} else {
flag = true;
}
}
counter1++;
if ((counter1 % speed) == 0) {
if (speaker_up) {
value++;
if (value >= 50) {
speaker_up = false;
value--;
}
} else
{
value--;
if (value <= 10) {
speaker_up = true;
value++;
}
}
}
}
else
{
flag = false;
}
if (flag) {
Speaker.write(true);
} else
Speaker.write(false);
waitForNextPeriod();
}
}
};
new RtThread(10, 100*1000) {
public void run() {
val = LED0;
up = true;
while (true)
{
if (state == STATE_ALL)
{
if (up){
switch (val) {
case LED0: val = LED1; break;
case LED1: val = LED2; break;
case LED2: val = LED3; break;
case LED3: {
up = false;
val = LED2;
break;
}
default: val = LED0; break;
}
} else {
switch (val) {
case LED0: {
up = true;
val = LED1;
break;
}
case LED1: val = LED0; break;
case LED2: val = LED1; break;
default: val = LED0; break;
}
}
}
else if (state != STATE_LINEFOLLOWER)
{
val = 0;
}
FutureUse.writePins(val);
waitForNextPeriod();
}
}
};
new RtThread(10, 50*1000)
{
public void run()
{
int forwardCount = 0;
System.out.println("Main thread ready.");
MOTORS = new Motor[] { new Motor(0), new Motor(1) };
freeValue = 0;
stop = true;
while (true)
{
for (int i = 0; i < 4; i++)
if (Buttons.getButton(i))
{
state = i+1;
//System.out.println(state);
Leds.setLed(1, (i & 1) != 0);
Leds.setLed(2, (i & 2) != 0);
break;
}
stop = !DigitalInputs.getDigitalInput(2);
if ((Buttons.getButtons() != 0) || stop)
freeValue = Sensors.readSensor(IR_SENSOR);
if (stop)
for (int i = 0; i < 2; i++)
MOTORS[i].setState(Motor.STATE_OFF);
// System.out.print(MOTORS[0].readNormalizedBackEMF()[0]);
// System.out.print(" ");
// System.out.print(MOTORS[0].readNormalizedBackEMF()[1]);
// System.out.println();
switch (state)
{
case STATE_OFF:
{
for (int i = 0; i < 2; i++)
MOTORS[i].setState(Motor.STATE_OFF);
break;
}
case STATE_LINEFOLLOWER:
{
if (!stop)
{
int val = Sensors.readSensor(IR_SENSOR);
//System.out.println(val);
boolean black = val > 285; // XXX
//boolean black = val > freeValue - 5;
MOTORS[MOTOR_LEFT].setDutyCyclePercentage(60);
MOTORS[MOTOR_RIGHT].setDutyCyclePercentage(60);
if (black) {
MOTORS[MOTOR_RIGHT].setState(Motor.STATE_FORWARD);
MOTORS[MOTOR_LEFT].setState(Motor.STATE_BRAKE);
FutureUse.writePins(LED0 | LED3);
} else {
MOTORS[MOTOR_LEFT].setState(Motor.STATE_FORWARD);
MOTORS[MOTOR_RIGHT].setState(Motor.STATE_BRAKE);
FutureUse.writePins(LED1 | LED2);
}
}
break;
}
case STATE_TOUCHSENSOR:
case STATE_IRSENSOR:
case STATE_ALL:
{
//System.out.println(stop ? "stop" : "!stop");
if (!stop)
{
for (int i = 0; i < 2; i++)
MOTORS[i].setMotorPercentage(Motor.STATE_FORWARD, true, 40);
int totalDifference = Sensors.readSensor(IR_SENSOR) - freeValue;
if (forwardCount >= 60)
{
if (MOTORS[0].readNormalizedBackEMF()[1] <= 5)
{
Leds.setLeds(0x9);
turnback(false);
}
}
if (((state == STATE_IRSENSOR)||(state == STATE_ALL)) && (Math.abs(totalDifference) >= IR_SENSOR_THRESHOLD))
{
turnback(true);
}
else if ((state == STATE_TOUCHSENSOR || state == STATE_ALL) &&
(forwardCount >= 6) &&
(DigitalInputs.getDigitalInput(1)))
{
turnback(false);
}
}
break;
}
}
{
boolean goingForward = false;
for (int i = 0; i < 2; i++)
{
if (MOTORS[i].getState() == Motor.STATE_FORWARD)
{
goingForward = true;
break;
}
}
forwardCount = goingForward ? forwardCount + 1 : 0;
}
waitForNextPeriod();
}
}
void turnback(boolean fast) {
int turnspeed, turnsleep;
boolean dirLeft = false;
if (fast)
{
turnspeed = 80;
turnsleep = 300;
} else
{
turnspeed = 70;
turnsleep = 400;
}
speed = !fast ? 0x400 : 0x50 ;
dirLeft = (Native.rd(Const.IO_US_CNT) & 1) != 0;
//Leds.setLed(0, dirLeft);
//Leds.setLed(3, !dirLeft);
for (int i = 0; i < 2; i++)
MOTORS[i].setMotorPercentage(Motor.STATE_BACKWARD,
false, 100);
RtThread.sleepMs(100);
for (int i = 0; i < 2; i++)
MOTORS[i].setMotorPercentage(Motor.STATE_BACKWARD,
false, turnspeed);
RtThread.sleepMs(turnsleep-100);
MOTORS[dirLeft ? MOTOR_LEFT : MOTOR_RIGHT].setMotorPercentage(Motor.STATE_BACKWARD, false, 70);
MOTORS[dirLeft ? MOTOR_RIGHT : MOTOR_LEFT].setState(Motor.STATE_OFF);
RtThread.sleepMs(600);
speed = 0x400;
Leds.setLed(0, false);
Leds.setLed(3, false);
}
};
RtThread.startMission();
}
}
1.1 jop/java/target/src/app/lego/FlashProgrammer.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/FlashProgrammer.java?rev=1.1&content-type=text/x-cvsweb-markup
Index: FlashProgrammer.java
===================================================================
package lego;
import java.io.IOException;
import lego.lib.Leds;
import util.Amd;
/**
* Reads in a TrivialFS.
* @author Peter Hilber (peter.hilber@s...)
*
*/
public class FlashProgrammer
{
static final int BLOCK_SIZE = 0x10000;
static final int FLASH_SIZE = BLOCK_SIZE * 8;
/**
* @param args
*/
public static void main(String[] args) throws IOException
{
Leds.setLeds(0x3);
{
for (int i = 0; i < FLASH_SIZE; i += BLOCK_SIZE)
Amd.erase(i);
}
System.out.println("Done erasing, ready for input.");
Leds.setLeds(0x3 << 2);
int index = 0;
int fileIndex = 0;
int length = 0;
while (true)
{
if (fileIndex == 4 + length)
fileIndex = 0;
int ch = System.in.read();
if (fileIndex == 0)
length = ch << 24;
if (fileIndex == 1)
length |= ch << 16;
if (fileIndex == 2)
length |= ch << 8;
if (fileIndex == 3)
{
length |= ch;
}
Amd.program(index, ch);
//System.out.print((char)0x55);
// System.out.print("Programming ");
// System.out.print(index);
// System.out.print(": ");
// System.out.print(bin);
// System.out.print(" ");
// System.out.println(Amd.read(index) == bin ? "succeeded" : "failed");
fileIndex++;
index++;
if (fileIndex == 4)
{
// System.out.print("File length: ");
// System.out.println(length);
if (length == 0)
break;
}
}
Leds.setLeds(~0);
System.out.print("Written: ");
System.out.println(index);
ReadTrivialFS.main(args);
}
}
1.3 jop/java/target/src/app/lego/InputTest.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/InputTest.java.diff?r1=1.2&r2=1.3
(In the diff below, changes in quantity of whitespace are not shown.)
Index: InputTest.java
===================================================================
RCS file: InputTest.java
diff -N InputTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ InputTest.java 5 Jun 2007 15:36:36 -0000 1.3
@@ -0,0 +1,18 @@
+package lego;
+
+import java.io.IOException;
+
+import lego.lib.Leds;
+
+public class InputTest
+{
+ public static void main(String[] args) throws IOException
+ {
+ System.out.println("Waiting for input to echo...");
+ while (true)
+ {
+ System.out.print((char)System.in.read());
+ Leds.setLeds(~Leds.getLeds());
+ }
+ }
+}
1.1 jop/java/target/src/app/lego/InputTestBinary.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/InputTestBinary.java?rev=1.1&content-type=text/x-cvsweb-markup
Index: InputTestBinary.java
===================================================================
package lego;
import java.io.IOException;
public class InputTestBinary
{
public static void main(String[] args) throws IOException
{
System.out.println("Waiting for input to echo...");
while (true)
{
System.out.print(System.in.read());
}
}
}
1.3 jop/java/target/src/app/lego/IsAliveTest.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/IsAliveTest.java.diff?r1=1.2&r2=1.3
(In the diff below, changes in quantity of whitespace are not shown.)
Index: IsAliveTest.java
===================================================================
RCS file: IsAliveTest.java
diff -N IsAliveTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ IsAliveTest.java 5 Jun 2007 15:36:36 -0000 1.3
@@ -0,0 +1,20 @@
+package lego;
+
+import lego.lib.*;
+
+public class IsAliveTest
+{
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args)
+ {
+ while (true)
+ {
+ for (int i = 0; i < 4; i++)
+ Leds.blinkUpdate(i);
+ }
+ }
+
+}
1.1 jop/java/target/src/app/lego/KnightRider.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/KnightRider.java?rev=1.1&content-type=text/x-cvsweb-markup
Index: KnightRider.java
===================================================================
package lego;
import joprt.RtThread;
import lego.lib.FutureUse;
public class KnightRider {
public static final int LED0 = 1<<1;
public static final int LED1 = 1<<7;
public static final int LED2 = 1<<5;
public static final int LED3 = 1<<4;
static int val;
static boolean up;
public static void init() {
val = LED0;
up = true;
}
public static void loop() {
FutureUse.writePins(val);
//Native.wr(val, IO_LEDS);
RtThread.sleepMs(100);
if (up){
switch (val) {
case LED0: val = LED1; break;
case LED1: val = LED2; break;
case LED2: val = LED3; break;
case LED3: {
up = false;
val = LED2;
break;
}
default: val = LED0; break;
}
} else {
switch (val) {
case LED0: {
up = true;
val = LED1;
break;
}
case LED1: val = LED0; break;
case LED2: val = LED1; break;
default: val = LED0; break;
}
}
}
public static void main(String[] agrgs) {
System.out.println("Hello LEGO world!");
init();
new RtThread(10, 100*1000) {
public void run() {
for (;;) {
loop();
waitForNextPeriod();
}
}
};
RtThread.startMission();
for (;;) {
loop();
}
}
}
1.1 jop/java/target/src/app/lego/LegoBoardTest.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/LegoBoardTest.java?rev=1.1&content-type=text/x-cvsweb-markup
Index: LegoBoardTest.java
===================================================================
package lego;
import com.jopdesign.sys.*;
import joprt.RtThread;
import lego.lib.Buttons;
import lego.lib.DigitalInputs;
import lego.lib.FutureUse;
import lego.lib.Leds;
import lego.lib.Microphone;
import lego.lib.Motor;
import lego.lib.Sensors;
import lego.lib.Speaker;
/**
*
* @author Peter Hilber (peter.hilber@s...) and Alexander Dejaco (alexander.dejaco@s...)
*
*/
public class LegoBoardTest
{
// configuration
static final boolean REPEAT = true;
static final int INTERVAL = 1000;
static final boolean SPEAKER_DEMO = true;
static final boolean BUTTONS = true;
static final boolean DIGITALINPUTS = true;
static final boolean FUTUREUSE = true;
static final boolean LEDS = true;
static final boolean MICROPHONE = true;
static final boolean MOTORS = false;
static final boolean SENSORS = true;
static final boolean PLD_RAW_INPUT = true;
static final boolean KNIGHT_RIDER_DEMO = true;
static final boolean FREEMEMORY = true;
public static final int LED0 = 1<<1;
public static final int LED1 = 1<<3;
public static final int LED2 = 1<<5;
public static final int LED3 = 1<<7;
static int val, counter, value, counter1;
static boolean up, flag, speaker_up;
public static void knightRiderLoop() {
FutureUse.writePins(val);
//Native.wr(val, IO_LEDS);
if (up){
switch (val) {
case LED0: val = LED1; break;
case LED1: val = LED2; break;
case LED2: val = LED3; break;
case LED3: {
up = false;
val = LED2;
break;
}
default: val = LED0; break;
}
} else {
switch (val) {
case LED0: {
up = true;
val = LED1;
break;
}
case LED1: val = LED0; break;
case LED2: val = LED1; break;
default: val = LED0; break;
}
}
}
public static void speakerLoop() {
counter++;
if ((counter % value) == 0) {
if (flag) {
flag = false;
} else {
flag = true;
}
}
counter1++;
if ((counter1 % 0x200) == 0) {
if (speaker_up) {
value++;
if (value >= 50) {
speaker_up = false;
value--;
}
} else
{
value--;
if (value <= 10) {
speaker_up = true;
value++;
}
}
}
if (flag) {
Speaker.write(true);
} else
Speaker.write(false);
}
/**
* @param args
*/
public static void main(String[] args)
{
System.out.println("Initializing.");
/* Motor.setMotor(0, Motor.STATE_FORWARD, true, Motor.MAX_DUTYCYCLE);
Motor.setMotor(1, Motor.STATE_FORWARD, true, Motor.MAX_DUTYCYCLE);
Motor.setMotor(2, Motor.STATE_FORWARD, true, Motor.MAX_DUTYCYCLE);
//Native.wr(-1 << 1, Motor.IO_OUTPUT_MOTOR[1]);
*/
if (KNIGHT_RIDER_DEMO)
{
val = LED0;
up = true;
new RtThread(10, 100*1000) {
public void run() {
for (;;) {
knightRiderLoop();
waitForNextPeriod();
}
}
};
}
if (SPEAKER_DEMO) {
up = true;
flag = false;
value = 10;
new RtThread(10, 1*100) {
public void run() {
for (;;) {
speakerLoop();
waitForNextPeriod();
}
}
};
}
new RtThread(10, 1000*1000)
{
public void run()
{
StringBuffer output = new StringBuffer(500);
do
{
output.setLength(0);
output.append("New measurement...\n\n");
if (FREEMEMORY)
{
output.append("Free memory: ").append(GC.freeMemory()).append("\n");
}
if (BUTTONS)
{
for (int i = 0; i < 4; i++)
{
// uncomment this to have fun with javac
//System.out.println("Button " + i + ": " + Buttons.getButton(i));
// uncomment this to have fun with JOP
//System.out.println("Button " + i + ": " + new Boolean(Buttons.getButton(i)));
output.append("Button ").append(i).append(": ").append(Buttons.getButton(i) ? "Down" : "Up").append("\n");
}
}
if (DIGITALINPUTS)
{
for (int i = 0; i < 3; i++)
output.append("Digital input ").append(i).append(": ").append(DigitalInputs.getDigitalInput(i) ? "1" : "0").append("\n");
}
if (FUTUREUSE)
{
output.append("Unknown input: 0x").append(
Integer.toHexString((FutureUse.readPins()))).append("\n");
}
if (LEDS)
{
Leds.setLeds(-1);
}
if (MICROPHONE)
{
output.append("Microphone: ").append(Microphone.readMicrophone()).append("\n");
}
if (MOTORS)
{
Motor.synchronizedReadBackEMF();
for (int i = 0; i < 2; i++)
{
int[] backEMF = new Motor(i).getSynchronizedBackEMF();
output.append(
"Motor ").append(i).append(" back-emf measurement: ").append(backEMF[0] - 0x100).append(", ").append(backEMF[1] - 0x100).append("\n");
}
}
if (SENSORS)
{
Sensors.synchronizedReadSensors();
for (int i = 0; i < 3; i++)
output.append("Analog sensor ").append(i).append(": ").append(Sensors.getBufferedSensor(i)).append(
" (").append(Sensors.readSensorValueAsPercentage(i)).append(("%)")).append("\n");
}
if (PLD_RAW_INPUT)
{
output.append("PLD raw input: ").append(Native.rd(Const.IO_LEGO + 7)).append("\n");
}
output.append("\nMeasurement finished.\n\n");
//output.append("Length: ").append(output.length()).append(" Capacity: " ).append(output.capacity()).append("\n");
System.out.print(output);
if (!REPEAT)
break;
waitForNextPeriod();
} while (true);
}
};
RtThread.startMission();
System.out.println("Started.");
}
}
1.4 jop/java/target/src/app/lego/LineFollower.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/LineFollower.java.diff?r1=1.3&r2=1.4
(In the diff below, changes in quantity of whitespace are not shown.)
Index: LineFollower.java
===================================================================
RCS file: LineFollower.java
diff -N LineFollower.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ LineFollower.java 5 Jun 2007 15:36:36 -0000 1.4
@@ -0,0 +1,71 @@
+package lego;
+
+/**
+ * Originally ran on prototype board. Ported to LRBJOP interface. Untested.
+ */
+
+import lego.lib.*;
+
+import joprt.RtThread;
+
+public class LineFollower {
+
+ static final int IR_SENSOR = 2;
+
+ static final int MAX = 1000;
+ static Motor left, right;
+ static boolean black;
+
+ public static void init() {
+
+ left = new Motor(0);
+ right = new Motor(1);
+
+ black = false;
+ }
+
+ public static void loop() {
+
+ int val = Sensors.readSensor(IR_SENSOR);
+ System.out.println(val);
+ black = val > 285; // XXX
+
+ left.setDutyCyclePercentage(75);
+ right.setDutyCyclePercentage(75);
+
+ if (black) {
+ right.setState(Motor.STATE_FORWARD);
+ left.setState(Motor.STATE_BRAKE);
+ } else {
+ left.setState(Motor.STATE_FORWARD);
+ right.setState(Motor.STATE_BRAKE);
+ }
+ }
+
+ public static void main(String[] agrgs) {
+
+
+ System.out.println("Hello LEGO world!");
+
+ init();
+
+ new RtThread(10, 20*1000) {
+ public void run() {
+ for (;;) {
+ loop();
+ waitForNextPeriod();
+ }
+ }
+ };
+
+ RtThread.startMission();
+
+ for (;;) {
+ int val = Sensors.readSensor(IR_SENSOR);
+ System.out.println(val);
+ RtThread.sleepMs(500);
+ }
+
+ }
+
+}
1.1 jop/java/target/src/app/lego/ReadTrivialFS.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/ReadTrivialFS.java?rev=1.1&content-type=text/x-cvsweb-markup
Index: ReadTrivialFS.java
===================================================================
package lego;
import lego.utils.TrivialFS;
public class ReadTrivialFS
{
static final int BLOCK_SIZE = 0x10000;
static final int FLASH_SIZE = BLOCK_SIZE * 8;
/**
* @param args
*/
public static void main(String[] args)
{
int count = TrivialFS.getFileCount();
System.out.print("File count: ");
System.out.println(count);
for (int i = 0; i < count; i++)
{
System.out.print("File address: ");
System.out.print(TrivialFS.getFileAddress(i));
System.out.print(", file size: ");
System.out.println(TrivialFS.getFileSize(i));
}
}
}
1.3 jop/java/target/src/app/lego/sound1.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/sound1.java.diff?r1=1.2&r2=1.3
(In the diff below, changes in quantity of whitespace are not shown.)
Index: sound1.java
===================================================================
RCS file: sound1.java
diff -N sound1.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sound1.java 5 Jun 2007 15:36:36 -0000 1.3
@@ -0,0 +1,91 @@
+package lego;
+
+import lego.lib.*;
+
+//import util.Timer;
+//import com.jopdesign.sys.Const;
+//import com.jopdesign.sys.Native;
+/*
+ * Multiple tones are output, the frequency is increased and decreased chronologically
+ * The rate of change can be set with Buttons 0 and 1
+ */
+
+public class sound1 {
+
+ static boolean flag, up;
+ static final int MAX = 50;
+ static final int MIN = 10;
+ static int value, counter, counter1, SPEED;
+
+ public static void init() {
+ up = true;
+ flag = false;
+ value = 10;
+ SPEED = 0x100;
+ }
+
+
+ public static void main(String[] agrgs) {
+ System.out.println("Initializing...");
+
+
+
+ init();
+
+
+ while (true) {
+ while (DigitalInputs.getDigitalInput(2)) {
+
+ if (Buttons.getButton(0)) {
+ while (Buttons.getButton(0) == true) ;
+ SPEED = SPEED + 0x50;
+ }
+ if (Buttons.getButton(1)) {
+ while (Buttons.getButton(1) == true) ;
+ if (SPEED > 0x50) {
+ SPEED = SPEED - 0x50;
+ }
+ }
+
+ counter++;
+
+ if ((counter % value) == 0) {
+ if (flag) {
+ flag = false;
+ } else {
+ flag = true;
+ }
+ }
+
+ counter1++;
+
+ if ((counter1 % SPEED) == 0) {
+ if (up) {
+ value++;
+ if (value >= MAX) {
+ up = false;
+ value--;
+ }
+ } else
+ {
+ value--;
+ if (value <= MIN) {
+ up = true;
+ value++;
+ }
+ }
+
+ }
+
+ if (flag) {
+ Speaker.write(true);
+ } else
+ Speaker.write(false);
+
+ }
+ }
+ }
+
+
+
+}
1.3 jop/java/target/src/app/lego/sound2.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/sound2.java.diff?r1=1.2&r2=1.3
(In the diff below, changes in quantity of whitespace are not shown.)
Index: sound2.java
===================================================================
RCS file: sound2.java
diff -N sound2.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sound2.java 5 Jun 2007 15:36:36 -0000 1.3
@@ -0,0 +1,74 @@
+package lego;
+
+import joprt.RtThread;
+import lego.lib.*;
+
+//import util.Timer;
+//import com.jopdesign.sys.Const;
+//import com.jopdesign.sys.Native;
+
+/*
+ * A single tone is output, the frequency can be chanced with buttons 0 and 1
+ */
+
+public class sound2 {
+
+ static boolean flag;
+
+ static int value, counter;
+
+ public static void init() {
+ flag = false;
+ value = 1;
+ }
+
+
+ public static void main(String[] agrgs) {
+ System.out.println("Initializing...");
+
+
+
+ init();
+
+
+ new RtThread(10, 10 * 1000) {
+ public void run() {
+ while (true) {
+ while (DigitalInputs.getDigitalInput(2)) {
+
+
+ if (Buttons.getButton(0)) {
+ while (Buttons.getButton(0) == true) ;
+ value++;
+ }
+ if (Buttons.getButton(1)) {
+ while (Buttons.getButton(1) == true) ;
+ value--;
+ }
+
+
+
+ if (flag) {
+ Speaker.write(true);
+ flag = false;
+ } else {
+ flag = true;
+ Speaker.write(false);
+ }
+
+ RtThread.sleepMs(value);
+
+
+ }
+ }
+ }
+ };
+
+
+ RtThread.startMission();
+
+ }
+
+
+
+}
1.1 jop/java/target/src/app/lego/sound4.java
http://www.opencores.org/cvsweb.shtml/jop/java/target/src/app/lego/sound4.java?rev=1.1&content-type=text/x-cvsweb-markup
Index: sound4.java
===================================================================
package lego;
import joprt.RtThread;
import lego.lib.*;
//import util.Timer;
//import com.jopdesign.sys.Const;
//import com.jopdesign.sys.Native;
/*
* A simple version of the tetris soundtrack is played.
* Some notes use only an approximate frequency.
* Better programming
*/
public class sound4 {
static boolean flag, on, done;
static int value, counter;
// notes
static final int E = 15;
static final int F = 14;
static final int H = 20;
static final int G = 12;
static final int C = 19;
static final int D = 17;
static final int A = 22;
static final int A1 = 11;
// flags
static final int ON = 90;
static final int OFF = 91;
static final int DONE = 92;
// pauses
static final int PH = 400; // half
static final int PQ = 200; // quarter
static final int PT = 600; // three quarter
static final int PB = 10; // very short pause
static final int PL = 800; // long pause
static final int TUNE_LENGTH = 39; // (38 notes+pauses) + 1 (done flag)
static final int[] NOTES = {E, H, C, D, C, H, A, C, E, D, C, H, C, D, E, C, A, OFF, ON, ON, D, F, A1, G, F, E, C, E, D, C, H, C, D, E, C, A, OFF, ON, DONE};
static final int[] PAUSES = {PH ,PQ ,PQ ,PH ,PQ ,PQ ,PL ,PQ ,PH ,PQ ,PQ ,PT ,PQ ,PH ,PH ,PH ,PH ,PB ,PL ,PQ ,PH ,PQ ,PH ,PQ ,PQ ,PT ,PQ ,PH ,PQ ,PQ ,PT ,PQ ,PH ,PH ,PH ,PH ,PB ,PL};
public static void init() {
on = true;
flag = false;
value = 10;
done = false;
}
public static void tetris () {
on = true;
RtThread.sleepMs(4);
on = false;
RtThread.sleepMs(796);
on = true;
RtThread.sleepMs(4);
on= false;
RtThread.sleepMs(796);
on = true;
RtThread.sleepMs(4);
on= false;
RtThread.sleepMs(796);
on = true;
RtThread.sleepMs(4);
on = false;
RtThread.sleepMs(796);
on = true;
for (int i=0;i<TUNE_LENGTH-1;i++)
{
if (NOTES[i] == ON)
{
on = true;
} else
if (NOTES[i] == OFF)
{
on = false;
} else
if (NOTES[i] == DONE)
{
break;
} else
{
value = NOTES[i];
}
RtThread.sleepMs(PAUSES[i]);
}
}
public static void loop() {
if (on) {
counter++;
if ((counter % value) == 0) {
if (flag) {
flag = false;
} else {
flag = true;
}
}
if (flag) {
Speaker.write(true);
} else
Speaker.write(false);
}
}
public static void main(String[] agrgs) {
System.out.println("Initializing...");
init();
new RtThread(10, 100) {
public void run() {
for (;;) {
loop();
waitForNextPeriod();
}
}
};
RtThread.startMission();
while(true) {
on = true;
tetris();
while (Buttons.getButtons() == 0);
while (Buttons.getButtons() != 0);
}
}
}
|
 |