|
|
|
@ -50,10 +50,7 @@ auto NvsStorage::OpenSync() -> NvsStorage* { |
|
|
|
|
return nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::unique_ptr<NvsStorage> instance = std::make_unique<NvsStorage>( |
|
|
|
|
std::unique_ptr<tasks::Worker>( |
|
|
|
|
tasks::Worker::Start<tasks::Type::kNvsWriter>()), |
|
|
|
|
handle); |
|
|
|
|
std::unique_ptr<NvsStorage> instance = std::make_unique<NvsStorage>(handle); |
|
|
|
|
if (instance->SchemaVersionSync() < kSchemaVersion && |
|
|
|
|
!instance->DowngradeSchemaSync()) { |
|
|
|
|
ESP_LOGW(kTag, "failed to init namespace"); |
|
|
|
@ -64,9 +61,7 @@ auto NvsStorage::OpenSync() -> NvsStorage* { |
|
|
|
|
return instance.release(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
NvsStorage::NvsStorage(std::unique_ptr<tasks::Worker> worker, |
|
|
|
|
nvs_handle_t handle) |
|
|
|
|
: writer_(std::move(worker)), handle_(handle) {} |
|
|
|
|
NvsStorage::NvsStorage(nvs_handle_t handle) : handle_(handle) {} |
|
|
|
|
|
|
|
|
|
NvsStorage::~NvsStorage() { |
|
|
|
|
nvs_close(handle_); |
|
|
|
@ -75,43 +70,31 @@ NvsStorage::~NvsStorage() { |
|
|
|
|
|
|
|
|
|
auto NvsStorage::DowngradeSchemaSync() -> bool { |
|
|
|
|
ESP_LOGW(kTag, "namespace needs downgrading"); |
|
|
|
|
return writer_ |
|
|
|
|
->Dispatch<bool>([&]() -> bool { |
|
|
|
|
nvs_erase_all(handle_); |
|
|
|
|
nvs_set_u8(handle_, kKeyVersion, kSchemaVersion); |
|
|
|
|
return nvs_commit(handle_); |
|
|
|
|
}) |
|
|
|
|
.get() == ESP_OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
auto NvsStorage::SchemaVersionSync() -> uint8_t { |
|
|
|
|
return writer_ |
|
|
|
|
->Dispatch<uint8_t>([&]() -> uint8_t { |
|
|
|
|
uint8_t ret; |
|
|
|
|
if (nvs_get_u8(handle_, kKeyVersion, &ret) != ESP_OK) { |
|
|
|
|
return UINT8_MAX; |
|
|
|
|
} |
|
|
|
|
return ret; |
|
|
|
|
}) |
|
|
|
|
.get(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
auto NvsStorage::PreferredBluetoothDevice() |
|
|
|
|
-> std::future<std::optional<bluetooth::mac_addr_t>> { |
|
|
|
|
return writer_->Dispatch<std::optional<bluetooth::mac_addr_t>>( |
|
|
|
|
[&]() -> std::optional<bluetooth::mac_addr_t> { |
|
|
|
|
-> std::optional<bluetooth::mac_addr_t> { |
|
|
|
|
bluetooth::mac_addr_t out{0}; |
|
|
|
|
size_t size = out.size(); |
|
|
|
|
if (nvs_get_blob(handle_, kKeyBluetooth, out.data(), &size) != ESP_OK) { |
|
|
|
|
return {}; |
|
|
|
|
} |
|
|
|
|
return out; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
auto NvsStorage::PreferredBluetoothDevice( |
|
|
|
|
std::optional<bluetooth::mac_addr_t> addr) -> std::future<bool> { |
|
|
|
|
return writer_->Dispatch<bool>([&]() { |
|
|
|
|
std::optional<bluetooth::mac_addr_t> addr) -> bool { |
|
|
|
|
if (!addr) { |
|
|
|
|
nvs_erase_key(handle_, kKeyBluetooth); |
|
|
|
|
} else { |
|
|
|
@ -119,11 +102,9 @@ auto NvsStorage::PreferredBluetoothDevice( |
|
|
|
|
addr.value().size()); |
|
|
|
|
} |
|
|
|
|
return nvs_commit(handle_) == ESP_OK; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
auto NvsStorage::OutputMode() -> std::future<Output> { |
|
|
|
|
return writer_->Dispatch<Output>([&]() -> Output { |
|
|
|
|
auto NvsStorage::OutputMode() -> Output { |
|
|
|
|
uint8_t out = 0; |
|
|
|
|
nvs_get_u8(handle_, kKeyOutput, &out); |
|
|
|
|
switch (out) { |
|
|
|
@ -133,75 +114,56 @@ auto NvsStorage::OutputMode() -> std::future<Output> { |
|
|
|
|
default: |
|
|
|
|
return Output::kHeadphones; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
auto NvsStorage::OutputMode(Output out) -> std::future<bool> { |
|
|
|
|
return writer_->Dispatch<bool>([&]() { |
|
|
|
|
auto NvsStorage::OutputMode(Output out) -> bool { |
|
|
|
|
uint8_t as_int = static_cast<uint8_t>(out); |
|
|
|
|
nvs_set_u8(handle_, kKeyOutput, as_int); |
|
|
|
|
return nvs_commit(handle_) == ESP_OK; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
auto NvsStorage::ScreenBrightness() -> std::future<uint_fast8_t> { |
|
|
|
|
return writer_->Dispatch<uint_fast8_t>([&]() -> uint_fast8_t { |
|
|
|
|
auto NvsStorage::ScreenBrightness() -> uint_fast8_t { |
|
|
|
|
uint8_t out = 50; |
|
|
|
|
nvs_get_u8(handle_, kKeyBrightness, &out); |
|
|
|
|
return out; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
auto NvsStorage::ScreenBrightness(uint_fast8_t val) -> std::future<bool> { |
|
|
|
|
return writer_->Dispatch<bool>([&]() { |
|
|
|
|
auto NvsStorage::ScreenBrightness(uint_fast8_t val) -> bool { |
|
|
|
|
nvs_set_u8(handle_, kKeyBrightness, val); |
|
|
|
|
return nvs_commit(handle_) == ESP_OK; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
auto NvsStorage::AmpMaxVolume() -> std::future<uint16_t> { |
|
|
|
|
return writer_->Dispatch<uint16_t>([&]() -> uint16_t { |
|
|
|
|
auto NvsStorage::AmpMaxVolume() -> uint16_t { |
|
|
|
|
uint16_t out = wm8523::kDefaultMaxVolume; |
|
|
|
|
nvs_get_u16(handle_, kKeyAmpMaxVolume, &out); |
|
|
|
|
return out; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
auto NvsStorage::AmpMaxVolume(uint16_t val) -> std::future<bool> { |
|
|
|
|
return writer_->Dispatch<bool>([&]() { |
|
|
|
|
auto NvsStorage::AmpMaxVolume(uint16_t val) -> bool { |
|
|
|
|
nvs_set_u16(handle_, kKeyAmpMaxVolume, val); |
|
|
|
|
return nvs_commit(handle_) == ESP_OK; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
auto NvsStorage::AmpCurrentVolume() -> std::future<uint16_t> { |
|
|
|
|
return writer_->Dispatch<uint16_t>([&]() -> uint16_t { |
|
|
|
|
auto NvsStorage::AmpCurrentVolume() -> uint16_t { |
|
|
|
|
uint16_t out = wm8523::kDefaultVolume; |
|
|
|
|
nvs_get_u16(handle_, kKeyAmpCurrentVolume, &out); |
|
|
|
|
return out; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
auto NvsStorage::AmpCurrentVolume(uint16_t val) -> std::future<bool> { |
|
|
|
|
return writer_->Dispatch<bool>([&]() { |
|
|
|
|
auto NvsStorage::AmpCurrentVolume(uint16_t val) -> bool { |
|
|
|
|
nvs_set_u16(handle_, kKeyAmpCurrentVolume, val); |
|
|
|
|
return nvs_commit(handle_) == ESP_OK; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
auto NvsStorage::HasShownOnboarding() -> std::future<bool> { |
|
|
|
|
return writer_->Dispatch<bool>([&]() -> bool { |
|
|
|
|
auto NvsStorage::HasShownOnboarding() -> bool { |
|
|
|
|
uint8_t out = false; |
|
|
|
|
nvs_get_u8(handle_, kKeyOnboarded, &out); |
|
|
|
|
return out; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
auto NvsStorage::HasShownOnboarding(bool val) -> std::future<bool> { |
|
|
|
|
return writer_->Dispatch<bool>([&]() { |
|
|
|
|
auto NvsStorage::HasShownOnboarding(bool val) -> bool { |
|
|
|
|
nvs_set_u8(handle_, kKeyOnboarded, val); |
|
|
|
|
return nvs_commit(handle_) == ESP_OK; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} // namespace drivers
|
|
|
|
|