-
Notifications
You must be signed in to change notification settings - Fork 386
/
Copy pathstoragemanager.cpp
155 lines (137 loc) · 4.76 KB
/
storagemanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: Copyright (c) 2024 OpenStickCommunity (gp2040-ce.info)
*/
#include "storagemanager.h"
#include "BoardConfig.h"
#include "animationstorage.h"
#include "FlashPROM.h"
#include "drivermanager.h"
#include "eventmanager.h"
#include "peripheralmanager.h"
#include "config.pb.h"
#include "hardware/watchdog.h"
#include "CRC32.h"
#include "types.h"
// Check for saves
#include "ps4/PS4Driver.h"
#include "config_utils.h"
void Storage::init() {
systemFlashSize = System::getPhysicalFlash(); // System Flash Size must be called once
EEPROM.start();
ConfigUtils::load(config);
}
/**
* @brief Save the config, but only if it is safe to (as in USB host is not being used.)
*/
bool Storage::save()
{
return save(false);
}
/**
* @brief Save the config; if forcing a save is requested, or if USB host is not enabled, this will write to flash.
*/
bool Storage::save(const bool force) {
// Conditions for saving:
// 1. Force = True
// 2. Input Mode NOT (PS4/PS5 with USB enabled)
// Save will disconnect USB host, which is okay for gamepad and keyboard hosts
if (!force &&
PeripheralManager::getInstance().isUSBEnabled(0) &&
(DriverManager::getInstance().getInputMode() == INPUT_MODE_PS4 ||
DriverManager::getInstance().getInputMode() == INPUT_MODE_PS5) &&
((PS4Driver*)DriverManager::getInstance().getDriver())->getDongleAuthRequired() == true ) {
return false;
}
return ConfigUtils::save(config);
}
void Storage::ResetSettings()
{
EEPROM.reset();
watchdog_reboot(0, SRAM_END, 2000);
}
bool Storage::setProfile(const uint32_t profileNum)
{
// is this profile defined?
if (profileNum >= 1 && profileNum <= config.profileOptions.gpioMappingsSets_count + 1) {
// is this profile enabled?
// profile 1 (core) is always enabled, others we must check
if (profileNum == 1 || config.profileOptions.gpioMappingsSets[profileNum-2].enabled) {
EventManager::getInstance().triggerEvent(new GPProfileChangeEvent(this->config.gamepadOptions.profileNumber, profileNum));
this->config.gamepadOptions.profileNumber = profileNum;
return true;
}
}
// if we get here, the requested profile doesn't exist or isn't enabled, so don't change it
return false;
}
void Storage::nextProfile()
{
uint32_t profileCeiling = config.profileOptions.gpioMappingsSets_count + 1;
uint32_t requestedProfile = (this->config.gamepadOptions.profileNumber % profileCeiling) + 1;
while (!setProfile(requestedProfile)) {
// if the set failed, try again with the next in the sequence
requestedProfile = (requestedProfile % profileCeiling) + 1;
}
}
void Storage::previousProfile()
{
uint32_t profileCeiling = config.profileOptions.gpioMappingsSets_count + 1;
uint32_t requestedProfile = this->config.gamepadOptions.profileNumber > 1 ?
config.gamepadOptions.profileNumber - 1 : profileCeiling;
while (!setProfile(requestedProfile)) {
// if the set failed, try again with the next in the sequence
requestedProfile = requestedProfile > 1 ? requestedProfile - 1 : profileCeiling;
}
}
/**
* @brief Return the current profile label.
*/
char* Storage::currentProfileLabel() {
if (this->config.gamepadOptions.profileNumber == 1)
return this->config.gpioMappings.profileLabel;
else
return this->config.profileOptions.gpioMappingsSets[config.gamepadOptions.profileNumber-2].profileLabel;
}
void Storage::setFunctionalPinMappings()
{
GpioMappingInfo* alts = nullptr;
if (config.gamepadOptions.profileNumber >= 2 &&
config.gamepadOptions.profileNumber <= config.profileOptions.gpioMappingsSets_count + 1) {
if (config.profileOptions.gpioMappingsSets[config.gamepadOptions.profileNumber-2].enabled) {
alts = config.profileOptions.gpioMappingsSets[config.gamepadOptions.profileNumber-2].pins;
}
}
for (Pin_t pin = 0; pin < (Pin_t)NUM_BANK0_GPIOS; pin++) {
// assign the functional pin to the profile pin if:
// 1: there was a profile to load
// 2: the new action isn't RESERVED or ASSIGNED_TO_ADDON (profiles can't affect special addons)
// 3: the old action isn't RESERVED or ASSIGNED_TO_ADDON (profiles can't affect special addons)
// else use whatever is in the core mapping
if (alts != nullptr &&
alts[pin].action != GpioAction::RESERVED &&
alts[pin].action != GpioAction::ASSIGNED_TO_ADDON &&
this->config.gpioMappings.pins[pin].action != GpioAction::RESERVED &&
this->config.gpioMappings.pins[pin].action != GpioAction::ASSIGNED_TO_ADDON) {
functionalPinMappings[pin] = alts[pin];
} else {
functionalPinMappings[pin] = this->config.gpioMappings.pins[pin];
}
}
}
void Storage::SetGamepad(Gamepad * newpad)
{
gamepad = newpad;
}
Gamepad * Storage::GetGamepad()
{
return gamepad;
}
void Storage::SetProcessedGamepad(Gamepad * newpad)
{
processedGamepad = newpad;
}
Gamepad * Storage::GetProcessedGamepad()
{
return processedGamepad;
}