Skip to content

Commit 36095da

Browse files
committed
Revert "Wrapping chrono for arduino compatibility"
This reverts commit d94191c.
1 parent d94191c commit 36095da

8 files changed

Lines changed: 40 additions & 181 deletions

File tree

README.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ This library is compatible with both **PlatformIO** and **Arduino** toolchains.
2020
2121
No migration is required for existing PlatformIO projects.
2222

23+
Documentation
24+
-------------
25+
26+
- `Protocol reference <doc/protocol_reference.rst>`_
27+
- `PlatformIO usage example <doc/usage_platformio.rst>`_
28+
- `Arduino usage example <doc/usage_arduino.rst>`_
29+
- `hOn simulator <doc/hon_simulator.rst>`_
30+
- `SmartAir2 simulator <doc/smartair2_simulator.rst>`_
31+
2332
Protocol description
2433
--------------------
2534

@@ -64,12 +73,3 @@ Where:
6473
* **Frame data** - n byte, data of the frame, can be empty. Sometimes first 2 bytes of data are used as a subcommand. Max size 246
6574
* **Checksum** - 1 byte, the least significant byte of the sum of all bytes of the frame except separator bytes, CRC, and checksum itself.
6675
* **CRC** - 2 bytes, CRC 16 of all bytes of the frame except separator bytes, checksum byte, and CRC itself (`CRC-16/ARC <https://crccalc.com/?crc=&method=CRC-16/ARC&datatype=1&outtype=0>`_ algorithm used). CRC is available only if the frame flags byte indicates it.
67-
68-
Documentation
69-
-------------
70-
71-
- `Protocol reference <doc/protocol_reference.rst>`_
72-
- `PlatformIO usage example <doc/usage_platformio.rst>`_
73-
- `Arduino usage example <doc/usage_arduino.rst>`_
74-
- `hOn simulator <doc/hon_simulator.rst>`_
75-
- `SmartAir2 simulator <doc/smartair2_simulator.rst>`_

src/protocol/haier_protocol.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#include <cstring>
2+
#include <chrono>
23
#include <memory>
3-
#include "utils/simple_time.h"
44
#include "protocol/haier_protocol.h"
55

66
namespace haier_protocol
77
{
88

99
constexpr uint8_t MAX_PACKET_RETRIES = 9;
10-
constexpr simple_time::ms_t DEFAULT_ANSWER_TIMEOUT = 200;
11-
constexpr simple_time::ms_t DEFAULT_COOLDOWN_INTERVAL = 400;
10+
constexpr std::chrono::milliseconds DEFAULT_ANSWER_TIMEOUT = std::chrono::milliseconds(200);
11+
constexpr std::chrono::milliseconds DEFAULT_COOLDOWN_INTERVAL = std::chrono::milliseconds(400);
1212

1313
#if HAIER_LOG_LEVEL > 3
14-
static simple_time::ms_t last_message_sent_;
14+
static std::chrono::steady_clock::time_point last_message_sent_;
1515
#endif
1616

1717
ProtocolHandler::ProtocolHandler(ProtocolStream &stream) noexcept : ProtocolHandler(stream, MAX_FRAME_SIZE + 10)
@@ -34,14 +34,14 @@ ProtocolHandler::ProtocolHandler(ProtocolStream& stream, size_t buffer_size) noe
3434
answer_timeout_interval_(DEFAULT_ANSWER_TIMEOUT),
3535
cooldown_interval_(DEFAULT_COOLDOWN_INTERVAL)
3636
{
37-
this->cooldown_time_point_ = simple_time::zero_ms();
37+
this->cooldown_time_point_ = std::chrono::steady_clock::time_point();
3838
}
3939

4040
void ProtocolHandler::loop()
4141
{
4242
this->transport_.read_data();
4343
this->transport_.process_data();
44-
simple_time::ms_t now = simple_time::now_ms();
44+
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
4545
switch (this->state_)
4646
{
4747
case ProtocolState::IDLE:
@@ -135,7 +135,7 @@ void ProtocolHandler::loop()
135135
if (this->transport_.available() > 0)
136136
{
137137
#if HAIER_LOG_LEVEL > 3
138-
HAIER_LOGD("Answer delay %dms", (int)(now - last_message_sent_));
138+
HAIER_LOGD("Answer delay %dms", std::chrono::duration_cast<std::chrono::milliseconds>(now - last_message_sent_));
139139
#endif
140140
TimestampedFrame frame;
141141
this->transport_.pop(frame);
@@ -175,7 +175,7 @@ bool ProtocolHandler::write_message_(const HaierMessage &message, bool use_crc)
175175
{
176176
HAIER_LOGE("Error sending message: %02X", frame_type);
177177
}
178-
simple_time::ms_t now = simple_time::now_ms();
178+
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
179179
#if HAIER_LOG_LEVEL > 3
180180
last_message_sent_ = now;
181181
#endif
@@ -185,32 +185,32 @@ bool ProtocolHandler::write_message_(const HaierMessage &message, bool use_crc)
185185

186186
void ProtocolHandler::set_answer_timeout(long long answer_timeout_miliseconds)
187187
{
188-
this->set_answer_timeout(simple_time::ms_t(answer_timeout_miliseconds));
188+
this->set_answer_timeout(std::chrono::milliseconds(answer_timeout_miliseconds));
189189
}
190190

191-
void ProtocolHandler::set_answer_timeout(simple_time::ms_t answer_timeout)
191+
void ProtocolHandler::set_answer_timeout(std::chrono::milliseconds answer_timeout)
192192
{
193193
this->answer_timeout_interval_ = answer_timeout;
194194
}
195195

196196
void ProtocolHandler::set_cooldown_interval(long long answer_timeout_miliseconds)
197197
{
198-
this->set_cooldown_interval(simple_time::ms_t(answer_timeout_miliseconds));
198+
this->set_cooldown_interval(std::chrono::milliseconds(answer_timeout_miliseconds));
199199
}
200200

201-
void ProtocolHandler::set_cooldown_interval(simple_time::ms_t answer_timeout)
201+
void ProtocolHandler::set_cooldown_interval(std::chrono::milliseconds answer_timeout)
202202
{
203203
this->cooldown_interval_ = answer_timeout;
204204
}
205205

206-
void ProtocolHandler::send_message(const HaierMessage& message, bool use_crc, uint8_t num_repeats, simple_time::ms_t interval)
206+
void ProtocolHandler::send_message(const HaierMessage& message, bool use_crc, uint8_t num_repeats, std::chrono::milliseconds interval)
207207
{
208208
this->outgoing_messages_.push({ message, use_crc, false, std::min(num_repeats, MAX_PACKET_RETRIES) + 1, interval });
209209
}
210210

211211
void ProtocolHandler::send_message_without_answer(const HaierMessage& message, bool use_crc)
212212
{
213-
this->outgoing_messages_.push({ message, use_crc, true, 1, simple_time::zero_ms() });
213+
this->outgoing_messages_.push({ message, use_crc, true, 1, std::chrono::milliseconds::zero() });
214214
}
215215

216216
void ProtocolHandler::send_answer(const HaierMessage &answer)

src/protocol/haier_protocol.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <stdint.h>
55
#include <stddef.h>
6-
#include "utils/simple_time.h"
6+
#include <chrono>
77
#include <functional>
88
#include <map>
99
#include <queue>
@@ -60,10 +60,10 @@ class ProtocolHandler
6060
size_t get_outgoing_queue_size() const noexcept {return this->outgoing_messages_.size(); };
6161
bool is_waiting_for_answer() const {return (this->state_ == ProtocolState::WAITING_FOR_ANSWER); };
6262
void set_answer_timeout(long long answer_timeout_miliseconds);
63-
void set_answer_timeout(simple_time::ms_t answer_timeout);
63+
void set_answer_timeout(std::chrono::milliseconds answer_timeout);
6464
void set_cooldown_interval(long long answer_timeout_miliseconds);
65-
void set_cooldown_interval(simple_time::ms_t answer_timeout);
66-
void send_message(const HaierMessage& message, bool use_crc, uint8_t num_retries = 0, simple_time::ms_t interval = simple_time::zero_ms());
65+
void set_cooldown_interval(std::chrono::milliseconds answer_timeout);
66+
void send_message(const HaierMessage& message, bool use_crc, uint8_t num_retries = 0, std::chrono::milliseconds interval = std::chrono::milliseconds::zero());
6767
void send_message_without_answer(const HaierMessage& message, bool use_crc);
6868
void send_answer(const HaierMessage& answer);
6969
void send_answer(const HaierMessage& answer, bool use_crc);
@@ -92,7 +92,7 @@ class ProtocolHandler
9292
bool use_crc;
9393
bool no_answer;
9494
int number_of_retries;
95-
simple_time::ms_t retry_interval;
95+
std::chrono::milliseconds retry_interval;
9696
};
9797
using OutgoingQueue = std::queue<OutgoingQueueItem>;
9898
TransportLevelHandler transport_;
@@ -108,11 +108,11 @@ class ProtocolHandler
108108
bool incoming_message_crc_status_;
109109
bool answer_sent_;
110110
FrameType last_message_type_;
111-
simple_time::ms_t answer_timeout_interval_;
112-
simple_time::ms_t cooldown_interval_;
113-
simple_time::ms_t cooldown_time_point_;
114-
simple_time::ms_t answer_time_point_;
115-
simple_time::ms_t retry_time_point_;
111+
std::chrono::milliseconds answer_timeout_interval_;
112+
std::chrono::milliseconds cooldown_interval_;
113+
std::chrono::steady_clock::time_point cooldown_time_point_;
114+
std::chrono::steady_clock::time_point answer_time_point_;
115+
std::chrono::steady_clock::time_point retry_time_point_;
116116
};
117117

118118

src/transport/protocol_transport.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace haier_protocol
77
{
88

9-
constexpr simple_time::ms_t FRAME_TIMEOUT_MS = 300;
9+
constexpr std::chrono::duration<long long, std::milli> FRAME_TIMEOUT(300);
1010

1111
TransportLevelHandler::TransportLevelHandler(ProtocolStream &stream, size_t buffer_size) noexcept : stream_(stream),
1212
buffer_(buffer_size),
@@ -81,8 +81,8 @@ void TransportLevelHandler::process_data()
8181
{
8282
if (this->current_frame_.get_status() > FrameStatus::FRAME_EMPTY)
8383
{
84-
simple_time::ms_t now = simple_time::now_ms();
85-
if ((now - this->frame_start_) > FRAME_TIMEOUT_MS)
84+
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
85+
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - this->frame_start_) > FRAME_TIMEOUT)
8686
{
8787
// Timeout
8888
HAIER_LOGW("Frame timeout!");
@@ -112,7 +112,6 @@ void TransportLevelHandler::process_data()
112112
if (this->pos_ - bytes_to_drop == FRAME_SEPARATORS_COUNT)
113113
{
114114
this->frame_start_found_ = true;
115-
this->frame_start_ = simple_time::now_ms();
116115
if (bytes_to_drop > 0)
117116
{
118117
// Dropping garbage

src/transport/protocol_transport.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define PROTOCOL_TRANSPORT_H
33
#include <stdint.h>
44
#include <cstddef>
5-
#include "utils/simple_time.h"
5+
#include <chrono>
66
#include <queue>
77
#include "utils/haier_log.h"
88
#include "utils/circular_buffer.h"
@@ -15,7 +15,7 @@ namespace haier_protocol
1515
struct TimestampedFrame
1616
{
1717
HaierFrame frame;
18-
simple_time::ms_t timestamp;
18+
std::chrono::steady_clock::time_point timestamp;
1919
};
2020

2121
class TransportLevelHandler
@@ -42,7 +42,7 @@ class TransportLevelHandler
4242
size_t sep_count_;
4343
bool frame_start_found_;
4444
HaierFrame current_frame_;
45-
simple_time::ms_t frame_start_;
45+
std::chrono::steady_clock::time_point frame_start_;
4646
std::queue<TimestampedFrame> incoming_queue_;
4747
};
4848

src/utils/simple_time.h

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,7 @@
1-
#include <Arduino.h>
21
#include <HaierProtocol.h>
32

4-
using namespace haier_protocol;
5-
6-
class UartProtocolStream : public ProtocolStream {
7-
public:
8-
explicit UartProtocolStream(Stream& serial) : serial_(serial) {}
9-
10-
size_t available() noexcept override {
11-
return static_cast<size_t>(serial_.available());
12-
}
13-
14-
size_t read_array(uint8_t* data, size_t len) noexcept override {
15-
size_t read_count = 0;
16-
while (read_count < len && serial_.available() > 0) {
17-
int value = serial_.read();
18-
if (value < 0) {
19-
break;
20-
}
21-
data[read_count++] = static_cast<uint8_t>(value);
22-
}
23-
return read_count;
24-
}
25-
26-
void write_array(const uint8_t* data, size_t len) noexcept override {
27-
serial_.write(data, len);
28-
}
29-
30-
private:
31-
Stream& serial_;
32-
};
33-
34-
UartProtocolStream proto_stream(Serial1);
35-
ProtocolHandler protocol(proto_stream);
36-
37-
HandlerError on_control(FrameType type, const uint8_t* data, size_t size) {
38-
(void)data;
39-
(void)size;
40-
41-
if (type == FrameType::CONTROL) {
42-
HaierMessage confirm(FrameType::CONFIRM);
43-
protocol.send_answer(confirm);
44-
return HandlerError::HANDLER_OK;
45-
}
46-
protocol.no_answer();
47-
return HandlerError::UNSUPPORTED_MESSAGE;
48-
}
49-
503
void setup() {
51-
Serial.begin(115200);
52-
Serial1.begin(9600);
53-
54-
protocol.set_message_handler(FrameType::CONTROL, on_control);
554
}
565

576
void loop() {
58-
protocol.loop();
597
}
Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,9 @@
11
#include <Arduino.h>
22
#include <HaierProtocol.h>
33

4-
using namespace haier_protocol;
5-
6-
class UartProtocolStream : public ProtocolStream {
7-
public:
8-
explicit UartProtocolStream(HardwareSerial& serial) : serial_(serial) {}
9-
10-
size_t available() noexcept override {
11-
return static_cast<size_t>(serial_.available());
12-
}
13-
14-
size_t read_array(uint8_t* data, size_t len) noexcept override {
15-
size_t read_count = 0;
16-
while (read_count < len && serial_.available() > 0) {
17-
int value = serial_.read();
18-
if (value < 0) {
19-
break;
20-
}
21-
data[read_count++] = static_cast<uint8_t>(value);
22-
}
23-
return read_count;
24-
}
25-
26-
void write_array(const uint8_t* data, size_t len) noexcept override {
27-
serial_.write(data, len);
28-
serial_.flush();
29-
}
30-
31-
private:
32-
HardwareSerial& serial_;
33-
};
34-
35-
UartProtocolStream proto_stream(Serial2);
36-
ProtocolHandler protocol(proto_stream);
37-
38-
HandlerError on_message(FrameType type, const uint8_t* data, size_t size) {
39-
if (type == FrameType::CONTROL) {
40-
HaierMessage answer(FrameType::CONFIRM);
41-
protocol.send_answer(answer);
42-
return HandlerError::HANDLER_OK;
43-
}
44-
protocol.no_answer();
45-
return HandlerError::UNSUPPORTED_MESSAGE;
46-
}
47-
484
void setup() {
49-
Serial.begin(115200);
50-
Serial2.begin(9600, SERIAL_8N1, 16, 17);
51-
52-
protocol.set_message_handler(FrameType::CONTROL, on_message);
53-
protocol.set_answer_timeout(200);
54-
protocol.set_cooldown_interval(400);
5+
// minimal build-only example
556
}
567

578
void loop() {
58-
protocol.loop();
59-
60-
// Example outgoing request every 5 seconds
61-
static uint32_t last_send = 0;
62-
uint32_t now = millis();
63-
if (now - last_send > 5000) {
64-
uint8_t payload[] = {0x01, 0x02};
65-
HaierMessage msg(FrameType::CONTROL, payload, sizeof(payload));
66-
protocol.send_message(msg, true, 1, 300);
67-
last_send = now;
68-
}
699
}

0 commit comments

Comments
 (0)