# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'beamSteer_GUI.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg


class Ui_MainWindow(object):
    
    def togglePause(self):
        text = self.toggleTrackerButton.text()
        if text == 'PAUSE':
            self.toggleTrackerButton.setText("PLAY")
            self.phaseCalibration.setEnabled(True)
        elif text == 'PLAY':
            self.toggleTrackerButton.setText("PAUSE")
            self.phaseCalibration.setEnabled(False)

    def getPauseToggle(self):
            text = self.toggleTrackerButton.text()
            if text == 'PAUSE':
                return True
            elif text == 'PLAY':
                return False

    def getPhaseCal(self):
        return self.phaseCalibration.value()
    
    def togglePeakDisplay(self):
        text = self.buttonDisplayPeaks.text()
        if text == 'DISPLAY PEAKS: \nON':
            self.buttonDisplayPeaks.setText('DISPLAY PEAKS: \nOFF')
        elif text == 'DISPLAY PEAKS: \nOFF':
            self.buttonDisplayPeaks.setText('DISPLAY PEAKS: \nON')

    def getPeakDisplayToggle(self):
        text = self.buttonDisplayPeaks.text()
        if text == 'DISPLAY PEAKS: \nON':
            return True
        elif text == 'DISPLAY PEAKS: \nOFF':
            return False

    def toggleResetPeaks(self):
        text = self.buttonResetPeaks.text()
        if text == 'RESET PEAKS: \nON':
            self.buttonResetPeaks.setText('RESET PEAKS: \nOFF')
        elif text == 'RESET PEAKS: \nOFF':
            self.buttonResetPeaks.setText('RESET PEAKS: \nON')

    def getResetPeaksToggle(self):
        text = self.buttonResetPeaks.text()
        if text == 'RESET PEAKS: \nON':
            return True
        elif text == 'RESET PEAKS: \nOFF':
            return False
        
    def adjustPhase(self):
        phaseValue = self.dialPhaseIncrement.value()
        self.labelPhaseIncrement.setText(str(phaseValue)+" deg")

    def adjustSpeed(self):
        speedValue = self.speedDial.value()
        self.labelSpeed.setText(str(speedValue)+" ms")

    def recalibrate(self):
        print("RECALIBRATION ROUTINE")
        self.labelPhaseCal.setText("Phase Calibration: "+str(27)+" deg")

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1384, 856)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        # Plots
        self.winFFT = GraphicsLayoutWidget(self.centralwidget)
        self.winFFT.setGeometry(QtCore.QRect(20, 20, 761, 401))
        self.winFFT.setObjectName("winFFT")
        self.winRADAR = GraphicsLayoutWidget(self.centralwidget)
        self.winRADAR.setGeometry(QtCore.QRect(790, 20, 581, 581))
        self.winRADAR.setObjectName("winRADAR")
        self.trackerView = GraphicsLayoutWidget(self.centralwidget)
        self.trackerView.setGeometry(QtCore.QRect(790, 20, 581, 581))
        self.trackerView.setObjectName("trackerView")
        # Buttons
        self.toggleTrackerButton = QtWidgets.QPushButton(self.centralwidget)
        self.toggleTrackerButton.setGeometry(QtCore.QRect(540, 620, 171, 171))
        self.toggleTrackerButton.setObjectName("toggleTrackerButton")
        self.toggleTrackerButton.clicked.connect(self.togglePause)
        self.buttonDisplayPeaks = QtWidgets.QPushButton(self.centralwidget)
        self.buttonDisplayPeaks.setGeometry(QtCore.QRect(1200, 610, 171, 91))
        self.buttonDisplayPeaks.setAcceptDrops(False)
        self.buttonDisplayPeaks.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.buttonDisplayPeaks.setAutoFillBackground(False)
        self.buttonDisplayPeaks.setObjectName("buttonDisplayPeaks")
        self.buttonDisplayPeaks.clicked.connect(self.togglePeakDisplay)
        self.buttonResetPeaks = QtWidgets.QPushButton(self.centralwidget)
        self.buttonResetPeaks.setGeometry(QtCore.QRect(1200, 710, 171, 91))
        self.buttonResetPeaks.setAcceptDrops(False)
        self.buttonResetPeaks.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.buttonResetPeaks.setAutoFillBackground(False)
        self.buttonResetPeaks.setObjectName("buttonResetPeaks")
        self.buttonResetPeaks.clicked.connect(self.toggleResetPeaks)
        self.phaseCalibration = QtWidgets.QPushButton(self.centralwidget)
        self.phaseCalibration.setGeometry(QtCore.QRect(310, 620, 171, 121))
        self.phaseCalibration.setEnabled(False)
        self.phaseCalibration.clicked.connect(self.recalibrate)
        self.phaseCalibration.setObjectName("phaseCalibration")
        # Dials
        self.dialPhaseIncrement = QtWidgets.QDial(self.centralwidget)
        self.dialPhaseIncrement.setGeometry(QtCore.QRect(1030, 650, 121, 121))
        self.dialPhaseIncrement.setAcceptDrops(False)
        self.dialPhaseIncrement.setMinimum(-5)
        self.dialPhaseIncrement.setMaximum(5)
        self.dialPhaseIncrement.setNotchTarget(1.0)
        self.dialPhaseIncrement.setNotchesVisible(True)
        self.dialPhaseIncrement.setValue(1)
        self.dialPhaseIncrement.setObjectName("dialPhaseIncrement")
        self.dialPhaseIncrement.valueChanged.connect(self.adjustPhase)
        self.speedDial = QtWidgets.QDial(self.centralwidget)
        self.speedDial.setGeometry(QtCore.QRect(850, 650, 121, 121))
        self.speedDial.setMaximum(100)
        self.speedDial.setProperty("value", 50)
        self.speedDial.setNotchTarget(5.0)
        self.speedDial.setNotchesVisible(True)
        self.speedDial.setObjectName("speedDial")
        self.speedDial.valueChanged.connect(self.adjustSpeed)
        # Variable Labels
        self.labelPhaseCal = QtWidgets.QLabel(self.centralwidget)
        self.labelPhaseCal.setGeometry(QtCore.QRect(290, 750, 211, 41))
        self.labelPhaseCal.setAlignment(QtCore.Qt.AlignCenter)
        self.labelPhaseCal.setObjectName("labelPhaseCal")
        self.labelPhaseIncrement = QtWidgets.QLabel(self.centralwidget)
        self.labelPhaseIncrement.setEnabled(True)
        self.labelPhaseIncrement.setGeometry(QtCore.QRect(1000, 620, 181, 31))
        self.labelPhaseIncrement.setAlignment(QtCore.Qt.AlignCenter)
        self.labelPhaseIncrement.setIndent(7)
        self.labelPhaseIncrement.setObjectName("labelPhaseIncrement")
        self.labelSpeed = QtWidgets.QLabel(self.centralwidget)
        self.labelSpeed.setGeometry(QtCore.QRect(840, 620, 141, 31))
        self.labelSpeed.setAlignment(QtCore.Qt.AlignCenter)
        self.labelSpeed.setObjectName("labelSpeed")
        # Static Labels
        self.staticLabelSpeed = QtWidgets.QLabel(self.centralwidget)
        self.staticLabelSpeed.setGeometry(QtCore.QRect(830, 780, 161, 31))
        self.staticLabelSpeed.setAlignment(QtCore.Qt.AlignCenter)
        self.staticLabelSpeed.setObjectName("staticLabelSpeed")
        self.staticLabelPhaseIncrement = QtWidgets.QLabel(self.centralwidget)
        self.staticLabelPhaseIncrement.setGeometry(QtCore.QRect(990, 780, 201, 31))
        self.staticLabelPhaseIncrement.setTextFormat(QtCore.Qt.PlainText)
        self.staticLabelPhaseIncrement.setScaledContents(False)
        self.staticLabelPhaseIncrement.setAlignment(QtCore.Qt.AlignCenter)
        self.staticLabelPhaseIncrement.setObjectName("staticLabelPhaseIncrement")
        # LCD Labels
        self.labelPhaseLCD = QtWidgets.QLabel(self.centralwidget)
        self.labelPhaseLCD.setGeometry(QtCore.QRect(70, 430, 191, 31))
        self.labelPhaseLCD.setAlignment(QtCore.Qt.AlignCenter)
        self.labelPhaseLCD.setObjectName("labelPhaseLCD")
        self.labelSteeringLCD = QtWidgets.QLabel(self.centralwidget)
        self.labelSteeringLCD.setGeometry(QtCore.QRect(300, 430, 191, 31))
        self.labelSteeringLCD.setAlignment(QtCore.Qt.AlignCenter)
        self.labelSteeringLCD.setObjectName("labelSteeringLCD")
        self.labelSignalLCD = QtWidgets.QLabel(self.centralwidget)
        self.labelSignalLCD.setGeometry(QtCore.QRect(530, 430, 191, 31))
        self.labelSignalLCD.setAlignment(QtCore.Qt.AlignCenter)
        self.labelSignalLCD.setObjectName("labelSignalLCD")
        self.labelPeakSignalLCD = QtWidgets.QLabel(self.centralwidget)
        self.labelPeakSignalLCD.setGeometry(QtCore.QRect(530, 520, 191, 31))
        self.labelPeakSignalLCD.setAlignment(QtCore.Qt.AlignCenter)
        self.labelPeakSignalLCD.setObjectName("labelPeakSignalLCD")
        self.labelPeakPhaseLCD = QtWidgets.QLabel(self.centralwidget)
        self.labelPeakPhaseLCD.setGeometry(QtCore.QRect(70, 520, 191, 31))
        self.labelPeakPhaseLCD.setAlignment(QtCore.Qt.AlignCenter)
        self.labelPeakPhaseLCD.setObjectName("labelPeakPhaseLCD")
        self.labelPeakSteeringLCD = QtWidgets.QLabel(self.centralwidget)
        self.labelPeakSteeringLCD.setGeometry(QtCore.QRect(300, 520, 191, 31))
        self.labelPeakSteeringLCD.setAlignment(QtCore.Qt.AlignCenter)
        self.labelPeakSteeringLCD.setObjectName("labelPeakSteeringLCD")
        # LCD Screens
        self.lcdPhase = QtWidgets.QLCDNumber(self.centralwidget)
        self.lcdPhase.setGeometry(QtCore.QRect(70, 460, 191, 51))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lcdPhase.setFont(font)
        self.lcdPhase.setSmallDecimalPoint(False)
        self.lcdPhase.setDigitCount(3)
        self.lcdPhase.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
        self.lcdPhase.setObjectName("lcdPhase")
        self.lcdSteering = QtWidgets.QLCDNumber(self.centralwidget)
        self.lcdSteering.setGeometry(QtCore.QRect(300, 460, 191, 51))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lcdSteering.setFont(font)
        self.lcdSteering.setSmallDecimalPoint(False)
        self.lcdSteering.setDigitCount(3)
        self.lcdSteering.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
        self.lcdSteering.setObjectName("lcdSteering")
        self.lcdSignal = QtWidgets.QLCDNumber(self.centralwidget)
        self.lcdSignal.setGeometry(QtCore.QRect(530, 460, 191, 51))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lcdSignal.setFont(font)
        self.lcdSignal.setSmallDecimalPoint(False)
        self.lcdSignal.setDigitCount(3)
        self.lcdSignal.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
        self.lcdSignal.setObjectName("lcdSignal")
        self.lcdPeakSignal = QtWidgets.QLCDNumber(self.centralwidget)
        self.lcdPeakSignal.setGeometry(QtCore.QRect(530, 550, 191, 51))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lcdPeakSignal.setFont(font)
        self.lcdPeakSignal.setSmallDecimalPoint(False)
        self.lcdPeakSignal.setDigitCount(3)
        self.lcdPeakSignal.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
        self.lcdPeakSignal.setObjectName("lcdPeakSignal")
        self.lcdPeakSteering = QtWidgets.QLCDNumber(self.centralwidget)
        self.lcdPeakSteering.setGeometry(QtCore.QRect(300, 550, 191, 51))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lcdPeakSteering.setFont(font)
        self.lcdPeakSteering.setSmallDecimalPoint(False)
        self.lcdPeakSteering.setDigitCount(3)
        self.lcdPeakSteering.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
        self.lcdPeakSteering.setObjectName("lcdPeakSteering")
        self.lcdPeakPhase = QtWidgets.QLCDNumber(self.centralwidget)
        self.lcdPeakPhase.setGeometry(QtCore.QRect(70, 550, 191, 51))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lcdPeakPhase.setFont(font)
        self.lcdPeakPhase.setSmallDecimalPoint(False)
        self.lcdPeakPhase.setDigitCount(3)
        self.lcdPeakPhase.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
        self.lcdPeakPhase.setObjectName("lcdPeakPhase")
        #
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "nTSDR - Linear Rx Beam Steering"))
        self.labelPhaseCal.setText(_translate("MainWindow", "Phase Calibration: 0 deg"))
        self.toggleTrackerButton.setText(_translate("MainWindow", "PAUSE"))
        self.labelPhaseIncrement.setText(_translate("MainWindow", "1 deg"))
        self.labelSpeed.setText(_translate("MainWindow", "50 ms"))
        self.staticLabelSpeed.setText(_translate("MainWindow", "Loop Speed"))
        self.staticLabelPhaseIncrement.setText(_translate("MainWindow", "Phase Increment"))
        self.buttonDisplayPeaks.setText(_translate("MainWindow", "DISPLAY PEAKS: \n"
"ON"))
        self.labelPhaseLCD.setText(_translate("MainWindow", "Phase Shift [deg]"))
        self.labelSteeringLCD.setText(_translate("MainWindow", "Steering Angle [deg]"))
        self.labelSignalLCD.setText(_translate("MainWindow", "Rx0+Rx1 [dBfs]"))
        self.labelPeakSignalLCD.setText(_translate("MainWindow", "Peak Signal [dBfs]"))
        self.labelPeakPhaseLCD.setText(_translate("MainWindow", "Peak Phase [deg]"))
        self.labelPeakSteeringLCD.setText(_translate("MainWindow", "Estimated DOA [deg]"))
        self.buttonResetPeaks.setText(_translate("MainWindow", "RESET PEAKS: \n"
"ON"))
        self.phaseCalibration.setText(_translate("MainWindow", "RECALIBRATE"))

    def setFFTGraph(self):
        p1 = self.winFFT.addPlot()
        p1.setXRange(-1.00, 1.00) 
        p1.setYRange(-100, 0)
        p1.setLabel('bottom', 'frequency', 'MHz', **{'color': '#FFF', 'size': '14pt'})
        p1.setLabel('left', 'Rx0 + Rx1', 'dBfs', **{'color': '#FFF', 'size': '14pt'})
        return p1
    
    def setRADARGraph(self):
        def addText(plot, degree, x, y):
            text = pg.TextItem(degree)
            plot.addItem(text)
            text.setPos(x, y)
        p2 = self.winRADAR.addPlot()
        p2.setAspectLocked()
        p2.setMouseEnabled(x=False, y=False)
        # Add polar grid lines
        p2.addLine(x=0, pen=0.2)
        p2.addLine(y=0, pen=0.2)
        for r in range(2, 36, 4):
            circle = QtWidgets.QGraphicsEllipseItem(-r, -r, r * 2, r * 2)
            circle.setPen(pg.mkPen(0.2))
            p2.addItem(circle)
            p2.hideAxis('bottom')
            p2.hideAxis('left')
        # Add text
        addText(p2, "0°", -2, 40)
        addText(p2, "45°", 24, 30)
        addText(p2, "90°", 36, 3)
        addText(p2, "135°", 24, -24)
        addText(p2, "180°", -2, -34)
        addText(p2, "225°", -30, -24)
        addText(p2, "270°", -44, 3)
        addText(p2, "315°", -30, 30)
        return p2

from pyqtgraph import GraphicsLayoutWidget
import pyqtgraph as pg
from sys import argv, exit, path

''' Setup Main UI Window '''
app = QtWidgets.QApplication(argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()

if __name__ == "__main__":
    exit(app.exec_())
