Skip to content

Commit 715e9a0

Browse files
committed
code cleanup
1 parent 81d9cea commit 715e9a0

File tree

15 files changed

+123
-125
lines changed

15 files changed

+123
-125
lines changed

pong/main/Display.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
#include <cstdlib>
77
#include <vector>
88

9-
static inline uint8_t clampU8(int v)
9+
namespace {
10+
11+
// Clamp integer to uint8_t range [0, 255].
12+
inline uint8_t clampU8(int v)
1013
{
1114
if (v < 0) return 0;
1215
if (v > 255) return 255;
1316
return (uint8_t)v;
1417
}
1518

19+
// Byte-swap a 32-bit value (big-endian <-> little-endian).
1620
uint32_t swapEndian32(uint32_t val)
1721
{
1822
return ((val >> 24) & 0x000000FF) |
@@ -21,20 +25,24 @@ uint32_t swapEndian32(uint32_t val)
2125
((val << 24) & 0xFF000000);
2226
}
2327

28+
// Read a big-endian int32 from a byte buffer (used for VLW font parsing).
2429
int32_t readInt32(const uint8_t *data)
2530
{
2631
int32_t val = *((int32_t *)data);
2732
val = (int32_t)swapEndian32((uint32_t)val);
2833
return val;
2934
}
3035

36+
// Read a big-endian uint32 from a byte buffer (used for VLW font parsing).
3137
uint32_t readUInt32(const uint8_t *data)
3238
{
3339
uint32_t val = *((uint32_t *)data);
3440
val = (uint32_t)swapEndian32((uint32_t)val);
3541
return val;
3642
}
3743

44+
} // namespace
45+
3846
Display::Display(int width, int height) : _width(width), _height(height)
3947
{
4048
// RGB565 framebuffer: width * height.

pong/main/Display.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Display.h - Software RGB565 framebuffer with drawing primitives
2+
//
3+
// Provides a TFT-style API for 2D graphics: rectangles, lines, polygons, and
4+
// anti-aliased text rendering using VLW fonts. The framebuffer can be read
5+
// directly for JPEG encoding or other processing.
6+
17
#pragma once
28

39
#include <cstdint>
@@ -89,8 +95,8 @@ class Display
8995
virtual void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, Color color);
9096
virtual void drawPolygon(const std::vector<Point> &vertices, Color color);
9197
virtual void drawFilledPolygon(const std::vector<Point> &vertices, Color color);
92-
int width() { return _width; }
93-
int height() { return _height; }
98+
int width() const { return _width; }
99+
int height() const { return _height; }
94100
virtual void drawPixel(Color color, int x, int y);
95101

96102
// Raw RGB565 framebuffer access (row-major, 1 uint16_t per pixel).

pong/main/Game.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// Game.h - Abstract base class for games rendered via USB UVC
2+
//
3+
// Defines the standard game loop interface: readInput() -> update(dt) -> render().
4+
// FrameStats provides performance metrics for on-screen display.
5+
16
#pragma once
27

38
#include <stdint.h>

pong/main/PongGame.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ void PongGame::resetBall(int field_w, int field_h, int dir)
8080
ball_y_ = (float)field_h * 0.5f;
8181

8282
// Fixed initial angle so self-play is deterministic.
83-
const float vy = 0.35f;
8483
ball_vx_ = (float)dir * kBallSpeed;
85-
ball_vy_ = kBallSpeed * vy;
84+
ball_vy_ = kBallSpeed * kServeAngleRatio;
8685
}
8786

8887
void PongGame::readInput()
@@ -245,7 +244,7 @@ void PongGame::render(Display &display, const FrameStats &stats)
245244
ball_vx_ = std::abs(ball_vx_);
246245
const float center = (py0 + py1) * 0.5f;
247246
const float rel = (ball_y_ - center) / ((float)kPaddleH * 0.5f); // -1..1
248-
ball_vy_ += rel * 60.0f;
247+
ball_vy_ += rel * kPaddleSpinFactor;
249248
}
250249
}
251250

@@ -258,12 +257,12 @@ void PongGame::render(Display &display, const FrameStats &stats)
258257
ball_vx_ = -std::abs(ball_vx_);
259258
const float center = (py0 + py1) * 0.5f;
260259
const float rel = (ball_y_ - center) / ((float)kPaddleH * 0.5f); // -1..1
261-
ball_vy_ += rel * 60.0f;
260+
ball_vy_ += rel * kPaddleSpinFactor;
262261
}
263262
}
264263

