Friday, 21 June 2013

Digital Clock using 7-segments on mbed LPC11U24

/*digital clock*/
#include"mbed.h"
BusOut Seg1(p5,p6,p7,p8,p9,p10,p11,p12);//A,B,C,D,E,F,G,DP

int A,C,D,E,F,G;
char B;
int main(){
char SegConvert(char SegValue);
while(1){
A++;
C=A%4;
switch(C){
 case 0:set1=0,set0=1,set2=0,set3=0;
     B=SegConvert(D);
     break;
 case 1:set1=1,set0=0,set2=0,set3=0;
    B=SegConvert(E);
    break;
 case 2:set1=0,set0=0,set2=1,set3=0;
     B=SegConvert(F);
     break;
  case 3:set1=0,set0=0,set2=0,set3=1;
     B=SegConvert(G);
     break; }
     Seg1=B;
     wait(0.001);
   if(A==1000)
   { A=0;
     D++;
     if (D>9)
      {D=0;
        E++;}
      if (E>5)
        { E=0;
          F++;}      
        if (F>9)
          {F=0;
            G++;}
            if (G>5)
            G=0;      } }}

char SegConvert(char SegValue)
{char SegByte=0x00;
switch(SegValue){
case 0: SegByte=0x3F ;break;
case 1: SegByte=0x06 ;break;
case 2: SegByte=0x5B ;break;
case 3: SegByte=0x4F ;break;
case 4: SegByte=0x66 ;break;
case 5: SegByte=0x6D ;break;
case 6: SegByte=0x7D ;break;
case 7: SegByte=0x07 ;break;
case 8: SegByte=0x7F ;break;
case 9: SegByte=0x6F ;break;
}return SegByte;}

Wednesday, 19 June 2013

How to make BlueTerm for working on HTC incredible mobile.

Overcoming Android bluetooth “blues” with Reflection method

Introduction
It is an Android problem: bluetooth does not function in a satisfactory manner. Having recently  started to “hack” into Android devices, and learning while doing, I was immediately confronted with the real world of something that does not fully work (bluetooth in Android devices) at least not satisfactorily as with J2ME devices I worked with before (Nokia and Blackberry).  It is worse if you do, as I did, in buying an Android smartphone in China (A3 Star, HTC T8585 clone) . You will be immediately confronted with the merchants that are insensitive to your requests for technical support. Effectively you learn immediately that there is nobody out there to talk to.
Android bluetoooth “blues”
When I connected my Android phone to external devices, in my case a bluetooth module, the immediate response was an “unable to connect” toast. “Googling“ made me desperate when the enormity of information confused me even more. How to approach the problem, what is wrong with my cheap chinese Android phone? MediaTek the chipset manufacturer of the device’s processor does not “leak” out any source codes to be able to hack bluetooth libraries for example.
Reflection method – solution to connection problem
If you seek quality technical responses I advise you Stock Overflow. That is where I finally learnt a few things such as the problem of connection is overcome with Reflection method.
Technically-speaking the reason is that the widely used connection method CreateInsecureRfcommSocketToServiceRecord() was only included starting with Android API Level 10. When targeting an API lower than 10 (ex 2.3.3 or Gingerbread), the method is not publicly accessible. The Reflection method overcomes that.
So in my chinese HTC clone “A3 Star” I have now a fully working Android market’sBlueTerm, a free app that I use for my engineering tests with external devices. I had to hack into BlueTerm´s source code (it is publicly available at http://pymasde.es/blueterm/) in order to change the method of connection to the Reflection one.
Looking into libraries of my phone’s processor MTK6573, I noted that they were made in 2008, and were never updated. Perhaps in China (the primary target of MTK processors) they do not use SPP at all, and are insensible to “techie“ learners like me wanting to use SPP connection in bluetooth.
On Reflection method among others, you can read more at Stock Overflow site
The code mod made to BlueTerm app
Additionally a special credit is due to Teholabs and their source code for BlueScripts app and the connection method of Reflection. You need to open in your adequate Eclipse environment BluetoothSerialService.java and add at the begining of the file:
Just before:
import java.util.UUID;
Add:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
And down in the file find:
 try {
    tmp = device.createRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);
    } catch (IOException e) {
    Log.e(TAG, "create() failed", e);
    }
    mmSocket = tmp;
    }
And substitute with
  Method m;
   try {
     m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class         });
        tmp = (BluetoothSocket) m.invoke(device, 1);  
        } catch (SecurityException e) {
     Log.e(TAG, "create() failed", e);
     } catch (NoSuchMethodException e) {
     Log.e(TAG, "create() failed", e);
     } catch (IllegalArgumentException e) {
     Log.e(TAG, "create() failed", e);
     } catch (IllegalAccessException e) {
     Log.e(TAG, "create() failed", e);
     } catch (InvocationTargetException e) {
     Log.e(TAG, "create() failed", e);
     }         

   mmSocket = tmp;
    }
