Switch Controller
Loading...
Searching...
No Matches
StartController.h
Go to the documentation of this file.
1#pragma once
2
3#include <SFML/Window/Joystick.hpp>
4#include <SFML/Window/Keyboard.hpp>
5
7#include "MacroConfig.h"
8#include "SerialPort.h"
9
10#include <boost/asio/read.hpp>
11#include <boost/asio/write.hpp>
12#include <boost/program_options.hpp>
13
14#include "Event/Controller.h"
16
17void StartController(boost::program_options::variables_map vm,
18 const std::map<std::string, std::string> &ffmpegOptions) {
19 spdlog::info("initializing");
20
21 // Updating joysticks to see if they are connected
22 sf::Joystick::update();
23 auto joystick_connected = sf::Joystick::isConnected(0);
24 if (!joystick_connected) {
25 spdlog::error("no joystick connected");
26 return;
27 }
28 spdlog::info("joystick connected");
29
30 spdlog::info("intializing ffmpeg");
31
32 std::vector<std::unique_ptr<FFmpegFrameSink>> sinks{};
33 sinks.push_back(std::make_unique<VideoFrameSink>());
34 AVChannelLayout channel_layout = AV_CHANNEL_LAYOUT_MONO;
35 sinks.push_back(std::make_unique<AudioFrameSink>(
36 channel_layout, AV_SAMPLE_FMT_S16, 48000, true, 48000));
37
38 std::unique_ptr<FFmpegRecorder> recorder{std::make_unique<FFmpegRecorder>(
39 vm["inputFormat"].as<std::string>(), vm["deviceName"].as<std::string>(),
40 ffmpegOptions, sinks)};
41
42 spdlog::info("ffmpeg intialized");
43 spdlog::info("loading input config");
44 Controller controller(vm);
45 auto modifierButton =
46 static_cast<unsigned int>(vm["controls.modifier"].as<int>());
47 auto stopMacros = [modifierButton,
48 stopButton = static_cast<unsigned int>(
49 vm["controls.stopMacros"].as<int>())] {
50 return sf::Joystick::isButtonPressed(0, stopButton) &&
51 sf::Joystick::isButtonPressed(0, modifierButton);
52 };
53 MacroRecorder macroRecorder{
54 [modifierButton, recordBtn = static_cast<unsigned int>(
55 vm["controls.record"].as<int>())] {
56 return sf::Joystick::isButtonPressed(0, recordBtn) &&
57 sf::Joystick::isButtonPressed(0, modifierButton);
58 },
59 [modifierButton, playBtn = static_cast<unsigned int>(
60 vm["controls.playLastRecorded"].as<int>())] {
61 return sf::Joystick::isButtonPressed(0, playBtn) &&
62 sf::Joystick::isButtonPressed(0, modifierButton);
63 }};
64 spdlog::info("input config loaded");
65 spdlog::info("loading macros");
66 MacroCollection macroCollection =
67 getMacroConfig(dynamic_cast<VideoFrameSink *>(sinks[0].get()),
68 dynamic_cast<AudioFrameSink *>(sinks[0].get()),
69 {macroRecorder.getLastRecordedMacro()});
70 spdlog::info("macros loaded");
71
72 std::unique_ptr<boost::asio::serial_port> port;
73 boost::asio::io_service io;
74 port = initializeSerialPort(vm["port"].as<std::string>(), 57600, io);
75 testSerialPort(port, io);
76
77 std::array<uint8_t, 8> send;
78 unsigned char recieve[1];
79
80 spdlog::info("Ready");
81
82 while (true) {
83 sf::Joystick::update();
84
85 // code used to time an iteration
86 // auto begin = std::chrono::steady_clock::now();
87
88 send = controller.getData();
89 macroRecorder.update(send);
90
91 if (stopMacros()) {
92 macroCollection.deactivateMacros();
93 }
94 macroCollection.activateMacros();
95 if (macroCollection.isMacroActive()) {
96 constexpr auto mergeFunction =
97 []([[maybe_unused]] std::array<uint8_t, 8> action1,
98 std::array<uint8_t, 8> action2) { return action2; };
99 send = macroCollection.getData(send, mergeFunction);
100 }
101
102 boost::asio::write(*port, boost::asio::buffer(send, 8));
103 boost::asio::read(*port, boost::asio::buffer(recieve, 1));
104
105 // code used to time an iteration
106 // auto end = std::chrono::steady_clock::now();
107 // std::cout <<
108 // std::chrono::duration_cast<std::chrono::milliseconds>(end -
109 // begin).count() << std::endl;
110 }
111}
MacroCollection getMacroConfig(VideoFrameSink *videoFrameSink, AudioFrameSink *audioFrameSink, std::vector< std::shared_ptr< Macro > > macros)
Definition: MacroConfig.h:15
void testSerialPort(std::unique_ptr< boost::asio::serial_port > &serialPort, boost::asio::io_service &io)
Definition: SerialPort.h:37
std::unique_ptr< boost::asio::serial_port > initializeSerialPort(std::string serialPort, unsigned int baud, boost::asio::io_service &io)
Definition: SerialPort.h:12
void StartController(boost::program_options::variables_map vm, const std::map< std::string, std::string > &ffmpegOptions)
Definition: StartController.h:17
Definition: AudioFrameSink.h:14
Definition: Controller.h:11
std::array< uint8_t, 8 > getData() const
Definition: Controller.h:163
Definition: MacroCollection.h:7
bool isMacroActive() const
Definition: MacroCollection.h:61
void deactivateMacros()
Definition: MacroCollection.h:62
std::array< uint8_t, 8 > getData(std::array< uint8_t, 8 > intitial, const std::function< std::array< uint8_t, 8 >(std::array< uint8_t, 8 >, std::array< uint8_t, 8 >)> mergeFunction)
Definition: MacroCollection.h:21
void activateMacros()
Definition: MacroCollection.h:54
Definition: MacroRecorder.h:7
Definition: VideoFrameSink.h:17