Skip to content

Commit f1b604a

Browse files
committed
Added new classes to support two way communications and extended messages and software reset
1 parent 1710862 commit f1b604a

9 files changed

+516
-2
lines changed

avr/libraries/IntegerEasyTransfer/README.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* IntegerEasyTransfer 1.0.0 library modified by Julian Sanin, sourced from:
2+
* IntegerEasyTransfer 1.1.0 library modified by Julian Sanin, sourced from:
33
*
44
* EasyTransfer Arduino Library v2.1
55
* details and example sketch:
@@ -44,6 +44,9 @@
4444
* The library supports a maximum of 20 uint8_t or 10 int16_t values.
4545
* Mixed uint8_t and int16_t are allowed but care should be taken that the
4646
* values do not overflow the data buffer.
47+
* 1.1.0
48+
* Added new classes to support two way communications and extended
49+
* messages and software reset.
4750
*
4851
*
4952
* Limits of the Library
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <TwoWayIntegerEasyTransfer.h>
2+
#include <IntegerEasyTransferFeatureManager.h>
3+
4+
// New custom feature.
5+
struct MyFeature : public IntegerEasyTransferFeature {
6+
7+
enum {
8+
MY_COMMAND = 0x0F,
9+
// Add further commands if needed.
10+
};
11+
12+
uint8_t data = 0x00;
13+
14+
bool handleMessage(uint8_t command, IntegerEasyTransfer & request) {
15+
// Filter commands that you are interested in.
16+
switch(command) {
17+
case MY_COMMAND:
18+
data = request.readByte();
19+
return true;
20+
default:
21+
// Got a message that is not handled by this feature.
22+
return false;
23+
}
24+
}
25+
26+
void reset() {
27+
data = 0x00;
28+
}
29+
30+
void sendNewData() {
31+
// Extended feature messages uses following format for sending data:
32+
TwoWayIntegerEasyTransfer.writeByte(
33+
TwoWayIntegerEasyTransfer.MESSAGE_FEATURE
34+
);
35+
TwoWayIntegerEasyTransfer.writeByte(MY_COMMAND);
36+
TwoWayIntegerEasyTransfer.writeByte(++data);
37+
TwoWayIntegerEasyTransfer.sendData();
38+
}
39+
};
40+
41+
// Instance of the feature.
42+
MyFeature myFeature;
43+
44+
// Create a instance of the feature manager.
45+
IntegerEasyTransferFeatureManager featureManager;
46+
47+
void setup() {
48+
// Begin communication channel.
49+
Serial.begin(9600);
50+
TwoWayIntegerEasyTransfer.begin(&Serial);
51+
// Attach callbacks and add features.
52+
TwoWayIntegerEasyTransfer.attach([]() { doSystemReset(); });
53+
featureManager.addFeature(myFeature);
54+
// Perform software reset manually.
55+
doSystemReset();
56+
}
57+
58+
void loop() {
59+
// Keep message pumping up and running.
60+
if (TwoWayIntegerEasyTransfer.hasReceivedData()) {
61+
TwoWayIntegerEasyTransfer.processInput();
62+
}
63+
}
64+
65+
void doSystemReset() {
66+
featureManager.reset();
67+
}

avr/libraries/IntegerEasyTransfer/keywords.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
#######################################
99

1010
IntegerEasyTransfer KEYWORD1
11+
IntegerEasyTransferFeature KEYWORD1
12+
IntegerEasyTransferFeatureManager KEYWORD1
13+
TwoWayIntegerEasyTransfer KEYWORD1
14+
DefaultMessageCallbackFunction KEYWORD1
15+
FeatureMessageCallbackFunction KEYWORD1
16+
SystemResetCallbackFunction KEYWORD1
1117

1218
#######################################
1319
# Methods and Functions (KEYWORD2)
@@ -20,7 +26,17 @@ writeByte KEYWORD2
2026
writeInt KEYWORD2
2127
readByte KEYWORD2
2228
readInt KEYWORD2
29+
sendSystemReset KEYWORD2
30+
hasReceivedData KEYWORD2
31+
processInput KEYWORD2
32+
attach KEYWORD2
33+
addFeature KEYWORD2
34+
handleMessage KEYWORD2
35+
reset KEYWORD2
2336

2437
#######################################
2538
# Constants (LITERAL1)
2639
#######################################
40+
41+
MESSAGE_FEATURE LITERAL1
42+
MESSAGE_SYSTEM_RESET LITERAL1

