Convert code to use Timer instead of Thread (#120)
This commit is contained in:
committed by
GitHub
parent
51c6aaa428
commit
34f99205ca
@@ -6,7 +6,7 @@ namespace tt::app::i2cscanner {
|
||||
|
||||
std::shared_ptr<Data> _Nullable optData();
|
||||
|
||||
static bool shouldStopScanThread(std::shared_ptr<Data> data) {
|
||||
static bool shouldStopScanTimer(std::shared_ptr<Data> data) {
|
||||
if (data->mutex.acquire(100 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
bool is_scanning = data->scanState == ScanStateScanning;
|
||||
tt_check(data->mutex.release() == TtStatusOk);
|
||||
@@ -38,10 +38,10 @@ static bool addAddressToList(std::shared_ptr<Data> data, uint8_t address) {
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t scanThread(TT_UNUSED void* context) {
|
||||
static void onScanTimer(TT_UNUSED std::shared_ptr<void> context) {
|
||||
auto data = optData();
|
||||
if (data == nullptr) {
|
||||
return -1;
|
||||
return;
|
||||
}
|
||||
|
||||
TT_LOG_I(TAG, "Scan thread started");
|
||||
@@ -51,39 +51,39 @@ static int32_t scanThread(TT_UNUSED void* context) {
|
||||
if (getPort(data, &port)) {
|
||||
if (hal::i2c::masterCheckAddressForDevice(port, address, 10 / portTICK_PERIOD_MS)) {
|
||||
TT_LOG_I(TAG, "Found device at address %d", address);
|
||||
if (!shouldStopScanThread(data)) {
|
||||
if (!shouldStopScanTimer(data)) {
|
||||
addAddressToList(data, address);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
TT_LOG_W(TAG, "scanThread lock");
|
||||
TT_LOG_W(TAG, "onScanTimer lock");
|
||||
break;
|
||||
}
|
||||
|
||||
if (shouldStopScanThread(data)) {
|
||||
if (shouldStopScanTimer(data)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
TT_LOG_I(TAG, "Scan thread finalizing");
|
||||
|
||||
onThreadFinished(data);
|
||||
onScanTimerFinished(data);
|
||||
|
||||
TT_LOG_I(TAG, "Scan thread stopped");
|
||||
|
||||
return 0;
|
||||
TT_LOG_I(TAG, "Scan timer done");
|
||||
}
|
||||
|
||||
bool hasScanThread(std::shared_ptr<Data> data) {
|
||||
bool has_thread;
|
||||
if (data->mutex.acquire(100 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
has_thread = data->scanThread != nullptr;
|
||||
has_thread = data->scanTimer != nullptr;
|
||||
tt_check(data->mutex.release() == TtStatusOk);
|
||||
return has_thread;
|
||||
} else {
|
||||
// Unsafe way
|
||||
TT_LOG_W(TAG, "hasScanThread lock");
|
||||
return data->scanThread != nullptr;
|
||||
TT_LOG_W(TAG, "hasScanTimer lock");
|
||||
return data->scanTimer != nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,15 +98,13 @@ void startScanning(std::shared_ptr<Data> data) {
|
||||
lv_obj_add_flag(data->scanListWidget, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_clean(data->scanListWidget);
|
||||
|
||||
tt_assert(data->scanThread == nullptr);
|
||||
data->scanState = ScanStateScanning;
|
||||
data->scanThread = new Thread(
|
||||
"i2c scanner",
|
||||
4096,
|
||||
scanThread,
|
||||
nullptr
|
||||
data->scanTimer = std::make_unique<Timer>(
|
||||
Timer::TypeOnce,
|
||||
onScanTimer,
|
||||
data
|
||||
);
|
||||
data->scanThread->start();
|
||||
data->scanTimer->start(10);
|
||||
tt_check(data->mutex.release() == TtStatusOk);
|
||||
} else {
|
||||
TT_LOG_W(TAG, "startScanning lock");
|
||||
@@ -114,25 +112,12 @@ void startScanning(std::shared_ptr<Data> data) {
|
||||
}
|
||||
|
||||
void stopScanning(std::shared_ptr<Data> data) {
|
||||
bool sent_halt;
|
||||
if (data->mutex.acquire(250 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
tt_assert(data->scanThread != nullptr);
|
||||
tt_assert(data->scanTimer != nullptr);
|
||||
data->scanState = ScanStateStopped;
|
||||
tt_check(data->mutex.release() == TtStatusOk);
|
||||
sent_halt = true;
|
||||
} else {
|
||||
sent_halt = false;
|
||||
}
|
||||
|
||||
if (sent_halt) {
|
||||
tt_assert(data->scanThread != nullptr);
|
||||
data->scanThread->join();
|
||||
|
||||
if (data->mutex.acquire(250 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
delete data->scanThread;
|
||||
data->scanThread = nullptr;
|
||||
tt_check(data->mutex.release() == TtStatusOk);
|
||||
}
|
||||
TT_LOG_E(TAG, "Acquire mutex failed");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user