Skip to content

Commit 216bed5

Browse files
committed
Changed the way BatteryIcon is displayed
1 parent 3537c7d commit 216bed5

5 files changed

Lines changed: 22 additions & 19 deletions

File tree

src/Avatar.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void Avatar::draw() {
157157
DrawContext *ctx = new DrawContext(this->expression, this->breath,
158158
&this->palette, g, this->eyeOpenRatio,
159159
this->mouthOpenRatio, this->speechText,
160-
this->rotation, this->scale, this->colorDepth, this->batteryIcon, this->speechFont);
160+
this->rotation, this->scale, this->colorDepth, this->batteryIcon, this->batteryLevel, this->speechFont);
161161
face->draw(ctx);
162162
delete ctx;
163163
}
@@ -216,4 +216,8 @@ void Avatar::setBatteryIcon(bool batteryIcon) {
216216
this->batteryIcon = batteryIcon;
217217
}
218218

219+
void Avatar::setBatteryLevel(int32_t batteryLevel) {
220+
this->batteryLevel = batteryLevel;
221+
}
222+
219223
} // namespace m5avatar

src/Avatar.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ class Avatar {
2424
ColorPalette palette;
2525
const char *speechText;
2626
int colorDepth;
27-
const lgfx::IFont *speechFont;
2827
bool batteryIcon;
28+
int32_t batteryLevel;
29+
const lgfx::IFont *speechFont;
2930

3031
public:
3132
Avatar();
@@ -59,6 +60,7 @@ class Avatar {
5960
void suspend();
6061
void resume();
6162
void setBatteryIcon(bool batteryIcon);
63+
void setBatteryLevel(int32_t batteryLevel);
6264
};
6365

6466
class DriveContext {

src/BatteryIcon.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,10 @@ 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) {
17-
//int32_t battery_level = M5.Power.getBatteryLevel();
18-
//M5.Log.printf("batterylevel:%d\n", battery_level);
19-
// if (M5.Power.isCharging()) {
20-
//color = TFT_GREEN;
21-
//} else {
22-
//color = TFT_RED;
23-
//}
24-
//uint32_t battery_level = M5.Power.getBatteryLevel();
25-
//M5.Log.printf("BatteryLevel:%d\n", battery_level);
16+
void drawBatteryIcon(M5Canvas *spi, uint32_t x, uint32_t y, uint16_t fgcolor, uint16_t bgcolor, float offset, int32_t batteryLevel) {
2617
spi->drawRect(x, y + 5, 5, 5, fgcolor);
2718
spi->drawRect(x + 5, y, 30, 15, fgcolor);
28-
int battery_width = 30 * (float)(M5.Power.getBatteryLevel() / 100.0f);
19+
int battery_width = 30 * (float)(batteryLevel / 100.0f);
2920
spi->fillRect(x + 5 + 30 - battery_width, y, battery_width, 15, fgcolor);
3021
}
3122

@@ -40,7 +31,8 @@ class BatteryIcon final : public Drawable {
4031
uint16_t primaryColor = ctx->getColorDepth() == 1 ? 1 : ctx->getColorPalette()->get(COLOR_PRIMARY);
4132
uint16_t bgColor = ctx->getColorDepth() == 1 ? ERACER_COLOR : ctx->getColorPalette()->get(COLOR_BACKGROUND);
4233
float offset = ctx->getBreath();
43-
drawBatteryIcon(spi, 285, 5, primaryColor, bgColor, -offset);
34+
int32_t batteryLevel = ctx->getBatteryLevel();
35+
drawBatteryIcon(spi, 285, 5, primaryColor, bgColor, -offset, batteryLevel);
4436
}
4537
};
4638

src/DrawContext.cpp

Lines changed: 6 additions & 3 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, const lgfx::IFont* speechFont)
13-
: DrawContext(expression, breath, palette, gaze, eyeOpenRatio, mouthOpenRatio, speechText, 0, 1, 1, false, speechFont){};
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){};
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, const lgfx::IFont* speechFont)
18+
const char* speechText, float rotation, float scale, int colorDepth, bool batteryIcon, int32_t batteryLevel, const lgfx::IFont* speechFont)
1919
: expression{expression},
2020
breath{breath},
2121
eyeOpenRatio{eyeOpenRatio},
@@ -27,6 +27,7 @@ DrawContext::DrawContext(Expression expression, float breath,
2727
scale{scale},
2828
colorDepth{colorDepth},
2929
batteryIcon(batteryIcon),
30+
batteryLevel(batteryLevel),
3031
speechFont{speechFont}{}
3132

3233
Expression DrawContext::getExpression() const { return expression; }
@@ -53,4 +54,6 @@ const lgfx::IFont* DrawContext::getSpeechFont() const { return speechFont; }
5354

5455
bool DrawContext::getBatteryIcon() const { return batteryIcon; }
5556

57+
int32_t DrawContext::getBatteryLevel() const { return batteryLevel; }
58+
5659
} // namespace m5avatar

src/DrawContext.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ class DrawContext {
2626
float scale = 1.0;
2727
int colorDepth = 1;
2828
bool batteryIcon = true;
29+
int32_t batteryLevel = 0;
2930
const lgfx::IFont* speechFont = nullptr; // = &fonts::lgfxJapanGothicP_16; // = &fonts::efontCN_10;
3031

3132
public:
3233
DrawContext() = delete;
3334
DrawContext(Expression expression, float breath, ColorPalette* const palette,
3435
Gaze gaze, float eyeOpenRatio, float mouthOpenRatio,
35-
const char* speechText, bool batteryIcon, const lgfx::IFont* speechFont);
36+
const char* speechText, bool batteryIcon, int32_t batteryLevel, const lgfx::IFont* speechFont);
3637
DrawContext(Expression expression, float breath, ColorPalette* const palette,
3738
Gaze gaze, float eyeOpenRatio, float mouthOpenRatio,
38-
const char* speechText, float rotation, float scale, int colorDepth, bool batteryIcon, const lgfx::IFont* speechFont);
39+
const char* speechText, float rotation, float scale, int colorDepth, bool batteryIcon, int32_t batteryLevel, const lgfx::IFont* speechFont);
3940
~DrawContext() = default;
4041
DrawContext(const DrawContext& other) = delete;
4142
DrawContext& operator=(const DrawContext& other) = delete;
@@ -50,6 +51,7 @@ class DrawContext {
5051
const char* getspeechText() const;
5152
int getColorDepth() const;
5253
bool getBatteryIcon() const;
54+
int32_t getBatteryLevel() const;
5355
const lgfx::IFont* getSpeechFont() const;
5456
};
5557
} // namespace m5avatar

0 commit comments

Comments
 (0)