usbkeys.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <stdio.h>
  2. #include "minilisp.h"
  3. #include "alloc.h"
  4. #include "stream.h"
  5. #include "compiler_new.h"
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #define KBUFSZ 5
  9. static char usb_key_in[KBUFSZ];
  10. static int ki = 0;
  11. void uspi_keypress_handler (const char *str)
  12. {
  13. printf("[uspi-keyboard] pressed: '%s' (%d)\r\n",str,str[0]);
  14. char k=str[0];
  15. if (strlen(str)>1) {
  16. if (k==27) {
  17. k = str[2];
  18. printf("[uspi-keyboard] esc seq key2: %d",k);
  19. }
  20. if (k==68) k=19;
  21. if (k==67) k=20;
  22. if (k==65) k=17;
  23. if (k==66) k=18;
  24. }
  25. usb_key_in[ki] = k;
  26. ki++;
  27. if (ki>=KBUFSZ) ki = 0;
  28. }
  29. Cell* usbkeys_open(Cell* cpath) {
  30. if (!cpath || cpath->tag!=TAG_STR) {
  31. printf("[usbkeys] open error: non-string path given\r\n");
  32. return alloc_nil();
  33. }
  34. for (int i=0; i<KBUFSZ; i++) {
  35. usb_key_in[i]=0;
  36. }
  37. ki = 0;
  38. return alloc_int(1);
  39. }
  40. Cell* usbkeys_read(Cell* stream) {
  41. char c = 0;
  42. if (ki>0) {
  43. ki--;
  44. c = usb_key_in[0];
  45. // FIFO
  46. for (int i=0; i<KBUFSZ-1; i++) {
  47. usb_key_in[i]=usb_key_in[i+1];
  48. }
  49. }
  50. Cell* res = alloc_string_copy(" ");
  51. ((uint8_t*)res->addr)[0] = c;
  52. return res;
  53. }
  54. Cell* usbkeys_write(Cell* arg) {
  55. // could be used to control LEDs
  56. return NULL;
  57. }
  58. Cell* usbkeys_mmap(Cell* arg) {
  59. return alloc_nil();
  60. }
  61. void mount_usbkeys() {
  62. fs_mount_builtin("/keyboard", usbkeys_open, usbkeys_read, usbkeys_write, 0, usbkeys_mmap);
  63. }