mappe1 beta

Dependencies:   mbed

Committer:
GramTech
Date:
Thu Feb 27 13:13:26 2014 +0000
Revision:
3:e55f06ddce90
Parent:
0:b49b25afcee5
Ferdig utgave av BitGuard 3.25.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GramTech 0:b49b25afcee5 1 #include "ctype.h"
GramTech 0:b49b25afcee5 2
GramTech 0:b49b25afcee5 3 class SerialBuff : public Serial
GramTech 0:b49b25afcee5 4 {
GramTech 0:b49b25afcee5 5 public:
GramTech 0:b49b25afcee5 6 enum BuffState {String_Empty, In_Progress, String_Complete};
GramTech 0:b49b25afcee5 7
GramTech 0:b49b25afcee5 8 SerialBuff(int n, PinName tx, PinName rx) : Serial (tx, rx) {
GramTech 0:b49b25afcee5 9 size = n;
GramTech 0:b49b25afcee5 10 line = new char[n];
GramTech 0:b49b25afcee5 11 state = String_Empty;
GramTech 0:b49b25afcee5 12 pos = 0;
GramTech 0:b49b25afcee5 13 }
GramTech 0:b49b25afcee5 14
GramTech 0:b49b25afcee5 15 SerialBuff() : Serial (USBTX, USBRX) {
GramTech 0:b49b25afcee5 16 size = 80;
GramTech 0:b49b25afcee5 17 line = new char[size];
GramTech 0:b49b25afcee5 18 state = String_Empty;
GramTech 0:b49b25afcee5 19 pos = 0;
GramTech 0:b49b25afcee5 20 }
GramTech 0:b49b25afcee5 21
GramTech 0:b49b25afcee5 22 // ~SerialBuff() {
GramTech 0:b49b25afcee5 23 // delete[] line;
GramTech 0:b49b25afcee5 24 // }
GramTech 0:b49b25afcee5 25
GramTech 0:b49b25afcee5 26 // SerialBuff(const SerialBuff& other) {
GramTech 0:b49b25afcee5 27 // //copy ctor
GramTech 0:b49b25afcee5 28 // }
GramTech 0:b49b25afcee5 29
GramTech 0:b49b25afcee5 30 int empty() {
GramTech 0:b49b25afcee5 31 return (pos == 0);
GramTech 0:b49b25afcee5 32 }
GramTech 0:b49b25afcee5 33
GramTech 0:b49b25afcee5 34 int getState() { // check internal state for buffer
GramTech 0:b49b25afcee5 35 return state;
GramTech 0:b49b25afcee5 36 }
GramTech 0:b49b25afcee5 37
GramTech 0:b49b25afcee5 38 int ready() { // complete line from serial input?
GramTech 0:b49b25afcee5 39 return (state == String_Complete);
GramTech 0:b49b25afcee5 40 }
GramTech 0:b49b25afcee5 41
GramTech 0:b49b25afcee5 42 int full() { // more space in buffer ?
GramTech 0:b49b25afcee5 43 return (pos == (size - 1));
GramTech 0:b49b25afcee5 44 }
GramTech 0:b49b25afcee5 45
GramTech 0:b49b25afcee5 46 void reset() { // reset input buffer
GramTech 0:b49b25afcee5 47 pos = 0;
GramTech 0:b49b25afcee5 48 state = 0;
GramTech 0:b49b25afcee5 49 line[0] = '\0';
GramTech 0:b49b25afcee5 50 }
GramTech 0:b49b25afcee5 51
GramTech 0:b49b25afcee5 52 int fill(); // Fill up buffer from serial input
GramTech 0:b49b25afcee5 53 void getBuffer(char *buf); // copy buffer
GramTech 0:b49b25afcee5 54
GramTech 0:b49b25afcee5 55 private:
GramTech 0:b49b25afcee5 56 char *line; // Textbuffer
GramTech 0:b49b25afcee5 57 int size; // Size of Textbuffer
GramTech 0:b49b25afcee5 58 int pos; // Next position to be written
GramTech 0:b49b25afcee5 59 int state; // 0: NEW, 1: Buffer Ready to be read, 2: In progress
GramTech 0:b49b25afcee5 60 };
GramTech 0:b49b25afcee5 61
GramTech 0:b49b25afcee5 62 int SerialBuff::fill() // Fill up buffer from serial input
GramTech 0:b49b25afcee5 63 {
GramTech 0:b49b25afcee5 64 int c;
GramTech 0:b49b25afcee5 65 while(this->readable()) {
GramTech 0:b49b25afcee5 66 c = this->getc();
GramTech 0:b49b25afcee5 67 if (state == String_Empty) { // NEW
GramTech 0:b49b25afcee5 68 if (isspace(c)) {
GramTech 0:b49b25afcee5 69 continue;
GramTech 0:b49b25afcee5 70 } else {
GramTech 0:b49b25afcee5 71 line[pos++] = c;
GramTech 0:b49b25afcee5 72 state = In_Progress; // INSIDE
GramTech 0:b49b25afcee5 73 }
GramTech 0:b49b25afcee5 74 } else if(state == 1) {
GramTech 0:b49b25afcee5 75 if (c == '\n' || c == '\r') {
GramTech 0:b49b25afcee5 76 line[pos] = '\0';
GramTech 0:b49b25afcee5 77 state = String_Complete; // COMPLETE;
GramTech 0:b49b25afcee5 78 } else {
GramTech 0:b49b25afcee5 79 line[pos++] = c;
GramTech 0:b49b25afcee5 80 }
GramTech 0:b49b25afcee5 81 }
GramTech 0:b49b25afcee5 82 }
GramTech 0:b49b25afcee5 83 return state;
GramTech 0:b49b25afcee5 84 }
GramTech 0:b49b25afcee5 85
GramTech 0:b49b25afcee5 86 void SerialBuff::getBuffer(char *buf) // copy buffer
GramTech 0:b49b25afcee5 87 {
GramTech 0:b49b25afcee5 88 if (state == String_Complete) {
GramTech 0:b49b25afcee5 89 strcpy(buf, line);
GramTech 0:b49b25afcee5 90 } else {
GramTech 0:b49b25afcee5 91 *buf = '\0';
GramTech 0:b49b25afcee5 92 }
GramTech 0:b49b25afcee5 93 }