Skip to content

Commit 23da7a4

Browse files
Correct and celete entry 'lib\' from git ignore. We need that folder.
1 parent cb123fd commit 23da7a4

File tree

8 files changed

+253
-1
lines changed

8 files changed

+253
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ dist/
1919
downloads/
2020
eggs/
2121
.eggs/
22-
lib/
2322
lib64/
2423
parts/
2524
sdist/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\\.git/
2+
.pioenvs/
3+
.sconsign.dblite
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Dennis van Gils, 11-03-2020
3+
*/
4+
5+
#include "DvG_SerialCommand.h"
6+
7+
DvG_SerialCommand::DvG_SerialCommand(Stream& mySerial) :
8+
_port(mySerial) // Initialise reference before body
9+
{
10+
_strIn[0] = '\0';
11+
_fTerminated = false;
12+
_iPos = 0;
13+
}
14+
15+
bool DvG_SerialCommand::available() {
16+
char c;
17+
18+
// Poll serial buffer
19+
if (_port.available()) {
20+
_fTerminated = false;
21+
while (_port.available()) {
22+
c = _port.peek();
23+
if (c == 13) {
24+
// Ignore ASCII 13 (carriage return)
25+
_port.read(); // Remove char from serial buffer
26+
} else if (c == 10) {
27+
// Found the proper termination character ASCII 10 (line feed)
28+
_port.read(); // Remove char from serial buffer
29+
_strIn[_iPos] = '\0'; // Terminate string
30+
_fTerminated = true;
31+
break;
32+
} else if (_iPos < STR_LEN - 1) {
33+
// Maximum length of incoming serial command is not yet reached. Append
34+
// characters to string.
35+
_port.read(); // Remove char from serial buffer
36+
_strIn[_iPos] = c;
37+
_iPos++;
38+
} else {
39+
// Maximum length of incoming serial command is reached. Forcefully
40+
// terminate string now. Leave the char in the serial buffer.
41+
_strIn[_iPos] = '\0'; // Terminate string
42+
_fTerminated = true;
43+
break;
44+
}
45+
}
46+
}
47+
return _fTerminated;
48+
}
49+
50+
char* DvG_SerialCommand::getCmd() {
51+
if (_fTerminated) {
52+
_fTerminated = false; // Reset incoming serial command char array
53+
_iPos = 0; // Reset incoming serial command char array
54+
return (char*) _strIn;
55+
} else {
56+
return (char*) _empty;
57+
}
58+
}
59+
60+
/*------------------------------------------------------------------------------
61+
Parse float value at end of string 'strIn' starting at position 'iPos'
62+
------------------------------------------------------------------------------*/
63+
64+
float parseFloatInString(char* strIn, uint8_t iPos) {
65+
if (strlen(strIn) > iPos) {
66+
return (float) atof(&strIn[iPos]);
67+
} else {
68+
return 0.0f;
69+
}
70+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
This library allows listening to a serial port for incoming commands and act
3+
upon them. To keep the memory usage low, it uses a C-string (null-terminated
4+
character array) to store incoming characters received over the serial port,
5+
instead of using a memory hungry C++ string. Carriage return ('\r', ASCII 13)
6+
characters are ignored. Once a linefeed ('\n', ASCII 10) character is received,
7+
or whenever the incoming message length has exceeded the buffer of size
8+
STR_LEN (defined in DvG_SerialCommand.h), we speak of a received 'command'.
9+
It doesn't matter if the command is ASCII or binary encoded.
10+
11+
'available()' should be called periodically to poll for incoming characters. It
12+
will return true when a new command is ready to be processed. Subsequently, the
13+
command string can be retrieved by calling 'getCmd()'.
14+
15+
Dennis van Gils, 11-03-2020
16+
*/
17+
18+
#ifndef H_DvG_SerialCommand
19+
#define H_DvG_SerialCommand
20+
21+
#include <Arduino.h>
22+
23+
// Buffer size for storing incoming characters. Includes the '\0' termination
24+
// character. Change buffer size to your needs up to a maximum of 255.
25+
#define STR_LEN 32
26+
27+
class DvG_SerialCommand {
28+
public:
29+
DvG_SerialCommand(Stream& mySerial);
30+
31+
// Poll the serial port for characters and append to buffer. Return true if
32+
// a command is ready to be processed.
33+
bool available();
34+
35+
// Return the incoming serial command only when it is ready, otherwise return
36+
// an empty C-string.
37+
char* getCmd();
38+
39+
private:
40+
Stream& _port; // Serial port reference
41+
char _strIn[STR_LEN]; // Incoming serial command string
42+
bool _fTerminated; // Incoming serial command is/got terminated?
43+
uint8_t _iPos; // Index within _strIn to insert new char
44+
const char* _empty = "\0"; // Reply when trying to retrieve command when not
45+
// yet terminated
46+
};
47+
48+
/*------------------------------------------------------------------------------
49+
Parse float value at end of string 'strIn' starting at position 'iPos'
50+
------------------------------------------------------------------------------*/
51+
52+
float parseFloatInString(char* strIn, uint8_t iPos);
53+
54+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) [2018] [Dennis P.M. van Gils]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Serial command listener
2+
3+
This library allows listening to a serial port for incoming commands and act upon them. To keep the memory usage low, it uses a C-string (null-terminated character array) to store incoming characters received over the serial port, instead of using a memory hungry C++ string. Carriage return ('\r', ASCII 13) characters are ignored. Once a linefeed ('\n', ASCII 10) character is received, or whenever the incoming message length has exceeded the buffer of size STR_LEN (defined in DvG_SerialCommand.h), we speak of a received 'command'. It doesn't matter if the command is ASCII or binary encoded.
4+
5+
``available()`` should be called periodically to poll for incoming characters. It will return true when a new command is ready to be processed. Subsequently, the command string can be retrieved by calling ``getCmd()``.
6+
7+
Example usage on an Arduino:
8+
```C
9+
#include <Arduino.h>
10+
#include "DvG_SerialCommand.h"
11+
12+
#define Ser Serial // Listen on this port
13+
DvG_SerialCommand sc(Ser);
14+
15+
void setup() {
16+
Ser.begin(9600); // Open port
17+
}
18+
19+
void loop() {
20+
char* strCmd; // Incoming serial command string
21+
22+
if (sc.available()) {
23+
strCmd = sc.getCmd();
24+
25+
// Your command string comparison routines and actions here
26+
if (strcmp(strCmd, "id?") == 0) {
27+
Ser.println("My Arduino");
28+
}
29+
}
30+
}
31+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Demonstrates how to listen to a serial port on an Arduino for commands.
3+
4+
Will turn the onboard LED at pin 13 on or off when the following commands are
5+
received: 'toggle', 'on', 'off'.
6+
7+
Dennis van Gils, 14-08-2018
8+
*/
9+
10+
#include <Arduino.h>
11+
#include "DvG_SerialCommand.h"
12+
13+
#ifndef PIN_LED
14+
#define PIN_LED 13
15+
#endif
16+
17+
// Serial port to listen to
18+
#define Ser Serial
19+
//#define Ser Serial1
20+
//#define Ser SerialUSB
21+
22+
// Instantiate serial command listener
23+
DvG_SerialCommand sc(Ser);
24+
25+
bool fState = false;
26+
27+
/*------------------------------------------------------------------------------
28+
setup
29+
------------------------------------------------------------------------------*/
30+
31+
void setup() {
32+
Ser.begin(9600);
33+
34+
pinMode(PIN_LED, OUTPUT);
35+
digitalWrite(PIN_LED, fState);
36+
}
37+
38+
/*------------------------------------------------------------------------------
39+
loop
40+
------------------------------------------------------------------------------*/
41+
42+
void loop() {
43+
char* strCmd; // Incoming serial command string
44+
45+
if (sc.available()) {
46+
strCmd = sc.getCmd();
47+
Ser.print("Received: "); Ser.println(strCmd);
48+
49+
if (strcmp(strCmd, "toggle") == 0) {
50+
Ser.println(" -> Toggling LED");
51+
fState = not(fState);
52+
digitalWrite(PIN_LED, fState);
53+
54+
} else if (strcmp(strCmd, "on") == 0) {
55+
Ser.println(" -> LED ON");
56+
fState = true;
57+
digitalWrite(PIN_LED, fState);
58+
59+
} else if (strcmp(strCmd, "off") == 0) {
60+
Ser.println(" -> LED OFF");
61+
fState = false;
62+
digitalWrite(PIN_LED, fState);
63+
}
64+
}
65+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=DvG_SerialCommand
2+
version=1.0.0
3+
author=Dennis van Gils <vangils.dennis@gmail.com>
4+
maintainer=Dennis van Gils <vangils.dennis@gmail.com>
5+
sentence=Listen to the serial port for commands
6+
paragraph=
7+
category=Communication
8+
url=https://github.com/Dennis-van-Gils/DvG_SerialCommand/
9+
architectures=*

0 commit comments

Comments
 (0)