Switch Controller
Loading...
Searching...
No Matches
MacroRecorder.h
Go to the documentation of this file.
1#pragma once
2
3#include "pch.h"
4
5#include "Macro.h"
6
8private:
9 const int RECORDING_BUTTON_COOLDOWN = 1000;
10
11 std::function<bool()> playEvent;
12
13 std::vector<Action> currentRecording;
14 std::shared_ptr<Macro> lastRecordedMacro;
15 std::function<bool()> recordEvent;
16
17 std::chrono::steady_clock::time_point activationTime =
18 std::chrono::steady_clock::now();
19 bool recording = false;
20
21public:
22 MacroRecorder(const std::function<bool()> &recordEvent,
23 const std::function<bool()> &playEvent)
24 : playEvent(playEvent),
25 lastRecordedMacro(std::make_shared<Macro>(Macro{{}, playEvent, {}})),
26 recordEvent(recordEvent) {}
27
28 void update(const std::array<uint8_t, 8> &data) {
29 auto now = std::chrono::steady_clock::now();
30 auto timeDiff = std::chrono::duration_cast<std::chrono::milliseconds>(
31 now - activationTime)
32 .count();
33
34 if (recordEvent() && timeDiff > RECORDING_BUTTON_COOLDOWN) {
35 activationTime = now;
36 timeDiff = 0;
37 if (recording) {
38 std::string str =
39 "RecordedMacros/" +
40 std::to_string(now.time_since_epoch().count()) + ".hex";
41 spdlog::info("Saving recording to \"{}\"", str);
42 saveActionVector(str, currentRecording);
43 *lastRecordedMacro = Macro(currentRecording, playEvent, {});
44 currentRecording = {};
45 }
46 recording = !recording;
47 }
48 if (recording) {
49 currentRecording.push_back({static_cast<uint64_t>(timeDiff), data});
50 }
51 }
52
53 const std::shared_ptr<Macro> getLastRecordedMacro() const {
54 return lastRecordedMacro;
55 }
56};
57
58#pragma once
void saveActionVector(const std::string &filename, const std::vector< Action > &record)
Definition: Action.h:11
Definition: MacroRecorder.h:7
MacroRecorder(const std::function< bool()> &recordEvent, const std::function< bool()> &playEvent)
Definition: MacroRecorder.h:22
const std::shared_ptr< Macro > getLastRecordedMacro() const
Definition: MacroRecorder.h:53
void update(const std::array< uint8_t, 8 > &data)
Definition: MacroRecorder.h:28
Definition: Macro.h:7