diff --git a/Battery.h b/Battery.h index 6d5c6a2..3ca17a3 100644 --- a/Battery.h +++ b/Battery.h @@ -17,10 +17,9 @@ void setPosAndSize(int posX, int posY, int showSizeNum); void setDeleteBgColor(uint16_t color); void setTextColor(uint16_t color); - void showBattery(); - void deleteBattery(); - void batteryUpdate(int percent); + void batteryUpdate(); void setSprite(TFT_eSprite *sprite); + void setCheckRate(uint16_t rate); private: const float MAX_BATTERY_VOLTAGE = 4.0f; @@ -41,11 +40,15 @@ uint16_t _bg_color; uint16_t _line_color; uint16_t _text_color; + uint16_t ratecount = 100; + uint16_t rateskip = 100; + int i_percent = -1; + uint16_t statusColor = 0; void drawBatteryLines(); int calcBatteryPercent(); uint16_t getBatteryColor(); - void showBatteryPercent(int i_percent); + void showBatteryPercent(); bool isLowBattery(); bool isUsingBattery(); }; @@ -97,6 +100,10 @@ _height = 10 * _showSize; _top_width = 2 * _showSize; } +void Battery::setCheckRate(uint16_t _checkrate) +{ + ratecount = _checkrate; +} /** * 削除時の背景色を設定する関数 @@ -116,59 +123,31 @@ } /** - * 電池残量を表示する関数 + * バッテリー残量の表示を更新する関数(ここを定期的に呼び出す) */ -void Battery::showBattery() +void Battery::batteryUpdate() { - _showFlg = true; - drawBatteryLines(); - // _sprite->setColorDepth(16); - // _sprite->createSprite(_width - 1, _height - 1); -} - -/** - * 電池残量を非表示(塗りつぶし)する関数 - */ -void Battery::deleteBattery() -{ - // スプライト全体を_bg_colorで塗りつぶしてメモリ開放 - // _sprite->deleteSprite(); - // _sprite->createSprite(_width + _top_width, _height + 1); - // _sprite->fillSprite(_bg_color); - // _sprite->pushSprite(_x, _y); - // _sprite->deleteSprite(); - _showFlg = false; -} - -/** - * バッテリー残量の表示を更新する関数 - */ -void Battery::batteryUpdate(int percent = -1) -{ - if (!_showFlg) + if (rateskip % 5 == 0 || statusColor == 0 ) { - return; + statusColor = getBatteryColor(); } - - // 電池図形内部背景塗りつぶし - _sprite->fillRect(_x, _y, _width - 1, _height - 1, TFT_BLACK); - - // 実際にバッテリーの値を計算するか判定 - int i_percent = percent; - if (i_percent == -1) + if (rateskip == 1 || i_percent == -1 ) { i_percent = calcBatteryPercent(); + rateskip = ratecount; } - - drawBatteryLines(); - + // 電池図形内部背景塗りつぶし + _sprite->fillRect(_x, _y, _width - 1, _height - 1, TFT_BLACK); + // 電池の外形 + drawBatteryLines(); // バッテリー残量の割合を計算して背景色塗りつぶし int b_width = int((_width - 2) * (i_percent / 100.0f)); - _sprite->fillRect(_x + 1, _y + 1, b_width + 1, _height - 1, getBatteryColor()); + _sprite->fillRect(_x + 2, _y + 2, b_width , _height - 3, statusColor); - showBatteryPercent(i_percent); // バッテリー数値を表示 - Serial.printf("batt %d", i_percent); + showBatteryPercent(); // バッテリー数値を表示 + // Serial.printf("batt %d", i_percent); // _sprite->pushSprite(_x + 1, _y + 1); // ディスプレイに表示 + rateskip--; } /** @@ -189,10 +168,10 @@ /** * バッテリー残量を表示する関数 */ -void Battery::showBatteryPercent(int i_percent) +void Battery::showBatteryPercent() { // バッテリー数値を表示 - _sprite->setCursor(_x + _width + 6, _y + _showSize); + _sprite->setCursor(_x + _width + 7, _y + _showSize+1); _sprite->setTextFont(1); _sprite->setTextColor(_text_color); _sprite->setTextSize(_showSize); diff --git a/M5StickCPlus_FactoryTest2022.ino b/M5StickCPlus_FactoryTest2022.ino index 5e3179e..59ad858 100644 --- a/M5StickCPlus_FactoryTest2022.ino +++ b/M5StickCPlus_FactoryTest2022.ino @@ -1505,6 +1505,7 @@ battery.setSprite(&Disbuff); // バッテリー残量表示 battery.setPosAndSize(160, 1, 1); + battery.setCheckRate(300); // // deleteBattery()時の塗りつぶし色を設定 // battery.setDeleteBgColor(TFT_BLACK); // 電池図形と%表示の色を設定