1 /*
2 * Copyright (c) 2023 Glenn Andrews
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/shell/shell.h>
9 #include "smf_calculator_thread.h"
10 #include <stdlib.h>
11
cmd_smf_key(const struct shell * sh,size_t argc,char ** argv)12 static int cmd_smf_key(const struct shell *sh, size_t argc, char **argv)
13 {
14 struct calculator_event event;
15 char c = argv[1][0];
16
17 event.operand = c;
18
19 if ((c >= '1') && (c <= '9')) {
20 event.event_id = DIGIT_1_9;
21 } else if (c == '0') {
22 event.event_id = DIGIT_0;
23 } else if (c == '.') {
24 event.event_id = DECIMAL_POINT;
25 } else if ((c == '+') || (c == '-') || (c == '*') || (c == '/')) {
26 event.event_id = OPERATOR;
27 } else if (c == '=') {
28 event.event_id = EQUALS;
29 } else if ((c == 'C') || (c == 'c')) {
30 event.event_id = CANCEL_BUTTON;
31 } else if ((c == 'E') || (c == 'e')) {
32 event.event_id = CANCEL_ENTRY;
33 } else {
34 shell_error(sh, "Invalid argument %s", argv[1]);
35 return -1;
36 }
37
38 int rc = post_calculator_event(&event, K_FOREVER);
39
40 if (rc != 0) {
41 shell_error(sh, "error posting key: %d", rc);
42 return rc;
43 }
44
45 shell_print(sh, "Key %c posted", c);
46 return 0;
47 }
48
49 SHELL_CMD_ARG_REGISTER(key, NULL,
50 "Send keypress to calculator\r\n"
51 "Valid keys 0-9, +-*/, '.', '=', 'E' (cancel entry) and C (clear)",
52 cmd_smf_key, 2, 0);
53