265264
// Out of bounds.
266-
if (ball_x_ < -10.0f) {
265+
if (ball_x_ < -kOutOfBoundsMargin) {
267266
if (playing) {
268267
score_r_++;
269268
if (score_r_ >= kWinPoints) {
@@ -279,7 +278,7 @@ void PongGame::render(Display &display, const FrameStats &stats)
279278
// Demo: just reset ball, no score.
280279
resetBall(field_w, field_h, 1);
281280
}
282-
} else if (ball_x_ > (float)field_w + 10.0f) {
281+
} else if (ball_x_ > (float)field_w + kOutOfBoundsMargin) {
283282
if (playing) {
284283
score_l_++;
285284
if (score_l_ >= kWinPoints) {

pong/main/PongGame.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// PongGame.h - Classic Pong game implementation
2+
//
3+
// Features AI opponent, demo mode (self-play), and optional potentiometer control.
4+
// The game runs at whatever frame rate the USB host requests.
5+
16
#pragma once
27

38
#include "Game.h"
@@ -62,16 +67,20 @@ class PongGame final : public Game {
6267

6368
bool started_once_ = false;
6469

65-
// Layout
70+
// Layout constants (pixels)
6671
static constexpr int kPaddleW = 6;
6772
static constexpr int kPaddleH = 40;
6873
static constexpr int kPaddleInset = 8;
69-
static constexpr int kBallSize = 6; // square ball
74+
static constexpr int kBallSize = 6;
7075

71-
// Tuning
72-
static constexpr float kPaddleSpeed = 180.0f; // px/s
73-
static constexpr float kBallSpeed = 180.0f; // px/s
76+
// Physics constants
77+
static constexpr float kPaddleSpeed = 180.0f; // px/s
78+
static constexpr float kBallSpeed = 180.0f; // px/s
79+
static constexpr float kServeAngleRatio = 0.35f; // Initial vertical velocity as fraction of ball speed
80+
static constexpr float kPaddleSpinFactor = 60.0f; // Velocity added when hitting paddle off-center
81+
static constexpr float kOutOfBoundsMargin = 10.0f; // How far ball travels past edge before scoring
7482

83+
// Game rules
7584
static constexpr int kWinPoints = 3;
7685
static constexpr uint64_t kGameOverToDemoDelayUs = 10ULL * 1000000ULL;
7786
};

pong/main/RollingAverage.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
// RollingAverage.h - Efficient rolling average calculator
2+
//
3+
// Computes a rolling average over the last N samples in O(1) time per update.
4+
// Useful for smoothing frame timing statistics (FPS, encode time, draw time).
5+
16
#pragma once
27

38
#include <stdint.h>
49

10+
/// Rolling average over the last WindowSize samples.
11+
/// Uses O(1) time per update and O(WindowSize) memory.
512
template <int WindowSize>
613
class RollingAverageU32 {
714
public:

pong/main/TestGame.cpp

Lines changed: 0 additions & 58 deletions
This file was deleted.

pong/main/TestGame.h

Lines changed: 0 additions & 16 deletions
This file was deleted.

pong/main/jpeg_encoder.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
// jpeg_encoder.h - JPEG encoder wrapper for ESP-IDF
2+
//
3+
// Wraps the ESP JPEG encoder with a simple C++ interface. Supports RGB888 and
4+
// RGB565 input formats. The encoder handle is cached to avoid per-frame overhead.
5+
//
6+
// Memory ownership: encode methods allocate output via malloc(); caller must free().
7+
18
#pragma once
29

310
#include <stdint.h>

pong/main/main.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "fonts/GillSans_25_vlw.h"
2929

3030

31-
static const char *TAG = "display";
31+
static const char *TAG = "main";
3232

3333
extern const unsigned char test_card_jpeg_start[] asm("_binary_test_card_jpeg_start");
3434
extern const unsigned char test_card_jpeg_end[] asm("_binary_test_card_jpeg_end");
@@ -47,16 +47,16 @@ extern "C" void app_main(void)
4747
{
4848
for(int i = 0; i < 5; i++) {
4949
vTaskDelay(1000 / portTICK_PERIOD_MS);
50-
printf("Startup delay %d seconds...\n", 5 - i);
50+
ESP_LOGI(TAG, "Startup delay %d seconds...", 5 - i);
5151
}
5252

53-
printf("Hello world!\n");
53+
ESP_LOGI(TAG, "Hello world!");
5454

5555
/* Print chip information */
5656
esp_chip_info_t chip_info;
5757
uint32_t flash_size;
5858
esp_chip_info(&chip_info);
59-
printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
59+
ESP_LOGI(TAG, "This is %s chip with %d CPU core(s), %s%s%s%s",
6060
CONFIG_IDF_TARGET,
6161
chip_info.cores,
6262
(chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
@@ -66,26 +66,27 @@ extern "C" void app_main(void)
6666

6767
unsigned major_rev = chip_info.revision / 100;
6868
unsigned minor_rev = chip_info.revision % 100;
69-
printf("silicon revision v%d.%d, ", major_rev, minor_rev);
7069
if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
71-
printf("Get flash size failed");
70+
ESP_LOGE(TAG, "Get flash size failed");
7271
return;
7372
}
7473

75-
printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
74+
ESP_LOGI(TAG, "Silicon revision v%d.%d, %" PRIu32 "MB %s flash",
75+
major_rev, minor_rev,
76+
flash_size / (uint32_t)(1024 * 1024),
7677
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
7778

7879
{
7980
uint32_t freq_hz = 0;
8081
esp_err_t ret = esp_clk_tree_src_get_freq_hz(SOC_MOD_CLK_CPU, ESP_CLK_TREE_SRC_FREQ_PRECISION_APPROX, &freq_hz);
8182
if (ret == ESP_OK) {
82-
printf("CPU clock: %" PRIu32 " MHz\n", freq_hz / 1000000u);
83+
ESP_LOGI(TAG, "CPU clock: %" PRIu32 " MHz", freq_hz / 1000000u);
8384
} else {
84-
printf("CPU clock: unknown (err=%d)\n", (int)ret);
85+
ESP_LOGW(TAG, "CPU clock: unknown (err=%d)", (int)ret);
8586
}
8687
}
8788

88-
printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
89+
ESP_LOGI(TAG, "Minimum free heap size: %" PRIu32 " bytes", esp_get_minimum_free_heap_size());
8990

9091
static Display display(320, 240);
9192
static const JpegEncoder encoder(JpegEncoder::Config{

0 commit comments

Comments
 (0)