Skip to content

Can I get the event's current value? #109

@realroot2185

Description

@realroot2185

I have a device with a tristate button.
I can get the new events is it possible to get the current value?

	let d = Device::new_from_path("/dev/input/event1").unwrap();
	  
	loop {
	    let ev = d.next_event(ReadFlag::NORMAL).map(|val| val.1);
	    match ev {
	        Ok(ev) => {
	         	if let Some(ev_type) = ev.event_type() {
                	if ev_type == EventType::EV_ABS {
	        			let state = ev.value;

Using a loop without sleeping after each iteration is quite hard on the battery.

This C function can return the current state:

#include <stdio.h>
#include <linux/input.h>
#include <fcntl.h>

int read_state() {
	int fd;
	int abs[6] = {0};
	
	if ((fd = open("/dev/input/event1", O_RDONLY)) < 0) {
	    perror("evtest");
	    return -1;
	}  
	
	ioctl(fd, EVIOCGABS(34), abs);
	return abs[0];
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions