Refactoring and other updates
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_SimpleChat by
main_ble.cpp@7:806b08205b25, 2017-05-10 (annotated)
- Committer:
- carbune92
- Date:
- Wed May 10 07:15:19 2017 +0000
- Revision:
- 7:806b08205b25
- Parent:
- 5:fca87573ed92
fixed minor issues
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
carbune92 | 3:b6e4e5529a52 | 1 | #include "BTDevice.hpp" |
carbune92 | 3:b6e4e5529a52 | 2 | #include "ProcessInterface.hpp" |
carbune92 | 3:b6e4e5529a52 | 3 | |
carbune92 | 3:b6e4e5529a52 | 4 | //#define TE 0.1f //minim!!! - o cuanta!! - va fi multiplu al perioadei de esantionare configurabila |
carbune92 | 7:806b08205b25 | 5 | //TODO - de folosi RTOS timer in loc de Ticker pentru a avita folosirea ISR pentru functii periodice |
carbune92 | 7:806b08205b25 | 6 | |
carbune92 | 3:b6e4e5529a52 | 7 | |
carbune92 | 3:b6e4e5529a52 | 8 | static Ticker controlLoopTicker, cmdTicker; |
carbune92 | 3:b6e4e5529a52 | 9 | static ProcessInterface pInt; |
carbune92 | 3:b6e4e5529a52 | 10 | static BTDevice bt; |
carbune92 | 3:b6e4e5529a52 | 11 | |
carbune92 | 3:b6e4e5529a52 | 12 | void cmdCall(void); |
carbune92 | 3:b6e4e5529a52 | 13 | void controlLoopCall(void); |
carbune92 | 3:b6e4e5529a52 | 14 | |
carbune92 | 3:b6e4e5529a52 | 15 | int main () { |
carbune92 | 3:b6e4e5529a52 | 16 | |
carbune92 | 3:b6e4e5529a52 | 17 | bt.init(); |
carbune92 | 3:b6e4e5529a52 | 18 | // while (!bt.isInit()) {} |
carbune92 | 3:b6e4e5529a52 | 19 | |
carbune92 | 3:b6e4e5529a52 | 20 | cmdTicker.attach(cmdCall, TE); |
carbune92 | 3:b6e4e5529a52 | 21 | controlLoopTicker.attach(controlLoopCall, (TE+0.05f)); |
carbune92 | 3:b6e4e5529a52 | 22 | //bt.attachProcessInterface(pInt); |
carbune92 | 3:b6e4e5529a52 | 23 | |
carbune92 | 4:cfc5c2426e21 | 24 | Controller *myCtrl; |
carbune92 | 4:cfc5c2426e21 | 25 | ControllerFactory cf; |
carbune92 | 5:fca87573ed92 | 26 | cf.createController( &myCtrl, PID ); |
carbune92 | 4:cfc5c2426e21 | 27 | bt.attachController( myCtrl ); |
carbune92 | 5:fca87573ed92 | 28 | pInt.attachBTDevice( &bt ); |
carbune92 | 5:fca87573ed92 | 29 | pInt.attachController( bt.getCtrl() ); |
carbune92 | 3:b6e4e5529a52 | 30 | |
carbune92 | 3:b6e4e5529a52 | 31 | while (bt.isInit()) { |
carbune92 | 3:b6e4e5529a52 | 32 | bt.waitForEvent(); |
carbune92 | 3:b6e4e5529a52 | 33 | } |
carbune92 | 3:b6e4e5529a52 | 34 | |
carbune92 | 3:b6e4e5529a52 | 35 | return 0; |
carbune92 | 3:b6e4e5529a52 | 36 | } |
carbune92 | 3:b6e4e5529a52 | 37 | |
carbune92 | 3:b6e4e5529a52 | 38 | // DE VAZUT CALLBACKUL DE RECEIVE |
carbune92 | 3:b6e4e5529a52 | 39 | void cmdCall() { |
carbune92 | 3:b6e4e5529a52 | 40 | uint8_t cmd[TXRX_BUF_LEN]; |
carbune92 | 3:b6e4e5529a52 | 41 | bt.getPayload(cmd); |
carbune92 | 3:b6e4e5529a52 | 42 | //bt.interpretCmd(cmd); //only configuration commands!! (like PID Params of reference update) |
carbune92 | 3:b6e4e5529a52 | 43 | } |
carbune92 | 3:b6e4e5529a52 | 44 | |
carbune92 | 3:b6e4e5529a52 | 45 | void controlLoopCall() { |
carbune92 | 3:b6e4e5529a52 | 46 | |
carbune92 | 3:b6e4e5529a52 | 47 | //STATE_MACHINE: CONTROLLER, MANUAL, MONITOR ETC |
carbune92 | 3:b6e4e5529a52 | 48 | //we check if a controller exists (was prev set by a command) |
carbune92 | 3:b6e4e5529a52 | 49 | if (bt.ctrlExists()) { |
carbune92 | 3:b6e4e5529a52 | 50 | // controllers were attached in the command Callback to btDevice and ProcessInterface |
carbune92 | 3:b6e4e5529a52 | 51 | pInt.getProcOutput(); |
carbune92 | 3:b6e4e5529a52 | 52 | pInt.exportOutput(); |
carbune92 | 3:b6e4e5529a52 | 53 | bt.getCtrl()->calculateCmd(); |
carbune92 | 3:b6e4e5529a52 | 54 | pInt.updateCmd(); // no parameters means that we get the updated command from the Controller instance |
carbune92 | 3:b6e4e5529a52 | 55 | pInt.applyCmd(); |
carbune92 | 3:b6e4e5529a52 | 56 | } else { |
carbune92 | 3:b6e4e5529a52 | 57 | pInt.updateCmd(0.0f); // we directly pass the command value |
carbune92 | 3:b6e4e5529a52 | 58 | pInt.applyCmd(); |
carbune92 | 3:b6e4e5529a52 | 59 | } |
carbune92 | 3:b6e4e5529a52 | 60 | } |