Skip to content

Commit dd2473e

Browse files
committed
Added a charge symbol to the battery icon.
1 parent 216bed5 commit dd2473e

5 files changed

Lines changed: 50 additions & 23 deletions

File tree

src/Avatar.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,22 @@ void Avatar::start(int colorDepth) {
154154

155155
void Avatar::draw() {
156156
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+
}
157169
DrawContext *ctx = new DrawContext(this->expression, this->breath,
158170
&this->palette, g, this->eyeOpenRatio,
159171
this->mouthOpenRatio, this->speechText,
160-
this->rotation, this->scale, this->colorDepth, this->batteryIcon, this->batteryLevel, this->speechFont);
172+
this->rotation, this->scale, this->colorDepth, this->batteryIconStatus, this->batteryLevel, this->speechFont);
161173
face->draw(ctx);
162174
delete ctx;
163175
}
@@ -212,12 +224,15 @@ void Avatar::setSpeechFont(const lgfx::IFont *speechFont) {
212224
this->speechFont = speechFont;
213225
}
214226

215-
void Avatar::setBatteryIcon(bool batteryIcon) {
216-
this->batteryIcon = batteryIcon;
217-
}
218-
219-
void Avatar::setBatteryLevel(int32_t batteryLevel) {
220-
this->batteryLevel = batteryLevel;
227+
void Avatar::setM5PowerClass(m5::Power_Class* power) {
228+
this->power = power;
229+
if (power->isCharging()) {
230+
this->batteryIconStatus = BatteryIconStatus::isCharging;
231+
} else {
232+
this->batteryIconStatus = BatteryIconStatus::disCharge;
233+
}
234+
this->batteryLevel = power->getBatteryLevel();
235+
this->last_power_display_time = millis();
221236
}
222237

223238
} // namespace m5avatar

