Professional Documents
Culture Documents
App
App
App
import javax.swing.*;
import parport.ParallelPort;
import java.util.*;
import java.util.Timer;
/**
*
* @author jcaf
*/
public class App extends javax.swing.JFrame {
/**
* 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() {
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);
}
});
.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
//jButton2.setEnabled(false);
//jButton3.setEnabled(false);
}
}//GEN-LAST:event_jButton1ActionPerformed
/**
* @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>
//-----------
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
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);
}