MCPcopy Create free account
hub / github.com/SmingHub/Sming / onData

Method onData

samples/Basic_Serial/app/SerialReadingDelegateDemo.cpp:26–78  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24}
25
26void SerialReadingDelegateDemo::onData(Stream& stream, char arrivedChar, unsigned short availableCharsCount)
27{
28 Serial << _F("Class Delegate Demo Time = ") << micros() << _F(" char = 0x") << String(arrivedChar, HEX, 2)
29 << _F(" available = ") << availableCharsCount << endl;
30
31 // Error detection
32 unsigned status = serial->getStatus();
33 if(status != 0) {
34 if(bitRead(status, eSERS_Overflow)) {
35 serial->println(_F("** RECEIVE OVERFLOW **"));
36 }
37 if(bitRead(status, eSERS_BreakDetected)) {
38 serial->println(_F("** BREAK DETECTED **"));
39 }
40 if(bitRead(status, eSERS_FramingError)) {
41 serial->println(_F("** FRAMING ERROR **"));
42 }
43 if(bitRead(status, eSERS_ParityError)) {
44 serial->println(_F("** PARITY ERROR **"));
45 }
46 // Discard what is likely to be garbage
47 serial->clear(SERIAL_RX_ONLY);
48 return;
49 }
50
51 numCallback++;
52
53 /*
54 * A more functional serial command-line interface can be implemented using the `LineBuffer` class.
55 * See `Basic_DateTime` sample application for a demonstration.
56 */
57
58 if(arrivedChar == '\n') // Lets show data!
59 {
60 serial->println(_F("<New line received>"));
61 // Read the string into a line
62 String line;
63 line.reserve(availableCharsCount);
64 while(availableCharsCount--) {
65 char cur = stream.read();
66 charReceived++;
67 serial->print(cur);
68 if(cur != '\n' && cur != '\r') {
69 line += cur;
70 }
71 }
72 serial->println();
73
74 if(callback) {
75 callback(line);
76 }
77 }
78}

Callers 1

executeMethod · 0.45

Calls 8

microsFunction · 0.85
printlnMethod · 0.80
reserveMethod · 0.80
StringClass · 0.50
getStatusMethod · 0.45
clearMethod · 0.45
readMethod · 0.45
printMethod · 0.45

Tested by

no test coverage detected