edge lab LIVE! starts Tuesday in Munich. Grab a workshop seat and take home the board you build on. Code FRIEND-HACKSTER saves 20%. () Learn more!
Jacek Pieczaba
Published © GPL3+

LeapKid

Solution using the DPS130 pressure sensor in a device that can help to protect either infants or toddlers from injuries.

IntermediateFull instructions provided16 hours1,018
LeapKid

Things used in this project

Hardware components

Barometric Pressure Sensor Evaluation Board
Infineon Barometric Pressure Sensor Evaluation Board
I am using the Android libraries for DPS310 devices provided by Infineon
×1

Story

Read more

Schematics

Flow Chart

Flow Chart description how the application is running

Code

Device Finder

Java
Setup and Establish BLE communication between Sensor Hub and Smartphone
package com.example.jacek.leapkid;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.bluetooth.BluetoothDevice;

import java.util.ArrayList;
import java.util.Set;


public class DeviceFinderActivity extends AppCompatActivity {

    private Button btnConnect;
    private TextView tvDevice;
    private String deviceAddress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.leapkid);

        btnConnect = (Button) findViewById(R.id.btn_connect);
        tvDevice = (TextView) findViewById(R.id.tv_device);

        btnConnect.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {
                connect();
            }

        });

       Set<BluetoothDevice> allDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();

        ArrayList<BluetoothDevice> myDevices = new ArrayList<>();

        for(BluetoothDevice d: allDevices)
        {
            if(isMyDevice(d))
                myDevices.add(d);

        }
        if(myDevices.size()==0){
            Toast.makeText(getApplicationContext(), "No Device found", Toast.LENGTH_LONG).show();
            finish();
        }
    tvDevice.setText("Device:" + myDevices.get(0).getName() + "\t" + myDevices.get(0).getAddress());
        deviceAddress=myDevices.get(0).getAddress();


    }



    private boolean isMyDevice(BluetoothDevice d){
        String deviceName = d.getName().toLowerCase();

        return deviceName.equals("ifx_nanohub");
    }
    void connect(){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.putExtra("deviceAddress", deviceAddress);

        startActivity(intent);
    }
}

Main Activity

Java
Main application
package com.example.jacek.leapkid;


import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import com.infineon.sen.comm.Model.Mode;
import com.infineon.sen.comm.SensorEvent;
import com.infineon.sen.comm.SensorHub;
import com.infineon.sen.comm.SensorHubListener;


public class MainActivity extends AppCompatActivity {


    private TextView Altitude;
    private Button btnSet;
    private Button btnReset;
    private Button btnOffset;

    ////////////////// Variables ///////////////////////////////////
    private double Alt = 0; // Variable containing Altitude value received from sensor Hub
    private double Alt_offset = 0; // Variable which setup the reference level
    private double Alt_adjust = 0; // Biased value of altitude based on offset value
    private boolean Active = false; // Boolean variable supporting set or reset the alarms.

    private SoundPool mSoundPool;
    private int mSoundID;
    boolean mLoaded = false;
    private int mSoundStreamId;



    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        Altitude = (TextView) findViewById(R.id.Altitude);
        btnSet = (Button) findViewById(R.id.btn_Set);
        btnReset = (Button) findViewById(R.id.btn_Reset);
        btnOffset = (Button) findViewById(R.id.btn_Offset);

        ///////////////////Setup a sound control /////////////////////

        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

