/*センサーテスト
[Run]button -- start, change port: 1 > 2 > 3 >
Default sensor: Port 1:Touch(t)0,1/count, 2:Light(l)0-100, 3:Raw(r)0-1023
Read number every TimeCycle:500 milliseconds
Stop after TimeOut:60 seconds
[View]button -- change touch sensor mode: Bool(b)0,1 > Pulse(p)count > Edge(e)count >
[Prgm]button -- change sensor type on port: Touch > Light > Raw > Blanc(Passive Raw) >
*/
import java.lang.*;
import josx.platform.rcx.*;
class SensorTest
implements SensorConstants, Segment
{
public final static int TimeCycle = 500; //millisecond
public final static int TimeOut = 60; //seconds
public final static int TimeOutCycle = 1000*TimeOut/TimeCycle;
public final static int Touch = 0; //type
public final static int Light = 1;
public final static int Raw = 2;
public final static int Blanc = 3; //Passive Raw
public final static int Bool = 0; //mode
public final static int[] aTYPE = {SENSOR_TYPE_TOUCH,SENSOR_TYPE_LIGHT,
SENSOR_TYPE_RAW,SENSOR_TYPE_RAW};
public final static int[] aTypeMODE = {SENSOR_MODE_BOOL,SENSOR_MODE_PCT,
SENSOR_MODE_RAW,SENSOR_MODE_RAW};
public final static int[] aTouchMODE = {SENSOR_MODE_BOOL,
SENSOR_MODE_PULSE,SENSOR_MODE_EDGE};
public final static char[] charType = {'t','l','r',' '}; //Touch,Light,Raw,Blanc
public final static char[] charMode = {'b','p','e'}; //Bool,Pulse,Edge
static int[] portType = {Touch,Light,Raw};
static int[] portTouchMode = {Bool,Bool,Bool};
static int port = 0; //Port Label 1
static int type = 0; //Touch
static int mode = 0; //Bool
static char charNum(int num)
{
return (char)('0'+num%10); //'0'-'9'
}
public static void main (String[] aArg)
throws Exception
{
for(int p=0; p<3; p++)
Sensor.SENSORS[p].setTypeAndMode(aTYPE[portType[p]],aTypeMODE[portType[p]]);
Sensor.SENSORS[port].activate();
Sound.playTone(1000,2);
Button.RUN.addButtonListener(new ButtonListener() {
public void buttonPressed(Button button) {
Sensor.SENSORS[port].passivate();
port = (port+1)%3;
type = portType[port];
if (type==Touch) mode = portTouchMode[port];
if (type!=Blanc) Sensor.SENSORS[port].activate();
Sound.playTone(1000,2);
}
public void buttonReleased(Button button) {
}
}
);
Button.PRGM.addButtonListener(new ButtonListener() {
public void buttonPressed(Button button) {
type = (type+1)%4;
portType[port] = type;
Sensor.SENSORS[port].setTypeAndMode(aTYPE[type],aTypeMODE[type]);
if (type==Touch) {
mode = Bool;
portTouchMode[port] = mode;
}
if (type==Blanc) Sensor.SENSORS[port].passivate();
else Sensor.SENSORS[port].activate();
Sound.playTone(700,2);
}
public void buttonReleased(Button button) {
}
}
);
Button.VIEW.addButtonListener(new ButtonListener() {
public void buttonPressed(Button button) {
if (type==Touch) {
mode = (mode+1)%3;
portTouchMode[port] = mode;
Sensor.SENSORS[port].setTypeAndMode(aTYPE[type],aTouchMODE[mode]);
Sound.playTone(500,2);
}
}
public void buttonReleased(Button button) {
}
}
);
for(int i=0; i<TimeOutCycle; i++) {
LCD.clear();
LCD.setSegment(SENSOR_1_VIEW+port*2);
LCD.setSegment(SENSOR_1_ACTIVE+port*2);
TextLCD.printChar(charType[type],0); //t,l,r,' '
int val = Sensor.SENSORS[port].readValue();
if (type==Touch) { //0-99
TextLCD.printChar(charMode[mode],1); //b,p,e
TextLCD.printChar(charNum(val),3);
if (10<=val) TextLCD.printChar(charNum(val/10),4);
}
else if (type==Light) { //00-100
TextLCD.printChar(charNum(val),2);
TextLCD.printChar(charNum(val/10),3);
if (100<=val) TextLCD.printChar(charNum(val/100),4);
}
else { //Raw,Blanc:0000-1023
LCD.showNumber(val);
}
LCD.setSegment(STANDING+i%2); //or WALKING
LCD.refresh();
try {
Thread.sleep(TimeCycle);
} catch(InterruptedException e) {
}
}
Sensor.SENSORS[port].passivate();
LCD.clear();
LCD.refresh();
Sound.beepSequence();
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
}
}
}