m5stack-avatar
Loading...
Searching...
No Matches
BatteryIcon.h
1// Copyright (c) Shinya Ishikawa. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full
3// license information.
4
5#ifndef BATTERYICON_H_
6#define BATTERYICON_H_
7#include <M5GFX.h>
8#include <M5Unified.h>
9#include "DrawContext.h"
10#include "Drawable.h"
11
12namespace m5avatar {
13
14class BatteryIcon final : public Drawable {
15 private:
16 void drawBatteryIcon(M5Canvas *spi, uint32_t x, uint32_t y, uint16_t fgcolor, uint16_t bgcolor, float offset, BatteryIconStatus batteryIconStatus, int32_t batteryLevel) {
17 spi->drawRect(x, y + 5, 5, 5, fgcolor);
18 spi->drawRect(x + 5, y, 30, 15, fgcolor);
19 int battery_width = 30 * (float)(batteryLevel / 100.0f);
20 spi->fillRect(x + 5 + 30 - battery_width, y, battery_width, 15, fgcolor);
21 if (batteryIconStatus == BatteryIconStatus::charging) {
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 }
30
31 public:
32 // constructor
33 BatteryIcon() = default;
34 ~BatteryIcon() = default;
35 BatteryIcon(const BatteryIcon &other) = default;
36 BatteryIcon &operator=(const BatteryIcon &other) = default;
37 void draw(M5Canvas *spi, BoundingRect rect, DrawContext *ctx) override {
38 if (ctx->getBatteryIconStatus() != BatteryIconStatus::invisible) {
39 uint16_t primaryColor = ctx->getColorDepth() == 1 ? 1 : ctx->getColorPalette()->get(COLOR_PRIMARY);
40 uint16_t bgColor = ctx->getColorDepth() == 1 ? ERACER_COLOR : ctx->getColorPalette()->get(COLOR_BACKGROUND);
41 float offset = ctx->getBreath();
42 int32_t batteryLevel = ctx->getBatteryLevel();
43 drawBatteryIcon(spi, 285, 5, primaryColor, bgColor, -offset, ctx->getBatteryIconStatus(), batteryLevel);
44 }
45 };
46
47};
48
49} // namespace m5avatar
50
51#endif // BATTERYICON_H_
Definition: BatteryIcon.h:14
Definition: BoundingRect.h:10
Definition: DrawContext.h:22
Definition: Drawable.h:13