-
Notifications
You must be signed in to change notification settings - Fork 10
Serial bus No. 3: Configure peers over Serial Bus #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d8957e7
add bus_backup & configuration for serial_bus peers
JensOgorek 17a5ea2
update - working configure
JensOgorek 6aeacaa
simplification
JensOgorek f0f5e93
add documentation
JensOgorek b05cbfa
add review suggestion
JensOgorek 00bf262
Review
JensOgorek 40d04bc
update handling strings
JensOgorek 564c1c6
Merge remote-tracking branch 'origin/main' into serial_bus_configure
JensOgorek 6bfea59
use consistent type of subheading
falkoschindler 49710a4
use braces for consistency
falkoschindler cb48523
minor renaming and refactoring
falkoschindler 0ee533a
review
JensOgorek fef8801
update timeout more dynamic
JensOgorek b717a9a
improve reboot polling on serial bus
falkoschindler 806ed38
simplify wait-for-reboot loops
falkoschindler 15990fb
fix empty or short lines
JensOgorek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| #include "bus_backup.h" | ||
|
|
||
| #include "../global.h" | ||
| #include "../modules/serial.h" | ||
| #include "../modules/serial_bus.h" | ||
| #include "uart.h" | ||
|
|
||
| #include "nvs.h" | ||
|
|
||
| #define NVS_NAMESPACE "bus_backup" | ||
|
|
||
| namespace bus_backup { | ||
|
|
||
| void save_if_present() { | ||
| for (const auto &[name, module] : Global::modules) { | ||
| if (module->type != serial_bus) { | ||
| continue; | ||
| } | ||
| const auto bus = std::static_pointer_cast<SerialBus>(module); | ||
| nvs_handle handle; | ||
| if (nvs_open(NVS_NAMESPACE, NVS_READWRITE, &handle) != ESP_OK) { | ||
| return; | ||
| } | ||
| bool ok = nvs_set_i8(handle, "tx", bus->serial->tx_pin) == ESP_OK && | ||
| nvs_set_i8(handle, "rx", bus->serial->rx_pin) == ESP_OK && | ||
| nvs_set_i32(handle, "baud", bus->serial->baud_rate) == ESP_OK && | ||
| nvs_set_i8(handle, "uart", bus->serial->uart_num) == ESP_OK && | ||
| nvs_set_i8(handle, "node", bus->node_id) == ESP_OK; | ||
| if (!ok) { | ||
| echo("error saving bus backup to NVS"); | ||
| } | ||
| nvs_commit(handle); | ||
| nvs_close(handle); | ||
| return; | ||
JensOgorek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| void restore_if_needed() { | ||
| for (const auto &[name, module] : Global::modules) { | ||
| if (module->type == serial_bus) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| nvs_handle handle; | ||
| if (nvs_open(NVS_NAMESPACE, NVS_READONLY, &handle) != ESP_OK) { | ||
| return; | ||
| } | ||
| int8_t tx, rx, uart, node; | ||
| int32_t baud; | ||
| bool ok = nvs_get_i8(handle, "tx", &tx) == ESP_OK && | ||
| nvs_get_i8(handle, "rx", &rx) == ESP_OK && | ||
| nvs_get_i32(handle, "baud", &baud) == ESP_OK && | ||
| nvs_get_i8(handle, "uart", &uart) == ESP_OK && | ||
| nvs_get_i8(handle, "node", &node) == ESP_OK; | ||
| nvs_close(handle); | ||
| if (!ok) { | ||
| return; | ||
| } | ||
|
|
||
| echo("restoring serial bus from backup"); | ||
| try { | ||
| std::vector<std::string> serials_to_remove; | ||
| for (const auto &[name, module] : Global::modules) { | ||
| if (module->type == serial) { | ||
| serials_to_remove.push_back(name); | ||
| } | ||
| } | ||
| for (const std::string &name : serials_to_remove) { | ||
| Global::modules.erase(name); | ||
| Global::variables.erase(name); | ||
| } | ||
| Serial_ptr backup_serial = std::make_shared<Serial>( | ||
| "_backup_serial", static_cast<gpio_num_t>(rx), static_cast<gpio_num_t>(tx), | ||
| baud, static_cast<uart_port_t>(uart)); | ||
| Global::add_module("_backup_serial", backup_serial); | ||
| const auto bus = std::make_shared<SerialBus>("_backup_bus", backup_serial, node); | ||
JensOgorek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Global::add_module("_backup_bus", bus); | ||
| } catch (const std::runtime_error &e) { | ||
| echo("bus backup error: %s", e.what()); | ||
| } | ||
| } | ||
|
|
||
| void remove() { | ||
| nvs_handle handle; | ||
| if (nvs_open(NVS_NAMESPACE, NVS_READWRITE, &handle) != ESP_OK) { | ||
| return; | ||
| } | ||
| nvs_erase_all(handle); | ||
| nvs_commit(handle); | ||
| nvs_close(handle); | ||
| } | ||
|
|
||
| } // namespace bus_backup | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #pragma once | ||
|
|
||
| namespace bus_backup { | ||
|
|
||
| void save_if_present(); | ||
| void restore_if_needed(); | ||
| void remove(); | ||
|
|
||
| } // namespace bus_backup |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.