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 DsoService class. 7 : */ 8 : 9 : #ifndef QTPOKIT_DSOSERVICE_H 10 : #define QTPOKIT_DSOSERVICE_H 11 : 12 : #include "abstractpokitservice.h" 13 : #include "pokitproducts.h" 14 : 15 : #include <QBluetoothAddress> 16 : #include <QBluetoothUuid> 17 : #include <QVersionNumber> 18 : 19 : QTPOKIT_BEGIN_NAMESPACE 20 : 21 : class DsoServicePrivate; 22 : 23 : class QTPOKIT_EXPORT DsoService : public AbstractPokitService 24 : { 25 2214 : Q_OBJECT 26 : 27 : public: 28 : static const QBluetoothUuid serviceUuid; 29 : 30 : struct QTPOKIT_EXPORT CharacteristicUuids { 31 : static const QBluetoothUuid settings; 32 : static const QBluetoothUuid metadata; 33 : static const QBluetoothUuid reading; 34 : }; 35 : 36 : enum class Command : quint8 { 37 : FreeRunning = 0, ///< Run free, without waiting for edge triggers. 38 : RisingEdgeTrigger = 1, ///< Trigger on a rising edge. 39 : FallingEdgeTrigger = 2, ///< Trigger on a falling edge. 40 : ResendData = 3 ///< Resend the last acquired data. 41 : }; 42 : 43 : enum class Mode : quint8 { 44 : Idle = 0, ///< Make device idle. 45 : DcVoltage = 1, ///< Measure DC voltage. 46 : AcVoltage = 2, ///< Measure AC voltage. 47 : DcCurrent = 3, ///< Measure DC current. 48 : AcCurrent = 4, ///< Measure AC current. 49 : }; 50 : static QString toString(const Mode &mode); 51 : 52 : static QString toString(const PokitProduct product, const quint8 range, const Mode mode); 53 : QString toString(const quint8 range, const Mode mode) const; 54 : static QVariant maxValue(const PokitProduct product, const quint8 range, const Mode mode); 55 : QVariant maxValue(const quint8 range, const Mode mode) const; 56 : 57 : struct Settings { 58 : Command command; ///< Custom operation request. 59 : float triggerLevel; ///< Trigger threshold level in Volts or Amps, depending on #mode. 60 : Mode mode; ///< Desired operation mode. 61 : quint8 range; ///< Desired range, eg settings.range = +PokitPro::CurrentRange::AutoRange; 62 : quint32 samplingWindow; ///< Desired sampling window in microseconds. 63 : quint16 numberOfSamples; ///< Desired number of samples to acquire. 64 : }; 65 : 66 : enum class DsoStatus : quint8 { 67 : Done = 0, ///< Sampling has completed. 68 : Sampling = 1, ///< Actively sampling. 69 : Error = 255 ///< An error has occurred. 70 : }; 71 : 72 : struct Metadata { 73 : DsoStatus status; ///< Current DSO status. 74 : float scale; ///< Scale to apply to read samples. 75 : Mode mode; ///< Operation mode used during last acquisition. 76 : quint8 range; ///< Range used during last acquisition. 77 : quint32 samplingWindow; ///< Sampling window (microseconds) used during last acquisition. 78 : quint16 numberOfSamples; ///< Number of samples acquired (1 to 8192). 79 : quint32 samplingRate; ///< Sampling rate used during last acquisition (1 to 1MHz). 80 : }; 81 : 82 : typedef QVector<qint16> Samples; 83 : 84 : DsoService(QLowEnergyController * const pokitDevice, QObject * parent = nullptr); 85 : ~DsoService() override; 86 : 87 : bool readCharacteristics() override; 88 : bool readMetadataCharacteristic(); 89 : 90 : // Settings characteristic (BLE write only). 91 : bool setSettings(const Settings &settings); 92 : bool startDso(const Settings &settings); 93 : bool fetchSamples(); 94 : 95 : // Metadata characteristic (BLE read/notify). 96 : Metadata metadata() const; 97 : bool enableMetadataNotifications(); 98 : bool disableMetadataNotifications(); 99 : 100 : // Reading characteristic (BLE notify only). 101 : bool enableReadingNotifications(); 102 : bool disableReadingNotifications(); 103 : 104 : signals: 105 : void settingsWritten(); 106 : void metadataRead(const DsoService::Metadata &meta); 107 : void samplesRead(const DsoService::Samples &samples); 108 : 109 : protected: 110 : /// \cond internal 111 : DsoService(DsoServicePrivate * const d, QObject * const parent); 112 : /// \endcond 113 : 114 : private: 115 270 : Q_DECLARE_PRIVATE(DsoService) 116 : Q_DISABLE_COPY(DsoService) 117 : friend class TestDsoService; 118 : }; 119 : 120 : QTPOKIT_END_NAMESPACE 121 : 122 : #endif // QTPOKIT_DSOSERVICE_H