Compare commits

...

5 Commits

Author SHA1 Message Date
sirlilpanda
fd589d4e00 added comments and fixed import string 2025-08-25 10:42:47 +12:00
sirlilpanda
3317a7cc33 fixed import string 2025-08-25 10:42:21 +12:00
sirlilpanda
bc93460d04 added some more info to the readme 2025-08-24 20:47:59 +12:00
sirlilpanda
0110b585b4 add code for checking the return value is actually set and is correct 2025-08-24 14:52:45 +12:00
sirlilpanda
cf627eac0d added build instru 2025-08-24 14:45:13 +12:00
3 changed files with 48 additions and 12 deletions

View File

@@ -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.

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "command.h " #include "command.h"
typedef enum { typedef enum {
BUTTON_UP, BUTTON_UP,

View File

@@ -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;
} }