Loading [MathJax]/extensions/tex2jax.js
CTRE Phoenix 6 C++ 25.4.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MusicTone.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
11#include <sstream>
12
13#include <units/frequency.h>
14#include <units/time.h>
15
16namespace ctre {
17namespace phoenix6 {
18namespace controls {
19
20/**
21 * Plays a single tone at the user specified frequency.
22 */
24{
25 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, std::shared_ptr<ControlRequest> &req) const override
26 {
27 if (req.get() != this)
28 {
29 auto const reqCast = dynamic_cast<MusicTone *>(req.get());
30 if (reqCast != nullptr)
31 {
32 *reqCast = *this;
33 }
34 else
35 {
36 req = std::make_shared<MusicTone>(*this);
37 }
38 }
39
40 return c_ctre_phoenix6_RequestControlMusicTone(network, deviceHash, UpdateFreqHz.to<double>(), AudioFrequency.to<double>());
41 }
42
43public:
44 /**
45 * \brief Sound frequency to play. A value of zero will silence the device. The
46 * effective frequency range is 10-20000 Hz. Any nonzero frequency less than 10
47 * Hz will be capped to 10 Hz. Any frequency above 20 kHz will be capped to 20
48 * kHz.
49 *
50 * - Units: Hz
51 *
52 */
53 units::frequency::hertz_t AudioFrequency;
54
55 /**
56 * \brief The period at which this control will update at.
57 * This is designated in Hertz, with a minimum of 20 Hz
58 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
59 *
60 * If this field is set to 0 Hz, the control request will
61 * be sent immediately as a one-shot frame. This may be useful
62 * for advanced applications that require outputs to be
63 * synchronized with data acquisition. In this case, we
64 * recommend not exceeding 50 ms between control calls.
65 */
66 units::frequency::hertz_t UpdateFreqHz{100_Hz};
67
68 /**
69 * \brief Plays a single tone at the user specified frequency.
70 *
71 * \param AudioFrequency Sound frequency to play. A value of zero will
72 * silence the device. The effective frequency range is
73 * 10-20000 Hz. Any nonzero frequency less than 10 Hz
74 * will be capped to 10 Hz. Any frequency above 20 kHz
75 * will be capped to 20 kHz.
76 */
77 MusicTone(units::frequency::hertz_t AudioFrequency) : ControlRequest{"MusicTone"},
79 {}
80
81 /**
82 * \brief Modifies this Control Request's AudioFrequency parameter and returns itself for
83 * method-chaining and easier to use request API.
84 *
85 * Sound frequency to play. A value of zero will silence the device. The
86 * effective frequency range is 10-20000 Hz. Any nonzero frequency less than 10
87 * Hz will be capped to 10 Hz. Any frequency above 20 kHz will be capped to 20
88 * kHz.
89 *
90 * - Units: Hz
91 *
92 *
93 * \param newAudioFrequency Parameter to modify
94 * \returns Itself
95 */
96 MusicTone &WithAudioFrequency(units::frequency::hertz_t newAudioFrequency)
97 {
98 AudioFrequency = std::move(newAudioFrequency);
99 return *this;
100 }
101 /**
102 * \brief Sets the period at which this control will update at.
103 * This is designated in Hertz, with a minimum of 20 Hz
104 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
105 *
106 * If this field is set to 0 Hz, the control request will
107 * be sent immediately as a one-shot frame. This may be useful
108 * for advanced applications that require outputs to be
109 * synchronized with data acquisition. In this case, we
110 * recommend not exceeding 50 ms between control calls.
111 *
112 * \param newUpdateFreqHz Parameter to modify
113 * \returns Itself
114 */
115 MusicTone &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
116 {
117 UpdateFreqHz = newUpdateFreqHz;
118 return *this;
119 }
120 /**
121 * \brief Returns a string representation of the object.
122 *
123 * \returns a string representation of the object.
124 */
125 std::string ToString() const override
126 {
127 std::stringstream ss;
128 ss << "Control: MusicTone" << std::endl;
129 ss << " AudioFrequency: " << AudioFrequency.to<double>() << " Hz" << std::endl;
130 return ss.str();
131 }
132
133 /**
134 * \brief Gets information about this control request.
135 *
136 * \returns Map of control parameter names and corresponding applied values
137 */
138 std::map<std::string, std::string> GetControlInfo() const override
139 {
140 std::map<std::string, std::string> controlInfo;
141 std::stringstream ss;
142 controlInfo["Name"] = GetName();
143 ss << AudioFrequency.to<double>(); controlInfo["AudioFrequency"] = ss.str(); ss.str(std::string{});
144 return controlInfo;
145 }
146};
147
148}
149}
150}
151
CTREXPORT int c_ctre_phoenix6_RequestControlMusicTone(const char *canbus, uint32_t ecuEncoding, double updateFrequency, double AudioFrequency)
Abstract Control Request class that other control requests extend for use.
Definition ControlRequest.hpp:30
std::string const & GetName() const
Definition ControlRequest.hpp:53
Plays a single tone at the user specified frequency.
Definition MusicTone.hpp:24
MusicTone & WithAudioFrequency(units::frequency::hertz_t newAudioFrequency)
Modifies this Control Request's AudioFrequency parameter and returns itself for method-chaining and e...
Definition MusicTone.hpp:96
MusicTone & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition MusicTone.hpp:115
units::frequency::hertz_t AudioFrequency
Sound frequency to play.
Definition MusicTone.hpp:53
MusicTone(units::frequency::hertz_t AudioFrequency)
Plays a single tone at the user specified frequency.
Definition MusicTone.hpp:77
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition MusicTone.hpp:138
std::string ToString() const override
Returns a string representation of the object.
Definition MusicTone.hpp:125
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition MusicTone.hpp:66
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
Definition Diff_PositionDutyCycle_Position.hpp:15
Definition span.hpp:401