~ruther/CTU-FEE-B0B35APO-Semestral-project

ref: 50cf3199857b9c0f7139e66ec4095400be6a05f5 CTU-FEE-B0B35APO-Semestral-project/lib-pheripherals/include/input.h -rw-r--r-- 2.5 KiB
50cf3199 — František Boháček refactor: commands size and count types 3 years ago
                                                                                
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
#ifndef __INPUT_H__
#define __INPUT_H__

#include <stdbool.h>
#include <stdint.h>
#include <time.h>

#define ROTATION_ENCODERS_COUNT 3

typedef void (*command_fun)(void *state, int delta);

typedef enum {
  IN_KEYBOARD,
  IN_ENCODER_ROTATE,
  IN_ENCODER_CLICK,
} input_type_t;

typedef struct {
  input_type_t type;
  char filter;
} input_t;

typedef struct {
  uint8_t absolute;
  int8_t delta;

  bool button;
  bool button_prev;

  time_t pressed_time;
} rotation_encoder_state_t;

typedef struct {
  void *base_address;
  rotation_encoder_state_t encoders_state[ROTATION_ENCODERS_COUNT];
} rotation_encoders_t;

typedef struct {
  command_fun cmd;
  input_t input;
  void *state;
} command_t;

typedef struct {
  command_t *commands;
  uint16_t count;
  uint16_t size;

  rotation_encoders_t encoders;
} commands_t;

/**
 * @brief Create and initialize commands structure to use for handling input
 *
 * @param array of commands with lifetime at least as big as commands_t lifetime
 * @param size maximum number of commands in array
 * @param reg_knobs_base address of reg knobs in memory to get their data
 * @return commands_t
 */
commands_t commands_create(command_t *array, uint8_t size,
                           void *reg_knobs_base);

/**
 * @brief Add new command to commands to handle input with
 *
 * @param commands
 * @param type what type of input to look for
 * @param filter depending on the type this means either a char key or index of
 * rotation encoder
 * @param fun function that will be called when correct input is received
 * @param state state to pass to fun on command call with lifetime bigger than
 * command
 * @return true on success
 * @return false on error (full list)
 */
bool commands_register(commands_t *commands, input_type_t type, char filter,
                       command_fun fun, void *state);

/**
 * @brief Delete/Unregister command from commands so its function is not called
 * anymore
 *
 * @param commands commands to delete from
 * @param command command to delete
 * @return true command found and deleted
 * @return false command not found
 */
bool commands_unregister(commands_t *commands, command_t *command);

/**
 * @brief Check new state of input and if conditions for any of the commands are
 * met, call its function
 *
 * @param commands
 * @return short number of commands called or -1 in case of an error
 */
short commands_check_input(commands_t *commands);

uint16_t commands_save_state(commands_t *commands);

bool commands_restore_state(commands_t *commands, uint16_t state);

#endif // __INPUT_H__
Do not follow this link