You are now ready to compile and download the BlueTerm app into your Andorid device, hopefully without any errors.
The Results
You need to first of all to pair your Android phone to the external module. So I assume you have paired your phone to external bluetooth module. Now we are ready to start the BlueTerm app. This is the first screen you see when you start BlueTerm app:
Opening screen of BlueTerm App
First screen of BlueTerm App
showing “not connected”
We next find the paired device pressing the menu button of your Android phone and choose “Connect Device“. You will see the paired devices including the module you have just paired:
Screen showing paired device
Screen showing paired device
Choose the device and immediately BlueTerm will show the message at the top right of the screen “connecting…”.
If connection is successful the folowing screen will show with virtual keyboard to send commands:
Screen showing connected SPP
Screen showing connected SPP
To leave the application you have to press the menu button on your device and you will now have to disconnect. It is advisable to disconnect:
Screen showing disconnect device
Screen showing disconnect device
UPDATE: For non-techincal users of Android devices I have made available a ready to install BlueTerm app at my most recent post. Before downloading the apk, please read my disclaimer on the same page.
Bugs and Improvements
This mod  is quite fresh and needs to be tested further. I have had to reboot once my HTC clone as the app froze when I tried to disconnect. Any improvements and code sharing from visitors to this blog will be appreciated.
Moral of history
If you want a fully working smartphone do not buy in China, and if you really want to take the risk, check beforehand on the reliability of your supplier.
However I confess, I have an inside feeling of gratitude to the chinese, because if everything was working fine, I would have had no challenges to overcome, and would have not learnt much about Android bluetooth.  The chinese manufacturers do not make available source codes, while non-chinese manufacturers do, or have “leaked” source codes.
Source codes mean opportunities to mod and to improve and a chinese manufacturers do not seem to “leak” these codes.
Conclusions
Just as Bluescripts does the next step for me will be to connect in default way, and then in alternate way (update: I have already made a ready to install modded app of Blueterm – check above).
From now on, I will be able to write new apps for my chinese HTC clone, using Reflection method. The Reflection method is only valid for situations similar to mine.  I hope that sharing this work of mine will help others with similar error messages “unable to connect” in their Android devices.

Learn Android App Development

Here is a link, from where you can learn Android App development step by step.

http://www.javatpoint.com/android-tutorial

My first android app ColorMatch

1. Acticity_main.java

(DemoAppProject\DemoApp\src\main\java\com\example\demoapp\MainActivity.java)


package com.example.demoapp;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button startBtn = (Button)findViewById(R.id.btnStart);
        startBtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                finish();
                Intent myIntent = new Intent(MainActivity.this,MatchActivity.class);
                startActivity(myIntent);
            }
        });
        Button exitBtn = (Button)findViewById(R.id.btnExit);
        exitBtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                finish();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
   

}

Activity_main.XML

