-
Notifications
You must be signed in to change notification settings - Fork 1
/
knit_counter.c
148 lines (126 loc) · 4.59 KB
/
knit_counter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <furi.h>
#include <gui/gui.h>
#include <input/input.h>
#include <stdlib.h>
#include <storage/storage.h>
#include <flipper_format/flipper_format_i.h>
/* generated by fbt from .png files in images folder */
#include <knit_counter_icons.h>
#define MAX_COUNT 99
#define BOXTIME 2
#define BOXWIDTH 30
#define MIDDLE_X 64 - BOXWIDTH / 2
#define MIDDLE_Y 32 - BOXWIDTH / 2
#define OFFSET_Y 9
typedef struct {
FuriMessageQueue* input_queue;
ViewPort* view_port;
Gui* gui;
FuriMutex* mutex;
int count;
bool pressed;
int boxtimer;
} Counter;
void state_free(Counter* c) {
gui_remove_view_port(c->gui, c->view_port);
furi_record_close(RECORD_GUI);
view_port_free(c->view_port);
furi_message_queue_free(c->input_queue);
furi_mutex_free(c->mutex);
free(c);
}
static void input_callback(InputEvent* input_event, void* ctx) {
Counter* c = ctx;
if(input_event->type == InputTypeShort) {
furi_message_queue_put(c->input_queue, input_event, 0);
}
}
static void render_callback(Canvas* canvas, void* ctx) {
Counter* c = ctx;
furi_check(furi_mutex_acquire(c->mutex, FuriWaitForever) == FuriStatusOk);
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignCenter, "Counter");
canvas_set_font(canvas, FontBigNumbers);
char scount[5];
if(c->pressed == true || c->boxtimer > 0) {
canvas_draw_rframe(canvas, MIDDLE_X, MIDDLE_Y + OFFSET_Y, BOXWIDTH, BOXWIDTH, 5);
canvas_draw_rframe(
canvas, MIDDLE_X - 1, MIDDLE_Y + OFFSET_Y - 1, BOXWIDTH + 2, BOXWIDTH + 2, 5);
canvas_draw_rframe(
canvas, MIDDLE_X - 2, MIDDLE_Y + OFFSET_Y - 2, BOXWIDTH + 4, BOXWIDTH + 4, 5);
c->pressed = false;
c->boxtimer--;
} else {
canvas_draw_rframe(canvas, MIDDLE_X, MIDDLE_Y + OFFSET_Y, BOXWIDTH, BOXWIDTH, 5);
}
snprintf(scount, sizeof(scount), "%d", c->count);
canvas_draw_str_aligned(canvas, 64, 32 + OFFSET_Y, AlignCenter, AlignCenter, scount);
furi_mutex_release(c->mutex);
}
Counter* state_init() {
Counter* c = malloc(sizeof(Counter));
c->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
c->view_port = view_port_alloc();
c->gui = furi_record_open(RECORD_GUI);
c->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
c->count = 0;
c->boxtimer = 0;
view_port_input_callback_set(c->view_port, input_callback, c);
view_port_draw_callback_set(c->view_port, render_callback, c);
gui_add_view_port(c->gui, c->view_port, GuiLayerFullscreen);
return c;
}
void save_count(uint32_t count) {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* file = flipper_format_file_alloc(storage);
do {
if(storage_file_exists(storage, APP_DATA_PATH("counter_storage.txt"))) {
storage_simply_remove(storage, APP_DATA_PATH("counter_storage.txt"));
}
if(!flipper_format_file_open_new(file, APP_DATA_PATH("counter_storage.txt"))) break;
if(!flipper_format_write_uint32(file, "UINT", &count, 1)) break;
} while(0);
flipper_format_free(file);
}
uint32_t read_count() {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* file = flipper_format_file_alloc(storage);
uint32_t uint32_value = 0;
do {
if(!flipper_format_file_open_existing(file, APP_DATA_PATH("counter_storage.txt"))) break;
if(!flipper_format_read_uint32(file, "UINT", &uint32_value, 1)) break;
} while(0);
flipper_format_free(file);
return uint32_value;
}
int32_t knit_counter_app(void) {
Counter* c = state_init();
c->count = read_count();
while(1) {
InputEvent input;
while(furi_message_queue_get(c->input_queue, &input, FuriWaitForever) == FuriStatusOk) {
furi_check(furi_mutex_acquire(c->mutex, FuriWaitForever) == FuriStatusOk);
if(input.key == InputKeyBack) {
save_count(c->count);
furi_mutex_release(c->mutex);
state_free(c);
return 0;
} else if(input.key == InputKeyUp && c->count < MAX_COUNT) {
c->pressed = true;
c->boxtimer = BOXTIME;
c->count++;
} else if(input.key == InputKeyDown && c->count != 0) {
c->pressed = true;
c->boxtimer = BOXTIME;
c->count--;
}
furi_mutex_release(c->mutex);
view_port_update(c->view_port);
}
}
save_count(c->count);
state_free(c);
return 0;
}