Package Nucleo

Source Code of Nucleo.Nucleo

/*
* File:        Nucleo.java
*
*
*     This file is a part of MICE, a program designed for
*              people with severe motor disabilities to whom it is impossible
*              to use a traditional mouse. This application gives these people
*              the control of the physical mouse via another type of device.
*
* Authors:     Isabel Gonzalez
* Date:        2008/ 2009
*
* Company:     Colegio Publico de Educacion Especial Alborada, Zaragoza
*              DIIS, Universidad de Zaragoza
*
* License:     Copyright (C) 2008
*
*              This program is free software: you can redistribute it and/or modify
*              it under the terms of the GNU General Public License as published by
*              the Free Software Foundation, either version 3 of the License, or
*              (at your option) any later version.
*
*              This program is distributed in the hope that it will be useful,
*              but WITHOUT ANY WARRANTY; without even the implied warranty of
*              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*              GNU General Public License for more details.
*
*              You should have received a copy of the GNU General Public License
*              along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*
*
*
*/


package Nucleo;

import java.awt.AWTException;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.io.*;
import java.util.*;

import java.nio.channels.SocketChannel;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.IntBuffer;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.CharBuffer;
import java.nio.charset.*;
import java.lang.StringBuffer;


public class Nucleo {
   
   
    private int puerto_nucleo = 17224;
    private Movimiento m;
    private long velocidad_normal=50;
    private long velocidad_lenta=500;
    private SocketChannel sc;
   
    private int accion_anterior=0; // codigo anterior
    private int parametro_anterior=0; // parametro anterior
   
    private int movimiento_anterior=0; // ultimo movimiento realizado, para frenar
   
    long valor;
   
    private enum estado {
        PARADO, MOVIMENDO1, MOVIMENDO2
    }
    private estado estado_actual;
   
    // Parametros a enviar al nucleo
     public static final int  MOVIMIENTO = 0;
     public static final int  PARADA     = 1;
     public static final int  CLIC       = 2;
     public static final int  ARRASTRAR  = 3;
     public static final int  VELOCIDAD  = 4;
     public static final int  TIPO       = 5;
     public static final int  RELACION   = 6;
     public static final int  FIN        = 7;
     public static final int  NOMBRE        = 8;
     public static final int  MENSAJERATON   = 9;
     public static final int  FRENAR   = 10;
    
    
     // Estos son los dos parametros que recibimos
     public static int tipoCod;
     public static int parametro;
   
   
   
    public Nucleo() {
        m= new Movimiento();
        estado_actual= estado.PARADO;
    }
   
