8000 Simplify type check in tick and init last · ATMakersBill/circuitpython@190bdf8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 190bdf8

Browse files
committed
Simplify type check in tick and init last
1 parent 6132a05 commit 190bdf8

File tree

4 files changed

+31
-33
lines changed

4 files changed

+31
-33
lines changed

shared-module/gamepad/GamePad.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void common_hal_gamepad_gamepad_init(gamepad_obj_t *gamepad,
4949
}
5050
gamepad->pins[i] = pin;
5151
}
52+
gamepad->last = 0;
5253
}
5354

5455
void common_hal_gamepad_gamepad_deinit(gamepad_obj_t *self) {

shared-module/gamepad/__init__.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,24 @@ void gamepad_tick(void) {
3838
uint8_t bit = 1;
3939

4040
void* singleton = MP_STATE_VM(gamepad_singleton);
41-
if (!singleton) {
41+
if (singleton == NULL || !MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepad_type)) {
4242
return;
4343
}
44-
if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepad_type)) {
45-
// buttons connected directly to pins
46-
gamepad_obj_t *self = singleton;
47-
for (int i = 0; i < 8; ++i) {
48-
digitalio_digitalinout_obj_t* pin = self->pins[i];
49-
if (!pin) {
50-
break;
51-
}
52-
if (common_hal_digitalio_digitalinout_get_value(pin)) {
53-
current |= bit;
54-
}
55-
bit <<= 1;
44+
45+
gamepad_obj_t *self = MP_OBJ_TO_PTR(singleton);
46+
for (int i = 0; i < 8; ++i) {
47+
digitalio_digitalinout_obj_t* pin = self->pins[i];
48+
if (!pin) {
49+
break;
50+
}
51+
if (common_hal_digitalio_digitalinout_get_value(pin)) {
52+
current |= bit;
5653
}
57-
current ^= self->pulls;
58-
self->pressed |= self->last & current;
59-
self->last = current;
54+
bit <<= 1;
6055
}
56+
current ^= self->pulls;
57+
self->pressed |= self->last & current;
58+
self->last = current;
6159
}
6260

6361
void gamepad_reset(void) {

shared-module/gamepadshift/GamePadShift.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ void common_hal_gamepadshift_gamepadshift_init(gamepadshift_obj_t *gamepadshift,
4040
common_hal_digitalio_digitalinout_switch_to_output(latch_pin, 1,
4141
DRIVE_MODE_PUSH_PULL);
4242
gamepadshift->latch_pin = latch_pin;
43+
44+
gamepadshift->last = 0;
4345
}
4446

4547
void common_hal_gamepadshift_gamepadshift_deinit(gamepadshift_obj_t *gamepadshift) {

shared-module/gamepadshift/__init__.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,25 @@
3131

3232
void gamepadshift_tick(void) {
3333
void* singleton = MP_STATE_VM(gamepad_singleton);
34-
if (!singleton) {
34+
if (singleton == NULL || !MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepadshift_type)) {
3535
return;
3636
}
3737

38-
if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepadshift_type)) {
39-
// buttons connected to a shift register
40-
gamepadshift_obj_t *self = MP_OBJ_TO_PTR(singleton);
41-
uint8_t current = 0;
42-
uint8_t bit = 1;
43-
common_hal_digitalio_digitalinout_set_value(self->latch_pin, 1);
44-
for (int i = 0; i < 8; ++i) {
45-
common_hal_digitalio_digitalinout_set_value(self->clock_pin, 0);
46-
if (common_hal_digitalio_digitalinout_get_value(self->data_pin)) {
47-
current |= bit;
48-
}
49-
common_hal_digitalio_digitalinout_set_value(self->clock_pin, 1);
50-
bit <<= 1;
38+
gamepadshift_obj_t *self = MP_OBJ_TO_PTR(singleton);
39+
uint8_t current = 0;
40+
uint8_t bit = 1;
41+
common_hal_digitalio_digitalinout_set_value(self->latch_pin, 1);
42+
for (int i = 0; i < 8; ++i) {
43+
common_hal_digitalio_digitalinout_set_value(self->clock_pin, 0);
44+
if (common_hal_digitalio_digitalinout_get_value(self->data_pin)) {
45+
current |= bit;
5146
}
52-
common_hal_digitalio_digitalinout_set_value(self->latch_pin, 0);
53-
self->pressed |= self->last & current;
54-
self->last = current;
47+
common_hal_digitalio_digitalinout_set_value(self->clock_pin, 1);
48+
bit <<= 1;
5549
}
50+
common_hal_digitalio_digitalinout_set_value(self->latch_pin, 0);
51+
self->pressed |= self->last & current;
52+
self->last = current;
5653
}
5754

5855
void gamepadshift_reset(void) {

0 commit comments

Comments
 (0)
0