Skip to content

Commit

Permalink
Add bluetooth support, via BluetoothSerial library
Browse files Browse the repository at this point in the history
  • Loading branch information
watsaig committed Apr 3, 2018
1 parent e3424e7 commit 33990b4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef CONSTANTS_H
#define CONSTANTS_H

// Uncomment following line to use Bluetooth serial interface instead of USB
//#define BLUETOOTH_SERIAL

#include <XIO.h>

// Testing stuff
Expand Down
24 changes: 22 additions & 2 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ void Controller::setNeoPixel(int index, const RgbColor& color)

#endif

#ifdef BLUETOOTH_SERIAL
#include <BluetoothSerial.h>
extern BluetoothSerial SerialBT;
#endif

Controller::Controller()
: mXIORefreshRequested(false)
Expand Down Expand Up @@ -144,7 +148,11 @@ void Controller::update()
mPressureControlTimer = millis();
}

#ifdef BLUETOOTH_SERIAL
if (SerialBT.available())
#else
if (Serial.available())
#endif
handleSerialData();

if (mXIORefreshRequested) {
Expand Down Expand Up @@ -188,12 +196,20 @@ void Controller::xioDigitalWrite(int pin, int value)
void Controller::handleSerialData()
{

#ifdef BLUETOOTH_SERIAL
while (SerialBT.available() >= 2) {
int length = SerialBT.available();
uint8_t firstByte, secondByte;
firstByte = SerialBT.read();
secondByte = SerialBT.read();
#else
while (Serial.available() >= 2) {
int length = Serial.available();
uint8_t firstByte, secondByte;
firstByte = Serial.read();
secondByte = Serial.read();

#endif
Log.notice("Received %d bytes: %d ; %d \n", length, firstByte, secondByte);

if (firstByte == STATUS_REQUEST) {
Expand Down Expand Up @@ -225,8 +241,12 @@ void Controller::handleSerialData()
void Controller::sendComponentValue(ComponentID component)
{
if (mComponents.count(component)) {
Serial.write(component);
Serial.write(mComponents[component]->getValue());
uint8_t toSend[2] = {(uint8_t)component, (uint8_t)mComponents[component]->getValue()};
#ifdef BLUETOOTH_SERIAL
SerialBT.write(toSend, 2);
#else
Serial.write(toSend, 2);
#endif
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
#include "constants.h"
#include "controller.h"

#ifdef BLUETOOTH_SERIAL
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
#endif

// ----------------------------------------
// Global Controller instance. To access it, add `extern Controller controller` to the top of your cpp file.
// Alternatively, a singleton could be used, but the result is the same.
Expand All @@ -16,6 +21,10 @@ void setup()
// Log level should be "LOG_LEVEL_VERBOSE" for debugging, and "LOG_LEVEL_ERROR" for production code.
Log.begin(LOG_LEVEL_ERROR, &Serial);

#ifdef BLUETOOTH_SERIAL
SerialBT.begin("Microfluidics control system");
#endif

// Wait for serial to attach
while (!Serial);

Expand Down

0 comments on commit 33990b4

Please sign in to comment.