Compass Reader Advanced by 祥瑞

Compass Reader Basic 的加強版, 可以按ENTER鍵記錄角度,左鍵切換方位角/方向角,右鍵清除已記錄的資料, EXIT鍵結束程式。

import lejos.nxt.*;
import lejos.nxt.addon.CompassSensor;
import lejos.util.Delay;
import javax.microedition.lcdui.Graphics;

class CompassAdvance implements ButtonListener {
    public CompassSensor compass = new CompassSensor(SensorPort.S1);
    public int lineLength = 50; //線長度
    public int arrowLength = 15; //箭頭線長度
    public int arrowAng = 30; //箭頭線與線的角度
    public int dots = 40; //圓周點的數目
    public int delay = 150; //更新螢幕間隔
   
    public int offsetX;
    public int offsetY;
    public Graphics g = new Graphics();
    public double direction;
    public boolean type = true;
    public double[] logs = new double[8];
   
    public CompassAdvance() {
        Button.ENTER.addButtonListener(this);
        Button.LEFT.addButtonListener(this);
        Button.RIGHT.addButtonListener(this);
        Button.ESCAPE.addButtonListener(this);
        for(int i=0; i<logs.length; i++) logs[i] = -1;
    }
   
    public void work() {
        g.fillRect(35, 0, 30, 30);
        g.fillRect(35, 40, 30, 12);
        for(int i=0; i<=30; i++) {
            g.drawLine((int)(Math.sqrt(3)*i/2), 15-i/2, (int)(Math.sqrt(3)*i/2), 15+i/2);
            g.drawLine((int)(100-15*Math.sqrt(3)+Math.sqrt(3)*i/2), 15-(30-i)/2, (int)(100-15*Math.sqrt(3)+Math.sqrt(3)*i/2), 15+(30-i)/2);
        }
        g.drawString("LOG", 50-LCD.FONT_WIDTH*3/2, 32);
        g.drawString("EXIT", 50-LCD.FONT_WIDTH*4/2, 54);
        g.drawString("TYPE", 0, 32);
        g.drawString("CLEAR", 100-LCD.FONT_WIDTH*5, 32);
        Button.ENTER.waitForPress();
        LCD.clear();
        while(true) {
            direction = compass.getDegreesCartesian();
            g.drawLine(getLocationX(0, lineLength), getLocationY(0, lineLength), 100-getLocationX(0, lineLength), 64-getLocationY(0, lineLength));
            g.drawLine(getLocationX(0, lineLength), getLocationY(0, lineLength), getLocationX(0, lineLength) + (int)(Math.cos(Math.toRadians(90-arrowAng))*arrowLength), getLocationY(0, lineLength) + (int)(Math.sin(Math.toRadians(90-arrowAng))*arrowLength));
            g.drawLine(getLocationX(0, lineLength), getLocationY(0, lineLength), getLocationX(0, lineLength) – (int)(Math.cos(Math.toRadians(90-arrowAng))*arrowLength), getLocationY(0, lineLength) + (int)(Math.sin(Math.toRadians(90-arrowAng))*arrowLength));
            g.drawString("N", getLocationX(direction, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction, lineLength+10)-LCD.FONT_HEIGHT/2);
            g.drawString("W", getLocationX(direction+270, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction+270, lineLength+10)-LCD.FONT_HEIGHT/2);
            g.drawString("E", getLocationX(direction+90, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction+90, lineLength+10)-LCD.FONT_HEIGHT/2);
            g.drawString("S", getLocationX(direction+180, lineLength+10)-LCD.FONT_WIDTH/2 , getLocationY(direction+180, lineLength+10)-LCD.FONT_HEIGHT/2);
            for(int i=0; i<360; i+=360/dots) g.drawLine(getLocationX(direction+i, lineLength), getLocationY(direction+i, lineLength), getLocationX(direction+i, lineLength-5), getLocationY(direction+i, lineLength-5));
            for(int i=0; i<8; i++) LCD.drawString(getDirectionString(logs[i]), 0, i);
            LCD.drawString(getDirectionString((360-direction)%360), 16-getDirectionString((360-direction)%360).length(), 0);
            Delay.msDelay(delay);
            LCD.clear();
        }
    }
   
    public void buttonReleased(Button b){
        int ID = b.getId();
        if(ID==b.ID_ENTER) {
            for(int i=logs.length-2; i>=0; i–) logs[i+1] = logs[i];
            logs[0] = compass.getDegrees();
        }
        else if(ID==b.ID_LEFT) type = !type;
        else if(ID==b.ID_RIGHT) {
            for(int i=0; i<logs.length; i++) logs[i] = -1;
        }
        else {System.exit(0);}
    }
   
    public void buttonPressed(Button b) {}
   
    public String getDirectionString(double direction) {
        if(direction==-1) return "";
        else if(type) {
            if(direction==0) return "N";
            else if(direction==90) return "E";
            else if(direction==180) return "S";
            else if(direction==270) return "W";
            else return ((direction<90 || direction>270)?"N":"S") + (int)((direction<90 || (direction>180 && direction<270))?(direction%90):(90-direction%90)) + ((direction<180)?"E":"W");
        }else return String.valueOf((int)direction);
    }
   
    public int getLocationX(double ang, int length) {
        return 50 + (int)(Math.sin(Math.toRadians(ang))*length/2);
    }
   
    public int getLocationY(double ang, int length) {
        return 32 – (int)(Math.cos(Math.toRadians(ang))*length/2);
    }
 &nbsp

    public static void main(String args[]) {
        CompassAdvance compassAdc = new CompassAdvance();
        compassAdc.work();
    }
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *