added bluetooth commands

This commit is contained in:
nymea 2019-10-10 10:51:40 +02:00 committed by Boernsman
parent 44256076a6
commit 4354fdc7af
3 changed files with 176 additions and 11 deletions

View File

@ -55,7 +55,7 @@ void DevicePluginLukeRoberts::discoverDevices(DeviceDiscoveryInfo *info)
}
foreach (const QBluetoothDeviceInfo &deviceInfo, reply->discoveredDevices()) {
if (deviceInfo.name().contains("Luke")) {
if (deviceInfo.name().contains("LRF")) {
DeviceDescriptor descriptor(modelFDeviceClassId, "Model F", deviceInfo.name() + " (" + deviceInfo.address().toString() + ")");
ParamList params;
@ -135,6 +135,23 @@ void DevicePluginLukeRoberts::executeAction(DeviceActionInfo *info)
return info->finish(Device::DeviceErrorHardwareNotAvailable);
}
if (action.actionTypeId() == modelFPowerActionTypeId) {
bool power = action.param(modelFPowerActionPowerParamTypeId).value().toBool();
if (!power) {
lamp->selectScene(0); //Scene 0 is the off scene
} else {
lamp.
}
} else if (action.actionTypeId() == modelFBrightnessActionTypeId) {
int brightness = action.param(modelFBrightnessActionBrightnessParamTypeId).value().toInt();
lamp->modifyBrightness(brightness);
} else if (action.actionTypeId() == modelFColorActionTypeId) {
} else if (action.actionTypeId() == modelFColorTemperatureActionTypeId) {
}
}

View File

@ -43,6 +43,122 @@ BluetoothLowEnergyDevice *LukeRoberts::bluetoothDevice()
return m_bluetoothDevice;
}
void LukeRoberts::ping()
{
QByteArray data;
data.append(0xa0);
data.append(0x02);
data.resize(3); //appending 0 not allowed
m_controlService->writeCharacteristic(m_externalApiEndpoint, data);
}
void LukeRoberts::queryScene(uint8_t id)
{
QByteArray data;
data.append(0xa0);
data.append(0x01);
data.append(0x01);
data << id;
m_controlService->writeCharacteristic(m_externalApiEndpoint, data);
}
/*
* duration in ms, 0 for infinite
* saturation 0 .. 255
* hue 0 .. 65535 - kelvin 2700 .. 4000 for white light when SS= 0
* brightness 0 .. 255
*/
void LukeRoberts::setImmediateLight(uint16_t duration, uint8_t saturation, uint16_t hue, uint8_t brightness)
{
QByteArray data;
data.append(0xa0);
data.append(0x01);
data.append(0x02);
data.append(0x02);
data << duration;
data << saturation;
data << hue;
data << brightness;
m_controlService->writeCharacteristic(m_externalApiEndpoint, data);
}
/*
* Lowers the brightness of the currently selected light scene.
* This modification is reverted when the user selects a different scene via app or Click Detection.
*/
void LukeRoberts::modifyBrightness(uint8_t percent)
{
QByteArray data;
data.append(0xa0);
data.append(0x01);
data.append(0x03);
data << percent;
m_controlService->writeCharacteristic(m_externalApiEndpoint, data);
}
void LukeRoberts::modifyColorTemperature(uint16_t kelvin)
{
QByteArray data;
data.append(0xa0); //Prefix
data.append(0x01); //Protocol version V1
data.append(0x04); //Opcode "Color Temperature"
data << kelvin;
m_controlService->writeCharacteristic(m_externalApiEndpoint, data);
}
/*
* Send 0xFF as IIto select the default scene, e.g. the one that would also appear when
* powering up the lamp.
*/
void LukeRoberts::selectScene(uint8_t id)
{
QByteArray data;
data.append(0xa0); //Prefix
data.append(0x02); //Protocol version V2
data.append(0x05); //Opcode "Select Scene"
data << id;
m_controlService->writeCharacteristic(m_externalApiEndpoint, data);
}
void LukeRoberts::setNextSceneByBrightness(int8_t direction)
{
QByteArray data;
data.append(0xa0); //Prefix
data.append(0x02); //Protocol version V2
data.append(0x06); //Opcode "Next Scene by Brightness"
data << direction; //1 selects the next brighter scene and -1 selects the next less bright scene.
m_controlService->writeCharacteristic(m_externalApiEndpoint, data);
}
/*
* Increments or decrements the currently visible color temperature in the Downlight part of the selected scene.
*/
void LukeRoberts::adjustColorTemperature(uint16_t kelvinIncrement)
{
QByteArray data;
data.append(0xa0); //Prefix
data.append(0x02); //Protocol version V2
data.append(0x07); //Opcode "Adjust Color Temperature"
data << kelvinIncrement;
m_controlService->writeCharacteristic(m_externalApiEndpoint, data);
}
/*
* Scales the brightness of the current scene by multiplication.
* As opposed to 03 Modify Brightness, this command respects the brightness set by previous 03 and 08 commands.
*/
void LukeRoberts::setRelativeBrightness(uint8_t percent)
{
QByteArray data;
data.append(0xa0); //Prefix
data.append(0x02); //Protocol version V2
data.append(0x08); //Opcode "Relative Brightness"
data << percent;
m_controlService->writeCharacteristic(m_externalApiEndpoint, data);
}
void LukeRoberts::printService(QLowEnergyService *service)
{

View File

@ -26,10 +26,12 @@
#include <QObject>
#include <QTimer>
#include <QBluetoothUuid>
#include <QByteArray>
#include "typeutils.h"
#include "hardware/bluetoothlowenergy/bluetoothlowenergydevice.h"
class LukeRoberts : public QObject
{
Q_OBJECT
@ -57,9 +59,24 @@ enum Opcode {
OpcodeRelativeBrightness
};
struct Scene {
int8_t id;
QString nama;
};
explicit LukeRoberts(BluetoothLowEnergyDevice *bluetoothDevice, QObject *parent = nullptr);
BluetoothLowEnergyDevice *bluetoothDevice();
void ping();
void queryScene(uint8_t id);
void getSceneList();
void setImmediateLight(uint16_t duration, uint8_t saturation, uint16_t hue, uint8_t brightness);
void modifyBrightness(uint8_t percent);
void modifyColorTemperature(uint16_t kelvin);
void selectScene(uint8_t id);
void setNextSceneByBrightness(int8_t direction);
void adjustColorTemperature(uint16_t kelvinIncrement);
void setRelativeBrightness(uint8_t percent);
private:
BluetoothLowEnergyDevice *m_bluetoothDevice = nullptr;
@ -71,16 +88,6 @@ private:
QLowEnergyCharacteristic m_deviceInfoCharacteristic;
QLowEnergyCharacteristic m_externalApiEndpoint;
void ping();
void queryScene(int id);
void immediateLight(int flags, int duration);
void brightness(int percent);
void colorTemperature(int kelvin);
void selectScene(int id);
void nextSceneByBrightness();
void adjustColorTemperature();
void relativeBrightness(int percent);
void printService(QLowEnergyService *service);
void onLongPressTimer();
@ -89,6 +96,7 @@ signals:
void deviceInformationChanged(const QString &firmwareRevision, const QString &hardwareRevision, const QString &softwareRevision);
void deviceInitializationFinished(bool success);
void statusCodeReveiced(StatusCodes statusCode);
void sceneListReceived(QList<Scene> scenes);
private slots:
void onConnectedChanged(bool connected);
@ -100,4 +108,28 @@ private slots:
void onExternalApiEndpointCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value);
};
QByteArray &operator<<(QByteArray &l, quint8 r)
{
l.append(r);
return l;
}
QByteArray &operator<<(QByteArray &l, qint8 r)
{
l.append(r);
return l;
}
QByteArray &operator<<(QByteArray &l, quint16 r)
{
return l<<quint8(r>>8)<<quint8(r);
}
QByteArray &operator<<(QByteArray &l, quint32 r)
{
return l<<quint16(r>>16)<<quint16(r);
}
#endif // NUIMO_H