Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test #4

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ plugins {

android {
compileSdkVersion 29
buildToolsVersion "30.0.2"

defaultConfig {
applicationId "de.jnns.bmsmonitor"
Expand Down Expand Up @@ -38,6 +37,7 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
namespace 'de.jnns.bmsmonitor'
}

dependencies {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.jnns.bmsmonitor">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Expand Down
181 changes: 105 additions & 76 deletions app/src/main/java/de/jnns/bmsmonitor/BatteryFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ class BatteryFragment : Fragment() {
override fun onReceive(context: Context, intent: Intent) {
try {
val msg: String = intent.getStringExtra("batteryData")!!

binding.labelStatus.text = String.format(resources.getString(R.string.connectedToBms), intent.getStringExtra("deviceName"))

if (msg.isNotEmpty()) {
updateUi(Gson().fromJson(msg, BatteryData::class.java))
}
Expand Down Expand Up @@ -98,31 +95,58 @@ class BatteryFragment : Fragment() {
minCellVoltage = PreferenceManager.getDefaultSharedPreferences(requireContext()).getString("minCellVoltage", "2500")!!.toInt()
maxCellVoltage = PreferenceManager.getDefaultSharedPreferences(requireContext()).getString("maxCellVoltage", "4200")!!.toInt()

binding.barchartCells.setNoDataTextColor(requireActivity().getColor(R.color.white))
binding.barchartCells.setNoDataText("...")
binding.barchartCells1.setNoDataTextColor(requireActivity().getColor(R.color.white))
binding.barchartCells1.setNoDataText("...")

binding.barchartCells1.setPinchZoom(false)
binding.barchartCells1.setTouchEnabled(false)
binding.barchartCells1.isClickable = false
binding.barchartCells1.isDoubleTapToZoomEnabled = false

binding.barchartCells1.setDrawBorders(false)
binding.barchartCells1.setDrawValueAboveBar(true)
binding.barchartCells1.setDrawBorders(false)
binding.barchartCells1.setDrawGridBackground(false)

binding.barchartCells1.description.isEnabled = false
binding.barchartCells1.legend.isEnabled = false

binding.barchartCells1.axisLeft.setDrawGridLines(false)
binding.barchartCells1.axisLeft.setDrawLabels(false)
binding.barchartCells1.axisLeft.setDrawAxisLine(false)

binding.barchartCells1.xAxis.setDrawGridLines(false)
binding.barchartCells1.xAxis.setDrawLabels(false)
binding.barchartCells1.xAxis.setDrawAxisLine(false)

binding.barchartCells1.axisRight.isEnabled = false


binding.barchartCells.setPinchZoom(false)
binding.barchartCells.setTouchEnabled(false)
binding.barchartCells.isClickable = false
binding.barchartCells.isDoubleTapToZoomEnabled = false
binding.barchartCells2.setNoDataTextColor(requireActivity().getColor(R.color.white))
binding.barchartCells2.setNoDataText("...")

binding.barchartCells.setDrawBorders(false)
binding.barchartCells.setDrawValueAboveBar(true)
binding.barchartCells.setDrawBorders(false)
binding.barchartCells.setDrawGridBackground(false)
binding.barchartCells2.setPinchZoom(false)
binding.barchartCells2.setTouchEnabled(false)
binding.barchartCells2.isClickable = false
binding.barchartCells2.isDoubleTapToZoomEnabled = false

binding.barchartCells.description.isEnabled = false
binding.barchartCells.legend.isEnabled = false
binding.barchartCells2.setDrawBorders(false)
binding.barchartCells2.setDrawValueAboveBar(true)
binding.barchartCells2.setDrawBorders(false)
binding.barchartCells2.setDrawGridBackground(false)

binding.barchartCells.axisLeft.setDrawGridLines(false)
binding.barchartCells.axisLeft.setDrawLabels(false)
binding.barchartCells.axisLeft.setDrawAxisLine(false)
binding.barchartCells2.description.isEnabled = false
binding.barchartCells2.legend.isEnabled = false

binding.barchartCells.xAxis.setDrawGridLines(false)
binding.barchartCells.xAxis.setDrawLabels(false)
binding.barchartCells.xAxis.setDrawAxisLine(false)
binding.barchartCells2.axisLeft.setDrawGridLines(false)
binding.barchartCells2.axisLeft.setDrawLabels(false)
binding.barchartCells2.axisLeft.setDrawAxisLine(false)

binding.barchartCells.axisRight.isEnabled = false
binding.barchartCells2.xAxis.setDrawGridLines(false)
binding.barchartCells2.xAxis.setDrawLabels(false)
binding.barchartCells2.xAxis.setDrawAxisLine(false)

binding.barchartCells2.axisRight.isEnabled = false
}

override fun onResume() {
Expand All @@ -145,84 +169,89 @@ class BatteryFragment : Fragment() {
}

requireActivity().runOnUiThread {
binding.labelStatus.text = String.format(resources.getString(R.string.connectedToBms), batteryData.bpVersion, batteryData.bpNumber)

// Power Gauge
val powerUsage = batteryData.power * -1.0f
val powerUsage = batteryData.packCurrent * batteryData.packVoltage

binding.speedViewSpeed.speedTo(powerUsage, 1000)
var chargePower:Float = 0.0f

if (powerUsage < 0.0f) {
binding.labelPower.text = "+${powerUsage.roundToInt() * -1}"
if(batteryData.packCurrent >= 0.0f)
{
chargePower = batteryData.packCurrent / batteryData.maxChargeCurrent
chargePower = chargePower * -500
} else {
binding.labelPower.text = powerUsage.roundToInt().toString()
}

// Remaining Time
wattValues[smoothIndex] = batteryData.power
++smoothIndex

if (smoothIndex == smoothCount) {
smoothIndex = 0
chargePower = batteryData.packCurrent / batteryData.maxDischargeCurrent
chargePower = chargePower * -1000
}

var averagePower = wattValues.filter { x -> x != 0.0f }.average().toFloat()
binding.speedViewSpeed.speedTo(chargePower, 1000)

if (averagePower < 0.0f) {
averagePower *= -1
}

val wattSeconds: Float = when {
batteryData.power < 0.0f -> {
(batteryData.watthours / averagePower) * 3600
}
batteryData.power > 0.0f -> {
((batteryData.totalWatthours - batteryData.watthours) / averagePower) * 3600
}
else -> {
0.0f
}
}

if (batteryData.power == 0.0f) {
wattValues.fill(0.0f)
if (powerUsage < 0.0f) {
binding.labelPower.text = "+${powerUsage.roundToInt() * -1}"
} else {
binding.labelPower.text = powerUsage.roundToInt().toString()
}

val remainingMinutes = (wattSeconds % 3600) / 60
val remainingHours = (wattSeconds - remainingMinutes) / 3600

binding.labelTime.text = String.format("%02d:%02d", remainingHours.roundToInt(), remainingMinutes.roundToInt())
binding.labelCycle.text = String.format("Cycle:%s", batteryData.packDischargCycle.toString())

// Cell Bar-Diagram
val cellBars = ArrayList<BarEntry>()

for ((i, cell) in batteryData.cellVoltages.withIndex()) {
cellBars.add(BarEntry(i.toFloat(), cell))
}
cellBars.add(BarEntry(0.toFloat(), batteryData.cellVoltage1))
cellBars.add(BarEntry(1.toFloat(), batteryData.cellVoltage2))
cellBars.add(BarEntry(2.toFloat(), batteryData.cellVoltage3))
cellBars.add(BarEntry(3.toFloat(), batteryData.cellVoltage4))
cellBars.add(BarEntry(4.toFloat(), batteryData.cellVoltage5))
cellBars.add(BarEntry(5.toFloat(), batteryData.cellVoltage6))
cellBars.add(BarEntry(6.toFloat(), batteryData.cellVoltage7))
cellBars.add(BarEntry(7.toFloat(), batteryData.cellVoltage8))

val barDataSetVoltage = BarDataSet(cellBars, "Cell Voltages")
barDataSetVoltage.valueTextColor = requireActivity().getColor(R.color.white)
barDataSetVoltage.valueTextSize = 12.0f
barDataSetVoltage.valueFormatter = DefaultValueFormatter(2)
barDataSetVoltage.valueFormatter = DefaultValueFormatter(3)
barDataSetVoltage.setColors(requireActivity().getColor(R.color.primary))

val barData = BarData(barDataSetVoltage)
binding.barchartCells.data = barData
binding.barchartCells.invalidate()

// Row 1
val totalPercentage = ((batteryData.percentage) * 1000.0f).roundToInt() / 10.0f
binding.barchartCells1.data = barData
binding.barchartCells1.invalidate()

val cellBars1 = ArrayList<BarEntry>()

cellBars1.add(BarEntry(8.toFloat(), batteryData.cellVoltage9))
cellBars1.add(BarEntry(9.toFloat(), batteryData.cellVoltage10))
cellBars1.add(BarEntry(10.toFloat(), batteryData.cellVoltage11))
cellBars1.add(BarEntry(11.toFloat(), batteryData.cellVoltage12))
cellBars1.add(BarEntry(12.toFloat(), batteryData.cellVoltage13))
cellBars1.add(BarEntry(13.toFloat(), batteryData.cellVoltage14))
cellBars1.add(BarEntry(14.toFloat(), batteryData.cellVoltage15))
cellBars1.add(BarEntry(15.toFloat(), batteryData.cellVoltage16))

val barDataSetVoltage1 = BarDataSet(cellBars, "Cell Voltages")
barDataSetVoltage1.valueTextColor = requireActivity().getColor(R.color.white)
barDataSetVoltage1.valueTextSize = 12.0f
barDataSetVoltage1.valueFormatter = DefaultValueFormatter(3)
barDataSetVoltage1.setColors(requireActivity().getColor(R.color.primary))

val barData1 = BarData(barDataSetVoltage)
binding.barchartCells2.data = barData1
binding.barchartCells2.invalidate()

var totalPercentage = batteryData.packRSOC.toUByte()
if(totalPercentage > 100U) {
totalPercentage = 100U
}

binding.labelVoltage.text = roundTo(batteryData.voltage, 1).toString()
binding.labelVoltage.text = roundTo(batteryData.packVoltage, 1).toString()
binding.labelPercentage.text = totalPercentage.toString()

uiBatteryCapacityBar(totalPercentage)
uiBatteryCapacityBar(totalPercentage.toFloat())

// Row 2
binding.labelCurrent.text = String.format(Locale.US, "%.1f", roundTo(batteryData.current, 1))
binding.labelCapacityWh.text = batteryData.watthours.roundToInt().toString()
binding.labelCurrent.text = roundTo(batteryData.packCurrent, 1).toString()
binding.labelCapacityWh.text = roundTo(batteryData.capacity, 1).toString()

// Row 3
binding.labelTemperature.text = roundTo(batteryData.avgTemperature, 1).toString()
binding.labelTemperatureMax.text = roundTo(batteryData.maxTemperature, 1).toString()
binding.labelTemperature.text = roundTo(batteryData.sysTemperature, 1).toString()
binding.labelTemperatureMax.text = roundTo(batteryData.heatsinkTemperature, 1).toString()
}
}

Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/de/jnns/bmsmonitor/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package de.jnns.bmsmonitor

import android.Manifest
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.navigation.findNavController
import androidx.preference.PreferenceManager
import de.jnns.bmsmonitor.bluetooth.BleService
import de.jnns.bmsmonitor.databinding.ActivityMainBinding
//import de.jnns.bmsmonitor.BatteryFragment1
import de.jnns.bmsmonitor.BatteryFragment
import de.jnns.bmsmonitor.services.BikeService
import de.jnns.bmsmonitor.services.BmsService
import io.realm.Realm
Expand All @@ -22,7 +26,7 @@ class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Log.d("BMS", "MainActivity Start...")
Realm.init(this)
Realm.compactRealm(Realm.getDefaultConfiguration()!!)

Expand All @@ -33,7 +37,8 @@ class MainActivity : AppCompatActivity() {
_binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

val batteryEnabled = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("batteryEnabled", false)
// val batteryEnabled = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("batteryEnabled", false)
val batteryEnabled = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("batteryEnabled", true)
val bikeEnabled = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("bikeEnabled", false)

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Expand Down Expand Up @@ -77,6 +82,7 @@ class MainActivity : AppCompatActivity() {
when (requestCode) {
4711 -> {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Intent(this, BleService::class.java).also { intent -> startService(intent) }
Intent(this, BmsService::class.java).also { intent ->
startService(intent)
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/de/jnns/bmsmonitor/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ class SettingsFragment : PreferenceFragmentCompat() {
super.onViewCreated(view, savedInstanceState)

val btPreference = findPreference<ListPreference>("macAddress") as ListPreference
val btPreferenceBike = findPreference<ListPreference>("macAddressBike") as ListPreference
// val btPreferenceBike = findPreference<ListPreference>("macAddressBike") as ListPreference

val bleNames = BleManager.i.getBleNames()
bleNames.add("None")
btPreference.entries = bleNames.toTypedArray()
btPreferenceBike.entries = bleNames.toTypedArray()
// btPreferenceBike.entries = bleNames.toTypedArray()

val bleAddresses = BleManager.i.getBleAddresses()
bleAddresses.add("0")
btPreference.entryValues = bleAddresses.toTypedArray()
btPreferenceBike.entryValues = bleAddresses.toTypedArray()
// btPreferenceBike.entryValues = bleAddresses.toTypedArray()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,9 @@ class BikeGattClientCallback(
super.onCharacteristicChanged(gatt, characteristic)

if (characteristic.uuid == lcdToMcuUuid) {
// Log.d("BluetoothGatt", "FrameData (LCD): " + characteristic.value.toHexString())

val data = LcdToMcuResponse(characteristic.value)
onLcdToMcuDataAvailable(data)
} else if (characteristic.uuid == mcuToLcdUuid) {
// Log.d("BluetoothGatt", "FrameData (MCU): " + characteristic.value.toHexString())

val data = McuToLcdResponse(characteristic.value)
onMcuToLcdDataAvailable(data)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.jnns.bmsmonitor.bluetooth

import android.bluetooth.BluetoothDevice
import android.util.Log
import kotlinx.coroutines.Runnable

class BleManager {
Expand Down
Loading