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
Prev Previous commit
Next Next commit
Developing
  • Loading branch information
jchenglee37 committed Aug 31, 2023
commit a15c6c6304c09d0007ef70d16c15c4787ed865a9
628 changes: 346 additions & 282 deletions app/src/main/java/de/jnns/bmsmonitor/BatteryFragment.kt

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions app/src/main/java/de/jnns/bmsmonitor/BatteryFragment1.kt
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 158,12 @@ class BatteryFragment1 : Fragment() {
// var chargeCurrent: Float = 0.0f
// var maxChargeCurrent: Float = 0.0f

binding.labelCycle.text = batteryData.cycles.toString()
binding.labelPackCapacity.text = roundTo(batteryData.capacity, 1).toString()
binding.labelDischargeCurrent.text = roundTo(batteryData.dischargeCurrent, 1).toString()
binding.labelPackVoltage.text = roundTo(batteryData.packVoltage, 1).toString()
binding.labelChargeCurrent.text = roundTo(batteryData.chargeCurrent, 1).toString()
binding.labelMaxCC.text = roundTo(batteryData.maxChargeCurrent, 1).toString()
// binding.labelCycle.text = batteryData.cycles.toString()
// binding.labelPackCapacity.text = roundTo(batteryData.capacity, 1).toString()
// binding.labelDischargeCurrent.text = roundTo(batteryData.dischargeCurrent, 1).toString()
// binding.labelPackVoltage.text = roundTo(batteryData.packVoltage, 1).toString()
// binding.labelChargeCurrent.text = roundTo(batteryData.chargeCurrent, 1).toString()
// binding.labelMaxCC.text = roundTo(batteryData.maxChargeCurrent, 1).toString()

// // Power Gauge
// val powerUsage = batteryData.power * -1.0f
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/java/de/jnns/bmsmonitor/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 12,8 @@ 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.BatteryFragment1
import de.jnns.bmsmonitor.BatteryFragment
import de.jnns.bmsmonitor.services.BikeService
import de.jnns.bmsmonitor.services.BmsService
import io.realm.Realm
Expand Down Expand Up @@ -54,16 55,16 @@ class MainActivity : AppCompatActivity() {
Intent(this, BleService::class.java).also { intent -> startService(intent) }
}

// val batteryFragment = BatteryFragment()
val batteryFragment1 = BatteryFragment1()
val batteryFragment = BatteryFragment()
// val batteryFragment1 = BatteryFragment1()
val bikeFragment = BikeFragment()
val statsFragment = StatsFragment()
val settingsFragment = SettingsFragment()

binding.bottomNavigation.setOnNavigationItemSelectedListener {
when (it.itemId) {
// R.id.page_battery -> supportFragmentManager.beginTransaction().apply { replace(R.id.nav_host_fragment, batteryFragment).commit() }
R.id.page_battery -> supportFragmentManager.beginTransaction().apply { replace(R.id.nav_host_fragment, batteryFragment1).commit() }
R.id.page_battery -> supportFragmentManager.beginTransaction().apply { replace(R.id.nav_host_fragment, batteryFragment).commit() }
// R.id.page_battery -> supportFragmentManager.beginTransaction().apply { replace(R.id.nav_host_fragment, batteryFragment1).commit() }
R.id.page_bike -> supportFragmentManager.beginTransaction().apply { replace(R.id.nav_host_fragment, bikeFragment).commit() }
R.id.page_stats -> supportFragmentManager.beginTransaction().apply { replace(R.id.nav_host_fragment, statsFragment).commit() }
R.id.page_settings -> supportFragmentManager.beginTransaction().apply { replace(R.id.nav_host_fragment, settingsFragment).commit() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 97,59 @@ class BmsGattClientCallback(
fun isInTransaction(): Boolean {
return isInTrans
}
// override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
// super.onCharacteristicChanged(gatt, characteristic)
//
// // Log.d("BluetoothGatt", "BLE Data (" characteristic.value.size "): " characteristic.value.toHexString())
//
// for (byte: Byte in characteristic.value) {
// if (uartBufferPos >= bufferSize) {
// isInFrame = false
// uartBufferPos = 0
// uartBytesLeft = 0
// }
//
// uartBuffer[uartBufferPos] = byte
//
// if (isInFrame) {
// if (uartBufferPos == 3) {
// uartBytesLeft = byte.toInt()
// }
//
// if (byte.toUByte() == 0x77.toUByte() && uartBytesLeft < 1) {
// isInFrame = false
// onFrameComplete(uartBufferPos)
// uartBufferPos = 0
// uartBytesLeft = 0
// } else {
//
// uartBufferPos
// uartBytesLeft--
// }
// } else if (byte.toUByte() == 0xDD.toUByte()) {
// isInFrame = true
// uartBufferPos
// }
// }
// }
//
// private fun onFrameComplete(size: Int) {
// if (size <= 0) {
// return
// }
//
// val frameBytes = uartBuffer.slice(IntRange(0, size)).toByteArray()
//
// // Log.d("BluetoothGatt", "FrameData (" frameBytes.size "): " frameBytes.toHexString())
//
// if (frameBytes[1] == 0x3.toByte()) {
// val generalInfo = BmsGeneralInfoResponse(frameBytes)
// onGeneralInfoCallback(generalInfo)
// } else if (frameBytes[1] == 0x4.toByte()) {
// val cellInfo = BmsCellInfoResponse(frameBytes)
// onCellInfoCallback(cellInfo)
// }
// }

override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
super.onCharacteristicChanged(gatt, characteristic)
Expand Down
62 changes: 52 additions & 10 deletions app/src/main/java/de/jnns/bmsmonitor/bms/BmsGeneralInfoResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 15,34 @@ class BmsGeneralInfoResponse(bytes: ByteArray) {
// var temperatureProbeCount: Int = 0
// var temperatureProbeValues: RealmList<Float>

var cycles: Short = 0
var capacity: Float = 0.0f
var temperature: Float = 0.0f
var dischargeCurrent: Float = 0.0f
var sysTemperature: Float = 0.0f
var packCurrent: Float = 0.0f
var packVoltage: Float = 0.0f
var chargeCurrent: Float = 0.0f
var maxDischargeCurrent: Float = 0.0f
var maxChargeCurrent: Float = 0.0f
var bpVersion: Byte = 0
var bpNumber: Byte = 0
var packRSOC: Byte = 0
var packDischargCycle: Short = 0
var heatsinkTemperature: Float = 0.0f
var cellVoltage1: Float = 0.0f
var cellVoltage2: Float = 0.0f
var cellVoltage3: Float = 0.0f
var cellVoltage4: Float = 0.0f
var cellVoltage5: Float = 0.0f
var cellVoltage6: Float = 0.0f
var cellVoltage7: Float = 0.0f
var cellVoltage8: Float = 0.0f
var cellVoltage9: Float = 0.0f
var cellVoltage10: Float = 0.0f
var cellVoltage11: Float = 0.0f
var cellVoltage12: Float = 0.0f
var cellVoltage13: Float = 0.0f
var cellVoltage14: Float = 0.0f
var cellVoltage15: Float = 0.0f
var cellVoltage16: Float = 0.0f


init {
// command = bytes[1].toInt()
Expand Down Expand Up @@ -56,16 77,37 @@ class BmsGeneralInfoResponse(bytes: ByteArray) {
// temperatureProbeValues.add((bytesToShort(bytes[27 (i * 2)], bytes[27 (i * 2) 1]) - 2731) / 10.0f)
// }

packVoltage = bytesToShort(bytes[8], bytes[9]) / 100.0f
dischargeCurrent = bytesToShort(bytes[10], bytes[11]) / 10.0f
cycles = bytesToShort(bytes[6], bytes[7])
bpVersion = bytes[0]
bpNumber = bytes[1]
packRSOC = bytes[2]
capacity = bytesToShort(bytes[4], bytes[5]) / 10.0f
chargeCurrent = bytesToShort(bytes[12], bytes[13]) / 10.0f
maxChargeCurrent = bytesToShort(bytes[14], bytes[15]) / 10.0f
packVoltage = bytesToShort(bytes[8], bytes[9]) / 100.0f
packCurrent = bytesToShort(bytes[10], bytes[11]) / 100.0f
maxDischargeCurrent = bytesToShort(bytes[12], bytes[13]) / 100.0f
maxChargeCurrent = bytesToShort(bytes[14], bytes[15]) / 100.0f

packDischargCycle = bytesToShort(bytes[74], bytes[75])

// System temperature
temperature = bytesToShort(bytes[24], bytes[25]) / 10.0f
sysTemperature = bytesToShort(bytes[52], bytes[53]) / 100.0f
heatsinkTemperature = bytesToShort(bytes[54], bytes[55]) / 100.0f

cellVoltage1 = bytesToShort(bytes[96], bytes[97]) / 1000.0f
cellVoltage2 = bytesToShort(bytes[98], bytes[99]) / 1000.0f
cellVoltage3 = bytesToShort(bytes[100], bytes[101]) / 1000.0f
cellVoltage4 = bytesToShort(bytes[102], bytes[103]) / 1000.0f
cellVoltage5 = bytesToShort(bytes[104], bytes[105]) / 1000.0f
cellVoltage6 = bytesToShort(bytes[106], bytes[107]) / 1000.0f
cellVoltage7 = bytesToShort(bytes[108], bytes[109]) / 1000.0f
cellVoltage8 = bytesToShort(bytes[110], bytes[111]) / 1000.0f
cellVoltage9 = bytesToShort(bytes[112], bytes[113]) / 1000.0f
cellVoltage10 = bytesToShort(bytes[114], bytes[115]) / 1000.0f
cellVoltage11 = bytesToShort(bytes[116], bytes[117]) / 1000.0f
cellVoltage12 = bytesToShort(bytes[118], bytes[119]) / 1000.0f
cellVoltage13 = bytesToShort(bytes[120], bytes[121]) / 1000.0f
cellVoltage14 = bytesToShort(bytes[122], bytes[123]) / 1000.0f
cellVoltage15 = bytesToShort(bytes[124], bytes[125]) / 1000.0f
cellVoltage16 = bytesToShort(bytes[126], bytes[127]) / 1000.0f
}

private fun bytesToShort(h: Byte, l: Byte, order: ByteOrder = ByteOrder.BIG_ENDIAN): Short {
Expand Down
44 changes: 37 additions & 7 deletions app/src/main/java/de/jnns/bmsmonitor/data/BatteryData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 15,44 @@ open class BatteryData(
var temperatures: RealmList<Float> = RealmList<Float>(),
var cellVoltages: RealmList<Float> = RealmList<Float>(),

var cycles: Short = 0,
var capacity: Float = 0.0f,
var temperature: Float = 0.0f,
var dischargeCurrent: Float = 0.0f,
var packVoltage: Float = 0.0f,
var chargeCurrent: Float = 0.0f,
var maxChargeCurrent: Float = 0.0f
// var cycles: Short = 0,
// var capacity: Float = 0.0f,
// var temperature: Float = 0.0f,
// var dischargeCurrent: Float = 0.0f,
// var packVoltage: Float = 0.0f,
// var chargeCurrent: Float = 0.0f,
// var maxChargeCurrent: Float = 0.0f


) : RealmObject() {
var capacity: Float = 0.0f
var sysTemperature: Float = 0.0f
var packCurrent: Float = 0.0f
var packVoltage: Float = 0.0f
var maxDischargeCurrent: Float = 0.0f
var maxChargeCurrent: Float = 0.0f
var bpVersion: Byte = 0
var bpNumber: Byte = 0
var packRSOC: Byte = 0
var packDischargCycle: Short = 0
var heatsinkTemperature: Float = 0.0f
var cellVoltage1: Float = 0.0f
var cellVoltage2: Float = 0.0f
var cellVoltage3: Float = 0.0f
var cellVoltage4: Float = 0.0f
var cellVoltage5: Float = 0.0f
var cellVoltage6: Float = 0.0f
var cellVoltage7: Float = 0.0f
var cellVoltage8: Float = 0.0f
var cellVoltage9: Float = 0.0f
var cellVoltage10: Float = 0.0f
var cellVoltage11: Float = 0.0f
var cellVoltage12: Float = 0.0f
var cellVoltage13: Float = 0.0f
var cellVoltage14: Float = 0.0f
var cellVoltage15: Float = 0.0f
var cellVoltage16: Float = 0.0f

val voltage: Float
get() {
return cellVoltages.sum()
Expand Down
39 changes: 34 additions & 5 deletions app/src/main/java/de/jnns/bmsmonitor/services/BmsService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 107,42 @@ class BmsService : Service() {
// batteryData.temperatureCount = generalInfo.temperatureProbeCount
// batteryData.temperatures = generalInfo.temperatureProbeValues
// batteryData.vol = generalInfo.totalVoltage
batteryData.packVoltage = generalInfo.packVoltage
batteryData.dischargeCurrent = generalInfo.dischargeCurrent

batteryData.capacity = generalInfo.capacity
batteryData.cycles = generalInfo.cycles
batteryData.chargeCurrent = generalInfo.chargeCurrent
batteryData.sysTemperature = generalInfo.sysTemperature
batteryData.packCurrent = generalInfo.packCurrent
batteryData.packVoltage = generalInfo.packVoltage
batteryData.maxDischargeCurrent = generalInfo.maxDischargeCurrent
batteryData.maxChargeCurrent = generalInfo.maxChargeCurrent
batteryData.temperature = generalInfo.temperature
batteryData.bpVersion = generalInfo.bpVersion
batteryData.bpNumber = generalInfo.bpNumber
batteryData.packRSOC = generalInfo.packRSOC
batteryData.packDischargCycle = generalInfo.packDischargCycle
batteryData.heatsinkTemperature = generalInfo.heatsinkTemperature
batteryData.cellVoltage1 = generalInfo.cellVoltage1
batteryData.cellVoltage2 = generalInfo.cellVoltage2
batteryData.cellVoltage3 = generalInfo.cellVoltage3
batteryData.cellVoltage4 = generalInfo.cellVoltage4
batteryData.cellVoltage5 = generalInfo.cellVoltage5
batteryData.cellVoltage6 = generalInfo.cellVoltage6
batteryData.cellVoltage7 = generalInfo.cellVoltage7
batteryData.cellVoltage8 = generalInfo.cellVoltage8
batteryData.cellVoltage9 = generalInfo.cellVoltage9
batteryData.cellVoltage10 = generalInfo.cellVoltage10
batteryData.cellVoltage11 = generalInfo.cellVoltage11
batteryData.cellVoltage12 = generalInfo.cellVoltage12
batteryData.cellVoltage13 = generalInfo.cellVoltage13
batteryData.cellVoltage14 = generalInfo.cellVoltage14
batteryData.cellVoltage15 = generalInfo.cellVoltage15
batteryData.cellVoltage16 = generalInfo.cellVoltage16

// batteryData.packVoltage = generalInfo.packVoltage
// batteryData.dischargeCurrent = generalInfo.dischargeCurrent
// batteryData.capacity = generalInfo.capacity
// batteryData.cycles = generalInfo.cycles
// batteryData.chargeCurrent = generalInfo.chargeCurrent
// batteryData.maxChargeCurrent = generalInfo.maxChargeCurrent
// batteryData.temperature = generalInfo.temperature

generalInfoReceived = true
sendData()
Expand Down
Loading