home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / avril / packet.c < prev    next >
C/C++ Source or Header  |  1996-03-19  |  2KB  |  65 lines

  1. /* Device packet support for PC version of AVRIL */
  2.  
  3. /* Written by Bernie Roehl, June 1994 */
  4.  
  5. /* Copyright 1994 by Bernie Roehl */
  6.  
  7. /* You may use this code for your own non-commercial projects without
  8.    paying any fees or royalties.  "Non-commercial", in this context,
  9.    means that the software you write is given away for free to anyone
  10.    who wants it.
  11.    
  12.    Commercial use, including shareware, requires a licensing
  13.    fee and a specific written agreement with the author.
  14.  
  15.    All programs created using this software (both commercial and
  16.    non-commercial) must acknowledge the use of the AVRIL library,
  17.    both in the documentation and in a banner screen at the start or
  18.    end of the program.
  19.  
  20.    For more information, contact Bernie Roehl (broehl@uwaterloo.ca).
  21.  
  22. */
  23.  
  24. #include "avril.h"
  25.  
  26. vrl_DevicePacketBuffer *vrl_DeviceCreatePacketBuffer(int buffsize)
  27.     {
  28.     vrl_DevicePacketBuffer *p;
  29.     p = vrl_malloc(sizeof(vrl_DevicePacketBuffer));
  30.     if (p == NULL) return NULL;
  31.     p->buffer = vrl_malloc(buffsize);
  32.     if (p->buffer == NULL)
  33.         {
  34.         vrl_free(p);
  35.         return NULL;
  36.         }
  37.     p->ind = 0;  p->buffsize = buffsize;
  38.     return p;
  39.     }
  40.  
  41. void vrl_DeviceDestroyPacketBuffer(vrl_DevicePacketBuffer *buff)
  42.     {
  43.     if (buff->buffer)
  44.         vrl_free(buff->buffer);
  45.     vrl_free(buff);
  46.     }
  47.  
  48. vrl_Boolean vrl_DeviceGetPacket(vrl_SerialPort *port, vrl_DevicePacketBuffer *buff)
  49.     {
  50.     while (vrl_SerialCheck(port))
  51.         {
  52.         int c = vrl_SerialGetc(port);
  53.         if (c & 0x80)  /* framing bit set */
  54.             buff->ind = 0;
  55.         if (buff->ind < buff->buffsize)
  56.             buff->buffer[buff->ind++] = c;
  57.         if (buff->ind == buff->buffsize)
  58.             {
  59.             ++buff->ind;  /* make sure we only return it once */
  60.             return 1;     /* complete packet received */
  61.             }
  62.         }
  63.     return 0;  /* no data waiting, packet not yet complete */
  64.     }
  65.