Skip to content

Commit

Permalink
minor bug fixes and logo change
Browse files Browse the repository at this point in the history
  • Loading branch information
NitishGadangi committed Jul 6, 2021
1 parent df9c112 commit 86ecb0d
Show file tree
Hide file tree
Showing 40 changed files with 236 additions and 225 deletions.
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:icon="@drawable/sensorcast_logo"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SensorCast">
<activity android:name=".ui.SensorActivity">
Expand Down
Binary file added app/src/main/ic_launcher-playstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void setSensorLog(String data) {
public String updateSensorLog(String data) {
String oldData = getSensorLog();
oldData = oldData.equals(SENSOR_DATA_DEFAULT) ? "" : oldData;
String result = oldData + "\n" + data;
String result = data + "\n" + oldData;
setSensorLog(result);
return result;
}
Expand Down
21 changes: 20 additions & 1 deletion app/src/main/java/com/nitish/sensorcast/ui/SensorActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.hardware.SensorManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
Expand All @@ -16,6 +17,9 @@ import com.nitish.sensorcast.databinding.FragmentAppBarBinding
import com.nitish.sensorcast.helpers.SensorDetails
import com.nitish.sensorcast.models.Status
import com.nitish.sensorcast.repository.SharedPrefManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SensorActivity : AppCompatActivity(), SensorEventListener {

Expand All @@ -27,6 +31,8 @@ class SensorActivity : AppCompatActivity(), SensorEventListener {
supportFragmentManager.findFragmentByTag("navHostFragment") as NavHostFragment
}

private var sensorEvent : SensorEvent? = null

val viewModel: SensorViewModel by lazy {
val sensorViewModelProviderFactory = SensorViewModelProviderFactory(
application,
Expand All @@ -42,7 +48,7 @@ class SensorActivity : AppCompatActivity(), SensorEventListener {
override fun onSensorChanged(event: SensorEvent?) {
event?.let {
if(it.sensor == viewModel.currentSensor.value){
if (viewModel.isCastEnabled.value == true) viewModel.sendData(event.values)
sensorEvent = event
viewModel.currentSensorValues.postValue(event.values)
}
}
Expand Down Expand Up @@ -80,6 +86,19 @@ class SensorActivity : AppCompatActivity(), SensorEventListener {

setUpObservers()
setUpListeners()

startCastListener()
}

private fun startCastListener() {
lifecycleScope.launch(Dispatchers.IO) {
while (true){
if (viewModel.isCastEnabled.value == true) sensorEvent?.let {
viewModel.sendData(it.values)
}
delay(500)
}
}
}

private fun setUpObservers() {
Expand Down
102 changes: 74 additions & 28 deletions app/src/main/java/com/nitish/sensorcast/ui/SensorViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class SensorViewModel(

val currentSensorValues = MutableLiveData(floatArrayOf())

var isDataBeingSent = false

private var btSocket: BluetoothSocket? = null

private var btMonitorJob: Job? = null
Expand Down Expand Up @@ -158,19 +160,34 @@ class SensorViewModel(
if (data == oldData) return
oldData = data
if (btSocket != null && btSocket?.isConnected == true) {
isDataBeingSent = true
try {
btSocket?.outputStream?.write(data.toByteArray(charset = Charset.defaultCharset()))
btSocket?.outputStream?.apply {
flush()
write(data.toByteArray(charset = Charset.defaultCharset()))
}
} catch (exp: IOException) {
isBtConnected.postValue(Status.DISCONNECTED)
}
isDataBeingSent = false
}
}

fun sendData(data: FloatArray){
sendData(data.map { it.round(1) }.joinToString(separator = " "))
fun sendData(data: FloatArray) {
if(!isDataBeingSent) sendData(data.map { it.round(1) }.joinToString(separator = " "))
}

fun clearLogs() {
sharedPrefManager.clearSensorLog()
inputStream.postValue(sharedPrefManager.sensorLog)
}

private fun getSensorDetails(sensorType:Int, sensorName:String, sensorDesc:String, sensorUnits:String): SensorDetails? {
private fun getSensorDetails(
sensorType: Int,
sensorName: String,
sensorDesc: String,
sensorUnits: String
): SensorDetails? {
val sensor = sensorManager.getDefaultSensor(sensorType) ?: return null
return SensorDetails(
name = sensorName,
Expand All @@ -186,13 +203,28 @@ class SensorViewModel(

fun getSensorsList(): List<SensorDetails> {
val result = mutableListOf<SensorDetails>()
getSensorDetails(Sensor.TYPE_ACCELEROMETER, "Accelerometer", ACCELEROMETER_DESCRIPTION, "m/s2")?.let {
getSensorDetails(
Sensor.TYPE_ACCELEROMETER,
"Accelerometer",
ACCELEROMETER_DESCRIPTION,
"m/s2"
)?.let {
result.add(it)
}
getSensorDetails(Sensor.TYPE_AMBIENT_TEMPERATURE, "Ambient Temperature", AMBIENT_TEMPERATURE_DESCRIPTION, "°C")?.let {
getSensorDetails(
Sensor.TYPE_AMBIENT_TEMPERATURE,
"Ambient Temperature",
AMBIENT_TEMPERATURE_DESCRIPTION,
"°C"
)?.let {
result.add(it)
}
getSensorDetails(Sensor.TYPE_MAGNETIC_FIELD, "Magnetic Field", MAGNETIC_FIELD_DESCRIPTION, "uT")?.let {
getSensorDetails(
Sensor.TYPE_MAGNETIC_FIELD,
"Magnetic Field",
MAGNETIC_FIELD_DESCRIPTION,
"uT"
)?.let {
result.add(it)
}
getSensorDetails(Sensor.TYPE_GYROSCOPE, "Gyroscope", GYROSCOPE_DESCRIPTION, "rad/s")?.let {
Expand All @@ -210,7 +242,12 @@ class SensorViewModel(
getSensorDetails(Sensor.TYPE_PRESSURE, "Pressure", PRESSURE_DESCRIPTION, "hPa")?.let {
result.add(it)
}
getSensorDetails(Sensor.TYPE_PRESSURE, "Relative Humidity", HUMIDITY_DESCRIPTION, "%")?.let {
getSensorDetails(
Sensor.TYPE_PRESSURE,
"Relative Humidity",
HUMIDITY_DESCRIPTION,
"%"
)?.let {
result.add(it)
}
return result
Expand All @@ -220,26 +257,35 @@ class SensorViewModel(
val BT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
const val MAC_SIZE = 17
const val ARDUINO_SETUP_LINK = "https://github.com/NitishGadangi/SensorCast"
const val ACCELEROMETER_DESCRIPTION = "An accelerometer sensor reports the acceleration of the device along the three sensor axes." +
"The measured acceleration includes both the physical acceleration (change of velocity) and the gravity." +
"The measurement is reported in the x, y, and z fields of sensors_event_t.acceleration."
const val GYROSCOPE_DESCRIPTION = "A gyroscope sensor reports the rate of rotation of the device around the three sensor axes.\n" +
"Rotation is positive in the counterclockwise direction (right-hand rule). " +
"That is, an observer looking from some positive location on the x, y, or z axis at a device positioned on the origin would" +
"report positive rotation if the device appeared to be rotating counter clockwise. Note that this is the standard mathematical " +
"definition of positive rotation and does not agree with the aerospace definition of roll."
const val AMBIENT_TEMPERATURE_DESCRIPTION ="This sensor provides the ambient (room) temperature in degrees Celsius."
const val MAGNETIC_FIELD_DESCRIPTION = "A magnetic field sensor (also known as magnetometer) reports the ambient magnetic field, as measured along the three sensor axes."
const val HEART_RATE_DESCRIPTION = "A heart rate sensor reports the current heart rate of the person touching the device."
const val LIGHT_DESCRIPTION = "A light sensor reports the current illumination in SI lux units."
const val PRESSURE_DESCRIPTION = "A pressure sensor (also known as barometer) reports the atmospheric pressure in hectopascal (hPa).\n" +
"\n" +
"The readings are calibrated using\n" +
"- Temperature compensation\n" +
"- Factory bias calibration\n" +
"- Factory scale calibration"
const val HUMIDITY_DESCRIPTION = "A relative humidity sensor measures relative ambient air humidity and returns a value in percent."
const val PROXIMITY_DESCRIPTION = "A proximity sensor reports the distance from the sensor to the closest visible surface. Note that some proximity sensors only support a binary \"near\" or \"far\" measurement."
const val ACCELEROMETER_DESCRIPTION =
"An accelerometer sensor reports the acceleration of the device along the three sensor axes." +
"The measured acceleration includes both the physical acceleration (change of velocity) and the gravity." +
"The measurement is reported in the x, y, and z fields of sensors_event_t.acceleration."
const val GYROSCOPE_DESCRIPTION =
"A gyroscope sensor reports the rate of rotation of the device around the three sensor axes.\n" +
"Rotation is positive in the counterclockwise direction (right-hand rule). " +
"That is, an observer looking from some positive location on the x, y, or z axis at a device positioned on the origin would" +
"report positive rotation if the device appeared to be rotating counter clockwise. Note that this is the standard mathematical " +
"definition of positive rotation and does not agree with the aerospace definition of roll."
const val AMBIENT_TEMPERATURE_DESCRIPTION =
"This sensor provides the ambient (room) temperature in degrees Celsius."
const val MAGNETIC_FIELD_DESCRIPTION =
"A magnetic field sensor (also known as magnetometer) reports the ambient magnetic field, as measured along the three sensor axes."
const val HEART_RATE_DESCRIPTION =
"A heart rate sensor reports the current heart rate of the person touching the device."
const val LIGHT_DESCRIPTION =
"A light sensor reports the current illumination in SI lux units."
const val PRESSURE_DESCRIPTION =
"A pressure sensor (also known as barometer) reports the atmospheric pressure in hectopascal (hPa).\n" +
"\n" +
"The readings are calibrated using\n" +
"- Temperature compensation\n" +
"- Factory bias calibration\n" +
"- Factory scale calibration"
const val HUMIDITY_DESCRIPTION =
"A relative humidity sensor measures relative ambient air humidity and returns a value in percent."
const val PROXIMITY_DESCRIPTION =
"A proximity sensor reports the distance from the sensor to the closest visible surface. Note that some proximity sensors only support a binary \"near\" or \"far\" measurement."

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nitish.sensorcast.ui
package com.nitish.sensorcast.ui.fragments

import android.os.Bundle
import android.view.LayoutInflater
Expand All @@ -11,6 +11,7 @@ import androidx.fragment.app.Fragment
import com.nitish.sensorcast.R
import com.nitish.sensorcast.databinding.FragmentBluetoothDevicesBinding
import com.nitish.sensorcast.models.Status
import com.nitish.sensorcast.ui.SensorActivity

class BluetoothDevicesFragment : Fragment(R.layout.fragment_bluetooth_devices) {

Expand All @@ -32,7 +33,7 @@ class BluetoothDevicesFragment : Fragment(R.layout.fragment_bluetooth_devices) {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewModel.bluetoothFragmentFisiblity.postValue(true)
viewModel.bluetoothFragmentFisiblity.postValue(true)
return super.onCreateView(inflater, container, savedInstanceState)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nitish.sensorcast.ui
package com.nitish.sensorcast.ui.fragments

import android.os.Bundle
import android.view.View
Expand All @@ -9,6 +9,8 @@ import com.nitish.sensorcast.helpers.SensorDetails
import com.nitish.sensorcast.helpers.openBrowser
import com.nitish.sensorcast.helpers.round
import com.nitish.sensorcast.models.Status
import com.nitish.sensorcast.ui.SensorActivity
import com.nitish.sensorcast.ui.SensorViewModel

class SensorDetailsFragment : Fragment(R.layout.fragment_sensor_details) {

Expand Down Expand Up @@ -36,42 +38,53 @@ class SensorDetailsFragment : Fragment(R.layout.fragment_sensor_details) {
binding.tvSensorDesc.text = sensorDetails.description
"Vendor : ${sensorDetails.vendor}".also { binding.tvSensorVendor.text = it }
"Type : ${sensorDetails.stringType}".also { binding.tvSensorType.text = it }
"Resolution : ${sensorDetails.resolution} ${sensorDetails.units}".also { binding.tvSensorResolution.text = it }
"Range : ${sensorDetails.range} ${sensorDetails.units}".also { binding.tvSensorRange.text = it }
"Resolution : ${sensorDetails.resolution} ${sensorDetails.units}".also {
binding.tvSensorResolution.text = it
}
"Range : ${sensorDetails.range} ${sensorDetails.units}".also {
binding.tvSensorRange.text = it
}
"Units : ${sensorDetails.units}".also { binding.tvSensorUnits.text = it }
}

private fun setUpObservers() {
viewModel.isBtConnected.observe(viewLifecycleOwner, {
binding.btnCastSensor.visibility = if (it == Status.CONNECTED) View.VISIBLE else View.GONE
binding.btnCastSensor.visibility =
if (it == Status.CONNECTED) View.VISIBLE else View.GONE
})

viewModel.isCastEnabled.observe(viewLifecycleOwner, {
if (it){
if (it) {
binding.btnCastSensor.apply {
("Stop Casting").also { text = it }
backgroundTintList = resources.getColorStateList(R.color.red)
setTextColor(resources.getColor(R.color.white))
}
}else {
} else {
binding.btnCastSensor.apply {
setText("Cast Sensor")
text = "Cast Sensor"
backgroundTintList = resources.getColorStateList(R.color.colorAccent)
setTextColor(resources.getColor(R.color.white))
}
}
})

viewModel.currentSensorValues.observe(viewLifecycleOwner, { values ->
if (values.size == 1){
"${values[0].round(1)} ${sensorDetails.units}".also { binding.tvSensorValues.text = it }
}else if(values.size == 3){
binding.textView12.visibility = View.VISIBLE
binding.tvSensorValues.visibility = View.VISIBLE
if (values.size == 1) {
"${values[0].round(1)} ${sensorDetails.units}".also {
binding.tvSensorValues.text = it
}
} else if (values.size == 3) {
("x : ${values[0].round(1)} ${sensorDetails.units}\n" +
"y : ${values[1].round(1)} ${sensorDetails.units}\n" +
"z : ${values[2].round(1)} ${sensorDetails.units}\n").also { binding.tvSensorValues.text = it }
}else {
binding.textView12.visibility = View.GONE
binding.tvSensorValues.visibility = View.GONE
"z : ${values[2].round(1)} ${sensorDetails.units}\n").also {
binding.tvSensorValues.text = it
}
} else {
binding.textView12.visibility = View.INVISIBLE
binding.tvSensorValues.visibility = View.INVISIBLE
}
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.nitish.sensorcast.ui
package com.nitish.sensorcast.ui.fragments

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.nitish.sensorcast.R
import com.nitish.sensorcast.databinding.FragmentSensorLogsBinding
import com.nitish.sensorcast.ui.SensorActivity

class SensorLogsFragment : Fragment(R.layout.fragment_sensor_logs) {

Expand All @@ -25,5 +27,9 @@ class SensorLogsFragment : Fragment(R.layout.fragment_sensor_logs) {
viewModel.inputStream.observe(viewLifecycleOwner, {
binding.textView.text = it
})

binding.btnClearAll.setOnClickListener {
viewModel.clearLogs()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nitish.sensorcast.ui
package com.nitish.sensorcast.ui.fragments

import android.os.Bundle
import android.view.View
Expand All @@ -7,6 +7,7 @@ import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.nitish.sensorcast.R
import com.nitish.sensorcast.databinding.FragmentSensorsOverviewBinding
import com.nitish.sensorcast.ui.SensorActivity
import com.nitish.sensorcast.ui.adapters.SensorsAdapter

class SensorsOverviewFragment : Fragment(R.layout.fragment_sensors_overview) {
Expand All @@ -31,10 +32,11 @@ class SensorsOverviewFragment : Fragment(R.layout.fragment_sensors_overview) {
layoutManager = LinearLayoutManager(activity)
}
sensorsAdapter.setOnItemClickListener {
(requireActivity() as SensorActivity).sensorManager.getDefaultSensor(it.type).also { sensor ->
viewModel.currentSensor.postValue(sensor)
(requireActivity() as SensorActivity).registerSensor()
}
(requireActivity() as SensorActivity).sensorManager.getDefaultSensor(it.type)
.also { sensor ->
viewModel.currentSensor.postValue(sensor)
(requireActivity() as SensorActivity).registerSensor()
}
val directions =
SensorsOverviewFragmentDirections.actionSensorsOverviewFragmentToSensorDetailsFragment(
it
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_delete_all.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="64dp" android:tint="#141414"
android:viewportHeight="24" android:viewportWidth="24"
android:width="64dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M15,16h4v2h-4zM15,8h7v2h-7zM15,12h6v2h-6zM3,18c0,1.1 0.9,2 2,2h6c1.1,0 2,-0.9 2,-2L13,8L3,8v10zM14,5h-3l-1,-1L6,4L5,5L2,5v2h12z"/>
</vector>
Loading

0 comments on commit 86ecb0d

Please sign in to comment.