avr/libraries/IntegerEasyTransfer/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=IntegerEasyTransfer
2-
version=1.0.0
2+
version=1.1.0
33
author=Bill Porter <bill@billporter.info>, Julian Sanin <sanin89julian@gmail.com>
44
maintainer=Julian Sanin <sanin89julian@gmail.com>
55
sentence=A library to interface Arduino boards.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Library for configurable and easy transfers.
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Julian Sanin
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef INTEGER_EASY_TRANSFER_FEATURE_H
28+
#define INTEGER_EASY_TRANSFER_FEATURE_H
29+
30+
#include <stdint.h>
31+
32+
class IntegerEasyTransfer;
33+
34+
class IntegerEasyTransferFeature {
35+
36+
// Link to next feature item, used by FeatureManager.
37+
IntegerEasyTransferFeature * _nextFeature = nullptr;
38+
friend class IntegerEasyTransferFeatureManager;
39+
40+
public:
41+
/// <summary>
42+
/// Handle the incoming request message.
43+
/// </summary>
44+
/// <param name="command">
45+
/// The first command byte of the incoming request message.
46+
/// </param>
47+
/// <param name="request">
48+
/// Further data messages of the incoming request.
49+
/// </param>
50+
/// <returns>
51+
/// True if the message has been handled otherwise false.
52+
/// </returns>
53+
virtual bool handleMessage(
54+
uint8_t command, IntegerEasyTransfer & request
55+
) = 0;
56+
57+
/// <summary>
58+
/// System reset command.
59+
/// </summary>
60+
virtual void reset() = 0;
61+
};
62+
63+
#endif // INTEGER_EASY_TRANSFER_FEATURE_H
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Library for configurable and easy transfers.
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Julian Sanin
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "IntegerEasyTransferFeatureManager.h"
28+
#include "TwoWayIntegerEasyTransfer.h"
29+
30+
static IntegerEasyTransferFeatureManager * _featureManager = nullptr;
31+
32+
IntegerEasyTransferFeatureManager::IntegerEasyTransferFeatureManager()
33+
: _features{ nullptr } {
34+
_featureManager = this;
35+
// Handle extended messages on connected features.
36+
TwoWayIntegerEasyTransfer.attach(
37+
[](IntegerEasyTransfer & request) {
38+
if (_featureManager != nullptr) {
39+
uint8_t extendedCommand = request.readByte();
40+
if (!_featureManager->handleMessage(extendedCommand, request)) {
41+
// TODO: Report unhandled message.
42+
}
43+
}
44+
});
45+
}
46+
47+
bool IntegerEasyTransferFeatureManager::handleMessage(
48+
uint8_t command, IntegerEasyTransfer & request
49+
) {
50+
IntegerEasyTransferFeature * selectedFeature = _features;
51+
while (selectedFeature != nullptr) {
52+
if (selectedFeature->handleMessage(command, request)) {
53+
return true;
54+
}
55+
selectedFeature = selectedFeature->_nextFeature;
56+
}
57+
return false;
58+
}
59+
60+
void IntegerEasyTransferFeatureManager::reset() {
61+
IntegerEasyTransferFeature * selectedFeature = _features;
62+
while (selectedFeature != nullptr) {
63+
selectedFeature->reset();
64+
selectedFeature = selectedFeature->_nextFeature;
65+
}
66+
}
67+
68+
void IntegerEasyTransferFeatureManager::addFeature(
69+
IntegerEasyTransferFeature & feature) {
70+
if (_features != nullptr) {
71+
IntegerEasyTransferFeature * selectedFeature = _features;
72+
while (selectedFeature->_nextFeature != nullptr) {
73+
selectedFeature = selectedFeature->_nextFeature;
74+
}
75+
selectedFeature->_nextFeature = &feature;
76+
selectedFeature->_nextFeature = nullptr;
77+
} else {
78+
_features = &feature;
79+
_features->_nextFeature = nullptr;
80+
}
81+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Library for configurable and easy transfers.
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Julian Sanin
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef INTEGER_EASY_TRANSFER_FEATURE_MANAGER_H
28+
#define INTEGER_EASY_TRANSFER_FEATURE_MANAGER_H
29+
30+
#include "IntegerEasyTransferFeature.h"
31+
32+
class IntegerEasyTransferFeatureManager : public IntegerEasyTransferFeature {
33+
34+
// List of managed features.
35+
IntegerEasyTransferFeature * _features;
36+
37+
public:
38+
IntegerEasyTransferFeatureManager();
39+
40+
bool handleMessage(uint8_t command, IntegerEasyTransfer & request);
41+
void reset();
42+
43+
/// <summary>
44+
/// Adds the feature to the manager. See also:
45+
/// <seealso cref="IntegerEasyTransferFeature" />
46+
/// </summary>
47+
/// <param name="feature">
48+
/// The feature to be added.
49+
/// </param>
50+
void addFeature(IntegerEasyTransferFeature & feature);
51+
};
52+
53+
#endif // INTEGER_EASY_TRANSFER_FEATURE_MANAGER_H

0 commit comments

Comments
 (0)