Kevin Abraham
/
MobileArcade
Platform controller for the Mobile Arcade project
main.cpp@0:d4e7f9c1287f, 2018-11-30 (annotated)
- Committer:
- abraha2d
- Date:
- Fri Nov 30 20:15:39 2018 +0000
- Revision:
- 0:d4e7f9c1287f
- Child:
- 1:113469a23547
Initial code for motor control and BT interface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
abraha2d | 0:d4e7f9c1287f | 1 | #include "mbed.h" |
abraha2d | 0:d4e7f9c1287f | 2 | #include "Motor.h" |
abraha2d | 0:d4e7f9c1287f | 3 | |
abraha2d | 0:d4e7f9c1287f | 4 | DigitalOut led1(LED1); |
abraha2d | 0:d4e7f9c1287f | 5 | |
abraha2d | 0:d4e7f9c1287f | 6 | // TB6612 mbed |
abraha2d | 0:d4e7f9c1287f | 7 | // PWMA 21 |
abraha2d | 0:d4e7f9c1287f | 8 | // AI2 22 |
abraha2d | 0:d4e7f9c1287f | 9 | // AI1 23 |
abraha2d | 0:d4e7f9c1287f | 10 | // BI1 24 |
abraha2d | 0:d4e7f9c1287f | 11 | // BI2 25 |
abraha2d | 0:d4e7f9c1287f | 12 | // PWMB 26 |
abraha2d | 0:d4e7f9c1287f | 13 | |
abraha2d | 0:d4e7f9c1287f | 14 | Motor l_motor(p21, p23, p22); // PWMA, AI1, AI2 |
abraha2d | 0:d4e7f9c1287f | 15 | Motor r_motor(p26, p24, p25); // PWMB, BI1, BI2 |
abraha2d | 0:d4e7f9c1287f | 16 | |
abraha2d | 0:d4e7f9c1287f | 17 | Serial bt(p28, p27); // Bluefruit RX, TX |
abraha2d | 0:d4e7f9c1287f | 18 | Thread btThread; |
abraha2d | 0:d4e7f9c1287f | 19 | |
abraha2d | 0:d4e7f9c1287f | 20 | void bt_thread() |
abraha2d | 0:d4e7f9c1287f | 21 | { |
abraha2d | 0:d4e7f9c1287f | 22 | char bnum = 0; |
abraha2d | 0:d4e7f9c1287f | 23 | char bhit = 0; |
abraha2d | 0:d4e7f9c1287f | 24 | while(1) { |
abraha2d | 0:d4e7f9c1287f | 25 | while (!bt.readable()) wait(0.01); |
abraha2d | 0:d4e7f9c1287f | 26 | if (bt.getc() == '!' && bt.getc() == 'B') { |
abraha2d | 0:d4e7f9c1287f | 27 | bnum = bt.getc(); // button number |
abraha2d | 0:d4e7f9c1287f | 28 | bhit = bt.getc(); // 1 = hit, 0 = release |
abraha2d | 0:d4e7f9c1287f | 29 | if (bt.getc() == char(~('!' + 'B' + bnum + bhit))) { |
abraha2d | 0:d4e7f9c1287f | 30 | switch (bnum) { |
abraha2d | 0:d4e7f9c1287f | 31 | case '1': // number button 1 |
abraha2d | 0:d4e7f9c1287f | 32 | l_motor.speed(bhit == '1' ? 1.0f : 0); |
abraha2d | 0:d4e7f9c1287f | 33 | break; |
abraha2d | 0:d4e7f9c1287f | 34 | case '2': // number button 2 |
abraha2d | 0:d4e7f9c1287f | 35 | r_motor.speed(bhit == '1' ? 1.0f : 0); |
abraha2d | 0:d4e7f9c1287f | 36 | break; |
abraha2d | 0:d4e7f9c1287f | 37 | case '3': // number button 3 |
abraha2d | 0:d4e7f9c1287f | 38 | l_motor.speed(bhit == '1' ? -1.0f : 0); |
abraha2d | 0:d4e7f9c1287f | 39 | break; |
abraha2d | 0:d4e7f9c1287f | 40 | case '4': // number button 4 |
abraha2d | 0:d4e7f9c1287f | 41 | r_motor.speed(bhit == '1' ? -1.0f : 0); |
abraha2d | 0:d4e7f9c1287f | 42 | break; |
abraha2d | 0:d4e7f9c1287f | 43 | default: |
abraha2d | 0:d4e7f9c1287f | 44 | break; |
abraha2d | 0:d4e7f9c1287f | 45 | } |
abraha2d | 0:d4e7f9c1287f | 46 | } |
abraha2d | 0:d4e7f9c1287f | 47 | } |
abraha2d | 0:d4e7f9c1287f | 48 | } |
abraha2d | 0:d4e7f9c1287f | 49 | } |
abraha2d | 0:d4e7f9c1287f | 50 | |
abraha2d | 0:d4e7f9c1287f | 51 | // main() runs in its own thread in the OS |
abraha2d | 0:d4e7f9c1287f | 52 | int main() |
abraha2d | 0:d4e7f9c1287f | 53 | { |
abraha2d | 0:d4e7f9c1287f | 54 | btThread.start(bt_thread); |
abraha2d | 0:d4e7f9c1287f | 55 | while (true) { |
abraha2d | 0:d4e7f9c1287f | 56 | // Blink LED and wait 0.5 seconds |
abraha2d | 0:d4e7f9c1287f | 57 | led1 = !led1; |
abraha2d | 0:d4e7f9c1287f | 58 | wait(0.5); |
abraha2d | 0:d4e7f9c1287f | 59 | } |
abraha2d | 0:d4e7f9c1287f | 60 | } |