Преглед изворни кода

code optimization & more events can be handled now

nmrr пре 3 година
родитељ
комит
7925a42ff6
1 измењених фајлова са 33 додато и 30 уклоњено
  1. 33 30
      flipper_geiger.c

+ 33 - 30
flipper_geiger.c

@@ -25,9 +25,8 @@ typedef struct {
 } EventApp;
 } EventApp;
 
 
 typedef struct {
 typedef struct {
-    uint32_t counter;
     uint32_t cps, cpm;
     uint32_t cps, cpm;
-    uint8_t line[SCREEN_SIZE_X/2];
+    uint32_t line[SCREEN_SIZE_X/2];
     float coef;
     float coef;
 } mutexStruct;
 } mutexStruct;
 
 
@@ -35,15 +34,17 @@ static void draw_callback(Canvas* canvas, void* ctx)
 {
 {
     UNUSED(ctx);
     UNUSED(ctx);
 
 
+    mutexStruct displayStruct;
     mutexStruct* lfsrMutex = (mutexStruct*)acquire_mutex_block((ValueMutex*)ctx);
     mutexStruct* lfsrMutex = (mutexStruct*)acquire_mutex_block((ValueMutex*)ctx);
-    char buffer[32];
+    memcpy(&displayStruct, lfsrMutex, sizeof(mutexStruct));
+    release_mutex((ValueMutex*)ctx, lfsrMutex);
 
 
-    snprintf(buffer, sizeof(buffer), "%ld cps - %ld cpm", lfsrMutex->cps, lfsrMutex->cpm);
+    char buffer[32];
+    snprintf(buffer, sizeof(buffer), "%ld cps - %ld cpm", displayStruct.cps, displayStruct.cpm);
 
 
     for (int i=0;i<SCREEN_SIZE_X;i+=2)
     for (int i=0;i<SCREEN_SIZE_X;i+=2)
     {
     {
-        float Y = SCREEN_SIZE_Y-(lfsrMutex->line[i/2]*lfsrMutex->coef);
-        if (Y < 0) Y = 0;
+        float Y = SCREEN_SIZE_Y-(displayStruct.line[i/2]*displayStruct.coef);
 
 
         canvas_draw_line(canvas, i, Y, i, SCREEN_SIZE_Y);
         canvas_draw_line(canvas, i, Y, i, SCREEN_SIZE_Y);
         canvas_draw_line(canvas, i+1, Y, i+1, SCREEN_SIZE_Y);
         canvas_draw_line(canvas, i+1, Y, i+1, SCREEN_SIZE_Y);
@@ -51,8 +52,6 @@ static void draw_callback(Canvas* canvas, void* ctx)
 
 
     canvas_set_font(canvas, FontPrimary);
     canvas_set_font(canvas, FontPrimary);
     canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignBottom, buffer);
     canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignBottom, buffer);
-
-    release_mutex((ValueMutex*)ctx, lfsrMutex);
 }
 }
 
 
 static void input_callback(InputEvent* input_event, void* ctx) 
 static void input_callback(InputEvent* input_event, void* ctx) 
