Skip to content

Commit

Permalink
next steps (#136)
Browse files Browse the repository at this point in the history
* Got storage working

* filled in defaults

* fixed tests

* removed debug code
  • Loading branch information
Unreal-Dan authored Nov 16, 2023
1 parent c6b04c1 commit 2d2fc94
Show file tree
Hide file tree
Showing 7 changed files with 2,377 additions and 2,310 deletions.
24 changes: 24 additions & 0 deletions VortexEngine/src/Modes/DefaultModes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,31 @@ const default_mode_entry default_modes[MAX_MODES] = {
RGB_GREEN,
RGB_BLUE
}
},
{
PATTERN_STROBE, 2, {
RGB_RED,
RGB_GREEN,
}
},
{
PATTERN_DRIP, 4, {
RGB_RED,
RGB_GREEN,
RGB_BLUE,
RGB_YELLOW
}
},
{
PATTERN_DASHCYCLE, 3, {
RGB_RED,
RGB_GREEN,
RGB_BLUE
}
}



};

// exposed size of the default modes array
Expand Down
53 changes: 45 additions & 8 deletions VortexEngine/src/Storage/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
#include "../VortexLib/VortexLib.h"
#endif

#ifndef VORTEX_EMBEDDED
#ifdef VORTEX_EMBEDDED
#include "../Leds/Leds.h"
#include <nvs.h>
#else // VORTEX_EMBEDDED
#ifdef _WIN32
#include <Windows.h>
#else
#else // _WIN32
#include <unistd.h>
#endif
#endif
Expand All @@ -30,6 +33,7 @@ std::string Storage::m_storageFilename;
#endif

uint32_t Storage::m_lastSaveSize = 0;
uint8_t Storage::m_storagePage = 0;

Storage::Storage()
{
Expand All @@ -49,6 +53,11 @@ void Storage::cleanup()
{
}

void Storage::setStoragePage(uint8_t page)
{
m_storagePage = page;
}

// store a serial buffer to storage
bool Storage::write(uint16_t slot, ByteStream &buffer)
{
Expand All @@ -69,15 +78,28 @@ bool Storage::write(uint16_t slot, ByteStream &buffer)
// just in case
buffer.recalcCRC();
#ifdef VORTEX_EMBEDDED
// implement device storage here
// ESP32 Arduino environment
nvs_handle_t nvs;
uint8_t name[3] = { (uint8_t)('a' + m_storagePage), (uint8_t)('a' + (uint8_t)slot), 0 };
esp_err_t err = nvs_open((char *)name, NVS_READWRITE, &nvs);
if (err != ESP_OK) {
nvs_close(nvs);
return false;
}
err = nvs_set_blob(nvs, (char *)name, buffer.rawData(), buffer.rawSize());
if (err != ESP_OK) {
nvs_close(nvs);
return false;
}
nvs_close(nvs);
#elif defined(_WIN32)
HANDLE hFile = CreateFile(STORAGE_FILENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
// error
return false;
}
DWORD written = 0;
DWORD offset = slot * MAX_MODE_SIZE;
DWORD offset = (slot * MAX_MODE_SIZE) + (m_storagePage * (MAX_MODE_SIZE * MAX_MODES));
SetFilePointer(hFile, offset, NULL, FILE_BEGIN);
if (!WriteFile(hFile, buffer.rawData(), MAX_MODE_SIZE, &written, NULL)) {
// error
Expand All @@ -89,7 +111,7 @@ bool Storage::write(uint16_t slot, ByteStream &buffer)
if (!f) {
return false;
}
long offset = slot * MAX_MODE_SIZE;
long offset = (slot * MAX_MODE_SIZE) + (m_storagePage * (MAX_MODE_SIZE * MAX_MODES));
fseek(f, offset, SEEK_SET);
if (!fwrite(buffer.rawData(), sizeof(char), MAX_MODE_SIZE, f)) {
return false;
Expand Down Expand Up @@ -118,15 +140,30 @@ bool Storage::read(uint16_t slot, ByteStream &buffer)
return false;
}
#ifdef VORTEX_EMBEDDED
// implement device storage here
// ESP32 Arduino environment
nvs_handle_t nvs;
uint8_t name[3] = { (uint8_t)('a' + m_storagePage), (uint8_t)('a' + (uint8_t)slot), 0 };
esp_err_t err = nvs_open((char *)name, NVS_READWRITE, &nvs);
if (err != ESP_OK) {
nvs_close(nvs);
return false;
}
size_t read_size = size;
// build a two letter name based on the slot and page
err = nvs_get_blob(nvs, (char *)name, buffer.rawData(), &read_size);
if (err != ESP_OK) {
nvs_close(nvs);
return false;
}
nvs_close(nvs);
#elif defined(_WIN32)
HANDLE hFile = CreateFile(STORAGE_FILENAME, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
// error
return false;
}
DWORD bytesRead = 0;
DWORD offset = slot * MAX_MODE_SIZE;
DWORD offset = (slot * MAX_MODE_SIZE) + (m_storagePage * (MAX_MODE_SIZE * MAX_MODES));
SetFilePointer(hFile, offset, NULL, FILE_BEGIN);
if (!ReadFile(hFile, buffer.rawData(), MAX_MODE_SIZE, &bytesRead, NULL)) {
// error
Expand All @@ -138,7 +175,7 @@ bool Storage::read(uint16_t slot, ByteStream &buffer)
if (!f) {
return false;
}
long offset = slot * MAX_MODE_SIZE;
long offset = (slot * MAX_MODE_SIZE) + (m_storagePage * (MAX_MODE_SIZE * MAX_MODES));
fseek(f, offset, SEEK_SET);
if (!fread(buffer.rawData(), sizeof(char), MAX_MODE_SIZE, f)) {
return false;
Expand Down
6 changes: 6 additions & 0 deletions VortexEngine/src/Storage/Storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Storage
static bool init();
static void cleanup();

// set the global storage page, the chromadeck has 8 pages of 16 slots each
static void setStoragePage(uint8_t page);

// store a serial buffer to storage
static bool write(uint16_t slot, ByteStream &buffer);
// read a serial buffer from storage
Expand All @@ -38,6 +41,9 @@ class Storage

// the size of the last save
static uint32_t m_lastSaveSize;

// the curren storage page
static uint8_t m_storagePage;
};

#endif
2 changes: 1 addition & 1 deletion VortexEngine/src/VortexConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
// This should not be set to 0, it should be a specific maximum for
// each separate device
//
#define MAX_MODES 13
#define MAX_MODES 16

// Default Tickrate in Ticks Per Second (TPS)
//
Expand Down
Loading

0 comments on commit 2d2fc94

Please sign in to comment.