Subversion Repositories libSerial

Rev

Rev 14 | 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. #if defined __cplusplus
  6. extern "C"
  7. {
  8. #endif
  9.  
  10.  
  11. void sendString(struct usart_ctl *ctl, char const *string, int length)
  12. {
  13.   for (int i = 0; i < length; i++)
  14.     PutCharSerial(ctl, string[i]);
  15. }
  16.  
  17. void initReadLine(editBuffer *context, char *buffer, int limit, char readLines)
  18. {
  19.   context->buffer = buffer;
  20.   context->limit = limit;
  21.   context->counter = 0;
  22.   context->complete = 0;
  23.   context->readLines = readLines; // if readlines , then we look for \r at the end of a line and skip control chars
  24.   context->buffer[0] = 0;
  25. }
  26.  
  27. editBufferReturn  readLine(struct 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.  
  60.  
  61. void  resetInput(editBuffer * context)
  62. {
  63.   context->counter = 0;
  64.   context->complete = 0;
  65. }
  66.  
  67. int charCount(editBuffer * context)
  68. {
  69.   return context->counter;
  70. }
  71.  
  72.  
  73. #if defined __cplusplus
  74. }
  75. #endif
  76.