Lab3_Gui code
Dependencies: mbed
mbed_beat_new.py@1:faa8aa177069, 2017-09-21 (annotated)
- Committer:
- dogcatfee
- Date:
- Thu Sep 21 22:16:15 2017 -0700
- Revision:
- 1:faa8aa177069
Add python GUI program file
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dogcatfee | 1:faa8aa177069 | 1 | #*****************************************************************# |
dogcatfee | 1:faa8aa177069 | 2 | # # |
dogcatfee | 1:faa8aa177069 | 3 | # File: beatfactorygui.py # |
dogcatfee | 1:faa8aa177069 | 4 | # Author: Ethan Takla # |
dogcatfee | 1:faa8aa177069 | 5 | # Date Created: 10/6/14 # |
dogcatfee | 1:faa8aa177069 | 6 | # Description: A python GUI for graphically creating songs on # |
dogcatfee | 1:faa8aa177069 | 7 | # the FDRM-KL46z # |
dogcatfee | 1:faa8aa177069 | 8 | # # |
dogcatfee | 1:faa8aa177069 | 9 | #*****************************************************************# |
dogcatfee | 1:faa8aa177069 | 10 | # Last edit: Matthew Sessions |
dogcatfee | 1:faa8aa177069 | 11 | |
dogcatfee | 1:faa8aa177069 | 12 | #System Imports |
dogcatfee | 1:faa8aa177069 | 13 | import sys |
dogcatfee | 1:faa8aa177069 | 14 | |
dogcatfee | 1:faa8aa177069 | 15 | #Serial Imports |
dogcatfee | 1:faa8aa177069 | 16 | import serial |
dogcatfee | 1:faa8aa177069 | 17 | from serial.tools import list_ports |
dogcatfee | 1:faa8aa177069 | 18 | |
dogcatfee | 1:faa8aa177069 | 19 | #Signal Imports |
dogcatfee | 1:faa8aa177069 | 20 | import signal |
dogcatfee | 1:faa8aa177069 | 21 | |
dogcatfee | 1:faa8aa177069 | 22 | #Sound Imports |
dogcatfee | 1:faa8aa177069 | 23 | |
dogcatfee | 1:faa8aa177069 | 24 | # Uncomment for sound |
dogcatfee | 1:faa8aa177069 | 25 | #import winsound |
dogcatfee | 1:faa8aa177069 | 26 | |
dogcatfee | 1:faa8aa177069 | 27 | #Threading imports |
dogcatfee | 1:faa8aa177069 | 28 | import thread |
dogcatfee | 1:faa8aa177069 | 29 | |
dogcatfee | 1:faa8aa177069 | 30 | #PyQt Imports |
dogcatfee | 1:faa8aa177069 | 31 | from PyQt4.QtGui import QApplication, QDialog, QMainWindow, QFileDialog |
dogcatfee | 1:faa8aa177069 | 32 | from PyQt4 import QtCore, QtGui |
dogcatfee | 1:faa8aa177069 | 33 | |
dogcatfee | 1:faa8aa177069 | 34 | |
dogcatfee | 1:faa8aa177069 | 35 | try: |
dogcatfee | 1:faa8aa177069 | 36 | _fromUtf8 = QtCore.QString.fromUtf8 |
dogcatfee | 1:faa8aa177069 | 37 | except AttributeError: |
dogcatfee | 1:faa8aa177069 | 38 | def _fromUtf8(s): |
dogcatfee | 1:faa8aa177069 | 39 | return s |
dogcatfee | 1:faa8aa177069 | 40 | |
dogcatfee | 1:faa8aa177069 | 41 | #Setup text encoding |
dogcatfee | 1:faa8aa177069 | 42 | try: |
dogcatfee | 1:faa8aa177069 | 43 | _encoding = QtGui.QApplication.UnicodeUTF8 |
dogcatfee | 1:faa8aa177069 | 44 | def _translate(context, text, disambig): |
dogcatfee | 1:faa8aa177069 | 45 | return QtGui.QApplication.translate(context, text, disambig, _encoding) |
dogcatfee | 1:faa8aa177069 | 46 | except AttributeError: |
dogcatfee | 1:faa8aa177069 | 47 | def _translate(context, text, disambig): |
dogcatfee | 1:faa8aa177069 | 48 | return QtGui.QApplication.translate(context, text, disambig) |
dogcatfee | 1:faa8aa177069 | 49 | |
dogcatfee | 1:faa8aa177069 | 50 | #Create Note Classs |
dogcatfee | 1:faa8aa177069 | 51 | class Note: |
dogcatfee | 1:faa8aa177069 | 52 | def __init__( self, note ): |
dogcatfee | 1:faa8aa177069 | 53 | self.note = note |
dogcatfee | 1:faa8aa177069 | 54 | |
dogcatfee | 1:faa8aa177069 | 55 | #Main Window Class |
dogcatfee | 1:faa8aa177069 | 56 | class Ui_MainWindow(QtGui.QMainWindow): |
dogcatfee | 1:faa8aa177069 | 57 | |
dogcatfee | 1:faa8aa177069 | 58 | def initializeUI(self, MainWindow): |
dogcatfee | 1:faa8aa177069 | 59 | |
dogcatfee | 1:faa8aa177069 | 60 | #Initialize main window properties |
dogcatfee | 1:faa8aa177069 | 61 | MainWindow.setObjectName(_fromUtf8("FDRM-KL46z Beat Factory")) |
dogcatfee | 1:faa8aa177069 | 62 | MainWindow.resize(1200, 848) |
dogcatfee | 1:faa8aa177069 | 63 | MainWindow.setMaximumSize(QtCore.QSize(16777215, 848)) |
dogcatfee | 1:faa8aa177069 | 64 | |
dogcatfee | 1:faa8aa177069 | 65 | #Create central widget |
dogcatfee | 1:faa8aa177069 | 66 | self.centralwidget = QtGui.QWidget(MainWindow) |
dogcatfee | 1:faa8aa177069 | 67 | self.centralwidget.setObjectName(_fromUtf8("centralwidget")) |
dogcatfee | 1:faa8aa177069 | 68 | |
dogcatfee | 1:faa8aa177069 | 69 | #Create gridLayout_2 |
dogcatfee | 1:faa8aa177069 | 70 | self.gridLayout_2 = QtGui.QGridLayout(self.centralwidget) |
dogcatfee | 1:faa8aa177069 | 71 | self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2")) |
dogcatfee | 1:faa8aa177069 | 72 | |
dogcatfee | 1:faa8aa177069 | 73 | #Create and format a graphics view widget |
dogcatfee | 1:faa8aa177069 | 74 | self.graphicsView = QtGui.QGraphicsView(self.centralwidget) |
dogcatfee | 1:faa8aa177069 | 75 | self.graphicsView.setMinimumSize(QtCore.QSize(300, 120)) |
dogcatfee | 1:faa8aa177069 | 76 | self.graphicsView.setMaximumSize(QtCore.QSize(300, 120)) |
dogcatfee | 1:faa8aa177069 | 77 | self.graphicsView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) |
dogcatfee | 1:faa8aa177069 | 78 | self.graphicsView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) |
dogcatfee | 1:faa8aa177069 | 79 | |
dogcatfee | 1:faa8aa177069 | 80 | #Set the graphics view image |
dogcatfee | 1:faa8aa177069 | 81 | self.scene = QtGui.QGraphicsScene(); |
dogcatfee | 1:faa8aa177069 | 82 | self.pixmap = QtGui.QPixmap( "TekBots.png" ) |
dogcatfee | 1:faa8aa177069 | 83 | self.scene.addPixmap(self.pixmap) |
dogcatfee | 1:faa8aa177069 | 84 | self.graphicsView.setScene( self.scene ) |
dogcatfee | 1:faa8aa177069 | 85 | self.graphicsView.setObjectName(_fromUtf8("graphicsView")) |
dogcatfee | 1:faa8aa177069 | 86 | |
dogcatfee | 1:faa8aa177069 | 87 | #Add the graphics view to gridLayout_2 |
dogcatfee | 1:faa8aa177069 | 88 | self.gridLayout_2.addWidget(self.graphicsView, 0, 1, 1, 1) |
dogcatfee | 1:faa8aa177069 | 89 | |
dogcatfee | 1:faa8aa177069 | 90 | #Create and format verticalLayout_2 |
dogcatfee | 1:faa8aa177069 | 91 | self.verticalLayout_2 = QtGui.QVBoxLayout() |
dogcatfee | 1:faa8aa177069 | 92 | self.verticalLayout_2.setSpacing(7) |
dogcatfee | 1:faa8aa177069 | 93 | self.verticalLayout_2.setSizeConstraint(QtGui.QLayout.SetNoConstraint) |
dogcatfee | 1:faa8aa177069 | 94 | self.verticalLayout_2.setContentsMargins(-1, -1, -1, 0) |
dogcatfee | 1:faa8aa177069 | 95 | self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2")) |
dogcatfee | 1:faa8aa177069 | 96 | |
dogcatfee | 1:faa8aa177069 | 97 | #Create a table view for beat editing |
dogcatfee | 1:faa8aa177069 | 98 | self.tableView = QtGui.QTableWidget(self.centralwidget) |
dogcatfee | 1:faa8aa177069 | 99 | |
dogcatfee | 1:faa8aa177069 | 100 | #Add the table view to verticalLayout2 |
dogcatfee | 1:faa8aa177069 | 101 | self.verticalLayout_2.addWidget(self.tableView) |
dogcatfee | 1:faa8aa177069 | 102 | |
dogcatfee | 1:faa8aa177069 | 103 | #Add verticalLayout2 to gridLayout2 |
dogcatfee | 1:faa8aa177069 | 104 | self.gridLayout_2.addLayout(self.verticalLayout_2, 0, 0, 7, 1) |
dogcatfee | 1:faa8aa177069 | 105 | |
dogcatfee | 1:faa8aa177069 | 106 | #Create gridLayout |
dogcatfee | 1:faa8aa177069 | 107 | self.gridLayout = QtGui.QGridLayout() |
dogcatfee | 1:faa8aa177069 | 108 | self.gridLayout.setObjectName(_fromUtf8("gridLayout")) |
dogcatfee | 1:faa8aa177069 | 109 | |
dogcatfee | 1:faa8aa177069 | 110 | #Create and format pushButton, add them to gridLayout |
dogcatfee | 1:faa8aa177069 | 111 | self.pushButton = QtGui.QPushButton(self.centralwidget) |
dogcatfee | 1:faa8aa177069 | 112 | self.pushButton.setObjectName(_fromUtf8("pushButton")) |
dogcatfee | 1:faa8aa177069 | 113 | self.pushButton.setStyleSheet("color: black; background-color: red;") |
dogcatfee | 1:faa8aa177069 | 114 | self.gridLayout.addWidget(self.pushButton, 0, 2, 1, 1) |
dogcatfee | 1:faa8aa177069 | 115 | self.pushButton_2 = QtGui.QPushButton(self.centralwidget) |
dogcatfee | 1:faa8aa177069 | 116 | self.pushButton_2.setObjectName(_fromUtf8("pushButton_2")) |
dogcatfee | 1:faa8aa177069 | 117 | self.pushButton_2.setEnabled( False ) |
dogcatfee | 1:faa8aa177069 | 118 | self.gridLayout.addWidget(self.pushButton_2, 3, 0, 1, 1) |
dogcatfee | 1:faa8aa177069 | 119 | self.pushButton_3 = QtGui.QPushButton(self.centralwidget) |
dogcatfee | 1:faa8aa177069 | 120 | self.pushButton_3.setObjectName(_fromUtf8("pushButton_3")) |
dogcatfee | 1:faa8aa177069 | 121 | self.pushButton_3.setEnabled( False ) |
dogcatfee | 1:faa8aa177069 | 122 | self.gridLayout.addWidget(self.pushButton_3, 3, 1, 1, 1) |
dogcatfee | 1:faa8aa177069 | 123 | |
dogcatfee | 1:faa8aa177069 | 124 | #Create labels and add them to gridLayout |
dogcatfee | 1:faa8aa177069 | 125 | self.label = QtGui.QLabel(self.centralwidget) |
dogcatfee | 1:faa8aa177069 | 126 | self.label.setObjectName(_fromUtf8("label")) |
dogcatfee | 1:faa8aa177069 | 127 | self.gridLayout.addWidget(self.label, 0, 0, 1, 1) |
dogcatfee | 1:faa8aa177069 | 128 | self.label2 = QtGui.QLabel(self.centralwidget) |
dogcatfee | 1:faa8aa177069 | 129 | self.label2.setObjectName(_fromUtf8("label2")) |
dogcatfee | 1:faa8aa177069 | 130 | self.gridLayout.addWidget(self.label2, 1, 0, 1, 1) |
dogcatfee | 1:faa8aa177069 | 131 | self.label3 = QtGui.QLabel(self.centralwidget) |
dogcatfee | 1:faa8aa177069 | 132 | self.label3.setObjectName(_fromUtf8("label3")) |
dogcatfee | 1:faa8aa177069 | 133 | self.gridLayout.addWidget(self.label3, 2, 0, 1, 1) |
dogcatfee | 1:faa8aa177069 | 134 | |
dogcatfee | 1:faa8aa177069 | 135 | #Create comboBoxs and add them to gridLayout |
dogcatfee | 1:faa8aa177069 | 136 | self.comboBox = QtGui.QComboBox(self.centralwidget) |
dogcatfee | 1:faa8aa177069 | 137 | self.comboBox.setObjectName(_fromUtf8("comboBox")) |
dogcatfee | 1:faa8aa177069 | 138 | self.gridLayout.addWidget(self.comboBox, 0, 1, 1, 1) |
dogcatfee | 1:faa8aa177069 | 139 | self.comboBox2 = QtGui.QComboBox(self.centralwidget) |
dogcatfee | 1:faa8aa177069 | 140 | self.comboBox2.setObjectName(_fromUtf8("comboBox2")) |
dogcatfee | 1:faa8aa177069 | 141 | self.gridLayout.addWidget(self.comboBox2, 1, 1, 1, 1) |
dogcatfee | 1:faa8aa177069 | 142 | self.comboBox3 = QtGui.QComboBox(self.centralwidget) |
dogcatfee | 1:faa8aa177069 | 143 | self.comboBox3.setObjectName(_fromUtf8("comboBox3")) |
dogcatfee | 1:faa8aa177069 | 144 | self.gridLayout.addWidget(self.comboBox3, 2, 1, 1, 1) |
dogcatfee | 1:faa8aa177069 | 145 | |
dogcatfee | 1:faa8aa177069 | 146 | #Add gridLayout to gridLayout_2 and format |
dogcatfee | 1:faa8aa177069 | 147 | self.gridLayout_2.addLayout(self.gridLayout, 1, 1, 1, 1) |
dogcatfee | 1:faa8aa177069 | 148 | MainWindow.setCentralWidget(self.centralwidget) |
dogcatfee | 1:faa8aa177069 | 149 | |
dogcatfee | 1:faa8aa177069 | 150 | #Create and configure the menu bar |
dogcatfee | 1:faa8aa177069 | 151 | self.menubar = QtGui.QMenuBar(MainWindow) |
dogcatfee | 1:faa8aa177069 | 152 | self.menubar.setGeometry(QtCore.QRect(0, 0, 972, 26)) |
dogcatfee | 1:faa8aa177069 | 153 | self.menubar.setObjectName(_fromUtf8("menubar")) |
dogcatfee | 1:faa8aa177069 | 154 | self.menuFile = QtGui.QMenu(self.menubar) |
dogcatfee | 1:faa8aa177069 | 155 | self.menuFile.setObjectName(_fromUtf8("menuFile")) |
dogcatfee | 1:faa8aa177069 | 156 | self.menuHelp = QtGui.QMenu(self.menubar) |
dogcatfee | 1:faa8aa177069 | 157 | self.menuHelp.setObjectName(_fromUtf8("menuHelp")) |
dogcatfee | 1:faa8aa177069 | 158 | MainWindow.setMenuBar(self.menubar) |
dogcatfee | 1:faa8aa177069 | 159 | self.statusbar = QtGui.QStatusBar(MainWindow) |
dogcatfee | 1:faa8aa177069 | 160 | self.statusbar.setObjectName(_fromUtf8("statusbar")) |
dogcatfee | 1:faa8aa177069 | 161 | MainWindow.setStatusBar(self.statusbar) |
dogcatfee | 1:faa8aa177069 | 162 | |
dogcatfee | 1:faa8aa177069 | 163 | #Create a QAction for the Load and Save songs, and About button |
dogcatfee | 1:faa8aa177069 | 164 | self.actionLoad_Song = QtGui.QAction(MainWindow) |
dogcatfee | 1:faa8aa177069 | 165 | self.actionLoad_Song.setObjectName(_fromUtf8("actionLoad_Song")) |
dogcatfee | 1:faa8aa177069 | 166 | self.actionSave_Song = QtGui.QAction(MainWindow) |
dogcatfee | 1:faa8aa177069 | 167 | self.actionSave_Song.setObjectName(_fromUtf8("actionSave_Song")) |
dogcatfee | 1:faa8aa177069 | 168 | self.actionAbout = QtGui.QAction(MainWindow) |
dogcatfee | 1:faa8aa177069 | 169 | self.actionAbout.setObjectName(_fromUtf8("actionAbout")) |
dogcatfee | 1:faa8aa177069 | 170 | self.menuFile.addAction(self.actionLoad_Song) |
dogcatfee | 1:faa8aa177069 | 171 | self.menuFile.addAction(self.actionSave_Song) |
dogcatfee | 1:faa8aa177069 | 172 | self.menuHelp.addAction(self.actionAbout) |
dogcatfee | 1:faa8aa177069 | 173 | self.menubar.addAction(self.menuFile.menuAction()) |
dogcatfee | 1:faa8aa177069 | 174 | self.menubar.addAction(self.menuHelp.menuAction()) |
dogcatfee | 1:faa8aa177069 | 175 | |
dogcatfee | 1:faa8aa177069 | 176 | #Set text of all UI elements |
dogcatfee | 1:faa8aa177069 | 177 | self.retranslateUI(MainWindow) |
dogcatfee | 1:faa8aa177069 | 178 | |
dogcatfee | 1:faa8aa177069 | 179 | #Connect Slots |
dogcatfee | 1:faa8aa177069 | 180 | QtCore.QMetaObject.connectSlotsByName(MainWindow) |
dogcatfee | 1:faa8aa177069 | 181 | |
dogcatfee | 1:faa8aa177069 | 182 | #Populate comboBox3 and configure tempo |
dogcatfee | 1:faa8aa177069 | 183 | self.comboBox3.clear() |
dogcatfee | 1:faa8aa177069 | 184 | self.comboBox3.addItems(self.tempoValues) |
dogcatfee | 1:faa8aa177069 | 185 | self.comboBox3.setCurrentIndex( 14 ) |
dogcatfee | 1:faa8aa177069 | 186 | self.tempo = 120 |
dogcatfee | 1:faa8aa177069 | 187 | |
dogcatfee | 1:faa8aa177069 | 188 | #Populate Combobox 2 |
dogcatfee | 1:faa8aa177069 | 189 | self.comboBox2.clear() |
dogcatfee | 1:faa8aa177069 | 190 | self.comboBox2.addItems(self.noteNumberValues) |
dogcatfee | 1:faa8aa177069 | 191 | |
dogcatfee | 1:faa8aa177069 | 192 | def retranslateUI(self, MainWindow): |
dogcatfee | 1:faa8aa177069 | 193 | |
dogcatfee | 1:faa8aa177069 | 194 | #Set the text of the UI elements |
dogcatfee | 1:faa8aa177069 | 195 | MainWindow.setWindowTitle(_translate("FDRM-KL46z Beat Factory", "FDRM-KL46z Beat Factory", None)) |
dogcatfee | 1:faa8aa177069 | 196 | self.pushButton.setText(_translate("FDRM-KL46z Beat Factory", "Scan", None)) |
dogcatfee | 1:faa8aa177069 | 197 | self.pushButton_2.setText(_translate("FDRM-KL46z Beat Factory", "Play", None)) |
dogcatfee | 1:faa8aa177069 | 198 | self.pushButton_3.setText(_translate("FDRM-KL46z Beat Factory", "Clear", None)) |
dogcatfee | 1:faa8aa177069 | 199 | self.label.setText(_translate("FDRM-KL46z Beat Factory", "COM Port:", None)) |
dogcatfee | 1:faa8aa177069 | 200 | self.label2.setText(_translate("FDRM-KL46z Beat Factory", "Number of Notes:", None)) |
dogcatfee | 1:faa8aa177069 | 201 | self.label3.setText(_translate("FDRM-KL46z Beat Factory", "Tempo:", None)) |
dogcatfee | 1:faa8aa177069 | 202 | self.menuFile.setTitle(_translate("FDRM-KL46z Beat Factory", "File", None)) |
dogcatfee | 1:faa8aa177069 | 203 | self.menuHelp.setTitle(_translate("FDRM-KL46z Beat Factory", "Help", None)) |
dogcatfee | 1:faa8aa177069 | 204 | self.actionLoad_Song.setText(_translate("FDRM-KL46z Beat Factory", "Load Song", None)) |
dogcatfee | 1:faa8aa177069 | 205 | self.actionSave_Song.setText(_translate("FDRM-KL46z Beat Factory", "Save Song", None)) |
dogcatfee | 1:faa8aa177069 | 206 | self.actionAbout.setText(_translate("FDRM-KL46z Beat Factory", "About", None)) |
dogcatfee | 1:faa8aa177069 | 207 | |
dogcatfee | 1:faa8aa177069 | 208 | def initializeVariables( self ): |
dogcatfee | 1:faa8aa177069 | 209 | |
dogcatfee | 1:faa8aa177069 | 210 | #Define cell selected/unselected variables |
dogcatfee | 1:faa8aa177069 | 211 | self.cellSelected = 1 |
dogcatfee | 1:faa8aa177069 | 212 | self.cellUnselected = 0 |
dogcatfee | 1:faa8aa177069 | 213 | |
dogcatfee | 1:faa8aa177069 | 214 | #Set initial comm. state |
dogcatfee | 1:faa8aa177069 | 215 | self.comButtonState = "Scan" |
dogcatfee | 1:faa8aa177069 | 216 | |
dogcatfee | 1:faa8aa177069 | 217 | #Create the song array |
dogcatfee | 1:faa8aa177069 | 218 | self.song_array = [] # delete all note elements |
dogcatfee | 1:faa8aa177069 | 219 | |
dogcatfee | 1:faa8aa177069 | 220 | #Set table row and column count variables |
dogcatfee | 1:faa8aa177069 | 221 | self.tableViewRowCount = 24 |
dogcatfee | 1:faa8aa177069 | 222 | self.tableViewColumnCount = 16 |
dogcatfee | 1:faa8aa177069 | 223 | self.selectedCells = [[0 for x in xrange(self.tableViewColumnCount)] for x in xrange(self.tableViewRowCount)] |
dogcatfee | 1:faa8aa177069 | 224 | self.noteFrequencies = [ 831, 784, 740, 698, 659, 622, 587, 554, 523, 494, 466, 440, 415, 392, 370, 349, 330, 311, 294, 277, 261, 247, 233, 220 ] |
dogcatfee | 1:faa8aa177069 | 225 | |
dogcatfee | 1:faa8aa177069 | 226 | #Define tempo values |
dogcatfee | 1:faa8aa177069 | 227 | self.tempoValues = [ "50", "55", "60", "65", "70", "75", "80", "85", "90", "95", "100", "105", "110", "115", "120", "125", "130", |
dogcatfee | 1:faa8aa177069 | 228 | "135", "140", "145", "150", "155", "160", "165", "170", "175", "180", "185", "190", "195", "200", "205", |
dogcatfee | 1:faa8aa177069 | 229 | "210", "215", "220", "225", "230", "235", "240"] |
dogcatfee | 1:faa8aa177069 | 230 | |
dogcatfee | 1:faa8aa177069 | 231 | #Define note number values |
dogcatfee | 1:faa8aa177069 | 232 | self.noteNumberValues = [ "16", "32", "48", "64" ] |
dogcatfee | 1:faa8aa177069 | 233 | |
dogcatfee | 1:faa8aa177069 | 234 | #Define serial ports that are to be 'detected' |
dogcatfee | 1:faa8aa177069 | 235 | self.ports = [ "Select", |
dogcatfee | 1:faa8aa177069 | 236 | 'COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9', 'COM10', |
dogcatfee | 1:faa8aa177069 | 237 | 'COM11', 'COM12', 'COM13', 'COM14', 'COM15', 'COM16', 'COM17', 'COM18', |
dogcatfee | 1:faa8aa177069 | 238 | 'COM19', 'COM20', 'COM21', '/dev/ttyACM0', '/dev/ttyACM1', '/dev/ttyACM2', '/dev/ttyACM3'] |
dogcatfee | 1:faa8aa177069 | 239 | |
dogcatfee | 1:faa8aa177069 | 240 | |
dogcatfee | 1:faa8aa177069 | 241 | |
dogcatfee | 1:faa8aa177069 | 242 | def initializeTable( self ): |
dogcatfee | 1:faa8aa177069 | 243 | |
dogcatfee | 1:faa8aa177069 | 244 | #Initialize beat table columns and rows |
dogcatfee | 1:faa8aa177069 | 245 | self.tableView.setColumnCount(self.tableViewColumnCount) |
dogcatfee | 1:faa8aa177069 | 246 | self.tableView.setRowCount(self.tableViewRowCount) |
dogcatfee | 1:faa8aa177069 | 247 | |
dogcatfee | 1:faa8aa177069 | 248 | #Initialize beat table rows for notes |
dogcatfee | 1:faa8aa177069 | 249 | self.verticalHeaderLabels = [ "G#", "G", "F#", "F", "E", "D#", "D", "C#", "C(High)", "B", "A#", "A", "G#", "G", "F#", "F", "E", "D#", "D", "C#", "C (Middle)", "B", "A#", "A" ] |
dogcatfee | 1:faa8aa177069 | 250 | |
dogcatfee | 1:faa8aa177069 | 251 | #Label vertical headers on table |
dogcatfee | 1:faa8aa177069 | 252 | self.tableView.setVerticalHeaderLabels( self.verticalHeaderLabels ) |
dogcatfee | 1:faa8aa177069 | 253 | |
dogcatfee | 1:faa8aa177069 | 254 | #Make table ready only by creating items widgets in each cell and disabling them |
dogcatfee | 1:faa8aa177069 | 255 | for i in xrange(self.tableView.rowCount()): |
dogcatfee | 1:faa8aa177069 | 256 | for j in xrange(self.tableView.columnCount()): |
dogcatfee | 1:faa8aa177069 | 257 | self.tableView.setItem(i,j, QtGui.QTableWidgetItem()) |
dogcatfee | 1:faa8aa177069 | 258 | self.tableView.item(i,j).setFlags(QtCore.Qt.ItemIsEnabled) |
dogcatfee | 1:faa8aa177069 | 259 | |
dogcatfee | 1:faa8aa177069 | 260 | def initializeSerial( self ): |
dogcatfee | 1:faa8aa177069 | 261 | |
dogcatfee | 1:faa8aa177069 | 262 | #Configure the serial port for 8-N-1, baudrate doesn't matter since the FDRM-KL46z is running a VCP |
dogcatfee | 1:faa8aa177069 | 263 | self.ser = serial.Serial() |
dogcatfee | 1:faa8aa177069 | 264 | self.ser.timeout = 1000 |
dogcatfee | 1:faa8aa177069 | 265 | self.ser.baudrate = 9600 |
dogcatfee | 1:faa8aa177069 | 266 | |
dogcatfee | 1:faa8aa177069 | 267 | def highlightColumn( self, column ): |
dogcatfee | 1:faa8aa177069 | 268 | |
dogcatfee | 1:faa8aa177069 | 269 | #Go through the unselected cells in each row and highlight them |
dogcatfee | 1:faa8aa177069 | 270 | for i in xrange(self.tableView.rowCount()): |
dogcatfee | 1:faa8aa177069 | 271 | if self.selectedCells[i][column] == self.cellUnselected: |
dogcatfee | 1:faa8aa177069 | 272 | self.tableView.item(i, column).setBackground(QtGui.QColor(255,179,0)) |
dogcatfee | 1:faa8aa177069 | 273 | |
dogcatfee | 1:faa8aa177069 | 274 | def unhighlightColumn( self, column ): |
dogcatfee | 1:faa8aa177069 | 275 | |
dogcatfee | 1:faa8aa177069 | 276 | #Go through the unselected cells in each row and unhighlight them |
dogcatfee | 1:faa8aa177069 | 277 | for i in xrange(self.tableView.rowCount()): |
dogcatfee | 1:faa8aa177069 | 278 | if self.selectedCells[i][column] == self.cellUnselected: |
dogcatfee | 1:faa8aa177069 | 279 | self.tableView.item(i, column).setBackground(QtGui.QColor(255,255,255)) |
dogcatfee | 1:faa8aa177069 | 280 | |
dogcatfee | 1:faa8aa177069 | 281 | def handlePlayButton( self ): |
dogcatfee | 1:faa8aa177069 | 282 | |
dogcatfee | 1:faa8aa177069 | 283 | #Disable play and clear buttons while song is playing |
dogcatfee | 1:faa8aa177069 | 284 | self.pushButton_2.setEnabled( False ) |
dogcatfee | 1:faa8aa177069 | 285 | self.pushButton_3.setEnabled( False ) |
dogcatfee | 1:faa8aa177069 | 286 | |
dogcatfee | 1:faa8aa177069 | 287 | #Empty song array |
dogcatfee | 1:faa8aa177069 | 288 | del self.song_array[:] |
dogcatfee | 1:faa8aa177069 | 289 | emptyCells = 0 |
dogcatfee | 1:faa8aa177069 | 290 | |
dogcatfee | 1:faa8aa177069 | 291 | |
dogcatfee | 1:faa8aa177069 | 292 | for j in xrange(self.tableView.columnCount()): |
dogcatfee | 1:faa8aa177069 | 293 | for i in xrange(self.tableView.rowCount()): |
dogcatfee | 1:faa8aa177069 | 294 | |
dogcatfee | 1:faa8aa177069 | 295 | #If a cell is selected, add the corresponding note to the song array |
dogcatfee | 1:faa8aa177069 | 296 | if self.selectedCells[i][j] == self.cellSelected: |
dogcatfee | 1:faa8aa177069 | 297 | |
dogcatfee | 1:faa8aa177069 | 298 | #Store the note index |
dogcatfee | 1:faa8aa177069 | 299 | note_name = i # note name is letter A-G |
dogcatfee | 1:faa8aa177069 | 300 | |
dogcatfee | 1:faa8aa177069 | 301 | # create Note class and populate |
dogcatfee | 1:faa8aa177069 | 302 | note = Note( note_name ) |
dogcatfee | 1:faa8aa177069 | 303 | self.song_array.append( note ) # add Note to array of Notes |
dogcatfee | 1:faa8aa177069 | 304 | else: |
dogcatfee | 1:faa8aa177069 | 305 | emptyCells += 1 |
dogcatfee | 1:faa8aa177069 | 306 | |
dogcatfee | 1:faa8aa177069 | 307 | #If an empty column is found, add a rest (index 24) to the song array |
dogcatfee | 1:faa8aa177069 | 308 | if emptyCells == self.tableView.rowCount(): |
dogcatfee | 1:faa8aa177069 | 309 | |
dogcatfee | 1:faa8aa177069 | 310 | # store user-inputted note into a Note class |
dogcatfee | 1:faa8aa177069 | 311 | note_name = 24 # note name is letter A-G |
dogcatfee | 1:faa8aa177069 | 312 | |
dogcatfee | 1:faa8aa177069 | 313 | # create Note class and populate |
dogcatfee | 1:faa8aa177069 | 314 | note = Note( note_name ) |
dogcatfee | 1:faa8aa177069 | 315 | self.song_array.append( note ) # add Note to array of Notes |
dogcatfee | 1:faa8aa177069 | 316 | |
dogcatfee | 1:faa8aa177069 | 317 | emptyCells = 0 |
dogcatfee | 1:faa8aa177069 | 318 | |
dogcatfee | 1:faa8aa177069 | 319 | |
dogcatfee | 1:faa8aa177069 | 320 | try: |
dogcatfee | 1:faa8aa177069 | 321 | self.ser.open() |
dogcatfee | 1:faa8aa177069 | 322 | except: |
dogcatfee | 1:faa8aa177069 | 323 | self.serialScan() |
dogcatfee | 1:faa8aa177069 | 324 | return |
dogcatfee | 1:faa8aa177069 | 325 | self.ser.write( "$NEW\n" ) #Send new song packet |
dogcatfee | 1:faa8aa177069 | 326 | self.ser.write( "$T" ) #Send Tempo Packet |
dogcatfee | 1:faa8aa177069 | 327 | self.ser.write( "%d" % (self.tempo*4) ) |
dogcatfee | 1:faa8aa177069 | 328 | self.ser.write( '\n' ) |
dogcatfee | 1:faa8aa177069 | 329 | self.ser.write( "$L" ) #Send song length packet |
dogcatfee | 1:faa8aa177069 | 330 | self.ser.write( "%d" % (self.tableView.columnCount() ) ) |
dogcatfee | 1:faa8aa177069 | 331 | self.ser.write( '\n' ) |
dogcatfee | 1:faa8aa177069 | 332 | for i in range(self.tableView.columnCount()): |
dogcatfee | 1:faa8aa177069 | 333 | self.ser.write( "$S" ) #Send note data packet |
dogcatfee | 1:faa8aa177069 | 334 | self.ser.write( "%d" % (self.song_array[i].note) ) |
dogcatfee | 1:faa8aa177069 | 335 | self.ser.write( '\n' ) |
dogcatfee | 1:faa8aa177069 | 336 | self.ser.write( "$PLAY\n" ) #Send play song packet |
dogcatfee | 1:faa8aa177069 | 337 | |
dogcatfee | 1:faa8aa177069 | 338 | data = 0 |
dogcatfee | 1:faa8aa177069 | 339 | |
dogcatfee | 1:faa8aa177069 | 340 | while 1: |
dogcatfee | 1:faa8aa177069 | 341 | |
dogcatfee | 1:faa8aa177069 | 342 | #Wait for serial data to be available |
dogcatfee | 1:faa8aa177069 | 343 | if self.ser.inWaiting() != 0: |
dogcatfee | 1:faa8aa177069 | 344 | |
dogcatfee | 1:faa8aa177069 | 345 | #Read a line of data |
dogcatfee | 1:faa8aa177069 | 346 | data = int(self.ser.readline()) |
dogcatfee | 1:faa8aa177069 | 347 | |
dogcatfee | 1:faa8aa177069 | 348 | #Exit the while loop if a termination character (0xFF) is received |
dogcatfee | 1:faa8aa177069 | 349 | if data == 255: |
dogcatfee | 1:faa8aa177069 | 350 | break; |
dogcatfee | 1:faa8aa177069 | 351 | |
dogcatfee | 1:faa8aa177069 | 352 | #Highlight the column specified by the data |
dogcatfee | 1:faa8aa177069 | 353 | self.highlightColumn( data ) |
dogcatfee | 1:faa8aa177069 | 354 | |
dogcatfee | 1:faa8aa177069 | 355 | #Scroll to the column so we can see it |
dogcatfee | 1:faa8aa177069 | 356 | self.tableView.scrollToItem( self.tableView.item(0,data) ) |
dogcatfee | 1:faa8aa177069 | 357 | |
dogcatfee | 1:faa8aa177069 | 358 | #Unhighlight previous column |
dogcatfee | 1:faa8aa177069 | 359 | if data > 0: |
dogcatfee | 1:faa8aa177069 | 360 | self.unhighlightColumn( data - 1 ) |
dogcatfee | 1:faa8aa177069 | 361 | |
dogcatfee | 1:faa8aa177069 | 362 | #Force a GUI uptdate |
dogcatfee | 1:faa8aa177069 | 363 | QApplication.processEvents() |
dogcatfee | 1:faa8aa177069 | 364 | |
dogcatfee | 1:faa8aa177069 | 365 | self.unhighlightColumn( self.tableView.columnCount()-1 ) |
dogcatfee | 1:faa8aa177069 | 366 | self.ser.close() |
dogcatfee | 1:faa8aa177069 | 367 | |
dogcatfee | 1:faa8aa177069 | 368 | #Enable play and clear buttons after song is song playing |
dogcatfee | 1:faa8aa177069 | 369 | self.pushButton_2.setEnabled( True ) |
dogcatfee | 1:faa8aa177069 | 370 | self.pushButton_3.setEnabled( True ) |
dogcatfee | 1:faa8aa177069 | 371 | |
dogcatfee | 1:faa8aa177069 | 372 | def handleClearButton( self ): |
dogcatfee | 1:faa8aa177069 | 373 | |
dogcatfee | 1:faa8aa177069 | 374 | #Go through each cell, make it white, and set it as unselected |
dogcatfee | 1:faa8aa177069 | 375 | for i in xrange(self.tableView.rowCount()): |
dogcatfee | 1:faa8aa177069 | 376 | for j in xrange(self.tableView.columnCount()): |
dogcatfee | 1:faa8aa177069 | 377 | self.tableView.item(i, j).setBackground(QtGui.QColor(255,255,255)) |
dogcatfee | 1:faa8aa177069 | 378 | self.selectedCells[i][j] = self.cellUnselected |
dogcatfee | 1:faa8aa177069 | 379 | |
dogcatfee | 1:faa8aa177069 | 380 | def serialScan( self ): |
dogcatfee | 1:faa8aa177069 | 381 | |
dogcatfee | 1:faa8aa177069 | 382 | #Populate the combobox with ports |
dogcatfee | 1:faa8aa177069 | 383 | self.comboBox.clear() |
dogcatfee | 1:faa8aa177069 | 384 | self.comboBox.addItems( self.ports ) |
dogcatfee | 1:faa8aa177069 | 385 | |
dogcatfee | 1:faa8aa177069 | 386 | #Go through each port name to see if it exists |
dogcatfee | 1:faa8aa177069 | 387 | for i in self.ports[1:]: |
dogcatfee | 1:faa8aa177069 | 388 | self.ser.port = i |
dogcatfee | 1:faa8aa177069 | 389 | try: |
dogcatfee | 1:faa8aa177069 | 390 | self.ser.open() |
dogcatfee | 1:faa8aa177069 | 391 | except Exception, e: |
dogcatfee | 1:faa8aa177069 | 392 | |
dogcatfee | 1:faa8aa177069 | 393 | #If the port name does not exist, remove it from the combobox |
dogcatfee | 1:faa8aa177069 | 394 | remove_port = self.comboBox.findText( self.ser.port ) |
dogcatfee | 1:faa8aa177069 | 395 | self.comboBox.removeItem( remove_port ) |
dogcatfee | 1:faa8aa177069 | 396 | self.ser.close() |
dogcatfee | 1:faa8aa177069 | 397 | |
dogcatfee | 1:faa8aa177069 | 398 | def handleComButton( self ): |
dogcatfee | 1:faa8aa177069 | 399 | |
dogcatfee | 1:faa8aa177069 | 400 | #If the comm. button is is Scan mode, scan the serial ports |
dogcatfee | 1:faa8aa177069 | 401 | if self.comButtonState == "Scan": |
dogcatfee | 1:faa8aa177069 | 402 | |
dogcatfee | 1:faa8aa177069 | 403 | self.serialScan() |
dogcatfee | 1:faa8aa177069 | 404 | |
dogcatfee | 1:faa8aa177069 | 405 | if self.comboBox.count() > 1: |
dogcatfee | 1:faa8aa177069 | 406 | |
dogcatfee | 1:faa8aa177069 | 407 | #If we have detected a serial port, change the button name to "connect" and change color |
dogcatfee | 1:faa8aa177069 | 408 | self.pushButton.setText(_translate("MainWindow", "Connect", None)) |
dogcatfee | 1:faa8aa177069 | 409 | self.pushButton.setStyleSheet("color: black; background-color: yellow;") |
dogcatfee | 1:faa8aa177069 | 410 | self.comButtonState = "Connect" |
dogcatfee | 1:faa8aa177069 | 411 | |
dogcatfee | 1:faa8aa177069 | 412 | #Enable the play and clear buttons |
dogcatfee | 1:faa8aa177069 | 413 | self.pushButton_2.setEnabled( True ) |
dogcatfee | 1:faa8aa177069 | 414 | self.pushButton_3.setEnabled( True ) |
dogcatfee | 1:faa8aa177069 | 415 | else: |
dogcatfee | 1:faa8aa177069 | 416 | |
dogcatfee | 1:faa8aa177069 | 417 | #Create a message box if there was no COM port detected |
dogcatfee | 1:faa8aa177069 | 418 | QtGui.QMessageBox.information(self, 'Error',"A serial device was not detected, please ensure that it is plugged into your computer.") |
dogcatfee | 1:faa8aa177069 | 419 | |
dogcatfee | 1:faa8aa177069 | 420 | elif self.comButtonState == "Connect": |
dogcatfee | 1:faa8aa177069 | 421 | |
dogcatfee | 1:faa8aa177069 | 422 | #Create a new port |
dogcatfee | 1:faa8aa177069 | 423 | self.ser.close() |
dogcatfee | 1:faa8aa177069 | 424 | new_port = str(self.comboBox.currentText()) |
dogcatfee | 1:faa8aa177069 | 425 | self.ser.port = new_port |
dogcatfee | 1:faa8aa177069 | 426 | |
dogcatfee | 1:faa8aa177069 | 427 | try: |
dogcatfee | 1:faa8aa177069 | 428 | if self.comboBox.currentText() != "Select": |
dogcatfee | 1:faa8aa177069 | 429 | self.ser.open() |
dogcatfee | 1:faa8aa177069 | 430 | else: |
dogcatfee | 1:faa8aa177069 | 431 | QtGui.QMessageBox.information(self, 'Error',"Please Select a COM port.") |
dogcatfee | 1:faa8aa177069 | 432 | |
dogcatfee | 1:faa8aa177069 | 433 | except Exception ,e: |
dogcatfee | 1:faa8aa177069 | 434 | |
dogcatfee | 1:faa8aa177069 | 435 | #If we can't connect, reset the button state to "scan" |
dogcatfee | 1:faa8aa177069 | 436 | self.pushButton.setText(_translate("MainWindow", "Disconnect", None)) |
dogcatfee | 1:faa8aa177069 | 437 | self.pushButton.setStyleSheet("color: black; background-color: red;") |
dogcatfee | 1:faa8aa177069 | 438 | self.comButtonState = "Scan" |
dogcatfee | 1:faa8aa177069 | 439 | if self.ser.isOpen(): |
dogcatfee | 1:faa8aa177069 | 440 | |
dogcatfee | 1:faa8aa177069 | 441 | #If we can open the serial port, change the button name to "disconnect" and change color |
dogcatfee | 1:faa8aa177069 | 442 | self.pushButton.setText(_translate("MainWindow", "Disconnect", None)) |
dogcatfee | 1:faa8aa177069 | 443 | self.pushButton.setStyleSheet("color: black; background-color: green;") |
dogcatfee | 1:faa8aa177069 | 444 | self.comButtonState = "Disconnect" |
dogcatfee | 1:faa8aa177069 | 445 | self.ser.close() |
dogcatfee | 1:faa8aa177069 | 446 | |
dogcatfee | 1:faa8aa177069 | 447 | elif self.comButtonState == "Disconnect": |
dogcatfee | 1:faa8aa177069 | 448 | |
dogcatfee | 1:faa8aa177069 | 449 | #Change the button text to "Scan" once disconnect it pressed, and change color |
dogcatfee | 1:faa8aa177069 | 450 | self.pushButton.setText(_translate("MainWindow", "Scan", None)) |
dogcatfee | 1:faa8aa177069 | 451 | self.pushButton.setStyleSheet("color: black; background-color: red;") |
dogcatfee | 1:faa8aa177069 | 452 | self.comButtonState = "Scan" |
dogcatfee | 1:faa8aa177069 | 453 | |
dogcatfee | 1:faa8aa177069 | 454 | #Disable play and clear buttons |
dogcatfee | 1:faa8aa177069 | 455 | self.pushButton_2.setEnabled( False ) |
dogcatfee | 1:faa8aa177069 | 456 | self.pushButton_3.setEnabled( False ) |
dogcatfee | 1:faa8aa177069 | 457 | |
dogcatfee | 1:faa8aa177069 | 458 | def beepThread( self, tone ): |
dogcatfee | 1:faa8aa177069 | 459 | print "No sound" |
dogcatfee | 1:faa8aa177069 | 460 | #Produce a tone using winsound |
dogcatfee | 1:faa8aa177069 | 461 | |
dogcatfee | 1:faa8aa177069 | 462 | # Uncomment below for sound |
dogcatfee | 1:faa8aa177069 | 463 | #winsound.Beep(tone, 150) |
dogcatfee | 1:faa8aa177069 | 464 | |
dogcatfee | 1:faa8aa177069 | 465 | def handleCellClicked(self, row, column): |
dogcatfee | 1:faa8aa177069 | 466 | |
dogcatfee | 1:faa8aa177069 | 467 | self.noteExists = False |
dogcatfee | 1:faa8aa177069 | 468 | |
dogcatfee | 1:faa8aa177069 | 469 | #Check to see if any notes are already selected in the row |
dogcatfee | 1:faa8aa177069 | 470 | for i in xrange(self.tableView.rowCount()): |
dogcatfee | 1:faa8aa177069 | 471 | if self.selectedCells[i][column] == self.cellSelected: |
dogcatfee | 1:faa8aa177069 | 472 | self.noteExists = True |
dogcatfee | 1:faa8aa177069 | 473 | |
dogcatfee | 1:faa8aa177069 | 474 | |
dogcatfee | 1:faa8aa177069 | 475 | #If a cell is clicked and unselected, change it's color, mark it as selected, and start the tone thread |
dogcatfee | 1:faa8aa177069 | 476 | if self.selectedCells[row][column] == self.cellUnselected: |
dogcatfee | 1:faa8aa177069 | 477 | |
dogcatfee | 1:faa8aa177069 | 478 | #Do not select cell if a note already exists in the column |
dogcatfee | 1:faa8aa177069 | 479 | if self.noteExists == False: |
dogcatfee | 1:faa8aa177069 | 480 | |
dogcatfee | 1:faa8aa177069 | 481 | self.tableView.item(row, column).setBackground(QtGui.QColor(100,100,150)) |
dogcatfee | 1:faa8aa177069 | 482 | self.selectedCells[row][column] = self.cellSelected |
dogcatfee | 1:faa8aa177069 | 483 | thread.start_new_thread(self.beepThread, (self.noteFrequencies[row],)) |
dogcatfee | 1:faa8aa177069 | 484 | |
dogcatfee | 1:faa8aa177069 | 485 | #If a cell is clicked and selected, change it's color and mark it as unselected |
dogcatfee | 1:faa8aa177069 | 486 | else: |
dogcatfee | 1:faa8aa177069 | 487 | self.tableView.item(row, column).setBackground(QtGui.QColor(255,255,255)) |
dogcatfee | 1:faa8aa177069 | 488 | self.selectedCells[row][column] = self.cellUnselected |
dogcatfee | 1:faa8aa177069 | 489 | |
dogcatfee | 1:faa8aa177069 | 490 | def handleSaveSongButton( self ): |
dogcatfee | 1:faa8aa177069 | 491 | |
dogcatfee | 1:faa8aa177069 | 492 | #Create a save file dialog |
dogcatfee | 1:faa8aa177069 | 493 | self.fileName = QFileDialog.getSaveFileName(self,"Save File", |
dogcatfee | 1:faa8aa177069 | 494 | "", |
dogcatfee | 1:faa8aa177069 | 495 | "FDRM-KL46z Song (*.song)"); |
dogcatfee | 1:faa8aa177069 | 496 | try: |
dogcatfee | 1:faa8aa177069 | 497 | #Create a file with the filename provided from the user |
dogcatfee | 1:faa8aa177069 | 498 | self.f = open(self.fileName, 'w') |
dogcatfee | 1:faa8aa177069 | 499 | |
dogcatfee | 1:faa8aa177069 | 500 | #Write the note number index |
dogcatfee | 1:faa8aa177069 | 501 | self.f.write( str(self.comboBox2.currentIndex()) + '\n' ) |
dogcatfee | 1:faa8aa177069 | 502 | |
dogcatfee | 1:faa8aa177069 | 503 | #Write each note as a coordinate on a newline |
dogcatfee | 1:faa8aa177069 | 504 | for i in xrange(self.tableView.rowCount()): |
dogcatfee | 1:faa8aa177069 | 505 | for j in xrange(self.tableView.columnCount()): |
dogcatfee | 1:faa8aa177069 | 506 | if self.selectedCells[i][j] == self.cellSelected: |
dogcatfee | 1:faa8aa177069 | 507 | self.f.write( str(i)+ ',' + str(j) + '\n') |
dogcatfee | 1:faa8aa177069 | 508 | |
dogcatfee | 1:faa8aa177069 | 509 | #Write the end of song marker |
dogcatfee | 1:faa8aa177069 | 510 | self.f.write( "end song") |
dogcatfee | 1:faa8aa177069 | 511 | self.f.close() |
dogcatfee | 1:faa8aa177069 | 512 | |
dogcatfee | 1:faa8aa177069 | 513 | except Exception ,e: |
dogcatfee | 1:faa8aa177069 | 514 | print(e) |
dogcatfee | 1:faa8aa177069 | 515 | |
dogcatfee | 1:faa8aa177069 | 516 | def handleLoadSongButton( self ): |
dogcatfee | 1:faa8aa177069 | 517 | |
dogcatfee | 1:faa8aa177069 | 518 | #Create and open file dialog |
dogcatfee | 1:faa8aa177069 | 519 | self.fileName = QFileDialog.getOpenFileName(self,"Open File", |
dogcatfee | 1:faa8aa177069 | 520 | "", |
dogcatfee | 1:faa8aa177069 | 521 | "FDRM-KL46z Song (*.song)"); |
dogcatfee | 1:faa8aa177069 | 522 | |
dogcatfee | 1:faa8aa177069 | 523 | try: |
dogcatfee | 1:faa8aa177069 | 524 | #Open the user defined file for reading |
dogcatfee | 1:faa8aa177069 | 525 | self.f = open(self.fileName, 'r') |
dogcatfee | 1:faa8aa177069 | 526 | |
dogcatfee | 1:faa8aa177069 | 527 | #cleat all notes from tableview |
dogcatfee | 1:faa8aa177069 | 528 | self.handleClearButton(); |
dogcatfee | 1:faa8aa177069 | 529 | |
dogcatfee | 1:faa8aa177069 | 530 | #Read index number and change the note number combo box |
dogcatfee | 1:faa8aa177069 | 531 | index = int(self.f.readline()) |
dogcatfee | 1:faa8aa177069 | 532 | self.handleNoteNumberChange(index) |
dogcatfee | 1:faa8aa177069 | 533 | self.comboBox2.setCurrentIndex(index) |
dogcatfee | 1:faa8aa177069 | 534 | |
dogcatfee | 1:faa8aa177069 | 535 | #Read the first cell location |
dogcatfee | 1:faa8aa177069 | 536 | self.cellID = self.f.readline() |
dogcatfee | 1:faa8aa177069 | 537 | |
dogcatfee | 1:faa8aa177069 | 538 | #Continue reading cell ID's and activate corresponding cells |
dogcatfee | 1:faa8aa177069 | 539 | while self.cellID != "end song": |
dogcatfee | 1:faa8aa177069 | 540 | i = 0 |
dogcatfee | 1:faa8aa177069 | 541 | columnString = "" |
dogcatfee | 1:faa8aa177069 | 542 | rowString = "" |
dogcatfee | 1:faa8aa177069 | 543 | |
dogcatfee | 1:faa8aa177069 | 544 | #Wait until we get a comma |
dogcatfee | 1:faa8aa177069 | 545 | while self.cellID[i] != ',': |
dogcatfee | 1:faa8aa177069 | 546 | rowString += self.cellID[i] |
dogcatfee | 1:faa8aa177069 | 547 | i += 1 |
dogcatfee | 1:faa8aa177069 | 548 | |
dogcatfee | 1:faa8aa177069 | 549 | #Go to the next data point after a newline character |
dogcatfee | 1:faa8aa177069 | 550 | while self.cellID[i+1] != '\n': |
dogcatfee | 1:faa8aa177069 | 551 | columnString += self.cellID[i+1] |
dogcatfee | 1:faa8aa177069 | 552 | i += 1 |
dogcatfee | 1:faa8aa177069 | 553 | |
dogcatfee | 1:faa8aa177069 | 554 | #Update the table view |
dogcatfee | 1:faa8aa177069 | 555 | self.tableView.item(int(rowString), int(columnString)).setBackground(QtGui.QColor(100,100,150)) |
dogcatfee | 1:faa8aa177069 | 556 | self.selectedCells[int(rowString)][int(columnString)] = self.cellSelected |
dogcatfee | 1:faa8aa177069 | 557 | self.cellID = self.f.readline() |
dogcatfee | 1:faa8aa177069 | 558 | except Exception ,e: |
dogcatfee | 1:faa8aa177069 | 559 | print(e) |
dogcatfee | 1:faa8aa177069 | 560 | |
dogcatfee | 1:faa8aa177069 | 561 | def handleNoteNumberChange( self, index ): |
dogcatfee | 1:faa8aa177069 | 562 | |
dogcatfee | 1:faa8aa177069 | 563 | #Change the number of columns in the table if the song length changes |
dogcatfee | 1:faa8aa177069 | 564 | self.tableViewColumnCount = int(self.noteNumberValues[int(index)]) |
dogcatfee | 1:faa8aa177069 | 565 | self.selectedCells = [[0 for x in xrange(self.tableViewColumnCount)] for x in xrange(self.tableViewRowCount)] |
dogcatfee | 1:faa8aa177069 | 566 | |
dogcatfee | 1:faa8aa177069 | 567 | #Reset the table |
dogcatfee | 1:faa8aa177069 | 568 | self.initializeTable() |
dogcatfee | 1:faa8aa177069 | 569 | self.handleClearButton() |
dogcatfee | 1:faa8aa177069 | 570 | |
dogcatfee | 1:faa8aa177069 | 571 | def handleTempoChange( self, index ): |
dogcatfee | 1:faa8aa177069 | 572 | |
dogcatfee | 1:faa8aa177069 | 573 | #Change necesarry variables if the tempo is chaged |
dogcatfee | 1:faa8aa177069 | 574 | self.tempo = int(self.tempoValues[int(index)]) |
dogcatfee | 1:faa8aa177069 | 575 | |
dogcatfee | 1:faa8aa177069 | 576 | def handleAboutButton( self ): |
dogcatfee | 1:faa8aa177069 | 577 | |
dogcatfee | 1:faa8aa177069 | 578 | #Opens the about form if the about button is pressed |
dogcatfee | 1:faa8aa177069 | 579 | self.dialog = QtGui.QDialog(self) |
dogcatfee | 1:faa8aa177069 | 580 | self.dialog.ui = aboutForm() |
dogcatfee | 1:faa8aa177069 | 581 | self.dialog.ui.setupUi(self.dialog) |
dogcatfee | 1:faa8aa177069 | 582 | self.dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose) |
dogcatfee | 1:faa8aa177069 | 583 | self.dialog.show() |
dogcatfee | 1:faa8aa177069 | 584 | |
dogcatfee | 1:faa8aa177069 | 585 | |
dogcatfee | 1:faa8aa177069 | 586 | def bind_events( self ): |
dogcatfee | 1:faa8aa177069 | 587 | #Bind events to handler functions |
dogcatfee | 1:faa8aa177069 | 588 | self.pushButton.clicked.connect( self.handleComButton ) |
dogcatfee | 1:faa8aa177069 | 589 | self.tableView.cellClicked.connect( self.handleCellClicked ) |
dogcatfee | 1:faa8aa177069 | 590 | self.pushButton_3.clicked.connect( self.handleClearButton ) |
dogcatfee | 1:faa8aa177069 | 591 | self.actionSave_Song.triggered.connect( self.handleSaveSongButton ) |
dogcatfee | 1:faa8aa177069 | 592 | self.actionLoad_Song.triggered.connect( self.handleLoadSongButton ) |
dogcatfee | 1:faa8aa177069 | 593 | self.pushButton_2.clicked.connect( self.handlePlayButton ) |
dogcatfee | 1:faa8aa177069 | 594 | self.comboBox2.currentIndexChanged.connect( self.handleNoteNumberChange ) |
dogcatfee | 1:faa8aa177069 | 595 | self.comboBox3.currentIndexChanged.connect( self.handleTempoChange ) |
dogcatfee | 1:faa8aa177069 | 596 | self.actionAbout.triggered.connect( self.handleAboutButton ) |
dogcatfee | 1:faa8aa177069 | 597 | |
dogcatfee | 1:faa8aa177069 | 598 | class aboutForm(object): |
dogcatfee | 1:faa8aa177069 | 599 | |
dogcatfee | 1:faa8aa177069 | 600 | def setupUi(self, Form): |
dogcatfee | 1:faa8aa177069 | 601 | |
dogcatfee | 1:faa8aa177069 | 602 | #Configure the form |
dogcatfee | 1:faa8aa177069 | 603 | Form.setObjectName(_fromUtf8("Form")) |
dogcatfee | 1:faa8aa177069 | 604 | Form.resize(381, 233) |
dogcatfee | 1:faa8aa177069 | 605 | Form.setMinimumSize(QtCore.QSize(381, 233)) |
dogcatfee | 1:faa8aa177069 | 606 | Form.setMaximumSize(QtCore.QSize(381, 233)) |
dogcatfee | 1:faa8aa177069 | 607 | |
dogcatfee | 1:faa8aa177069 | 608 | #Create label |
dogcatfee | 1:faa8aa177069 | 609 | self.label = QtGui.QLabel(Form) |
dogcatfee | 1:faa8aa177069 | 610 | self.label.setGeometry(QtCore.QRect(140, 160, 121, 16)) |
dogcatfee | 1:faa8aa177069 | 611 | self.label.setObjectName(_fromUtf8("label")) |
dogcatfee | 1:faa8aa177069 | 612 | |
dogcatfee | 1:faa8aa177069 | 613 | #Create label_2 |
dogcatfee | 1:faa8aa177069 | 614 | self.label_2 = QtGui.QLabel(Form) |
dogcatfee | 1:faa8aa177069 | 615 | self.label_2.setGeometry(QtCore.QRect(140, 180, 121, 16)) |
dogcatfee | 1:faa8aa177069 | 616 | self.label_2.setObjectName(_fromUtf8("label_2")) |
dogcatfee | 1:faa8aa177069 | 617 | |
dogcatfee | 1:faa8aa177069 | 618 | #Create label_3 |
dogcatfee | 1:faa8aa177069 | 619 | self.label_3 = QtGui.QLabel(Form) |
dogcatfee | 1:faa8aa177069 | 620 | self.label_3.setGeometry(QtCore.QRect(160, 200, 121, 16)) |
dogcatfee | 1:faa8aa177069 | 621 | self.label_3.setObjectName(_fromUtf8("label_3")) |
dogcatfee | 1:faa8aa177069 | 622 | |
dogcatfee | 1:faa8aa177069 | 623 | #Create a graphics view |
dogcatfee | 1:faa8aa177069 | 624 | self.graphicsView = QtGui.QGraphicsView(Form) |
dogcatfee | 1:faa8aa177069 | 625 | self.graphicsView.setGeometry(QtCore.QRect(40, 20, 300, 120)) |
dogcatfee | 1:faa8aa177069 | 626 | self.graphicsView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) |
dogcatfee | 1:faa8aa177069 | 627 | self.graphicsView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) |
dogcatfee | 1:faa8aa177069 | 628 | self.graphicsView.setObjectName(_fromUtf8("graphicsView")) |
dogcatfee | 1:faa8aa177069 | 629 | |
dogcatfee | 1:faa8aa177069 | 630 | #Set the graphics view image |
dogcatfee | 1:faa8aa177069 | 631 | self.scene = QtGui.QGraphicsScene(); |
dogcatfee | 1:faa8aa177069 | 632 | self.pixmap = QtGui.QPixmap( "TekBots.png" ) |
dogcatfee | 1:faa8aa177069 | 633 | self.scene.addPixmap(self.pixmap) |
dogcatfee | 1:faa8aa177069 | 634 | self.graphicsView.setScene( self.scene ) |
dogcatfee | 1:faa8aa177069 | 635 | self.graphicsView.setObjectName(_fromUtf8("graphicsView")) |
dogcatfee | 1:faa8aa177069 | 636 | |
dogcatfee | 1:faa8aa177069 | 637 | #Set text of all UI elements |
dogcatfee | 1:faa8aa177069 | 638 | self.retranslateUi(Form) |
dogcatfee | 1:faa8aa177069 | 639 | |
dogcatfee | 1:faa8aa177069 | 640 | #Connect Slots |
dogcatfee | 1:faa8aa177069 | 641 | QtCore.QMetaObject.connectSlotsByName(Form) |
dogcatfee | 1:faa8aa177069 | 642 | |
dogcatfee | 1:faa8aa177069 | 643 | def retranslateUi(self, Form): |
dogcatfee | 1:faa8aa177069 | 644 | |
dogcatfee | 1:faa8aa177069 | 645 | #Set text of all UI elements |
dogcatfee | 1:faa8aa177069 | 646 | Form.setWindowTitle(_translate("About", "About", None)) |
dogcatfee | 1:faa8aa177069 | 647 | self.label.setText(_translate("About", "FDRM-KL46z Beat Factory", None)) |
dogcatfee | 1:faa8aa177069 | 648 | self.label_2.setText(_translate("About", "Author: Ethan Takla", None)) |
dogcatfee | 1:faa8aa177069 | 649 | self.label_3.setText(_translate("About", "Revision 0.1", None)) |
dogcatfee | 1:faa8aa177069 | 650 | |
dogcatfee | 1:faa8aa177069 | 651 | #Start the app |
dogcatfee | 1:faa8aa177069 | 652 | app = QApplication(sys.argv) |
dogcatfee | 1:faa8aa177069 | 653 | window = QMainWindow() |
dogcatfee | 1:faa8aa177069 | 654 | ui = Ui_MainWindow() |
dogcatfee | 1:faa8aa177069 | 655 | ui.initializeVariables() |
dogcatfee | 1:faa8aa177069 | 656 | ui.initializeUI(window) |
dogcatfee | 1:faa8aa177069 | 657 | ui.initializeTable() |
dogcatfee | 1:faa8aa177069 | 658 | ui.initializeSerial() |
dogcatfee | 1:faa8aa177069 | 659 | ui.bind_events() |
dogcatfee | 1:faa8aa177069 | 660 | |
dogcatfee | 1:faa8aa177069 | 661 | window.show() |
dogcatfee | 1:faa8aa177069 | 662 | sys.exit(app.exec_()) |