Repository navigation
Expand file tree
/
Copy pathSDCardComponent.cpp
More file actions
175 lines (149 loc) · 4.29 KB
/
Copy pathSDCardComponent.cpp
File metadata and controls
175 lines (149 loc) · 4.29 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "SDCardComponent.h"
#include <deki/providers/Memory.h>
#include "DekiSDCard.h"
#include <deki/providers/FileSystem.h>
#include "IDekiSDCard.h"
#include <deki/PackageConfig.h>
#include <deki/assets/AssetManager.h>
#include <deki/ProjectSettings.h>
#include <deki/Storage.h>
#include <deki/LogSystem.h>
#include <deki/Engine.h>
#include <deki/SceneSystem.h>
namespace DekiSdCard
{
// The mounted SD card driver.
static IDekiSDCard* s_SDCardPackage = nullptr;
SDCardComponent::SDCardComponent()
{
}
SDCardComponent::~SDCardComponent()
{
if (m_Mounted)
{
Unmount();
}
}
void SDCardComponent::Setup(SetupCallback onComplete)
{
bool success = Mount();
if (success && GetOwner())
{
Deki::Engine::GetInstance().GetSceneSystem().MarkPersistent(GetOwner());
}
// A failed step stops the boot, unless the card is not required (a board
// with a card slot may boot without a card).
if (!success && !required)
{
DEKI_LOG_WARNING("SDCardComponent: no card mounted; carrying on without one. Assets on S:/ will not load.");
success = true;
}
if (onComplete)
{
onComplete(success);
}
}
bool SDCardComponent::Mount()
{
if (m_Mounted)
{
return true;
}
if (!s_SDCardPackage)
{
s_SDCardPackage = DekiSDCard::Create();
if (!s_SDCardPackage)
{
DEKI_LOG_ERROR("SDCardComponent: No SD card backend registered");
return false;
}
}
Deki::PackageConfig config;
config.packageId = "sd_card";
config.enabled = true;
config.settings["auto_mount"] = "false";
if (mode == SDCardMode::SDMMC1Bit || mode == SDCardMode::SDMMC4Bit)
{
config.settings["mode"] = (mode == SDCardMode::SDMMC4Bit) ? "SDMMC4Bit" : "SDMMC1Bit";
config.settings["sdmmcHz"] = std::to_string(sdmmcHz);
config.pins["CLK"] = clkPin;
config.pins["CMD"] = cmdPin;
config.pins["D0"] = d0Pin;
if (mode == SDCardMode::SDMMC4Bit)
{
config.pins["D1"] = d1Pin;
config.pins["D2"] = d2Pin;
config.pins["D3"] = d3Pin;
}
if (cdPin >= 0)
{
config.pins["CD"] = cdPin;
}
}
else
{
config.settings["mode"] = "SPI";
config.pins["MOSI"] = mosiPin;
config.pins["MISO"] = misoPin;
config.pins["CLK"] = clkPin;
config.pins["CS"] = csPin;
if (cdPin >= 0)
{
config.pins["CD"] = cdPin;
}
config.settings["spiHz"] = std::to_string(spiHz);
}
s_SDCardPackage->Configure(config);
if (!s_SDCardPackage->Initialize())
{
DEKI_LOG_ERROR("SDCardComponent: Failed to initialize SD card backend");
return false;
}
if (!s_SDCardPackage->Mount())
{
DEKI_LOG_ERROR("SDCardComponent: Failed to mount SD card");
return false;
}
Deki::IFileSystem* sdFs = s_SDCardPackage->GetFileSystem();
if (sdFs)
{
Deki::FileSystem::RegisterFileSystem("S:/", sdFs);
m_PreviousDefault = Deki::FileSystem::GetFileSystemForPath(nullptr); // the default
Deki::FileSystem::SetDefaultFileSystem(sdFs);
}
m_Mounted = true;
// A build that keeps its assets on the card finds them now. One that keeps
// them in internal storage loaded them at startup, and the card is only
// storage.
if (Deki::ProjectSettings::GetAssetStorage() == Deki::Storage::External)
{
Deki::AssetManager::Get()->LoadAssetRoot(Deki::Storage::AssetRoot(Deki::Storage::External));
}
return true;
}
void SDCardComponent::Unmount()
{
if (!m_Mounted)
{
return;
}
Deki::FileSystem::UnregisterFileSystem("S:/");
// Still the default, the card's file system would go on taking every
// path without a mount after it is shut down.
if (s_SDCardPackage && Deki::FileSystem::GetFileSystemForPath(nullptr) == s_SDCardPackage->GetFileSystem())
{
Deki::FileSystem::SetDefaultFileSystem(m_PreviousDefault);
}
m_PreviousDefault = nullptr;
if (s_SDCardPackage)
{
s_SDCardPackage->Unmount();
s_SDCardPackage->Shutdown();
}
m_Mounted = false;
}
IDekiSDCard* SDCardComponent::GetSDCardPackage()
{
return s_SDCardPackage;
}
} // namespace DekiSdCard