        // Load the sound
        mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId,
                                       int status) {
                mLoaded = true;
            }
        });

        mSoundID = mSoundPool.load(this, R.raw.alarm, 1);
        //////////////////////////////////////////////////////////////


        String deviceAddress = getIntent().getStringExtra("deviceAddress");

        SensorHub sensorHub = new SensorHub(getApplicationContext(), deviceAddress);

        sensorHub.addSensorHubListener(new SensorHubListener() {
            @Override
            public void onConnected(SensorHub sensorHub) {

                sensorHub.setMode("mode", "bg");
                sensorHub.setMode("prs_mr", "128");
                sensorHub.setMode("prs_osr", "128");
                sensorHub.start();
            }

            @Override
            public void onDisconnected(SensorHub sensorHub) {

                // Toast.makeText(getApplicationContext(),"Disconnect to" + sensorHub.getName(). Toast.LENGTH_LONG.show());

            }

            @Override
            public void onConnectionError(SensorHub sensorHub) {
                //  Toast.makeText(getApplicationContext(),"Connection Error" + sensorHub.getName(). Toast.LENGTH_LONG.show());

            }


            @Override
            public void onModeChanged(SensorHub sensorHub, Mode mode) {

            }

            @Override
            public void onSensorDataReceived(SensorHub sensorHub, final SensorEvent sensorEvent) {


                // measure altitude
                if (sensorEvent.getDataId().equals("a")) {

                    Alt = sensorEvent.getSensorValue();

                    Alt_adjust = (Alt - Alt_offset); // Setup the altitude level with correct offset

                    if (Alt_adjust < 0.15) Alt_adjust = 0; // Ignore changes of the altitude below 15cm

                    if(Active) { // here we activate an alarm if requested conditions happen

                         if (Alt_adjust >= 0.4) alarm_on(); // we will engage the sound alarm if the altitude will reach 40cm or more.
                    }
                    Altitude.setText(Alt_adjust + "m");

                }
            }
        });
        sensorHub.connect();

        btnSet.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) { // Activate watching

              Active = true;

            }

        });

        btnReset.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) { // Deactivate alarms

                Active = false;
                alarm_off();

            }

        });

        btnOffset.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) { // setup the offset for altitude measurements

                Alt_offset = Alt;

            }

        });

}


    private void alarm_on() {

        AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        float actualVolume = (float) audioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = (float) audioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float volume = actualVolume / maxVolume;
        // Is the sound loaded already?
        if (mLoaded) {
            mSoundStreamId = mSoundPool.play(mSoundID, volume, volume, 1, -1, 1f);
        }
        Active = false;
    }  // function responsible for sound alarm activation
    private void alarm_off() {

        mSoundPool.stop(mSoundStreamId);
        Active = false;
    } // function responsible for deactivate the sound alarm

}

Android Manifest

XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jacek.leapkid">

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".DeviceFinderActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"></activity>
    </application>

</manifest>

LeapKid Layout

XML
First Screen
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light">

    <TextView
        android:id="@+id/tv_device"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="17dp"
        android:text="Device ID:"
        android:layout_above="@+id/btn_connect"
        android:layout_alignParentStart="true"
        android:layout_marginStart="31dp" />

    <Button
        android:id="@+id/btn_connect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:elevation="0dp"
        android:text="Connect" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:alpha="1"
        android:text="Watch for Children"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_device"
        android:layout_alignParentStart="true"
        app:srcCompat="@drawable/eye" />
</RelativeLayout>

LeapKid layout

XML
Second Screen
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light">

    <Button
        android:id="@+id/btn_Set"
        style="@style/Widget.AppCompat.Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/holo_orange_light"
        android:text="Start Watching" />

    <TextView
        android:id="@+id/Altitude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_Set"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="168dp"
        android:maxLength="6"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

    <Button
        android:id="@+id/btn_Reset"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@android:color/holo_blue_light"
        android:text="Stop Watching" />

    <Button
        android:id="@+id/btn_Offset"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Altitude"
        android:layout_marginTop="44dp"
        android:background="@android:color/holo_green_dark"
        android:text="SET Reference Level"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:id="@+id/Level"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_Offset"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="48dp"
        android:text=" Level [m]:" />

</RelativeLayout>

Credits

Jacek Pieczaba
6 projects • 8 followers
Hardware design engineer;Technology enthusiast;

Comments