(DemoAppProject\DemoApp\src\main\res\layout\activity_main.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
            android:layout_width="250dip"
            android:layout_height="60dip"
            android:text="Start"
            android:id="@+id/btnStart"
            android:layout_marginTop="58dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"/>

    <Button
            android:layout_width="250dip"
            android:layout_height="60dip"
            android:text="Exit"
            android:id="@+id/btnExit"
            android:layout_marginTop="21dp"
            android:layout_below="@+id/btnStart"
            android:layout_alignLeft="@+id/btnStart"/>
</RelativeLayout>


Match_activity.java

package com.example.demoapp;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.Random;

/**
 * Created by ABHA on 6/16/13.
 */
public class MatchActivity extends Activity {
    View circleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match);

        circleView = (View)findViewById(R.id.circleId);
        Random rand = new Random();
        int x = rand.nextInt(3);
        switch (x) {
            case 0:
                circleView.setBackgroundColor(getResources().getColor(R.color.green));
                circleView.setTag("Green");
                break;
            case 1:
                circleView.setBackgroundColor(getResources().getColor(R.color.red));
                circleView.setTag("Red");
                break;
            case 2:
                circleView.setBackgroundColor(getResources().getColor(R.color.blue));
                circleView.setTag("Blue");
                break;
        }

        Button redBtn = (Button)findViewById(R.id.btnRed);
        Button blueBtn = (Button)findViewById(R.id.btnBlue);
        Button greenBtn = (Button)findViewById(R.id.btnGreen);

        redBtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                String c = (String)circleView.getTag();
                Intent myIntent = new Intent(MatchActivity.this,ResultActivity.class);

                if(c.equalsIgnoreCase("Red")){
                    myIntent.putExtra("Result",true );
                    //Toast.makeText(MatchActivity.this, "Correct",Toast.LENGTH_SHORT).show();
                }else{
                    myIntent.putExtra("Result",false );
                    //Toast.makeText(MatchActivity.this, "Wrong",Toast.LENGTH_SHORT).show();
                }
                finish();
                startActivity(myIntent);
            }
        });

        blueBtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                String c = (String)circleView.getTag();
                Intent myIntent = new Intent(MatchActivity.this,ResultActivity.class);

                if(c.equalsIgnoreCase("Blue")){
                    myIntent.putExtra("Result",true );
                    //Toast.makeText(MatchActivity.this, "Correct",Toast.LENGTH_SHORT).show();
                }else{
                    myIntent.putExtra("Result",false );
                    //Toast.makeText(MatchActivity.this, "Wrong",Toast.LENGTH_SHORT).show();
                }
                finish();
                startActivity(myIntent);
            }
        });

        greenBtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                String c = (String)circleView.getTag();
                Intent myIntent = new Intent(MatchActivity.this,ResultActivity.class);

                if(c.equalsIgnoreCase("Green")){
                    myIntent.putExtra("Result",true );
                    //Toast.makeText(MatchActivity.this, "Correct",Toast.LENGTH_SHORT).show();
                }else{
                    myIntent.putExtra("Result",false );
                    //Toast.makeText(MatchActivity.this, "Wrong",Toast.LENGTH_SHORT).show();
                }
                finish();
                startActivity(myIntent);
            }
        });

    }
}


Activity_match.XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:paddingBottom="@dimen/activity_vertical_margin">

    <View android:layout_width="fill_parent"
          android:layout_height="200dip"
          android:id="@+id/circleId"
          android:background="@drawable/circle"
         />

    <Button
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:text="Red"
            android:id="@+id/btnRed"
            android:layout_marginTop="50dp"
            android:layout_below="@+id/circleId"
            android:layout_alignLeft="@+id/circleId"
            />



    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:text="Green"
            android:id="@+id/btnGreen"
            android:layout_marginTop="21dp"
            android:layout_below="@+id/btnRed"
            android:layout_alignLeft="@+id/btnRed"
            />


    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:text="Blue"
            android:id="@+id/btnBlue"
            android:layout_marginTop="21dp"
            android:layout_below="@+id/btnGreen"
            android:layout_alignLeft="@+id/btnGreen"
            />
</LinearLayout>

Result_activity.java

package com.example.demoapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by ABHA on 6/16/13.
 */
public class ResultActivity  extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        TextView textView = (TextView)findViewById(R.id.textView);
        boolean result = getIntent().getBooleanExtra("Result",false);
        if(result){
            textView.setText(getResources().getString(R.string.win));
        }else{
            textView.setText(getResources().getString(R.string.failed));
        }

        Button exitBtn2 = (Button)findViewById(R.id.btnExit2);
        exitBtn2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                finish();

            }
        });

    }
}


Result_Activity.XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:baselineAligned="true">

    <TextView
            android:layout_width="250dip"
            android:layout_height="250dip"
            android:text="New Text"
            android:id="@+id/textView"
            android:layout_marginTop="40dp"
            android:layout_gravity="center"
      />

    <Button
            android:layout_width="250dip"
            android:layout_height="60dip"
            android:text="Exit"
            android:id="@+id/btnExit2"
            android:layout_marginTop="58dp"
            android:layout_gravity="center"/>
</LinearLayout>

Circle.XML//some error

(DemoAppProject\DemoApp\src\main\res\drawable-hdpi\circle.xml)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >

</shape>


Color.XML (DemoAppProject\DemoApp\src\main\res\values\color.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#ff0000</color>
    <color name="green">#00ff00</color>
    <color name="blue">#0000ff</color>
</resources>





Sunday, 16 June 2013

My first project: IOT(Internet Of Thing)

I will share everything how am I doing my project........

My achievements.......

Till today, I have worked with 8051, AVR, STM8S and mbed LPC11U24
On 8051:
1.LEDs nd LCD interfacing, serial communication with PC.
2. 7 segment counter
3.keypad and LCD interfacing with 8051
.
 On AVR:
1.LEDs and switching

On STM8S:
1.LED blinking.

On mbed:
1. LED and switching
2.serial communication
3.7-segment interface
4.Digital clock and alarm using buzzer and 7-segment

Software Used:
1.Keil for 8051
2. Keil for ARM
3.Proteus
4.Atmel Studio
5.Flowcode
6.Win AVR
7.ST development tool
8. Cosmic compiler