Snorktracker
 All Data Structures Files Functions Variables Macros Pages
Serial.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 SFini
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
26 class MySerial : public SoftwareSerial
27 {
28 protected:
29  char inData[255];
30  int inIdx;
31  char outData[255];
32  int outIdx;
34  bool &debug;
35 
36 public:
37  MySerial(StringList &li, bool &dbg, uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
38 
39  virtual int read();
40  virtual size_t write(uint8_t byte);
41 };
42 
43 /* ******************************************** */
44 
46 MySerial::MySerial(StringList &li, bool &dbg, uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /*= false*/)
47  : SoftwareSerial(receivePin, transmitPin, inverse_logic)
48  , inIdx(0)
49  , outIdx(0)
50  , logInfos(li)
51  , debug(dbg)
52 {
53 }
54 
59 {
60  int ret = SoftwareSerial::read();
61 
62  if (ret >= 0 && debug) {
63  char c = (char) ret;
64 
65  if (c != '\r' && c != '\n') {
66  if (inIdx < 250) {
67  inData[inIdx++] = c;
68  }
69  } else {
70  if (inIdx > 0) {
71  inData[inIdx] = 0;
72 
73  String info = (String) F("< ") + (String) inData;
74  logInfos.addTail(info);
75  // Pass thrue to the default Serial for debugging
76  Serial.println(info);
77  }
78  inIdx = 0;
79  }
80  }
81 
82  return ret;
83 }
84 
86 size_t MySerial::write(uint8_t byte)
87 {
88  size_t ret = SoftwareSerial::write(byte);
89 
90  if (debug) {
91  char c = (char)byte;
92 
93  if (c != '\r' && c != '\n') {
94  if (outIdx < 250) {
95  outData[outIdx++] = c;
96  }
97  } else {
98  if (outIdx > 0) {
99  outData[outIdx] = 0;
100 
101  String info = (String) F("> ") + (String)outData;
102 
103  logInfos.addTail(info);
104  // Pass thrue to the default Serial for debugging
105  Serial.println(info);
106  }
107  outIdx = 0;
108  }
109  }
110  return ret;
111 }
MySerial(StringList &li, bool &dbg, uint8_t receivePin, uint8_t transmitPin, bool inverse_logic=false)
Definition: Serial.h:46
StringList & logInfos
Hook pointer for the data logging.
Definition: Serial.h:33
virtual size_t write(uint8_t byte)
Definition: Serial.h:86
int outIdx
How many bytes are read.
Definition: Serial.h:32
char inData[255]
Helper data for a serial write.
Definition: Serial.h:29
char outData[255]
Helper data for a serial read.
Definition: Serial.h:31
bool & debug
Enable or disable the hooking.
Definition: Serial.h:34
int inIdx
How many bytes are written.
Definition: Serial.h:30
void addTail(String newInfo)
Definition: StringList.h:112
virtual int read()
Definition: Serial.h:58