Skip to content

Commit

Permalink
"DPAD" expression operation
Browse files Browse the repository at this point in the history
Adds a "DPAD" expression operation that takes four input states and
produces a 0-8 value that can be used as a d-pad (hat switch) output
on a gamepad.

Example use:

0x00070050 input_state_binary
0x0007004f input_state_binary
0x00070052 input_state_binary
0x00070051 input_state_binary
dpad
  • Loading branch information
jfedor2 committed Dec 30, 2023
1 parent 59f82eb commit 61591ac
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions EXPRESSIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ Here's a list of all operations that can be used in an expression. Each operatio
| `tap_state` | _usage_ | _tap\_state(usage)_ | 1 if input is in tap state, 0 otherwise. |
| `hold_state` | _usage_ | _hold\_state(usage)_ | 1 if input is in hold state, 0 otherwise. |
| `port` | _port number_ | | Sets the value of the port register that determines which input state is fetched by `input_state` etc. Defaults to 0 at the beginning of each mapping engine iteration, which means "all ports". |
| `dpad` | _left_state_, _right_state_, _up_state_, _down_state_ | d-pad output state | Takes four input states and produces a 0-8 value that can be used as a d-pad (hat switch) output on a gamepad. |
1 change: 1 addition & 0 deletions config-tool-web/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const ops = {
"ATAN2": 32,
"ROUND": 33,
"PORT": 34,
"DPAD": 35,
}

const opcodes = Object.fromEntries(Object.entries(ops).map(([key, value]) => [value, key]));
Expand Down
1 change: 1 addition & 0 deletions config-tool/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"ATAN2": 32,
"ROUND": 33,
"PORT": 34,
"DPAD": 35,
}

opcodes = {v: k for k, v in ops.items()}
Expand Down
17 changes: 17 additions & 0 deletions firmware/src/remapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ bool is_expr_valid(uint8_t expr) {
}
on_stack--;
break;
case Op::DPAD:
if (on_stack < 4) {
return false;
}
on_stack -= 3;
break;
default:
printf("unknown op in is_expr_valid()\n");
return false;
Expand Down Expand Up @@ -580,6 +586,13 @@ void aggregate_relative(uint8_t* prev_report, const uint8_t* report, uint8_t rep
}
}

static uint8_t dpad_table[16] = { 8, 6, 2, 8, 0, 7, 1, 0, 4, 5, 3, 4, 8, 6, 2, 8 };

static inline uint8_t dpad(bool left, bool right, bool up, bool down) {
uint8_t index = left | (right << 1) | (up << 2) | (down << 3);
return dpad_table[index];
}

int32_t eval_expr(uint8_t expr, uint64_t now, bool auto_repeat) {
static int32_t stack[STACK_SIZE];
bool debug = false;
Expand Down Expand Up @@ -749,6 +762,10 @@ int32_t eval_expr(uint8_t expr, uint64_t now, bool auto_repeat) {
}
ptr--;
break;
case Op::DPAD:
stack[ptr - 3] = 1000 * dpad(stack[ptr - 3], stack[ptr - 2], stack[ptr - 1], stack[ptr]);
ptr -= 3;
break;
default:
printf("unknown op!\n");
return 0;
Expand Down
1 change: 1 addition & 0 deletions firmware/src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ enum class Op : int8_t {
ATAN2 = 32,
ROUND = 33,
PORT = 34,
DPAD = 35,
};

struct expr_elem_t {
Expand Down

0 comments on commit 61591ac

Please sign in to comment.