Skip to content

Commit e575513

Browse files
committed
upd counter
1 parent 8f2b917 commit e575513

File tree

2 files changed

+71
-26
lines changed

2 files changed

+71
-26
lines changed

apps_source_code/counter/application.fam

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ App(
1111
fap_icon_assets="icons",
1212
fap_author="@Krulknul",
1313
fap_weburl="https://github.com/Krulknul/dolphin-counter",
14-
fap_version="1.2",
14+
fap_version="1.3",
1515
fap_description="Simple counter",
1616
)

apps_source_code/counter/counter.c

+70-25
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,30 @@
33
#include <input/input.h>
44
#include <stdlib.h>
55
#include <counter_icons.h>
6+
#include <notification/notification.h>
7+
#include <notification/notification_messages.h>
68

7-
#define MAX_COUNT 99
9+
#define MAX_COUNT 99999
10+
#define NUM_WIDTH 12
811
#define BOXTIME 2
9-
#define BOXWIDTH 30
10-
#define MIDDLE_X 64 - BOXWIDTH / 2
11-
#define MIDDLE_Y 32 - BOXWIDTH / 2
12+
#define MIN_BOXWIDTH 30
13+
#define BOXHEIGHT 30
14+
#define MIDDLE_Y 32 - BOXHEIGHT / 2
15+
#define OFFSET_X 8
1216
#define OFFSET_Y 9
17+
#define VIBRO_TIME_MS 20
1318

1419
typedef struct {
1520
FuriMessageQueue* input_queue;
1621
ViewPort* view_port;
1722
Gui* gui;
1823
FuriMutex** mutex;
24+
NotificationApp* notifications;
1925

2026
int count;
2127
bool pressed;
2228
int boxtimer;
29+
bool vibro;
2330
} Counter;
2431

