2023-09-14 02:56:30 +00:00
|
|
|
#include "../wiegand.h"
|
|
|
|
|
2023-11-21 21:08:54 +00:00
|
|
|
FuriTimer *timer = NULL;
|
2023-09-14 02:56:30 +00:00
|
|
|
|
2023-11-21 21:08:54 +00:00
|
|
|
static void wiegand_scan_isr_d0(void *context)
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
UNUSED(context);
|
|
|
|
uint32_t time = DWT->CYCCNT;
|
|
|
|
bool rise = furi_hal_gpio_read(pinD0);
|
|
|
|
|
|
|
|
data[bit_count] = 0;
|
|
|
|
|
2023-11-21 21:08:54 +00:00
|
|
|
if (rise)
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
data_rise[bit_count] = time;
|
2023-11-21 21:08:54 +00:00
|
|
|
if (bit_count < MAX_BITS)
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
bit_count++;
|
|
|
|
}
|
2023-11-21 21:08:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
data_fall[bit_count] = time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 21:08:54 +00:00
|
|
|
static void wiegand_scan_isr_d1(void *context)
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
UNUSED(context);
|
|
|
|
uint32_t time = DWT->CYCCNT;
|
|
|
|
bool rise = furi_hal_gpio_read(pinD1);
|
|
|
|
|
|
|
|
data[bit_count] = 1;
|
|
|
|
|
2023-11-21 21:08:54 +00:00
|
|
|
if (rise)
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
data_rise[bit_count] = time;
|
2023-11-21 21:08:54 +00:00
|
|
|
if (bit_count < MAX_BITS)
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
bit_count++;
|
|
|
|
}
|
2023-11-21 21:08:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
data_fall[bit_count] = time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 21:08:54 +00:00
|
|
|
static void wiegand_start_scan(void *context)
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
UNUSED(context);
|
|
|
|
data_saved = false;
|
|
|
|
bit_count = 0;
|
|
|
|
furi_hal_gpio_init_simple(pinD0, GpioModeInterruptRiseFall);
|
|
|
|
furi_hal_gpio_init_simple(pinD1, GpioModeInterruptRiseFall);
|
|
|
|
furi_hal_gpio_add_int_callback(pinD0, wiegand_scan_isr_d0, NULL);
|
|
|
|
furi_hal_gpio_add_int_callback(pinD1, wiegand_scan_isr_d1, NULL);
|
|
|
|
furi_timer_start(timer, 100);
|
|
|
|
}
|
|
|
|
|
2023-11-21 21:08:54 +00:00
|
|
|
static void wiegand_stop_scan(void *context)
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
UNUSED(context);
|
|
|
|
furi_hal_gpio_remove_int_callback(pinD0);
|
|
|
|
furi_hal_gpio_remove_int_callback(pinD1);
|
|
|
|
furi_hal_gpio_init_simple(pinD0, GpioModeAnalog);
|
|
|
|
furi_hal_gpio_init_simple(pinD1, GpioModeAnalog);
|
|
|
|
furi_timer_stop(timer);
|
|
|
|
}
|
|
|
|
|
2023-11-21 21:08:54 +00:00
|
|
|
static void wiegand_scan_found(void *context)
|
|
|
|
{
|
|
|
|
App *app = context;
|
2023-09-14 02:56:30 +00:00
|
|
|
|
|
|
|
FuriHalRtcDateTime datetime;
|
|
|
|
furi_hal_rtc_get_datetime(&datetime);
|
|
|
|
snprintf(
|
|
|
|
app->file_name,
|
2023-11-22 06:15:31 +00:00
|
|
|
50,
|
|
|
|
"%02d_%02d_%02d_%02d_%02d_%02d",
|
2023-09-14 02:56:30 +00:00
|
|
|
datetime.year,
|
|
|
|
datetime.month,
|
|
|
|
datetime.day,
|
|
|
|
datetime.hour,
|
|
|
|
datetime.minute,
|
|
|
|
datetime.second);
|
|
|
|
furi_hal_vibro_on(true);
|
|
|
|
furi_delay_ms(50);
|
|
|
|
furi_hal_vibro_on(false);
|
|
|
|
furi_delay_ms(50);
|
|
|
|
wiegand_save(app);
|
|
|
|
furi_hal_vibro_on(true);
|
|
|
|
furi_delay_ms(50);
|
|
|
|
furi_hal_vibro_on(false);
|
|
|
|
furi_delay_ms(1000);
|
|
|
|
wiegand_start_scan(app);
|
|
|
|
}
|
|
|
|
|
2023-11-21 21:08:54 +00:00
|
|
|
static void wiegand_scan_timer_callback(void *context)
|
|
|
|
{
|
|
|
|
App *app = context;
|
2023-09-14 02:56:30 +00:00
|
|
|
uint32_t duration = DWT->CYCCNT;
|
|
|
|
const uint32_t one_millisecond = 64000;
|
|
|
|
|
2023-11-21 21:08:54 +00:00
|
|
|
if (bit_count == 0)
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
duration -= data_fall[bit_count - 1];
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
FURI_CRITICAL_ENTER();
|
2023-11-21 21:08:54 +00:00
|
|
|
if (duration > 25 * one_millisecond)
|
|
|
|
{
|
|
|
|
if (bit_count == 4 || bit_count == 8 || bit_count == 24 || bit_count == 26 ||
|
|
|
|
bit_count == 32 || bit_count == 34 || bit_count == 35 || bit_count == 36 ||
|
|
|
|
bit_count == 37 || bit_count == 40 || bit_count == 48)
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
wiegand_stop_scan(app);
|
|
|
|
found = true;
|
2023-11-21 21:08:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
// No data, clear
|
|
|
|
bit_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FURI_CRITICAL_EXIT();
|
|
|
|
|
2023-11-21 21:08:54 +00:00
|
|
|
if (found)
|
|
|
|
{
|
2023-09-14 02:56:30 +00:00
|
|
|
wiegand_scan_found(app);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 21:08:54 +00:00
|
|
|
void wiegand_scan_scene_on_enter(void *context)
|
|
|
|
{
|
|
|
|
App *app = context;
|
2023-09-14 02:56:30 +00:00
|
|
|
timer = furi_timer_alloc(wiegand_scan_timer_callback, FuriTimerTypePeriodic, app);
|
|
|
|
widget_reset(app->widget);
|
|
|
|
widget_add_string_element(app->widget, 0, 0, AlignLeft, AlignTop, FontPrimary, "Scan Wiegand");
|
|
|
|
widget_add_string_element(
|
|
|
|
app->widget, 0, 20, AlignLeft, AlignTop, FontSecondary, "Files saved automatically.");
|
|
|
|
widget_add_string_element(
|
|
|
|
app->widget, 0, 30, AlignLeft, AlignTop, FontSecondary, "Vibrates on detection.");
|
|
|
|
widget_add_string_element(
|
|
|
|
app->widget, 0, 45, AlignLeft, AlignTop, FontSecondary, "D0/Green/A4");
|
|
|
|
widget_add_string_element(
|
|
|
|
app->widget, 0, 53, AlignLeft, AlignTop, FontSecondary, "D1/White/A7");
|
|
|
|
wiegand_start_scan(app);
|
|
|
|
view_dispatcher_switch_to_view(app->view_dispatcher, WiegandWidgetView);
|
|
|
|
}
|
|
|
|
|
2023-11-21 21:08:54 +00:00
|
|
|
void wiegand_scan_scene_on_exit(void *context)
|
|
|
|
{
|
|
|
|
App *app = context;
|
2023-09-14 02:56:30 +00:00
|
|
|
wiegand_stop_scan(app);
|
|
|
|
furi_timer_free(timer);
|
|
|
|
}
|