From 3e88e517a687096915019c78781a833844c38536 Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Tue, 20 Apr 2021 18:38:25 +0200 Subject: [PATCH] Added easier connection directly to the device serial port instead of using Linux/Windows tools. --- Sming/Components/Hosted/component.mk | 11 +- .../Components/Hosted/include/Hosted/Serial.h | 112 ++++++++++++++++++ .../Hosted/init/serial/InitClient.cpp | 21 +++- 3 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 Sming/Components/Hosted/include/Hosted/Serial.h diff --git a/Sming/Components/Hosted/component.mk b/Sming/Components/Hosted/component.mk index d57a632783..49d6f81bd2 100644 --- a/Sming/Components/Hosted/component.mk +++ b/Sming/Components/Hosted/component.mk @@ -18,5 +18,14 @@ endif COMPONENT_VARS += HOSTED_SERVER_IP -COMPONENT_CFLAGS = -DHOSTED_SERVER_IP=$(HOSTED_SERVER_IP) +COMPONENT_VARS += HOSTED_COM_PORT +HOSTED_COM_PORT := "/dev/ttyUSB0" +ifneq ($(COM_PORT),) + HOSTED_COM_PORT:=$(COM_PORT) +endif + +COMPONENT_VARS += HOSTED_COM_SPEED +HOSTED_COM_SPEED := 115200 + +COMPONENT_CFLAGS = -DHOSTED_SERVER_IP=$(HOSTED_SERVER_IP) -DHOSTED_COM_PORT="\"$(HOSTED_COM_PORT)"\" -DHOSTED_COM_SPEED=$(HOSTED_COM_SPEED) COMPONENT_CXXFLAGS := $(COMPONENT_CFLAGS) diff --git a/Sming/Components/Hosted/include/Hosted/Serial.h b/Sming/Components/Hosted/include/Hosted/Serial.h new file mode 100644 index 0000000000..64517e63b5 --- /dev/null +++ b/Sming/Components/Hosted/include/Hosted/Serial.h @@ -0,0 +1,112 @@ +/**** + * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. + * Created 2015 by Skurydin Alexey + * http://github.com/SmingHub/Sming + * All files of the Sming Core are provided under the LGPL v3 license. + * + * Serial.h + * + * @author 2021 Slavey Karadzhov + * + * + ****/ + +#pragma once + +#ifndef ARCH_HOST +#error "Hosted::Serial can be used only on the Host architecture!" +#endif + +#include +#include +#include + +namespace Hosted +{ +class Serial : public Stream +{ +public: + Serial(const String& ttyDevice) : ttyDevice(ttyDevice) + { + } + + ~Serial() + { + transport.closeDevice(); + } + + /** @brief Initialise the serial port + * @param baud BAUD rate of the serial port (Default: 9600) + */ + bool begin(uint32_t baud = 9600) + { + char result = transport.openDevice(ttyDevice.c_str(), baud); + if(result == 1) { + return true; + } + + debug_w("Hosted::Serial:begin error: %d", result); + return false; + } + + size_t write(uint8_t c) override + { + if(transport.writeChar(c)) { + return 1; + } + + return 0; + } + + int available() override + { + return transport.available(); + } + + int read() override + { + int ch; + int result = transport.readChar(reinterpret_cast(&ch), 1); + if(result == 1) { + return ch; + } + + return -1; + } + + size_t readBytes(char* buffer, size_t length) override + { + int result = transport.readBytes(buffer, length, 100, 100); + if(result > 0) { + return result; + } + + return 0; + } + + size_t write(const uint8_t* buffer, size_t size) + { + char result = transport.writeBytes(buffer, size); + if(result == 1) { + return size; + } + + return 0; + } + + int peek() override + { + return -1; + } + + void flush() override + { + } + +private: + String ttyDevice; + + serialib transport; +}; + +} // namespace Hosted diff --git a/Sming/Components/Hosted/init/serial/InitClient.cpp b/Sming/Components/Hosted/init/serial/InitClient.cpp index cb9d2b2745..9a9d22cf19 100644 --- a/Sming/Components/Hosted/init/serial/InitClient.cpp +++ b/Sming/Components/Hosted/init/serial/InitClient.cpp @@ -13,6 +13,15 @@ #include #include +#include + +#ifndef HOSTED_COM_PORT +#define HOSTED_COM_PORT "/dev/ttyUSB0" +#endif + +#ifndef HOSTED_COM_SPEED +#define HOSTED_COM_SPEED 115200 +#endif Hosted::Client* hostedClient{nullptr}; @@ -23,10 +32,20 @@ void __real_host_init(); void __wrap_host_init(); } +Hosted::Serial hostedSerial(HOSTED_COM_PORT); + void __wrap_host_init() { Serial.begin(115200); - hostedClient = new Hosted::Client(Serial); + Serial.printf("Connecting to: %s ...\n", HOSTED_COM_PORT); + + bool serialReady = false; + do { + serialReady = hostedSerial.begin(HOSTED_COM_SPEED); + usleep(200); + } while(!serialReady); + + hostedClient = new Hosted::Client(hostedSerial); hostedClient->getRemoteCommands(); init(); }