Subversion Repositories libSerial

Rev

Rev 13 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /// Functions provided that use serial code, but are more independent
  2.  
  3. #include "libSerial/serialUtils.h"
  4.  
  5. void sendString(usart_ctl *ctl, char const *string, int length)
  6. {
  7.   for (int i = 0; i < length; i++)
  8.     PutCharSerial(ctl, string[i]);
  9. }
  10.  
  11. void initReadLine(editBuffer *context, char *buffer, int limit, char readLines)
  12. {
  13.   context->buffer = buffer;
  14.   context->limit = limit;
  15.   context->counter = 0;
  16.   context->complete = 0;
  17.   context->readLines = readLines; // if readlines , then we look for \r at the end of a line and skip control chars
  18.   context->buffer[0] = 0;
  19. }
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. //
  27. editBufferReturn  readLine(usart_ctl *ctl, editBuffer *context)
  28. {
  29.   int cnt = PollSerial(ctl);
  30.   if (!cnt)
  31.     return EDIT_NULL;
  32.  
  33.   while (cnt && context->counter != context->limit)
  34.   {
  35.     char c = GetCharSerial(ctl);
  36.     if (!context->readLines || c >= ' ')
  37.     {
  38.       context->buffer[context->counter++] =c;
  39.       if (context->counter != context->limit)
  40.         context->buffer[context->counter] = 0;
  41.     }
  42.     if (context->readLines)
  43.     {
  44.       if (c == '\r')
  45.       {
  46.         context->complete = 1;
  47.         return EDIT_CR;
  48.       }
  49.       else if (c < ' ')
  50.       {
  51.         context->counter = 0;
  52.         context->buffer[0] = 0;
  53.       }
  54.     }
  55.   }
  56.  
  57.   return (context->counter == context->limit) ? EDIT_DONE : EDIT_NULL;
  58. }
  59.