int Interpreter::execute() { int res; emit runState(true); emit enableConsole(false); QMutexLocker locker(&m_mutexProg); while(1) { for (; m_pc<m_program.size(); m_pc++) { if (!m_localProgramRunning) { prompt(); res = 0; goto end; } res = m_chirp->execute(m_program[m_pc]); if (res<0) goto end; } m_pc = 0; } end: m_localProgramRunning = false; emit runState(false); emit enableConsole(true); return res; }
int stm_0() { std::default_random_engine de; std::uniform_int_distribution<int> di(10, 20); state_computation<int,std::uniform_int_distribution<int>> getrand = [&de] (std::uniform_int_distribution<int> s) { auto val = s(de); return state_tuple<int, std::uniform_int_distribution<int> >(val, s); }; state <int, std::uniform_int_distribution<int>> ST(getrand); std::function<char(int)> f = [] (int i) { if (i < 15) { return 'A'; } return 'Z'; }; int n = 10; while (n-- > 0) { auto SRT = functor<state>::fmap(f, ST); auto S = runState(SRT, di); std::cerr << S << std::endl;; } //auto SRT = functor<state>::fmap<std::uniform_int_distribution<int>>(f)(ST); auto SRT = functor<state>::fmap<std::uniform_int_distribution<int>>(f, ST); auto S = runState(SRT, di); std::cerr << S << std::endl;; return 0; }
int stm_3() { istack L = {1,2,3,4}; std::function<char(int)> f = [] (int i) { if (i < 3) { return 'A'; } return 'Z'; }; stack_comp pop = [] (istack s) { auto val = s.front(); s.pop_front(); return state_tuple<int, istack>(val, s); }; state <int, istack> SM(pop); auto F = applicative_functor<state>::pure<istack>(f); std::cerr << F << std::endl; auto r1 = runState(SM, L); std::cerr << r1 << std::endl;; auto SC = applicative_functor<state>::apply<istack, int, char>(F, SM); auto r2 = runState(SC, L); std::cerr << r2 << std::endl;; return 0; }
void Interpreter::handlePendingCommand() { QMutexLocker locker(&m_mutexQueue); if (m_commandQueue.empty()) return; const Command command = m_commandQueue.front(); m_commandQueue.pop_front(); locker.unlock(); switch (command.m_type) { case STOP: sendStop(); break; case RUN: sendRun(); break; case STOP_LOCAL: m_localProgramRunning = false; emit runState(false); break; case RUN_LOCAL: if (m_program.size()>0) { m_localProgramRunning = true; emit runState(true); emit enableConsole(false); m_pc = 0; } break; case LOAD_PARAMS: handleLoadParams(); break; case SAVE_PARAMS: handleSaveParams(command.m_arg0.toBool()); break; case UPDATE_PARAM: handleUpdateParam(); break; case CLOSE: emit runState(-1); break; } }
static state<B,S> bind(state<A,S> M, std::function< state<B,S> (A)> f) { state_computation<B,S> comp =[f,&M](S s) { auto res = runState(M, s); auto valr = res.value(); if (valr.second) { A a = valr.first; S new_state = res.state().first; state<B,S> state_g = f (a); return runState(state_g, new_state); } return state_tuple<B, S>(s); }; return state<B,S> (comp); }
void VtolLandFSM::Update() { runState(); if (GetCurrentState() != PFFSM_STATE_INACTIVE) { runAlways(); } }
void ZLQtSelectionDialog::accept() { if (handler().isOpenHandler()) { runNodeSlot(); } else { runState((const char*)myStateLine->text().utf8()); } }
void Interpreter::getRunning() { int res, running; res = m_chirp->callSync(m_exec_running, END_OUT_ARGS, &running, END_IN_ARGS); qDebug("running %d %d", res, running); if (res<0 && !m_notified) { running = false; m_notified = true; emit connected(PIXY, false); } // emit state if we've changed if (m_running!=running) { if (running==2) m_fastPoll = true; else m_fastPoll = false; m_running = running; emit runState(running); emit enableConsole(!running); if (!running && m_externalCommand=="") prompt(); // print prompt only if we expect an actual human to be typing into the command window, and we've stopped } }
int stm_1() { istack L = {1,2,3,4}; std::function<char(int)> f = [] (int i) { if (i < 3) { return 'A'; } return 'Z'; }; stack_comp pop = [] (istack s) { auto val = s.front(); s.pop_front(); return state_tuple<int, istack>(val, s); }; state <int, istack> SM(pop); std::cerr << SM << std::endl; state <char, istack> SMT = functor<state>::fmap(f, SM); std::cerr << SMT << std::endl; auto R = runState(SMT, L); std::cerr << R << std::endl; std::function<stack_comp(int)> push = [](int val) { return [val] (istack s) { s.push_front(val); return state_tuple<int, istack>(s); }; }; state <int, istack> SP(push(498)); std::cerr << SP << std::endl; auto S = runState(SP, L); std::cerr << S << std::endl; state <char, istack> SPT = functor<state>::fmap(f, SP); auto st = runState(SPT, L); std::cerr << st << std::endl; return 0; }
int stm_4() { istack L = {1,2,3,4}; std::function< state<int, istack> (int)> f = [] (int v) { std::function<stack_comp(int)> push = [](int val) { return [val] (istack s) { s.push_front(val); return state_tuple<int, istack>(s); }; }; std::cerr << "v : " << v << std::endl; if (v > 3) { state <int, istack> SP(push(v)); return SP; } stack_comp pop = [] (istack s) { auto val = s.front(); s.pop_front(); return state_tuple<int, istack>(val, s); }; state <int, istack> SP(pop); return SP; }; stack_comp pop = [] (istack s) { auto val = s.front(); s.pop_front(); return state_tuple<int, istack>(val, s); }; state <int, istack> SM(pop); auto R = monad<state>::bind<istack,int,int>(SM, f); std::cerr << R << std::endl; auto r0 = runState(SM, istack{1,4,5,3,1,8}); std::cerr << r0 << std::endl; auto r1 = runState(R, istack{1,4,5,3,1,8}); std::cerr << r1 << std::endl; return 0; }
void MainWindow::connectPixy(bool state) { if (state) // connect { try { if (m_pixyDFUConnected) // we're in programming mode { m_flash = new Flash(); if (m_firmwareFile!="") program(m_firmwareFile); else { QString dir; QFileDialog fd(this); dir = m_settings->value("fw_dialog").toString(); fd.setWindowTitle("Select a Firmware File"); fd.setDirectory(QDir(dir)); fd.setNameFilter("Firmware (*.hex)"); if (fd.exec()) { QStringList slist = fd.selectedFiles(); if (slist.size()==1 && m_flash) { program(slist.at(0)); } } dir = fd.directory().absolutePath(); m_settings->setValue("fw_dialog", QVariant(dir)); } } else { m_console->print("Pixy detected.\n"); m_interpreter = new Interpreter(m_console, m_video); m_initScriptExecuted = false; // reset so we'll execute for this instance connect(m_interpreter, SIGNAL(runState(uint)), this, SLOT(handleRunState(uint))); connect(m_interpreter, SIGNAL(finished()), this, SLOT(interpreterFinished())); connect(m_interpreter, SIGNAL(connected(Device,bool)), this, SLOT(handleConnected(Device,bool))); connect(m_interpreter, SIGNAL(actionScriptlet(int,QString,QStringList)), this, SLOT(handleActionScriptlet(int,QString,QStringList))); clearActions(); m_interpreter->getAction(0); // start action query process } m_pixyConnected = true; } catch (std::runtime_error &exception) { m_console->error(QString(exception.what())+"\n"); m_flash = NULL; m_pixyDFUConnected = false; m_pixyConnected = false; } } else // disconnect {
alt_32 update(void *context) { int i; for (i = 0; i < 4; i++) prev_state[i] = button_states[i]; for (i = 0; i < 4; i++) button_states[i] = getButton(i); readDat(); runState(); return 1; }
void Character::controller(int walkState, int turnState) { b2Vec2 desiredVelocity; b2Vec2 vel = m_Body->GetLinearVelocity(); b2Vec2 direction ; PlayerState State = STEADY; float desiredAngle = m_Body->GetAngle(); if (walkState == FOWARD) { State = WALK; direction = m_Body->GetWorldVector(b2Vec2(0,1.f)); } if (walkState == BACKWARD) { State = WALK; direction = m_Body->GetWorldVector(b2Vec2(0,-1.f)); } if (turnState == TURNLEFT) desiredAngle += -5.f * DEGTORAD; if (turnState == TURNRIGHT) desiredAngle += 5.f * DEGTORAD; runState(State); m_AniSprite->Update(); draw(); desiredVelocity = m_Body->GetWorldVector(direction); m_Body->ApplyLinearImpulse(direction , m_Body->GetWorldCenter()); m_Body->ApplyLinearImpulse(-0.1 * m_Body->GetMass() * m_Body->GetLinearVelocity(), m_Body->GetWorldCenter()); float nextAngle = m_Body->GetAngle() + m_Body->GetAngularVelocity()/ 60; //sf::Vector2i targetPos = sf::Mouse::getPosition(*m_Window) - sf::Vector2i(m_Window->getSize().x/2, m_Window->getSize().y/2); //float desiredAngle = atan2f(-targetPos.x, targetPos.y); float totalRotation = desiredAngle - nextAngle; while (totalRotation < -180 * DEGTORAD) totalRotation+=360 * DEGTORAD; while (totalRotation > 180 * DEGTORAD) totalRotation-=360 * DEGTORAD; float desiredAngularVelocity = totalRotation * 60; m_Body->ApplyAngularImpulse(m_Body->GetInertia() * desiredAngularVelocity); if (sf::Keyboard::isKeyPressed(sf::Keyboard::F)) { keyTemp++; if (keyTemp == 1) driveHandler(); //Make Sure driveHandler() only called once } else { keyTemp = 0; } if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) { new Bullet(m_Body->GetWorld(), m_Body->GetWorldPoint(b2Vec2(0,1)) , getNormal()); } for (unsigned int i = 0; i < VehicleCache.size(); i++) { setVehicle(VehicleCache[i]); } VehicleCache.clear(); }
int stm_2() { istack L = {1,2,3,4}; auto ST = applicative_functor<state>::pure<istack>(5); std::cerr << ST << std::endl; auto S = runState(ST, L); std::cerr << S << std::endl;; return 0; }
int main(int argc __attribute__((unused)), char *argv[]) { /* init allegro library */ ALLEGRO_TIMER *timer = NULL; al_init(); /* prepare random number generator */ srand(time(NULL)); /* activate all engine subsystems using the startup macro */ resetCollisionTable(); STARTUP(videoInit()) STARTUP(init_datafile(argv)) STARTUP(al_install_keyboard()) STARTUP(al_install_joystick()) STARTUP(fontInit()) STARTUP(setupSound()) timer = al_create_timer(1.0 / 60); al_start_timer(timer); initMediaLib(); initBuffers(); /* prepare game */ startGame(); /* begin game loop */ while(!state.terminate) { runState(timer); } /* finish, return control to os */ al_destroy_timer(timer); shutdownState(); shutdownGame(); videoKill(); shutdownSound(); al_uninstall_system(); PHYSFS_deinit(); /* no problems, exit false */ return EXIT_SUCCESS; }
bool ZLGtkSelectionDialog::run() { while (gtk_dialog_run(myDialog) == GTK_RESPONSE_ACCEPT) { if (myNodeSelected || handler().isOpenHandler()) { GtkTreeSelection *selection = gtk_tree_view_get_selection(myView); GtkTreeModel *dummy; GtkTreeIter iter; if (gtk_tree_selection_get_selected(selection, &dummy, &iter)) { int index; gtk_tree_model_get(GTK_TREE_MODEL(myStore), &iter, 2, &index, -1); const std::vector<ZLTreeNodePtr> &nodes = handler().subnodes(); if ((index >= 0) && (index < (int)nodes.size())) { runNode(nodes[index]); } } myNodeSelected = false; } else { runState(gtk_entry_get_text(myStateLine)); } if (myExitFlag) { return true; } } return false; }
void Interpreter::getRunning() { int res, running; res = m_chirp->callSync(m_exec_running, END_OUT_ARGS, &running, END_IN_ARGS); DBG("running %d %d", res, running); if (res<0 && !m_notified) { running = false; m_notified = true; emit connected(PIXY, false); } // emit state if we've changed if (m_running!=running) { if (running==2) m_fastPoll = true; else m_fastPoll = false; m_running = running; emit runState(running); emit enableConsole(!running); } }
status = H5Sclose(dataspace); // delete temp buffers delete iReds; delete iGreens; } else { std::cout <<"MajorityVoteProblem::writeSingleTimeHDF5 called.\n"; // count numbers int nreds=0; int ngreens=0; // we assume that the buffer is int .. const int *iBuffer = &(runState().front()); int scount = 0; // running counter for species BOOST_FOREACH(Species *s, species()) { // count all particles for this species int count=0; for (int x=0; x<gridWidth(); x++) for (int y=0; y<gridHeight(); y++) count += iBuffer[x + y*gridWidth() + scount*gridArea()]; // is it red or green? if ((s->id().c_str())[0]=='R') { //std::cout <<"Species "<<s->id().c_str()<<" is RED. Found "<<count <<" particles.\n"; nreds += count;
void MajorityVoteProblem::writeSingleTimeHDF5(hid_t currentDataGroup, const char *buffer, size_t bufferStep, hid_t bufferType) { if (m_individual) { // just call the standard implementation DiffusionModel::writeSingleTimeHDF5(currentDataGroup, buffer, bufferStep, bufferType); } if (m_isNonDiffusive) { std::cout <<"MajorityVoteProblem::writeSingleTimeHDF5 called for non-diffusive system.\n"; // we assume that the buffer is int .. const int *iBuffer = &(runState().front()); // we need two new buffers int *iReds = new int[gridArea()]; int *iGreens = new int[gridArea()]; int scount = 0; // running counter for species // initialize buffers for (int x=0; x<gridWidth(); x++) { for (int y=0; y<gridHeight(); y++) { iReds[x+y*gridWidth()]=0; iGreens[x+y*gridWidth()]=0; } } for (int x=0; x<gridWidth(); x++) { for (int y=0; y<gridHeight(); y++) { scount=0; BOOST_FOREACH(Species *s, species()) { // is it red or green? if ((s->id().c_str())[0]=='R') { //std::cout <<"Species "<<s->id().c_str()<<" is RED. Found "<<count <<" particles.\n"; iReds[x+y*gridWidth()] += iBuffer[x + y*gridWidth() + scount*gridArea()]; } else if ((s->id().c_str())[0]=='G') { //std::cout <<"Species "<<s->id().c_str()<<" is GREEN. Found "<<count <<" particles.\n"; iGreens[x+y*gridWidth()] += iBuffer[x + y*gridWidth() + scount*gridArea()]; } else { //std::cout <<"Species "<<s->id().c_str()<<" is UNKNOWN. Found "<<count <<" particles.\n"; } // increase species index scount++; } // DEBUG: check if total species count agrees for each cell //if ((iReds[x+y*gridWidth()] + iGreens[x+y*gridWidth()]) != 150) { // std::cerr <<"ERROR: at x="<<x<<" and y = "<<y<<" we find r="<<iReds[x+y*gridWidth()]<<" and g="<<iGreens[x+y*gridWidth()]<<".\n"<<std::flush; // exit(1); // } } // dimensions of the dataset std::cout <<"Grid dimensions are: "<<gridWidth()<<" x "<<gridHeight()<<".\n"; hsize_t dims[2]; dims[0] = gridWidth(); dims[1] = gridHeight(); // data set and data space hid_t dataspace, dataset; // status variable herr_t status; dataspace = H5Screate_simple(2, dims, 0); dataset = H5Dcreate2(currentDataGroup, "Red", H5T_NATIVE_INT, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); status = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, iReds); status = H5Dclose(dataset); status = H5Sclose(dataspace); dataspace = H5Screate_simple(2, dims, 0); dataset = H5Dcreate2(currentDataGroup, "Green", H5T_NATIVE_INT, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); status = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, iGreens); status = H5Dclose(dataset); status = H5Sclose(dataspace); // delete temp buffers delete iReds; delete iGreens; } else {
void VtolBrakeFSM::Update() { runState(); }