Package org.simbuck.plugins.mydevice

Source Code of org.simbuck.plugins.mydevice.MyDeviceTab$MyDeviceActionListenerImpl

/**
* *****************************************************************
* SIMBUCK LIMITED THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO
* WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, IN ANY CIRCUMSTANCES,
* BE LIABLE FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON
* WHATSOEVER.
*
********************************************************************
* File Description:
*
* Change History: Rev Description ----
* ----------------------------------------- 1.0 First release
*
******************************************************************
*/
package org.simbuck.plugins.mydevice;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;
import javax.swing.border.Border;
import org.simbuck.application.core.SimbuckPinConfiguration;
import static org.simbuck.core.comms.SimbuckUSBComms.getSimbuckComms;
import org.simbuck.plugins.baseboard4550.Baseboard4550Tab;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//--//import org.simbuck.plugins.baseboard4685.Baseboard4685Tab;

/**
* The main user tab displayed in the Application
*
* @author rjones
*/
public class MyDeviceTab extends Baseboard4550Tab {
//--//public class MyDeviceTab extends Baseboard4685Tab {   

    private JPanel titlepanel;

    private JPanel simbuckDevicePinsPanel;
    private JPanel baseboard;
    private JPanel pinpanel;

    private JPanel boardStatusPanel;

    /**
     * Visual component for MyDeviceBoard class
     */
    protected MyDeviceBoard boardStatus;

    private MyDeviceDataTable mydevicepinsTable;
    private MyDeviceDataTable mydeviceRegsTable;

    private final String device_name = "My Device";

    /**
     * Set the mainFrameWidth size
     */
    protected final int mainFrameWidth = 600;

    /**
     * Set the mainFrameHeight size
     */
    protected final int mainFrameHeight = 200;

    /**
     * data held in the Comms buffer
     *
     */
    static final ArrayList reg_data = new ArrayList();
    /**
     * share data held in the GUI table rather than accessing the USB Buffer
     */
    static final ArrayList shared_data = new ArrayList();
    /**
     * share data held in the GUI table rather than accessing the USB Buffer
     */
    static final ArrayList shareData = new ArrayList();

    /**
     * Local instance of error logger from slf4j library. It is good practice to
     * write into a log file for debugging issues on a real device.
     *
     * System.out.print(error......) is to be avoided for functional devices.
     */
    private static final Logger _logger = LoggerFactory.getLogger(MyDeviceTab.class);

    /**
     * Default constructor. Set up Comms: usbtask - baseboard4550 serialtask -
     * baseboard4685
     */
    public MyDeviceTab() {
        // **** set up the USB comms ****//
        usbtask = getSimbuckComms();

    }

