org.hwo.pulscounter/src/org/hwo/pulscounter/ui/LiveViewFrame.java

310 lines
9.2 KiB
Java

package org.hwo.pulscounter.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.hwo.datetime.JTimeOfDay;
import org.hwo.io.servicelink.ServiceLink;
import org.hwo.io.servicelink.ServiceLinkException;
import org.hwo.io.servicelink.ServiceLinkRequestFailedException;
import org.hwo.pulscounter.PulsCounter2Application;
import java.awt.GridBagLayout;
import java.io.IOException;
import java.io.ObjectOutputStream.PutField;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JToggleButton;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.border.TitledBorder;
import java.awt.GridLayout;
public class LiveViewFrame extends JFrame implements AppSettingsListener {
private JPanel contentPane;
private Timer liveViewTimer;
private ServiceLink serviceLink;
private JToggleButton[] inputButtons;
private JToggleButton[] pullupButtons;
private JToggleButton[] outputButtons;
private JLabel[] analogLabels;
private JPanel pDisplay;
private JPanel pPullups;
private JPanel pAnalog;
private JPanel pOutputs;
private Integer inputs,
outputs,
pullups;
/**
* Create the frame.
*/
public LiveViewFrame() {
setTitle("Live Ansicht");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 873, 530);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
pDisplay = new JPanel();
pDisplay.setBorder(new TitledBorder(null, "Eing\u00E4nge", TitledBorder.LEADING, TitledBorder.TOP, null, null));
GridBagConstraints gbc_pDisplay = new GridBagConstraints();
gbc_pDisplay.insets = new Insets(0, 0, 5, 0);
gbc_pDisplay.fill = GridBagConstraints.BOTH;
gbc_pDisplay.gridx = 0;
gbc_pDisplay.gridy = 0;
contentPane.add(pDisplay, gbc_pDisplay);
GridBagLayout gbl_pDisplay = new GridBagLayout();
gbl_pDisplay.columnWidths = new int[] {50, 50, 50, 50, 50, 50, 50, 50};
gbl_pDisplay.rowHeights = new int[] {25, 25, 25, 25};
gbl_pDisplay.columnWeights = new double[]{1.0, 1.0,1.0, 1.0,1.0, 1.0,1.0, 1.0};
gbl_pDisplay.rowWeights = new double[]{1.0, 1.0,1.0, 1.0};
pDisplay.setLayout(gbl_pDisplay);
pPullups = new JPanel();
pPullups.setBorder(new TitledBorder(null, "PullUPs", TitledBorder.LEADING, TitledBorder.TOP, null, null));
GridBagConstraints gbc_pPullups = new GridBagConstraints();
gbc_pPullups.insets = new Insets(0, 0, 5, 0);
gbc_pPullups.fill = GridBagConstraints.BOTH;
gbc_pPullups.gridx = 0;
gbc_pPullups.gridy = 1;
contentPane.add(pPullups, gbc_pPullups);
GridBagLayout gbl_pPullups = new GridBagLayout();
gbl_pPullups.columnWidths = new int[] {50, 50, 50, 50, 50, 50, 50, 50};
gbl_pPullups.rowHeights = new int[] {25, 25, 25, 25};
gbl_pPullups.columnWeights = new double[]{1.0, 1.0,1.0, 1.0,1.0, 1.0,1.0, 1.0};
gbl_pPullups.rowWeights = new double[]{1.0, 1.0,1.0, 1.0};
pPullups.setLayout(gbl_pPullups);
pOutputs = new JPanel();
pOutputs.setBorder(new TitledBorder(null, "Ausg\u00E4nge", TitledBorder.LEADING, TitledBorder.TOP, null, null));
GridBagConstraints gbc_pOutputs = new GridBagConstraints();
gbc_pOutputs.insets = new Insets(0, 0, 5, 0);
gbc_pOutputs.fill = GridBagConstraints.BOTH;
gbc_pOutputs.gridx = 0;
gbc_pOutputs.gridy = 2;
contentPane.add(pOutputs, gbc_pOutputs);
GridBagLayout gbl_pOutputs = new GridBagLayout();
gbl_pOutputs.columnWidths = new int[] {50, 50, 50, 50, 50, 50, 50, 50};
gbl_pOutputs.rowHeights = new int[] {25, 25, 25, 25};
gbl_pOutputs.columnWeights = new double[]{1.0, 1.0,1.0, 1.0,1.0, 1.0,1.0, 1.0};
gbl_pOutputs.rowWeights = new double[]{1.0, 1.0,1.0, 1.0};
pOutputs.setLayout(gbl_pOutputs);
pAnalog = new JPanel();
pAnalog.setBorder(new TitledBorder(null, "Analogkan\u00E4le", TitledBorder.LEADING, TitledBorder.TOP, null, null));
GridBagConstraints gbc_pAnalog = new GridBagConstraints();
gbc_pAnalog.fill = GridBagConstraints.BOTH;
gbc_pAnalog.gridx = 0;
gbc_pAnalog.gridy = 3;
contentPane.add(pAnalog, gbc_pAnalog);
pAnalog.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
initialize();
}
@Override
public void dispose() {
PulsCounter2Application pc2a = PulsCounter2Application.getApplication();
pc2a.removeAppSettingsListener(this);
super.dispose();
}
private void initialize(){
PulsCounter2Application pc2a = PulsCounter2Application.getApplication();
inputButtons = new JToggleButton[32];
outputButtons = new JToggleButton[32];
pullupButtons = new JToggleButton[32];
for (int i=0;i<32;i++){
final int n = i;
inputButtons[i] = new JToggleButton(String.format("%d", i));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 0, 0, 0);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = i % 8;
gbc.gridy = (i / 8);
pDisplay.add(inputButtons[i], gbc);
outputButtons[i] = new JToggleButton(String.format("%d", i));
outputButtons[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
setOutput(n, outputButtons[n].isSelected());
}
});
pOutputs.add(outputButtons[i], gbc);
pullupButtons[i] = new JToggleButton(String.format("%d", i));
pullupButtons[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
setPullUp(n, pullupButtons[n].isSelected());
}
});
pPullups.add(pullupButtons[i],gbc);
}
analogLabels = new JLabel[8];
for (int i=0;i<8;i++){
analogLabels[i] = new JLabel(String.format("---"));
analogLabels[i].setBorder(new TitledBorder(String.format("AN%d",i)));
pAnalog.add(analogLabels[i]);
}
liveViewTimer = new Timer();
liveViewTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
updateLiveView();
}
}, 200, 250);
pc2a.addAppSettingsListener(this);
serviceLink = pc2a.getServiceLink();
}
private synchronized void updateLiveView(){
System.err.println("LiveView Update");
if (serviceLink != null){
System.err.println("ServiceLink exists.");
try {
serviceLink.open();
} catch (ServiceLinkException e) {
e.printStackTrace();
}
inputs = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x84F0 );
outputs = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x81F9 );
pullups = serviceLink.getServiceRegisterCache().getCachedInteger(13, 0, 0x81FA );
if (inputs == null)
inputs = 0;
if (outputs == null)
outputs = 0;
if (pullups == null)
pullups = 0;
System.err.println(String.format("Inputs State: 0x%08x",inputs));
System.err.println(String.format("Outputs State: 0x%08x",outputs));
System.err.println(String.format("PullUP State: 0x%08x",pullups));
for (int i=0;i<32;i++){
if ((inputs & (1<<i))!=0){
inputButtons[i].setBackground(Color.GREEN);
inputButtons[i].setSelected(true);
} else {
inputButtons[i].setBackground(Color.RED);
inputButtons[i].setSelected(false);
}
if ((outputs & (1<<i))!=0){
outputButtons[i].setBackground(Color.GREEN);
outputButtons[i].setSelected(true);
} else {
outputButtons[i].setBackground(Color.WHITE);
outputButtons[i].setSelected(false);
}
if ((pullups & (1<<i))!=0){
pullupButtons[i].setBackground(Color.BLUE);
pullupButtons[i].setSelected(true);
} else {
pullupButtons[i].setBackground(Color.WHITE);
pullupButtons[i].setSelected(false);
}
}
for (int i=0;i<8;i++){
float analog = serviceLink.getServiceRegisterCache().getCachedFloat(13, 0, 0x8000 + i );
analogLabels[i].setText(String.format("%5.3fV", (analog * 10.0f)));
}
}
}
@Override
public synchronized void ServiceLinkChanged(ServiceLink serviceLink) {
this.serviceLink = serviceLink;
}
public synchronized void setOutput(int ch,boolean set){
if (this.serviceLink != null){
try {
if (set){
serviceLink.writeInt((byte)13, (byte)0, 0x8100 + ch, 1);
} else {
serviceLink.writeInt((byte)13, (byte)0, 0x8100 + ch, 0);
}
} catch (ServiceLinkRequestFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ServiceLinkException e) {
e.printStackTrace();
}
}
}
public synchronized void setPullUp(int ch,boolean set){
if (this.serviceLink != null){
if (set){
pullups |= (1<<ch);
} else {
pullups &= ~(1<<ch);
}
try {
serviceLink.writeInt((byte)13, (byte)0, 0x81FA, pullups);
} catch (ServiceLinkRequestFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ServiceLinkException e) {
e.printStackTrace();
}
}
}
}