-
Notifications
You must be signed in to change notification settings - Fork 0
/
stepcounter.c
166 lines (139 loc) · 5.18 KB
/
stepcounter.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <furi.h>
#include <furi_hal.h>
#include <furi_hal_gpio.h>
#include <furi_hal_resources.h>
#include <gui/gui.h>
#include <input/input.h>
#include <locale/locale.h>
#include <notification/notification.h>
#include <notification/notification_messages.h>
#include <expansion/expansion.h>
#define TAG_MEMSIC "memsic_2125_app"
#define TAG_COUNTER "step_counter_app"
typedef struct {
const GpioPin* pin;
bool prevState;
uint32_t stepCount;
bool counting;
uint32_t time_of_last_high_pulse;
uint32_t time_of_high_to_high;
} StepCounterData;
typedef enum {
StepCounterEventTypeStep,
StepCounterEventTypeKey,
} StepCounterEventType;
typedef struct {
StepCounterEventType type;
InputEvent input_event;
} StepCounterEvent;
typedef struct {
FuriMessageQueue* queue;
StepCounterData* data;
ViewPort* view_port;
} StepCounterContext;
const GpioPin* const gpio_accelerometer = &gpio_ext_pc0;
void step_callback(void* ctx) {
StepCounterContext* context = (StepCounterContext*)ctx;
StepCounterData* stepData = context->data;
FuriHalCortexTimer timer = furi_hal_cortex_timer_get(0);
uint32_t now = timer.start;
if (furi_hal_gpio_read(stepData->pin)) {
if (stepData->time_of_last_high_pulse != 0) {
stepData->time_of_high_to_high = now - stepData->time_of_last_high_pulse;
}
stepData->time_of_last_high_pulse = now;
} else {
uint32_t high_duration = now - stepData->time_of_last_high_pulse;
bool current_state = high_duration < (stepData->time_of_high_to_high >> 1);
if(current_state != stepData->prevState) {
stepData->prevState = current_state;
stepData->stepCount++;
StepCounterEvent event = {.type = StepCounterEventTypeStep};
furi_message_queue_put(context->queue, &event, 0);
}
}
}
static void input_callback(InputEvent* input_event, void* ctx) {
furi_assert(ctx);
FuriMessageQueue* queue = ctx;
StepCounterEvent event = {
.type = StepCounterEventTypeKey,
.input_event.key = input_event->key,
.input_event.type = input_event->type,
};
furi_message_queue_put(queue, &event, FuriWaitForever);
}
static void render_callback(Canvas* canvas, void* ctx) {
StepCounterContext* stepContext = (StepCounterContext*)ctx;
StepCounterData* stepData = stepContext->data;
char stepText[20];
snprintf(stepText, sizeof(stepText), "Steps: %ld", stepData->stepCount);
canvas_draw_str_aligned(canvas, 1, 1, AlignLeft, AlignTop, stepText);
char buttonText[10];
snprintf(buttonText, sizeof(buttonText), stepData->counting ? "STOP" : "START");
canvas_draw_str_aligned(
canvas,
canvas_width(canvas) / 2,
canvas_height(canvas) - 1,
AlignCenter,
AlignBottom,
buttonText);
}
int32_t step_counter_app(void* p) {
UNUSED(p);
Expansion* expansion = furi_record_open(RECORD_EXPANSION);
expansion_disable(expansion);
StepCounterContext* stepContext = malloc(sizeof(StepCounterContext));
stepContext->data = malloc(sizeof(StepCounterData));
stepContext->data->pin = gpio_accelerometer;
stepContext->data->prevState = furi_hal_gpio_read(stepContext->data->pin);
stepContext->data->stepCount = 0;
stepContext->data->counting = false;
stepContext->queue = furi_message_queue_alloc(8, sizeof(StepCounterEvent));
furi_hal_gpio_init(
stepContext->data->pin, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedVeryHigh);
furi_hal_gpio_add_int_callback(stepContext->data->pin, step_callback, stepContext);
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, render_callback, stepContext);
view_port_input_callback_set(view_port, input_callback, stepContext->queue);
Gui* gui = furi_record_open(RECORD_GUI);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
StepCounterEvent event;
bool processing = true;
do {
if(furi_message_queue_get(stepContext->queue, &event, FuriWaitForever) == FuriStatusOk) {
switch(event.type) {
case StepCounterEventTypeKey:
if(event.input_event.type == InputTypeShort &&
event.input_event.key == InputKeyBack) {
processing = false;
} else if(
event.input_event.type == InputTypeShort &&
event.input_event.key == InputKeyOk) {
stepContext->data->counting = !stepContext->data->counting;
view_port_update(view_port);
}
break;
case StepCounterEventTypeStep:
view_port_update(view_port);
break;
default:
break;
}
view_port_update(view_port);
} else {
processing = false;
}
} while(processing);
furi_hal_gpio_remove_int_callback(stepContext->data->pin);
view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port);
view_port_free(view_port);
furi_record_close(RECORD_GUI);
furi_message_queue_free(stepContext->queue);
free(stepContext->data);
free(stepContext);
expansion_enable(expansion);
furi_record_close(RECORD_EXPANSION);
return 0;
}