diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2019-04-16 10:44:09 -0700 |
|---|---|---|
| committer | Scott Shawcroft <scott@tannewt.org> | 2019-04-16 10:44:09 -0700 |
| commit | 190bdf811716e35df086d317f01f97a10746790d (patch) | |
| tree | 2cd0e4eb5dbc4f5f4a8be803a4b16492035c1f35 /shared-module/gamepadshift | |
| parent | 6132a05fd9e6fa51972f1445ee051e99b816527f (diff) | |
Simplify type check in tick and init last
Diffstat (limited to 'shared-module/gamepadshift')
| -rw-r--r-- | shared-module/gamepadshift/GamePadShift.c | 2 | ||||
| -rw-r--r-- | shared-module/gamepadshift/__init__.c | 31 |
2 files changed, 16 insertions, 17 deletions
diff --git a/shared-module/gamepadshift/GamePadShift.c b/shared-module/gamepadshift/GamePadShift.c index f9303938f..b8fb4d3f4 100644 --- a/shared-module/gamepadshift/GamePadShift.c +++ b/shared-module/gamepadshift/GamePadShift.c @@ -40,6 +40,8 @@ void common_hal_gamepadshift_gamepadshift_init(gamepadshift_obj_t *gamepadshift, common_hal_digitalio_digitalinout_switch_to_output(latch_pin, 1, DRIVE_MODE_PUSH_PULL); gamepadshift->latch_pin = latch_pin; + + gamepadshift->last = 0; } void common_hal_gamepadshift_gamepadshift_deinit(gamepadshift_obj_t *gamepadshift) { diff --git a/shared-module/gamepadshift/__init__.c b/shared-module/gamepadshift/__init__.c index 728c2187e..47a008c50 100644 --- a/shared-module/gamepadshift/__init__.c +++ b/shared-module/gamepadshift/__init__.c @@ -31,28 +31,25 @@ void gamepadshift_tick(void) { void* singleton = MP_STATE_VM(gamepad_singleton); - if (!singleton) { + if (singleton == NULL || !MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepadshift_type)) { return; } - if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepadshift_type)) { - // buttons connected to a shift register - gamepadshift_obj_t *self = MP_OBJ_TO_PTR(singleton); - uint8_t current = 0; - uint8_t bit = 1; - common_hal_digitalio_digitalinout_set_value(self->latch_pin, 1); - for (int i = 0; i < 8; ++i) { - common_hal_digitalio_digitalinout_set_value(self->clock_pin, 0); - if (common_hal_digitalio_digitalinout_get_value(self->data_pin)) { - current |= bit; - } - common_hal_digitalio_digitalinout_set_value(self->clock_pin, 1); - bit <<= 1; + gamepadshift_obj_t *self = MP_OBJ_TO_PTR(singleton); + uint8_t current = 0; + uint8_t bit = 1; + common_hal_digitalio_digitalinout_set_value(self->latch_pin, 1); + for (int i = 0; i < 8; ++i) { + common_hal_digitalio_digitalinout_set_value(self->clock_pin, 0); + if (common_hal_digitalio_digitalinout_get_value(self->data_pin)) { + current |= bit; } - common_hal_digitalio_digitalinout_set_value(self->latch_pin, 0); - self->pressed |= self->last & current; - self->last = current; + common_hal_digitalio_digitalinout_set_value(self->clock_pin, 1); + bit <<= 1; } + common_hal_digitalio_digitalinout_set_value(self->latch_pin, 0); + self->pressed |= self->last & current; + self->last = current; } void gamepadshift_reset(void) { |
