Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/tmx_cpp/tmx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class TMX {
void sendPing(uint8_t num = 0);
void sendMessage(const std::vector<uint8_t> &message);
void sendMessage(MESSAGE_TYPE type, const std::vector<uint8_t> &message);
void sendEmptyMessage();
// Normal functions for use by the user:
void setPinMode(uint8_t pin, TMX::PIN_MODES mode, bool reporting = true,
uint16_t analog_differential = 0);
Expand Down
50 changes: 42 additions & 8 deletions src/tmx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ void TMX::parseOne(const std::vector<uint8_t> &message) {
}
std::cout << std::endl;
#endif

if (message.size() == 1 && message[0] == 0) {
std::cout << "parseOne: empty message received" << std::endl;
return;
}
boost::asio::post(this->parsePool,
std::bind(&TMX::parseOne_task, this, message));
}
Expand All @@ -227,7 +230,6 @@ void TMX::parseOne_task(const std::vector<uint8_t> &message) {
// Note:: this runs on a different thread than any other things.
// Makes it possible to have longer running callbacks without interfering with
// other callbacks and reading in data.

// msg: {len, type, ...}
auto type = (MESSAGE_IN_TYPE)message[1];
#ifdef TMX_TX_DEBUG
Expand Down Expand Up @@ -399,6 +401,13 @@ void TMX::parseOne_task(const std::vector<uint8_t> &message) {
} break;
case MESSAGE_IN_TYPE::SERIAL_LOOP_BACK_REPORT:
std::cout << "Serial loopback not implemented" << std::endl;
// {
// std::cout << "debug len:" << std::dec << (uint)message.size() <<
// std::endl; for(auto i = 0; i < message.size(); i++) {
// std::cout << "debug " << i << ":" << std::dec << (uint)message[i] <<
// std::endl;
// }
// }
break;
case MESSAGE_IN_TYPE::DHT_REPORT: {
for (const auto &callback : this->dht_callbacks) {
Expand Down Expand Up @@ -525,7 +534,16 @@ void TMX::sendPing(uint8_t num) {
void TMX::sendMessage(const std::vector<uint8_t> &message) {
std::vector<char> charMessage(message.begin(), message.end());
charMessage.insert(charMessage.begin(), charMessage.size());
serial->write(charMessage);
// serial write sometimes hangs when pico is restarted, use future and async
// to avoid this
std::future<bool> future =
std::async(std::launch::async, [this, charMessage]() {
serial->write(charMessage);
return true;
});
if (future.wait_for(std::chrono::seconds(1)) == std::future_status::timeout) {
std::cerr << "Serial write timed out" << std::endl;
}
#if 0
std::ofstream file("tmx_data.log", std::ios_base::app);
file << "writing: len = " << charMessage.size()
Expand All @@ -546,10 +564,9 @@ void TMX::sendMessage(MESSAGE_TYPE type, const std::vector<uint8_t> &message) {
std::cout << "Feature not supported: " << type << std::endl;
return;
}
std::vector<char> charMessage(message.begin(), message.end());
charMessage.insert(charMessage.begin(),
{(char)(charMessage.size() + 1), (char)type});

std::vector<uint8_t> charMessage(message.begin(), message.end());
charMessage.insert(charMessage.begin(), {(uint8_t)type});
this->sendMessage(charMessage);
#ifdef TMX_TX_DEBUG
std::cout << "T charMessage = ";
for (auto i : charMessage) {
Expand All @@ -568,8 +585,14 @@ void TMX::sendMessage(MESSAGE_TYPE type, const std::vector<uint8_t> &message) {
file.flush();
file.close();
#endif
serial->write(charMessage);
// serial->write(charMessage);
}

void TMX::sendEmptyMessage() {
std::vector<uint8_t> message = {}; // size 0
this->sendMessage(message);
}

void TMX::setPinMode(uint8_t pin, TMX::PIN_MODES mode, bool reporting,
uint16_t analog_differential) {
std::vector<uint8_t> message;
Expand Down Expand Up @@ -1161,13 +1184,23 @@ void TMX::ping_task() {
std::cout << "got feature ping task!" << std::endl;
if (!this->get_feature(MESSAGE_TYPE::PING).first) {
std::cout << "ping not supported" << std::endl;
std::cerr << "Ping is supported on both Arduino and Pico versions, this "
"shouldn't happen."
<< std::endl;
for (auto i = 0; i < 5; i++) {
this->sendEmptyMessage();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
throw std::runtime_error("Ping not supported, assuming broken "
"communcation, restart the program.");
return;
}
uint8_t num = 0;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
while (!this->is_stopped) {
num++;
this->sendPing(num);
this->sendEmptyMessage();
std::this_thread::sleep_for(std::chrono::milliseconds(400));
uint8_t diff = num - this->last_ping;
if (diff > 10) {
Expand Down Expand Up @@ -1206,6 +1239,7 @@ void TMX::feature_detect_task() {
this->feature_detected = false;
for (auto i = 0; i < (int)MESSAGE_TYPE::MAX && !this->is_stopped; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(30));
// this->sendEmptyMessage();
this->sendMessage(MESSAGE_TYPE::FEATURE_REQUEST, {(uint8_t)i});
}
if (this->is_stopped) {
Expand Down
Loading