Skip to content

Commit

Permalink
Added remove presets feature (#496)
Browse files Browse the repository at this point in the history
* Added remove presets feature

* Extracted "slotIndexMap" to a global, for prevent out of sync between functions
  • Loading branch information
AlivE-git authored Nov 24, 2023
1 parent a522b49 commit 3088038
Show file tree
Hide file tree
Showing 3 changed files with 2,516 additions and 2,405 deletions.
79 changes: 78 additions & 1 deletion gbs-control.ino
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ typedef struct
SlotMeta slot[SLOTS_TOTAL]; // the max avaliable slots that can be encoded in a the charset[A-Za-z0-9-._~()!*:,;]
} SlotMetaArray;

String slotIndexMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~()!*:,";

char serialCommand; // Serial / Web Server commands
char userCommand; // Serial / Web Server commands
static uint8_t lastSegment = 0xFF; // GBS segment for direct access
Expand Down Expand Up @@ -9737,6 +9739,82 @@ void startWebserver()
request->send(200, "application/json", result ? "true" : "false");
});

server.on("/slot/remove", HTTP_GET, [](AsyncWebServerRequest *request) {
bool result = false;
int params = request->params();
AsyncWebParameter *p = request->getParam(0);
char param = p->name().charAt(0);
if (params > 0)
{
if (param == '0')
{
SerialM.println("Wait...");
result = true;
}
else
{
Ascii8 slot = uopt->presetSlot;
Ascii8 nextSlot;
auto currentSlot = slotIndexMap.indexOf(slot);

SlotMetaArray slotsObject;
File slotsBinaryFileRead = SPIFFS.open(SLOTS_FILE, "r");
slotsBinaryFileRead.read((byte *)&slotsObject, sizeof(slotsObject));
slotsBinaryFileRead.close();
String slotName = slotsObject.slot[currentSlot].name;

// remove preset files
SPIFFS.remove("/preset_ntsc." + String((char)slot));
SPIFFS.remove("/preset_pal." + String((char)slot));
SPIFFS.remove("/preset_ntsc_480p." + String((char)slot));
SPIFFS.remove("/preset_pal_576p." + String((char)slot));
SPIFFS.remove("/preset_ntsc_720p." + String((char)slot));
SPIFFS.remove("/preset_ntsc_1080p." + String((char)slot));
SPIFFS.remove("/preset_medium_res." + String((char)slot));
SPIFFS.remove("/preset_vga_upscale." + String((char)slot));
SPIFFS.remove("/preset_unknown." + String((char)slot));

uint8_t loopCount = 0;
uint8_t flag = 1;
while (flag != 0)
{
slot = slotIndexMap[currentSlot + loopCount];
nextSlot = slotIndexMap[currentSlot + loopCount + 1];
flag = 0;
flag += SPIFFS.rename("/preset_ntsc." + String((char)(nextSlot)), "/preset_ntsc." + String((char)slot));
flag += SPIFFS.rename("/preset_pal." + String((char)(nextSlot)), "/preset_pal." + String((char)slot));
flag += SPIFFS.rename("/preset_ntsc_480p." + String((char)(nextSlot)), "/preset_ntsc_480p." + String((char)slot));
flag += SPIFFS.rename("/preset_pal_576p." + String((char)(nextSlot)), "/preset_pal_576p." + String((char)slot));
flag += SPIFFS.rename("/preset_ntsc_720p." + String((char)(nextSlot)), "/preset_ntsc_720p." + String((char)slot));
flag += SPIFFS.rename("/preset_ntsc_1080p." + String((char)(nextSlot)), "/preset_ntsc_1080p." + String((char)slot));
flag += SPIFFS.rename("/preset_medium_res." + String((char)(nextSlot)), "/preset_medium_res." + String((char)slot));
flag += SPIFFS.rename("/preset_vga_upscale." + String((char)(nextSlot)), "/preset_vga_upscale." + String((char)slot));
flag += SPIFFS.rename("/preset_unknown." + String((char)(nextSlot)), "/preset_unknown." + String((char)slot));

slotsObject.slot[currentSlot + loopCount].slot = slotsObject.slot[currentSlot + loopCount + 1].slot;
slotsObject.slot[currentSlot + loopCount].presetID = slotsObject.slot[currentSlot + loopCount + 1].presetID;
slotsObject.slot[currentSlot + loopCount].scanlines = slotsObject.slot[currentSlot + loopCount + 1].scanlines;
slotsObject.slot[currentSlot + loopCount].scanlinesStrength = slotsObject.slot[currentSlot + loopCount + 1].scanlinesStrength;
slotsObject.slot[currentSlot + loopCount].wantVdsLineFilter = slotsObject.slot[currentSlot + loopCount + 1].wantVdsLineFilter;
slotsObject.slot[currentSlot + loopCount].wantStepResponse = slotsObject.slot[currentSlot + loopCount + 1].wantStepResponse;
slotsObject.slot[currentSlot + loopCount].wantPeaking = slotsObject.slot[currentSlot + loopCount + 1].wantPeaking;
// slotsObject.slot[currentSlot + loopCount].name = slotsObject.slot[currentSlot + loopCount + 1].name;
strncpy(slotsObject.slot[currentSlot + loopCount].name, slotsObject.slot[currentSlot + loopCount + 1].name, 25);
loopCount++;
}

File slotsBinaryFileWrite = SPIFFS.open(SLOTS_FILE, "w");
slotsBinaryFileWrite.write((byte *)&slotsObject, sizeof(slotsObject));
slotsBinaryFileWrite.close();
SerialM.println("Preset \"" + slotName + "\" removed");
result = true;
}
}

fail:
request->send(200, "application/json", result ? "true" : "false");
});

server.on("/spiffs/upload", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "application/json", "true");
});
Expand Down Expand Up @@ -9807,7 +9885,6 @@ void startWebserver()
if (slotsBinaryFileRead) {
slotsBinaryFileRead.read((byte *)&slotsObject, sizeof(slotsObject));
slotsBinaryFileRead.close();
String slotIndexMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~()!*:,";
auto currentSlot = slotIndexMap.indexOf(uopt->presetSlot);
if (currentSlot == -1) {
goto fail;
Expand Down
27 changes: 26 additions & 1 deletion webui.html
Original file line number Diff line number Diff line change
Expand Up @@ -908,9 +908,17 @@
onclick="savePreset()"
active
>
<div class="gbs-icon">fiber_manual_record</div>
<div class="gbs-icon">add_circle_outline</div>
<div>save preset</div>
</button>
<button
class="gbs-button gbs-button__control-action gbs-button__secondary"
onclick="removePreset()"
active
>
<div class="gbs-icon">highlight_remove</div>
<div>remove preset</div>
</button>
</div>
</fieldset>
</section>
Expand Down Expand Up @@ -2574,6 +2582,23 @@
}
});
};
const removePreset = () => {
fetch(`/slot/remove?0&nocache=${new Date().getTime()}`).then(() => {
setTimeout(() => {
clearTimeout(GBSControl.wsTimeout);
// GBSControl.wsTimeout = setTimeout(timeOutWs, 6000); //TODO: calc timeout
fetch(`/slot/remove?1&nocache=${new Date().getTime()}`).then(() => {
setTimeout(() => {
fetchSlotNames().then((success) => {
if (success) {
updateSlotNames();
}
});
}, 500);
});
}, 200);
});
};
const getSlotsHTML = () => {
// prettier-ignore
return [
Expand Down
Loading

0 comments on commit 3088038

Please sign in to comment.