Kevin Abraham
/
MobileArcade
Platform controller for the Mobile Arcade project
Diff: main.cpp
- Revision:
- 0:d4e7f9c1287f
- Child:
- 1:113469a23547
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Nov 30 20:15:39 2018 +0000 @@ -0,0 +1,60 @@ +#include "mbed.h" +#include "Motor.h" + +DigitalOut led1(LED1); + +// TB6612 mbed +// PWMA 21 +// AI2 22 +// AI1 23 +// BI1 24 +// BI2 25 +// PWMB 26 + +Motor l_motor(p21, p23, p22); // PWMA, AI1, AI2 +Motor r_motor(p26, p24, p25); // PWMB, BI1, BI2 + +Serial bt(p28, p27); // Bluefruit RX, TX +Thread btThread; + +void bt_thread() +{ + char bnum = 0; + char bhit = 0; + while(1) { + while (!bt.readable()) wait(0.01); + if (bt.getc() == '!' && bt.getc() == 'B') { + bnum = bt.getc(); // button number + bhit = bt.getc(); // 1 = hit, 0 = release + if (bt.getc() == char(~('!' + 'B' + bnum + bhit))) { + switch (bnum) { + case '1': // number button 1 + l_motor.speed(bhit == '1' ? 1.0f : 0); + break; + case '2': // number button 2 + r_motor.speed(bhit == '1' ? 1.0f : 0); + break; + case '3': // number button 3 + l_motor.speed(bhit == '1' ? -1.0f : 0); + break; + case '4': // number button 4 + r_motor.speed(bhit == '1' ? -1.0f : 0); + break; + default: + break; + } + } + } + } +} + +// main() runs in its own thread in the OS +int main() +{ + btThread.start(bt_thread); + while (true) { + // Blink LED and wait 0.5 seconds + led1 = !led1; + wait(0.5); + } +}