From ebfd2aa5ff5e23834da1b61a232a9d683c7ebe42 Mon Sep 17 00:00:00 2001 From: Herwig Birke Date: Fri, 12 Aug 2022 14:59:05 +0200 Subject: [PATCH] initial commit --- ckeystonecomm.cpp | 199 +++++++++++++++++++++++++++++++++++++++++++++- ckeystonecomm.h | 11 +++ cmainwindow.cpp | 29 +++++++ cmainwindow.h | 5 ++ cmainwindow.ui | 143 ++++++++++++++++++++++++++++++--- 5 files changed, 372 insertions(+), 15 deletions(-) diff --git a/ckeystonecomm.cpp b/ckeystonecomm.cpp index 8ac1cf7..5b1d73c 100644 --- a/ckeystonecomm.cpp +++ b/ckeystonecomm.cpp @@ -54,6 +54,7 @@ cKeyStoneCOMM::cKeyStoneCOMM(const QString& commPort, QObject *parent) : m_hardMute{false}, m_initialized{false}, m_totalProgram{0}, + m_currentProgram{0}, m_mutePrevVolume{0} { } @@ -248,9 +249,17 @@ bool cKeyStoneCOMM::startStream(STREAM_MODE mode, uint16_t channel) if(!readSerialBytes(input, &dwBytes)) return(false); + m_currentProgram = channel; + m_currentMode = mode; + return(true); } +int16_t cKeyStoneCOMM::channel() +{ + return(m_currentProgram); +} + bool cKeyStoneCOMM::stopStream() { uint16_t dwBytes; @@ -446,8 +455,21 @@ int8_t cKeyStoneCOMM::volume() int8_t cKeyStoneCOMM::signalStrength() { - int biterror; - return(getSignalStrength(&biterror)); + int biterror; + int8_t signalStrength = getSignalStrength(&biterror); + if(signalStrength < 0) + signalStrength = 0; + return(signalStrength); +} + +QString cKeyStoneCOMM::programName() +{ + return(getProgramName(m_currentMode, m_currentProgram, 1)); +} + +QString cKeyStoneCOMM::programText() +{ + return(getProgramText()); } bool cKeyStoneCOMM::hardMute() @@ -766,6 +788,179 @@ int8_t cKeyStoneCOMM::getSignalStrength(int* biterror) return(-1); } +QString cKeyStoneCOMM::getProgramText() +{ + uint16_t dwBytes; + unsigned char input[TEXT_BUFFER_LEN] = {0}; + unsigned char output[] = {HEAD,0x01,0x2E,0x01,0x00,0x00,END}; + QString programText; + + if(!writeSerialBytes(output, 7, &dwBytes)) + return(programText); + + if(dwBytes != 7) + return(programText); + + if(!readSerialBytes(input, &dwBytes)) + return(programText); + + if(dwBytes < 8) + return(programText); + + if(goodHeader(input, dwBytes)) + { + if((input[1] == 0x01) && (input[2] == 0x2E)) + { + int textLen = (0xFF & input[4]) << 8 |(0xFF & input[5]); + + if(textLen > TEXT_BUFFER_LEN-1) + return(programText); // buffer overrun + + int unicodesize = textLen/2; + char front; + + for(int i = 0;i < textLen;i += 2) // WATCHOUT + { + front = input[6+i]; + input[6+i] = input[6+i+1]; + input[6+i+1] = front; + } +#ifdef WIN32 + programText = QString::fromWCharArray((wchar_t *)&input[6], unicodesize-1).trimmed(); + +#ifdef LABVIEW + int asciilen=WideCharToMultiByte(CP_ACP, 0, programText, unicodesize-1, NULL,0,NULL,NULL); + char *asciitext = new char[asciilen+1]; + WideCharToMultiByte(CP_ACP, 0, programText, unicodesize-1, asciitext, asciilen, NULL, NULL); + asciitext[asciilen]=0; + memset((char*)programText,0, asciilen+1); + memcpy((char*)programText, asciitext, asciilen); + delete asciitext; +#endif + +#endif + +#ifdef __linux__ + unsigned char tempBuffer[TEXT_BUFFER_LEN*4]={0}; + + for(int i=0; i< unicodesize;i++) + { + tempBuffer[i*4]=input[6+(i*2)]; + tempBuffer[(i*4)+1]=input[6+(i*2)+1]; + tempBuffer[(i*4)+2]=0; + tempBuffer[(i*4)+3]=0; + } + + programText = QString::fromWCharArray((wchar_t *)&tempBuffer[0], unicodesize-1).trimmed(); +#endif + return(0); + + } + else if((input[1] == 0x00) && (input[2] == 0x02)) + { + if(input[6] == 0x01) + return(programText); + else + return(programText); + } + else + return(programText); + } + else + return(programText); +} + +QString cKeyStoneCOMM::getProgramName(char mode, long dabIndex, char namemode) +{ + uint16_t dwBytes; + unsigned char input[50] = {0}; + unsigned char output[] = {HEAD,0x01,0x2D,0x01,0x00,0x05,0xFF,0xFF,0xFF,0xFF,0x01,END}; + unsigned char strChannel[4] = {0}; + QString programName; + + if((mode != STREAM_MODE_DAB) && (mode != STREAM_MODE_FM)) + return(programName); + + if(mode == STREAM_MODE_DAB) + { + memcpy(&strChannel[0], &dabIndex, sizeof(long)); + output[6] = strChannel[3]; + output[7] = strChannel[2]; + output[8] = strChannel[1]; + output[9] = strChannel[0]; + } + + output[10] = namemode; + + if(!writeSerialBytes(output, 12, &dwBytes)) + return(programName); + + if(dwBytes != 12) + return(programName); + + if(!readSerialBytes(input, &dwBytes)) + return(programName); + + if(dwBytes < 8) + return(programName); + + if(goodHeader(input, dwBytes)) + { + if((input[1] == 0x01) && (input[2] == 0x2D)) + { + int textLen = (int)input[5]; + + if(textLen > 50) + return(programName); // prevent buffer overrun + + int unicodesize = textLen/2; + char front; + + for(int i = 0;i < textLen;i += 2) + { + front = input[6+i]; + input[6+i] = input[6+i+1]; + input[6+i+1] = front; + } + +#ifdef WIN32 + programName = QString::fromWCharArray((wchar_t*)&input[6], unicodesize-1).trimmed(); + +#ifdef LABVIEW + int asciilen=WideCharToMultiByte(CP_ACP, 0, programName, unicodesize-1, NULL,0,NULL,NULL); + char *asciitext = new char[asciilen+1]; + WideCharToMultiByte(CP_ACP, 0, programName, unicodesize-1, asciitext, asciilen, NULL, NULL); + asciitext[asciilen]=0; + memset((char*)programName,0, asciilen+1); + memcpy((char*)programName, asciitext, asciilen); + delete asciitext; +#endif + +#endif + +#ifdef __linux__ + unsigned char tempBuffer[TEXT_BUFFER_LEN*4]={0}; + + for(int i=0; i< unicodesize;i++) + { + tempBuffer[i*4]=input[6+(i*2)]; + tempBuffer[(i*4)+1]=input[6+(i*2)+1]; + tempBuffer[(i*4)+2]=0; + tempBuffer[(i*4)+3]=0; + } + + programName = QString::fromWCharArray((wchar_t *)&tempBuffer[0], unicodesize-1).trimmed(); +#endif + return(programName); + + } + else + return(programName); + } + else + return(programName); +} + void cKeyStoneCOMM::serialReadReady() { m_readData.append(m_serialPort->readAll()); diff --git a/ckeystonecomm.h b/ckeystonecomm.h index 5213ebc..1250ce7 100644 --- a/ckeystonecomm.h +++ b/ckeystonecomm.h @@ -8,6 +8,9 @@ #include +#define TEXT_BUFFER_LEN 300 + + class cKeyStoneCOMM : public QObject { Q_OBJECT @@ -27,6 +30,7 @@ public: void close(); int16_t channelCount(); + int16_t channel(); STREAM_MODE playMode(); int16_t playIndex(); bool setVolume(int8_t volume); @@ -39,6 +43,8 @@ public: void volumeMute(); int8_t volume(); int8_t signalStrength(); + QString programName(); + QString programText(); signals: private: @@ -54,6 +60,8 @@ private: int m_cntReadData; uint16_t m_totalProgram; + uint16_t m_currentProgram; + STREAM_MODE m_currentMode; int8_t m_currentVolume; int8_t m_mutePrevVolume; @@ -61,6 +69,9 @@ private: bool hardUnmute(); bool hardResetRadio(); + QString getProgramText(); + QString getProgramName(char mode, long dabIndex, char namemode); + bool readSerialBytes (unsigned char* buffer, uint16_t* bytesreadreturn); bool writeSerialBytes(unsigned char* buffer, uint16_t nNumberOfBytesToWrite, uint16_t* lpNumberOfBytesWritten); bool goodHeader(unsigned char* input, uint16_t dwBytes); diff --git a/cmainwindow.cpp b/cmainwindow.cpp index d5ca111..f14e1e8 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -35,4 +35,33 @@ void cMainWindow::updateSignalStrength() { int8_t signalStrength = m_keystoneCOMM->signalStrength(); ui->m_signalStrength->setText(QString("%1").arg(signalStrength)); + ui->m_sender->setText(m_keystoneCOMM->programName()); + ui->m_text->setText(m_keystoneCOMM->programText()); + int16_t channel = m_keystoneCOMM->channel(); + ui->m_channel->setText(QString("%1").arg(channel)); + int8_t volume = m_keystoneCOMM->volume(); + ui->m_volume->setText(QString("%1").arg(volume)); } + +void cMainWindow::on_m_channelDown_clicked() +{ + m_keystoneCOMM->prevStream(); +} + + +void cMainWindow::on_m_channelUp_clicked() +{ + m_keystoneCOMM->nextStream(); +} + +void cMainWindow::on_m_volumeDown_clicked() +{ + m_keystoneCOMM->volumeMinus(); +} + + +void cMainWindow::on_m_volumeUp_clicked() +{ + m_keystoneCOMM->volumePlus(); +} + diff --git a/cmainwindow.h b/cmainwindow.h index 41182bf..b72eb82 100644 --- a/cmainwindow.h +++ b/cmainwindow.h @@ -27,6 +27,11 @@ private: public slots: void updateSignalStrength(); +private slots: + void on_m_channelDown_clicked(); + void on_m_channelUp_clicked(); + void on_m_volumeDown_clicked(); + void on_m_volumeUp_clicked(); }; #endif // CMAINWINDOW_H diff --git a/cmainwindow.ui b/cmainwindow.ui index 0c0c5d6..1805636 100644 --- a/cmainwindow.ui +++ b/cmainwindow.ui @@ -14,19 +14,136 @@ cMainWindow - - - - 20 - 20 - 49 - 16 - - - - TextLabel - - + + + + + + + Text: + + + + + + + Sender: + + + + + + + Signal: + + + + + + + Colume: + + + + + + + TextLabel + + + + + + + Channel: + + + + + + + channel - + + + + + + + TextLabel + + + + + + + TextLabel + + + + + + + TextLabel + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + TextLabel + + + + + + + channel + + + + + + + + volume - + + + + + + + volume + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + +