giovedì 30 ottobre 2014

Android dev: creare un'applicazione per telecomandare il robot aspirapolvere Samsung Navibot

Se avete uno smartphone android con porta infrarossi e un robot aspirapolvere Samsung Navibot siete a posto!

Innanzitutto i codici IRDA per comandare il Navibot:
Frequency; 38000
Header Time On: 4508
Header Time Off; 4437
Bit One on: 605
Bit One off: 1621
Bit Zero on: 605
Bit Zero off: 509
Hex Code PreData 8181

Command:
On/Off 00ff
Home 807f
Start Auto 40bf
Spot c03f
Repeat 20df
Manual Start/Stop 10ef
Avanti a05f
Sinistra 609f
Destra e01f

Altri codici da testare:

50af
d02f
30cf
b04f
708f

Questo i listati:

Irda.java:

package com.example.cristina.IRda;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.hardware.ConsumerIrManager;
import android.widget.ImageButton;
import android.os.Handler;


public class Irda extends Activity implements View.OnTouchListener {

    private Handler handler1 = new Handler();
    private Runnable runnable1 = new Runnable() {

        @Override
        public void run() {
            su();
            handler1.postDelayed(this, 1); //ripete l'operazione ogni 500 millisecondi
        }

    };

    private Handler handler2 = new Handler();
    private Runnable runnable2 = new Runnable() {

        @Override
        public void run() {
            sinistra();
            handler2.postDelayed(this, 1); //ripete l'operazione ogni 500 millisecondi
        }

    };

    private Handler handler3 = new Handler();
    private Runnable runnable3 = new Runnable() {

        @Override
        public void run() {
            destra();
            handler3.postDelayed(this, 1); //ripete l'operazione ogni 500 millisecondi
        }

    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_irda);
        ImageButton power = (ImageButton) findViewById(R.id.power);
        ImageButton home = (ImageButton) findViewById(R.id.home);
        ImageButton auto = (ImageButton) findViewById(R.id.auto);
        ImageButton max = (ImageButton) findViewById(R.id.max);
        ImageButton spot = (ImageButton) findViewById(R.id.spot);
        ImageButton up = (ImageButton) findViewById(R.id.up);
        ImageButton left = (ImageButton) findViewById(R.id.left);
        ImageButton right = (ImageButton) findViewById(R.id.right);
        ImageButton manual = (ImageButton) findViewById(R.id.manual);
        ImageButton pausa = (ImageButton) findViewById(R.id.pausa);

        up.setOnTouchListener(this);
        left.setOnTouchListener(this);
        right.setOnTouchListener(this);


        power.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 19, 23,
                        19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23,
                        62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

        home.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 19, 23,
                        19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23,
                        62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

        auto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 62, 23,
                        19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23,
                        62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

        manual.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 19, 23,
                        19, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        62, 23, 62, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

        spot.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 62, 23,
                        19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

        max.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 19, 23,
                        62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23,
                        62, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

/*        left.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 62, 23,
                        62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23,
                        19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

        right.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19,
                        23, 19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 62,
                        23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                        19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });
*/

        pausa.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ConsumerIrManager mCIR;
                mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
                int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19,
                        23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 19, 23, 19,
                        23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 19, 23, 62, 23, 62, 23, 62,
                        23, 62, 23, 2500};
                mCIR.transmit(38000, pattern);
                // Perform action on click
            }
        });

    }
















//************************************************************************






















    public void su() {
        ConsumerIrManager mCIR;
        mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
        int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 19, 23,
                62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                62, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
        mCIR.transmit(38000, pattern);
    }

    public void sinistra() {
        ConsumerIrManager mCIR;
        mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
        int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 19, 23, 62, 23,
                62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23,
                19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
        mCIR.transmit(38000, pattern);
    }

    public void destra() {
        ConsumerIrManager mCIR;
        mCIR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);
        int[] pattern = {173, 170, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19,
                23, 19, 23, 62, 23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 62, 23, 62, 23, 62,
                23, 62, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23, 19, 23,
                19, 23, 19, 23, 62, 23, 62, 23, 62, 23, 62, 23, 62, 23, 2500};
        mCIR.transmit(38000, pattern);
    }


    public void start1() {
        handler1.postDelayed(runnable1, 1); // avvia il loop
    }

    public void stop1() {
        handler1.removeCallbacks(runnable1); // cancella il loop
    }

    public void start2() {
        handler2.postDelayed(runnable2, 1); // avvia il loop
    }

    public void stop2() {
        handler2.removeCallbacks(runnable2); // cancella il loop
    }

    public void start3() {
        handler3.postDelayed(runnable3, 1); // avvia il loop
    }

    public void stop3() {
        handler3.removeCallbacks(runnable3); // cancella il loop
    }


    @Override
    public void onPause() { // quando va in pausa ferma il loop
        super.onPause();
        handler1.removeCallbacks(runnable1);
        handler2.removeCallbacks(runnable2);
        handler3.removeCallbacks(runnable3);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (v.getId() == R.id.up) {
                start1();
            }

            if (v.getId() == R.id.left) {
                start2();
            }

            if (v.getId() == R.id.right) {
                    start3();
                }

            }

            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (v.getId() == R.id.up) {
                    stop1();
                }
                if (v.getId() == R.id.left) {
                    stop2();
                }
                if (v.getId() == R.id.right) {
                    stop3();
                }
            }

                return false;
            }}

