From c34f09dce1c11d86a6302fa2f265df07c9c50ac8 Mon Sep 17 00:00:00 2001 From: shylie Date: Sun, 8 Jun 2025 09:32:56 -0400 Subject: [PATCH] Add bounce sound; update submodule --- CMakeLists.txt | 2 ++ picoled | 2 +- pong/CMakeLists.txt | 2 +- pong/src/main.cpp | 52 ++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b1092a8..10afb23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,9 @@ pico_sdk_init() add_library(common INTERFACE) target_compile_definitions(common INTERFACE PICO_XOSC_STARTUP_DELAY_MULTIPLIER=128) +target_link_libraries(common INTERFACE pico_stdlib) add_subdirectory(picoled) add_subdirectory(pong) +add_subdirectory(gfx2d) diff --git a/picoled b/picoled index 8115ae1..10e9176 160000 --- a/picoled +++ b/picoled @@ -1 +1 @@ -Subproject commit 8115ae141217b05512ff59833360bbd5070a1ec2 +Subproject commit 10e9176d13b249e2705745a441aa333d8a2c1823 diff --git a/pong/CMakeLists.txt b/pong/CMakeLists.txt index acd911e..422ded0 100644 --- a/pong/CMakeLists.txt +++ b/pong/CMakeLists.txt @@ -4,7 +4,7 @@ add_executable(pong src/main.cpp ) -target_link_libraries(pong common picoled) +target_link_libraries(pong common picoled hardware_pwm) pico_enable_stdio_uart(pong 0) pico_enable_stdio_usb(pong 1) diff --git a/pong/src/main.cpp b/pong/src/main.cpp index 4552910..9bb3907 100644 --- a/pong/src/main.cpp +++ b/pong/src/main.cpp @@ -3,6 +3,8 @@ #include #include +#include +#include #include @@ -19,7 +21,7 @@ struct rect { for (int j = 0; j < h; j++) { - oled(x + i, y + j) = 0xFF; + oled.write(x + i, y + j, 0xFF); } } } @@ -68,6 +70,21 @@ constexpr uint8_t OLED_ADDRESS = 0x3C; constexpr float PADDLE_SPEED = 20; constexpr float INITIAL_BALL_SPEED = 32; +constexpr uint8_t BUZZER_PIN = 2; + +int64_t unbeep(alarm_id_t, void*) +{ + pwm_set_enabled(pwm_gpio_to_slice_num(BUZZER_PIN), false); + return 0; +} + +void beep() +{ + pwm_set_enabled(pwm_gpio_to_slice_num(BUZZER_PIN), true); + + add_alarm_in_ms(40, unbeep, nullptr, false); +} + } int main() @@ -94,20 +111,28 @@ int main() int64_t us = time_us_32(); + pwm_config config = pwm_get_default_config(); + pwm_config_set_wrap(&config, 0x6000); + pwm_config_set_clkdiv(&config, 2.6f); + pwm_init(pwm_gpio_to_slice_num(BUZZER_PIN), &config, false); + pwm_set_gpio_level(BUZZER_PIN, 0x3000); + + gpio_set_function(BUZZER_PIN, GPIO_FUNC_PWM); + while (true) { oled.clear(); for (int i = 0; i < oled.get_width(); i++) { - oled(i, 0) = 0xFF; - oled(i, oled.get_height() - 1) = 0xFF; + oled.write(i, 0, 0xFF); + oled.write(i, oled.get_height() - 1, 0xFF); } for (int i = 0; i < oled.get_height(); i++) { - oled(0, i) = 0xFF; - oled(oled.get_width() - 1, i) = 0xFF; + oled.write(0, i, 0xFF); + oled.write(oled.get_width() - 1, i, 0xFF); } int64_t current = time_us_32(); @@ -130,6 +155,8 @@ int main() if (ball.overlaps(player_top) || ball.overlaps(player_bottom)) { ball_dy = -ball_dy; + + beep(); } else if (ball.overlaps(player)) { @@ -142,6 +169,8 @@ int main() ball_dx = cosf(angle + angle_delta); ball_dy = sinf(angle + angle_delta); + + beep(); } const rect ai_top = { ai.left() + 2, ai.top(), ai.w - 2, 1 }; @@ -149,6 +178,8 @@ int main() if (ball.overlaps(ai_top) || ball.overlaps(ai_bottom)) { ball_dy = -ball_dy; + + beep(); } else if (ball.overlaps(ai)) { @@ -162,28 +193,38 @@ int main() ball_dx = cosf(angle + angle_delta); ball_dy = sinf(angle + angle_delta); + + beep(); } if (ball.left() < 0) { ball_dx = -ball_dx; ball.x = 0; + + beep(); } if (ball.right() >= oled.get_width()) { ball_dx = -ball_dx; ball.x = oled.get_width() - ball.w; + + beep(); } if (ball.top() < 0) { ball_dy = -ball_dy; ball.y = 0; + + beep(); } if (ball.bottom() >= oled.get_height()) { ball_dy = -ball_dy; ball.y = oled.get_height() - ball.h; + + beep(); } player.draw(oled); @@ -191,5 +232,6 @@ int main() ball.draw(oled); oled.update(); + oled.swap_buffers(); } }