bool StateMachine::checkTimes(){ //Comparer les heures d'arrivées de mySequence et de sequenceBuffer. en prenant en compte deltaTime //Si on est bon renvoyer true sinon false std::cout<<"about to check times"<<std::endl; bool result = true; double stepTime; double normalizedTime; //remove delay double timeDelay = sequenceBuffer[0]->getTimeStamp(); for(int i = 0 ; i<sequenceBuffer.size();i++){ sequenceBuffer[i]->substractTime(timeDelay); } double estimatedMusicalTime = sequenceBuffer[1]->getTimeStamp() / mySequence[1]->getTimeStamp(); for(int i = 1; i< mySequence.size(); i++){ stepTime = sequenceBuffer[i]->getTimeStamp(); normalizedTime = mySequence[i]->getTimeStamp(); std::cout<<"checkTimes i : " << i << "\n stepTime =" << stepTime<< "\n wanted Time = " << normalizedTime << "\n estimated tempo :" << estimatedMusicalTime << std::endl; if (!(stepTime< normalizedTime * estimatedMusicalTime + authorizedTime && stepTime > normalizedTime*estimatedMusicalTime - authorizedTime)){ std::cout<<"result = false. i : " << i << std::endl; result = false; }else{ estimatedMusicalTime = (estimatedMusicalTime*i + (stepTime / normalizedTime)) / (i+1); } } resetMachine(); return result; }
bool StateMachine::updateMachine(MidiEvent* message){ bool result = false; //Comparer ce MidiEvent avec le MidiEvent correspondant à sequenceBuffer.size() dans mySequence if(message->getNote() == mySequence[sequenceBuffer.size()]->getNote()){ std::cout<<"Event matched with " << name << " steps confirmed : "<< sequenceBuffer.size() + 1 <<std::endl; //Instancier une copie de la machine avant de modifier sequenceBuffer. createClone(); //Si il est bon on ajoute le message dans le sequenceBuffer sequenceBuffer.push_back(message); //si sequenceBuffer.size() == mySequence.size() -> lancer checkTimes() (on a trouvé assez de notes) if(sequenceBuffer.size() == mySequence.size()){ result = checkTimes(); } }else if(sequenceBuffer.size() != 0){ //Si ça fait trop longtemps, on détruit la machine / ou bien on la vide. if(message->getTimeStamp() - sequenceBuffer[sequenceBuffer.size()]->getTimeStamp() > timeBeforeRestart){ if(!isCopy){ resetMachine(); }else{ mAnalyzer->removeMachine(this); } } } return result; }
void IoModule::logic_init_lamp_machinestat() { pMachine = new QStateMachine(this); m_pTimer = new QTimer(this); m_pTimer->setSingleShot(true); QState *pInitS = new QState(); QState *pInitS1 = new QState(pInitS); QState *pInitS2 = new QState(pInitS); QState *pInitS3 = new QState(pInitS); QState *pInitS4 = new QState(pInitS); pInitS->setInitialState(pInitS1); pInitS1->addTransition(m_pTimer, SIGNAL(timeout()), pInitS2); pInitS2->addTransition(m_pTimer, SIGNAL(timeout()), pInitS3); pInitS3->addTransition(m_pTimer, SIGNAL(timeout()), pInitS4); pInitS4->addTransition(this, SIGNAL(resetMachine()), pInitS1); QState *pOperationS = new QState(); //pInitS->addTransition(this, SIGNAL(initLampSuccess()),pOperationS); pMachine->addState(pInitS);//³õʼ»¯×´Ì¬; pMachine->addState(pOperationS);//²Ù×÷״̬; pMachine->setInitialState(pInitS); connect(pInitS1, SIGNAL(entered()), this, SLOT(init_s1())); connect(pInitS2, SIGNAL(entered()), this, SLOT(init_s2())); connect(pInitS3, SIGNAL(entered()), this, SLOT(init_s3())); connect(pInitS4, SIGNAL(entered()), this, SLOT(init_s4())); connect(pOperationS, SIGNAL(entered()), this, SLOT(operation_s())); }
void IoModule::initLamp( quint8 which ) { whichLamp = which; if(!pMachine->isRunning()) pMachine->start(); else emit(resetMachine()); }
int main(int argc, char * argv[]){ WINDOW * win; initscr(); clear(); noecho(); cbreak(); curs_set(0); m = newMachine(); if(argc == 2){ loadMachine(m,argv[1]); } win = newwin(WINDOW_SIZE_HEIGHT,WINDOW_SIZE_WIDTH,2,2); keypad(win,TRUE); refresh(); createDefaultView(win); updateView(win); char read; int c = 0; while(c!='q'){ c = wgetch(win); switch(c){ // all these if statements are just edge cases where we wrap around // the table case KEY_UP: if(section == SECTION_MEMORY){ if(highlight[section] < 16) highlight[section] = 255 - (15 - highlight[section]); else highlight[section]-=16; } break; case KEY_DOWN: if(section == SECTION_MEMORY){ if(highlight[section] < 256 && highlight[section] >= 240) highlight[section] = highlight[section] - 16*15; else highlight[section]+=16; } break; case KEY_LEFT: if(section == SECTION_MEMORY){ if(highlight[section]%16==0) highlight[section] += 15; else highlight[section]--; }else if(section == SECTION_REGISTERS){ if(highlight[section] == 0) highlight[section] = 15; else highlight[section]--; } break; case KEY_RIGHT: if(section == SECTION_MEMORY){ if((highlight[section]-15) % 16 ==0 ) highlight[section] -= 15; else highlight[section]++; }else if(section == SECTION_REGISTERS){ if(highlight[section] == 15) highlight[section] = 0; else highlight[section]++; } break; // tab key case 9: // toggle section section++; section = section%3; break; case 'r': run(m); break; case 's': saveMachine(m); break; case 'c': resetMachine(m); break; // enter key case 10: read = readInput(win); if(section == SECTION_MEMORY) writeMemoryAt(m, highlight[section], read); else if(section == SECTION_REGISTERS) writeRegistersAt(m, highlight[section], read); else writePC(m, read); break; case 'n': step(m); break; default: refresh(); break; } updateView(win); } clrtoeol(); refresh(); endwin(); return 0; }