Skip to content

Commit

Permalink
Added easier connection directly to the device serial port instead of…
Browse files Browse the repository at this point in the history
… using Linux/Windows tools.
  • Loading branch information
slav-at-attachix committed Apr 20, 2021
1 parent f1eca99 commit fe6fcdc
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 2 deletions.
11 changes: 10 additions & 1 deletion Sming/Components/Hosted/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
112 changes: 112 additions & 0 deletions Sming/Components/Hosted/include/Hosted/Serial.h
Original file line number Diff line number Diff line change
@@ -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 <slav@attachix.com>
*
*
****/

#pragma once

#ifndef ARCH_HOST
#error "Hosted::Serial can be used only on the Host architecture!"
#endif

#include <Stream.h>
#include <WString.h>
#include <serialib.h>

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<char*>(&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
21 changes: 20 additions & 1 deletion Sming/Components/Hosted/init/serial/InitClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

#include <SmingCore.h>
#include <Hosted/Client.h>
#include <Hosted/Serial.h>

#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};

Expand All @@ -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); // HOSTED_COM_SPEED 115200
usleep(200);
} while(!serialReady);

hostedClient = new Hosted::Client(hostedSerial);
hostedClient->getRemoteCommands();
init();
}

0 comments on commit fe6fcdc

Please sign in to comment.