-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_handling.c
153 lines (145 loc) · 5.37 KB
/
input_handling.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
#include "input_handling.h"
#include "calculator_state.h"
#include "calculator.h"
#include <furi.h>
#include <furi_hal.h>
#include <gui/gui.h>
#include <input/input.h>
#include <notification/notification.h>
#include <notification/notification_messages.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void calculator_input_callback(InputEvent* input_event, void* ctx) {
furi_assert(ctx);
FuriMessageQueue* event_queue = ctx;
furi_message_queue_put(event_queue, input_event, FuriWaitForever);
}
void handle_short_press(Calculator* calculator_state, ViewPort* view_port, InputEvent* event) {
switch(event->key) {
case InputKeyUp:
if(calculator_state->position.y > 0) {
if(calculator_state->position.y == 1) {
calculator_state->position.x = 0;
}
calculator_state->position.y--;
// if(calculator_state->position.y == 1) { // If cursor moves to row 2, skip it
// calculator_state->position.y--;
// }
}
break;
case InputKeyDown:
if(calculator_state->position.y < 5 - 1) {
if(calculator_state->position.y == 5 - 2 &&
(calculator_state->position.x == 3 || calculator_state->position.x == 4)) {
calculator_state->position.y = 5 - 1;
calculator_state->position.x = 3;
} else {
calculator_state->position.y++;
}
// if(calculator_state->position.y == 1) { // If cursor moves to row 2, skip it
// calculator_state->position.y++;
// }
}
break;
case InputKeyLeft:
if(calculator_state->position.y > 0 && calculator_state->position.x > 0) {
calculator_state->position.x--;
}
break;
case InputKeyRight:
if(calculator_state->position.y < 1) {
// Cursor stays in the same column
} else if(calculator_state->position.y == 5 - 1) {
if(calculator_state->position.x < 3) {
calculator_state->position.x++;
}
} else {
if(calculator_state->position.x < 5 - 1) {
calculator_state->position.x++;
}
}
break;
case InputKeyOk:
if(calculator_state->position.y == 0) {
toggle_mode(calculator_state);
} else {
char key =
getKeyAtPosition(calculator_state->position.x, calculator_state->position.y);
handle_key_press(calculator_state, key);
}
break;
default:
break;
}
view_port_update(view_port);
}
void handle_long_press(Calculator* calculator_state, ViewPort* view_port, InputEvent* event) {
// Handling a long press event
switch(event->key) {
case InputKeyOk: {
const char* inputMessage = " github armixz";
strncpy(calculator_state->text, inputMessage, MAX_TEXT_LENGTH_INPUT - 1);
calculator_state->text[MAX_TEXT_LENGTH_INPUT - 1] = '\0';
calculator_state->textLength = strlen(calculator_state->text);
calculator_state->newInputStarted = true;
view_port_update(view_port);
} break;
default:
break;
}
}
void handle_key_press(Calculator* calculator_state, char key) {
switch(key) {
case '=':
// Logic for '=' key
strncpy(calculator_state->originalInput, calculator_state->text, MAX_TEXT_LENGTH_INPUT);
calculate(calculator_state);
// calculator_state->text[0] = '\0';
calculator_state->textLength = 0;
break;
case 'R':
// Logic for 'R' key, typically 'Clear'
calculator_state->text[0] = '\0';
calculator_state->textLength = 0;
calculator_state->decToBinResult[0] = '\0';
calculator_state->decToHexResult[0] = '\0';
calculator_state->decToCharResult[0] = '\0';
calculator_state->hexToBinResult[0] = '\0';
calculator_state->hexToDecResult[0] = '\0';
calculator_state->binToDecResult[0] = '\0';
calculator_state->binToHexResult[0] = '\0';
calculator_state->newInputStarted = false;
break;
case '<':
// Logic for '<' key, typically 'Backspace'
if(calculator_state->textLength > 0) {
calculator_state->text[--calculator_state->textLength] = '\0';
}
calculator_state->newInputStarted = false;
break;
default:
// Default logic for number and operator keys
if(calculator_state->newInputStarted) {
// Reset the text for a fresh input if new input has started
calculator_state->text[0] = '\0';
calculator_state->textLength = 0;
calculator_state->newInputStarted = false;
}
// Add the new character to the text, respecting the maximum text length
if(calculator_state->textLength < MAX_TEXT_LENGTH_INPUT - 1) {
calculator_state->text[calculator_state->textLength++] = key;
calculator_state->text[calculator_state->textLength] = '\0';
}
break;
}
}
void handle_event(Calculator* calculator_state, ViewPort* view_port, InputEvent* event) {
if(event->type == InputTypeShort) {
handle_short_press(calculator_state, view_port, event);
} else if(event->type == InputTypeLong) {
handle_long_press(calculator_state, view_port, event);
}
view_port_update(view_port);
}