Skip to content
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

flash_bootloader example fix #214

Merged
merged 2 commits into from
Sep 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions examples/src/flash_bootloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string>

#include "depthai/depthai.hpp"
#include "depthai/xlink/XLinkConnection.hpp"

int main(int argc, char** argv) {
using namespace std::chrono;
Expand All @@ -19,14 +20,6 @@ int main(int argc, char** argv) {
}
}

std::cout << "Warning! Flashing bootloader can potentially soft brick your device and should be done with caution." << std::endl;
std::cout << "Do not unplug your device while the bootloader is flashing." << std::endl;
std::cout << "Type 'y' and press enter to proceed, otherwise exits: ";
if(std::cin.get() != 'y') {
std::cout << "Prompt declined, exiting..." << std::endl;
return -1;
}

bool found = false;
dai::DeviceInfo info;
std::tie(found, info) = dai::DeviceBootloader::getFirstAvailableDevice();
Expand All @@ -35,13 +28,28 @@ int main(int argc, char** argv) {
return -1;
}

bool hasBootloader = (info.state == X_LINK_BOOTLOADER);
if(hasBootloader) {
std::cout << "Warning! Flashing bootloader can potentially soft brick your device and should be done with caution." << std::endl;
std::cout << "Do not unplug your device while the bootloader is flashing." << std::endl;
std::cout << "Type 'y' and press enter to proceed, otherwise exits: ";
if(std::cin.get() != 'y') {
std::cout << "Prompt declined, exiting..." << std::endl;
return -1;
}
}

// Open DeviceBootloader and allow flashing bootloader
std::cout << "Booting latest bootloader first, will take a tad longer..." << std::endl;
dai::DeviceBootloader bl(info, true);
auto currentBlType = bl.getType();

// Check if bootloader type is the same
if(blType != dai::DeviceBootloader::Type::AUTO && currentBlType != blType) {
if(blType == dai::DeviceBootloader::Type::AUTO) {
blType = currentBlType;
}

// Check if bootloader type is the same, if already booted by bootloader (not in USB recovery mode)
if(currentBlType != blType && hasBootloader) {
std::cout << "Are you sure you want to flash '" << blType << "' bootloader over current '" << currentBlType << "' bootloader?" << std::endl;
std::cout << "Type 'y' and press enter to proceed, otherwise exits: ";
std::cin.ignore();
Expand All @@ -54,11 +62,11 @@ int main(int argc, char** argv) {
// Create a progress callback lambda
auto progress = [](float p) { std::cout << "Flashing Progress..." << p * 100 << "%" << std::endl; };

std::cout << "Flashing " << currentBlType << " bootloader..." << std::endl;
std::cout << "Flashing " << blType << " bootloader..." << std::endl;
auto t1 = steady_clock::now();
bool success = false;
std::string message;
std::tie(success, message) = bl.flashBootloader(dai::DeviceBootloader::Memory::FLASH, currentBlType, progress);
std::tie(success, message) = bl.flashBootloader(dai::DeviceBootloader::Memory::FLASH, blType, progress);
if(success) {
std::cout << "Flashing successful. Took " << duration_cast<milliseconds>(steady_clock::now() - t1).count() << "ms" << std::endl;
} else {
Expand Down