Bluetooth serial communication example

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
dralisz82
Date:
Mon Sep 18 12:30:35 2017 +0000
Commit message:
Bluetooth serial communication example;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r d511d7e52cd8 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Sep 18 12:30:35 2017 +0000
@@ -0,0 +1,54 @@
+#include "mbed.h"
+
+DigitalOut red(LED_RED);
+DigitalOut green(LED_GREEN);
+DigitalOut blue(LED_BLUE);
+
+Serial pc(USBTX, USBRX);  // USB serial via debugger
+Serial BT(PTC15, PTC14);  // Bluetooth module header
+
+char color;
+
+/**
+ * UART Interrupt handler
+ */
+void gotChar() {
+    char c = BT.getc(); // read incoming character
+    pc.putc(c); // forward to debug serial
+    
+    // interpret received character as a command to switch color
+    if(c == 'r' || c == 'g' || c == 'b')
+        color = c;
+}
+
+/**
+ * Main thread
+ */
+int main()
+{
+    red = 1; green = 1; blue = 1; // turn off all LEDs
+    color = 'r'; // red is initial
+    
+    // initialization
+    BT.baud(9600); // HC-05 module works at this rate by default
+    pc.printf("Hello World!\n");
+    BT.printf("Hello World!\n");
+    BT.attach(&gotChar); // register interrupt handler
+    
+    // main program loop
+    while (true) {
+        wait(0.5f); // wait half second
+        if(color == 'r') {
+            red = !red; // toggle red
+            green = 1; blue = 1; // turn off other 2 colors
+        }
+        if(color == 'g') {
+            green = !green; // toggle green
+            red = 1; blue = 1; // turn off other 2 colors
+        }
+        if(color == 'b') {
+            blue = !blue; // toggle blue
+            red = 1; green = 1; // turn off other 2 colors
+        }
+    }
+}
diff -r 000000000000 -r d511d7e52cd8 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Sep 18 12:30:35 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed/builds/a330f0fddbec
\ No newline at end of file