1 |
|
|
/** |
2 |
|
|
* @file PatternCommand.hpp |
3 |
|
|
* @brief Header file for blink1_control::config::PatternCommand and derived classes |
4 |
|
|
*/ |
5 |
|
|
|
6 |
|
|
|
7 |
|
|
#pragma once |
8 |
|
|
|
9 |
|
|
#include <chrono> |
10 |
|
|
|
11 |
|
|
#include "blink-lib.hpp" |
12 |
|
|
|
13 |
|
|
namespace blink1_control::config { |
14 |
|
|
|
15 |
|
|
/** |
16 |
|
|
* Abstract class defining a command that can be in a pattern. |
17 |
|
|
* |
18 |
|
|
* @see PatternConfig |
19 |
|
|
*/ |
20 |
|
|
struct PatternCommand { |
21 |
|
18 |
virtual ~PatternCommand() = default; |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* Executes the command. |
25 |
|
|
* |
26 |
|
|
* @param device The blink(1) device to execute on, if applicable. |
27 |
|
|
*/ |
28 |
|
|
virtual void execute(blink1_lib::Blink1Device& device) = 0; |
29 |
|
|
|
30 |
|
|
/** |
31 |
|
|
* Output operator |
32 |
|
|
* |
33 |
|
|
* @param os Output stream |
34 |
|
|
* @param config PatternCommand to output |
35 |
|
|
*/ |
36 |
|
2 |
friend std::ostream& operator<<(std::ostream& os, const blink1_control::config::PatternCommand& config) { |
37 |
|
2 |
config.output(os); |
38 |
|
2 |
return os; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
private: |
42 |
|
|
/** |
43 |
|
|
* Actual implementation of output operator |
44 |
|
|
* |
45 |
|
|
* @param os Output stream |
46 |
|
|
*/ |
47 |
|
|
virtual void output(std::ostream& os) const = 0; |
48 |
|
|
}; |
49 |
|
|
|
50 |
|
|
/** |
51 |
|
|
* Fades the blink(1) device (1 or all LEDs, depending on the value of N in fadeParams) |
52 |
|
|
* to a new color according to the parameters passed in the constructor |
53 |
|
|
*/ |
54 |
|
|
struct FadeCommand : public PatternCommand { |
55 |
|
|
|
56 |
|
|
/** |
57 |
|
|
* All information needed to do the fade is held in this object |
58 |
|
|
*/ |
59 |
|
|
blink1_lib::PatternLineN fadeParams; |
60 |
|
|
|
61 |
|
|
/** |
62 |
|
|
* Constructor |
63 |
|
|
* |
64 |
|
|
* @param fadeParams Information about the fade (color and duration) |
65 |
|
|
*/ |
66 |
|
|
explicit FadeCommand(blink1_lib::PatternLineN fadeParams); |
67 |
|
|
|
68 |
|
|
/** |
69 |
|
|
* Executes the fade command on the given device. |
70 |
|
|
* |
71 |
|
|
* @param device The blink(1) device to execute on |
72 |
|
|
*/ |
73 |
|
|
void execute(blink1_lib::Blink1Device& device) override; |
74 |
|
|
|
75 |
|
|
private: |
76 |
|
|
/** |
77 |
|
|
* Actual implementation of output operator |
78 |
|
|
* |
79 |
|
|
* @param os Output stream |
80 |
|
|
*/ |
81 |
|
1 |
void output(std::ostream& os) const override { |
82 |
|
1 |
os << "FadeCommand{fadeParams: " << fadeParams << "}"; |
83 |
|
1 |
} |
84 |
|
|
}; |
85 |
|
|
|
86 |
|
|
/** |
87 |
|
|
* Simply sleeps for the specified time. Used to insert delays into patterns. |
88 |
|
|
*/ |
89 |
|
|
struct WaitCommand : public PatternCommand { |
90 |
|
|
|
91 |
|
|
/** |
92 |
|
|
* How long to wait |
93 |
|
|
*/ |
94 |
|
|
std::chrono::milliseconds waitTime; |
95 |
|
|
|
96 |
|
|
/** |
97 |
|
|
* Constructor. |
98 |
|
|
* |
99 |
|
|
* @param waitTime How long to wait |
100 |
|
|
*/ |
101 |
|
|
explicit WaitCommand(std::chrono::milliseconds waitTime); |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* Executes the sleep. |
105 |
|
|
* |
106 |
|
|
* @param device Ignored |
107 |
|
|
*/ |
108 |
|
|
void execute(blink1_lib::Blink1Device& device) override; |
109 |
|
|
|
110 |
|
|
private: |
111 |
|
|
/** |
112 |
|
|
* Actual implementation of output operator |
113 |
|
|
* |
114 |
|
|
* @param os Output stream |
115 |
|
|
*/ |
116 |
|
1 |
void output(std::ostream& os) const override { |
117 |
|
1 |
os << "WaitCommand{waitTime: " << waitTime.count() << "ms}"; |
118 |
|
1 |
} |
119 |
|
|
}; |
120 |
|
|
|
121 |
|
|
} |