I have a hardware device and the vendor that supplied it gave a bit of C code to listen for button presses which uses ioctl
. The device has an SSD1289 controller.
Push buttons require no additional pins, their status canbe read over SPI.
That's what I want, to read which push button was pressed.
I am trying to replicate this script in Python for my own application, but the _IOR and ioctl requirements are throwing me off.
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#define SSD1289_GET_KEYS _IOR('keys', 1, unsigned char *)
void get_keys(int fd)
{
unsigned char keys;
if (ioctl(fd, SSD1289_GET_KEYS, &keys) == -1)
{
perror("_apps ioctl get");
}
else
{
printf("Keys : %2x\n", keys);
}
}
int main(int argc, char *argv[])
{
char *file_name = "/dev/fb1";
int fd;
fd = open(file_name, O_RDWR);
if (fd == -1)
{
perror("_apps open");
return 2;
}
while(1)
get_keys(fd);
printf("Ioctl Number: (int)%d (hex)%x\n", SSD1289_GET_KEYS, SSD1289_GET_KEYS);
close (fd);
return 0;
}
Now I know that Python has an ioctl module, and at some point I should be calling
file = open("/dev/fb1")
buf = array.array('h', [0])
fcntl.ioctl(file, ????, buf, 1)
I can't figure out what the SSD1289_GET_KEYS
is supposed to be. How do I get this and what is _IOR
?
Also, if this is the wrong approach, knowing that would be a help too. There are libraries such as spidev which are supposedly for SPI, but I don't know what to read using it.
@alexis provided some useful steps below, which got me to this point:
import fcntl
import array
file = open("/dev/fb1")
buf = array.array('h', [0])
fcntl.ioctl(file, -444763391, buf, 1)
Now, pressing a button changes the value of buf if I keep the above in a loop.