    /**
     * Create the main GUI tab
     *
     * @return JComponent - Device User tab
     */
    public JComponent createMyDeviceTab() {

        /**
         * *******************************************************************
         * provide some helpers to make the visual representation look pretty!
         *******************************************************************
         */
        Border raisedbevel, loweredbevel, solidline, emptyborder;
        // **** make some borders for the various panels ****//
        raisedbevel = BorderFactory.createRaisedBevelBorder();
        loweredbevel = BorderFactory.createLoweredBevelBorder();
        solidline = BorderFactory.createLineBorder(Color.lightGray);
        emptyborder = BorderFactory.createEmptyBorder(1, 1, 1, 1);

        /**
         * ******************************************************************
         * Set the title for the Device
         *******************************************************************
         */
        titlepanel = new JPanel();
        titlepanel.add(new JLabel(device_name)).setFont(new Font("Serif", Font.BOLD, 18));

        /**
         * ******************************************************************
         * The visual representation will need a main Panel to act as the main
         * container for the user 'widgets'
         *******************************************************************
         */
        simbuckDevicePinsPanel = new JPanel(new BorderLayout());
        simbuckDevicePinsPanel.setMaximumSize(new Dimension(mainFrameWidth, mainFrameHeight));
        simbuckDevicePinsPanel.setMinimumSize(new Dimension(mainFrameWidth, mainFrameHeight));
        simbuckDevicePinsPanel.setPreferredSize(new Dimension(mainFrameWidth, mainFrameHeight));
        simbuckDevicePinsPanel.setBorder(solidline);

        /**
         * *******************************************************************
         * Device boards by definition have additional features and content The
         * status panel holds Status LED's, switches etc
         *******************************************************************
         */
        boardStatusPanel = new JPanel(new BorderLayout());
        boardStatusPanel.setSize(mainFrameWidth, 40);
        // call your Board status class here
        boardStatus = new MyDeviceBoard();
        boardStatusPanel.add(((JPanel) boardStatus.createBoardStatusPanel()));

        /**
         * *******************************************************************
         * Device boards will have additional IO so we use the same table
         * structures tailored to our needs
         ********************************************************************
         */
        // set the titles for the table columns
        ArrayList cols = new ArrayList();
        cols.add("Pin");
        cols.add("Name");
        cols.add("Useage");

        /**
         * *******************************************************************
         * create the table and model for pins device pins are defined in the
         * MyDevice_pins class
         *
         * They could be read in from a file in the form of a properties, ini,
         * or xml file
        *********************************************************************
         */
        // Add the Device pins
        MyDevice_pins DevicePins = new MyDevice_pins();
        ArrayList device_pins = new ArrayList();
        device_pins.addAll(DevicePins.my_device_pins().keySet());

        // create the table and model     
        mydevicepinsTable = new MyDeviceDataTable();

        // put it into a scrollpane in case it gets bigger than the allocated space
        final JScrollPane pinsPanel = new JScrollPane(mydevicepinsTable.generateTableView(cols, device_pins));
        pinsPanel.setBorder(solidline);
        pinsPanel.setMaximumSize(new Dimension(mainFrameWidth, mainFrameHeight));
        pinsPanel.setMinimumSize(new Dimension(mainFrameWidth, mainFrameHeight));
        pinsPanel.setPreferredSize(new Dimension(mainFrameWidth, mainFrameHeight));

        // create the Panel to hold everything
        final JPanel simbuckPinsPanel = new JPanel(new BorderLayout());
        simbuckPinsPanel.add(pinsPanel, BorderLayout.CENTER);
        simbuckPinsPanel.setMaximumSize(new Dimension(mainFrameWidth / 3, mainFrameHeight));
        simbuckPinsPanel.setMinimumSize(new Dimension(mainFrameWidth / 3, mainFrameHeight / 2));
        simbuckPinsPanel.setPreferredSize(new Dimension(mainFrameWidth / 3, mainFrameHeight / 2));
        simbuckPinsPanel.add(new JLabel("Pins"), BorderLayout.NORTH);
        simbuckPinsPanel.add(pinsPanel, BorderLayout.WEST);

        /**
         * *******************************************************************
         * Device boards will have IO taken from the baseboard that it uses so
         * we use the same table structures tailored to our needs
         ********************************************************************
         */
        // titles for the table columns - Relevant to registers
        // used from the Baseboard
        ArrayList col = new ArrayList();
        col.add("Register");
        col.add("Value");
        col.add("Bitmap");

        /**
         * *******************************************************************
         * create the table and model for pins device pins are defined in the
         * MyDevice_pins class
         *
         * They could be read in from a file in the form of a properties, ini,
         * or xml file
        *********************************************************************
         */
        // Add the Device registers we are interested in
        ArrayList device_regs = new ArrayList();
        device_regs.addAll(DevicePins.my_device_regs().keySet());

        // **** create the table and model **** //     
        mydeviceRegsTable = new MyDeviceDataTable();

        // put it into a scrollpane in case it gets bigger than the allocated space
        final JScrollPane regsPanel = new JScrollPane(mydeviceRegsTable.generateTableView(col, device_regs));
        regsPanel.setBorder(solidline);
        regsPanel.setMaximumSize(new Dimension(mainFrameWidth, mainFrameHeight));
        regsPanel.setMinimumSize(new Dimension(mainFrameWidth, mainFrameHeight));
        regsPanel.setPreferredSize(new Dimension(mainFrameWidth, mainFrameHeight));

        // create the Panel to hold everything associated with the Pins
        final JPanel registersPanel = new JPanel(new BorderLayout());
        registersPanel.add(regsPanel, BorderLayout.CENTER);
        registersPanel.setMaximumSize(new Dimension(mainFrameWidth / 3, mainFrameHeight));
        registersPanel.setMinimumSize(new Dimension(mainFrameWidth / 3, mainFrameHeight / 2));
        registersPanel.setPreferredSize(new Dimension(mainFrameWidth / 3, mainFrameHeight / 2));
        registersPanel.add(new JLabel("Registers"), BorderLayout.NORTH);
        registersPanel.add(regsPanel, BorderLayout.WEST);

        /**
         * *******************************************************************
         * deviceboards have the same pin layout and connections as the
         * baseboard(s) but the useage is now device specific
         *******************************************************************
         */
        // create the baseboard visual representation
        baseboard = new JPanel(new BorderLayout());
        baseboard.add(titlepanel, BorderLayout.NORTH);
        baseboard.setBorder(solidline);
        baseboard.setMaximumSize(new Dimension(mainFrameWidth, mainFrameHeight));
        baseboard.setMinimumSize(new Dimension(mainFrameWidth, mainFrameHeight));
        baseboard.setPreferredSize(new Dimension(mainFrameWidth, mainFrameHeight));

        // create a panel to hold the Pin configuration
        pinpanel = new JPanel(new BorderLayout());
        pinpanel.setBorder(solidline);

        // Create the standard Simbuck pin layout class
        pinpad = new SimbuckPinConfiguration();

        // add the pins to the panel
        pinpanel.add(pinpad.createPinConfiguration(), BorderLayout.CENTER);
        pinpanel.setMaximumSize(new Dimension(mainFrameWidth / 2, mainFrameHeight / 2));
        pinpanel.setMinimumSize(new Dimension(mainFrameWidth / 2, mainFrameHeight / 2));
        pinpanel.setPreferredSize(new Dimension(mainFrameWidth / 2, mainFrameHeight / 2));

        // put the pins layout onto the board
        baseboard.add(pinpanel, BorderLayout.CENTER);
        baseboard.add(boardStatusPanel, BorderLayout.SOUTH);

        // wrap it in a Scrollable window
        JScrollPane configPinsPanel = new JScrollPane(baseboard);
        configPinsPanel.setMaximumSize(new Dimension(mainFrameWidth, mainFrameHeight));
        configPinsPanel.setMinimumSize(new Dimension(mainFrameWidth, mainFrameHeight));
        configPinsPanel.setPreferredSize(new Dimension(mainFrameWidth, mainFrameHeight));

        /**
         * ******************************************************************
         * add everything to the main panel
         *******************************************************************
         */
        simbuckDevicePinsPanel.add(configPinsPanel, BorderLayout.CENTER);
        simbuckDevicePinsPanel.add(simbuckPinsPanel, BorderLayout.WEST);
        simbuckDevicePinsPanel.add(registersPanel, BorderLayout.EAST);

        /**
         * ******************************************************************
         * start an ActionListener to populate the ArrayList with the data to be
         * shared from the comms buffer (Easier than using a Multi-Thread
         * approach)
         *******************************************************************
         */
        // set the update rate
        int delay = 10; //milliseconds
        ActionListener myDeviceTaskPerformer = new MyDeviceActionListenerImpl();

        // set the ActionListener running
        new Timer(delay, myDeviceTaskPerformer).start();

        // return the component       
        return simbuckDevicePinsPanel;
    }

