Line data Source code
1 : // SPDX-FileCopyrightText: 2022-2026 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 8890 : Q_OBJECT
26 0 :
27 0 : public:
28 0 : /// UUID of the "DSO" service.
29 0 : static inline const QBluetoothUuid serviceUuid { QStringLiteral("1569801e-1425-4a7a-b617-a4f4ed719de6") };
30 0 :
31 0 : /// Characteristics available via the `DSO` service.
32 0 : struct QTPOKIT_EXPORT CharacteristicUuids {
33 0 : /// UUID of the `DSO` service's `Settings` characteristic.
34 0 : static inline const QBluetoothUuid settings { QStringLiteral("a81af1b6-b8b3-4244-8859-3da368d2be39") };
35 0 :
36 0 : /// UUID of the `DSO` service's `Metadata` characteristic.
37 0 : static inline const QBluetoothUuid metadata { QStringLiteral("970f00ba-f46f-4825-96a8-153a5cd0cda9") };
38 0 :
39 0 : /// UUID of the `DSO` service's `Reading` characteristic.
40 0 : static inline const QBluetoothUuid reading { QStringLiteral("98e14f8e-536e-4f24-b4f4-1debfed0a99e") };
41 0 : };
42 0 :
43 0 : /// Values supported by the `Command` attribute of the `Settings` characteristic.
44 0 : enum class Command : quint8 {
45 0 : FreeRunning = 0, ///< Run free, without waiting for edge triggers.
46 0 : RisingEdgeTrigger = 1, ///< Trigger on a rising edge.
47 0 : FallingEdgeTrigger = 2, ///< Trigger on a falling edge.
48 0 : ResendData = 3 ///< Resend the last acquired data.
49 0 : };
50 0 : static QString toString(const Command &command);
51 0 :
52 0 : /// Values supported by the `Mode` attribute of the `Settings` and `Metadata` characteristics.
53 0 : enum class Mode : quint8 {
54 0 : Idle = 0, ///< Make device idle.
55 0 : DcVoltage = 1, ///< Measure DC voltage.
56 0 : AcVoltage = 2, ///< Measure AC voltage.
57 0 : DcCurrent = 3, ///< Measure DC current.
58 0 : AcCurrent = 4, ///< Measure AC current.
59 0 : };
60 0 : static QString toString(const Mode &mode);
61 0 :
62 0 : static QString toString(const PokitProduct product, const quint8 range, const Mode mode);
63 0 : QString toString(const quint8 range, const Mode mode) const;
64 0 : static quint32 maxValue(const PokitProduct product, const quint8 range, const Mode mode);
65 0 : quint32 maxValue(const quint8 range, const Mode mode) const;
66 0 :
67 0 : /// Attributes included in the `Settings` characteristic.
68 0 : struct Settings {
69 0 : Command command; ///< Custom operation request.
70 0 : float triggerLevel; ///< Trigger threshold level in Volts or Amps, depending on #mode.
71 0 : Mode mode; ///< Desired operation mode.
72 0 : quint8 range; ///< Desired range, eg settings.range = +PokitPro::CurrentRange::AutoRange;
73 0 : quint32 samplingWindow; ///< Desired sampling window in microseconds.
74 0 : quint16 numberOfSamples; ///< Desired number of samples to acquire.
75 0 : };
76 0 :
77 0 : /// Values supported by the `Status` attribute of the `Metadata` characteristic.
78 0 : enum class DsoStatus : quint8 {
79 0 : Done = 0, ///< Sampling has completed.
80 0 : Sampling = 1, ///< Actively sampling.
81 0 : Error = 255 ///< An error has occurred.
82 0 : };
83 0 :
84 0 : /// Attributes included in the `Metadata` characteristic.
85 0 : struct Metadata {
86 0 : DsoStatus status; ///< Current DSO status.
87 0 : float scale; ///< Scale to apply to read samples.
88 0 : Mode mode; ///< Operation mode used during last acquisition.
89 0 : quint8 range; ///< Range used during last acquisition.
90 0 : quint32 samplingWindow; ///< Sampling window (microseconds) used during last acquisition.
91 0 : quint16 numberOfSamples; ///< Number of samples acquired (1 to 8192).
92 0 : quint32 samplingRate; ///< Sampling rate used during last acquisition (1 to 1MHz).
93 0 : };
94 0 :
95 0 : typedef QVector<qint16> Samples;
96 0 :
97 0 : DsoService(QLowEnergyController * const pokitDevice, QObject * parent = nullptr);
98 3720 : ~DsoService() = default;
99 :
100 : bool readCharacteristics() override;
101 : bool readMetadataCharacteristic();
102 :
103 : // Settings characteristic (BLE write only).
104 : bool setSettings(const Settings &settings);
105 : bool startDso(const Settings &settings);
106 : bool fetchSamples();
107 :
108 : // Metadata characteristic (BLE read/notify).
109 : Metadata metadata() const;
110 : bool enableMetadataNotifications();
111 : bool disableMetadataNotifications();
112 :
113 : // Reading characteristic (BLE notify only).
114 : bool enableReadingNotifications();
115 : bool disableReadingNotifications();
116 :
117 : Q_SIGNALS:
118 : void settingsWritten();
119 : void metadataRead(const DsoService::Metadata &meta);
120 : void samplesRead(const DsoService::Samples &samples);
121 :
122 : protected:
123 : /// \cond internal
124 : DsoService(DsoServicePrivate * const d, QObject * const parent);
125 : /// \endcond
126 :
127 : private:
128 1050 : Q_DECLARE_PRIVATE(DsoService)
129 : Q_DISABLE_COPY(DsoService)
130 : QTPOKIT_BEFRIEND_TEST(DsoService)
131 : };
132 :
133 : QTPOKIT_END_NAMESPACE
134 :
135 : #endif // QTPOKIT_DSOSERVICE_H
|