' Gambas module file

' Copyright(C) 2011-2014, Michał Głowienka aka eloaders <eloaders@linux.pl>
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.

Public addres_battery As String
Public addres_battery_2 As String
Public Present As String
Public Design_capacity As String
Public last_full_capacity As String
Public battery_technology As String
Public design_voltage As String
Public design_capacity_warning As String
Public design_capacity_low As String
Public cycle_count As String
Public capacity_granularity_1 As String
Public capacity_granularity_2 As String
Public model_number As String
Public serial_number As String
Public battery_type As String
Public OEM_info As String
Public calculate_percentage_max As String
Public calculate_percentage As String
Public cal_max As Float
Public cal As Float
'Public variables for BAT1
Public alarm As String
Public capacity As String
Public charge_full As String
Public charge_full_design As String
Public charge_now As String
Public current_now As String
Public manufacturer As String
Public model_name As String
Public status As String
Public technology As String
Public voltage_min_design As String
Public voltage_now As String

Public Sub _inits()
If Exist("/proc/acpi/battery/BAT0", True) Then
 addres_battery = "/proc/acpi/battery/BAT0/info"
 addres_battery_2 = "/proc/acpi/battery/BAT0/state"
 Finfosys.TabStrip1[9].Visible = True
 battery._get_inf()
 battery._replace_n()
 Finfosys.battery_info()
Endif
If Exist("/proc/acpi/battery/BAT1/info", True) Then
 addres_battery = "/proc/acpi/battery/BAT1/info"
 addres_battery_2 = "/proc/acpi/battery/BAT1/state"
 Finfosys.TabStrip1[9].Visible = True
 battery._get_inf()
 battery._replace_n()
 Finfosys.battery_info()
Endif
If Exist("/sys/class/power_supply/BAT1/", True) Then
 Finfosys.TabStrip1[11].Visible = True
 battery._get_inf_other()
 battery._replace_n()
 Finfosys.battery_info()
Endif
End

Public Sub _get_inf()
 
 Shell "grep 'present:' " & addres_battery & " | cut -d ':' -f 2" Wait To Present
 Shell "grep 'design capacity:' " & addres_battery & " | cut -d ':' -f 2" Wait To Design_capacity
 Shell "grep 'last full capacity:' " & addres_battery & " | cut -d ':' -f 2" Wait To last_full_capacity
 Shell "grep 'battery technology:' " & addres_battery & " | cut -d ':' -f 2" Wait To battery_technology
 Shell "grep 'design voltage:' " & addres_battery & " | cut -d ':' -f 2" Wait To design_voltage
 Shell "grep 'design capacity warning:' " & addres_battery & " | cut -d ':' -f 2" Wait To design_capacity_warning
 Shell "grep 'design capacity low:' " & addres_battery & " | cut -d ':' -f 2" Wait To design_capacity_low
 Shell "grep 'cycle count:' " & addres_battery & " | cut -d ':' -f 2" Wait To cycle_count
 Shell "grep 'capacity granularity 1:' " & addres_battery & " | cut -d ':' -f 2" Wait To capacity_granularity_1
 Shell "grep 'capacity granularity 2:' " & addres_battery & " | cut -d ':' -f 2" Wait To capacity_granularity_2
 Shell "grep 'model number:' " & addres_battery & " | cut -d ':' -f 2" Wait To model_number
 Shell "grep 'serial number:' " & addres_battery & " | cut -d ':' -f 2" Wait To serial_number
 Shell "grep 'battery type:' " & addres_battery & " | cut -d ':' -f 2" Wait To battery_type
 Shell "grep 'OEM info:' " & addres_battery & " | cut -d ':' -f 2" Wait To OEM_info
 Shell "grep 'last full capacity:' " & addres_battery & " | cut -d ':' -f 2" Wait To calculate_percentage_max
 Shell "grep 'remaining capacity:' " & addres_battery_2 & " | cut -d ':' -f 2" Wait To calculate_percentage

End

Public Sub _get_inf_other()

 Try alarm = Replace(File.Load("/sys/class/power_supply/BAT1/alarm"), "\n", "")
 Try capacity = Replace(File.Load("/sys/class/power_supply/BAT1/capacity"), "\n", "")
 Try charge_full = Replace(File.Load("/sys/class/power_supply/BAT1/charge_full"), "\n", "")
 Try charge_full_design = Replace(File.Load("/sys/class/power_supply/BAT1/charge_full_design"), "\n", "")
 Try charge_now = Replace(File.Load("/sys/class/power_supply/BAT1/charge_now"), "\n", "")
 Try current_now = Replace(File.Load("/sys/class/power_supply/BAT1/current_now"), "\n", "")
 Try cycle_count = Replace(File.Load("/sys/class/power_supply/BAT1/cycle_count"), "\n", "")
 Try manufacturer = Replace(File.Load("/sys/class/power_supply/BAT1/manufacturer"), "\n", "")
 Try model_name = Replace(File.Load("/sys/class/power_supply/BAT1/model_name"), "\n", "")
 Try Present = Replace(File.Load("/sys/class/power_supply/BAT1/present"), "\n", "")
 Try serial_number = Replace(File.Load("/sys/class/power_supply/BAT1/serial_number"), "\n", "")
 Try status = Replace(File.Load("/sys/class/power_supply/BAT1/status"), "\n", "")
 Try technology = Replace(File.Load("/sys/class/power_supply/BAT1/technology"), "\n", "")
 Try battery_type = Replace(File.Load("/sys/class/power_supply/BAT1/type"), "\n", "")
 Try voltage_min_design = Replace(File.Load("/sys/class/power_supply/BAT1/voltage_min_design"), "\n", "")
 Try voltage_now = Replace(File.Load("/sys/class/power_supply/BAT1/voltage_now"), "\n", "")

End

Public Sub _replace_n()
 Present = Replace(LTrim$(Present), "\n", "")
 Design_capacity = Replace(LTrim$(Design_capacity), "\n", "")
 last_full_capacity = Replace(LTrim$(last_full_capacity), "\n", "")
 battery_technology = Replace(LTrim$(battery_technology), "\n", "")
 design_voltage = Replace(LTrim$(design_voltage), "\n", "")
 design_capacity_warning = Replace(LTrim$(design_capacity_warning), "\n", "")
 design_capacity_low = Replace(LTrim$(design_capacity_low), "\n", "")
 cycle_count = Replace(LTrim$(cycle_count), "\n", "")
 capacity_granularity_1 = Replace(LTrim$(capacity_granularity_1), "\n", "")
 capacity_granularity_2 = Replace(LTrim$(capacity_granularity_2), "\n", "")
 model_number = Replace(LTrim$(model_number), "\n", "")
 serial_number = Replace(LTrim$(serial_number), "\n", "")
 battery_type = Replace(LTrim$(battery_type), "\n", "")
 OEM_info = Replace(LTrim$(OEM_info), "\n", "")
 'Calculate battery
 calculate_percentage = Replace(Replace(Replace(calculate_percentage, "mAh", ""), " ", ""), "\n", "")
 calculate_percentage_max = Replace(Replace(Replace(calculate_percentage_max, "mAh", ""), " ", ""), "\n", "")
 Try Finfosys.ProgressBar3.Value = calculate_percentage / calculate_percentage_max
 Try Finfosys.ProgressBar5.Value = CInteger(Replace(capacity, "\n", "")) / 100
End

