Skip to content

Commit 7508f44

Browse files
committed
add setBatteryStatus
1 parent dd2473e commit 7508f44

4 files changed

Lines changed: 22 additions & 25 deletions

File tree

src/Avatar.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ Avatar::Avatar(Face *face)
7979
scale{1},
8080
palette{ColorPalette()},
8181
speechText{""},
82-
colorDepth{1}{}
82+
colorDepth{1},
83+
batteryIconStatus{BatteryIconStatus::invisible}{}
8384

8485
void Avatar::setFace(Face *face) { this->face = face; }
8586

@@ -154,18 +155,6 @@ void Avatar::start(int colorDepth) {
154155

155156
void Avatar::draw() {
156157
Gaze g = Gaze(this->gazeV, this->gazeH);
157-
if (power != nullptr) {
158-
if ((millis() - last_power_display_time) > 3000) {
159-
// バッテリー残量の更新は3秒に一度
160-
if (power->isCharging()) {
161-
this->batteryIconStatus = BatteryIconStatus::isCharging;
162-
} else {
163-
this->batteryIconStatus = BatteryIconStatus::disCharge;
164-
}
165-
this->batteryLevel = power->getBatteryLevel();
166-
last_power_display_time = millis();
167-
}
168-
}
169158
DrawContext *ctx = new DrawContext(this->expression, this->breath,
170159
&this->palette, g, this->eyeOpenRatio,
171160
this->mouthOpenRatio, this->speechText,
@@ -224,15 +213,24 @@ void Avatar::setSpeechFont(const lgfx::IFont *speechFont) {
224213
this->speechFont = speechFont;
225214
}
226215

227-
void Avatar::setM5PowerClass(m5::Power_Class* power) {
228-
this->power = power;
229-
if (power->isCharging()) {
230-
this->batteryIconStatus = BatteryIconStatus::isCharging;
216+
void Avatar::setBatteryIcon(bool batteryIcon) {
217+
if (!batteryIcon) {
218+
batteryIconStatus = BatteryIconStatus::invisible;
231219
} else {
232-
this->batteryIconStatus = BatteryIconStatus::disCharge;
220+
batteryIconStatus = BatteryIconStatus::unknown;
233221
}
234-
this->batteryLevel = power->getBatteryLevel();
235-
this->last_power_display_time = millis();
222+
}
223+
224+
void Avatar::setBatteryStatus(bool isCharging, int32_t batteryLevel) {
225+
if (this->batteryIconStatus != BatteryIconStatus::invisible) {
226+
if (isCharging) {
227+
this->batteryIconStatus = BatteryIconStatus::charging;
228+
} else {
229+
this->batteryIconStatus = BatteryIconStatus::discharging;
230+
}
231+
this->batteryLevel = batteryLevel;
232+
}
233+
236234
}
237235

238236
} // namespace m5avatar

src/Avatar.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class Avatar {
2727
BatteryIconStatus batteryIconStatus;
2828
int32_t batteryLevel;
2929
const lgfx::IFont *speechFont;
30-
m5::Power_Class* power = nullptr;
31-
uint64_t last_power_display_time = 0;
3230

3331
public:
3432
Avatar();
@@ -61,7 +59,8 @@ class Avatar {
6159
void addTask(TaskFunction_t f, const char* name);
6260
void suspend();
6361
void resume();
64-
void setM5PowerClass(m5::Power_Class* power);
62+
void setBatteryIcon(bool iconStatus);
63+
void setBatteryStatus(bool isCharging, int32_t batteryLevel);
6564
};
6665

6766

src/BatteryIcon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class BatteryIcon final : public Drawable {
1818
spi->drawRect(x + 5, y, 30, 15, fgcolor);
1919
int battery_width = 30 * (float)(batteryLevel / 100.0f);
2020
spi->fillRect(x + 5 + 30 - battery_width, y, battery_width, 15, fgcolor);
21-
if (batteryIconStatus == BatteryIconStatus::isCharging) {
21+
if (batteryIconStatus == BatteryIconStatus::charging) {
2222
spi->fillTriangle(x + 20, y, x + 15, y + 8, x + 20, y + 8, bgcolor);
2323
spi->fillTriangle(x + 18, y + 7, x + 18, y + 15, x + 23, y + 7, bgcolor);
2424
spi->drawLine(x + 20, y, x + 15, y + 8, fgcolor);

src/DrawContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
namespace m5avatar {
17-
enum BatteryIconStatus { disCharge, isCharging, invisible };
17+
enum BatteryIconStatus { discharging, charging, invisible, unknown };
1818
class DrawContext {
1919
private:
2020
Expression expression;

0 commit comments

Comments
 (0)