librefdatool.py 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
# This file is part of librefdatool. librefdatool 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, version 2.
#
# 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, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2013 Javier D. Garcia-Lasheras


'''*** LibreFDATool ***
Libre Filter Design and Analysis Tool
LICENSED UNDER GPLv2 TERMS

This package provides the next librefdatool objects:
* device: filter hardware model
* generator: arbitrary waveform generator
* scope: multifunction signal scope

'''

# metadata module
__version__ = '0.1'
__author__ = 'Javier D. Garcia-Lasheras'
__email__ = 'javier@garcialasheras.com'
__url__ = 'http://www.ohwr.org/projects/libre-fdatool'


# LibreFDATool Libraries
from generator import * 
from scope import * 
#from device import * 
from filter import *
from simulator import *

from threading import Thread

import sys, os, random
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class LibreFDATool(
    QMainWindow, 
    Generator, 
    Filter,
    Simulator
    ):
    
    # TODO: I need to redeclare the signals here and I don't like it!!
    stimulusUpdatedSignal = pyqtSignal()
    filterUpdatedSignal = pyqtSignal()
    simulatorUpdatedSignal = pyqtSignal()
60 61 62
    simulatorBeginSignal = pyqtSignal()
    simulatorEndSignal = pyqtSignal()
    
63 64 65 66 67 68 69 70 71 72 73 74 75
    
    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowTitle('Libre-FDATool')

        self.create_menu()
        self.create_toolbar()
        self.create_main_frame()
        self.create_status_bar()
        
        self.stimulusUpdatedSignal.connect(self.on_stimulus_updated)
        self.filterUpdatedSignal.connect(self.on_filter_updated)
        self.simulatorUpdatedSignal.connect(self.on_simulator_updated)
