Rev 15 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 10 | mjames | 1 | /// Functions provided that use serial code, but are more independent |
| 2 | |||
| 3 | #include "libSerial/serialUtils.h" |
||
| 4 | |||
| 15 | mjames | 5 | #if defined __cplusplus |
| 6 | extern "C" |
||
| 10 | mjames | 7 | { |
| 15 | mjames | 8 | #endif |
| 9 | |||
| 10 | |||
| 11 | void sendString(struct usart_ctl *ctl, char const *string, int length) |
||
| 12 | { |
||
| 10 | mjames | 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 | |||
| 15 | mjames | 27 | editBufferReturn readLine(struct usart_ctl *ctl, editBuffer *context) |
| 10 | mjames | 28 | { |
| 29 | int cnt = PollSerial(ctl); |
||
| 30 | if (!cnt) |
||
| 31 | return EDIT_NULL; |
||
| 32 | |||
| 33 | while (cnt && context->counter != context->limit) |
||
| 34 | { |
||
| 16 | mjames | 35 | char c = GetCharSerial(ctl); --cnt; |
| 10 | mjames | 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 | } |
||
| 14 | mjames | 59 | |
| 60 | |||
| 61 | void resetInput(editBuffer * context) |
||
| 62 | { |
||
| 63 | context->counter = 0; |
||
| 64 | context->complete = 0; |
||
| 16 | mjames | 65 | context->buffer[0] = 0; |
| 14 | mjames | 66 | } |
| 67 | |||
| 68 | int charCount(editBuffer * context) |
||
| 69 | { |
||
| 70 | return context->counter; |
||
| 15 | mjames | 71 | } |
| 72 | |||
| 73 | |||
| 74 | #if defined __cplusplus |
||
| 75 | } |
||
| 76 | #endif |