void loop() { receiveBuffer(); transmitBuffer(); struct timeval end; long mtime, seconds, useconds; gettimeofday(&end, NULL); seconds = end.tv_sec - startLoop.tv_sec; useconds = end.tv_usec - startLoop.tv_usec; mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; if( (!serial.isBusySending()) && mtime>10) { gettimeofday(&startLoop, NULL); send_buffer.push(1); for(int i=0;i<SEND_BUFFER_SIZE-1;i++){ send_buffer.push(2); } serial.sendSerialPacket( &send_buffer ); uiTxPacketCtr++; } seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; if(mtime > RUNTIME_MS){ printf("%d packets received\n%d packets sent\nRX data rate = %dbytes/s\nTX data rate = %dbytes/s\n", uiRxPacketCtr, uiTxPacketCtr, uiRxPacketCtr*(SEND_BUFFER_SIZE-1)*1000/RUNTIME_MS, uiTxPacketCtr*(SEND_BUFFER_SIZE-1)*1000/RUNTIME_MS); exit(0); } }
/*------------------------------------ cmd_List ---*/ Pvoid cmd_List(SpeakPtr xx, PSymbol message, short argc, PAtom argv) { #pragma unused(message) bool okSoFar = true; EnterCallback(); if (xx && xx->fSpeaker) { if (xx->fPaused) { xx->fActive = xx->fPaused = false; StopSpeechAt(xx->fSpeaker, kImmediate); outlet_bang(xx->fDoneOut); } else if (xx->fActive) { LOG_POST_1(OUTPUT_PREFIX "previous speech not finished") xx->fActive = false; StopSpeechAt(xx->fSpeaker, kImmediate); outlet_bang(xx->fDoneOut); } clearDataBuffer(*xx->fBuffer); for (short ii = 0; okSoFar && (ii < argc); ++ii) okSoFar = addAtomToBuffer(OUR_NAME, *xx->fBuffer, &argv[ii]); if (okSoFar) transmitBuffer(xx); } ExitMaxMessageHandler() } /* cmd_List */
/*------------------------------------ cmd_Int ---*/ Pvoid cmd_Int(SpeakPtr xx, long msg) { EnterCallback(); if (xx) { clearDataBuffer(*xx->fBuffer); addLongToBuffer(OUR_NAME, *xx->fBuffer, msg); transmitBuffer(xx); } ExitMaxMessageHandler() } /* cmd_Int */
static void transmitted(Worklet *base) { auto packet = frg::container_of(base, &RecvInlineBase::worklet); packet->callback(packet->error(), packet->transmitBuffer()); }
//--------------------------------------------------------------------------- // protected //--------------------------------------------------------------------------- // thread body void NetThread::run() { if (settings->getOutputLog()) cout << "[" << name << "]" << " start thread...start run()" << endl << flush; // start thread runThread = true; // main loop while(runThread){ // set start time of main loop startTime = QDateTime::currentDateTime().toMSecsSinceEpoch(); //QThread::msleep(5); // 5 (ms) sleep QThread::msleep(threadSleepTime); // threadSleepTime (ms) sleep CONNECT_RESULT result_connect = connectToServer(); if (result_connect != CONNECT_SUCCEEDED){ if (result_connect == CONNECT_WAITED_COUNT){ continue; } if (result_connect == CONNECT_FAILED){ //cout << "[" << name << "]" << " connect Error: connectToServer()" << endl << flush; // error shutdownConnection(); emit networkError(true); //emit outputLogMessage(QTB_MSG_CONNECT_ERROR); continue; } if (result_connect == CONNECT_FAILED_RETRY){ if (settings->getOutputLog()) cout << "[" << name << "]" << " connect Error: connectToServer()" << endl << flush; // error runThread = false; break; } } // process header (send and receive) PROCESS_RESULT result_process = processForHeader(); if (result_process != PROCESS_SUCCEEDED){ if (result_process == PROCESS_RESTART){ // restart shutdownConnection(); QThread::sleep(2); emit networkError(true); continue; } if (result_process == PROCESS_NETWORK_ERROR){ // error if (settings->getOutputLog()) cout << "[" << name << "]" << " Network Error: processForHeader()" << endl << flush; // error shutdownConnection(); emit networkError(true); continue; } if (result_process == PROCESS_PASSWORD_ERROR){ // error if (settings->getOutputLog()) cout << "[" << name << "]" << " Password Error: processForHeader()" << endl << flush; // error shutdownConnection(); runThread = false; emit networkError(false); emit outputLogMessage(QTB_MSG_PASSWORD_ERROR); break; } if (result_process == PROCESS_CONNECT_ERROR){ // error if (settings->getOutputLog()) cout << "[" << name << "]" << " Connect Error: processForHeader()" << endl << flush; // error shutdownConnection(); runThread = false; emit networkError(false); emit outputLogMessage(QTB_MSG_ALREADY_CONNECT_ANOTHER_CLIENT); break; } if (result_process == PROCESS_VIDEO_MODE_ERROR){ // error if (settings->getOutputLog()) cout << "[" << name << "]" << " Connect Error: processForHeader()" << endl << flush; // error shutdownConnection(); runThread = false; emit networkError(false); emit outputLogMessage(QTB_MSG_NOTSUPPORT_VIDEO_MODE); break; } if (result_process == PROCESS_UNKNOWN_ERROR){ // error if (settings->getOutputLog()) cout << "[" << name << "]" << " Unkown Error: processForHeader()" << endl << flush; // error shutdownConnection(); runThread = false; emit networkError(false); emit outputLogMessage(QTB_MSG_UNKNOWN_ERROR); break; } } // transmit local buffer to device buffer TRANSMIT_RESULT result_transmit = transmitBuffer(); switch(result_transmit){ case TRANSMIT_SUCCEEDED: // Nothing to do break; case TRANSMIT_RESTART: // restart shutdownConnection(); QThread::sleep(2); emit networkError(true); continue; break; case TRANSMIT_NETWORK_ERROR: if (settings->getOutputLog()) cout << "[" << name << "]" << " Failed: transmitBuffer(): network error" << endl << flush; // error shutdownConnection(); emit networkError(true); continue; break; case TRANSMIT_DATASIZE_ERROR: if (settings->getOutputLog()) cout << "[" << name << "]" << " Failed: transmitBuffer(): data size" << endl << flush; // error break; case TRANSMIT_FAILED_PUT_BUFFER: if (settings->getOutputLog()) cout << "[" << name << "]" << " Failed: transmitBuffer(): put buffer." << endl << flush; // error break; case TRANSMIT_FAILED_TRANSMIT_DEVICE_BUFFER: if (settings->getOutputLog()) cout << "[" << name << "]" << " Failed: transmitBuffer(): transmit device buffer." << endl << flush; // error break; case TRANSMIT_FAILED_IMAGE_DRAW: if (settings->getOutputLog()) cout << "[" << name << "]" << " Failed: transmitBuffer(): image draw." << endl << flush; // error break; default: // error ABORT(); break; } } // disconnect shutdownConnection(); if (settings->getOutputLog()) cout << "[" << name << "]" << " stop thread...exit run()" << endl << flush; }
void control() { char ch=0; if(!usart0_Rx(&ch)) { ch=0; } switch(controlState) { case ControlMenu : { menu(); controlState=ControlIdle; break; } case ControlIdle : { if((ch=='L')||(ch=='l')) { controlState=ControlLoggerPassthrough; } else if((ch=='D')||(ch=='d')) { controlState=ControlDataStream; } // else if(((ch=='R')||(ch=='r'))|| // (!(PINK&0x80)) ) else if(((ch=='R')||(ch=='r'))) { startRecording(); autoStop=0; idleTicks=0; idleSeconds=0; controlState=ControlLogging; } else if((ch=='F')||(ch=='f')) { char buffer[80]; controlState=ControlRecoverFileListA; sprintf(buffer,"ls *.log\r"); transmitNullTerminatedBuffer(usart2_Tx,(uint8_t *)buffer); } else if(ch!=0) { menu(); } break; } case ControlLoggerPassthrough : { if(ch==27) { controlState=ControlMenu; } else if(ch!=0) { usart2_Tx(ch); } break; } case ControlDataStream : { if(ch==27) { controlState=ControlMenu; } break; } case ControlLogging : { // if((ch==27)|| // (PINK&0x80)) if((ch==27)) { controlState=ControlMenu; _delay_ms(500); char buffer[3]={26,26,26}; // stop logging sequence transmitBuffer(usart2_Tx,(uint8_t *)buffer,3); menu(); } break; } } }