src/Avatar.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ class Avatar {
2424
ColorPalette palette;
2525
const char *speechText;
2626
int colorDepth;
27-
bool batteryIcon;
27+
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;
3032

3133
public:
3234
Avatar();
@@ -59,10 +61,10 @@ class Avatar {
5961
void addTask(TaskFunction_t f, const char* name);
6062
void suspend();
6163
void resume();
62-
void setBatteryIcon(bool batteryIcon);
63-
void setBatteryLevel(int32_t batteryLevel);
64+
void setM5PowerClass(m5::Power_Class* power);
6465
};
6566

67+
6668
class DriveContext {
6769
private:
6870
// TODO(meganetaaan): cyclic reference

src/BatteryIcon.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@ namespace m5avatar {
1313

1414
class BatteryIcon final : public Drawable {
1515
private:
16-
void drawBatteryIcon(M5Canvas *spi, uint32_t x, uint32_t y, uint16_t fgcolor, uint16_t bgcolor, float offset, int32_t batteryLevel) {
16+
void drawBatteryIcon(M5Canvas *spi, uint32_t x, uint32_t y, uint16_t fgcolor, uint16_t bgcolor, float offset, BatteryIconStatus batteryIconStatus, int32_t batteryLevel) {
1717
spi->drawRect(x, y + 5, 5, 5, fgcolor);
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-
}
21+
if (batteryIconStatus == BatteryIconStatus::isCharging) {
22+
spi->fillTriangle(x + 20, y, x + 15, y + 8, x + 20, y + 8, bgcolor);
23+
spi->fillTriangle(x + 18, y + 7, x + 18, y + 15, x + 23, y + 7, bgcolor);
24+
spi->drawLine(x + 20, y, x + 15, y + 8, fgcolor);
25+
spi->drawLine(x + 20, y, x + 20, y + 7, fgcolor);
26+
spi->drawLine(x + 18, y + 15, x + 23, y + 7, fgcolor);
27+
spi->drawLine(x + 18, y + 8, x + 18, y + 15, fgcolor);
28+
}
29+
}
2230

2331
public:
2432
// constructor
@@ -27,12 +35,12 @@ class BatteryIcon final : public Drawable {
2735
BatteryIcon(const BatteryIcon &other) = default;
2836
BatteryIcon &operator=(const BatteryIcon &other) = default;
2937
void draw(M5Canvas *spi, BoundingRect rect, DrawContext *ctx) override {
30-
if (ctx->getBatteryIcon()) {
38+
if (ctx->getBatteryIconStatus() != BatteryIconStatus::invisible) {
3139
uint16_t primaryColor = ctx->getColorDepth() == 1 ? 1 : ctx->getColorPalette()->get(COLOR_PRIMARY);
3240
uint16_t bgColor = ctx->getColorDepth() == 1 ? ERACER_COLOR : ctx->getColorPalette()->get(COLOR_BACKGROUND);
3341
float offset = ctx->getBreath();
3442
int32_t batteryLevel = ctx->getBatteryLevel();
35-
drawBatteryIcon(spi, 285, 5, primaryColor, bgColor, -offset, batteryLevel);
43+
drawBatteryIcon(spi, 285, 5, primaryColor, bgColor, -offset, ctx->getBatteryIconStatus(), batteryLevel);
3644
}
3745
};
3846

src/DrawContext.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace m5avatar {
99
DrawContext::DrawContext(Expression expression, float breath,
1010
ColorPalette* const palette, Gaze gaze,
1111
float eyeOpenRatio, float mouthOpenRatio,
12-
const char* speechText, bool batteryIcon, int32_t batteryLevel, const lgfx::IFont* speechFont)
13-
: DrawContext(expression, breath, palette, gaze, eyeOpenRatio, mouthOpenRatio, speechText, 0, 1, 1, false, 0, speechFont){};
12+
const char* speechText, BatteryIconStatus batteryIcon, int32_t batteryLevel, const lgfx::IFont* speechFont)
13+
: DrawContext(expression, breath, palette, gaze, eyeOpenRatio, mouthOpenRatio, speechText, 0, 1, 1, BatteryIconStatus::invisible, 0, speechFont){};
1414

1515
DrawContext::DrawContext(Expression expression, float breath,
1616
ColorPalette* const palette, Gaze gaze,
1717
float eyeOpenRatio, float mouthOpenRatio,
18-
const char* speechText, float rotation, float scale, int colorDepth, bool batteryIcon, int32_t batteryLevel, const lgfx::IFont* speechFont)
18+
const char* speechText, float rotation, float scale, int colorDepth, BatteryIconStatus batteryIconStatus, int32_t batteryLevel, const lgfx::IFont* speechFont)
1919
: expression{expression},
2020
breath{breath},
2121
eyeOpenRatio{eyeOpenRatio},
@@ -26,7 +26,7 @@ DrawContext::DrawContext(Expression expression, float breath,
2626
rotation{rotation},
2727
scale{scale},
2828
colorDepth{colorDepth},
29-
batteryIcon(batteryIcon),
29+
batteryIconStatus(batteryIconStatus),
3030
batteryLevel(batteryLevel),
3131
speechFont{speechFont}{}
3232

@@ -52,7 +52,7 @@ int DrawContext::getColorDepth() const { return colorDepth; }
5252

5353
const lgfx::IFont* DrawContext::getSpeechFont() const { return speechFont; }
5454

55-
bool DrawContext::getBatteryIcon() const { return batteryIcon; }
55+
BatteryIconStatus DrawContext::getBatteryIconStatus() const { return batteryIconStatus; }
5656

5757
int32_t DrawContext::getBatteryLevel() const { return batteryLevel; }
5858

src/DrawContext.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
#include "Expression.h"
1313
#include "Gaze.h"
1414

15+
1516
namespace m5avatar {
17+
enum BatteryIconStatus { disCharge, isCharging, invisible };
1618
class DrawContext {
1719
private:
1820
Expression expression;
@@ -25,18 +27,18 @@ class DrawContext {
2527
float rotation = 0.0;
2628
float scale = 1.0;
2729
int colorDepth = 1;
28-
bool batteryIcon = true;
30+
BatteryIconStatus batteryIconStatus = BatteryIconStatus::invisible;
2931
int32_t batteryLevel = 0;
3032
const lgfx::IFont* speechFont = nullptr; // = &fonts::lgfxJapanGothicP_16; // = &fonts::efontCN_10;
3133

3234
public:
3335
DrawContext() = delete;
3436
DrawContext(Expression expression, float breath, ColorPalette* const palette,
3537
Gaze gaze, float eyeOpenRatio, float mouthOpenRatio,
36-
const char* speechText, bool batteryIcon, int32_t batteryLevel, const lgfx::IFont* speechFont);
38+
const char* speechText, BatteryIconStatus batteryIconStatus, int32_t batteryLevel, const lgfx::IFont* speechFont);
3739
DrawContext(Expression expression, float breath, ColorPalette* const palette,
3840
Gaze gaze, float eyeOpenRatio, float mouthOpenRatio,
39-
const char* speechText, float rotation, float scale, int colorDepth, bool batteryIcon, int32_t batteryLevel, const lgfx::IFont* speechFont);
41+
const char* speechText, float rotation, float scale, int colorDepth, BatteryIconStatus batteryIconStatus, int32_t batteryLevel, const lgfx::IFont* speechFont);
4042
~DrawContext() = default;
4143
DrawContext(const DrawContext& other) = delete;
4244
DrawContext& operator=(const DrawContext& other) = delete;
@@ -50,7 +52,7 @@ class DrawContext {
5052
ColorPalette* const getColorPalette() const;
5153
const char* getspeechText() const;
5254
int getColorDepth() const;
53-
bool getBatteryIcon() const;
55+
BatteryIconStatus getBatteryIconStatus() const;
5456
int32_t getBatteryLevel() const;
5557
const lgfx::IFont* getSpeechFont() const;
5658
};

0 commit comments

Comments
 (0)