the code
This commit is contained in:
11
src/command.h
Normal file
11
src/command.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define exe(cmd) cmd.command(cmd.args, cmd.ret);
|
||||||
|
|
||||||
|
typedef void (*CommandFunction)(void* args, void* ret);
|
||||||
|
|
||||||
|
typedef struct Command_s{
|
||||||
|
void* args;
|
||||||
|
void* ret;
|
||||||
|
CommandFunction command;
|
||||||
|
}Command_t;
|
||||||
16
src/input.c
Normal file
16
src/input.c
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "input.h"
|
||||||
|
#include "command.h"
|
||||||
|
|
||||||
|
// in theroy your input should be a single ton but i want to support multiple interfaces
|
||||||
|
void handleInput(Input_t input_device) {
|
||||||
|
Inputs_e input = input_device.handle.getInput();
|
||||||
|
|
||||||
|
switch (input) {
|
||||||
|
case BUTTON_UP: exe(input_device.button_up_cmd); break;
|
||||||
|
case BUTTON_LEFT: exe(input_device.button_left_cmd); break;
|
||||||
|
case BUTTON_RIGHT: exe(input_device.button_right_cmd); break;
|
||||||
|
case BUTTON_DOWN: exe(input_device.button_down_cmd); break;
|
||||||
|
default: printf("HOW THE FUCK DID YOU PRESS A NON EXISTANT BUTTON"); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
26
src/input.h
Normal file
26
src/input.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "command.h "
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
BUTTON_UP,
|
||||||
|
BUTTON_LEFT,
|
||||||
|
BUTTON_RIGHT,
|
||||||
|
BUTTON_DOWN,
|
||||||
|
TOTAL_COMMANDS,
|
||||||
|
} Inputs_e;
|
||||||
|
|
||||||
|
typedef struct InputHandler_s {
|
||||||
|
Inputs_e (*getInput)(void);
|
||||||
|
}InputHandler_t;
|
||||||
|
|
||||||
|
typedef struct Input_s{
|
||||||
|
Command_t button_up_cmd;
|
||||||
|
Command_t button_left_cmd;
|
||||||
|
Command_t button_right_cmd;
|
||||||
|
Command_t button_down_cmd;
|
||||||
|
InputHandler_t handle;
|
||||||
|
}Input_t;
|
||||||
|
|
||||||
|
void handleInput(Input_t input_device);
|
||||||
|
|
||||||
73
src/main.c
Normal file
73
src/main.c
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "command.h "
|
||||||
|
#include "command.h"
|
||||||
|
#include "input.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// this is the underlying function
|
||||||
|
int printButton(const char* string) {
|
||||||
|
return printf(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
void commandWrapper(void* args, void* ret) {
|
||||||
|
// we cast ret to a int and then place the return val in to it
|
||||||
|
*(int*)(ret) = printButton(*(const char**)(args));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Inputs_e getinput() {
|
||||||
|
return BUTTON_DOWN;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// this is the only part i dont like
|
||||||
|
const char* button_up_cmd_args = "up button pressed";
|
||||||
|
int button_up_cmd_return_val = 0; // dont leave thing uninstalised
|
||||||
|
|
||||||
|
const char* button_left_cmd_args = "left button pressed";
|
||||||
|
int button_left_cmd_return_val = 0; // dont leave thing uninstalised
|
||||||
|
|
||||||
|
const char* button_right_cmd_args = "right button pressed";
|
||||||
|
int button_right_cmd_return_val = 0; // dont leave thing uninstalised
|
||||||
|
|
||||||
|
const char* button_down_cmd_args = "down button pressed";
|
||||||
|
int button_down_cmd_return_val = 0; // dont leave thing uninstalised
|
||||||
|
|
||||||
|
static Input_t input_device = (Input_t){
|
||||||
|
.button_up_cmd = (Command_t){
|
||||||
|
.args = (void*)(&button_up_cmd_args),
|
||||||
|
.ret = (void*)(&button_up_cmd_return_val),
|
||||||
|
.command = &commandWrapper,
|
||||||
|
},
|
||||||
|
.button_left_cmd = (Command_t){
|
||||||
|
.args = (void*)(&button_left_cmd_args),
|
||||||
|
.ret = (void*)(&button_left_cmd_return_val),
|
||||||
|
.command = &commandWrapper,
|
||||||
|
},
|
||||||
|
.button_right_cmd = (Command_t){
|
||||||
|
.args = (void*)(&button_right_cmd_args),
|
||||||
|
.ret = (void*)(&button_right_cmd_return_val),
|
||||||
|
.command = &commandWrapper,
|
||||||
|
},
|
||||||
|
.button_down_cmd = (Command_t){
|
||||||
|
.args = (void*)(&button_down_cmd_args),
|
||||||
|
.ret = (void*)(&button_down_cmd_return_val),
|
||||||
|
.command = &commandWrapper,
|
||||||
|
},
|
||||||
|
.handle = (InputHandler_t) {
|
||||||
|
.getInput = &getinput,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
// we have our input now we will handle it
|
||||||
|
handleInput(input_device);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user