Prev: 2. Let's Play
Next: 4. Designing an Event-Driven Application
Perhaps the best place to start the explanation of the "Fly 'n' Shoot" application code is the main() function, located in the file main.cpp. Unless indicated otherwise in this Tutorial, you can browse the code either in the DOS version, or the Cortex-M3 version, because the application source code is identical in both. The complete main.cpp file is shown in Listing 3-1.
(1) #include "qp_port.h" (2) #include "bsp.h" (3) #include "game.h" // Local-scope objects ------------------------------------------------------- (4) static QEvent const * l_missileQueueSto[2]; (5) static QEvent const * l_shipQueueSto[3]; (6) static QEvent const * l_tunnelQueueSto[GAME_MINES_MAX + 5]; (7) static ObjectPosEvt l_smlPoolSto[GAME_MINES_MAX + 5]; // small event pool (8) static ObjectImageEvt l_medPoolSto[GAME_MINES_MAX + 5]; // medium event pool (9) static QSubscrList l_subscrSto[MAX_PUB_SIG]; //............................................................................ void main(int argc, char *argv[]) { (10) BSP_init(argc, argv); // initialize the Board Support Package (11) QF::init(); // initialize the framework and the underlying RT kernel // initialize the event pools... (12) QF::poolInit(l_smlPoolSto, sizeof(l_smlPoolSto), sizeof(l_smlPoolSto[0])); (13) QF::poolInit(l_medPoolSto, sizeof(l_medPoolSto), sizeof(l_medPoolSto[0])); (14) QF::psInit(l_subscrSto, Q_DIM(l_subscrSto)); // init publish-subscribe // start the active objects... (15) AO_Missile->start(1, // priority l_missileQueueSto, Q_DIM(l_missileQueueSto),// evt queue (void *)0, 0, // no per-thread stack (QEvent *)0); // no initialization event (16) AO_Ship ->start(2, // priority l_shipQueueSto, Q_DIM(l_shipQueueSto), // evt queue (void *)0, 0, // no per-thread stack (QEvent *)0); // no initialization event (17) AO_Tunnel ->start(3, // priority l_tunnelQueueSto, Q_DIM(l_tunnelQueueSto), // evt queue (void *)0, 0, // no per-thread stack (QEvent *)0); // no initialization event (18) QF::run(); // run the QF application }
<qpcpp>\ports\80x86\dos\tcpp101\l\, and the Cortex-M3 version from the directory <qpcpp>\ports\cortex-m3\vanilla\iar\.
BSP_init() initializes the board and is defined in the bsp.cpp file.a[] computed as sizeof(a)/sizeof(a[0]), which is a compile-time constant. The use of this macro simplifies the code because it allows me to eliminate many define constants that otherwise I would need to provide for the dimensions of various arrays. I can simply hard-code the dimension right in the definition of an array, which is the only place that I specify it. I then use the macro Q_DIM() whenever I need this dimension in the code.
AO_Ship.
main(). In an embedded system, such as the Cortex-M3 board, QF::run() runs forever or till the power is removed, whichever comes first.
1.5.4