NSLU2/Usb-keypad

Från Basvrak
Hoppa till navigering Hoppa till sök

Code to use an USB keypad with embedded linux, on for example a NSLU2. There are other examples out there, for example "slugterm", http://chezphil.org/slugterm/, but unfortunately they don't work with my keypad for reasons described below. Also, this is C which is much more compileable on the Slug than C++.

My keypad works like this:

A number key is pressed->keypad sends numlock event->waits for numlock LED to be turned on If numlock LED isn't turned on by host, then the keypad hangs forever, so one must ACK the numlock. Then the number is sent, followed by a similar numlock sequence to turn off numlock.

And, yes, I know, the code is butt ugly.


#include <stdint.h>

#include <linux/input.h>

#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int xyzzy=1;

int main (int argc, char **argv)
{
        int fd, rd, i, j, k;
        struct input_event ev[64];
        int version;

        if (argc < 2) {
                printf("Usage: evtest /dev/input/eventX\n");
                printf("Where X = input device number\n");
                return 1;
        }

        if ((fd = open(argv[argc - 1], O_RDWR)) < 0) {
                perror("evtest");
                return 1;
        }

{
 struct input_event ew; /* the event */

                                          /* we turn off all the LEDs to start */
                                            ew.type = EV_LED;
                                                 ew.value = 0;
                                                ew.code = LED_NUML;
                                                write(fd, &ew, sizeof(struct input_event));
                                                printf("NUML is now %s\n", ew.value?"on":"off");
}


        while (1) {
                rd = read(fd, ev, sizeof(struct input_event));

                if (rd < (int) sizeof(struct input_event)) {
                        printf("yyy\n");
                        perror("\nevtest: error reading");
                        return 1;
                }

                for (i = 0; i < rd / sizeof(struct input_event); i++)

                        if (ev[i].type == EV_KEY && ev[i].value == 1) {

                        if((ev[i].code == KEY_NUMLOCK) && (ev[i].value == 1)) {
                                /* acknowledge numlock */
                                            struct input_event ew; /* the event */

                                          /* we turn off all the LEDs to start */
                                            ew.type = EV_LED;
                                                 ew.value = xyzzy;
                                                xyzzy=1-xyzzy;
                                                ew.code = LED_NUML;
                                                write(fd, &ew, sizeof(struct input_event));
                        /*                      printf("NUML is now %s\n", ew.value?"on":"off");
*/

                        }

                        { int ccc=0;
                        switch(ev[i].code) {
                                case KEY_KP9:ccc++;
                                case KEY_KP8:ccc++;
                                case KEY_KP7:ccc++;
                                case KEY_KP6:ccc++;
                                case KEY_KP5:ccc++;
                                case KEY_KP4:ccc++;
                                case KEY_KP3:ccc++;
                                case KEY_KP2:ccc++;
                                case KEY_KP1:ccc++;
                              case KEY_KP0:
                                printf("%d", ccc);
                                fflush(0);
                                break;
                                default:
                                        break;
                        }}
                }
                        if(ev[i].type == EV_LED) {
                                printf("blinkenlicht!\n");
                        }
        }
}