Compare commits
5 Commits
ab4ab04957
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd589d4e00 | ||
|
|
3317a7cc33 | ||
|
|
bc93460d04 | ||
|
|
0110b585b4 | ||
|
|
cf627eac0d |
21
README.md
21
README.md
@@ -1,3 +1,22 @@
|
|||||||
# c_command_pattern
|
# c_command_pattern
|
||||||
|
|
||||||
a simple example of how to implement the command pattern in c
|
a simple example of how to implement the command pattern in c. check [`main.c`](src/main.c) for the implementation
|
||||||
|
|
||||||
|
# how to build and run it
|
||||||
|
|
||||||
|
this demo uses a simple build system called [nob](https://github.com/tsoding/nob.h) a header only build system for c projects.
|
||||||
|
|
||||||
|
to build run these command in the root of the project:
|
||||||
|
```bash
|
||||||
|
|
||||||
|
# bootstraps the build system
|
||||||
|
> gcc nob.c -o nob
|
||||||
|
|
||||||
|
# runs the build system
|
||||||
|
> ./nob
|
||||||
|
|
||||||
|
# runs the program
|
||||||
|
> ./build/main
|
||||||
|
|
||||||
|
```
|
||||||
|
and if you have a differnt complier you want to use that is posix compliant just change the `CC` macro in the `nob.c` with your one.
|
||||||
37
src/main.c
37
src/main.c
@@ -1,40 +1,43 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "command.h "
|
#include <string.h>
|
||||||
|
|
||||||
#include "command.h"
|
#include "command.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// this is the underlying function
|
// this is the underlying function
|
||||||
int printButton(const char* string) {
|
int printButton(const char* string) {
|
||||||
|
// printf actually returns the number of chars printed to the screen
|
||||||
return printf(string);
|
return printf(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this should live in the app logic
|
||||||
void commandWrapper(void* args, void* ret) {
|
void commandWrapper(void* args, void* ret) {
|
||||||
// we cast ret to a int and then place the return val in to it
|
// we cast ret to a int and then place the return val in to it
|
||||||
*(int*)(ret) = printButton(*(const char**)(args));
|
*(int*)(ret) = printButton(*(const char**)(args));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// this should be in your input driver, so in here you may have a task
|
||||||
|
// that is getting these inputs and adding them to a queue
|
||||||
|
static Inputs_e current_input = BUTTON_DOWN;
|
||||||
Inputs_e getinput() {
|
Inputs_e getinput() {
|
||||||
return BUTTON_DOWN;
|
return current_input;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// this is the only part i dont like
|
const char* button_up_cmd_args = "up button pressed\n";
|
||||||
const char* button_up_cmd_args = "up button pressed";
|
|
||||||
int button_up_cmd_return_val = 0; // dont leave thing uninstalised
|
int button_up_cmd_return_val = 0; // dont leave thing uninstalised
|
||||||
|
|
||||||
const char* button_left_cmd_args = "left button pressed";
|
const char* button_left_cmd_args = "left button pressed\n";
|
||||||
int button_left_cmd_return_val = 0; // dont leave thing uninstalised
|
int button_left_cmd_return_val = 0; // dont leave thing uninstalised
|
||||||
|
|
||||||
const char* button_right_cmd_args = "right button pressed";
|
const char* button_right_cmd_args = "right button pressed\n";
|
||||||
int button_right_cmd_return_val = 0; // dont leave thing uninstalised
|
int button_right_cmd_return_val = 0; // dont leave thing uninstalised
|
||||||
|
|
||||||
const char* button_down_cmd_args = "down button pressed";
|
const char* button_down_cmd_args = "down button pressed\n";
|
||||||
int button_down_cmd_return_val = 0; // dont leave thing uninstalised
|
int button_down_cmd_return_val = 0; // dont leave thing uninstalised
|
||||||
|
|
||||||
|
// this should be implemented with your app logic
|
||||||
static Input_t input_device = (Input_t){
|
static Input_t input_device = (Input_t){
|
||||||
.button_up_cmd = (Command_t){
|
.button_up_cmd = (Command_t){
|
||||||
.args = (void*)(&button_up_cmd_args),
|
.args = (void*)(&button_up_cmd_args),
|
||||||
@@ -64,8 +67,22 @@ static Input_t input_device = (Input_t){
|
|||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
// we have our input now we will handle it
|
// we have our input now we will handle it
|
||||||
|
|
||||||
|
// arange
|
||||||
|
// set that current input is being pressed
|
||||||
|
current_input = BUTTON_UP;
|
||||||
|
|
||||||
|
// act
|
||||||
|
// poll the input a
|
||||||
handleInput(input_device);
|
handleInput(input_device);
|
||||||
|
|
||||||
|
// assert
|
||||||
|
if ((int)(strlen(button_up_cmd_args)) == button_up_cmd_return_val) {
|
||||||
|
printf("return value correct");
|
||||||
|
} else {
|
||||||
|
printf("return value incorrect");
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user