select() isnt responding to writing on /dev/input/mice [duplicate] - linux

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
select() isn't responding to writing on /dev/input/mice
I am writing a program which monitors through select() on keyboard and mouse device files. It waits for any write operation (this should happen when there is a keystroke or mouse movement) on those file and as soon as there is a write operation, some jobs are executed. But it is not working. The code is given below. Can someone help me?
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<linux/input.h>
#include<linux/uinput.h>
#include<sys/time.h>
#include<unistd.h>
void main()
{
int mouse_fd,kbd_fd,fd_max;
struct input_event ev;
fd_set rfs,wfs;
if((mouse_fd=open("/dev/input/event3",O_WRONLY))==-1)
{
printf("opening mouse device file has failed \n");
}
else
{
printf("opening mouse device file has been successfull \n");
}
if((kbd_fd=open("/dev/input/event2",O_WRONLY))==-1)
{
printf("opening keyboard device file has failed \n");
}
else
{
printf("opening keyboard device file has been successfull \n");
}
if(mouse_fd>kbd_fd)
{
fd_max=mouse_fd;
}
else
{
fd_max=kbd_fd;
}
while(1)
{
FD_ZERO(&rfs);
FD_ZERO(&wfs);
FD_SET(mouse_fd,&rfs);
FD_SET(kbd_fd,&rfs);
FD_SET(mouse_fd,&wfs);
FD_SET(kbd_fd,&wfs);
select((fd_max+1),&rfs,NULL,NULL,NULL);
if(FD_ISSET(mouse_fd,&rfs))
{
printf("test mouse \n");
}
if(FD_ISSET(kbd_fd,&rfs))
{
printf("test keyboard \n");
}
}
}
when I am executing the program it gives output like:
[root#localhost Project]# gcc select.c
[root#localhost Project]# ./a.out
opening mouse device file has been successfull
opening keyboard device file has been successfull
test keyboard
test mouse
test keyboard
test mouse
test keyboard
test mouse
test keyboard
test mouse
test keyboard
test mouse
Though I am not pressing any key or moving the mouse.

You are opening I think wrong files
/dev/input/event3 - keyboard events
/dev/input/event2 - mouse events
check with
cat /dev/input/event3

Related

Issues while reading Writting USB data

I am new to Linux operating system, i am trying to interface GSM module with USB,i interfaced USB with GSM module successfully, but i have problem while reading the data from GSM module, i can able to write data to GSM module but while reading its too difficult for me, some time i can able to read data but some time i can't able to read data, here i copy and pasted my code, please go through the code and please let me where i am failing to read data,
i need to write data normally to USB port and data should be read on interrupt based
USB configuration
int init_ttyusb0(int speed,int parity)
{
fd=open("/dev/ttyUSB1",O_RDWR|O_NDELAY|O_NOCTTY|O_APPEND,S_IRWXU|S_IRWXG|S_IRWXO);|O_NOCTTY|O_SYNC|O_NDELAY);
perror("open");
if(fd==-1)
{
perror("open");
printf("open usb driver failed\n");
//close(fd);
return 1;
}
saio.sa_handler= signal_handler_IO;
saio.sa_flags=0;
saio.sa_restorer=NULL;
sigaction(SIGIO,&saio,NULL);
fcntl(fd,F_SETFL,FNDELAY);
fcntl(fd,F_SETOWN,getpid());
tcgetattr(fd,&tty);
perror("tcgetattr");
cfsetospeed(&tty,speed);
perror("cfsetospeed");
cfsetispeed(&tty,speed);
perror("cfsetispeed");
tty.c_cflag=(tty.c_cflag&~CSIZE)|CS8; // 8bit character
tty.c_iflag&=~IGNBRK;
// tty.c_lflag=0; // No signaling break process
// tty.c_oflag=0; //No remaping no delay
tty.c_cc[VMIN]=0; // read dosen't block
tty.c_cc[VTIME]=10; //0.5 seconds
tty.c_iflag&=~(IXON|IXOFF|IXANY);
tty.c_cflag|=CLOCAL; // igniore modem controls
tty.c_cflag|=CREAD;
tty.c_cflag&=~(PARENB|PARODD);
tty.c_cflag&=~CSTOPB;
tty.c_cflag&=~CRTSCTS;
tty.c_cflag&=~ICANON; // Disabling cananicle from disbling newline
tty.c_cflag&=~ISIG; // Disabling interrupt signaling bits
// tty.c_iflag&=~(IXON|IXOFF|IXANY); // Disabling software flow control
tty.c_lflag&=~ECHO;
tty.c_lflag&=~ECHOE;
tty.c_lflag&=~ECHONL;
tty.c_lflag&=~(IXON|IXOFF|IXANY);
tty.c_oflag&=~OPOST;
tcsetattr(fd,TCSANOW,&tty);
perror("tcsetattr");
printf("UART configured.....................\n");
}
Interrupt handler
-----------------------
void signal_handler_IO(int status)
{
static int gsm_count
read(fd,&zfc_buffer[gsm_count],sizeof(zfc_buffer));
printf("%s\n",zfc_buffer);
memset(zfc_buffer,0,sizeof(zfc_buffer));
fcntl(fd,F_SETFL,!(O_ASYNC));
}
int main(void)
{
int init_ttyusb0(B9600,0)
while(1)
{
printf("\n eneter option > ");
scanf("%d",&operation);
int m=26;
switch(operation)
{
case 0:
memset(buf_rcv,0,sizeof(buf_rcv));
buf[0]='A';
buf[1]='T';
buf[2]='\r';
buf[3]='\0';
write(fd,"AT\r",3);
usleep((3+25)*100);
fcntl(fd,F_SETFL,O_ASYNC); // Enabling interrupt after write
usleep((3+25)*100);
perror("write");
break;
case 1:
write(fd,"AT+CFUN=1\r",10);
usleep(175000);
fcntl(fd,F_SETFL,O_ASYNC);// Enabling interrupt after write
break;
case 2:
memset(buf,0,sizeof(buf));
strcpy(buf,"AT+CCID\r");
write(fd,"AT+CPIN?\r",9);
usleep(150000);
fcntl(fd,F_SETFL,O_ASYNC);// Enabling interrupt after write
break;
}

Arduino mega2560 play an mp3 file while reading another file

My setup is built of Arduino Mega2560 with an Adafruit's Music Maker shield.
I am running their "player_simple" example and listen (by earphone) to an mp3 playing in the background.
That's work.
Once I try to read additional binary file from the same SD card, in parallel to listening to the music, I hear a white noise through the earphone.
Also, the reading of the binary file from the SD card - fails.
After resetting the Arduino couple of time (by pressing the rst button on the board), I am managing to hear the music but the reading of the binary file never happens.
Questions:
I wonder what I am doing wrong? (see the code below)
Is there an option to read the binary file from the SD card while playing a song (loaded on the same SD card) in the background?
Here is the relevant part of the code:
void setup{
if (! musicPlayer.begin()) { // initialize the music player
Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
while (1);
}
Serial.println(F("VS1053 found"));
if (!SD.begin(CARDCS)) {
Serial.println(F("SD failed, or not present"));
while (1); // don't do anything more
}
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(20,20);
// If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
// audio playing
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int
// Play another file in the background, REQUIRES interrupts!
musicPlayer.startPlayingFile("/e01.wav");
myFile = SD.open("S1F25.bin"); // open the binary file
Serial.print (" myfile.read() = ");
Serial.println (myFile.read());
}
void loop() {
volume_control();
if (musicPlayer.stopped()) {
Serial.println("Done playing music");
while (1) {
delay(10); // we're done! do nothing...
}
}
delay(100);
}
void volume_control(){
volume = volume + 1;
if (volume > 90){volume = 90;}
musicPlayer.setVolume(volume,volume);
}

Can't find HC-05 communications speed (findBaud)

I am trying out Arduino Uno + HC-05 using this library. I don't think it is working properly. This is the Serial Monitor's output of the example "echo":
findBaud
Trying 4800... x
Trying 9600... x
Trying 19200... x
Trying 38400... x
Trying 57600... x
Trying 115200... x
No connection
No Connection, waiting...OK
None of the Communication speed works, but I manage to connect my Android phone (w/ Bluetooth Terminal) to the HC-05, which is why you see "OK" at the end of the output. But it is not able to echo my input from the Bluetooth Terminal.
The Arduino code:
#include <Arduino.h>
#include "HC05.h"
#include <SoftwareSerial.h>
HC05 btSerial = HC05(A2, A5, A3, A4); // cmd, state, rx, tx
void setup()
{
DEBUG_BEGIN(57600);
btSerial.findBaud();
}
void loop()
{
btSerial.println("Echo Server- type something");
while (btSerial.connected())
{
if (btSerial.available())
{
btSerial.write(btSerial.read());
}
}
}
How I connect the HC-05 to Arduino:
I just follow the instruction(5V and GND; State, Rx and Tx to A5, A3 and A4 respectively) , except I don't have pin "cmd", but I do have pin "CFG", so I just assume that should be cmd and connect it to A2
(I know I should comment instead of writting an answer, but I need 50 reputation)
Same here.
I tried with and without the lib and with different pins. I also tried an arduino micro and uno.
Always the same: the module's LED blink (when I send data trough RX/TX it seems) but I can't have any answer (neither when switching to command mode with "$$$" and neither with AT commands with KEY pin - on my module it is named EN, maybe for enable, I assume it is the same)
EDIT:
I should have RTFM. Especially this.
Here are my findings :
The bluetooth module is called HC-05 and the whole board I have is ZS-040.
The EN pin is used to switch off the module, if set to LOW
The small button switch can be pushed to enter command mode (I don't have a KEY pin)
Speed is 9600 bauds by default and stay the same when entering command mode
To debug your connection you can use the Android app called BlueSerial
For the record, here is my code :
#include <SoftwareSerial.h>
#define HC05_STATE 5
#define HC05_RXD_ARDUINO_TXD 4
#define HC05_TXD_ARDUINO_RXD 3
#define HC05_EN 2
SoftwareSerial BTSerial(HC05_TXD_ARDUINO_RXD, HC05_RXD_ARDUINO_TXD); // RX | TX
void setup(void)
{
pinMode(HC05_EN, OUTPUT);
digitalWrite(HC05_EN, HIGH); // just to be sure to enable the module, if not plugged it still works
Serial.begin(9600);
BTSerial.begin(9600); // default speed
Serial.println("Ready");
}
void loop(void)
{
if (BTSerial.available())
{
Serial.print("< ");
while (BTSerial.available())
Serial.write(BTSerial.read());
Serial.print("\n");
}
if (Serial.available())
{
Serial.print("> ");
while (Serial.available())
{
char c = Serial.read();
BTSerial.write(c);
Serial.write(c);
}
Serial.print("\n");
}
}

select() isn't responding to writing on /dev/input/mice

I am writing a program which monitors through select() on the keyboard and mouse device files. It waits for any write operation (this should happen when there is a keystroke or mouse movement) on those files and as soon as there is a write operation, some jobs are executed.
But it's not working. My code is given below.
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<linux/input.h>
#include<linux/uinput.h>
#include<sys/time.h>
#include<unistd.h>
void main()
{
int mouse_fd,kbd_fd,fd_max;
struct input_event ev;
fd_set rfs,wfs;
if((mouse_fd=open("/dev/input/event3",O_WRONLY))==-1)
{
printf("opening mouse device file has failed \n");
}
else
{
printf("opening mouse device file has been successfull \n");
}
if((kbd_fd=open("/dev/input/event2",O_WRONLY))==-1)
{
printf("opening keyboard device file has failed \n");
}
else
{
printf("opening keyboard device file has been successfull \n");
}
FD_ZERO(&rfs);
FD_ZERO(&wfs);
FD_SET(mouse_fd,&rfs);
FD_SET(kbd_fd,&rfs);
FD_SET(mouse_fd,&wfs);
FD_SET(kbd_fd,&wfs);
if(mouse_fd>kbd_fd)
{
fd_max=mouse_fd;
}
else
{
fd_max=kbd_fd;
}
while(1)
{
select((fd_max+1),&rfs,NULL,NULL,NULL);
sleep(2);
if(FD_ISSET(mouse_fd,&rfs))
{
printf("test mouse \n");
}
if(FD_ISSET(kbd_fd,&rfs))
{
printf("test keyboard \n");
}
}
}
When I execute the program it produces output like this,
[root#localhost Project]# gcc select.c
[root#localhost Project]# ./a.out
opening mouse device file has been successfull
opening keyboard device file has been successfull
test keyboard
test keyboard
test keyboard
test keyboard
test keyboard
test keyboard
test keyboard
test keyboard
test keyboard
even though I am not pressing any key. Also, the mouse device file is never selected by select(), even though there is a physical mouse movement.
What am I doing wrong?
You need to reinitialize your fd sets before every select call. So, the loop in your program would look like:
while(1)
{
FD_ZERO(&rfs);
FD_ZERO(&wfs);
FD_SET(mouse_fd, &rfs);
FD_SET(kbd_fd, &rfs);
FD_SET(mouse_fd, &wfs);
FD_SET(kbd_fd, &wfs);
select((fd_max+1),&rfs,NULL,NULL,NULL);
// proceed normally
}
Also, per User1's comment on your same question on Stack Overflow, you need to open the devices for reading, because you are trying to read data from them:
// Open device for reading (do you mean to use "/dev/input/mice" here?)
if ((mouse_fd = open("/dev/input/event3", O_RDONLY)) == -1)
Linux includes a select_tut(2) tutorial manpage with explanations of how to use select and an example program which can be useful as a reference. "Select Law" #11 reminds you that you need to reinitialize your fd sets before each call to select.

Getting Arduino LilyPad to switch BlueSmirf v2.11 to/from command mode

A battery powered (2 x AA) Arduino LilyPad should switch a BlueSmirf v2.11 Bluetooth modem to/from command mode (see source code below). The BlueSmirf has been set to 9600 baud.
If the PC connects via Bluetooth (see source code below), the Arduino program runs fine at the beginning (sending multiple "ping\n"). After some time it (LilyPad/BlueSmirf) starts to also send "$$$" and "---\n" over the Bluetooth connection instead of switching to/from command mode.
Any ideas?
Regards,
tamberg
// Arduino source code:
void setup () {
Serial.begin(9600);
}
void loop () {
Serial.print("$$$");
delay(2000); // TODO: Inquiry, etc.
Serial.print("---\n");
delay(100);
Serial.print("ping\n");
delay(2000);
}
// C# source code (runs on PC)
using System;
using System.IO.Ports;
class Program {
static void Main () {
SerialPort p = new SerialPort(
"COM20", 9600, Parity.None, 8, StopBits.One);
using (p) {
p.Open();
while (p.IsOpen) {
Console.Write((char) p.ReadChar());
}
}
}
}
From the datasheet, page 6:
NOTE1 : You can enter command mode
locally over the serial port at any
time when not connected. Once a
connection is made, you can only enter
command mode if the config timer has
not expired. To enable continuous
configuration, set the config timer to
255. Also, if the device is in Auto Master mode 3, you will NOT be able to
enter command mode when connected over
Bluetooth.
My guess would be that the config timer is expiring.

Resources