Dokit
Internal development documentation
Loading...
Searching...
No Matches
calibrationservice.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022-2024 Paul Colby <git@colby.id.au>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4/*!
5 * \file
6 * Defines the CalibrationService and CalibrationServicePrivate classes.
7 */
8
11
12#include <QtEndian>
13
14/*!
15 * \class CalibrationService
16 *
17 * The CalibrationService class accesses the `Calibrartion` service of Pokit devices.
18 */
19
20
21/*!
22 * Constructs a new Pokit service with \a parent.
23 */
25 : AbstractPokitService(new CalibrationServicePrivate(controller, this), parent)
26{
27
28}
29
30/*!
31 * \cond internal
32 * Constructs a new Pokit service with \a parent, and private implementation \a d.
33 */
35 CalibrationServicePrivate * const d, QObject * const parent)
36 : AbstractPokitService(d, parent)
37{
38
39}
40/// \endcond
41
42/*!
43 * \copybrief AbstractPokitService::readCharacteristics
44 *
45 * This implementation always returns `true`, since the Calibration service provides no *readable*
46 * characteristics (they're all write-only).
47 */
49{
51 qCDebug(d->lc).noquote() << tr("Ignoring read request; the Calibration service is write-only.");
52 return true;
53}
54
55/*!
56 * Calibrates the Pokit device's temperature to \a ambientTemperature.
57 *
58 * Returns `true` if the write request was successfully queued, `false` otherwise.
59 *
60 * Emits temperatureCalibrated() if/when the \a name has been set.
61 */
62bool CalibrationService::calibrateTemperature(const float ambientTemperature)
63{
64 static_assert(sizeof(float) == 4, "Pokit devices expect 32-bit floats");
65 Q_D(const CalibrationService);
66 const QLowEnergyCharacteristic characteristic =
67 d->getCharacteristic(CharacteristicUuids::temperature);
68 if (!characteristic.isValid()) {
69 return false;
70 }
71
72 const QByteArray newValue = CalibrationServicePrivate::encodeTemperature(ambientTemperature);
73 qCDebug(d->lc).noquote() << tr("Writing new temperature %1 (0x%2).")
74 .arg(ambientTemperature).arg(QLatin1String(newValue.toHex()));
75 d->service->writeCharacteristic(characteristic, newValue);
76 return (d->service->error() != QLowEnergyService::ServiceError::CharacteristicWriteError);
77}
78
79/*!
80 * \fn CalibrationService::temperatureCalibrated
81 *
82 * This signal is emitted when the `Temperature` characteristic has been written succesfully.
83 *
84 * \see calibrateTemperature
85 */
86
87/*!
88 * \cond internal
89 * \class CalibrationServicePrivate
90 *
91 * The CalibrationServicePrivate class provides private implementation for CalibrationService.
92 */
93
94/*!
95 * \internal
96 * Constructs a new CalibrationServicePrivate object with public implementation \a q.
97 */
104
105/*!
106 * Returns \a value in a format Pokit devices expect. Specifically, this just enocdes \a value as
107 * a 32-bit float in litte-endian byte order.
108 */
110{
111 static_assert(sizeof(value) == 4, "Pokit devices expect 32-bit floats");
112 QByteArray bytes(sizeof(float), '\0');
113 qToLittleEndian<float>(value, bytes.data());
114 return bytes;
115}
116
117/*!
118 * Implements AbstractPokitServicePrivate::characteristicWritten to parse \a newValue, then emit a
119 * specialised signal, for each supported \a characteristic.
120 */
122 const QByteArray &newValue)
123{
125
128 Q_EMIT q->temperatureCalibrated();
129 return;
130 }
131
132 qCWarning(lc).noquote() << tr("Unknown characteristic written for Calibration service")
133 << serviceUuid << characteristic.name() << characteristic.uuid();
134}
135
136/// \endcond
Declares the CalibrationService class.
Declares the CalibrationServicePrivate class.
The AbstractPokitServicePrivate class provides private implementation for AbstractPokitService.
QBluetoothUuid serviceUuid
UUIDs for service.
virtual void characteristicWritten(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue)
Handles QLowEnergyService::characteristicWritten events.
The AbstractPokitService class provides a common base for Pokit services classes.
The CalibrationServicePrivate class provides private implementation for CalibrationService.
CalibrationServicePrivate(QLowEnergyController *controller, CalibrationService *const q)
static QByteArray encodeTemperature(const float value)
Returns value in a format Pokit devices expect.
void characteristicWritten(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue) override
Implements AbstractPokitServicePrivate::characteristicWritten to parse newValue, then emit a speciali...
The CalibrationService class accesses the Calibrartion service of Pokit devices.
bool calibrateTemperature(const float ambientTemperature)
Calibrates the Pokit device's temperature to ambientTemperature.
CalibrationService(QLowEnergyController *const pokitDevice, QObject *parent=nullptr)
Constructs a new Pokit service with parent.
bool readCharacteristics() override
Read all characteristics.
char * data()
QByteArray toHex() const const
bool isValid() const const
QString name() const const
QBluetoothUuid uuid() const const
Q_EMITQ_EMIT
QString tr(const char *sourceText, const char *disambiguation, int n)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
static const QBluetoothUuid temperature
UUID of the Calibration service's Temperature characterstic.