OpenSourceSimWheelESP32
Open-source wireless steering wheel/button box for ESP32 boards
Loading...
Searching...
No Matches
InternalServices.hpp
Go to the documentation of this file.
1
12#pragma once
13
14//-------------------------------------------------------------------
15// Imports
16//-------------------------------------------------------------------
17
18#include "InternalTypes.hpp"
19#include <cstdint>
20#include <type_traits>
21
22//-------------------------------------------------------------------
23// Dependency manager
24//-------------------------------------------------------------------
25
31template <typename Service>
33{
39 static Service &call()
40 {
41 if constexpr (!::std::is_default_constructible<Service>::value)
42 {
43 if (_singleton)
44 return *_singleton;
45 }
46 else
47 {
48 if (!_singleton)
49 _singleton = new Service();
50 return *_singleton;
51 }
52 throw ::std::runtime_error("Service provider not injected");
53 }
54
62 template <class Provider, typename... _Args>
63 static void inject(_Args &&...__args)
64 {
65
66 static_assert(
67 std::is_base_of<Service, Provider>::value,
68 "Provider does not implement Service");
69 inject(new Provider(::std::forward<_Args>(__args)...));
70 }
71
77 static void inject(Service *provider)
78 {
79 if (provider)
80 {
81 if (!_singleton)
82 _singleton = provider;
83 else
84 throw ::std::runtime_error(
85 "Provider already injected to service");
86 }
87 else
88 throw ::std::runtime_error("A null provider is not allowed");
89 }
90
97 static void reset()
98 {
99 _singleton = nullptr;
100 }
101
102private:
103 inline static Service *_singleton = nullptr;
104};
105
106//-------------------------------------------------------------------
107// Input service
108//-------------------------------------------------------------------
109
114class InputService : public DependencyManager<InputService>
115{
116public:
121 virtual void recalibrateAxes() {}
122
128 virtual void reverseLeftAxis() {}
129
135 virtual void reverseRightAxis() {}
136
147 PulseWidthMultiplier multiplier,
148 bool save = true) {}
149
156 {
157 return (PulseWidthMultiplier::X1);
158 }
159
173 virtual bool getAxisCalibration(
174 int &minLeft,
175 int &maxLeft,
176 int &minRight,
177 int &maxRight) { return (false); }
178
190 virtual void setAxisCalibration(
191 int minLeft,
192 int maxLeft,
193 int minRight,
194 int maxRight,
195 bool save = true) {}
196
203 virtual void getAxisPolarity(
204 bool &leftAxisReversed,
205 bool &rightAxisReversed) {}
206
214 virtual void setAxisPolarity(
215 bool leftAxisReversed,
216 bool rightAxisReversed,
217 bool save = true) {}
218
223 virtual void update() {}
224};
225
226//-------------------------------------------------------------------
227// Input hub service
228//-------------------------------------------------------------------
229
234class InputHubService : public DependencyManager<InputHubService>
235{
236public:
243 virtual bool getSecurityLock()
244 {
245 return (false);
246 }
247
253 virtual uint8_t getBitePoint()
254 {
255 return (CLUTCH_DEFAULT_VALUE);
256 }
257
264 {
265 return (ClutchWorkingMode::_DEFAULT_VALUE);
266 }
267
274 {
275 return (AltButtonsWorkingMode::_DEFAULT_VALUE);
276 }
277
284 {
285 return (DPadWorkingMode::_DEFAULT_VALUE);
286 }
287
294 virtual void setBitePoint(uint8_t value, bool save = true)
295 {
296 }
297
304 virtual void setClutchWorkingMode(ClutchWorkingMode mode, bool save = true)
305 {
306 }
307
316 bool save = true) {}
317
324 virtual void setDPadWorkingMode(DPadWorkingMode mode, bool save = true)
325 {
326 }
327
334 virtual void setSecurityLock(bool value, bool save = true)
335 {
336 }
337};
338
339//-------------------------------------------------------------------
340// Input map service
341//-------------------------------------------------------------------
342
347class InputMapService : public DependencyManager<InputMapService>
348{
349public:
359 virtual void setMap(
360 uint8_t firmware_defined,
361 uint8_t user_defined,
362 uint8_t user_defined_alt) {}
363
373 virtual void getMap(
374 uint8_t firmware_defined,
375 uint8_t &user_defined,
376 uint8_t &user_defined_alt)
377 {
378 user_defined = firmware_defined;
379 user_defined_alt = firmware_defined + 64;
380 };
381
386 virtual void resetMap() {}
387};
388
389//-------------------------------------------------------------------
390// Power service
391//-------------------------------------------------------------------
392
397class PowerService : public DependencyManager<PowerService>
398{
399public:
401 virtual void shutdown() {}
402};
403
404//-------------------------------------------------------------------
405// Battery service
406//-------------------------------------------------------------------
407
412class BatteryService : public DependencyManager<BatteryService>
413{
414public:
420 virtual int getLastBatteryLevel() { return (0); }
421
430 virtual bool hasBattery() { return (false); }
431
443 virtual bool isBatteryPresent()
444 {
445 return (false);
446 }
447
453 virtual void getStatus(BatteryStatus &status) {}
454};
455
456//-------------------------------------------------------------------
457// Battery calibration
458//-------------------------------------------------------------------
459
465 : public DependencyManager<BatteryCalibrationService>
466{
467public:
473 {
474 }
475
483 virtual int getBatteryLevel(int reading)
484 {
485 return (-1);
486 }
487
502 virtual int getBatteryLevelAutoCalibrated(int reading)
503 {
504 return (0);
505 }
506
512 virtual uint8_t getCalibrationDataCount()
513 {
514 return (0);
515 }
516
523 virtual uint16_t getCalibrationData(uint8_t index)
524 {
525 return (0);
526 }
527
535 virtual void setCalibrationData(
536 uint8_t index, uint16_t data,
537 bool save = true) {}
538
545 {
546 return (-1);
547 }
548
555 virtual void setAutoCalibrationParameter(int value, bool save = true)
556 {
557 }
558};
559
560//-------------------------------------------------------------------
561// HID
562//-------------------------------------------------------------------
563
568class HidService : public DependencyManager<HidService>
569{
570public:
578 uint16_t &customVID,
579 uint16_t &customPID) {}
580
593 uint16_t customVID,
594 uint16_t customPID,
595 bool save = true) {}
596};
597
598//-------------------------------------------------------------------
599// UI
600//-------------------------------------------------------------------
601
606class UIService : public DependencyManager<UIService>
607{
608public:
614 virtual uint8_t getMaxFPS()
615 {
616 return (0);
617 }
618};
619
620//-------------------------------------------------------------------
621// Firmware
622//-------------------------------------------------------------------
623
628class FirmwareService : public DependencyManager<FirmwareService>
629{
630 friend void firmwareSetIsRunningState(bool state);
631
632public:
636 virtual bool isRunning()
637 {
638 return (_is_running);
639 }
640
641private:
642 inline static bool _is_running = false;
643};
Types and constants used everywhere for firmware implementation.
ClutchWorkingMode
User-selected working mode of the clutch paddles.
PulseWidthMultiplier
User-selected pulse width multiplier.
AltButtonsWorkingMode
User-selected working mode of "ALT" buttons.
DPadWorkingMode
User-selected working mode of directional pads.
#define CLUTCH_DEFAULT_VALUE
Default bite point value.
Battery calibration service.
virtual void setAutoCalibrationParameter(int value, bool save=true)
Set the auto-calibration parameter.
virtual int getAutoCalibrationParameter()
Get the auto-calibration parameter.
virtual void restartAutoCalibration()
Restart autocalibration algorithm.
virtual uint16_t getCalibrationData(uint8_t index)
Get calibration data.
virtual uint8_t getCalibrationDataCount()
Get the count of calibration data items.
virtual int getBatteryLevelAutoCalibrated(int reading)
Get a percentage of battery charge using auto-calibration. Will provide incorrect battery levels (hig...
virtual int getBatteryLevel(int reading)
Get a percentage of battery charge based on calibration data.
virtual void setCalibrationData(uint8_t index, uint16_t data, bool save=true)
Set calibration data.
Battery service.
virtual void getStatus(BatteryStatus &status)
Get the full battery status.
virtual bool isBatteryPresent()
Check if the battery is detected.
virtual int getLastBatteryLevel()
Get the last known battery level (SoC)
virtual bool hasBattery()
Check if the device can be powered by a battery.
Global firmware service.
virtual bool isRunning()
Check if the firmware daemons are already running.
HID service.
virtual void getCustomHardwareID(uint16_t &customVID, uint16_t &customPID)
Get the user-defined custom hardware ID.
virtual void setCustomHardwareID(uint16_t customVID, uint16_t customPID, bool save=true)
Set the user-defiend custom hardware ID.
Input Hub service.
virtual void setSecurityLock(bool value, bool save=true)
Set the status of the security lock.
virtual AltButtonsWorkingMode getAltButtonsWorkingMode()
Get the current working mode of ALT buttons.
virtual bool getSecurityLock()
Get the status of the security lock.
virtual ClutchWorkingMode getClutchWorkingMode()
Get the current clutch working mode.
virtual uint8_t getBitePoint()
Get the clutch bite point.
virtual void setClutchWorkingMode(ClutchWorkingMode mode, bool save=true)
Set the clutch working mode.
virtual void setDPadWorkingMode(DPadWorkingMode mode, bool save=true)
Set the directional pad working mode.
virtual void setBitePoint(uint8_t value, bool save=true)
Set the clutch bite point.
virtual DPadWorkingMode getDPadWorkingMode()
Get the current working mode of the directional pad.
virtual void setAltButtonsWorkingMode(AltButtonsWorkingMode mode, bool save=true)
Set the working mode of ALT buttons.
Input map service.
virtual void setMap(uint8_t firmware_defined, uint8_t user_defined, uint8_t user_defined_alt)
Map a firmware-defined input number to user-defined.
virtual void resetMap()
Revert the input map to factory defaults.
virtual void getMap(uint8_t firmware_defined, uint8_t &user_defined, uint8_t &user_defined_alt)
Get the current map Firmware-defined input number.
Input hardware services.
virtual void update()
Repeat last input event.
virtual void setRotaryPulseWidthMultiplier(PulseWidthMultiplier multiplier, bool save=true)
Multiply the pulse width of rotary encoders.
virtual PulseWidthMultiplier getRotaryPulseWidthMultiplier()
Get the current pulse width multiplier for rotary encoders.
virtual void reverseLeftAxis()
Change polarity of left axis (if any)
virtual void setAxisCalibration(int minLeft, int maxLeft, int minRight, int maxRight, bool save=true)
Set the Axis calibration data.
virtual void reverseRightAxis()
Change polarity of right axis (if any)
virtual void getAxisPolarity(bool &leftAxisReversed, bool &rightAxisReversed)
Get axes polarity.
virtual void setAxisPolarity(bool leftAxisReversed, bool rightAxisReversed, bool save=true)
Set axes polarity.
virtual void recalibrateAxes()
Force auto-calibration of all axes (analog clutch paddles)
virtual bool getAxisCalibration(int &minLeft, int &maxLeft, int &minRight, int &maxRight)
Get current axis calibration data.
Power service.
virtual void shutdown()
Command a system shutdown.
User interface service.
virtual uint8_t getMaxFPS()
Get the maximum frames per seconds value required by UI instances.
Battery status.
Dependency Manager.
static Service & call()
Get the service provider implementing the service.
static void inject(Service *provider)
Inject a service provider.
static void reset()
Remove the injected service provider (for testing)
static void inject(_Args &&...__args)
Inject a service provider.