94 lines
3.2 KiB
C++
94 lines
3.2 KiB
C++
#include "BluetoothSerialTelemetry.h"
|
|
#include "../../Static.h"
|
|
#include "../../Platform.h"
|
|
#include <ArduinoLog.h>
|
|
#include "../../inputs/Buttons.h"
|
|
|
|
#include <cstdlib>
|
|
|
|
BluetoothSerialTelemetry::BluetoothSerialTelemetry() : InputSource("Bluetooth")
|
|
{
|
|
//m_serial.setPin("0000");
|
|
m_serial.enableSSP();
|
|
}
|
|
|
|
InputEvent
|
|
BluetoothSerialTelemetry::read()
|
|
{
|
|
bool didRead = false;
|
|
while (m_serial.available()) {
|
|
didRead = true;
|
|
char charRead = m_serial.read();
|
|
m_ringbuf.insert(charRead);
|
|
if (charRead == '*') {
|
|
static char commandBuf[32];
|
|
size_t cmdSize = m_ringbuf.write(commandBuf);
|
|
// Overwrite the '*' character, to leave us with a complete command
|
|
commandBuf[cmdSize-1] = 0;
|
|
|
|
Log.verbose("Bluetooth read %s", commandBuf);
|
|
|
|
if (commandBuf[0] == 'R') {
|
|
m_color = CRGB(std::atoi(&commandBuf[1]), m_color.g, m_color.b);
|
|
return InputEvent{InputEvent::SetColor, m_color};
|
|
} else if (commandBuf[0] == 'G') {
|
|
m_color = CRGB(m_color.r, std::atoi(&commandBuf[1]), m_color.b);
|
|
return InputEvent{InputEvent::SetColor, m_color};
|
|
} else if (commandBuf[0] == 'B') {
|
|
m_color = CRGB(m_color.r, m_color.g, std::atoi(&commandBuf[1]));
|
|
return InputEvent{InputEvent::SetColor, m_color};
|
|
} else if (commandBuf[0] == 'O') {
|
|
return InputEvent{InputEvent::UserInput, Buttons::Circle};
|
|
} else if (commandBuf[0] == 'S') {
|
|
return InputEvent{InputEvent::UserInput, Buttons::Triangle};
|
|
} else if (commandBuf[0] == 'X') {
|
|
return InputEvent{InputEvent::UserInput, Buttons::Cross};
|
|
} else if (commandBuf[0] == '+') {
|
|
return InputEvent{InputEvent::SetPower, 1};
|
|
} else if (commandBuf[0] == '-') {
|
|
return InputEvent{InputEvent::SetPower, 0};
|
|
} else if (commandBuf[0] == 'p') {
|
|
return InputEvent{InputEvent::SetPattern, &commandBuf[1]};
|
|
} else if (commandBuf[0] == 'b') {
|
|
return InputEvent::BeatDetect;
|
|
} else if (commandBuf[0] == 'c') {
|
|
return InputEvent{InputEvent::SetDisplayLength, std::atoi(&commandBuf[1])};
|
|
} else if (commandBuf[0] == 'Y') {
|
|
return InputEvent::SaveConfigurationRequest;
|
|
} else if (commandBuf[0] == 'A') {
|
|
char* axisVal = strtok(&commandBuf[1], ",");
|
|
const uint8_t accelX = std::atof(axisVal) * 10;
|
|
axisVal = strtok(NULL, ",");
|
|
const uint8_t accelY = std::atof(axisVal) * 10;
|
|
axisVal = strtok(NULL, ",");
|
|
const uint8_t accelZ = std::atof(axisVal) * 10;
|
|
const uint16_t accelSum = abs(accelX) + abs(accelY) + abs(accelZ);
|
|
const uint16_t delta = abs(m_value.value() - accelSum);
|
|
m_value.add(accelSum);
|
|
if (delta > 32) {
|
|
return InputEvent{InputEvent::Acceleration, delta};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (didRead) {
|
|
return InputEvent::NetworkActivity;
|
|
} else {
|
|
return InputEvent{};
|
|
}
|
|
}
|
|
|
|
void
|
|
BluetoothSerialTelemetry::onStart()
|
|
{
|
|
Log.trace("Starting up Bluetooth...");
|
|
if (m_serial.begin(Platform::deviceName())) {
|
|
Log.notice("Bluetooth started! Device name is %s", Platform::deviceName());
|
|
} else {
|
|
Log.warning("Bluetooth could not be started!");
|
|
}
|
|
}
|
|
|
|
STATIC_ALLOC(BluetoothSerialTelemetry);
|
|
STATIC_TASK(BluetoothSerialTelemetry);
|