venerdì 3 aprile 2015

CE Fake Reveal: sovrapporre un'immagine CE scalabile sullo sfondo della fotocamera.

All'avvio dell'applicazione si aprirà uno sfondo live della fotocamera e verrà sovrapposta una scritta CE con le misure conforme al marchio CE. Questa immagine è scalabile e con un minimo di alpha, mantenendo le proporzioni, in modo da confrontarla con l'immagine CE sull'oggetto da verificare.
Ricordarsi di aggiungere su manifest le seguenti righe per abilitare l'uso della fotocamera:

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />


CE_Fake_Reveal.java :

package com.example.marco.cefakereveal;


import java.io.IOException;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.graphics.Matrix;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;
import android.widget.TextView;


public class CE_Fake_Reveal extends Activity implements SurfaceHolder.Callback{

    ImageView img;
    private Matrix matrix = new Matrix();
    private float scale = 0.2f;
    private ScaleGestureDetector SGD;
    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_ce__fake__reveal);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        img = (ImageView)findViewById(R.id.imageView1);
        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        SGD = new ScaleGestureDetector(this,new ScaleListener());

        scale = 1.0f;

        img.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
        {
        updateScale();

        }
        });




     }



    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        SGD.onTouchEvent(ev);
        return true;
    }

    void updateScale()
    {
        final float imageWidth = (float)img.getDrawable().getIntrinsicWidth();
        final float imageHeight = (float)img.getDrawable().getIntrinsicHeight();
        float frameHeight = img.getHeight();
        float frameWidth = img.getWidth();

        matrix.reset();
        matrix.postTranslate(-imageWidth / 2.0f, -imageHeight / 2.0f);
        matrix.postScale(scale, scale);
        matrix.postTranslate(frameWidth / 2.0f, frameHeight / 2.0f );
        img.setImageMatrix(matrix);

    }





    private class ScaleListener extends ScaleGestureDetector.
            SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            scale *= detector.getScaleFactor();
            scale = Math.max(0.03f, Math.min(scale, 0.5f));
            updateScale();
            return true;

        }
    }






/* *************************************************/
/* Ora inizia il codice che riguarda la fotocamera */
/* *************************************************/





    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
// TODO Auto-generated method stub
        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                Camera.Parameters parameters = camera.getParameters();
                parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                camera.setParameters(parameters);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
        camera = Camera.open();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }




}

activity_ce_fake_reveal.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">

    <SurfaceView
        android:id="@+id/camerapreview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <ImageView
        android:id="@+id/imageView1"

        android:scaleType="matrix"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:src="@drawable/ce"
        android:layout_centerInParent="true"
        android:alpha="0.4" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Confrontare la scritta CE per verificarne la validità"
        android:id="@+id/textView"
        android:textStyle="bold"
        android:textColor="#fffdff12"
        android:layout_alignParentTop="true"
        android:textAlignment="center"
        android:layout_centerHorizontal="true" />


</RelativeLayout>

lunedì 23 marzo 2015

Android Dev: preview camera liscio

activity_ce_fake_reveal.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">

    <SurfaceView
        android:id="@+id/camerapreview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>


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

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

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

</manifest>

CE_Fake_Reveal.java
package com.example.marco.cefakereveal;

import java.io.IOException;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class CE_Fake_Reveal extends Activity implements SurfaceHolder.Callback{

    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_ce__fake__reveal);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
     //   surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }



    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
// TODO Auto-generated method stub
        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                Camera.Parameters parameters = camera.getParameters();
                parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                camera.setParameters(parameters);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
        camera = Camera.open();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }
}


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!

domenica 25 marzo 2012

[Finito] Timer lavatrice con scelta del lavaggio

Con questo progetto si avrà un timer per la lavatrice.
Basta selezionare il tipo di lavaggio (cotone 60°, cotone 40°, etc etc) e l'ora in cui si desidera che la lavatrice finisca il lavaggio:

/*
Collegare lo schermo LCD nei pin 13,12,11,10,9,8 dell'arduino
 Collegare un button (per set orologio) nel pin 7
 Collegare il rotary encoder nei pin 2-3 e il button pin nel pin 4
 Collegare il modulo RTC:  SCL-->pin AN5   SDA-->pin AN4
 */
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,13,11,10,9,8);
int ora;
int minuti;
int oraA = 13;
int minutiA = 00;
int val;
int button2 = 7;
int encoder0PinA = 3;
int encoder0PinB = 2;
int encoder0Pos = 0;
int button = 4;
int encoder0PinALast = LOW;
int n = LOW;
int oraextra = 1;
boolean OldButtonState = false;
boolean NewButtonState = false;
boolean OldButtonState2 = false;
boolean NewButtonState2 = false;
char* lavo[]={"   Cotone 60'   ","   Cotone 40'   ","  Sintetici 60' ","  Sintetici 40' ","   Misti  40'   ","   Rapido 30'   ","  Delicati 40'  ","    Lana  30'   "};
int timelavoore[]={2, 2, 1, 1, 1, 0, 0, 0};
int timelavominuti[]={20, 5, 30, 20, 0, 30, 45, 40};
int lavaggio = 1;
int oraX;
int minutiX;

void setup()  {
  Serial.begin(9600);
  pinMode(7, INPUT);
  pinMode(4, INPUT);
  pinMode(3, INPUT);
  pinMode(2, INPUT);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.println("  BUON  GIORNO  ");
  lcd.setCursor(0,1);
  lcd.println(" Wash-timer 1.0 ");
  delay(1500);
  lcd.clear();
  ora = RTC.get(DS1307_HR, true);
  minuti = RTC.get(DS1307_MIN, false);

}




void loop(){ 
  ora = RTC.get(DS1307_HR, true);
  minuti = RTC.get(DS1307_MIN, false);
  visualizza_ora();
  visualizza_allarme();
  if (ButtonPressed()){
    washtype();
    SetAlarm();
  }
  if (ButtonPressed2()){
    SetClock();
  }
 
  if (ora == oraX && minuti == minutiX){
    allarme();
    delay(1000);
    //fai partire il relè che attiverà la lavatrice
  }

}

void allarme(){
  lcd.setCursor(0,1);
  lcd.print(" Avvio lavatrice ");
  lcd.setCursor(0,0);
  lcd.print("                 ");
  delay(500);
  lcd.setCursor(0,0);
  lcd.print(" Avvio lavatrice ");
  lcd.setCursor(0,1);
  lcd.print("                 ");
  delay(500);
}


void visualizza_ora(){
  lcd.setCursor(0,0);
  lcd.print("  ");
  lcd.print("Orario ");
  if (ora <10){
    lcd.setCursor(9,0);
    lcd.print("0");
    lcd.print(ora);
  }
  else { 
    lcd.setCursor(9,0);
    lcd.print(ora);
  }

  lcd.setCursor(11,0);
  lcd.print(":");

  if (minuti <10){
    lcd.setCursor(12,0);
    lcd.print("0");
    lcd.print(minuti);
  }
  else { 
    lcd.setCursor(12,0);
    lcd.print(minuti);
  }

  lcd.print("  ");
}

void visualizza_allarme(){
  lcd.setCursor(0,1);
  lcd.print("Avvia alle:");
  if (oraX <10){
    lcd.setCursor(11,1);
    lcd.print("0");
    lcd.print(oraX);
  }
  else { 
    lcd.setCursor(11,1);
    lcd.print(oraX);
  }

  lcd.setCursor(13,1);
  lcd.print(":");

  if (minutiX <10){
    lcd.setCursor(14,1);
    lcd.print("0");
    lcd.print(minutiX);
  }
  else { 
    lcd.setCursor(14,1);
    lcd.print(minutiX);
  }
}



void SetAlarm(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" Fine lavaggio: ");
 
  while (!ButtonPressed()){
    setOraA();   
    lcd.setCursor(6,1);
    if (oraA < 10){
      lcd.setCursor(6,1);
      lcd.print("0");
      lcd.print(oraA);
    }
    else
    {
      lcd.setCursor(6,1);
      lcd.print(oraA);
    }
  }
  while (!ButtonPressed()){
    setMinutiA();
    lcd.setCursor(8,1);
    lcd.print(":");
    if (minutiA < 10){
      lcd.setCursor(9,1);
      lcd.print("0");
      lcd.print(minutiA);
    }
    else
    {
      lcd.setCursor(9,1);
      lcd.print(minutiA);
    }
  }
 
  minutiX = minutiA - timelavominuti[lavaggio];
  if(minutiX < 0){
    minutiX = 60 + (minutiA - timelavominuti[lavaggio]);
    oraextra = 1;
    } else {oraextra = 0;}
 
  oraX = oraA - timelavoore[lavaggio] - oraextra;
  if(oraX < 0){
   oraX = 23 + (oraA -timelavoore[lavaggio]);
  }
 
}


void SetClock(){

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Imposta l'orario");

  while (!ButtonPressed()){
    setOra();   
    lcd.setCursor(6,1);
    if (ora < 10){
      lcd.setCursor(6,1);
      lcd.print("0");
      lcd.print(ora);
    }
    else
    {
      lcd.setCursor(6,1);
      lcd.print(ora);
    }

  }

  while (!ButtonPressed()){
    setMinuti();

    lcd.setCursor(8,1);
    lcd.print(":");
    if (minuti < 10){
      lcd.setCursor(9,1);
      lcd.print("0");
      lcd.print(minuti);
    }
    else
    {
      lcd.setCursor(9,1);
      lcd.print(minuti);
    }
  }
  RTC.stop();
  RTC.set(DS1307_MIN, minuti);
  RTC.set(DS1307_HR,ora);     
  RTC.start();
}

void setOraA() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      oraA--;
      if (oraA < 0){
        oraA = 23;
      }
    }
    else {
      oraA++;
      if (oraA >= 24){
        oraA = 00;      
      }
    }
  }
  encoder0PinALast = n;
}

void setMinutiA() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      minutiA--;
      if (minutiA < 0){
        minutiA = 59;
      }
    }
    else {
      minutiA++;
      if (minutiA >= 59){
        minutiA = 00;      
      }
    }
  }
  encoder0PinALast = n;
}


void setOra() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      ora--;
      if (ora < 0){
        ora = 23;
      }
    }
    else {
      ora++;
      if (ora >= 24){
        ora = 00;      
      }
    }
    Serial.print ("Ora impostata: ");
    Serial.println (ora);
  }
  encoder0PinALast = n;
}

void setMinuti() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      minuti--;
      if (minuti < 0){
        minuti = 59;
      }
    }
    else {
      minuti++;
      if (minuti >= 59){
        minuti = 00;      
      }
    }
    Serial.print ("Minuti impostati: ");
    Serial.println (minuti);
  }
  encoder0PinALast = n;
}

boolean ButtonPressed() {
  boolean WasIt;
  NewButtonState = !digitalRead(button);
  if ((!OldButtonState) && (NewButtonState)) {
    WasIt = true;
  }
  else {
    WasIt = false;
  }
  OldButtonState = NewButtonState;
  return(WasIt);
}

boolean ButtonPressed2() {
  boolean WasIt2;  // as in, "Was It pressed?"
  NewButtonState2 = !digitalRead(button2);
  if ((!OldButtonState2) && (NewButtonState2)) {
    WasIt2 = true;
  }
  else {
    WasIt2 = false;
  }
  OldButtonState2 = NewButtonState2;
  return(WasIt2);
}














void washtype(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Scegli  lavaggio");
  while(!ButtonPressed()){
      menulavaggio();
      lcd.setCursor(0,1);
      lcd.print(lavo[lavaggio]);
     }
}
 
void menulavaggio() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      lavaggio--;
      if (lavaggio <0){
        lavaggio = 7;
      }
    }
    else {
      lavaggio++;
      if (lavaggio > 7){
        lavaggio = 0;      
      }
    }
  }
  encoder0PinALast = n;
}