CTRE Phoenix 6 C++ 24.2.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 * \param newAudioFrequency Parameter to modify
84 * \returns Itself
85 */
86 MusicTone& WithAudioFrequency(units::frequency::hertz_t newAudioFrequency)
87 {
88 AudioFrequency = std::move(newAudioFrequency);
89 return *this;
90 }
91 /**
92 * \brief Sets the period at which this control will update at.
93 * This is designated in Hertz, with a minimum of 20 Hz
94 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
95 *
96 * If this field is set to 0 Hz, the control request will
97 * be sent immediately as a one-shot frame. This may be useful
98 * for advanced applications that require outputs to be
99 * synchronized with data acquisition. In this case, we
100 * recommend not exceeding 50 ms between control calls.
101 *
102 * \param newUpdateFreqHz Parameter to modify
103 * \returns Itself
104 */
105 MusicTone &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
106 {
107 UpdateFreqHz = newUpdateFreqHz;
108 return *this;
109 }
110 /**
111 * Returns a string representation of the object.
112 *
113 * \returns a string representation of the object.
114 */
115 std::string ToString() const override
116 {
117 std::stringstream ss;
118 ss << "class: MusicTone" << std::endl;
119 ss << "AudioFrequency: " << AudioFrequency.to<double>() << std::endl;
120 return ss.str();
121 }
122
123 /**
124 * \brief Gets information about this control request.
125 *
126 * \returns Map of control parameter names and corresponding applied values
127 */
128 std::map<std::string, std::string> GetControlInfo() const override
129 {
130 std::map<std::string, std::string> controlInfo;
131 std::stringstream ss;
132 controlInfo["Name"] = GetName();
133 ss << AudioFrequency.to<double>(); controlInfo["AudioFrequency"] = ss.str(); ss.str(std::string{});
134 return controlInfo;
135 }
136};
137
138}
139}
140}
141
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:86
MusicTone & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MusicTone.hpp:105
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:128
std::string ToString() const override
Returns a string representation of the object.
Definition: MusicTone.hpp:115
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: MusicTone.hpp:65
Definition: RcManualEvent.hpp:12