    /**
     * ******************************************************************
     * Process the data (shared) with the mydeviceRegsTable in the device GUI
         *******************************************************************
     */
    private void processIN(ArrayList share) {

        /* LatA -> share.get(0) */
        /* LatB -> share.get(1) */
        /* LatC -> share.get(2) */
        /* LatD -> share.get(3) */
        /* LatE -> share.get(4) */
        /* PortA -> share.get(5) */
        /* PortB -> share.get(6) */
        /* PortC -> share.get(7) */
        /* PortD -> share.get(8) */
        /* PortE -> share.get(9) */
        String txt = util.convertToBitmap(share.get(9).toString());
        for (int j = 0; j < 8; j++) {

            char bit = txt.charAt(j);

            switch (j) {
                case 0:
                    break;

                case 1:
                    break;

                case 2:
                    break;

                case 3:
                    break;

                case 4:
                    break;

                case 5:

                    break;

                case 6:             // Status LED
                    if (bit == '0') {
                        boardStatus.getStateBtn(0).setIcon(util.createImageIcon("images/green_light.png"));
                    } else {
                        boardStatus.getStateBtn(0).setIcon(util.createImageIcon("images/green_light_on.png"));
                    }
                    break;

                case 7:             // Status LED
                    if (bit == '0') {
                        boardStatus.getStateBtn(1).setIcon(util.createImageIcon("images/green_light.png"));
                    } else {
                        boardStatus.getStateBtn(1).setIcon(util.createImageIcon("images/green_light_on.png"));
                    }

                    break;

            }

        }

    }

    /**
     * Private class to implement the ActionListener for this device
     *
     * It has responsibility for retrieving the Comms buffer data and copying
     * the required data to the shared_data arraylist.
     *
     * Options: This implementation populates the shared_data Arraylist at the
     * same time as retrieving the USB data via usbtask.GetCommsBuffer().
     *
     * We could read the data from the mydeviceRegsTable and copy it to the
     * sharedData ArrayList instead
     *
     * We could use a different ActionListerner at a different rate.
     *
     */
    private class MyDeviceActionListenerImpl implements ActionListener {

