Ver Fonte

added uhf rfid worker thread classes

frux-c há 2 anos atrás
pai
commit
caaa294ece
2 ficheiros alterados com 88 adições e 6 exclusões
  1. 48 0
      uhf_worker.c
  2. 40 6
      uhf_worker.h

+ 48 - 0
uhf_worker.c

@@ -0,0 +1,48 @@
+#include "uhf_data.h"
+#include "uhf_worker.h"
+
+int32_t uhf_worker_task(void* ctx) {
+    UNUSED(ctx);
+    return 0;
+}
+
+UHFWorker* uhf_worker_alloc() {
+    UHFWorker* uhf_worker = (UHFWorker*)malloc(sizeof(UHFWorker));
+    uhf_worker->thread = furi_thread_alloc_ex("UHFWorker", 8 * 1024, uhf_worker_task, uhf_worker);
+    uhf_worker->data = uhf_response_data_alloc();
+    uhf_worker->callback = NULL;
+    uhf_worker->ctx = NULL;
+    return uhf_worker;
+}
+
+void uhf_worker_change_state(UHFWorker* worker, UHFWorkerState state) {
+    worker->state = state;
+}
+
+void uhf_worker_start(
+    UHFWorker* uhf_worker,
+    UHFWorkerState state,
+    UHFWorkerCallback callback,
+    void* ctx) {
+    uhf_worker->state = state;
+    uhf_worker->callback = callback;
+    uhf_worker->ctx = ctx;
+    furi_thread_start(uhf_worker->thread);
+}
+
+void uhf_worker_stop(UHFWorker* uhf_worker) {
+    furi_assert(uhf_worker);
+    furi_assert(uhf_worker->thread);
+
+    if(furi_thread_get_state(uhf_worker->thread) != FuriThreadStateStopped) {
+        uhf_worker_change_state(uhf_worker, UHFWorkerStateStop);
+        furi_thread_join(uhf_worker->thread);
+    }
+}
+
+void uhf_worker_free(UHFWorker* uhf_worker) {
+    furi_assert(uhf_worker);
+    furi_thread_free(uhf_worker->thread);
+    uhf_data_free((UHFData*)uhf_worker->data);
+    free(uhf_worker);
+}

+ 40 - 6
uhf_worker.h

@@ -1,13 +1,47 @@
 #pragma once
+
 #include <furi.h>
 #include <furi_hal.h>
 #include "uhf_data.h"
 
-typedef struct UHFRFIDWorker {
+typedef enum {
+    // Init states
+    UHFWorkerStateNone,
+    UHFWorkerStateBroken,
+    UHFWorkerStateReady,
+    // Main worker states
+    UHFWorkerStateDetect,
+    UHFWorkerStateWrite,
+    UHFWorkerStateWriteKey,
+    // Transition
+    UHFWorkerStateStop,
+} UHFWorkerState;
+
+typedef enum {
+    UHFWorkerEventSuccess,
+    UHFWorkerEventFail,
+    UHFWorkerEventNoTagDetected,
+    UHFWorkerEventAborted,
+    UHFWorkerEventCardDetected,
+} UHFWorkerEvent;
+
+typedef void (*UHFWorkerCallback)(UHFWorkerEvent event, void* ctx);
+
+typedef struct UHFWorker {
     FuriThread* thread;
-    UHFReturnData* uhf_data;
-    // UHFRFIDWorkerCMD cmd;
-    // Storage* storage;
-    // UHFRFIDWorkerCallback callback;
+    UHFResponseData* data;
+    UHFWorkerCallback callback;
+    UHFWorkerState state;
+    void* ctx;
+} UHFWorker;
 
-} UHFRFIDWorker;
+int32_t uhf_worker_task(void* ctx);
+UHFWorker* uhf_worker_alloc();
+void uhf_worker_change_state(UHFWorker* worker, UHFWorkerState state);
+void uhf_worker_start(
+    UHFWorker* uhf_worker,
+    UHFWorkerState state,
+    UHFWorkerCallback callback,
+    void* ctx);
+void uhf_worker_stop(UHFWorker* uhf_worker);
+void uhf_worker_free(UHFWorker* uhf_worker);