CTRE Phoenix 6 C++ 24.3.0
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
12#include <sstream>
13#include <units/frequency.h>
14#include <units/frequency.h>
15#include <units/time.h>
16
17
18namespace ctre {
19namespace phoenix6 {
20namespace controls {
21
22/**
23 * Plays a single tone at the user specified frequency.
24 */
26{
27 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
28 {
29 if (req.get() != this)
30 {
31 auto const reqCast = dynamic_cast<MusicTone *>(req.get());
32 if (reqCast != nullptr)
33 {
34 *reqCast = *this;
35 }
36 else
37 {
38 req = std::make_shared<MusicTone>(*this);
39 }
40 }
41
42 return c_ctre_phoenix6_RequestControlMusicTone(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, AudioFrequency.to<double>());
43 }
44
45public:
46 /**
47 * Sound frequency to play. A value of zero will silence the device. The
48 * effective frequency range is 10-20000Hz. Any nonzero frequency less than 10
49 * Hz will be capped to 10Hz. Any frequency above 20Khz will be capped to
50 * 20KHz.
51 */
52 units::frequency::hertz_t AudioFrequency;
53
54 /**
55 * \brief The period at which this control will update at.
56 * This is designated in Hertz, with a minimum of 20 Hz
57 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
58 *
59 * If this field is set to 0 Hz, the control request will
60 * be sent immediately as a one-shot frame. This may be useful
61 * for advanced applications that require outputs to be
62 * synchronized with data acquisition. In this case, we
63 * recommend not exceeding 50 ms between control calls.
64 */
65 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
66
67 /**
68 * \brief Plays a single tone at the user specified frequency.
69 *
70 * \param AudioFrequency Sound frequency to play. A value of zero will
71 * silence the device. The effective frequency range is
72 * 10-20000Hz. Any nonzero frequency less than 10 Hz
73 * will be capped to 10Hz. Any frequency above 20Khz
74 * will be capped to 20KHz.
75 */
76 MusicTone(units::frequency::hertz_t AudioFrequency) : ControlRequest{"MusicTone"},
78 {}
79
80 /**
81 * \brief Modifies this Control Request's AudioFrequency parameter and returns itself for
82 * method-chaining and easier to use request API.
83 *
84 * Sound frequency to play. A value of zero will silence the device. The
85 * effective frequency range is 10-20000Hz. Any nonzero frequency less than 10
86 * Hz will be capped to 10Hz. Any frequency above 20Khz will be capped to
87 * 20KHz.
88 *
89 * \param newAudioFrequency Parameter to modify
90 * \returns Itself
91 */
92 MusicTone& WithAudioFrequency(units::frequency::hertz_t newAudioFrequency)
93 {
94 AudioFrequency = std::move(newAudioFrequency);
95 return *this;
96 }
97 /**
98 * \brief Sets the period at which this control will update at.
99 * This is designated in Hertz, with a minimum of 20 Hz
100 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
101 *
102 * If this field is set to 0 Hz, the control request will
103 * be sent immediately as a one-shot frame. This may be useful
104 * for advanced applications that require outputs to be
105 * synchronized with data acquisition. In this case, we
106 * recommend not exceeding 50 ms between control calls.
107 *
108 * \param newUpdateFreqHz Parameter to modify
109 * \returns Itself
110 */
111 MusicTone &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
112 {
113 UpdateFreqHz = newUpdateFreqHz;
114 return *this;
115 }
116 /**
117 * Returns a string representation of the object.
118 *
119 * \returns a string representation of the object.
120 */
121 std::string ToString() const override
122 {
123 std::stringstream ss;
124 ss << "class: MusicTone" << std::endl;
125 ss << "AudioFrequency: " << AudioFrequency.to<double>() << std::endl;
126 return ss.str();
127 }
128
129 /**
130 * \brief Gets information about this control request.
131 *
132 * \returns Map of control parameter names and corresponding applied values
133 */
134 std::map<std::string, std::string> GetControlInfo() const override
135 {
136 std::map<std::string, std::string> controlInfo;
137 std::stringstream ss;
138 controlInfo["Name"] = GetName();
139 ss << AudioFrequency.to<double>(); controlInfo["AudioFrequency"] = ss.str(); ss.str(std::string{});
140 return controlInfo;
141 }
142};
143
144}
145}
146}
147
CTREXPORT int c_ctre_phoenix6_RequestControlMusicTone(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double AudioFrequency)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
std::string const & GetName() const
Definition: ControlRequest.hpp:51
Plays a single tone at the user specified frequency.
Definition: MusicTone.hpp:26
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:92
MusicTone & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MusicTone.hpp:111
units::frequency::hertz_t AudioFrequency
Sound frequency to play.
Definition: MusicTone.hpp:52
MusicTone(units::frequency::hertz_t AudioFrequency)
Plays a single tone at the user specified frequency.
Definition: MusicTone.hpp:76
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: MusicTone.hpp:134
std::string ToString() const override
Returns a string representation of the object.
Definition: MusicTone.hpp:121
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: MusicTone.hpp:65
Definition: string_util.hpp:15