static void DoTestRoundTrip(const T *values, size_t numValues) { { wxFileOutputStream fileOut(wxT("test.txt")); wxTextOutputStream textOut(fileOut); for ( size_t n = 0; n < numValues; n++ ) { textOut << values[n] << endl; } } { wxFileInputStream fileIn(wxT("test.txt")); wxTextInputStream textIn(fileIn); T value; for ( size_t n = 0; n < numValues; n++ ) { textIn >> value; CPPUNIT_ASSERT( value == values[n] ); } } }
Interpreter::Interpreter(ConsoleWidget *console, VideoWidget *video, MonParameterDB *data, const QString &initScript) : m_mutexProg(QMutex::Recursive) { m_initScript = initScript; m_initScript.remove(QRegExp("^\\s+")); // remove initial whitespace m_console = console; m_video = video; m_pixymonParameters = data; m_pc = 0; m_programming = false; m_localProgramRunning = false; m_waiting = false; m_fastPoll = true; m_notified = false; m_running = -1; // set to bogus value to force update m_chirp = NULL; m_renderer = new Renderer(m_video, this); connect(m_console, SIGNAL(textLine(QString)), this, SLOT(command(QString))); connect(m_console, SIGNAL(controlKey(Qt::Key)), this, SLOT(controlKey(Qt::Key))); connect(this, SIGNAL(textOut(QString, QColor)), m_console, SLOT(print(QString, QColor))); connect(this, SIGNAL(error(QString)), m_console, SLOT(error(QString))); connect(this, SIGNAL(enableConsole(bool)), m_console, SLOT(acceptInput(bool))); connect(this, SIGNAL(prompt(QString)), m_console, SLOT(prompt(QString))); connect(this, SIGNAL(consoleCommand(QString)), m_console, SLOT(command(QString))); connect(this, SIGNAL(videoInput(VideoWidget::InputMode)), m_video, SLOT(acceptInput(VideoWidget::InputMode))); connect(m_video, SIGNAL(selection(int,int,int,int)), this, SLOT(handleSelection(int,int,int,int))); prompt(); m_run = true; }
Interpreter::Interpreter(ConsoleWidget *console, VideoWidget *video) : m_mutexProg(QMutex::Recursive) { m_console = console; m_video = video; m_pc = 0; m_programming = false; m_localProgramRunning = false; m_rcount = 0; m_waiting = false; m_fastPoll = true; m_notified = false; m_pendingCommand = NONE; m_running = -1; // set to bogus value to force update m_chirp = NULL; m_renderer = new Renderer(m_video); connect(m_console, SIGNAL(textLine(QString)), this, SLOT(command(QString))); connect(m_console, SIGNAL(controlKey(Qt::Key)), this, SLOT(controlKey(Qt::Key))); connect(this, SIGNAL(textOut(QString, QColor)), m_console, SLOT(print(QString, QColor))); connect(this, SIGNAL(error(QString)), m_console, SLOT(error(QString))); connect(this, SIGNAL(enableConsole(bool)), m_console, SLOT(acceptInput(bool))); connect(this, SIGNAL(prompt(QString)), m_console, SLOT(prompt(QString))); connect(this, SIGNAL(videoInput(VideoWidget::InputMode)), m_video, SLOT(acceptInput(VideoWidget::InputMode))); connect(m_video, SIGNAL(selection(int,int,int,int)), this, SLOT(handleSelection(int,int,int,int))); m_run = true; start(); }
void Interpreter::cprintf(const char *format, ...) { char buffer[256]; va_list args; va_start (args, format); vsprintf (buffer, format, args); emit textOut(buffer, Qt::blue); }
void AbstractGame::paintPosition(CDC &dc, int pos) { const Point2D &p = m_brickPositions[pos]; dc.Ellipse((int)(p.x-POSSIZE),(int)(p.y-POSSIZE),(int)(p.x+POSSIZE),(int)(p.y+POSSIZE)); if(m_showNumbers && isOccupied(pos)) { textOut(dc, (int)(p.x - ((pos>9)?8:4)), (int)(p.y-8), format(_T("%d"), pos)); } }
////////////////// // printf-like function for device context. // long WPDevContext::printF(WPPoint p, LPSTR format, ...) { char buf[128]; va_list argptr; va_start(argptr, format); int len = wvsprintf(buf, format, argptr); va_end(argptr); return textOut(p.x, p.y, buf, len); }
void Interpreter::handleSelection(int x0, int y0, int width, int height) { m_mutexInput.lock(); m_command = QString::number(x0) + " " + QString::number(y0) + " " + QString::number(width) + " " + QString::number(height) + "\n"; textOut(m_command); m_selection = RectA(x0, y0, width, height); m_key = (Qt::Key)0; m_waitInput.wakeAll(); m_mutexInput.unlock(); }
void Interpreter::printHelp() { ProcInfo info; ChirpProc p; for (p=0; true; p++) { if (m_chirp->getProcInfo(p, &info)<0) break; emit textOut(QString::number(p) + ": " + printProc(&info)); } }
////////////////// // Do line output. Automatically eject page properly. // int WPLinePrinter::outLine(LPCSTR buf, int len) { if (curLine==0) startPage(); if (buf[0]==CONTROL_L) // do form-feed for Control-L return formFeed(); if (curLine >= nLinesPage) formFeed(); textOut(0, yChar*curLine, buf, len); curLine++; return error(); }
void Interpreter::handleHelp() { ChirpProc proc; ProcInfo info; if (m_argv.size()==1) printHelp(); else if (m_argv.size()>1) { if ((proc=m_chirp->getProc(m_argv[1].toLocal8Bit()))>=0 && m_chirp->getProcInfo(proc, &info)>=0) emit textOut(printProc(&info, 1)); else emit error("can't find procedure.\n"); } }
////////////////////////// // Add word to dictionary void HunspellSpellChecker::AddWord(wxString word) { // Dictionary OK? if (!hunspell) return; // Add to currently loaded file hunspell->put_word(word.mb_str(*conv)); // Load dictionary wxArrayString dic; wxString curLine; if (!dicpath.IsEmpty()) { // Even if you ever want to remove this "if", keep the braces, so the stream closes at the end bool first = true; bool added = false; wxFileInputStream in(dicpath); if (!in.IsOk()) return; wxTextInputStream textIn(in,_T(" \t"),*conv); // Read it while (in.CanRead() && !in.Eof()) { // Read line curLine = textIn.ReadLine(); curLine.Trim(); // First if (first) { first = false; if (curLine.IsNumber()) continue; } // See if word to be added goes here if (!added && curLine.Lower() > word.Lower()) { dic.Add(word); added = true; } // Add to memory dictionary dic.Add(curLine); } } // Write back to disk wxFileOutputStream out(dicpath); if (!out.IsOk()) return; wxTextOutputStream textOut(out,wxEOL_UNIX,*conv); textOut.WriteString(wxString::Format(_T("%i"),dic.Count())+_T("\n")); for (unsigned int i=0;i<dic.Count();i++) textOut.WriteString(dic[i]+_T("\n")); }
YSRESULT YsShell::SaveSrf( const char fn[], YSSIZE_T nNoShadingPolygon,const YsShellPolygonHandle noShadingPolygon[], YSSIZE_T nRoundVtx,const YsShellVertexHandle roundVtx[]) const { FILE *fp; fp=fopen(fn,"w"); if(fp!=NULL) { YsTextFileOutputStream textOut(fp); YSRESULT res=SaveSrf(textOut,nNoShadingPolygon,noShadingPolygon,nRoundVtx,roundVtx); fclose(fp); return res; } return YSERR; }
void print_debug_info(Mouse *mouse, Transform *tf_world, long ticks) { //printText(true, rasterLeft, rasterBottom, "", 128, 128, 0); //printInt(true, rasterLeft, rasterBottom, mouse->x, 128, 128, 0); /*printText(false, 0, 0, ", ", 128, 128, 0); printInt(false, 0, 0, mouse->y, 128, 128, 0); printText(false, 0, 0, " - ", 128, 128, 0); printFloat(false, 0, 0, tf_world->scale, 128, 128, 0); printText(false, 0, 0, " - ", 128, 128, 0); printFloat(false, 0, 0, tf_world->translate_x, 128, 128, 0); printText(false, 0, 0, " :: ", 128, 128, 0);*/ textYellow(); textResetBottomLeft(); textOut("ticks: "); textOutInt(ticks); //printText(true, rasterLeft, rasterBottom, "ticks: ", 255, 255, 0); //printInt(true, rasterLeft, rasterBottom, ticks, 255, 255, 0); }
void Interpreter::handleData(void *args[]) { uint8_t type; QColor color = CW_DEFAULT_COLOR; if (args[0]) { type = Chirp::getType(args[0]); if (type==CRP_TYPE_HINT) { m_print += printType(*(uint32_t *)args[0]) + " frame data\n"; m_renderer->render(*(uint32_t *)args[0], args+1); } else if (type==CRP_HSTRING) { m_print += (char *)args[0]; color = Qt::blue; } else qDebug() << "unknown type " << type; } if (m_print.right(1)!="\n") m_print += "\n"; // wait queue business keeps worker thread from getting too far ahead of gui thread // (when this happens, things can get sluggish.) if (m_localProgramRunning || m_running) m_console->m_mutexPrint.lock(); emit textOut(m_print, color); m_print = ""; if (m_localProgramRunning || m_running) { m_console->m_waitPrint.wait(&m_console->m_mutexPrint); m_console->m_mutexPrint.unlock(); } }
void Interpreter::listProgram() { QMutexLocker locker(&m_mutexProg); QString print; unsigned int i; int j; for (i=0; i<m_programText.size(); i++) { if (m_programText[i].size()<1) continue; print += QString::number(i+1) + ": " + m_programText[i][0] + " "; for (j=1; j<m_programText[i].size(); j++) { if (j>1) print += ", "; print += m_programText[i][j]; } print += "\n"; } emit textOut(print); }
int Interpreter::call(const QStringList &argv, bool interactive) { ChirpProc proc; ProcInfo info; int args[20]; int i, j, k, n, base, res; bool ok; uint type; ArgList list; // not allowed if (argv.size()<1) return -1; // check modules to see if they handle this command, if so, skip to end emit enableConsole(false); for (i=0; i<m_modules.size(); i++) { if (m_modules[i]->command(argv)) return 0; } // a procedure needs extension info (arg info, etc) in order for us to call... if ((proc=m_chirp->getProc(argv[0].toLocal8Bit()))>=0 && m_chirp->getProcInfo(proc, &info)>=0) { memset(args, 0, sizeof(args)); // zero args getArgs(&info, &list); n = strlen((char *)info.argTypes); // if we have fewer args than required... if ((int)list.size()>argv.size()-1) { // if we're interactive, ask for values if (interactive && argv.size()>0) { QStringList cargv = argv; QString pstring, pstring2; for (i=cargv.size()-1; i<(int)list.size(); i++) { if (info.argTypes[i]==CRP_TYPE_HINT) { if (n>i+4) { type = *(uint *)&info.argTypes[i+1]; if (type==FOURCC('R','E','G','1')) { emit videoInput(VideoWidget::REGION); pstring2 = "(select region with mouse)"; } if (type==FOURCC('P','N','T','1')) { emit videoInput(VideoWidget::POINT); pstring2 = "(select point with mouse)"; } } } k = i; pstring = printArgType(&info.argTypes[i], i) + " " + list[k].first + (list[k].second=="" ? "?" : " (" + list[k].second + ")?") + " " + pstring2; emit enableConsole(true); emit prompt(pstring); m_mutexInput.lock(); m_waiting = true; m_waitInput.wait(&m_mutexInput); m_waiting = false; m_mutexInput.unlock(); emit prompt(PROMPT); emit enableConsole(false); if (m_key==Qt::Key_Escape) return -1; cargv << m_command.split(QRegExp("\\s+")); } // call ourselves again, now that we have all the args return call(cargv, true); } else { emit error("too few arguments.\n"); return -1; } } augmentProcInfo(&info); // if we have all the args we need, parse, put in args array for (i=0, j=0; m_argTypes[i]; i++) { if (argv.size()>i+1) { if (m_argTypes[i]==CRP_INT8 || m_argTypes[i]==CRP_INT16 || m_argTypes[i]==CRP_INT32) { args[j++] = m_argTypes[i]; if (argv[i+1].left(2)=="0x") base = 16; else base = 10; args[j++] = argv[i+1].toInt(&ok, base); if (!ok) { emit error("argument didn't parse.\n"); return -1; } } #if 0 else if (m_argTypes[i]==CRP_STRING) { args[j++] = m_argTypes[i]; // string goes where? can't cast pointer to int... } #endif else { // deal with non-integer types return -1; } } } #if 0 // print helpful chirp argument string if (interactive && argv.size()>1) { QString callString = "Chirp arguments for " + argv[0] + " (ChirpProc=" + QString::number(proc) + "): "; for (i=1; i<argv.size(); i++) { if (i>1) callString += ", "; j = i; callString += printArgType(&m_argTypes[i-1], i) + "(" + argv[j] + ")"; } emit textOut(callString + "\n"); } #endif // make chirp call res = m_chirp->callAsync(proc, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13], args[14], args[15], args[16], args[17], args[18], args[19], END_OUT_ARGS); // check for cable disconnect if (res<0 && !m_notified) //res==LIBUSB_ERROR_PIPE) { m_notified = true; emit connected(PIXY, false); return res; } // get response if we're not programming, save text if we are if (m_programming) addProgram(argv); else m_chirp->serviceChirp(); } else { emit error("procedure unsupported.\n"); return -1; } return 0; }
void Interpreter::run() { int res; QTime time; // init try { ChirpProc versionProc; uint16_t *version; uint32_t verLen, responseInt; if (m_link.open()<0) throw std::runtime_error("Unable to open USB device."); m_chirp = new ChirpMon(this, &m_link); // get version and compare versionProc = m_chirp->getProc("version"); if (versionProc<0) throw std::runtime_error("Can't get firmware version."); res = m_chirp->callSync(versionProc, END_OUT_ARGS, &responseInt, &verLen, &version, END_IN_ARGS); if (res<0) throw std::runtime_error("Can't get firmware version."); memcpy(m_version, version, 3*sizeof(uint16_t)); if (m_version[0]!=VER_MAJOR || m_version[1]>VER_MINOR) { char buf[0x100]; sprintf(buf, "This Pixy's firmware version (%d.%d.%d) is not compatible with this PixyMon version (%d.%d.%d).", m_version[0], m_version[1], m_version[2], VER_MAJOR, VER_MINOR, VER_BUILD); throw std::runtime_error(buf); } m_exec_run = m_chirp->getProc("run"); m_exec_running = m_chirp->getProc("running"); m_exec_stop = m_chirp->getProc("stop"); m_exec_get_action = m_chirp->getProc("getAction"); m_get_param = m_chirp->getProc("prm_get"); m_getAll_param = m_chirp->getProc("prm_getAll"); m_set_param = m_chirp->getProc("prm_set"); if (m_exec_run<0 || m_exec_running<0 || m_exec_stop<0 || m_exec_get_action<0 || m_get_param<0 || m_getAll_param<0 || m_set_param<0) throw std::runtime_error("Communication error with Pixy."); } catch (std::runtime_error &exception) { emit error(QString(exception.what())); return; } qDebug() << "*** init done"; time.start(); getRunning(); handleLoadParams(); // load params upon initialization while(m_run) { if (!m_programming && ((m_fastPoll && time.elapsed()>RUN_POLL_PERIOD_FAST) || (!m_fastPoll && time.elapsed()>RUN_POLL_PERIOD_SLOW))) { getRunning(); time.start(); } else { m_chirp->service(false); msleep(1); // give config thread time to run } handlePendingCommand(); if (!m_running) { if (m_localProgramRunning) execute(); else { Sleeper::msleep(10); if (m_mutexProg.tryLock()) { if (m_argv.size()) { if (m_externalCommand!="") // print command to make things explicit and all pretty emit textOut(PROMPT " " + m_externalCommand); if (m_argv[0]=="help") handleHelp(); else { res = call(m_argv, true); if (res<0) { if (m_programming) { endLocalProgram(); clearLocalProgram(); } m_commandList.clear(); // abort our little scriptlet } } m_argv.clear(); if (m_externalCommand=="") prompt(); // print prompt only if we expect an actual human to be typing into the command window else m_externalCommand = ""; // check quickly to see if we're running after this command if (!m_programming) getRunning(); // is there another command in our little scriptlet? if (m_commandList.size()) { execute(m_commandList[0]); m_commandList.removeFirst(); } } m_mutexProg.unlock(); } } } } sendStop(); msleep(200); // let things settle a bit qDebug("worker thead exiting"); }
void Interpreter::handleData(const void *args[]) { int i; uint8_t type; QColor color = CW_DEFAULT_COLOR; if (args[0]) { type = Chirp::getType(args[0]); if (type==CRP_TYPE_HINT) { if (g_debug) m_print += printType(*(uint32_t *)args[0]) + " data\n"; if (*(uint32_t *)args[0]==FOURCC('E','V','T','1')) { uint32_t event = *(uint32_t *)args[1]; if (event==EVT_PARAM_CHANGE) loadParams(); } else { // check modules, see if they handle the fourcc for (i=0; i<m_modules.size(); i++) { if (m_modules[i]->render(*(uint32_t *)args[0], args+1)) break; } } } else if (type==CRP_HSTRING) { m_print += (char *)args[0]; color = Qt::blue; } else DBG("unknown type: %d", type); } if (m_print.size()>0 && !m_print.endsWith('\n')) m_print += '\n'; // wait queue business keeps worker thread from getting too far ahead of gui thread // (when this happens, things can get sluggish.) // update: this causes the worker thread to block. For example, bringing up a file // dialog will cause the gui to block and a block in the gui causes ths console to block // the console blocks and the xdata console print blocks, blocking the worker thread. // Removing this fixes the issue. But it's now possible for the console prints to queue // up if they are coming in too fast, causing gui sluggishness. Limit size of queue? // Need some kind of throttling mechanism -- putting sleeps in the worker thread? Or // a call somewhere to processevents? #if 0 if (m_localProgramRunning || m_running) m_console->m_mutexPrint.lock(); #endif if (m_print.size()>0) { emit textOut(m_print, color); m_print = ""; } #if 0 if (m_localProgramRunning || m_running) { m_console->m_waitPrint.wait(&m_console->m_mutexPrint); m_console->m_mutexPrint.unlock(); } #endif }
void Interpreter::command(const QString &command) { QMutexLocker locker(&m_mutexInput); if (m_localProgramRunning) return; if (m_waiting) { m_command = command; m_command.remove(QRegExp("[(),\\t]")); m_key = (Qt::Key)0; m_waitInput.wakeAll(); return; } QStringList words = command.split(QRegExp("[\\s(),\\t]"), QString::SkipEmptyParts); if (words.size()==0) goto end; if (words[0]=="do") { clearLocalProgram(); beginLocalProgram(); } else if (words[0]=="done") { endLocalProgram(); runLocalProgram(); return; } else if (words[0]=="list") listProgram(); else if (words[0].left(4)=="cont") { if (runLocalProgram()>=0) return; } else if (words[0]=="rendermode") { if (words.size()>1) m_renderer->setMode(words[1].toInt()); else emit textOut("Missing mode parameter.\n"); } else if (words[0]=="region") { emit videoInput(VideoWidget::REGION); m_argvHost = words; } #if 0 else if (words[0]=="set") { if (words.size()==3) { words[1].remove(QRegExp("[\\s\\D]+")); m_renderer->m_blobs.setLabel(words[1], words[2]); } } #endif else { handleCall(words); return; // don't print prompt } end: prompt(); }