        /**
         * Default Constructor
         */
        public MyDeviceActionListenerImpl() {
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            if (usbtask.isRunning()) {
                // make sure the copy to buffer is clear
                reg_data.clear();
                reg_data.addAll(usbtask.GetCommsBuffer());

                if (!reg_data.isEmpty()) {
                    /*
                     INPacket[4] = MCU_regs._PORTE;
                     INPacket[5] = MCU_regs._PORTD;
                     INPacket[6] = MCU_regs._PORTC;
                     INPacket[7] = MCU_regs._PORTB;
                     INPacket[8] = MCU_regs._PORTA;
                     INPacket[9] = MCU_regs._LATE;
                     INPacket[10] = MCU_regs._LATD;
                     INPacket[11] = MCU_regs._LATC;
                     INPacket[12] = MCU_regs._LATB;
                     INPacket[13] = MCU_regs._LATA;
                     */
                    mydeviceRegsTable.getTableModel().setValueAt(util.formatValueStr(reg_data.get(4).toString()), 9, 1);       //PORTE
                    mydeviceRegsTable.getTableModel().setValueAt(util.formatValueStr(reg_data.get(5).toString()), 8, 1);       //PORTD
                    mydeviceRegsTable.getTableModel().setValueAt(util.formatValueStr(reg_data.get(6).toString()), 7, 1);       //PORTC
                    mydeviceRegsTable.getTableModel().setValueAt(util.formatValueStr(reg_data.get(7).toString()), 6, 1);       //PORTB
                    mydeviceRegsTable.getTableModel().setValueAt(util.formatValueStr(reg_data.get(8).toString()), 5, 1);       //PORTA
                    mydeviceRegsTable.getTableModel().setValueAt(util.formatValueStr(reg_data.get(9).toString()), 4, 1);        //LATE
                    mydeviceRegsTable.getTableModel().setValueAt(util.formatValueStr(reg_data.get(10).toString()), 3, 1);        //LATD
                    mydeviceRegsTable.getTableModel().setValueAt(util.formatValueStr(reg_data.get(11).toString()), 2, 1);       //LATC
                    mydeviceRegsTable.getTableModel().setValueAt(util.formatValueStr(reg_data.get(12).toString()), 1, 1);       //LATB
                    mydeviceRegsTable.getTableModel().setValueAt(util.formatValueStr(reg_data.get(13).toString()), 0, 1);       //LATA

                    mydeviceRegsTable.getTableModel().setValueAt(util.convertToBitmap(reg_data.get(4).toString()), 9, 2);      //PORTE
                    mydeviceRegsTable.getTableModel().setValueAt(util.convertToBitmap(reg_data.get(5).toString()), 8, 2);      //PORTD
                    mydeviceRegsTable.getTableModel().setValueAt(util.convertToBitmap(reg_data.get(6).toString()), 7, 2);      //PORTC
                    mydeviceRegsTable.getTableModel().setValueAt(util.convertToBitmap(reg_data.get(7).toString()), 6, 2);      //PORTB
                    mydeviceRegsTable.getTableModel().setValueAt(util.convertToBitmap(reg_data.get(8).toString()), 5, 2);      //PORTA
                    mydeviceRegsTable.getTableModel().setValueAt(util.convertToBitmap(reg_data.get(9).toString()), 4, 2);       //LATE
                    mydeviceRegsTable.getTableModel().setValueAt(util.convertToBitmap(reg_data.get(10).toString()), 3, 2);       //LATD
                    mydeviceRegsTable.getTableModel().setValueAt(util.convertToBitmap(reg_data.get(11).toString()), 2, 2);      //LATC
                    mydeviceRegsTable.getTableModel().setValueAt(util.convertToBitmap(reg_data.get(12).toString()), 1, 2);      //LATB
                    mydeviceRegsTable.getTableModel().setValueAt(util.convertToBitmap(reg_data.get(13).toString()), 0, 2);      //LATA

                    mydeviceRegsTable.simbuckDeviceDataTable.repaint();

                    // prepare to copy the data for sharing
                    shared_data.clear();

                    // copy the data for sharing
                    for (int i = 0; i < 10; i++) {
                        shared_data.add(reg_data.get(13 - i));
                    }

                    // make sure the copy to buffer is clear
                    reg_data.clear();

                    // process it - do this here for an example.....
                    processIN(shared_data);
                }
            }
        }
    }

    /**
     * Provide shared data directly from the Device GUI
     *
     * Here we share the data read from a DataTable in the Device GUI.
     *
     * @return ArrayList - contains latest data for sharing
     */
    @Override
    protected synchronized ArrayList<String> getSharedData() {

        return shared_data;

    }

    /**
     * Provide shared data directly from the USB Buffer
     *
     * Here we share the data directly from the USB buffer.
     *
     * @return ArrayList - shareData
     */
    @Override
    protected synchronized ArrayList<String> getCommsData() {

        return shareData;

    }

}
TOP

Related Classes of org.simbuck.plugins.mydevice.MyDeviceTab$MyDeviceActionListenerImpl

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.