@@ -66,10 +65,11 @@ static void input_callback(InputEvent* input_event, void* ctx)
 static void clock_tick(void* ctx) {
 static void clock_tick(void* ctx) {
     furi_assert(ctx);
     furi_assert(ctx);
 
 
-    uint8_t randomuint8[1];
-    furi_hal_random_fill_buf(randomuint8,1);
-    randomuint8[0] &= 0b00001111;                
-    furi_hal_pwm_start(FuriHalPwmOutputIdLptim2PA4, randomuint8[0], 20);
+    uint32_t randomNumber = furi_hal_random_get();
+    randomNumber &= 0xFFF;
+    if (randomNumber == 0) randomNumber = 1;
+
+    furi_hal_pwm_start(FuriHalPwmOutputIdLptim2PA4, randomNumber, 50);
 
 
     FuriMessageQueue* queue = ctx;
     FuriMessageQueue* queue = ctx;
     EventApp event = {.type = ClockEventTypeTick};
     EventApp event = {.type = ClockEventTypeTick};
@@ -93,12 +93,13 @@ int32_t flipper_geiger_app()
     furi_hal_pwm_start(FuriHalPwmOutputIdLptim2PA4, 5, 20);
     furi_hal_pwm_start(FuriHalPwmOutputIdLptim2PA4, 5, 20);
 
 
     mutexStruct mutexVal;
     mutexStruct mutexVal;
-    mutexVal.counter = 0;
     mutexVal.cps = 0;
     mutexVal.cps = 0;
     mutexVal.cpm = 0;
     mutexVal.cpm = 0;
     for (int i=0;i<SCREEN_SIZE_X/2;i++) mutexVal.line[i] = 0;
     for (int i=0;i<SCREEN_SIZE_X/2;i++) mutexVal.line[i] = 0;
     mutexVal.coef = 1;
     mutexVal.coef = 1;
 
 
+    uint32_t counter = 0;
+
     ValueMutex state_mutex;
     ValueMutex state_mutex;
     init_mutex(&state_mutex, &mutexVal, sizeof(mutexVal));
     init_mutex(&state_mutex, &mutexVal, sizeof(mutexVal));
 
 
@@ -117,8 +118,7 @@ int32_t flipper_geiger_app()
     while(1) 
     while(1) 
     {
     {
         FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
         FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
-        mutexStruct* lfsrMutex = (mutexStruct*)acquire_mutex_block(&state_mutex);
-
+        
         uint8_t screenRefresh = 0;
         uint8_t screenRefresh = 0;
 
 
         if (event_status == FuriStatusOk)
         if (event_status == FuriStatusOk)
@@ -127,48 +127,51 @@ int32_t flipper_geiger_app()
             {
             {
                 if(event.input.key == InputKeyBack) 
                 if(event.input.key == InputKeyBack) 
                 {
                 {
-                    release_mutex(&state_mutex, lfsrMutex);
                     break;
                     break;
                 }
                 }
                 else if(event.input.key == InputKeyOk && event.input.type == InputTypeShort)
                 else if(event.input.key == InputKeyOk && event.input.type == InputTypeShort)
                 {
                 {
-                    mutexVal.counter = 0;
+                    counter = 0;
+                    mutexStruct* lfsrMutex = (mutexStruct*)acquire_mutex_block(&state_mutex);
+
                     mutexVal.cps = 0;
                     mutexVal.cps = 0;
                     mutexVal.cpm = 0;
                     mutexVal.cpm = 0;
                     for (int i=0;i<SCREEN_SIZE_X/2;i++) mutexVal.line[i] = 0;
                     for (int i=0;i<SCREEN_SIZE_X/2;i++) mutexVal.line[i] = 0;
+
+                    screenRefresh = 1;
+                    release_mutex(&state_mutex, lfsrMutex);
                 }
                 }
             }
             }
             else if (event.type == ClockEventTypeTick)
             else if (event.type == ClockEventTypeTick)
             {
             {
-                for (int i=0;i<SCREEN_SIZE_X/2-1;i++) lfsrMutex->line[SCREEN_SIZE_X/2-1-i] = lfsrMutex->line[SCREEN_SIZE_X/2-2-i]; 
+                mutexStruct* lfsrMutex = (mutexStruct*)acquire_mutex_block(&state_mutex);
 
 
-                lfsrMutex->line[0] = lfsrMutex->counter;
-                lfsrMutex->cps = lfsrMutex->counter;
-                lfsrMutex->counter = 0;
+                for (int i=0;i<SCREEN_SIZE_X/2-1;i++) lfsrMutex->line[SCREEN_SIZE_X/2-1-i] = lfsrMutex->line[SCREEN_SIZE_X/2-2-i]; 
 
 
-                lfsrMutex->cpm = 0;
-                for (int i=0;i<60;i++) lfsrMutex->cpm += lfsrMutex->line[i];
+                lfsrMutex->line[0] = counter;
+                lfsrMutex->cps = counter;
+                counter = 0;
 
 
+                lfsrMutex->cpm = lfsrMutex->line[0];
                 uint32_t max = lfsrMutex->line[0];
                 uint32_t max = lfsrMutex->line[0];
                 for (int i=1;i<SCREEN_SIZE_X/2;i++)
                 for (int i=1;i<SCREEN_SIZE_X/2;i++)
                 {
                 {
+                    if (i < 60) lfsrMutex->cpm += lfsrMutex->line[i];
                     if (lfsrMutex->line[i] > max) max = lfsrMutex->line[i];
                     if (lfsrMutex->line[i] > max) max = lfsrMutex->line[i];
                 }
                 }
 
 
-                if (max > 0)
-                {
-                    lfsrMutex->coef = ((float)(SCREEN_SIZE_Y-15))/((float)max);
-                }
+                if (max > 0) lfsrMutex->coef = ((float)(SCREEN_SIZE_Y-15))/((float)max);
+                else lfsrMutex->coef = 1;
 
 
                 screenRefresh = 1;
                 screenRefresh = 1;
+                release_mutex(&state_mutex, lfsrMutex);
             }
             }
             else if (event.type == EventGPIO)
             else if (event.type == EventGPIO)
             {
             {
-                lfsrMutex->counter = lfsrMutex->counter + 1;
+                counter++;
             }
             }
         }
         }
 
 
-        release_mutex(&state_mutex, lfsrMutex);
         if (screenRefresh == 1) view_port_update(view_port);
         if (screenRefresh == 1) view_port_update(view_port);
     }
     }
 
 
@@ -184,4 +187,4 @@ int32_t flipper_geiger_app()
     furi_record_close(RECORD_GUI);
     furi_record_close(RECORD_GUI);
 
 
     return 0;
     return 0;
-}
+}