-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolume_ui.cpp
More file actions
61 lines (46 loc) · 1.65 KB
/
volume_ui.cpp
File metadata and controls
61 lines (46 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) 2025 Yasushi
// Licensed under the MIT License. See LICENSE for details.
#include "volume_ui.h"
#include <algorithm>
#include "logging.h"
namespace VolumeUI {
void draw(uint8_t vol) {
const int screenW = M5.Display.width();
const int screenH = M5.Display.height();
int y0 = screenH - VOLUME_UI_HEIGHT;
if (y0 < 0) y0 = 0;
const int x0 = 20;
const int x1 = screenW - 20;
const int barY = y0 + (VOLUME_UI_HEIGHT / 2);
SemaphoreHandle_t mutex = Logging::displayMutex();
if (!mutex || xSemaphoreTake(mutex, portMAX_DELAY) == pdTRUE) {
M5.Display.fillRect(0, y0, screenW, screenH - y0, 0x000000U);
M5.Display.fillRect(x0, barY - 4, x1 - x0, 8, 0x444444U); // バー
float t = vol / 255.0f;
int knobX = x0 + static_cast<int>(t * (x1 - x0));
M5.Display.fillCircle(knobX, barY, 10, 0xFFA500U); // ノブ(オレンジ)
M5.Display.setTextColor(0xFFFFFFU);
M5.Display.setCursor(x0, y0 + 5);
M5.Display.printf("Vol: %3u", vol);
if (mutex) xSemaphoreGive(mutex);
}
}
bool handleTouch(uint8_t& vol) {
int32_t x, y;
if (!M5.Display.getTouch(&x, &y)) return false;
const int screenW = M5.Display.width();
const int screenH = M5.Display.height();
const int y0 = std::max(0, screenH - VOLUME_UI_HEIGHT);
if (y < y0) return false; // ログ領域は無視
const int x0 = 20;
const int x1 = screenW - 20;
if (x1 <= x0) return false;
x = std::max(x0, std::min(static_cast<int>(x), x1));
uint8_t newVol = static_cast<uint8_t>((x - x0) * 255.0f / (x1 - x0));
if (newVol == vol) return false;
vol = newVol;
M5.Speaker.setVolume(vol);
draw(vol);
return true;
}
} // namespace VolumeUI