Backlight of by C-program

holger
Hi,
I wrote a little C program to turn off the backlight after some seconds if
the user don't touch the screen and turn it on again if the user touchs the
srceen. (I am not really a C programmer:-) )

The following code works... basicly:-)

#define SECONDS_TO_WAIT 10

int backlight = 1;
int seconds = 0;
void *Timer() {
  while(1){
    if(seconds < SECONDS_TO_WAIT){
      sleep(1);
      seconds++;
    } else {
      if(backlight == 1) {
        system("echo 20 > /dev/backlight-1wire");
        backlight = 0;
      }
      sleep(1);
    }
  }
}

main()
{
  // Mini6410 Touchsreen
  int fileno = open("/dev/touchscreen-1wire",O_RDONLY),num_bytes;

  char ev[50];
  
  pthread_t pth;
  pthread_create(&pth,NULL,Timer,"");

  while(1) {
    num_bytes = read(fileno,ev,50);
    if(num_bytes > 0) { //  Movement
      seconds = 1;
      if(backlight == 0) {
        system("echo 128 > /dev/backlight-1wire");
        backlight = 1;
      }
    }
    if(num_bytes<=0)
    {
      perror("Cannot access");
      exit(1);
    }
  }
  close(fileno);
}


with this code, the light turns of after 10 seconds but then the
mousepointer only sometimes is moving if I touch the screen. I can touch
and draw the screen a few seconds and the screen stays dark. It seems the
touchscreen device can not be read from more then one process (the
inputprocess, I think). How can I do this? How can I read the touchscreen
if there is movement without "disturb" the normal operation from it?

(I am a little confused: In the rcS I had the
QWS_MOUSE_PROTO=TSLIB:/dev/input/ts-if to get the touchscreen working
without Qtopia, but if I try to read in the above code from the same
device, I get the "Cannot access" message. From the shell:
[root@FriendlyARM /]# cat /dev/input/ts-if 
cat: read error: Invalid argument

cat /dev/touchscreen-1wire only sometimes makes an output if I touch or
draw on the screen?!? But the screen works.)

sv1eed
i dont have an 6410, but here are some suggestions:

this way of checking a file is not very efficient.

better check the file for the touch screen input events (whatever it is)
using the poll function and checking for POLLIN event

this way there is no need for timers or delays

try modifying this part of the code i use for mini2440:
__________________________________________________________________
  int fd;
  char buf[64];
  int r;
  struct pollfd pfds[1];
  
  fd = open(event_file, O_RDONLY);

  pfds[0].fd = fd; // the opened (readonly) dev file for the touchscreen
events
  pfds[0].events = POLLIN;
    
  while (1) {


    poll(pfds, 1, timeout); // screensave timeout in milliseconds

    if (pfds[0].revents & POLLIN) {

      r = read(fd, buf, 64);
      if (!r) {
      return 0;
      }
    
      if(ss_is_active)
      {
        ss_off();  // turn backlight back on
      }
    }
    else
    {
      if (!ss_is_active)
      {
        ss_on();  // turn backlight off
      }
    }
  }

yong
I have tiny6410 and im interested with your code
i want to try it.. do I have to attach your code somewhere in
the linux kernel to see the board working?