76 77
        self.simulatorBeginSignal.connect(self.on_simulator_begin)
        self.simulatorEndSignal.connect(self.on_simulator_end)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        
        self.runSimulator()
        
        self.main_frame.setCurrentIndex(0)
        
        #Generator.__init__(self)
        #Filter.__init__(self)

    

    def save_plot(self):
        file_choices = "PNG (*.png)|*.png"
        
        path = unicode(QFileDialog.getSaveFileName(self, 
                        'Save file', '', 
                        file_choices))
        if path:
            self.canvas.print_figure(path, dpi=self.dpi)
            self.statusBar().showMessage('Saved to %s' % path, 2000)
    
    def on_about(self):
        msg = """ This is Libre-FDATool development version:
        
         * Use stimulus tab to configure the input
         * Use filter tab to analyze and configure the filter
         * Use simulation to configure the simulator

        """
        QMessageBox.about(self, "About the demo", msg.strip())
    
    def on_pick(self, event):
        # The event received here is of the type
        # matplotlib.backend_bases.PickEvent
        #
        # It carries lots of information, of which we're using
        # only a small amount here.
        # 
        box_points = event.artist.get_bbox().get_points()
        msg = "You've clicked on a bar with coords:\n %s" % box_points
        
        QMessageBox.information(self, "Click!", msg)
    

        
    
    def create_main_frame(self):

        self.main_frame = QTabWidget()
        tabStimulus = QWidget()
        tabFilter = QWidget()
        tabSimulator = QWidget()

        self.main_frame.addTab(tabStimulus, "Stimulus")
        self.main_frame.addTab(tabFilter, "Filter")
        self.main_frame.addTab(tabSimulator, "Simulation")
        
        tabStimulus.setLayout(self.create_stimulus_layout())        
        tabFilter.setLayout(self.create_filter_layout())   
        tabSimulator.setLayout(self.create_simulator_layout())   
        
        self.setCentralWidget(self.main_frame)
        
        
        
    def create_status_bar(self):
        self.status_text = QLabel("This is a demo")
        self.statusBar().addWidget(self.status_text, 1)
        
        
    def create_menu(self):        
        self.file_menu = self.menuBar().addMenu("&File")
        
        load_file_action = self.create_action("&Save plot",
            shortcut="Ctrl+S", slot=self.save_plot, 
            tip="Save the plot")
        quit_action = self.create_action("&Quit", slot=self.close, 
            shortcut="Ctrl+Q", tip="Close the application")
        
        self.add_actions(self.file_menu, 
            (load_file_action, None, quit_action))
        
        self.help_menu = self.menuBar().addMenu("&Help")
        about_action = self.create_action("&About", 
            shortcut='F1', slot=self.on_about, 
            tip='About the demo')
        
        self.add_actions(self.help_menu, (about_action,))
        
        
    def create_toolbar(self):
        exitAction = QAction(QIcon('images/exit_48.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(qApp.quit)
        exitAction.setToolTip('Exit Libre-FDATool')
        
        exportAction = QAction(QIcon('images/export_48.png'), 'Export', self);
        exportAction.setShortcut('Ctrl+E');
        exportAction.triggered.connect(self.exportHDL)
        exportAction.setToolTip('Export HDL Code')
        
        simulatorAction = QAction(QIcon('images/run_48.png'), 'Run', self);
        simulatorAction.setShortcut('Ctrl+E');
        simulatorAction.triggered.connect(self.runSimulator)
        simulatorAction.setToolTip('Run the simulation')

        self.toolbar = self.addToolBar('Stimulus')
        self.toolbar.addAction(exitAction)
        self.toolbar.addAction(exportAction)
        self.toolbar.addAction(simulatorAction)
        
        #self.toolbar = self.addToolBar('Filter')
        #self.toolbar.addAction(exitAction)
        


    def add_actions(self, target, actions):
        for action in actions:
            if action is None:
                target.addSeparator()
            else:
                target.addAction(action)


    def create_action(  self, text, slot=None, shortcut=None, 
                        icon=None, tip=None, checkable=False, 
                        signal="triggered()"):
        action = QAction(text, self)
        if icon is not None:
            action.setIcon(QIcon(":/%s.png" % icon))
        if shortcut is not None:
            action.setShortcut(shortcut)
        if tip is not None:
            action.setToolTip(tip)
            action.setStatusTip(tip)
        if slot is not None:
            self.connect(action, SIGNAL(signal), slot)
        if checkable:
            action.setCheckable(True)
        return action


    def runSimulator(self):
        
        # TODO: Simulator takes the configuration values even 
        # if they are not good!! We make a warning... something cleaner?
        
        if self.pbUpdateFilter.isEnabled():
            # The filter parameters are not updated!!
            self.status_text.setText('Filter parameters update required')
            self.main_frame.setCurrentIndex(1)
            QMessageBox.warning(self, 'Filter not updated', 
                                    'Filter parameters need to be updated',
                                    QMessageBox.Ok)
        else:
        
            self.status_text.setText('Running the simulation...')
            self.status_text.repaint()
        
            busX=[int(self.spinBoxInputP.value()), 
                  int(self.spinBoxInputQ.value())] 
        
            busY=[int(self.spinBoxOutputP.value()), 
                  int(self.spinBoxOutputQ.value())]
        
            busC=[int(self.spinBoxCoefficientP.value()), 
                  int(self.spinBoxCoefficientQ.value())]
        
            scalingX = 1
            scalingC = 1
        
248 249 250
            self.execute_simulation(self.b, self.a, self.x,  
                          str(self.textFilterName.text()), str(self.comboSimulatorLanguage.currentText()),
                          str(self.comboSimulatorEngine.currentText()), str(self.comboFilterStructure.currentText()),
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
                          busX, busY, busC, 
                          scalingX, scalingC)

            self.on_simulator_draw()
            self.main_frame.setCurrentIndex(2)

            self.status_text.setText('Successful Simulation!!!')
        

    def exportHDL(self):
        #QMessageBox.information(self, 'Export HDL', 
        #                        'This is only a message', QMessageBox.Ok)
        self.status_text.setText('Filter export tool has been launched')

    def on_stimulus_updated(self):
        #QMessageBox.information(self, 'Stimulus Updated', 
        #                        'This is only a message', QMessageBox.Ok)
        self.status_text.setText('Stimulus configuration has been updated')
    
    def on_filter_updated(self):
        #QMessageBox.information(self, 'Filter Updated', 
        #                        'This is only a message', QMessageBox.Ok)
        self.status_text.setText('Filter configuration has been updated')
        
    def on_simulator_updated(self):
        #QMessageBox.information(self, 'Simulator Updated', 
        #                        'This is only a message', QMessageBox.Ok)
        self.status_text.setText('Simulator configuration has been updated')
279 280 281 282 283 284 285 286 287 288
    
    def on_simulator_begin(self):
        #QMessageBox.information(self, 'Simulator Begin', 
        #                        'This is only a message', QMessageBox.Ok)
        self.status_text.setText('Simulation started...')
        
    def on_simulator_end(self):
        #QMessageBox.information(self, 'Simulator End', 
        #                        'This is only a message', QMessageBox.Ok)
        self.status_text.setText('Simulation ended!!')
289 290 291 292 293 294 295 296 297 298 299

def main():
    app = QApplication(sys.argv)
    form = LibreFDATool()
    form.show()
    app.exec_()


if __name__ == "__main__":
    main()