2532
void state_free(Counter* c) {
@@ -33,7 +40,7 @@ void state_free(Counter* c) {
3340

3441
static void input_callback(InputEvent* input_event, void* ctx) {
3542
Counter* c = ctx;
36-
if(input_event->type == InputTypeShort) {
43+
if(input_event->type == InputTypeShort || input_event->type == InputTypeLong) {
3744
furi_message_queue_put(c->input_queue, input_event, 0);
3845
}
3946
}
@@ -47,19 +54,25 @@ static void render_callback(Canvas* canvas, void* ctx) {
4754
canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignCenter, "Counter :)");
4855
canvas_set_font(canvas, FontBigNumbers);
4956

50-
char scount[5];
57+
char scount[8];
58+
snprintf(scount, sizeof(scount), "%d", c->count);
59+
size_t cntr_len = strlen(scount);
60+
size_t boxwidth = cntr_len * NUM_WIDTH + OFFSET_X;
61+
if (boxwidth < MIN_BOXWIDTH) {
62+
boxwidth = MIN_BOXWIDTH;
63+
}
64+
size_t middle_x = 64 - boxwidth / 2;
5165
if(c->pressed == true || c->boxtimer > 0) {
52-
canvas_draw_rframe(canvas, MIDDLE_X, MIDDLE_Y + OFFSET_Y, BOXWIDTH, BOXWIDTH, 5);
66+
canvas_draw_rframe(canvas, middle_x, MIDDLE_Y + OFFSET_Y, boxwidth, BOXHEIGHT, 5);
5367
canvas_draw_rframe(
54-
canvas, MIDDLE_X - 1, MIDDLE_Y + OFFSET_Y - 1, BOXWIDTH + 2, BOXWIDTH + 2, 5);
68+
canvas, middle_x - 1, MIDDLE_Y + OFFSET_Y - 1, boxwidth + 2, BOXHEIGHT + 2, 5);
5569
canvas_draw_rframe(
56-
canvas, MIDDLE_X - 2, MIDDLE_Y + OFFSET_Y - 2, BOXWIDTH + 4, BOXWIDTH + 4, 5);
70+
canvas, middle_x - 2, MIDDLE_Y + OFFSET_Y - 2, boxwidth + 4, BOXHEIGHT + 4, 5);
5771
c->pressed = false;
5872
c->boxtimer--;
5973
} else {
60-
canvas_draw_rframe(canvas, MIDDLE_X, MIDDLE_Y + OFFSET_Y, BOXWIDTH, BOXWIDTH, 5);
74+
canvas_draw_rframe(canvas, middle_x, MIDDLE_Y + OFFSET_Y, boxwidth, BOXHEIGHT, 5);
6175
}
62-
snprintf(scount, sizeof(scount), "%d", c->count);
6376
canvas_draw_str_aligned(canvas, 64, 32 + OFFSET_Y, AlignCenter, AlignCenter, scount);
6477
furi_mutex_release(c->mutex);
6578
}
@@ -70,8 +83,10 @@ Counter* state_init() {
7083
c->view_port = view_port_alloc();
7184
c->gui = furi_record_open(RECORD_GUI);
7285
c->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
86+
c->notifications = furi_record_open(RECORD_NOTIFICATION);
7387
c->count = 0;
7488
c->boxtimer = 0;
89+
c->vibro = false;
7590
view_port_input_callback_set(c->view_port, input_callback, c);
7691
view_port_draw_callback_set(c->view_port, render_callback, c);
7792
gui_add_view_port(c->gui, c->view_port, GuiLayerFullscreen);
@@ -81,25 +96,55 @@ Counter* state_init() {
8196
int32_t counterapp(void) {
8297
Counter* c = state_init();
8398

84-
while(1) {
85-
InputEvent input;
99+
InputEvent input;
100+
for(bool processing = true; processing;) {
86101
while(furi_message_queue_get(c->input_queue, &input, FuriWaitForever) == FuriStatusOk) {
87102
furi_check(furi_mutex_acquire(c->mutex, FuriWaitForever) == FuriStatusOk);
88103

89-
if(input.key == InputKeyBack) {
90-
furi_mutex_release(c->mutex);
91-
state_free(c);
92-
return 0;
93-
} else if((input.key == InputKeyUp || input.key == InputKeyOk) && c->count < MAX_COUNT) {
94-
c->pressed = true;
95-
c->boxtimer = BOXTIME;
96-
c->count++;
97-
} else if(input.key == InputKeyDown && c->count != 0) {
98-
c->pressed = true;
99-
c->boxtimer = BOXTIME;
100-
c->count--;
104+
if(input.type == InputTypeShort) {
105+
switch(input.key) {
106+
case InputKeyBack:
107+
processing = false;
108+
break;
109+
case InputKeyUp:
110+
case InputKeyOk:
111+
if (c->count < MAX_COUNT) {
112+
c->pressed = true;
113+
c->boxtimer = BOXTIME;
114+
c->count++;
115+
if (c->vibro) {
116+
notification_message(c->notifications, &sequence_set_vibro_on);
117+
furi_delay_ms(VIBRO_TIME_MS);
118+
notification_message(c->notifications, &sequence_reset_vibro);
119+
}
120+
}
121+
break;
122+
case InputKeyDown:
123+
if (c->count > 0) {
124+
c->pressed = true;
125+
c->boxtimer = BOXTIME;
126+
c->count--;
127+
}
128+
break;
129+
default:
130+
break;
131+
}
132+
} else if(input.type == InputTypeLong) {
133+
switch(input.key) {
134+
case InputKeyBack:
135+
c->count = 0;
136+
break;
137+
case InputKeyOk:
138+
c->vibro = !c->vibro;
139+
break;
140+
default:
141+
break;
142+
}
101143
}
102144
furi_mutex_release(c->mutex);
145+
if(!processing) {
146+
break;
147+
}
103148
view_port_update(c->view_port);
104149
}
105150
}

0 commit comments

Comments
 (0)