4.4 KiB
~english \page using_basic Getting started ~russian \page using_basic Простые начала
~english
Many novice programmers face common tasks when interacting with the system: output to console, detecting keyboard presses, working with serial ports, ethernet or files, and more. This library addresses these tasks; code based on PIP will compile and work similarly on many systems: Windows, any Linux, Red Hat, FreeBSD, MacOS X and QNX. Typical application on PIP looks like this: \n
~russian
Многие начинающие программисты решают общие задачи взаимодействия с операционной системой: вывод в консоль, определение нажатия клавиш, работа с последовательными портами, сетью или файлами, и многое другое. Эти задачи решены в библиотеке, и код, основанный на PIP будет компилироваться и работать одинаково на многих системах: Windows, любой Linux, Red Hat, FreeBSD, MacOS X и QNX. Типовое приложение на PIP выглядит примерно так: \n
\code{.cpp} #include <pip.h>
// declare key press handler void key_event(char key, void * );
PIConsole console(false, key_event); // don`t start now, key handler is "key_event"
// some vars int i = 2, j = 3;
// implicit key press handler void key_event(char key, void * ) { switch (key) { case '-': i--; break; case '+': i++; break; case '(': j--; break; case ')': j++; break; }; };
class MainClass: public PITimer { PIOBJECT(MainClass) public: MainClass() {} protected: void tick(int delimiter) { piCout << "timer tick"; // timer tick } };
MainClass main_class;
int main(int argc, char * argv[]) { // enabling auto-detection of exit button press, by default 'Q' (shift+q) console.enableExitCapture();
// if we want to parse command-line arguments
PICLI cli(argc, argv);
cli.addArgument("console"); // "-c" or "--console"
cli.addArgument("debug"); // "-d" or "--debug"
// enabling or disabling global debug flag
piDebug = cli.hasArgument("debug");
// configure console
console.addTab("first tab", '1');
console.addString("PIP console", 1, PIConsole::Bold);
console.addVariable("int var (i)", &i, 1);
console.addVariable("int green var (j)", &j, 1, PIConsole::Green);
console.addString("'-' - i--", 2);
console.addString("'+' - i++", 2);
console.addString("'(' - j--", 2);
console.addString("')' - j++", 2);
console.addTab("second tab", '2');
console.addString("col 1", 1);
console.addString("col 2", 2);
console.addString("col 3", 3);
console.setTab("first tab");
// start output to console if "console" argument exists
if (cli.hasArgument("console"))
console.start();
// start main class, e.g. 40 Hz
main_class.start(25.);
// wait for 'Q' press, independently if console is started or not
console.waitForFinish();
return 0;
}; \endcode
~english
This code demonstrates simple interactive configurable program, which can be started with console display or not, and with debug or not. \b MainClass is central class that also can be inherited from \a PIThread and reimplement \a run() function. \n Many PIP classes have events and event handlers, which can be connected one to another. Details you can see at \a PIObject reference page (\ref PIObject_sec0). \n To configure your program from file use \a PIConfig (see \ref config). \n For more topics see \ref using_advanced.
~russian
Этот код демонстрирует простую конфигурируемую программу, которая может быть запущена с консолью или без неё, с отладочным выводом или без. \b MainClass — центральный класс, который также может быть унаследован от \a PIThread с переопределением \a run(). \n У многих классов PIP есть события и обработчики, которые можно связывать между собой. Подробности — на странице \a PIObject (\ref PIObject_sec0). \n Для настройки приложения из файла используйте \a PIConfig (см. \ref config). \n Дополнительные темы — на странице \ref using_advanced.