Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
Add initial BS party mode!
Browse files Browse the repository at this point in the history
  • Loading branch information
rnd-ash committed Nov 27, 2020
1 parent 6db1f63 commit 3fc0a3d
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 121 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/rndash/mbheadunit/CarComm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class CarComm(device: UsbDevice, manager: UsbManager) {
nativeBA = CanBusNative.getSendFrame()
if (nativeBA != null) {
//println(nativeBA.joinToString(" ") { x -> String.format("%02X", x) })
serialDevice?.write(nativeBA, 10)
serialDevice?.write(nativeBA, 100)
txBytes += nativeBA[3].toInt() + extra_bytes
} else {
Thread.sleep(25)
Thread.sleep(5)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/rndash/mbheadunit/CarData.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.rndash.mbheadunit

import android.content.Intent
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.rndash.mbheadunit.car.PartyMode
import com.rndash.mbheadunit.nativeCan.canC.KOMBI_412h
import com.rndash.mbheadunit.nativeCan.canC.MS_608h
import kotlin.properties.Delegates

@ExperimentalUnsignedTypes
@ExperimentalStdlibApi
Expand All @@ -11,6 +14,7 @@ object CarData {
var currSpd = 0
var tripFuelUsed = 0L
var fuelCurrent = 0.0
var isSportFeel: Boolean = false

val dataCollector = Thread() {
while(true) {
Expand Down
22 changes: 10 additions & 12 deletions app/src/main/java/com/rndash/mbheadunit/FullscreenActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,18 @@ class FullscreenActivity : FragmentActivity() {
//val mbux = MBUXDialog(this)
//mbux.show()
val bg = findViewById<ImageView>(R.id.ui_bg)
Thread {
while(true) {
when (Calendar.getInstance().get(Calendar.HOUR_OF_DAY)) {
in 5..7 -> runOnUiThread { bg.setImageResource(R.drawable.bg_dusk) }
in 7..9 -> runOnUiThread { bg.setImageResource(R.drawable.bg_mid) }
in 9..11 -> runOnUiThread { bg.setImageResource(R.drawable.bg_rise) }
in 11..15 -> runOnUiThread { bg.setImageResource(R.drawable.bg_day) }
in 15..18 -> runOnUiThread { bg.setImageResource(R.drawable.bg_mid) }
in 18..21 -> runOnUiThread { bg.setImageResource(R.drawable.bg_dusk) }
else -> runOnUiThread { bg.setImageResource(R.drawable.bg_night) }
Timer().schedule(object: TimerTask() {
override fun run() {
runOnUiThread {
if (CarData.isSportFeel) {
bg.setImageResource(R.drawable.bg_sport)
} else {
bg.setImageResource(R.drawable.bg_normal)
}
}
Thread.sleep(60000)
}
}.start()
}, 0, 500)

// Register for all Microntek intents
val intentFilter = IntentFilter()
intentFilter.addAction("com.microntek.bootcheck")
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/rndash/mbheadunit/IOManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ open class IOManager(readBufSize: Int, private val serialPort: UsbSerialPort) :
check(state == ManagerState.STOPPED) { "Already running" }
state = ManagerState.RUNNING
}

Log.i("IOManager", "Running ...")
try {
while (true) {
Expand Down Expand Up @@ -63,7 +62,7 @@ open class IOManager(readBufSize: Int, private val serialPort: UsbSerialPort) :

fun step() {
// Handle incoming data.
var len = serialPort.read(readBuffer.array(), 100)
var len = serialPort.read(readBuffer.array(), 50)
if (len > 0) {
CanBusNative.sendBytesToBuffer(readBuffer.array(), len)
readBuffer.clear()
Expand Down
25 changes: 14 additions & 11 deletions app/src/main/java/com/rndash/mbheadunit/car/PartyMode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.rndash.mbheadunit.CarComm
import com.rndash.mbheadunit.nativeCan.canB.EZS_A2
import com.rndash.mbheadunit.nativeCan.canB.SAM_H_A3
import com.rndash.mbheadunit.nativeCan.canB.SAM_H_A5
import java.lang.Long.max

/**
* Object that deals with special effects such as disco lights on the car
Expand Down Expand Up @@ -45,15 +46,17 @@ object PartyMode {
private fun threadLoop() {
//BTMusic.setupSampler()
println("Party mode thread start!")
var millis: Long
while(true) {
if (!isEngineOn()) {
millis = System.currentTimeMillis()
// -- DO BLINKER STUFF --
if (millis() >= leftInidicatorCutoff && leftIndicatorOn) {
if (millis >= leftInidicatorCutoff && leftIndicatorOn) {
leftIndicatorOn = false
blinkerStateChange = true
}
// Do i turn off the right indicator?
if (millis() >= rightIndicatorCutOff && rightIndicatorOn) {
if (millis >= rightIndicatorCutOff && rightIndicatorOn) {
rightIndicatorOn = false
blinkerStateChange = true
}
Expand All @@ -64,7 +67,10 @@ object PartyMode {
SAM_H_A3.set_bli_li_ein(sha3, leftIndicatorOn)
SAM_H_A3.set_bli_re_ein(sha3, rightIndicatorOn)
if (leftIndicatorOn || rightIndicatorOn) {
SAM_H_A3.set_hell_blink(sha3, 0xFF) // FF as we will tell SAM When to turn off
SAM_H_A3.set_hell_blink(
sha3,
0xFF
) // FF as we will tell SAM When to turn off
} else {
SAM_H_A3.set_hell_blink(sha3, 0x00) // Tell SAM to turn off
}
Expand All @@ -73,12 +79,12 @@ object PartyMode {

// Do lights stuff

if (millis() >= fogCutoff && fogOn) {
if (millis >= fogCutoff && fogOn) {
fogOn = false
lightStateChange = true
}
// Do i turn off the right indicator?
if (millis() >= dippedCutOff && dippedOn) {
if (millis >= dippedCutOff && dippedOn) {
dippedOn = false
lightStateChange = true
}
Expand All @@ -96,23 +102,20 @@ object PartyMode {
CarComm.sendFrame(sha5)
}
}


// -- Finally sleep a bit

try {
Thread.sleep(1)
Thread.sleep(2)
} catch (e: InterruptedException) {
println("Party thread terminated by host!")
break
}
}
//BTMusic.tearDownSampler()
}

// Check if engine is on. If it is party thread MUST exit
fun isEngineOn() : Boolean {
return EZS_A2.get_n_mot() != 65535 && EZS_A2.get_n_mot() != 0
//return EZS_A2.get_n_mot() != 65535 && EZS_A2.get_n_mot() != 0
return false
}

fun activateLeftBlinker(durationMs: Long) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.rndash.mbheadunit.partytime

import android.media.MediaPlayer

class BeatSaberData(val af: MediaPlayer, val levels: ArrayList<BeatSaberLevel>)
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.rndash.mbheadunit.partytime

import android.graphics.Color
import org.json.JSONObject

class BeatSaberLevel(name: String, bpm: Int, json: JSONObject) {
companion object {
const val EVENT_OFFSET_MS = 5
const val EVENT_RESOLUTION_MS = 5
}
inner class LevelFailException(cause: String) : Exception(cause){}

lateinit var instructions: Array<MutableList<Instruction>?>
init {
println("Processing $name")
val msPerBeat = (60000.0 / bpm.toFloat())
val note_json = json.getJSONArray("_notes")
val allevents = ArrayList<Pair<Int, Instruction>>()
for (i in 0 until note_json.length()) {
val note = JSONObject(note_json[i].toString())
val beatTimeStampMs = note.getDouble("_time")
val ts = (((beatTimeStampMs * msPerBeat).toLong() / EVENT_RESOLUTION_MS).toInt() - EVENT_OFFSET_MS)
val lineIndex = note.getInt("_lineIndex")
val lineLayer = note.getInt("_lineLayer")
val type = note.getInt("_type")
if (type == 0 || type == 1) {
allevents.add(
Pair(
ts, Instruction(
OpCode.DISPLAY_NOTE, 10, lineIndex, lineLayer,
if (type == 0) {
Color.RED
} else {
Color.BLUE
}
)
)
)
}
}

val event_json = json.getJSONArray("_events")
if (event_json.length() != 0) {
for (i in 0 until event_json.length()) {
val note = JSONObject(event_json[i].toString())
val beatTimeStampMs = note.getDouble("_time")
val type = note.getInt("_type")
val value = note.getInt("_value")
val ts = (((beatTimeStampMs * msPerBeat).toLong() / EVENT_RESOLUTION_MS).toInt() - EVENT_OFFSET_MS)
var instruction: Instruction? = null
when (type) {
0 -> { // Back center Lazer
var operand = 0L
val colour = when (value) {
1, 2, 3 -> {
operand = 1L
0x220000FF
} // Is it blue?
5, 6, 7 -> {
operand = 2L
0x22FF0000
} // Is it red?
else -> 0x00000000 // No colour
}
instruction = Instruction(OpCode.BG_COLOUR_CHANGE, operand, 0, 0, colour)
}
else -> {} // Unknown, do nothing
}
instruction?.let { allevents.add(Pair(ts, it)) }
}
}

if (allevents.size == 0) {
throw LevelFailException("BeatMap has no events")
}

val elementsNeeded = ((allevents.last().first * msPerBeat).toLong() / EVENT_RESOLUTION_MS)
instructions = Array(elementsNeeded.toInt()){null}
allevents.forEach { e ->
if (instructions[e.first] == null) {
instructions[e.first] = mutableListOf(e.second)
} else {
instructions[e.first]!!.add(e.second)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.rndash.mbheadunit.partytime

import android.graphics.Color

class BeatSaberNote(val beatTimestamp: Double, val lineIndex: Int, val lineLayer: Int, val cutDir: Int, val type: Int) {
override fun toString(): String {
return "[TS: $beatTimestamp - LOC: (${lineIndex}x${lineLayer}) - DIR: $cutDir - TYPE: $type]"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.rndash.mbheadunit.partytime

import android.graphics.Color

class Instruction(val opcode: OpCode, val operand: Long, val locx: Int, val locy: Int, val c: Int) {
override fun toString(): String {
return "Op: $opcode - V: $operand"
}
}

enum class OpCode {
DISPLAY_NOTE,
BG_COLOUR_CHANGE,
END_OF_SONG
}

val OpCodes = OpCode.values()
Loading

0 comments on commit 3fc0a3d

Please sign in to comment.