Newer
Older
powermon_manager_sw / gui / gui_fg_stats.cpp
@Razvan Turiac Razvan Turiac on 8 Jul 5 KB Initial import
/* Copyright (C) 2020 - 2024, Thornwave Labs Inc
 * Written by Razvan Turiac <razvan.turiac@thornwave.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 * documentation files (the “Software”), to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * Attribution shall be given to Thornwave Labs Inc. and shall be made visible to the final user. 
 * 
 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <gui_fg_stats.h>


GuiFgStats::GuiFgStats(wxWindow *parent, const wxString &title, const Powermon::FuelgaugeStatistics &stats): wxDialog(parent, wxID_ANY, title)
{
	const size_t BORDER_SIZE = FromDIP(6);

	mCloseButton = new wxButton(this, wxID_CLOSE, wxT("Close"), wxDefaultPosition, FromDIP(wxSize(110, 40)));

	//main window sizer
	wxBoxSizer*	main_sizer = new wxBoxSizer(wxVERTICAL);
	SetSizer(main_sizer);

	wxString str;

	if (stats.time_since_last_full_charge != 0xFFFFFFFF)
	{
		uint32_t seconds = stats.time_since_last_full_charge;

		const uint32_t days = seconds / (3600 * 24);
		seconds -= days * 3600 * 24;

		const uint32_t hours = seconds / 3600;
		seconds -= hours * 3600;

		const uint32_t minutes = seconds / 60;
		seconds -= minutes * 60;

		if (days)
			str.Printf(wxT("Time since last full charge: %ud %uh %um"), days, hours, minutes);
		else if (hours)
			str.Printf(wxT("Time since last full charge: %uh %um %us"), hours, minutes, seconds);
		else
			str.Printf(wxT("Time since last full charge: %um %us"), minutes, seconds);
	}
	else
	{
		str.Printf(wxT("Time since last full charge: unknown"));
	}


	main_sizer->Add(new wxStaticText(this, wxID_ANY, str), 0, wxALL | wxALIGN_LEFT, BORDER_SIZE);


	str.Printf(wxT("Full charge capacity: %5.0f Ah"), stats.full_charge_capacity);
	main_sizer->Add(new wxStaticText(this, wxID_ANY, str), 0, wxALL | wxALIGN_LEFT, BORDER_SIZE);


	if (fabs(stats.total_discharge) >= 1000000)
		str.Printf(wxT("Total discharged capacity: %.2f kAh"), stats.total_discharge / 1000000.0);
	else
		str.Printf(wxT("Total discharged capacity: %.1f Ah"), stats.total_discharge / 1000.0);
	main_sizer->Add(new wxStaticText(this, wxID_ANY, str), 0, wxALL | wxALIGN_LEFT, BORDER_SIZE);

	if (fabs(stats.total_discharge_energy) >= 1000000)
		str.Printf(wxT("Total discharged energy: %.2f kWh"), stats.total_discharge_energy / 1000000.0);
	else
		str.Printf(wxT("Total discharged energy: %.0f Wh"), stats.total_discharge_energy / 1000.0);
	main_sizer->Add(new wxStaticText(this, wxID_ANY, str), 0, wxALL | wxALIGN_LEFT, BORDER_SIZE);


	if (fabs(stats.total_charge) >= 1000000)
		str.Printf(wxT("Total charged capacity: %.2f kAh"), stats.total_charge / 1000000.0);
	else
		str.Printf(wxT("Total charged capacity: %.1f Ah"), stats.total_charge / 1000.0);
	main_sizer->Add(new wxStaticText(this, wxID_ANY, str), 0, wxALL | wxALIGN_LEFT, BORDER_SIZE);

	if (fabs(stats.total_charge_energy) >= 1000000)
		str.Printf(wxT("Total charged energy: %.2f kWh"), stats.total_charge_energy / 1000000.0);
	else
		str.Printf(wxT("Total charged energy: %.0f Wh"), stats.total_charge_energy / 1000.0);
	main_sizer->Add(new wxStaticText(this, wxID_ANY, str), 0, wxALL | wxALIGN_LEFT, BORDER_SIZE);


	str.Printf(wxT("Minimum battery voltage: %.3f V"), stats.min_voltage);
	main_sizer->Add(new wxStaticText(this, wxID_ANY, str), 0, wxALL | wxALIGN_LEFT, BORDER_SIZE);

	str.Printf(wxT("Maximum battery voltage: %.3f V"), stats.max_voltage);
	main_sizer->Add(new wxStaticText(this, wxID_ANY, str), 0, wxALL | wxALIGN_LEFT, BORDER_SIZE);

	str.Printf(wxT("Maximum discharge current: %.2f A"), stats.max_discharge_current);
	main_sizer->Add(new wxStaticText(this, wxID_ANY, str), 0, wxALL | wxALIGN_LEFT, BORDER_SIZE);

	str.Printf(wxT("Maximum charge current: %.2f A"), stats.max_charge_current);
	main_sizer->Add(new wxStaticText(this, wxID_ANY, str), 0, wxALL | wxALIGN_LEFT, BORDER_SIZE);

	str.Printf(wxT("Last discharge depth: %.1f Ah"), stats.last_discharge);
	main_sizer->Add(new wxStaticText(this, wxID_ANY, str), 0, wxALL | wxALIGN_LEFT, BORDER_SIZE);

	str.Printf(wxT("Deepest discharge: %.1f Ah"), stats.deepest_discharge);
	main_sizer->Add(new wxStaticText(this, wxID_ANY, str), 0, wxALL | wxALIGN_LEFT, BORDER_SIZE);
	
	str.Printf(wxT("Number of full discharge cycles: %u"), (uint32_t)round(stats.total_discharge / (1000.0 * stats.full_charge_capacity)));
	main_sizer->Add(new wxStaticText(this, wxID_ANY, str), 0, wxALL | wxALIGN_LEFT, BORDER_SIZE);

	main_sizer->Add(mCloseButton, 0, wxALL | wxALIGN_CENTER, BORDER_SIZE);

	main_sizer->SetSizeHints(this);
	Centre();
}


void GuiFgStats::OnButtonCloseClicked(wxCommandEvent &event)
{
	Hide();
}


BEGIN_EVENT_TABLE(GuiFgStats, wxDialog)

EVT_BUTTON(wxID_CLOSE, GuiFgStats::OnButtonCloseClicked)

END_EVENT_TABLE()