home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 1.2 / amidev_cd_12.iso / reference_library / devices / dev_examples / read_keyboard_matrix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  2.4 KB  |  106 lines

  1. /*
  2.  * Read_Keyboard_Matrix.c
  3.  *
  4.  * Compile with SAS C 5.10  lc -b1 -cfistq -v -y -L
  5.  *
  6.  * Run from CLI only
  7.  */
  8.  
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #include <exec/libraries.h>
  12. #include <dos/dos.h>
  13. #include <devices/keyboard.h>
  14.  
  15. #include <clib/exec_protos.h>
  16. #include <clib/alib_protos.h>
  17.  
  18. #include <stdio.h>
  19.  
  20. #ifdef LATTICE
  21. int CXBRK(void) { return(0); }     /* Disable SAS CTRL/C handling */
  22. int chkabort(void) { return(0); }  /* really */
  23. #endif
  24.  
  25. /*
  26.  * There are keycodes from 0x00 to 0x7F, so the matrix needs to be
  27.  * of 0x80 bits in size, or 0x80/8 which is 0x10 or 16 bytes...
  28.  */
  29. #define MATRIX_SIZE 16L
  30.  
  31. /*
  32.  * This assembles the matrix for display that translates directly
  33.  * to the RAW key value of the key that is up or down
  34.  */
  35.  
  36. VOID Display_Matrix(UBYTE *keyMatrix)
  37. {
  38. SHORT  bitcount;
  39. SHORT  bytecount;
  40. SHORT   mask;
  41. USHORT twobyte;
  42.  
  43. printf("\n    0 1 2 3 4 5 6 7");
  44. printf("\n  +-----------------");
  45. for (bitcount=0;bitcount<16;bitcount++)
  46.     {
  47.     printf("\n%x |",bitcount);
  48.     mask=1 << bitcount;
  49.     for (bytecount=0;bytecount<16;bytecount+=2)
  50.         {
  51.         twobyte=keyMatrix[bytecount] | (keyMatrix[bytecount+1] << 8);
  52.         if (twobyte & mask)
  53.             printf(" *");
  54.         else
  55.             printf(" -");
  56.         }
  57.     }
  58. printf("\n\n");
  59. }
  60.  
  61.  
  62. void main(int argc, char *argv[])
  63. {
  64. extern struct Library *SysBase;
  65. struct IOStdReq *KeyIO;
  66. struct MsgPort  *KeyMP;
  67. UBYTE    *keyMatrix;
  68.  
  69. if (KeyMP=CreatePort(NULL,NULL))
  70.   {
  71.   if (KeyIO=(struct IOStdReq *)CreateExtIO(KeyMP,sizeof(struct IOStdReq)))
  72.     {
  73.     if (!OpenDevice("keyboard.device",NULL,(struct IORequest *)KeyIO,NULL))
  74.       {
  75.       if (keyMatrix=AllocMem(MATRIX_SIZE,MEMF_PUBLIC|MEMF_CLEAR))
  76.         {
  77.          KeyIO->io_Command=KBD_READMATRIX;
  78.          KeyIO->io_Data=(APTR)keyMatrix;
  79.          KeyIO->io_Length= SysBase->lib_Version >= 36 ? MATRIX_SIZE : 13;
  80.          DoIO((struct IORequest *)KeyIO);
  81.  
  82.         /* Check for CLI startup... */
  83.         if (argc)
  84.             Display_Matrix(keyMatrix);
  85.  
  86.         FreeMem(keyMatrix,MATRIX_SIZE);
  87.         }
  88.     else
  89.           printf("Error: Could not allocate keymatrix memory\");
  90.  
  91.       CloseDevice((struct IORequest *)KeyIO);
  92.       }
  93.       else
  94.         printf("Error: Could not open keyboard.device\n");
  95.  
  96.     DeleteExtIO((struct IORequest *)KeyIO);
  97.     }
  98.     else
  99.       printf("Error: Could not create I/O request\n");
  100.  
  101.   DeletePort(KeyMP);
  102.   }
  103.   else
  104.     printf("Error: Could not create message port\n");
  105. }
  106.