-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknob.c
146 lines (126 loc) · 4.22 KB
/
knob.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
#include <furi.h>
#include <gui/view_dispatcher.h>
#include "fzshock.h"
#include "knob.h"
// Set a callback to invoke when knob has an event.
// @knob is a pointer to our Knob instance.
// @callback is a function to invoke when we have custom events.
void knob_set_callback(Knob* knob, KnobCallback callback, void* callback_context) {
with_view_model(
knob->view,
KnobModel * model,
{
model->callback_context = callback_context;
model->callback = callback;
},
true);
}
// Invoked when input (button press) is detected.
// @input_even is the event the occured.
// @ctx is a pointer to our Knob instance.
static bool knob_input_callback(InputEvent* input_event, void* ctx) {
message("knob_input_callback");
Knob* knob = (Knob*)ctx;
bool handled = false;
if(input_event->type == InputTypePress && input_event->key == InputKeyUp) {
with_view_model(
knob->view,
KnobModel * model,
{ model->strength++;
if (model->strength > 99)
model->strength = 0;
},
true);
handled = true;
} else if(input_event->type == InputTypePress && input_event->key == InputKeyDown) {
with_view_model(
knob->view,
KnobModel * model,
{ model->strength--;
if (model->strength < 0)
model->strength = 99;
},
true); // Render new data.
handled = true;
} else if(input_event->type == InputTypePress && input_event->key == InputKeyOk) {
with_view_model(
knob->view,
KnobModel * model,
{
if(model->callback) {
message("invoking callback");
model->callback(model->callback_context, KnobEventDone);
} else {
message("no callback set; use knob_set_callback first.");
}
},
false); // No new data.
handled = true;
}
return handled;
}
// Invoked by the draw callback to render the knob.
// @canvas is the canvas to draw on.
// @ctx is our model.
static void knob_render_callback(Canvas* canvas, void* ctx) {
message("knob_render_callback");
KnobModel* model = ctx;
furi_string_printf(model->buffer, "%02d", model->strength);
canvas_set_font(canvas, FontBigNumbers);
canvas_draw_str_aligned(
canvas, 128 / 2, 64 / 2, AlignCenter, AlignCenter, furi_string_get_cstr(model->buffer));
}
// Allocates a Knob instance.
Knob* knob_alloc() {
message("knob_alloc");
Knob* knob = malloc(sizeof(Knob));
knob->view = view_alloc();
// context passed to input_callback.
view_set_context(knob->view, knob);
// context passed to render.
view_allocate_model(knob->view, ViewModelTypeLockFree, sizeof(KnobModel));
with_view_model(
knob->view,
KnobModel * model,
{
model->buffer = furi_string_alloc();
model->strength = 0;
},
true);
view_set_draw_callback(knob->view, knob_render_callback);
view_set_input_callback(knob->view, knob_input_callback);
return knob;
}
// Free a Knob instance.
// @knob pointer to a Knob instance.
void knob_free(Knob* knob) {
message("knob_free");
furi_assert(knob);
with_view_model(
knob->view, KnobModel * model, { furi_string_free(model->buffer); }, true);
view_free(knob->view);
free(knob);
}
// Gets the view associated with our Knob.
// @knob pointer to a Knob instance.
View* knob_get_view(Knob* knob) {
message("knob_get_view");
furi_assert(knob);
return knob->view;
}
// Gets the current counter value for a given Knob instance.
// @knob pointer to a Knob instance.
uint32_t knob_get_counter(Knob* knob) {
message("knob_get_counter");
furi_assert(knob);
uint32_t value = 0;
with_view_model(
knob->view, KnobModel * model, { value = model->strength; }, false);
return value;
}
// Set the counter value for a given Knob instance.
// @knob pointer to a Knob instance.
void knob_set_counter(Knob* knob, uint32_t count) {
with_view_model(
knob->view, KnobModel * model, { model->strength = count; }, true);
}