home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377b.lha / devices / keyboard / keymatrix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-02-03  |  3.3 KB  |  104 lines

  1. /* Copyright (c) 1990 Commodore-Amiga, Inc.
  2.  *
  3.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  4.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals.
  5.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  6.  * information on the correct usage of the techniques and operating system
  7.  * functions presented in this example.  The source and executable code of
  8.  * this example may only be distributed in free electronic form, via bulletin
  9.  * board or as part of a fully non-commercial and freely redistributable
  10.  * diskette.  Both the source and executable code (including comments) must
  11.  * be included, without modification, in any copy.  This example may not be
  12.  * published in printed form or distributed with any commercial product.
  13.  * However, the programming techniques and support routines set forth in
  14.  * this example may be used in the development of original executable
  15.  * software products for Commodore Amiga computers.
  16.  * All other rights reserved.
  17.  * This example is provided "as-is" and is subject to change; no warranties
  18.  * are made.  All use is at your own risk.  No liability or responsibility
  19.  * is assumed.
  20.  */
  21.  
  22. /*
  23.  * Keyboard device matrix example...
  24.  */
  25.  
  26. #include <exec/types.h>
  27. #include <exec/io.h>
  28. #include <exec/ports.h>
  29. #include <exec/memory.h>
  30. #include <devices/keyboard.h>
  31.  
  32. #include <proto/exec.h>
  33.  
  34. #include <stdio.h>
  35.  
  36. int CXBRK(VOID) { return(0); }
  37.  
  38. /*
  39.  * There are keycodes from 0x00 to 0x7F, so the matrix needs to be
  40.  * of 0x80 bits in size, or 0x80/8 which is 0x10 or 16 bytes...
  41.  */
  42. #define MATRIX_SIZE 16L
  43.  
  44. /*
  45.  * This assembles the matrix for display that translates directly
  46.  * to the RAW key value of the key that is up or down
  47.  */
  48. VOID Display_Matrix(UBYTE *keyMatrix)
  49. {
  50. SHORT  bitcount;
  51. SHORT  bytecount;
  52. SHORT  mask;
  53. USHORT twobyte;
  54.  
  55.     printf("\n    0 1 2 3 4 5 6 7");
  56.     printf("\n  +-----------------");
  57.     for (bitcount=0;bitcount<16;bitcount++)
  58.     {
  59.         printf("\n%x |",bitcount);
  60.         mask=1 << bitcount;
  61.         for (bytecount=0;bytecount<16;bytecount+=2)
  62.         {
  63.             twobyte=keyMatrix[bytecount] | (keyMatrix[bytecount+1] << 8);
  64.             if (twobyte & mask) printf(" *");
  65.             else printf(" -");
  66.         }
  67.     }
  68.     printf("\n\n");
  69. }
  70.  
  71. VOID main(int argc, char *argv[])
  72. {
  73. struct IOStdReq *keyRequest;
  74. struct MsgPort  *keyPort;
  75.        UBYTE    *keyMatrix;
  76.  
  77.     if (keyPort=CreatePort(NULL,NULL))
  78.     {
  79.         if (keyRequest=(struct IOStdReq *)CreateExtIO(keyPort,
  80.                                                   sizeof(struct IOStdReq)))
  81.         {
  82.             if (!OpenDevice("keyboard.device",NULL,
  83.                              (struct IORequest *)keyRequest,NULL))
  84.             {
  85.                 if (keyMatrix=AllocMem(MATRIX_SIZE,MEMF_PUBLIC|MEMF_CLEAR))
  86.                 {
  87.                     keyRequest->io_Command=KBD_READMATRIX;
  88.                     keyRequest->io_Data=(APTR)keyMatrix;
  89.                     keyRequest->io_Length=13;  /* MUST for 1.2/1.3 */
  90.                     DoIO((struct IORequest *)keyRequest);
  91.  
  92.                         /* Check for CLI startup... */
  93.                     if (argc) Display_Matrix(keyMatrix);
  94.  
  95.                     FreeMem(keyMatrix,MATRIX_SIZE);
  96.                 }
  97.                 CloseDevice((struct IORequest *)keyRequest);
  98.             }
  99.             DeleteExtIO((struct IORequest *)keyRequest);
  100.         }
  101.         DeletePort(keyPort);
  102.     }
  103. }
  104.