Android system porting touch

Source: Internet
Author: User

Turn: http://hi.baidu.com/xx%B9%C2%D3%B0%C6% AE %C1%E3xx/blog/item/d8257c44941db12bcefca3c1.html

Let's start porting now. First, decompress your tslibonandroid file package and put it into the external \ tslibonandroid directory. in this folder, tests \ ts_calibrate.c can verify the touch screen, and analyze the situation.

The Android app runs Java, so we must create a Java application here to implement operations on the display screen. Add the source code in \ Development \ calibrate \ app_calibrate \ SRC \ com \ Android \ Calibrate. The Code is as follows:

Package com. Android. Calibrate;

Import Android. App. activity;
Import Android. widget. textview;
Import Android. OS. Bundle;

Public class calibrate extends Activity
{
@ Override
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
/*
Textview TV = new textview (this );
TV. settext (stringfromjni ());
Setcontentview (TV );
*/
// Super. oncreate (savedinstancestate );
// Setcontentview (R. layout. Main );
Tsmainloop ();
}
Public native void tsmainloop ();

Static {
System. loadlibrary ("calibrate-JNI ");
}
}

Java programs do not work either, because we need a Java Local interface (JNI) to implement Java-C local library interaction, add our JNI source code in Development \ calibrate \ app_calibrate \ ts_calibrate \ JNI \. The Code is as follows:

# Include <utils/log. h>
# Include <nativehelper/JNI. h>
# Include <stdio. h>
# Include ".../../external/tslibonandroid/tests/calibratejni. H"

Static void tsmainloop (jnienv * ENV, jobject object)
{
LogE ("Hello JNI calirate \ n ");
Ts_main ();
}

Static const char * classpathname = "com/Android/calibrate ";

Static jninativemethod methods [] = {// method list
{"Tsmainloop", "() V", (void *) tsmainloop },
};

/*
* Register several native methods for one class.
*/
Static int registernativemethods (jnienv * ENV, const char * classname,
Jninativemethod * gmethods, int nummethods)
// Method registration
{
Jclass clazz;
Clazz = env-> findclass (classname );
If (clazz = NULL ){
Fprintf (stderr, "Native registration unable to find class '% S'", classname );
Return jni_false;
}
If (env-> registernatives (clazz, gmethods, nummethods) <0 ){
Fprintf (stderr, "registernatives failed for '% S'", classname );
Return jni_false;
}

Return jni_true;
}

