Simplify type check in tick and init last

This commit is contained in:
Scott Shawcroft 2019-04-16 10:44:09 -07:00
parent 6132a05fd9
commit 190bdf8117
No known key found for this signature in database
GPG Key ID: FD0EDC4B6C53CA59
4 changed files with 31 additions and 33 deletions

View File

@ -49,6 +49,7 @@ void common_hal_gamepad_gamepad_init(gamepad_obj_t *gamepad,
}
gamepad->pins[i] = pin;
}
gamepad->last = 0;
}
void common_hal_gamepad_gamepad_deinit(gamepad_obj_t *self) {

View File

@ -38,26 +38,24 @@ void gamepad_tick(void) {
uint8_t bit = 1;
void* singleton = MP_STATE_VM(gamepad_singleton);
if (!singleton) {
if (singleton == NULL || !MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepad_type)) {
return;
}
if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepad_type)) {
// buttons connected directly to pins
gamepad_obj_t *self = singleton;
for (int i = 0; i < 8; ++i) {
digitalio_digitalinout_obj_t* pin = self->pins[i];
if (!pin) {
break;
}
if (common_hal_digitalio_digitalinout_get_value(pin)) {
current |= bit;
}
bit <<= 1;
gamepad_obj_t *self = MP_OBJ_TO_PTR(singleton);
for (int i = 0; i < 8; ++i) {
digitalio_digitalinout_obj_t* pin = self->pins[i];
if (!pin) {
break;
}
current ^= self->pulls;
self->pressed |= self->last & current;
self->last = current;
if (common_hal_digitalio_digitalinout_get_value(pin)) {
current |= bit;
}
bit <<= 1;
}
current ^= self->pulls;
self->pressed |= self->last & current;
self->last = current;
}
void gamepad_reset(void) {

View File

@ -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) {

View File

@ -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) {