Skip to content

Commit

Permalink
add setBatteryStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
mongonta0716 committed May 27, 2023
1 parent dd2473e commit 7508f44
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
38 changes: 18 additions & 20 deletions src/Avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ Avatar::Avatar(Face *face)
scale{1},
palette{ColorPalette()},
speechText{""},
colorDepth{1}{}
colorDepth{1},
batteryIconStatus{BatteryIconStatus::invisible}{}

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

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

void Avatar::draw() {
Gaze g = Gaze(this->gazeV, this->gazeH);
if (power != nullptr) {
if ((millis() - last_power_display_time) > 3000) {
// バッテリー残量の更新は3秒に一度
if (power->isCharging()) {
this->batteryIconStatus = BatteryIconStatus::isCharging;
} else {
this->batteryIconStatus = BatteryIconStatus::disCharge;
}
this->batteryLevel = power->getBatteryLevel();
last_power_display_time = millis();
}
}
DrawContext *ctx = new DrawContext(this->expression, this->breath,
&this->palette, g, this->eyeOpenRatio,
this->mouthOpenRatio, this->speechText,
Expand Down Expand Up @@ -224,15 +213,24 @@ void Avatar::setSpeechFont(const lgfx::IFont *speechFont) {
this->speechFont = speechFont;
}

void Avatar::setM5PowerClass(m5::Power_Class* power) {
this->power = power;
if (power->isCharging()) {
this->batteryIconStatus = BatteryIconStatus::isCharging;
void Avatar::setBatteryIcon(bool batteryIcon) {
if (!batteryIcon) {
batteryIconStatus = BatteryIconStatus::invisible;
} else {
this->batteryIconStatus = BatteryIconStatus::disCharge;
batteryIconStatus = BatteryIconStatus::unknown;
}
this->batteryLevel = power->getBatteryLevel();
this->last_power_display_time = millis();
}

void Avatar::setBatteryStatus(bool isCharging, int32_t batteryLevel) {
if (this->batteryIconStatus != BatteryIconStatus::invisible) {
if (isCharging) {
this->batteryIconStatus = BatteryIconStatus::charging;
} else {
this->batteryIconStatus = BatteryIconStatus::discharging;
}
this->batteryLevel = batteryLevel;
}

}

} // namespace m5avatar
5 changes: 2 additions & 3 deletions src/Avatar.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class Avatar {
BatteryIconStatus batteryIconStatus;
int32_t batteryLevel;
const lgfx::IFont *speechFont;
m5::Power_Class* power = nullptr;
uint64_t last_power_display_time = 0;

public:
Avatar();
Expand Down Expand Up @@ -61,7 +59,8 @@ class Avatar {
void addTask(TaskFunction_t f, const char* name);
void suspend();
void resume();
void setM5PowerClass(m5::Power_Class* power);
void setBatteryIcon(bool iconStatus);
void setBatteryStatus(bool isCharging, int32_t batteryLevel);
};


Expand Down
2 changes: 1 addition & 1 deletion src/BatteryIcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BatteryIcon final : public Drawable {
spi->drawRect(x + 5, y, 30, 15, fgcolor);
int battery_width = 30 * (float)(batteryLevel / 100.0f);
spi->fillRect(x + 5 + 30 - battery_width, y, battery_width, 15, fgcolor);
if (batteryIconStatus == BatteryIconStatus::isCharging) {
if (batteryIconStatus == BatteryIconStatus::charging) {
spi->fillTriangle(x + 20, y, x + 15, y + 8, x + 20, y + 8, bgcolor);
spi->fillTriangle(x + 18, y + 7, x + 18, y + 15, x + 23, y + 7, bgcolor);
spi->drawLine(x + 20, y, x + 15, y + 8, fgcolor);
Expand Down
2 changes: 1 addition & 1 deletion src/DrawContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


namespace m5avatar {
enum BatteryIconStatus { disCharge, isCharging, invisible };
enum BatteryIconStatus { discharging, charging, invisible, unknown };
class DrawContext {
private:
Expression expression;
Expand Down

0 comments on commit 7508f44

Please sign in to comment.