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 * Destroys this CalibrationService object.
44 */
49
50/*!
51 * \copybrief AbstractPokitService::readCharacteristics
52 *
53 * This implementation always returns `true`, since the Calibration service provides no *readable*
54 * characteristics (they're all write-only).
55 */
57{
59 qCDebug(d->lc).noquote() << tr("Ignoring read request; the Calibration service is write-only.");
60 return true;
61}
62
63/*!
64 * Calibrates the Pokit device's temperature to \a ambientTemperature.
65 *
66 * Returns `true` if the write request was successfully queued, `false` otherwise.
67 *
68 * Emits temperatureCalibrated() if/when the \a name has been set.
69 */
70bool CalibrationService::calibrateTemperature(const float ambientTemperature)
71{
72 static_assert(sizeof(float) == 4, "Pokit devices expect 32-bit floats");
73 Q_D(const CalibrationService);
74 const QLowEnergyCharacteristic characteristic =
75 d->getCharacteristic(CharacteristicUuids::temperature);
76 if (!characteristic.isValid()) {
77 return false;
78 }
79
80 const QByteArray newValue = CalibrationServicePrivate::encodeTemperature(ambientTemperature);
81 qCDebug(d->lc).noquote() << tr("Writing new temperature %1 (0x%2).")
82 .arg(ambientTemperature).arg(QLatin1String(newValue.toHex()));
83 d->service->writeCharacteristic(characteristic, newValue);
84 return (d->service->error() != QLowEnergyService::ServiceError::CharacteristicWriteError);
85}
86
87/*!
88 * \fn CalibrationService::temperatureCalibrated
89 *
90 * This signal is emitted when the `Temperature` characteristic has been written succesfully.
91 *
92 * \see calibrateTemperature
93 */
94
95/*!
96 * \cond internal
97 * \class CalibrationServicePrivate
98 *
99 * The CalibrationServicePrivate class provides private implementation for CalibrationService.
100 */
101
102/*!
103 * \internal
104 * Constructs a new CalibrationServicePrivate object with public implementation \a q.
105 */
112
113/*!
114 * Returns \a value in a format Pokit devices expect. Specifically, this just enocdes \a value as
115 * a 32-bit float in litte-endian byte order.
116 */
118{
119 static_assert(sizeof(value) == 4, "Pokit devices expect 32-bit floats");
120 QByteArray bytes(sizeof(float), '\0');
121 qToLittleEndian<float>(value, bytes.data());
122 return bytes;
123}
124
125/*!
126 * Implements AbstractPokitServicePrivate::characteristicWritten to parse \a newValue, then emit a
127 * specialised signal, for each supported \a characteristic.
128 */
130 const QByteArray &newValue)
131{
133
136 Q_EMIT q->temperatureCalibrated();
137 return;
138 }
139
140 qCWarning(lc).noquote() << tr("Unknown characteristic written for Calibration service")
141 << serviceUuid << characteristic.name() << characteristic.uuid();
142}
143
144/// \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.
~CalibrationService() override
Destroys this CalibrationService object.
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.