Skip to content

Commit

Permalink
BtHidApp: move variable from bss to model, cleanup naming.
Browse files Browse the repository at this point in the history
  • Loading branch information
skotopes committed Jul 8, 2022
1 parent 6ebe6e4 commit d419fe0
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions applications/bt/bt_hid_app/views/bt_hid_keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef struct {
bool ok_pressed;
bool back_pressed;
bool connected;
char key_string[5];
} BtHidKeyboardModel;

typedef struct {
Expand All @@ -35,6 +36,7 @@ typedef struct {
int8_t x;
int8_t y;
} BtHidKeyboardPoint;

// 4 BY 12
#define MARGIN_TOP 0
#define MARGIN_LEFT 4
Expand All @@ -45,7 +47,7 @@ typedef struct {
#define COLUMN_COUNT 12

// 0 width items are not drawn, but there value is used
BtHidKeyboardKey keyboardKeySet[ROW_COUNT][COLUMN_COUNT] = {
const BtHidKeyboardKey bt_hid_keyboard_keyset[ROW_COUNT][COLUMN_COUNT] = {
{
{.width = 1, .icon = NULL, .key = "1", .shift_key = "!", .value = HID_KEYBOARD_1},
{.width = 1, .icon = NULL, .key = "2", .shift_key = "@", .value = HID_KEYBOARD_2},
Expand Down Expand Up @@ -135,13 +137,14 @@ BtHidKeyboardKey keyboardKeySet[ROW_COUNT][COLUMN_COUNT] = {
{.width = 0, .icon = NULL, .value = HID_KEYBOARD_TAB},
},
};

static void bt_hid_keyboard_to_upper(char* str) {
while(*str) {
*str = toupper((unsigned char)*str);
str++;
}
}
char keyString[5];

static void bt_hid_keyboard_draw_key(
Canvas* canvas,
BtHidKeyboardModel* model,
Expand Down Expand Up @@ -180,20 +183,20 @@ static void bt_hid_keyboard_draw_key(
key.icon);
} else {
// If shift is toggled use the shift key when available
strcpy(keyString, (model->shift && key.shift_key != 0) ? key.shift_key : key.key);
strcpy(model->key_string, (model->shift && key.shift_key != 0) ? key.shift_key : key.key);
// Upper case if ctrl or alt was toggled true
if((model->ctrl && key.value == HID_KEYBOARD_L_CTRL) ||
(model->alt && key.value == HID_KEYBOARD_L_ALT) ||
(model->gui && key.value == HID_KEYBOARD_L_GUI)) {
bt_hid_keyboard_to_upper(keyString);
bt_hid_keyboard_to_upper(model->key_string);
}
canvas_draw_str_aligned(
canvas,
MARGIN_LEFT + x * (KEY_WIDTH + KEY_PADDING) + keyWidth / 2 + 1,
MARGIN_TOP + y * (KEY_HEIGHT + KEY_PADDING) + KEY_HEIGHT / 2,
AlignCenter,
AlignCenter,
keyString);
model->key_string);
}
}

Expand All @@ -215,7 +218,7 @@ static void bt_hid_keyboard_draw_callback(Canvas* canvas, void* context) {
// Start shifting the all keys up if on the next row (Scrolling)
uint8_t initY = model->y - 4 > 0 ? model->y - 4 : 0;
for(uint8_t y = initY; y < ROW_COUNT; y++) {
BtHidKeyboardKey* keyboardKeyRow = keyboardKeySet[y];
const BtHidKeyboardKey* keyboardKeyRow = bt_hid_keyboard_keyset[y];
uint8_t x = 0;
for(uint8_t i = 0; i < COLUMN_COUNT; i++) {
BtHidKeyboardKey key = keyboardKeyRow[i];
Expand All @@ -236,8 +239,9 @@ static void bt_hid_keyboard_draw_callback(Canvas* canvas, void* context) {
}
}
}

static uint8_t bt_hid_keyboard_get_selected_key(BtHidKeyboardModel* model) {
BtHidKeyboardKey key = keyboardKeySet[model->y][model->x];
BtHidKeyboardKey key = bt_hid_keyboard_keyset[model->y][model->x];
// Use upper case if shift is toggled
bool useUppercase = model->shift;
// Check if the key has an upper case version
Expand All @@ -247,21 +251,22 @@ static uint8_t bt_hid_keyboard_get_selected_key(BtHidKeyboardModel* model) {
else
return key.value;
}

static void bt_hid_keyboard_get_select_key(BtHidKeyboardModel* model, BtHidKeyboardPoint delta) {
// Keep going until a valid spot is found, this allows for nulls and zero width keys in the map
do {
if(((int8_t)model->y) + delta.y < 0)
model->y = ROW_COUNT - 1;
else
model->y = (model->y + delta.y) % ROW_COUNT;
} while(delta.y != 0 && keyboardKeySet[model->y][model->x].value == 0);
} while(delta.y != 0 && bt_hid_keyboard_keyset[model->y][model->x].value == 0);

do {
if(((int8_t)model->x) + delta.x < 0)
model->x = COLUMN_COUNT - 1;
else
model->x = (model->x + delta.x) % COLUMN_COUNT;
} while(delta.x != 0 && keyboardKeySet[model->y][model->x].width ==
} while(delta.x != 0 && bt_hid_keyboard_keyset[model->y][model->x].width ==
0); // Skip zero width keys, pretend they are one key
}

Expand Down

0 comments on commit d419fe0

Please sign in to comment.