FL-528 GUI: View, ViewDispather. Dolphin: first start. (#276)

* GUI: view. Flooper-blooper fix compilation error.
* GUI: view and viewdispatcher bones
* GUI: view implementation, view models, view dispatcher
* GUI: view navigation, model refinement. Power: use view, view dispatcher.
* HAL Flash: proper page write. Dolphin: views. Power: views
* Dolphin: transition idle scree to Views
* Dolphin: input events on stats view. Format sources.
* HAL: flash erase. Dolphin: permanent state storage.
* Dolphin: first start welcome. HAL: flash operation status, errata 2.2.9 crutch.
This commit is contained in:
あく
2021-01-08 07:42:48 +03:00
committed by GitHub
parent d65e9b04ce
commit 1b78418f9f
33 changed files with 995 additions and 212 deletions

View File

@@ -1,100 +1,126 @@
#include "dolphin_i.h"
void dolphin_draw_callback(Canvas* canvas, void* context) {
bool dolphin_view_first_start_input(InputEvent* event, void* context) {
furi_assert(event);
furi_assert(context);
Dolphin* dolphin = context;
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
if(dolphin->screen == DolphinScreenIdle) {
dolphin_draw_idle(canvas, dolphin);
} else if(dolphin->screen == DolphinScreenDebug) {
dolphin_draw_debug(canvas, dolphin);
} else if(dolphin->screen == DolphinScreenStats) {
dolphin_draw_stats(canvas, dolphin);
if(event->state) {
if(event->input == InputRight) {
uint32_t page;
with_view_model(
dolphin->idle_view_first_start,
(DolphinViewFirstStartModel * model) { page = ++model->page; });
if(page > 8) {
dolphin_save(dolphin);
view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
}
}
}
// All events consumed
return true;
}
void dolphin_draw_idle(Canvas* canvas, Dolphin* dolphin) {
canvas_draw_icon(canvas, 128 - 80, 0, dolphin->icon);
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 2, 10, "/\\: Stats");
canvas_draw_str(canvas, 5, 32, "OK: Menu");
canvas_draw_str(canvas, 2, 52, "\\/: Version");
}
void dolphin_draw_debug(Canvas* canvas, Dolphin* dolphin) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 2, 10, "Version info:");
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 5, 22, TARGET " " BUILD_DATE);
canvas_draw_str(canvas, 5, 32, GIT_BRANCH);
canvas_draw_str(canvas, 5, 42, GIT_BRANCH_NUM);
canvas_draw_str(canvas, 5, 52, GIT_COMMIT);
}
void dolphin_draw_stats(Canvas* canvas, Dolphin* dolphin) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 2, 10, "Dolphin stats:");
char buffer[64];
canvas_set_font(canvas, FontSecondary);
snprintf(buffer, 64, "Icounter: %ld", dolphin_state_get_icounter(dolphin->state));
canvas_draw_str(canvas, 5, 22, buffer);
snprintf(buffer, 64, "Butthurt: %ld", dolphin_state_get_butthurt(dolphin->state));
canvas_draw_str(canvas, 5, 32, buffer);
canvas_draw_str(canvas, 5, 40, "< > change icounter");
}
void dolphin_input_callback(InputEvent* event, void* context) {
bool dolphin_view_idle_main_input(InputEvent* event, void* context) {
furi_assert(event);
furi_assert(context);
Dolphin* dolphin = context;
if(!event->state) return;
if(event->state) {
if(event->input == InputOk) {
with_value_mutex(
dolphin->menu_vm, (Menu * menu) { menu_ok(menu); });
} else if(event->input == InputUp) {
view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleStats);
} else if(event->input == InputDown) {
view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleDebug);
}
}
// All events consumed
return true;
}
if(event->input == InputOk) {
with_value_mutex(
dolphin->menu_vm, (Menu * menu) { menu_ok(menu); });
} else if(event->input == InputUp) {
if(dolphin->screen != DolphinScreenStats) {
dolphin->screen++;
}
} else if(event->input == InputDown) {
if(dolphin->screen != DolphinScreenDebug) {
dolphin->screen--;
}
} else if(event->input == InputBack) {
dolphin->screen = DolphinScreenIdle;
} else if(event->input == InputLeft) {
dolphin_deed(dolphin, DolphinDeedIButtonEmulate);
} else if(event->input == InputRight) {
bool dolphin_view_idle_stats_input(InputEvent* event, void* context) {
furi_assert(event);
furi_assert(context);
Dolphin* dolphin = context;
if(!event->state) return false;
if(event->input == InputLeft) {
dolphin_deed(dolphin, DolphinDeedWrong);
} else if(event->input == InputRight) {
dolphin_deed(dolphin, DolphinDeedIButtonRead);
} else if(event->input == InputOk) {
dolphin_save(dolphin);
} else {
return false;
}
widget_update(dolphin->widget);
return true;
}
Dolphin* dolphin_alloc() {
Dolphin* dolphin = furi_alloc(sizeof(Dolphin));
dolphin->icon = assets_icons_get(I_Flipper_young_80x60);
icon_start_animation(dolphin->icon);
dolphin->widget = widget_alloc();
widget_draw_callback_set(dolphin->widget, dolphin_draw_callback, dolphin);
widget_input_callback_set(dolphin->widget, dolphin_input_callback, dolphin);
dolphin->menu_vm = furi_open("menu");
furi_check(dolphin->menu_vm);
dolphin->state = dolphin_state_alloc();
dolphin->screen = DolphinScreenIdle;
// Message queue
dolphin->event_queue = osMessageQueueNew(8, sizeof(DolphinEvent), NULL);
furi_check(dolphin->event_queue);
// State
dolphin->state = dolphin_state_alloc();
// Menu
dolphin->menu_vm = furi_open("menu");
furi_check(dolphin->menu_vm);
// GUI
dolphin->idle_view_dispatcher = view_dispatcher_alloc();
// First start View
dolphin->idle_view_first_start = view_alloc();
view_allocate_model(
dolphin->idle_view_first_start, ViewModelTypeLockFree, sizeof(DolphinViewFirstStartModel));
view_set_context(dolphin->idle_view_first_start, dolphin);
view_set_draw_callback(dolphin->idle_view_first_start, dolphin_view_first_start_draw);
view_set_input_callback(dolphin->idle_view_first_start, dolphin_view_first_start_input);
view_dispatcher_add_view(
dolphin->idle_view_dispatcher, DolphinViewFirstStart, dolphin->idle_view_first_start);
// Main Idle View
dolphin->idle_view_main = view_alloc();
view_set_context(dolphin->idle_view_main, dolphin);
view_set_draw_callback(dolphin->idle_view_main, dolphin_view_idle_main_draw);
view_set_input_callback(dolphin->idle_view_main, dolphin_view_idle_main_input);
view_dispatcher_add_view(
dolphin->idle_view_dispatcher, DolphinViewIdleMain, dolphin->idle_view_main);
// Stats Idle View
dolphin->idle_view_stats = view_alloc();
view_set_context(dolphin->idle_view_stats, dolphin);
view_allocate_model(
dolphin->idle_view_stats, ViewModelTypeLockFree, sizeof(DolphinViewIdleStatsModel));
with_view_model(
dolphin->idle_view_stats, (DolphinViewIdleStatsModel * model) {
model->icounter = dolphin_state_get_icounter(dolphin->state);
model->butthurt = dolphin_state_get_butthurt(dolphin->state);
});
view_set_draw_callback(dolphin->idle_view_stats, dolphin_view_idle_stats_draw);
view_set_input_callback(dolphin->idle_view_stats, dolphin_view_idle_stats_input);
view_set_previous_callback(dolphin->idle_view_stats, dolphin_view_idle_back);
view_dispatcher_add_view(
dolphin->idle_view_dispatcher, DolphinViewIdleStats, dolphin->idle_view_stats);
// Debug Idle View
dolphin->idle_view_debug = view_alloc();
view_set_draw_callback(dolphin->idle_view_debug, dolphin_view_idle_debug_draw);
view_set_previous_callback(dolphin->idle_view_debug, dolphin_view_idle_back);
view_dispatcher_add_view(
dolphin->idle_view_dispatcher, DolphinViewIdleDebug, dolphin->idle_view_debug);
return dolphin;
}
void dolphin_save(Dolphin* dolphin) {
furi_assert(dolphin);
DolphinEvent event;
event.type = DolphinEventTypeSave;
furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
}
void dolphin_deed(Dolphin* dolphin, DolphinDeed deed) {
furi_assert(dolphin);
DolphinEvent event;
event.type = DolphinEventTypeDeed;
event.deed = deed;
@@ -105,7 +131,12 @@ void dolphin_task() {
Dolphin* dolphin = dolphin_alloc();
Gui* gui = furi_open("gui");
gui_add_widget(gui, dolphin->widget, GuiLayerNone);
view_dispatcher_attach_to_gui(dolphin->idle_view_dispatcher, gui, ViewDispatcherTypeWindow);
if(dolphin_state_load(dolphin->state)) {
view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
} else {
view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewFirstStart);
}
if(!furi_create("dolphin", dolphin)) {
printf("[dolphin_task] cannot create the dolphin record\n");
@@ -119,6 +150,13 @@ void dolphin_task() {
furi_check(osMessageQueueGet(dolphin->event_queue, &event, NULL, osWaitForever) == osOK);
if(event.type == DolphinEventTypeDeed) {
dolphin_state_on_deed(dolphin->state, event.deed);
with_view_model(
dolphin->idle_view_stats, (DolphinViewIdleStatsModel * model) {
model->icounter = dolphin_state_get_icounter(dolphin->state);
model->butthurt = dolphin_state_get_butthurt(dolphin->state);
});
} else if(event.type == DolphinEventTypeSave) {
dolphin_state_save(dolphin->state);
}
}
}

View File

@@ -4,8 +4,8 @@
typedef struct Dolphin Dolphin;
/*
* Deed complete notification. Call it on deed completion.
/* Deed complete notification. Call it on deed completion.
* See dolphin_deed.h for available deeds. In futures it will become part of assets.
* Thread safe
*/
void dolphin_deed(Dolphin* dolphin, DolphinDeed deed);

View File

@@ -2,20 +2,21 @@
#include "dolphin.h"
#include "dolphin_state.h"
#include "dolphin_views.h"
#include <flipper_v2.h>
#include <gui/gui.h>
#include <gui/widget.h>
#include <gui/view_dispatcher.h>
#include <gui/canvas.h>
#include <menu/menu.h>
#include <assets_icons.h>
#include <stdint.h>
typedef enum {
DolphinEventTypeDeed,
DolphinEventTypeSave,
} DolphinEventType;
typedef struct {
@@ -25,27 +26,24 @@ typedef struct {
};
} DolphinEvent;
typedef enum {
DolphinScreenDebug,
DolphinScreenIdle,
DolphinScreenStats,
} DolphinScreen;
struct Dolphin {
Icon* icon;
Widget* widget;
ValueMutex* menu_vm;
// State
DolphinState* state;
DolphinScreen screen;
// Internal message queue
osMessageQueueId_t event_queue;
// State
DolphinState* state;
// Menu
ValueMutex* menu_vm;
// GUI
ViewDispatcher* idle_view_dispatcher;
View* idle_view_first_start;
View* idle_view_main;
View* idle_view_stats;
View* idle_view_debug;
};
void dolphin_draw_callback(Canvas* canvas, void* context);
void dolphin_draw_idle(Canvas* canvas, Dolphin* dolphin);
void dolphin_draw_debug(Canvas* canvas, Dolphin* dolphin);
void dolphin_draw_stats(Canvas* canvas, Dolphin* dolphin);
void dolphin_input_callback(InputEvent* event, void* context);
Dolphin* dolphin_alloc();
/* Save Dolphin state (write to permanent memory)
* Thread safe
*/
void dolphin_save(Dolphin* dolphin);

View File

@@ -1,18 +1,35 @@
#include "dolphin_state.h"
#include <api-hal-flash.h>
#include <flipper_v2.h>
typedef struct {
uint32_t ibutton;
uint32_t nfc;
uint32_t ir;
uint32_t rfid;
} DolphinLimit;
uint8_t magic;
uint8_t version;
uint8_t checksum;
uint8_t flags;
uint32_t timestamp;
} DolphinDataHeader;
struct DolphinState {
#define DOLPHIN_DATA_PAGE 0xC0
#define DOLPHIN_DATA_HEADER_ADDRESS 0x080C0000U
#define DOLPHIN_DATA_DATA_ADDRESS (DOLPHIN_DATA_HEADER_ADDRESS + sizeof(DolphinDataHeader))
#define DOLPHIN_DATA_HEADER_MAGIC 0xD0
#define DOLPHIN_DATA_HEADER_VERSION 0x00
typedef struct {
uint32_t limit_ibutton;
uint32_t limit_nfc;
uint32_t limit_ir;
uint32_t limit_rfid;
uint32_t flags;
uint32_t icounter;
uint32_t butthurt;
} DolphinData;
DolphinLimit limit;
struct DolphinState {
DolphinData data;
};
DolphinState* dolphin_state_alloc() {
@@ -24,29 +41,81 @@ void dolphin_state_release(DolphinState* dolphin_state) {
free(dolphin_state);
}
void dolphin_state_save(DolphinState* dolphin_state) {
bool dolphin_state_save(DolphinState* dolphin_state) {
if(!api_hal_flash_erase(DOLPHIN_DATA_PAGE, 1)) {
return false;
}
uint8_t* source = (uint8_t*)&dolphin_state->data;
uint8_t checksum = 0;
for(size_t i = 0; i < sizeof(DolphinData); i++) {
checksum += source[i];
}
DolphinDataHeader header;
header.magic = DOLPHIN_DATA_HEADER_MAGIC;
header.version = DOLPHIN_DATA_HEADER_VERSION;
header.checksum = checksum;
header.flags = 0;
header.timestamp = 0;
if(!api_hal_flash_write_dword(DOLPHIN_DATA_HEADER_ADDRESS, *(uint64_t*)&header)) {
return false;
}
uint8_t destination[sizeof(uint64_t)];
size_t block_count = sizeof(DolphinData) / sizeof(uint64_t) + 1;
size_t offset = 0;
for(size_t i = 0; i < block_count; i++) {
for(size_t n = 0; n < sizeof(uint64_t); n++) {
if(offset < sizeof(DolphinData)) {
destination[n] = source[offset];
} else {
destination[n] = 0;
}
offset++;
}
if(!api_hal_flash_write_dword(
DOLPHIN_DATA_DATA_ADDRESS + i * sizeof(uint64_t), *(uint64_t*)destination)) {
return false;
}
}
return true;
}
void dolphin_state_load(DolphinState* dolphin_state) {
bool dolphin_state_load(DolphinState* dolphin_state) {
const DolphinDataHeader* header = (const DolphinDataHeader*)DOLPHIN_DATA_HEADER_ADDRESS;
if(header->magic == DOLPHIN_DATA_HEADER_MAGIC &&
header->version == DOLPHIN_DATA_HEADER_VERSION) {
uint8_t checksum = 0;
const uint8_t* source = (const uint8_t*)DOLPHIN_DATA_DATA_ADDRESS;
for(size_t i = 0; i < sizeof(DolphinData); i++) {
checksum += source[i];
}
if(header->checksum == checksum) {
memcpy(
&dolphin_state->data, (const void*)DOLPHIN_DATA_DATA_ADDRESS, sizeof(DolphinData));
return true;
}
}
return false;
}
void dolphin_state_clear(DolphinState* dolphin_state) {
memset(dolphin_state, 0, sizeof(DolphinState));
memset(&dolphin_state->data, 0, sizeof(DolphinData));
}
void dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
const DolphinDeedWeight* deed_weight = dolphin_deed_weight(deed);
int32_t icounter = dolphin_state->icounter + deed_weight->icounter;
int32_t icounter = dolphin_state->data.icounter + deed_weight->icounter;
if(icounter >= 0) {
dolphin_state->icounter = icounter;
dolphin_state->data.icounter = icounter;
}
}
uint32_t dolphin_state_get_icounter(DolphinState* dolphin_state) {
return dolphin_state->icounter;
return dolphin_state->data.icounter;
}
uint32_t dolphin_state_get_butthurt(DolphinState* dolphin_state) {
return dolphin_state->butthurt;
return dolphin_state->data.butthurt;
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include "dolphin_deed.h"
#include <stdbool.h>
#include <stdint.h>
typedef struct DolphinState DolphinState;
@@ -9,9 +10,9 @@ DolphinState* dolphin_state_alloc();
void dolphin_state_release(DolphinState* dolphin_state);
void dolphin_state_save(DolphinState* dolphin_state);
bool dolphin_state_save(DolphinState* dolphin_state);
void dolphin_state_load(DolphinState* dolphin_state);
bool dolphin_state_load(DolphinState* dolphin_state);
void dolphin_state_clear(DolphinState* dolphin_state);

View File

@@ -0,0 +1,69 @@
#include "dolphin_views.h"
#include <gui/view.h>
void dolphin_view_first_start_draw(Canvas* canvas, void* model) {
DolphinViewFirstStartModel* m = model;
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
if(m->page == 0) {
canvas_draw_icon_name(canvas, 0, 1, I_DolphinFirstStart0_128x54);
} else if(m->page == 1) {
canvas_draw_icon_name(canvas, 0, 1, I_DolphinFirstStart1_128x54);
} else if(m->page == 2) {
canvas_draw_icon_name(canvas, 0, 1, I_DolphinFirstStart2_128x54);
} else if(m->page == 3) {
canvas_draw_icon_name(canvas, 0, 1, I_DolphinFirstStart3_128x54);
} else if(m->page == 4) {
canvas_draw_icon_name(canvas, 0, 1, I_DolphinFirstStart4_128x54);
} else if(m->page == 5) {
canvas_draw_icon_name(canvas, 0, 1, I_DolphinFirstStart5_128x54);
} else if(m->page == 6) {
canvas_draw_icon_name(canvas, 0, 1, I_DolphinFirstStart6_128x54);
} else if(m->page == 7) {
canvas_draw_icon_name(canvas, 0, 1, I_DolphinFirstStart7_128x54);
} else if(m->page == 8) {
canvas_draw_icon_name(canvas, 0, 1, I_DolphinFirstStart8_128x54);
}
}
void dolphin_view_idle_main_draw(Canvas* canvas, void* model) {
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
canvas_draw_icon_name(canvas, 128 - 80, 0, I_Flipper_young_80x60);
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 2, 10, "/\\: Stats");
canvas_draw_str(canvas, 5, 32, "OK: Menu");
canvas_draw_str(canvas, 2, 52, "\\/: Version");
}
void dolphin_view_idle_stats_draw(Canvas* canvas, void* model) {
DolphinViewIdleStatsModel* m = model;
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 2, 10, "Dolphin stats:");
char buffer[64];
canvas_set_font(canvas, FontSecondary);
snprintf(buffer, 64, "Icounter: %ld", m->icounter);
canvas_draw_str(canvas, 5, 22, buffer);
snprintf(buffer, 64, "Butthurt: %ld", m->butthurt);
canvas_draw_str(canvas, 5, 32, buffer);
canvas_draw_str(canvas, 5, 40, "< > change icounter");
}
void dolphin_view_idle_debug_draw(Canvas* canvas, void* model) {
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 2, 10, "Version info:");
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 5, 22, TARGET " " BUILD_DATE);
canvas_draw_str(canvas, 5, 32, GIT_BRANCH);
canvas_draw_str(canvas, 5, 42, GIT_BRANCH_NUM);
canvas_draw_str(canvas, 5, 52, GIT_COMMIT);
}
uint32_t dolphin_view_idle_back(void* context) {
return DolphinViewIdleMain;
}

View File

@@ -0,0 +1,33 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <gui/canvas.h>
#include <flipper_v2.h>
// Idle scree
typedef enum {
DolphinViewFirstStart,
DolphinViewIdleMain,
DolphinViewIdleStats,
DolphinViewIdleDebug,
} DolphinViewIdle;
typedef struct {
uint32_t page;
} DolphinViewFirstStartModel;
void dolphin_view_first_start_draw(Canvas* canvas, void* model);
bool dolphin_view_first_start_input(InputEvent* event, void* context);
typedef struct {
uint32_t icounter;
uint32_t butthurt;
} DolphinViewIdleStatsModel;
void dolphin_view_idle_main_draw(Canvas* canvas, void* model);
bool dolphin_view_idle_main_input(InputEvent* event, void* context);
void dolphin_view_idle_stats_draw(Canvas* canvas, void* model);
bool dolphin_view_idle_stats_input(InputEvent* event, void* context);
void dolphin_view_idle_debug_draw(Canvas* canvas, void* model);
uint32_t dolphin_view_idle_back(void* context);