Procházet zdrojové kódy

Add atomicdiceroller from https://github.com/xMasterX/all-the-plugins

git-subtree-dir: atomicdiceroller
git-subtree-mainline: 359db7168bf2abd5ae730d69a349f7078172cfd3
git-subtree-split: 7a90c00d9baeb255e8c4c3d47b43b5127ee9f6a1
Willy-JL před 2 roky
rodič
revize
1ac00140fd

+ 1 - 0
atomicdiceroller/.gitsubtree

@@ -0,0 +1 @@
+https://github.com/xMasterX/all-the-plugins dev non_catalog_apps/flipper_atomicdiceroller

+ 48 - 0
atomicdiceroller/README.md

@@ -0,0 +1,48 @@
+# flipperzero-atomicdiceroller
+🎲☢ An atomic dice roller for the Flipper Zero ☢🎲
+
+![banner](https://github.com/nmrr/flipperzero-atomicdiceroller/blob/main/img/banner.jpg)
+
+[**Geiger Counter**](https://github.com/nmrr/flipperzero-geigercounter) application must work on your **Flipper Zero** to be able to use this application. You need the same **geiger board** and this board must be connected in the same way as described in the description of the **Geiger Counter** application.
+
+**Note:** There is no test port compared to the **Geiger Counter** application, your absolutely need a **geiger board** to run this application.
+
+This application generates random numbers by hashing timestamps from a 64 MHz 32-bit time clock (TIM2) from the **MCU**. Two hash methods have been implemented:
+- CRC32: 8 ticks are needed to obtain a hash, for low activity sources
+- MD5: 32 ticks are needed to obtain a hash, for high activity source
+
+Dice rolls are produced by transforming hashes into 1 to 6 numbers. Output is balanced by ignoring out of scope values. Modulo-based methods are ugly because they are usually unbalanced.
+
+It's possible to roll the dice without using a **radioactive isotope**. Air contains **radon** gas that is **radioactive**. **Geiger board** can detect descendants of radon gas that emit strong **beta** or **gamma** rays.
+
+## Gallery
+
+<img src="https://github.com/nmrr/flipperzero-atomicdiceroller/blob/main/img/flipper2.png" width=25% height=25%> <img src="https://github.com/nmrr/flipperzero-atomicdiceroller/blob/main/img/flipper3.png" width=25% height=25%>
+
+In the left corner, **counts per second** (cps) indicates the activity. In the right corner, **availiable dice rolls** are indicated. 64 rolls can be stored.
+
+## Build the program
+
+Assuming the toolchain is already installed, copy **flipper_atomicdiceroller** directory to **applications_user**
+
+Plug your **Flipper Zero** and build the atomic dice roller:
+```
+./fbt launch_app APPSRC=applications_user/flipper_atomicdiceroller
+```
+
+The program will automatically be launched after compilation
+
+<img src="https://github.com/nmrr/flipperzero-atomicdiceroller/blob/main/img/flipper1.png" width=25% height=25%>
+
+**Button assignments**: 
+
+button  | function
+------------- | -------------
+**Ok** *[short short]*  | Roll the dice
+**Left** *[long press]* | Set CRC32 as hash method
+**Right** *[long press]* | Set MD5 as hash method
+**Back** *[long press]*  | Exit
+
+If you don't want to build this application, just simply copy **flipper_atomicdiceroller.fap** on your **Flipper Zero** 
+
+Build has been made with official toolchain, **API Mismatch** error may appear if you are using custom firmware. You can bypass this error but the program may crash.

+ 13 - 0
atomicdiceroller/application.fam

@@ -0,0 +1,13 @@
+App(
+    appid="flipper_atomicdiceroller",
+    name="Atomic Dice Roller",
+    apptype=FlipperAppType.EXTERNAL,
+    entry_point="flipper_atomicdiceroller_app",
+    cdefines=["APP_ATOMICDICEROLLER"],
+    requires=[
+        "gui",
+    ],
+    stack_size=2 * 1024,
+    fap_icon="atomicdiceroller.png",
+    fap_category="GPIO",
+)

binární
atomicdiceroller/atomicdiceroller.png


+ 368 - 0
atomicdiceroller/flipper_atomicdiceroller.c

@@ -0,0 +1,368 @@
+// CC0 1.0 Universal (CC0 1.0)
+// Public Domain Dedication
+// https://github.com/nmrr
+
+#include <stdio.h>
+#include <furi.h>
+#include <gui/gui.h>
+#include <input/input.h>
+#include <notification/notification_messages.h>
+#include <furi_hal_power.h>
+#include <locale/locale.h>
+#include <toolbox/crc32_calc.h>
+#include <lib/toolbox/md5.h>
+
+#define SCREEN_SIZE_X 128
+#define SCREEN_SIZE_Y 64
+
+typedef enum {
+    EventTypeInput,
+    ClockEventTypeTick,
+    ClockEventTypeTickPause,
+    EventGPIO,
+} EventType;
+
+typedef struct {
+    EventType type;
+    InputEvent input;
+} EventApp;
+
+#define lineArraySize 128
+
+typedef struct {
+    FuriMutex* mutex;
+    uint32_t cps;
+    uint32_t diceAvailiable;
+    uint8_t dice;
+    uint8_t method;
+    uint8_t pause;
+} mutexStruct;
+
+static void draw_callback(Canvas* canvas, void* ctx) 
+{
+    mutexStruct* mutexVal = ctx;
+    mutexStruct mutexDraw;
+    furi_mutex_acquire(mutexVal->mutex, FuriWaitForever);
+    memcpy(&mutexDraw, mutexVal, sizeof(mutexStruct));
+    furi_mutex_release(mutexVal->mutex);
+
+    canvas_set_font(canvas, FontPrimary);
+    char buffer[32];
+    snprintf(buffer, sizeof(buffer), "%ld cps", mutexDraw.cps);
+    canvas_draw_str_aligned(canvas, 0, 10, AlignLeft, AlignBottom, buffer);
+
+    snprintf(buffer, sizeof(buffer), "%lu/64", mutexDraw.diceAvailiable);
+    canvas_draw_str_aligned(canvas, SCREEN_SIZE_X, 10, AlignRight, AlignBottom, buffer);
+
+    if (mutexDraw.method == 0) canvas_draw_str_aligned(canvas, 0, 20, AlignLeft, AlignBottom, "Hash: CRC32");
+    else canvas_draw_str_aligned(canvas, 0, 20, AlignLeft, AlignBottom, "Hash: MD5");
+
+    if (mutexDraw.dice != 0 && mutexDraw.pause == 0)
+    {
+        canvas_set_font(canvas, FontBigNumbers);
+        snprintf(buffer, sizeof(buffer), "%u", mutexDraw.dice);
+        canvas_draw_str_aligned(canvas, SCREEN_SIZE_X/2, 50, AlignCenter, AlignBottom, buffer);
+    }
+}
+
+static void input_callback(InputEvent* input_event, void* ctx) 
+{
+    furi_assert(ctx);
+    FuriMessageQueue* event_queue = ctx;
+    EventApp event = {.type = EventTypeInput, .input = *input_event};
+    furi_message_queue_put(event_queue, &event, FuriWaitForever);
+}
+
+static void clock_tick(void* ctx) {
+    furi_assert(ctx);
+
+    FuriMessageQueue* queue = ctx;
+    EventApp event = {.type = ClockEventTypeTick};
+    furi_message_queue_put(queue, &event, 0);
+}
+
+static void clock_tick_pause(void* ctx) {
+    furi_assert(ctx);
+
+    FuriMessageQueue* queue = ctx;
+    EventApp event = {.type = ClockEventTypeTickPause};
+    furi_message_queue_put(queue, &event, 0);
+}
+
+static void gpiocallback(void* ctx) {
+    furi_assert(ctx);
+    FuriMessageQueue* queue = ctx;
+    EventApp event = {.type = EventGPIO};
+    furi_message_queue_put(queue, &event, 0);
+}
+
+int32_t flipper_atomicdiceroller_app() 
+{
+    furi_hal_bus_enable(FuriHalBusTIM2);
+    LL_TIM_SetCounterMode(TIM2, LL_TIM_COUNTERMODE_UP);
+    LL_TIM_SetClockDivision(TIM2, LL_TIM_CLOCKDIVISION_DIV1);
+    LL_TIM_SetPrescaler(TIM2, 0);
+    LL_TIM_SetAutoReload(TIM2, 0xFFFFFFFF);
+    LL_TIM_SetCounter(TIM2, 0);
+    LL_TIM_EnableCounter(TIM2);
+
+    EventApp event;
+    FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(EventApp));
+
+    furi_hal_gpio_init(&gpio_ext_pa7, GpioModeInterruptFall, GpioPullUp, GpioSpeedVeryHigh);
+
+    mutexStruct mutexVal;
+    mutexVal.cps = 0;
+    mutexVal.dice = 0;
+    mutexVal.diceAvailiable = 0;
+    mutexVal.method = 0;
+    uint32_t counter = 0;
+
+    mutexVal.mutex= furi_mutex_alloc(FuriMutexTypeNormal);
+    if(!mutexVal.mutex) {
+        furi_message_queue_free(event_queue);
+        return 255;
+    }
+
+    ViewPort* view_port = view_port_alloc();
+    view_port_draw_callback_set(view_port, draw_callback, &mutexVal.mutex);
+    view_port_input_callback_set(view_port, input_callback, event_queue);
+
+    furi_hal_gpio_add_int_callback(&gpio_ext_pa7, gpiocallback, event_queue);
+
+    Gui* gui = furi_record_open(RECORD_GUI);
+    gui_add_view_port(gui, view_port, GuiLayerFullscreen);
+
+    FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
+    furi_timer_start(timer, 1000);
+
+    FuriTimer* timerPause = furi_timer_alloc(clock_tick_pause, FuriTimerTypePeriodic, event_queue);
+
+    // ENABLE 5V pin
+    furi_hal_power_enable_otg();
+
+    uint8_t diceBuffer[64];
+    for (uint8_t i=0;i<64;i++) diceBuffer[i] = 0;
+
+    uint8_t diceBufferCounter = 0;
+    uint8_t diceBufferPositionWrite = 0;
+    uint8_t diceBufferPositionRead = 0;
+    uint8_t tickCounter = 0;
+    uint32_t CRC32 = 0;
+    uint8_t method = 0;
+
+    // MD5
+    md5_context* md5_ctx = malloc(sizeof(md5_context));
+    uint8_t* hash = malloc(sizeof(uint8_t) * 16);
+    uint8_t* bufferTim2 = malloc(4);
+    md5_starts(md5_ctx);
+
+    uint8_t pause = 0;
+
+    while(1) 
+    {
+        FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
+        
+        uint8_t screenRefresh = 0;
+
+        if (event_status == FuriStatusOk)
+        {   
+            if(event.type == EventTypeInput) 
+            {
+                if(event.input.key == InputKeyBack && event.input.type == InputTypeLong) 
+                {
+                    break;
+                }
+                else if (pause == 0)
+                {
+                    if (event.input.key == InputKeyOk  && event.input.type == InputTypeShort)
+                    {
+                        if (diceBufferCounter > 0)
+                        {
+                            furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
+                            mutexVal.dice = diceBuffer[diceBufferPositionRead];
+                            mutexVal.diceAvailiable = --diceBufferCounter;
+                            mutexVal.pause = 1;
+                            furi_mutex_release(mutexVal.mutex);
+
+                            if (diceBufferPositionRead != 63) diceBufferPositionRead++;
+                            else diceBufferPositionRead = 0;
+
+                            pause = 1;
+                            furi_timer_start(timerPause, 500);
+                            screenRefresh = 1;
+                        }
+                    }
+                    else if (event.input.key == InputKeyLeft  && event.input.type == InputTypeLong)
+                    {
+                        if (method == 1)
+                        {
+                            method = 0;
+                            diceBufferPositionWrite = 0;
+                            diceBufferPositionRead = 0;
+                            diceBufferCounter = 0;
+                            CRC32 = 0;
+                            tickCounter = 0;
+                            furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
+                            mutexVal.method = 0;
+                            mutexVal.dice = 0;
+                            mutexVal.diceAvailiable = 0;
+                            furi_mutex_release(mutexVal.mutex);
+                            screenRefresh = 1;
+                        }
+                    }
+                    else if (event.input.key == InputKeyRight && event.input.type == InputTypeLong)
+                    {
+                        if (method == 0)
+                        {
+                            method = 1;
+                            diceBufferPositionWrite = 0;
+                            diceBufferPositionRead = 0;
+                            diceBufferCounter = 0;
+                            md5_starts(md5_ctx);
+                            tickCounter = 0;
+                            furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
+                            mutexVal.method = 1;
+                            mutexVal.dice = 0;
+                            mutexVal.diceAvailiable = 0;
+                            furi_mutex_release(mutexVal.mutex);
+                            screenRefresh = 1;
+                        }
+                    }
+                }
+            }
+            else if (event.type == ClockEventTypeTick)
+            {
+                furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
+                mutexVal.cps = counter;
+                furi_mutex_release(mutexVal.mutex);
+
+                counter = 0;
+                screenRefresh = 1;
+            }
+            else if (event.type == ClockEventTypeTickPause)
+            {
+                furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
+                mutexVal.pause = 0;
+                furi_mutex_release(mutexVal.mutex);
+
+                furi_timer_stop(timerPause);
+
+                pause = 0;
+                screenRefresh = 1;
+            }
+            else if (event.type == EventGPIO)
+            {
+                if (diceBufferCounter < 64)
+                {
+                    // CRC32
+                    if (method == 0)
+                    {
+                        uint32_t TIM2Tick = TIM2->CNT;
+                        bufferTim2[0] = (uint8_t)(TIM2Tick >> 24);
+                        bufferTim2[1] = (uint8_t)(TIM2Tick >> 16);
+                        bufferTim2[2] = (uint8_t)(TIM2Tick >> 8);
+                        bufferTim2[3] = (uint8_t)TIM2Tick;
+                        CRC32 = crc32_calc_buffer(CRC32, bufferTim2, 4);
+                        tickCounter++;
+
+                        if (tickCounter == 8)
+                        {
+                            uint8_t localDice = CRC32 & 0b111;
+
+                            if (localDice == 0 || localDice == 7)
+                            {
+                                localDice = (diceBuffer[diceBufferPositionRead] >> 3) & 0b111;
+                            }
+
+                            if (localDice >= 1 && localDice <= 6)
+                            {
+                                diceBuffer[diceBufferPositionWrite] = localDice;
+                                diceBufferCounter++;
+                                if (diceBufferPositionWrite != 63) diceBufferPositionWrite++;
+                                else diceBufferPositionWrite = 0;
+
+                                furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
+                                mutexVal.diceAvailiable = diceBufferCounter;
+                                furi_mutex_release(mutexVal.mutex);
+
+                                screenRefresh = 1;
+                            }
+
+                            CRC32 = 0;
+                            tickCounter = 0;
+                        }
+                    }
+                    // MD5
+                    else
+                    {
+                        uint32_t tick = TIM2->CNT;
+                        bufferTim2[0] = (uint8_t)(tick >> 24);
+                        bufferTim2[1] = (uint8_t)(tick >> 16);
+                        bufferTim2[2] = (uint8_t)(tick >> 8);
+                        bufferTim2[3] = (uint8_t)tick;
+                        md5_update(md5_ctx, bufferTim2, 4);
+
+                        tickCounter++;
+
+                        if (tickCounter == 32)
+                        {
+                            md5_finish(md5_ctx, hash);
+                            uint8_t localDice = 0;
+
+                            for (uint8_t i=0;i<16;i++)
+                            {
+                                localDice = hash[i] & 0b111;
+                                if (localDice >= 1 && localDice <= 6)
+                                {
+                                    diceBuffer[diceBufferPositionWrite] = localDice;
+                                    diceBufferCounter++;
+                                    if (diceBufferPositionWrite != 63) diceBufferPositionWrite++;
+                                    else diceBufferPositionWrite = 0;
+
+                                    furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
+                                    mutexVal.diceAvailiable = diceBufferCounter;
+                                    furi_mutex_release(mutexVal.mutex);
+
+                                    screenRefresh = 1;
+                                    break;
+                                }
+                            }
+
+                            md5_starts(md5_ctx);
+                            tickCounter = 0;
+                        }
+                    }
+                }
+
+                counter++;
+            }
+        }
+
+        if (screenRefresh == 1) view_port_update(view_port);
+    }
+
+    LL_TIM_DisableCounter(TIM2);
+    furi_hal_bus_disable(FuriHalBusTIM2);
+
+    free(md5_ctx);
+    free(bufferTim2);
+    free(hash);
+        
+    furi_record_close(RECORD_NOTIFICATION);
+
+    furi_hal_power_disable_otg();
+
+    furi_hal_gpio_disable_int_callback(&gpio_ext_pa7);
+    furi_hal_gpio_remove_int_callback(&gpio_ext_pa7);
+
+    furi_message_queue_free(event_queue);
+    furi_mutex_free(mutexVal.mutex);
+    gui_remove_view_port(gui, view_port);
+    view_port_free(view_port);
+    furi_timer_free(timer);
+    furi_timer_free(timerPause);
+    furi_record_close(RECORD_GUI);
+
+    return 0;
+}