App

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.codebind;

import javax.swing.*;
import parport.ParallelPort;
import java.util.*;
import java.util.Timer;
/**
*
* @author jcaf
*/
public class App extends javax.swing.JFrame {

static boolean boton_piso_lock= true;//false;

static ParallelPort lpt1 = new ParallelPort(0x378);//C0b0


final static long periodo = 20;//1000;

/**
* Creates new form App
*/
public App() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-
BEGIN:initComponents
private void initComponents() {

jButton1 = new javax.swing.JButton();


jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});

jButton1.setText("Piso1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Piso2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText("Piso3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(150, 150, 150)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3)
.addComponent(jButton2)
.addComponent(jButton1))
.addContainerGap(185, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(102, Short.MAX_VALUE)
.addComponent(jButton3)
.addGap(30, 30, 30)
.addComponent(jButton2)
.addGap(28, 28, 28)
.addComponent(jButton1)
.addGap(65, 65, 65))
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-


FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
if (boton_piso_lock == true)
{
goPiso(1);
System.out.println("Hacia piso: 1");

//jButton2.setEnabled(false);
//jButton3.setEnabled(false);
}
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-


FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
if (boton_piso_lock == true)
{
goPiso(2);
System.out.println("Hacia piso: 2");
// Piso3.setEnabled(false);
// Piso1.setEnabled(false);
}
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-


FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
if (boton_piso_lock == true)
{
goPiso(3);
System.out.println("Hacia piso: 3");
// Piso1.setEnabled(false);
// Piso2.setEnabled(false);
}
}//GEN-LAST:event_jButton3ActionPerformed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-


FIRST:event_formWindowClosing
// TODO add your handling code here:
lpt1.write(0x00 );
}//GEN-LAST:event_formWindowClosing

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code
(optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the
default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level
.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level
.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level
.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level
.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new App().setVisible(true);
}
});

//-----------
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
leerSensores();
}
}, 0, periodo);//cada x millis

motor_init_pos();
}

static
public void motor_init_pos()
{
switches_state = lpt1.read();
if ( ( (switches_state&0xff) & (1<<SWITCH_PISO_1)) != 0)//Si motor no esta
en el piso 1...
{
motor_girar_derecha();
PisoDestino = 1;
}
else
{
PisoActual = 1;

// Piso1.setEnabled(false);
// Piso2.setEnabled(true);
// Piso3.setEnabled(true);

boton_piso_lock = true;
}
}

//-------------------
static public void goPiso(int gotoPiso) {
int giro = PisoActual - gotoPiso;
if (giro != 0)
{
if ( giro < 0)
{
motor_girar_izquierda();

}
else
{
motor_girar_derecha();
}
PisoDestino = gotoPiso;

boton_piso_lock = false;

//clear leds
BitTo0(5);
BitTo0(4);
BitTo0(3);
lpt1.write(data_addr & 0xFF);

}
static int PisoDestino = -1;
static int PisoActual = 1;
static int switches_state = 0;
static int switches_state_last=0xFF;
final public static int SWITCH_PISO_1 = 6;//PIN 10 = ~ACK
final public static int SWITCH_PISO_2 = 5;//PIN 12 = PE
final public static int SWITCH_PISO_3 = 4;//PIN 13 SLCT_IN

static int timer1;


static public void leerSensores()
{
if (PisoDestino > 0)
{
//timming led
if (++timer1 >= 10)
{
timer1 = 0;
if (PisoDestino == 1)
TogglingBit(5);
else if (PisoDestino == 2)
TogglingBit(4);
else if (PisoDestino == 3)
TogglingBit(3);

lpt1.write(data_addr & 0xFF);

switches_state = lpt1.read();

if (PisoDestino == 1)
{
if ( ( (switches_state&0xff) & (1<<SWITCH_PISO_1)) == 0)//if
( ( (switches_state&0xff) & (1<<6)) == 0)
{
motor_stop();
PisoActual = PisoDestino;
PisoDestino = -1;

boton_piso_lock = true;
// Piso1.setEnabled(false);
// Piso2.setEnabled(true);
// Piso3.setEnabled(true);

BitTo1(5);
lpt1.write(data_addr & 0xFF);

}
}
else if (PisoDestino == 2)
{
if ( ( (switches_state&0xff) & (1<<SWITCH_PISO_2)) == 0)
{
motor_stop();
PisoActual = PisoDestino;
PisoDestino = -1;

boton_piso_lock = true;
// Piso1.setEnabled(true);
// Piso2.setEnabled(false);
// Piso3.setEnabled(true);
BitTo1(4);
lpt1.write(data_addr & 0xFF);
}

}
else if (PisoDestino == 3)
{
if ( ( (switches_state&0xff) & (1<<SWITCH_PISO_3)) == 0)
{
motor_stop();
PisoActual = PisoDestino;
PisoDestino = -1;

boton_piso_lock = true;
// Piso1.setEnabled(true);
// Piso2.setEnabled(true);
// Piso3.setEnabled(false);
BitTo1(3);
lpt1.write(data_addr & 0xFF);
}

// PIN D7-D6 controlan el motor


static public void motor_stop()
{
//lpt1.write(0x00 & 0xFF);
BitTo0(7);
BitTo0(6);
lpt1.write(data_addr & 0xff);
}
static public void motor_girar_derecha()
{
BitTo1(7);
BitTo0(6);
lpt1.write(data_addr & 0xFF);
//lpt1.write(0x80 & 0xFF);
}
static public void motor_girar_izquierda()
{
BitTo0(7);
BitTo1(6);
lpt1.write(data_addr & 0xFF);
//lpt1.write(0x40 & 0xFF);
}

static int data_addr = 0;


static public void TogglingBit(int _BIT_)
{
data_addr = (data_addr&0xff) ^ (1<<(_BIT_));
}
static public void BitTo1(int _KTE_)
{
data_addr = (data_addr&0xff) | (1<<_KTE_);
}
static public void BitTo0(int _KTE_)
{
data_addr = (data_addr&0xff) & (~(1<<_KTE_));
}

// Variables declaration - do not modify//GEN-BEGIN:variables


private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
// End of variables declaration//GEN-END:variables
}

You might also like