flipperzero-firmware/applications/desktop/views/desktop_view_locked.c
SG 274c12fc56
[FL-2274] Inventing streams and moving FFF to them (#981)
* Streams: string stream
* String stream: updated insert/delete api
* Streams: generic stream interface and string stream implementation
* Streams: helpers for insert and delete_and_insert
* FFF: now compatible with streams
* MinUnit: introduced tests with arguments
* FFF: stream access violation
* Streams: copy data between streams
* Streams: file stream
* FFF: documentation
* FFStream: documentation
* FFF: alloc as file
* MinUnit: support for nested tests
* Streams: changed delete_and_insert, now it returns success flag. Added ability dump stream inner parameters and data to cout.
* FFF: simplified file open function
* Streams: unit tests
* FFF: tests
* Streams: declare cache_size constant as define, to allow variable modified arrays
* FFF: lib moved to a separate folder
* iButton: new FFF
* RFID: new FFF
* Animations: new FFF
* IR: new FFF
* NFC: new FFF
* Flipper file format: delete lib
* U2F: new FFF
* Subghz: new FFF and streams
* Streams: read line
* Streams: split
* FuriCore: implement memset with extra asserts
* FuriCore: implement extra heap asserts without inventing memset
* Scene manager: protected access to the scene id stack with a size check
* NFC worker: dirty fix for issue where hal_nfc was busy on app start
* Furi: update allocator to erase memory on allocation. Replace furi_alloc with malloc.
* FuriCore: cleanup memmgr code.
* Furi HAL: furi_hal_init is split into critical and non-critical parts. The critical part is currently clock and console.
* Memmgr: added ability to track allocations and deallocations through console.
* FFStream: some speedup
* Streams, FF: minor fixes
* Tests: restore
* File stream: a slightly more thread-safe version of file_stream_delete_and_insert

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2022-02-18 22:53:46 +03:00

234 lines
7.7 KiB
C

#include <projdefs.h>
#include <stdint.h>
#include <furi.h>
#include <gui/elements.h>
#include <gui/icon.h>
#include <gui/view.h>
#include <portmacro.h>
#include "../desktop_settings/desktop_settings.h"
#include "../desktop_i.h"
#include "desktop_view_locked.h"
#define DOOR_MOVING_INTERVAL_MS (1000 / 16)
#define UNLOCKED_HINT_TIMEOUT_MS (2000)
#define DOOR_OFFSET_START -55
#define DOOR_OFFSET_END 0
#define DOOR_L_FINAL_POS 0
#define DOOR_R_FINAL_POS 60
#define UNLOCK_CNT 3
#define UNLOCK_RST_TIMEOUT 600
struct DesktopViewLocked {
View* view;
DesktopViewLockedCallback callback;
void* context;
TimerHandle_t timer;
uint8_t lock_count;
uint32_t lock_lastpress;
};
typedef struct {
uint32_t hint_icon_expire_at;
bool unlocked_hint;
bool locked;
bool pin_locked;
int8_t door_offset;
bool doors_closing;
} DesktopViewLockedModel;
void desktop_view_locked_set_callback(
DesktopViewLocked* locked_view,
DesktopViewLockedCallback callback,
void* context) {
furi_assert(locked_view);
furi_assert(callback);
locked_view->callback = callback;
locked_view->context = context;
}
static void locked_view_timer_callback(TimerHandle_t timer) {
DesktopViewLocked* locked_view = pvTimerGetTimerID(timer);
locked_view->callback(DesktopLockedEventUpdate, locked_view->context);
}
static void desktop_view_locked_doors_draw(Canvas* canvas, DesktopViewLockedModel* model) {
int8_t offset = model->door_offset;
uint8_t door_left_x = DOOR_L_FINAL_POS + offset;
uint8_t door_right_x = DOOR_R_FINAL_POS - offset;
uint8_t height = icon_get_height(&I_DoorLeft_70x55);
canvas_draw_icon(canvas, door_left_x, canvas_height(canvas) - height, &I_DoorLeft_70x55);
canvas_draw_icon(canvas, door_right_x, canvas_height(canvas) - height, &I_DoorRight_70x55);
}
static bool desktop_view_locked_doors_move(DesktopViewLockedModel* model) {
bool stop = false;
if(model->door_offset < DOOR_OFFSET_END) {
model->door_offset = CLAMP(model->door_offset + 5, DOOR_OFFSET_END, DOOR_OFFSET_START);
stop = true;
}
return stop;
}
static void desktop_view_locked_update_hint_icon_timeout(DesktopViewLocked* locked_view) {
DesktopViewLockedModel* model = view_get_model(locked_view->view);
model->hint_icon_expire_at = osKernelGetTickCount() + osKernelGetTickFreq();
view_commit_model(locked_view->view, true);
}
void desktop_view_locked_update(DesktopViewLocked* locked_view) {
bool stop_timer = false;
DesktopViewLockedModel* model = view_get_model(locked_view->view);
if(model->locked) {
model->doors_closing = desktop_view_locked_doors_move(model);
stop_timer = !model->doors_closing;
} else {
model->unlocked_hint = false;
stop_timer = true;
}
view_commit_model(locked_view->view, true);
if(stop_timer) {
xTimerStop(locked_view->timer, portMAX_DELAY);
}
}
static void desktop_view_locked_draw(Canvas* canvas, void* model) {
DesktopViewLockedModel* m = model;
uint32_t now = osKernelGetTickCount();
canvas_set_color(canvas, ColorBlack);
if(m->locked) {
if(m->doors_closing) {
desktop_view_locked_doors_draw(canvas, m);
canvas_set_font(canvas, FontPrimary);
elements_multiline_text_framed(canvas, 42, 30 + STATUS_BAR_Y_SHIFT, "Locked");
} else if((now < m->hint_icon_expire_at) && !m->pin_locked) {
canvas_set_font(canvas, FontSecondary);
elements_bold_rounded_frame(canvas, 14, 2 + STATUS_BAR_Y_SHIFT, 99, 48);
elements_multiline_text(canvas, 65, 20 + STATUS_BAR_Y_SHIFT, "To unlock\npress:");
canvas_draw_icon(canvas, 65, 36 + STATUS_BAR_Y_SHIFT, &I_Back3_45x8);
canvas_draw_icon(canvas, 16, 7 + STATUS_BAR_Y_SHIFT, &I_WarningDolphin_45x42);
canvas_draw_dot(canvas, 17, 61);
}
} else {
if(m->unlocked_hint) {
canvas_set_font(canvas, FontPrimary);
elements_multiline_text_framed(canvas, 42, 30 + STATUS_BAR_Y_SHIFT, "Unlocked");
}
}
}
View* desktop_view_locked_get_view(DesktopViewLocked* locked_view) {
furi_assert(locked_view);
return locked_view->view;
}
static bool desktop_view_locked_input(InputEvent* event, void* context) {
furi_assert(event);
furi_assert(context);
DesktopViewLocked* locked_view = context;
bool locked = false;
bool locked_with_pin = false;
bool doors_closing = false;
uint32_t press_time = xTaskGetTickCount();
{
DesktopViewLockedModel* model = view_get_model(locked_view->view);
bool changed = false;
locked = model->locked;
locked_with_pin = model->pin_locked;
doors_closing = model->doors_closing;
if(!locked && model->unlocked_hint && event->type == InputTypePress) {
model->unlocked_hint = false;
changed = true;
}
view_commit_model(locked_view->view, changed);
}
if(!locked || doors_closing || (event->type != InputTypeShort)) {
return locked;
}
if(locked_with_pin) {
locked_view->callback(DesktopLockedEventShowPinInput, locked_view->context);
} else {
if(press_time - locked_view->lock_lastpress > UNLOCK_RST_TIMEOUT) {
locked_view->lock_lastpress = press_time;
locked_view->lock_count = 0;
}
desktop_view_locked_update_hint_icon_timeout(locked_view);
if(event->key == InputKeyBack) {
locked_view->lock_lastpress = press_time;
locked_view->lock_count++;
if(locked_view->lock_count == UNLOCK_CNT) {
desktop_view_locked_unlock(locked_view);
locked_view->callback(DesktopLockedEventUnlocked, locked_view->context);
}
} else {
locked_view->lock_count = 0;
}
locked_view->lock_lastpress = press_time;
}
return locked;
}
DesktopViewLocked* desktop_view_locked_alloc() {
DesktopViewLocked* locked_view = malloc(sizeof(DesktopViewLocked));
locked_view->view = view_alloc();
locked_view->timer =
xTimerCreate(NULL, 1000 / 16, pdTRUE, locked_view, locked_view_timer_callback);
locked_view->view = view_alloc();
view_allocate_model(locked_view->view, ViewModelTypeLocking, sizeof(DesktopViewLockedModel));
view_set_context(locked_view->view, locked_view);
view_set_draw_callback(locked_view->view, desktop_view_locked_draw);
view_set_input_callback(locked_view->view, desktop_view_locked_input);
return locked_view;
}
void desktop_view_locked_free(DesktopViewLocked* locked_view) {
furi_assert(locked_view);
osTimerDelete(locked_view->timer);
view_free(locked_view->view);
free(locked_view);
}
void desktop_view_locked_close_doors(DesktopViewLocked* locked_view) {
DesktopViewLockedModel* model = view_get_model(locked_view->view);
model->doors_closing = true;
model->door_offset = DOOR_OFFSET_START;
view_commit_model(locked_view->view, true);
xTimerChangePeriod(locked_view->timer, pdMS_TO_TICKS(DOOR_MOVING_INTERVAL_MS), portMAX_DELAY);
}
void desktop_view_locked_lock(DesktopViewLocked* locked_view, bool pin_locked) {
DesktopViewLockedModel* model = view_get_model(locked_view->view);
model->locked = true;
model->pin_locked = pin_locked;
view_commit_model(locked_view->view, true);
}
void desktop_view_locked_unlock(DesktopViewLocked* locked_view) {
furi_assert(locked_view);
locked_view->lock_count = 0;
DesktopViewLockedModel* model = view_get_model(locked_view->view);
model->locked = false;
model->pin_locked = false;
model->unlocked_hint = true;
view_commit_model(locked_view->view, true);
xTimerChangePeriod(locked_view->timer, pdMS_TO_TICKS(UNLOCKED_HINT_TIMEOUT_MS), portMAX_DELAY);
}