Line data Source code
1 : // SPDX-FileCopyrightText: 2022-2023 Paul Colby <git@colby.id.au> 2 : // SPDX-License-Identifier: LGPL-3.0-or-later 3 : 4 : /*! 5 : * \file 6 : * Declares the DataLoggerService class. 7 : */ 8 : 9 : #ifndef QTPOKIT_DATALOGGERSERVICE_H 10 : #define QTPOKIT_DATALOGGERSERVICE_H 11 : 12 : #include "abstractpokitservice.h" 13 : 14 : #include <QBluetoothAddress> 15 : #include <QBluetoothUuid> 16 : #include <QVersionNumber> 17 : 18 : QTPOKIT_BEGIN_NAMESPACE 19 : 20 : class DataLoggerServicePrivate; 21 : 22 : class QTPOKIT_EXPORT DataLoggerService : public AbstractPokitService 23 : { 24 2772 : Q_OBJECT 25 : 26 : public: 27 : static const QBluetoothUuid serviceUuid; 28 : 29 : struct QTPOKIT_EXPORT CharacteristicUuids { 30 : static const QBluetoothUuid settings; 31 : static const QBluetoothUuid metadata; 32 : static const QBluetoothUuid reading; 33 : }; 34 : 35 : enum class Command : quint8 { 36 : Start = 0, ///< Start the Data Logger. 37 : Stop = 1, ///< Stop the Data Logger. 38 : Refresh = 2, ///< Refresh the Data Logger. 39 : }; 40 : 41 : enum class Mode : quint8 { 42 : Idle = 0, ///< Make device idle. 43 : DcVoltage = 1, ///< Measure DC voltage. 44 : AcVoltage = 2, ///< Measure AC voltage. 45 : DcCurrent = 3, ///< Measure DC current. 46 : AcCurrent = 4, ///< Measure AC current. 47 : Temperature = 5, ///< Measure temperature. 48 : }; 49 : static QString toString(const Mode &mode); 50 : 51 : static QString toString(const PokitProduct product, const quint8 range, const Mode mode); 52 : QString toString(const quint8 range, const Mode mode) const; 53 : static QVariant maxValue(const PokitProduct product, const quint8 range, const Mode mode); 54 : QVariant maxValue(const quint8 range, const Mode mode) const; 55 : 56 : struct Settings { 57 : Command command; ///< Custom operation request. 58 : quint16 arguments; ///< Reserved to used along with #command in future. 59 : Mode mode; ///< Desired operation mode. 60 : quint8 range; ///< Desired range. 61 : quint32 updateInterval; ///< Desired update interval in milliseconds. 62 : quint32 timestamp; ///< Custom timestamp for start time in retrieved metadata. 63 : }; 64 : 65 : enum class LoggerStatus : quint8 { 66 : Done = 0, ///< Sampling has completed. 67 : Sampling = 1, ///< Actively sampling. 68 : BufferFull = 2, ///< Buffer is full. 69 : Error = 255 ///< An error has occurred. 70 : }; 71 : 72 : struct Metadata { 73 : LoggerStatus status; ///< Current data logger status. 74 : float scale; ///< Scale to apply to read samples. 75 : Mode mode; ///< Current operation mode. 76 : quint8 range; ///< Current range. 77 : quint32 updateInterval; ///< Current logging interval in milliseconds. 78 : quint16 numberOfSamples; ///< Number of samples acquired (1 to 6192). 79 : quint32 timestamp; ///< Timestamp stored at the beginning of the logging session. 80 : }; 81 : 82 : typedef QVector<qint16> Samples; 83 : 84 : DataLoggerService(QLowEnergyController * const pokitDevice, QObject * parent = nullptr); 85 : ~DataLoggerService() override; 86 : 87 : bool readCharacteristics() override; 88 : bool readMetadataCharacteristic(); 89 : 90 : // Settings characteristic (BLE write only). 91 : bool setSettings(const Settings &settings); 92 : bool startLogger(const Settings &settings); 93 : bool stopLogger(); 94 : bool fetchSamples(); 95 : 96 : // Metadata characteristic (BLE read/notify). 97 : Metadata metadata() const; 98 : bool enableMetadataNotifications(); 99 : bool disableMetadataNotifications(); 100 : 101 : // Reading characteristic (BLE notify only). 102 : bool enableReadingNotifications(); 103 : bool disableReadingNotifications(); 104 : 105 : signals: 106 : void settingsWritten(); 107 : void metadataRead(const DataLoggerService::Metadata &meta); 108 : void samplesRead(const DataLoggerService::Samples &samples); 109 : 110 : protected: 111 : /// \cond internal 112 : DataLoggerService(DataLoggerServicePrivate * const d, QObject * const parent); 113 : /// \endcond 114 : 115 : private: 116 252 : Q_DECLARE_PRIVATE(DataLoggerService) 117 : Q_DISABLE_COPY(DataLoggerService) 118 : friend class TestDataLoggerService; 119 : }; 120 : 121 : QTPOKIT_END_NAMESPACE 122 : 123 : #endif // QTPOKIT_DATALOGGERSERVICE_H