/*
* Register native methods for all classes we know about.
*/
Static int registernatives (jnienv * env)
{
If (! Registernativemethods (ENV, classpathname,
Methods, sizeof (methods)/sizeof (methods [0]) {
Return jni_false;
}

Return jni_true;
}

Typedef Union {
Jnienv * env;
Void * venv;
} Unionjnienvtovoid;

Jint jni_onload (JavaVM * Vm, void * Reserved)
{
Unionjnienvtovoid uenv;
Uenv. venv = NULL;
Jint result =-1;
Jnienv * Env = NULL;

Printf ("jni_onload ");

If (Vm-> getenv (& uenv. venv, jni_version_1_4 )! = Jni_ OK ){
Fprintf (stderr, "getenv failed ");
Goto bail;
}
ENV = uenv. env;

If (! Registernatives (ENV )){
Fprintf (stderr, "registernatives failed ");
}

Result = jni_version_1_4;

Bail:
Return result;
}
This part mainly implements JNI registration and provides call interfaces for the lower layer.

3. Touch Verification

Ts_main () is mentioned above. It is in the \ external \ tslibonandroid \ tests \ ts_calibrate.c file, which implements vertices verification.

# Define tslib_tsdevice "/dev/input/event1"
# Define device_name "/dev/myts" // myts device name
# Define tslib_calibfile "/system/etc/pointercal"
# Define ts_pointercal "/system/etc/pointercal"

Int ts_main ()
{
Struct tsdev * ts;
Calibration Cal;
Int cal_fd;
Char cal_buffer [256];
Char * tsdevice = NULL;
Char * calfile = NULL;
Unsigned int I;
Unsigned int ret;

Signal (SIGSEGV, sig );
Signal (SIGINT, sig );
Signal (sigterm, sig );

Int size;
Char buffer [5] = "OK ";
Int FD = open ("/dev/myts", o_rdwr );
If (FD =-1)
{
LogE ("... open device failed 1 ...");
Return-1;
}
Printf ("FD: % d \ n", FD );
Write (FD, buffer, 2 );
Close (FD );

If (tsdevice = getenv ("tslib_tsdevice "))! = NULL)
{
TS = ts_open (tsdevice, 0 );
LogE ("--- tsdevice: % s getenv (tslib_tsdevice )! = NULL ----- \ n ", tsdevice );
}
Else
{
If (! (TS = ts_open ("/dev/input/event1", 0 )))
{
LogE ("--- ts_open (/dev/input/event1) --- failed \ n ");
TS = ts_open ("/dev/touchscreen/ucb1x00", 0 );
}
}

LogE ("ts configure \ n ");
If (! TS ){
LogE ("ts open failed \ n ");
Ret =-1;
Goto end;
}
If (ts_config (TS )){
Perror ("ts_config ");
LogE ("ts config failed \ n ");
Ret =-1;
Goto end;
}

If (open_framebuffer ()){
Close_framebuffer ();
LogE ("open_framebuffer failed \ n ");
Ret =-1;
Goto end;
}

For (I = 0; I <nr_colors; I ++)
Setcolor (I, palette [I]);

Put_string_center (xres/2, yres/4, "tslib calibration utility", 1 );
Put_string_center (xres/2, yres/4 + 20, "Touch crosshair to calibrate", 2 );

// Printf ("xres = % d, yres = % d \ n", xres, yres );

// Clear the buffer
Clearbuf (TS );

Get_sample (TS, & Cal, 0, 50, 50, "top left ");
Clearbuf (TS );
Get_sample (TS, & Cal, 1, xres-50, 50, "Top right ");
Clearbuf (TS );
Get_sample (TS, & Cal, 2, xres-50, yres-50, "BOT right ");
Clearbuf (TS );
Get_sample (TS, & Cal, 3, 50, yres-50, "BOT left ");
Clearbuf (TS );
Get_sample (TS, & Cal, 4, xres/2, yres/2, "center ");

If (perform_calibration (& Cal )){
LogE ("calibration constants :");
For (I = 0; I <7; I ++)
LogE ("% d", Cal. A [I]);

If (calfile = getenv ("tslib_calibfile "))! = NULL ){
Cal_fd = open (calfile, o_creat | o_rdwr, s_irusr | s_iwusr | s_irgrp | s_iroth );
LogE ("getenv (tslib_calibfile) OK ");
} Else {
Cal_fd = open (ts_pointercal, o_creat | o_rdwr, s_irusr | s_iwusr | s_irgrp | s_iroth );
LogE ("getenv (tslib_calibfile) null ");
}
Sprintf (cal_buffer, "% d", Cal. A [1], Cal. A [2], Cal. A [0], Cal. A [4], Cal. A [5], Cal. A [3], Cal. A [6]);
Write (cal_fd, cal_buffer, strlen (cal_buffer) + 1 );
Close (cal_fd );
I = 0;
} Else {
LogE ("Calibration FAILED ...");
I =-1;
}
Close_framebuffer ();
Goto end2;

End:
I = ret;
End2:
# If 1
Strcpy (buffer, "end ");
FD = open ("/dev/myts", o_rdwr );
If (FD =-1)
{
// Printf ("... open device failed... \ n ");
LogE ("... open device failed 2 ...");
Return-1;
}
Printf ("FD: % d \ n", FD );
Write (FD, buffer, 3 );
Close (FD );
# Endif
//---------------------------------------
Return I;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.