Qui invece il file Activity_irda.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff000000"
    android:id="@+id/dd">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Power"
        android:id="@+id/textView6"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:textColor="#ffffffff"
        android:layout_marginLeft="55dp"
        android:layout_marginTop="35dp" />

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/power"
        android:layout_below="@+id/textView6"
        android:layout_alignParentStart="true"
        android:background="@drawable/power"
        android:layout_marginLeft="55dp"
        android:focusableInTouchMode="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Home"
        android:id="@+id/textView7"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:textColor="#ffffffff"
        android:layout_marginTop="35dp"
        android:layout_marginRight="55dp" />

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/home"
        android:layout_below="@+id/textView7"
        android:layout_alignParentEnd="true"
        android:background="@drawable/home"
        android:layout_marginRight="55dp"
        android:focusableInTouchMode="false" />


    <ImageButton
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/up"
        android:background="@drawable/up"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ImageButton
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/left"
        android:background="@drawable/left"
        android:layout_toEndOf="@+id/auto"
        android:layout_below="@+id/up" />

    <ImageButton
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/right"
        android:background="@drawable/right"
        android:layout_below="@+id/up"
        android:layout_toEndOf="@+id/up" />


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/power"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:id="@+id/linearLayout2">

        <TextView
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Auto"
            android:id="@+id/textView9"
            android:textColor="#ffffffff"
            android:layout_below="@+id/power"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" />

        <TextView
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Manual"
            android:id="@+id/textView10"
            android:textColor="#ffffffff"
            android:layout_alignParentStart="false"
            android:layout_alignParentBottom="false"
            android:layout_below="@+id/power"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" />

        <TextView
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Spot"
            android:id="@+id/textView11"
            android:textColor="#ffffffff"
            android:layout_below="@+id/power"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" />

        <TextView
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Max"
            android:id="@+id/textView8"
            android:textColor="#ffffffff"
            android:layout_below="@+id/power"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" />
    </LinearLayout>

    <ImageButton

        android:layout_height="25dp"
        android:id="@+id/auto"
        android:background="@drawable/bottone"
        android:layout_width="50dp"
        android:layout_weight="0"
        android:gravity="center_horizontal"
        android:layout_below="@+id/linearLayout2"
        android:layout_marginLeft="27dp" />

    <ImageButton
        android:layout_height="25dp"
        android:id="@+id/manual"
        android:background="@drawable/bottone"
        android:layout_width="50dp"
        android:layout_weight="0"
        android:gravity="center_horizontal"
        android:layout_alignBottom="@+id/auto"
        android:layout_toEndOf="@+id/power"
        android:layout_marginLeft="8dp" />

    <ImageButton
        android:layout_height="25dp"
        android:id="@+id/spot"
        android:background="@drawable/bottone"
        android:layout_width="50dp"
        android:layout_weight="0"
        android:gravity="center_horizontal"
        android:layout_below="@+id/linearLayout2"
        android:layout_toStartOf="@+id/home"
        android:layout_marginRight="8dp" />

    <ImageButton
        android:layout_height="25dp"
        android:id="@+id/max"
        android:background="@drawable/bottone"
        android:layout_width="50dp"
        android:layout_weight="0"
        android:gravity="center_horizontal"
        android:layout_marginLeft="25dp"
        android:layout_alignBottom="@+id/spot"
        android:layout_alignStart="@+id/textView7" />

    <ImageView
        android:layout_width="187dp"
        android:layout_height="28dp"
        android:id="@+id/imageView"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:background="@drawable/samsunglogo" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="by Marco Giglio 2014"
        android:id="@+id/textView"
        android:textColor="#fff9f9f9"
        android:textAlignment="center"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/manual" />

    <ImageButton
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/pausa"
        android:background="@drawable/pause"
        android:layout_below="@+id/left"
        android:layout_toStartOf="@+id/right" />

</RelativeLayout>


Qui invece i file originali pronti da usare con Android Studio:
https://www.mediafire.com/folder/b202qgggvup4h/

Il ritorno!

Dopo mesi e mesi di inattività causa terremoto sentimentale/lavorativo/abitativo, sono tornato a dilettarmi nel mondo dell'elettronica!