Use adafruit encoder breakouts for controls instead

This commit is contained in:
shylie 2025-06-05 01:04:13 -04:00
parent bc1443a526
commit decb27cbdc

View File

@ -46,9 +46,24 @@ struct rect
}
};
constexpr uint8_t LEFT_BUTTON = 2;
constexpr uint8_t MIDDLE_BUTTON = 3;
constexpr uint8_t RIGHT_BUTTON = 4;
int32_t get_encoder_delta(i2c_inst* i2c, uint8_t address)
{
// SEESAW_ENCODER_BASE and SEESAW_ENCODER_DELTA values
// https://github.com/adafruit/Adafruit_Seesaw/blob/master/Adafruit_seesaw.h
uint8_t buf[4] = { 0x11, 0x40, 0x00, 0x00 };
i2c_write_blocking(i2c, address, buf, 2, false);
sleep_us(250);
i2c_read_blocking(i2c, address, buf, 4, false);
return
(static_cast<int32_t>(buf[0]) << 24) |
(static_cast<int32_t>(buf[1]) << 16) |
(static_cast<int32_t>(buf[2]) << 8 ) |
(static_cast<int32_t>(buf[3]) << 0 );
}
constexpr uint8_t LEFT_ENCODER_ADDRESS = 0x36;
constexpr uint8_t RIGHT_ENCODER_ADDRESS = 0x37;
constexpr uint8_t OLED_ADDRESS = 0x3C;
constexpr float PADDLE_SPEED = 20;
constexpr float INITIAL_BALL_SPEED = 32;
@ -60,23 +75,13 @@ int main()
stdio_usb_init();
srand(time_us_64());
i2c_init(i2c0, 3 * 1000 * 1000);
i2c_init(i2c0, 1000 * 1000);
gpio_set_function(0, GPIO_FUNC_I2C);
gpio_set_function(1, GPIO_FUNC_I2C);
gpio_pull_up(0);
gpio_pull_up(1);
gpio_set_function(LEFT_BUTTON, GPIO_FUNC_SIO);
gpio_set_function(MIDDLE_BUTTON, GPIO_FUNC_SIO);
gpio_set_function(RIGHT_BUTTON, GPIO_FUNC_SIO);
gpio_set_dir(LEFT_BUTTON, GPIO_IN);
gpio_set_dir(MIDDLE_BUTTON, GPIO_IN);
gpio_set_dir(RIGHT_BUTTON, GPIO_IN);
gpio_pull_down(LEFT_BUTTON);
gpio_pull_down(MIDDLE_BUTTON);
gpio_pull_down(RIGHT_BUTTON);
picoled::ssd1306 oled(i2c0, 0x3C);
picoled::ssd1306 oled(i2c0, OLED_ADDRESS);
oled.update();
rect player = { 8.0f, oled.get_height() / 2.0f - 8.0f, 4.0f, 16.0f };
@ -109,11 +114,13 @@ int main()
float dt = std::abs(current - us) / 1000000.0f;
us = current;
if (gpio_get(LEFT_BUTTON)) { player.y -= PADDLE_SPEED * dt; }
if (gpio_get(MIDDLE_BUTTON)) { player.y += PADDLE_SPEED * dt; }
int32_t left_enocder_delta = get_encoder_delta(i2c0, LEFT_ENCODER_ADDRESS);
int32_t right_encoder_delta = get_encoder_delta(i2c0, RIGHT_ENCODER_ADDRESS);
player.y += 2 * right_encoder_delta;
float diff = ball.middle_v() - ai.middle_v();
ai.y += 1.35f * diff * dt;
ai.y += 1.4f * diff * dt;
ball.x += ball_dx * dt * ball_speed;
ball.y += ball_dy * dt * ball_speed;