Skip to content

Commit 0bf4110

Browse files
authored
Merge pull request #10 from PagoPlus/feature/aovx-protocol
[FEAT] Add aovx protocol
2 parents efe5cdb + 9532b45 commit 0bf4110

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.pio
22
.vscode/c_cpp_properties.json
3+
.aider*

platformio.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ framework = arduino
2929
test_build_src = true
3030
build_unflags = -std=gnu++11
3131
build_flags =
32+
-DDEBUGLOG_DEFAULT_LOG_LEVEL_TRACE
33+
-D__PLATFORMIO_BUILD_DEBUG__=1
3234
-DPIO_UNIT_TESTING=1
3335
-std=gnu++17
3436
lib_deps =

src/PPNet.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "utils/adler.h"
33

44
#include <variant>
5+
#include <Arduino.h>
56

67
using namespace PPNetwork;
78
using namespace PPNetwork::Message;
@@ -20,6 +21,7 @@ constexpr bool validateWriteTargetType(WriteTargetType type)
2021
{
2122
case WriteTargetType::RAW:
2223
case WriteTargetType::SUNTECH:
24+
case WriteTargetType::AOVX:
2325
return true;
2426
default:
2527
return false;
@@ -68,7 +70,6 @@ size_t PPNet::WriteMessage(AnyMessage msg)
6870

6971
// calculate package size (1=code, 4=checksum, rest=payload)
7072
auto totalSize = 1 + 4 + this->packer.size();
71-
assert(totalSize < 255);
7273

7374
auto checksum = PPNetwork::Utils::adler32(packer.data(), packer.size());
7475
uint8_t checksum_arr[4] = {0};
@@ -77,18 +78,36 @@ size_t PPNet::WriteMessage(AnyMessage msg)
7778
switch (this->targetType)
7879
{
7980
case WriteTargetType::RAW:
81+
assert(totalSize < 255);
8082
this->output->write(static_cast<uint8_t>(type));
8183
this->output->write(checksum_arr, 4);
8284
this->output->write(packer.data(), packer.size());
8385
this->output->flush();
8486
break;
8587
case WriteTargetType::SUNTECH:
88+
assert(totalSize < 255);
8689
this->output->write(static_cast<uint8_t>(type));
8790
this->output->write(checksum_arr, 4);
8891
this->output->write(packer.data(), packer.size());
8992
this->output->write("\r\n");
9093
this->output->flush();
9194
break;
95+
case WriteTargetType::AOVX:
96+
assert(totalSize < 511);
97+
{
98+
unsigned long currentTime = millis();
99+
unsigned long elapsedTime = currentTime - this->lastMessageTime;
100+
if (elapsedTime < 120) {
101+
delay(120 - elapsedTime);
102+
}
103+
this->output->write(static_cast<uint8_t>(type));
104+
this->output->write(checksum_arr, 4);
105+
this->output->write(packer.data(), packer.size());
106+
this->output->write("\n");
107+
this->output->flush();
108+
this->lastMessageTime = millis(); // Update the last message timestamp
109+
}
110+
break;
92111
}
93112

94113
return totalSize;

src/PPNet.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ namespace PPNetwork
141141
enum class WriteTargetType : uint8_t
142142
{
143143
RAW = 0x01,
144-
SUNTECH = 0x02
144+
SUNTECH = 0x02,
145+
AOVX = 0x03
145146
};
146147

147148
/**
@@ -165,6 +166,7 @@ namespace PPNetwork
165166
Stream *output;
166167
WriteTargetType targetType;
167168
MsgPack::Packer packer;
169+
unsigned long lastMessageTime = 0; // New member to store the last message timestamp
168170
};
169171
}
170172

test/test-message-encoding.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,25 @@ TEST_F(MessageEncodingTest, encode_SingleCounterMessage)
8585
ASSERT_DATA_EQUALS(serial.getTransmittedData(), {0x02, 0x31, 0xfe, 0x06, 0xc5, 0x94, 0xa3, 0x7a, 0x61, 0x7a, 0x2a, 0xce, 0x02, 0x87, 0x57, 0xb2, 0xcd, 0x05, 0xdc});
8686
serial.clearTransmittedData();
8787
}
88+
89+
TEST_F(MessageEncodingTest, encode_AOVXMessage)
90+
{
91+
FakeSerial serial;
92+
PPNetwork::PPNet ppnet(&serial, PPNetwork::WriteTargetType::AOVX);
93+
PPNetwork::Message::HelloMessage msg;
94+
memset(&msg, 0x00, sizeof(msg));
95+
96+
msg.uniqueId = "TestRunner";
97+
msg.boardIdentifier = "Tester";
98+
msg.version = 0x1234;
99+
msg.boardVersion = 0x4321;
100+
msg.bootId = 0x5353456;
101+
msg.ppnetVersion = 0x00;
102+
ASSERT_EQ(ppnet.WriteMessage(msg), 36); // 35 bytes + 1 byte for newline
103+
ASSERT_DATA_EQUALS(serial.getTransmittedData(), {0x01,
104+
0xda, 0x12, 0x0c, 0x4f,
105+
0x96, 0xaa, 0x54, 0x65, 0x73, 0x74, 0x52, 0x75, 0x6e, 0x6e, 0x65, 0x72, 0xa6, 0x54, 0x65, 0x73,
106+
0x74, 0x65, 0x72, 0xcd, 0x12, 0x34, 0xcd, 0x43, 0x21, 0xce, 0x05, 0x35, 0x34, 0x56,
107+
0x01, 0x0a}); // 0x0a is the newline character
108+
serial.clearTransmittedData();
109+
}

0 commit comments

Comments
 (0)