    public void conectarAPasarela(){
        try{
            sc = SocketChannel.open();
            System.out.println("making conection ...");
            sc.socket().connect(new InetSocketAddress("localhost",puerto_nucleo));
            System.out.println("connection established");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
   
   
    public static String decode( ByteBuffer byteBuffer ) throws CharacterCodingException {
        Charset charset = Charset.forName( "us-ascii" );
        CharsetDecoder decoder = charset.newDecoder();
        CharBuffer charBuffer = decoder.decode( byteBuffer );
        String result = charBuffer.toString();
        return result;
    }
   
    public void elegirMovimiento (long velocidad){
        if ((this.tipoCod== this.MOVIMIENTO)|| ((this.tipoCod== this.FRENAR) && (estado_actual== estado.MOVIMENDO2))){
            if (this.parametro==0)          m.haciaArriba(velocidad);
            else if (this.parametro==1)     m.haciaAbajo(velocidad);
            else if (this.parametro==2)     m.haciaDerecha(velocidad);
            else if (this.parametro==3)     m.haciaIzquierda(velocidad);
            else if (this.parametro==4)     m.haciaArribaIzquierda(velocidad);
            else if (this.parametro==5)     m.haciaArribaDerecha(velocidad);
            else if (this.parametro==6)     m.haciaAbajoIzquierda(velocidad);
            else if (this.parametro==7)     m.haciaAbajoDerecha(velocidad);
        }
    }
   
    public void elegirAccionClick (){
        if (this.parametro==9){
            enviar(this.MENSAJERATON,00);
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            m.clickIzquierdo();
            enviar(this.MENSAJERATON,10);
        }
        else if (this.parametro==10){
            enviar(this.MENSAJERATON,02);
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            m.clickDerecho();
            enviar(this.MENSAJERATON,12);
        }
        else if (this.parametro==11){
            enviar(this.MENSAJERATON,01);
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            m.clickMedio();
            enviar(this.MENSAJERATON,11);
        }
        else if (this.parametro==12){
            enviar(this.MENSAJERATON,00);
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            m.dobleClickIzquierdo();
            enviar(this.MENSAJERATON,10);
        }
    }
 
    public void AnalizarEstadoPARADO (){
        if (this.tipoCod==ARRASTRAR){
            if(this.parametro==13){
                m.arrastrar();
            }
            else if(this.parametro==14){
                m.finArrastrar();
            }
        }
        else if ((this.tipoCod==this.CLIC) && (! m.arrastrando)){
            elegirAccionClick();
        }
        else if ((this.tipoCod== this.PARADA) &&(this.parametro==8)){
            m.finalizarMovimiento();
        }
        else if (this.tipoCod== this.MOVIMIENTO){
            estado_actual= estado.MOVIMENDO1;
            elegirMovimiento(velocidad_normal);
        }
       
    }
       
   
    public void AnalizarEstadoMOVIENDO1 (){
        if ((this.tipoCod==this.ARRASTRAR) && (parametro==13)){
            m.arrastrar();
        }
        else if ((this.tipoCod==this.ARRASTRAR) && (parametro==14)){
            m.finArrastrar();
        }
        else if ((this.tipoCod==this.CLIC) && (! m.arrastrando)){
            elegirAccionClick();
        }
        else if ((this.tipoCod==this.PARADA) && (parametro==8)){
            estado_actual=estado.PARADO;
            m.finalizarMovimiento();
        }
       else if ((this.tipoCod==this.MOVIMIENTO) && (!m.dosTiempos)){
            estado_actual= estado.PARADO;
            m.finalizarMovimiento();
           
        }
        else if ((this.tipoCod==this.MOVIMIENTO) && m.dosTiempos){
            m.finalizarMovimiento();
            estado_actual= estado.MOVIMENDO2;
            elegirMovimiento(velocidad_lenta);
        }
        else if ((this.tipoCod==this.FRENAR) && m.dosTiempos){
            m.finalizarMovimiento();
            estado_actual= estado.MOVIMENDO2;
            this.parametro= this.movimiento_anterior;
            elegirMovimiento(velocidad_lenta);
        }
       
    }
   
    public void AnalizarEstadoMOVIENDO2 (){
        if (this.tipoCod==this.MOVIMIENTO){
            m.finalizarMovimiento();
            estado_actual=estado.PARADO;
        }
        else if ((this.tipoCod==this.ARRASTRAR) &&(parametro== 13)) m.arrastrar();
        else if ((this.tipoCod==this.ARRASTRAR) &&(parametro== 14)) m.finArrastrar();
        else if ((this.tipoCod==this.CLIC) && (! m.arrastrando)){
            elegirAccionClick();
        }
        else if ((this.tipoCod==this.PARADA) && (parametro==8)){
            estado_actual=estado.PARADO;
            m.finalizarMovimiento();
        }
       
    }
   
   
    public void AnalizarConfiguracion(){
        String aux;
       
        //configuracion de tipo (simple o dos tiempos)
        if (this.tipoCod==this.TIPO){
            if (this.parametro==0)
            {
                // simple
                m.dosTiempos=false;
            }
            else if (this.parametro==1){
                m.dosTiempos=true;
            }
        }
        //configuracion del valor de la velocidad
        else if (this.tipoCod== this.VELOCIDAD)
        {
            velocidad_normal=this.parametro;
        }
       
        //configuracion de relacion velocidad (25, 50, 75)
        else if (this.tipoCod==this.RELACION){
            if (parametro!=0){
               velocidad_lenta= (long)(((velocidad_normal * 1000)/parametro)/10);
            }
        }
    }
    public void MostrarVariables(){
        System.out.println("---------------------------------");
        System.out.println("estado actual: "+ estado_actual);
        System.out.println("arrastrando " + m.arrastrando);
        System.out.println("dosTiempos: "+ m.dosTiempos);
        System.out.println("velocidad normal: "+ velocidad_normal);
        System.out.println("velocidad lenta: "+ velocidad_lenta);
        System.out.println("accion_anterior: "+ accion_anterior);
        System.out.println("---------------------------------");
    }
   
    public void esperarEventos(){
        String result;
        ByteBuffer buffer = ByteBuffer.allocate(8);
        IntBuffer intBuffer = buffer.asIntBuffer();
        try {
            while(true){
                buffer.clear();
                int numBytesRead =sc.read(buffer);
                if (numBytesRead == -1) {
                    // No more bytes can be read from the channel
                    sc.close();
                } else {
                    tipoCod = intBuffer.get(0);
                    parametro = intBuffer.get(1);
                    buffer.flip();
                    buffer.clear();
                    // To read the bytes, flip the buffer
                    if (this.tipoCod==this.FIN){
                        System.out.println("Desconexion");
                        sc.close();
                        System.exit(0);
                    }
                   
                   
                    if ((this.tipoCod== this.VELOCIDAD)||(this.tipoCod== this.TIPO)|| (this.tipoCod== this.RELACION)){
                        AnalizarConfiguracion();
                    }
                    else {
                    switch (estado_actual){
                        case PARADO:
                            AnalizarEstadoPARADO();
                        break;
                        case MOVIMENDO1:
                            AnalizarEstadoMOVIENDO1();
                        break;
                        case MOVIMENDO2:
                            AnalizarEstadoMOVIENDO2();
                        break;
                    }
                    }
                 //MostrarVariables();
                 accion_anterior=this.tipoCod;
                 parametro_anterior= this.parametro;
                 if (this.tipoCod== this.MOVIMIENTO){
                     this.movimiento_anterior= this.parametro;
                 }
                }
            }
        } catch (IOException e) {
            // Connection may have been closed
            e.printStackTrace();
        }
    }
   
    public void enviar(int cod, int param){
        ByteBuffer buffer = ByteBuffer.allocate(8);
        IntBuffer intBuffer = buffer.asIntBuffer();
       
        intBuffer.put(0, cod);
        intBuffer.put(1, param);
        try {
                if (buffer !=null){
                    sc.write(buffer);
                }
            }
        catch(Exception e) {
                e.printStackTrace();
                System.exit(0);
            }
    }
   
    public static void main( String[] args ) {
        Nucleo n =new Nucleo();
        n.conectarAPasarela();
        n.esperarEventos();
    }
   
   
}
TOP

Related Classes of Nucleo.Nucleo

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.