view.Dashboard.java Source code

Java tutorial

Introduction

Here is the source code for view.Dashboard.java

Source

/*
 * Copyright (C) 2015 Riva Syafri Rachmatullah
 *
 * 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 view;

import controller.ConnectionController;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import jssc.SerialPortException;
import jssc.SerialPortList;
import jssc.SerialPortTimeoutException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * Dashboard view of this task
 * @author Riva Syafri Rachmatullah
 */
public class Dashboard extends javax.swing.JFrame {

    private double setPoint;
    private int currentTime = 0;
    private int counter = 0;
    private int setRangeY = 0;
    private final ConnectionController cc;
    private Timer timer;
    private JFreeChart voltageLineChart;
    private XYSeries voltageSeries;
    private XYSeries setPointSeries;
    private XYSeriesCollection voltageDataset;
    private XYPlot xyPlot;
    private int tickpoint = 50;

    /**
     * Creates new form Dashboard
     */
    public Dashboard() {
        cc = new ConnectionController();
        voltageSeries = new XYSeries("voltage");
        setPointSeries = new XYSeries("setpoint");
        voltageSeries.add(currentTime, 0);
        currentTime++;
        voltageDataset = new XYSeriesCollection();
        voltageDataset.addSeries(voltageSeries);
        voltageDataset.addSeries(setPointSeries);
        voltageLineChart = generateVoltageLineChart();
        initComponents();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2);
        this.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
        baudRate.setSelectedItem(baudRate.getItemAt(3));
    }

    /**
     * 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() {

        ControlPanel = new javax.swing.JPanel();
        KPLabel = new javax.swing.JLabel();
        KPField = new javax.swing.JFormattedTextField();
        KIField = new javax.swing.JFormattedTextField();
        KDField = new javax.swing.JFormattedTextField();
        setPointField = new javax.swing.JFormattedTextField();
        KILabel = new javax.swing.JLabel();
        KDLabel = new javax.swing.JLabel();
        setPointLabel = new javax.swing.JLabel();
        rotationSelection = new javax.swing.JComboBox();
        rotationLabel = new javax.swing.JLabel();
        startButton = new javax.swing.JButton();
        modifyButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        ChartPanel chart = new ChartPanel(voltageLineChart);
        chart.setDomainZoomable(true);
        chartPanel = new javax.swing.JPanel();
        ConnectorPanel = new javax.swing.JPanel();
        baudRate = new javax.swing.JComboBox();
        portName = new javax.swing.JComboBox();
        baudRateLabel = new javax.swing.JLabel();
        portNameLabel = new javax.swing.JLabel();
        timedivLabel = new javax.swing.JLabel();
        timedivSelection = new javax.swing.JComboBox();
        connectButton = new javax.swing.JButton();
        yAxisRange = new javax.swing.JLabel();
        yAxisRangeSelection = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("PIC Controller");
        setBackground(new java.awt.Color(51, 51, 51));
        setForeground(new java.awt.Color(51, 51, 51));

        ControlPanel
                .setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 0, 2, new java.awt.Color(0, 0, 0)));
        ControlPanel.setPreferredSize(new java.awt.Dimension(200, 768));

        KPLabel.setText("KP");

        KPField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
                new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.000"))));
        KPField.setMinimumSize(new java.awt.Dimension(85, 20));
        KPField.setPreferredSize(new java.awt.Dimension(85, 20));

        KIField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
                new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.000"))));
        KIField.setMinimumSize(new java.awt.Dimension(85, 20));
        KIField.setPreferredSize(new java.awt.Dimension(85, 20));

        KDField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
                new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.000"))));
        KDField.setMinimumSize(new java.awt.Dimension(85, 20));
        KDField.setPreferredSize(new java.awt.Dimension(85, 20));

        setPointField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
                new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.000"))));
        setPointField.setMinimumSize(new java.awt.Dimension(85, 20));
        setPointField.setPreferredSize(new java.awt.Dimension(85, 20));

        KILabel.setText("KI");

        KDLabel.setText("KD");

        setPointLabel.setText("Setpoint");

        rotationSelection.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CW", "CCW" }));

        rotationLabel.setText("Rotation");

        startButton.setText("Start");
        startButton.setEnabled(false);
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });

        modifyButton.setText("Modify");
        modifyButton.setEnabled(false);
        modifyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("PIC CONTROLLER");

        javax.swing.GroupLayout ControlPanelLayout = new javax.swing.GroupLayout(ControlPanel);
        ControlPanel.setLayout(ControlPanelLayout);
        ControlPanelLayout.setHorizontalGroup(ControlPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ControlPanelLayout.createSequentialGroup()
                        .addGroup(ControlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(ControlPanelLayout.createSequentialGroup().addContainerGap().addComponent(
                                        jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, ControlPanelLayout
                                        .createSequentialGroup().addGap(20, 20, 20)
                                        .addGroup(ControlPanelLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(ControlPanelLayout.createSequentialGroup()
                                                        .addGap(66, 66, 66)
                                                        .addGroup(ControlPanelLayout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(KPField,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE, 89,
                                                                        Short.MAX_VALUE)
                                                                .addComponent(KIField,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        Short.MAX_VALUE)))
                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                        ControlPanelLayout.createSequentialGroup()
                                                                .addGroup(ControlPanelLayout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.TRAILING)
                                                                        .addGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING,
                                                                                ControlPanelLayout
                                                                                        .createSequentialGroup()
                                                                                        .addGap(21, 21, 21)
                                                                                        .addGroup(ControlPanelLayout
                                                                                                .createParallelGroup(
                                                                                                        javax.swing.GroupLayout.Alignment.TRAILING)
                                                                                                .addComponent(
                                                                                                        KILabel)
                                                                                                .addComponent(
                                                                                                        KDLabel)
                                                                                                .addComponent(
                                                                                                        KPLabel))
                                                                                        .addGap(0, 0,
                                                                                                Short.MAX_VALUE))
                                                                        .addGroup(ControlPanelLayout
                                                                                .createSequentialGroup()
                                                                                .addGap(0, 0, Short.MAX_VALUE)
                                                                                .addGroup(ControlPanelLayout
                                                                                        .createParallelGroup(
                                                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                                                        .addComponent(setPointLabel)
                                                                                        .addComponent(
                                                                                                rotationLabel))))
                                                                .addGap(18, 18, 18)
                                                                .addGroup(ControlPanelLayout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addComponent(KDField,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                Short.MAX_VALUE)
                                                                        .addComponent(setPointField,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                Short.MAX_VALUE)
                                                                        .addComponent(rotationSelection, 0,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                Short.MAX_VALUE)))
                                                .addGroup(ControlPanelLayout.createSequentialGroup()
                                                        .addComponent(startButton,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE)
                                                        .addGap(18, 18, 18).addComponent(modifyButton,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 69,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(23, 23, 23)));
        ControlPanelLayout.setVerticalGroup(ControlPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(ControlPanelLayout.createSequentialGroup().addGap(23, 23, 23)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(ControlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(KPLabel).addComponent(KPField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(ControlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(KIField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(KILabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(ControlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(KDField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(KDLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(ControlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(setPointField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(setPointLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(ControlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(rotationSelection, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(rotationLabel))
                        .addGap(18, 18, 18)
                        .addGroup(ControlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(startButton).addComponent(modifyButton))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        chartPanel.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 2, 2, 0, new java.awt.Color(0, 0, 0)));
        chartPanel.setPreferredSize(new java.awt.Dimension(810, 660));
        chartPanel.setLayout(new java.awt.BorderLayout());

        chartPanel.add(chart, java.awt.BorderLayout.CENTER);

        ConnectorPanel
                .setBorder(javax.swing.BorderFactory.createMatteBorder(2, 2, 0, 0, new java.awt.Color(0, 0, 0)));
        ConnectorPanel.setPreferredSize(new java.awt.Dimension(814, 100));

        baudRate.setModel(new javax.swing.DefaultComboBoxModel(
                new String[] { "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" }));

        portName.setModel(new javax.swing.DefaultComboBoxModel(SerialPortList.getPortNames()));

        baudRateLabel.setText("Baud Rate");

        portNameLabel.setText("Port Name");

        timedivLabel.setText("Time/DIV");

        timedivSelection
                .setModel(new javax.swing.DefaultComboBoxModel(new String[] { "50", "100", "200", "500", "1000" }));
        timedivSelection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                timedivSelectionActionPerformed(evt);
            }
        });

        connectButton.setText("Connect");
        connectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectButtonActionPerformed(evt);
            }
        });

        yAxisRange.setText("Y-Axis Range");

        yAxisRangeSelection.setModel(
                new javax.swing.DefaultComboBoxModel(new String[] { "Auto", "0 - 5 V", "0 - 10 V", "0 - 15 V" }));
        yAxisRangeSelection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                yAxisRangeSelectionActionPerformed(evt);
            }
        });

        jLabel3.setText("Update Rate = 50ms");

        javax.swing.GroupLayout ConnectorPanelLayout = new javax.swing.GroupLayout(ConnectorPanel);
        ConnectorPanel.setLayout(ConnectorPanelLayout);
        ConnectorPanelLayout
                .setHorizontalGroup(
                        ConnectorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(ConnectorPanelLayout.createSequentialGroup().addGroup(ConnectorPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(ConnectorPanelLayout.createSequentialGroup().addContainerGap()
                                                .addComponent(jLabel3)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(portNameLabel))
                                        .addGroup(ConnectorPanelLayout.createSequentialGroup().addGap(470, 470, 470)
                                                .addComponent(yAxisRange)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(ConnectorPanelLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                                        false)
                                                .addComponent(yAxisRangeSelection, 0,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(portName, 0, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(ConnectorPanelLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(timedivLabel).addComponent(baudRateLabel))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(ConnectorPanelLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                        false)
                                                .addComponent(baudRate, 0, 67, Short.MAX_VALUE)
                                                .addComponent(timedivSelection, 0,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(connectButton).addGap(26, 26, 26)));
        ConnectorPanelLayout.setVerticalGroup(
                ConnectorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING,
                        ConnectorPanelLayout.createSequentialGroup().addContainerGap()
                                .addGroup(ConnectorPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(yAxisRange)
                                        .addComponent(yAxisRangeSelection, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(timedivLabel).addComponent(timedivSelection,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(ConnectorPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(baudRate, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(portName, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(baudRateLabel).addComponent(portNameLabel)
                                        .addComponent(connectButton).addComponent(jLabel3))
                                .addContainerGap()));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(ControlPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(ConnectorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 838,
                                        Short.MAX_VALUE)
                                .addComponent(chartPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(ControlPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(chartPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(ConnectorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
                                javax.swing.GroupLayout.PREFERRED_SIZE)));

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

    public void updateChart() {
        if (cc.getConnectionStatus()) {
            try {
                double data = cc.readData();
                if (counter == 1000) {
                    voltageSeries.remove(0);
                    setPointSeries.remove(0);
                } else if (counter < 1000) {
                    counter++;
                }
                voltageSeries.add(currentTime * 50, data);
                setPointSeries.add(currentTime * 50, setPoint);
                xyPlot = (XYPlot) voltageLineChart.getPlot();
                xyPlot.setDomainCrosshairVisible(true);
                xyPlot.setRangeCrosshairVisible(true);
                NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
                domain.setRange(currentTime * 50 - tickpoint * 10, currentTime * 50);
                System.out.println(voltageSeries.getItemCount());
                domain.setTickUnit(new NumberTickUnit(tickpoint));
                currentTime++;
                NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
                switch (setRangeY) {
                case 1:
                    range.setRange(0, 5);
                    range.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
                    break;
                case 2:
                    range.setRange(0, 10);
                    range.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
                    break;
                case 3:
                    range.setRange(0, 15);
                    range.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
                    break;
                default:
                    range.setAutoRange(true);
                    range.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
                    break;
                }
            } catch (SerialPortTimeoutException ex) {
                timer.stop();
                cc.setConnectionStatus(false);
                cc.setReadStatus(false);
            }
        } else {
            timer.stop();
        }
    }

    public void updateDashboard() {
        portName.setModel(new javax.swing.DefaultComboBoxModel(SerialPortList.getPortNames()));
        setComponentsEnabled();
        setConnectionText();
        setStartText();
    }

    private double getKI() {
        return Double.parseDouble(KIField.getText());
    }

    private double getKP() {
        return Double.parseDouble(KPField.getText());
    }

    private double getKD() {
        return Double.parseDouble(KDField.getText());
    }

    private double getSetPoint() {
        setPoint = Double.parseDouble(setPointField.getText());
        return setPoint;
    }

    private int getRotation() {
        if (((String) rotationSelection.getSelectedItem()).equals("CW")) {
            return 0;
        } else {
            return 1;
        }
    }

    private JFreeChart generateVoltageLineChart() {
        JFreeChart lineChartObject = ChartFactory.createXYLineChart(null, "TIME", "VOLTAGE", voltageDataset,
                PlotOrientation.VERTICAL, true, true, false);
        return lineChartObject;
    }

    private void setComponentsEnabled() {
        portName.setEnabled(!cc.getConnectionStatus());
        baudRate.setEnabled(!cc.getConnectionStatus());
        startButton.setEnabled(cc.getConnectionStatus());
        KDField.setEnabled(cc.getConnectionStatus());
        KIField.setEnabled(cc.getConnectionStatus());
        KPField.setEnabled(cc.getConnectionStatus());
        setPointField.setEnabled(cc.getConnectionStatus());
        rotationSelection.setEnabled(!cc.getReadStatus());
        modifyButton.setEnabled(cc.getReadStatus());
    }

    private void resetData() {
        setPointSeries.clear();
        voltageSeries.clear();
        currentTime = 0;
        counter = 0;
    }

    private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectButtonActionPerformed
        try {
            if (cc.getConnectionStatus()) {
                cc.disconnect();
                cc.setReadStatus(false);
                connectButton.setText("Connect");
                setComponentsEnabled();
            } else {
                resetData();
                cc.connect((String) portName.getSelectedItem(),
                        Integer.parseInt((String) baudRate.getSelectedItem()));
                connectButton.setText("Disconnect");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Dashboard.class.getName()).log(Level.SEVERE, null, ex);
                }
                setComponentsEnabled();
            }
        } catch (SerialPortException ex) {
            Logger.getLogger(Dashboard.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_connectButtonActionPerformed

    private void setConnectionText() {
        if (!cc.getConnectionStatus()) {
            connectButton.setText("Connect");
        } else {
            connectButton.setText("Disconnect");
        }
    }

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
        try {
            if (cc.getReadStatus()) {
                timer.stop();
                cc.stopReading();
                setStartText();
            } else {
                if (KPField.getText().isEmpty() || KIField.getText().isEmpty() || KDField.getText().isEmpty()
                        || setPointField.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(rootPane, "KP, KD, KI, or Setpoint is still empty", "ERROR",
                            JOptionPane.ERROR_MESSAGE);
                } else {
                    if (Double.parseDouble(KPField.getText()) > 50 || Double.parseDouble(KPField.getText()) < 0) {
                        JOptionPane.showMessageDialog(rootPane, "KP has to be in 0 to 50", "ERROR",
                                JOptionPane.ERROR_MESSAGE);
                    } else if (Double.parseDouble(KIField.getText()) > 50
                            || Double.parseDouble(KIField.getText()) < 0) {
                        JOptionPane.showMessageDialog(rootPane, "KI has to be in 0 to 50", "ERROR",
                                JOptionPane.ERROR_MESSAGE);
                    } else if (Double.parseDouble(KDField.getText()) > 50
                            || Double.parseDouble(KDField.getText()) < 0) {
                        JOptionPane.showMessageDialog(rootPane, "KD has to be in 0 to 50", "ERROR",
                                JOptionPane.ERROR_MESSAGE);
                    } else if (Double.parseDouble(setPointField.getText()) > 12
                            || Double.parseDouble(setPointField.getText()) <= 0) {
                        JOptionPane.showMessageDialog(rootPane, "Setpoint has to be between 0 to 12", "ERROR",
                                JOptionPane.ERROR_MESSAGE);
                    } else {
                        timer = new Timer(40, (ActionEvent e) -> {
                            updateChart();
                        });
                        timer.start();
                        cc.startReading(getKP(), getKI(), getKD(), getSetPoint(), getRotation());
                        setStartText();
                    }
                }
            }
        } catch (SerialPortException ex) {
            Logger.getLogger(Dashboard.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_startButtonActionPerformed

    private void setStartText() {
        if (cc.getReadStatus()) {
            startButton.setText("Stop");
        } else {
            startButton.setText("Start");
        }
    }

    private void modifyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyButtonActionPerformed
        try {
            if (KPField.getText().isEmpty() || KIField.getText().isEmpty() || KDField.getText().isEmpty()
                    || setPointField.getText().isEmpty()) {
                JOptionPane.showMessageDialog(rootPane, "KP, KD, KI, or Setpoint is still empty", "ERROR",
                        JOptionPane.ERROR_MESSAGE);
            } else {
                cc.modifyData(getKP(), getKI(), getKD(), getSetPoint(), getRotation());
            }
        } catch (SerialPortException ex) {
            Logger.getLogger(Dashboard.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_modifyButtonActionPerformed

    private void timedivSelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_timedivSelectionActionPerformed
        tickpoint = Integer.parseInt((String) timedivSelection.getSelectedItem());
    }//GEN-LAST:event_timedivSelectionActionPerformed

    private void yAxisRangeSelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_yAxisRangeSelectionActionPerformed
        setRangeY = yAxisRangeSelection.getSelectedIndex();
    }//GEN-LAST:event_yAxisRangeSelectionActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ConnectorPanel;
    private javax.swing.JPanel ControlPanel;
    private javax.swing.JFormattedTextField KDField;
    private javax.swing.JLabel KDLabel;
    private javax.swing.JFormattedTextField KIField;
    private javax.swing.JLabel KILabel;
    private javax.swing.JFormattedTextField KPField;
    private javax.swing.JLabel KPLabel;
    private javax.swing.JComboBox baudRate;
    private javax.swing.JLabel baudRateLabel;
    private javax.swing.JPanel chartPanel;
    private javax.swing.JButton connectButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton modifyButton;
    private javax.swing.JComboBox portName;
    private javax.swing.JLabel portNameLabel;
    private javax.swing.JLabel rotationLabel;
    private javax.swing.JComboBox rotationSelection;
    private javax.swing.JFormattedTextField setPointField;
    private javax.swing.JLabel setPointLabel;
    private javax.swing.JButton startButton;
    private javax.swing.JLabel timedivLabel;
    private javax.swing.JComboBox timedivSelection;
    private javax.swing.JLabel yAxisRange;
    private javax.swing.JComboBox yAxisRangeSelection;
    // End of variables declaration//GEN-END:variables
}