chore(workspace, xod-client-electron): add clang-format config, format new library and node implementations

This commit is contained in:
Kirill Shumilov
2018-07-05 18:02:27 +03:00
parent e334ad294c
commit b6918100ad
29 changed files with 164 additions and 89 deletions

6
.clang-format Normal file
View File

@@ -0,0 +1,6 @@
---
Language: Cpp
BasedOnStyle: WebKit
BreakBeforeBraces: Attach
NamespaceIndentation: None
...

View File

@@ -48,7 +48,7 @@ namespace xod {
class Uart;
}
class ESP8266 {
private:
private:
xod::Uart* _uart;
#ifdef ESP8266_DEBUG
Stream* _debug = nullptr;
@@ -67,9 +67,9 @@ protected:
}
}
void debugChar(char ch) {
if (_debug) {
_debug->print(ch);
}
if (_debug) {
_debug->print(ch);
}
}
#endif
@@ -206,31 +206,30 @@ public:
};
void ESP8266::begin() {
_uart->begin();
_uart->begin();
}
bool ESP8266::kick() {
writeCmd(AT, EOL);
return cmdOK(OK, ERROR, 5000);
writeCmd(AT, EOL);
return cmdOK(OK, ERROR, 5000);
}
bool ESP8266::setStationMode() {
writeCmd(CWMODE_SET);
println("1");
return cmdOK(OK, ERROR, 1000);
writeCmd(CWMODE_SET);
println("1");
return cmdOK(OK, ERROR, 1000);
}
bool ESP8266::connect(const char* ssid, const char* password) {
writeCmd(CWJAP);
print(ssid);
writeCmd(COMMA_2);
print(password);
writeCmd(DOUBLE_QUOTE, EOL);
cmdOK(OK, FAIL, 15000);
return isConnectedToAP();
writeCmd(CWJAP);
print(ssid);
writeCmd(COMMA_2);
print(password);
writeCmd(DOUBLE_QUOTE, EOL);
cmdOK(OK, FAIL, 15000);
return isConnectedToAP();
}
bool ESP8266::isConnectedToAP() {
writeCmd(CIFSR, EOL);
uint8_t code = readCmd(NO_IP, ERROR, 350);
@@ -257,38 +256,42 @@ uint32_t ESP8266::getIP() {
}
bool ESP8266::createTCP(const char* addr, uint32_t port) {
char _port[32];
writeCmd(CIPSTART);
writeCmd(TCP);
writeCmd(COMMA_2);
print(addr);
writeCmd(COMMA_1);
dtostrf(port, 0, 0, _port);
println(_port);
char _port[32];
writeCmd(CIPSTART);
writeCmd(TCP);
writeCmd(COMMA_2);
print(addr);
writeCmd(COMMA_1);
dtostrf(port, 0, 0, _port);
println(_port);
bool ok = cmdOK(OK, ERROR, 5000);
if (ok) _connected = true;
return ok;
bool ok = cmdOK(OK, ERROR, 5000);
if (ok)
_connected = true;
return ok;
}
bool ESP8266::releaseTCP() {
if (!_connected) return true;
writeCmd(CIPCLOSE, EOL);
bool ok = cmdOK(OK, ERROR, 5000);
if (ok) _connected = false;
return ok;
if (!_connected)
return true;
writeCmd(CIPCLOSE, EOL);
bool ok = cmdOK(OK, ERROR, 5000);
if (ok)
_connected = false;
return ok;
}
bool ESP8266::send(char* message) {
writeCmd(CIPSEND);
writeCmd(CIPSEND);
size_t len = sprintf(message, "%s", message);
char reqLen[len];
dtostrf(len, 0, 0, reqLen);
println(reqLen);
size_t len = sprintf(message, "%s", message);
char reqLen[len];
dtostrf(len, 0, 0, reqLen);
println(reqLen);
bool prompt = cmdOK(PROMPT, LINK_IS_NOT);
if (!prompt) return false;
bool prompt = cmdOK(PROMPT, LINK_IS_NOT);
if (!prompt)
return false;
print(message);
bool ok = cmdOK(SEND_OK, nullptr, 5000);
@@ -296,15 +299,15 @@ bool ESP8266::send(char* message) {
}
bool ESP8266::read(char* outBuff) {
if(_uart->available()) {
*outBuff = _uart->toStream()->read();
return true;
}
return false;
if (_uart->available()) {
*outBuff = _uart->toStream()->read();
return true;
}
return false;
}
bool ESP8266::isSocketOpen() {
return _connected;
return _connected;
}
bool ESP8266::receive(char* r) {
@@ -320,16 +323,16 @@ bool ESP8266::receive(char* r) {
}
}
if (_pkgSize > 0) {
if (_uart->available()) {
if (read(r)) {
_pkgSize--;
return true;
}
if (_pkgSize > 0) {
if (_uart->available()) {
if (read(r)) {
_pkgSize--;
return true;
}
}
}
}
return false;
return false;
}
#endif /* #ifndef __ESP8266_UART_H__ */

View File

@@ -2,7 +2,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))

View File

@@ -1,7 +1,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))

View File

@@ -1,6 +1,8 @@
// clang-format off
{{#global}}
#include <ESP8266UART.h>
{{/global}}
// clang-format on
struct State {
uint8_t mem[sizeof(ESP8266)];
@@ -11,7 +13,9 @@ struct Type {
ESP8266* wifi;
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_INIT>(ctx))

View File

@@ -2,7 +2,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))

View File

@@ -1,6 +1,8 @@
// clang-format off
{{#global}}
#include <ESP8266UART.h>
{{/global}}
// clang-format on
struct State {
};
@@ -10,7 +12,9 @@ struct Type {
ESP8266* wifi;
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
auto dev = getValue<input_DEV>(ctx);

View File

@@ -2,7 +2,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))

View File

@@ -2,7 +2,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_KICK>(ctx))

View File

@@ -2,7 +2,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
auto inet = getValue<input_INET>(ctx);

View File

@@ -2,7 +2,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))
@@ -12,7 +14,7 @@ void evaluate(Context ctx) {
char _host[length(host) + 1] = { 0 };
dump(host, _host);
uint32_t port = (uint32_t) getValue<input_PORT>(ctx);
uint32_t port = (uint32_t)getValue<input_PORT>(ctx);
auto inet = getValue<input_INET>(ctx);
bool res = inet.wifi->createTCP(_host, port);

View File

@@ -1,7 +1,9 @@
struct State {};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))
@@ -15,7 +17,7 @@ void evaluate(Context ctx) {
char r = '\0';
if (inet.wifi->receive(&r)) {
emitValue<output_RES>(ctx, (uint8_t) r);
emitValue<output_RES>(ctx, (uint8_t)r);
emitValue<output_DONE>(ctx, 1);
} else {
emitValue<output_ERR>(ctx, 1);

View File

@@ -2,7 +2,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))

View File

@@ -2,7 +2,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))

View File

@@ -1,7 +1,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))

View File

@@ -2,14 +2,16 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))
return;
auto uart = getValue<input_UART>(ctx);
long baud = (long) getValue<input_BAUD>(ctx);
long baud = (long)getValue<input_BAUD>(ctx);
uart->changeBaudRate(baud);
emitValue<output_DONE>(ctx, 1);
}

View File

@@ -1,7 +1,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))

View File

@@ -1,7 +1,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))

View File

@@ -2,7 +2,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx))

View File

@@ -1,14 +1,16 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void emitErr(Context ctx) {
emitValue<output_ERR>(ctx, 1);
}
void evaluate(Context ctx) {
if (!isInputDirty<input_SEND>(ctx))
if (!isInputDirty<input_SEND>(ctx))
return;
auto uart = getValue<input_UART>(ctx);
@@ -16,10 +18,13 @@ if (!isInputDirty<input_SEND>(ctx))
for (auto it = data.iterate(); it; ++it) {
bool err = !(uart->writeByte((char)*it));
if (err) return emitErr(ctx);
if (err)
return emitErr(ctx);
}
if (!uart->writeByte('\r')) return emitErr(ctx);
if (!uart->writeByte('\n')) return emitErr(ctx);
if (!uart->writeByte('\r'))
return emitErr(ctx);
if (!uart->writeByte('\n'))
return emitErr(ctx);
uart->flush();
emitValue<output_DONE>(ctx, 1);
}

View File

@@ -1,7 +1,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_READ>(ctx))

View File

@@ -1,16 +1,19 @@
// clang-format off
{{#global}}
#include <SoftwareSerial.h>
{{/global}}
// clang-format on
class SoftwareUart : public Uart {
private:
private:
SoftwareSerial _serial;
uint8_t _rx;
uint8_t _tx;
public:
SoftwareUart(uint8_t rx, uint8_t tx, long baud = 9600) : Uart(baud), _serial(rx, tx) {
public:
SoftwareUart(uint8_t rx, uint8_t tx, long baud = 9600)
: Uart(baud)
, _serial(rx, tx) {
_rx = rx;
_tx = tx;
}
@@ -20,18 +23,19 @@ class SoftwareUart : public Uart {
void flush();
bool available() {
return (bool) _serial.available();
return (bool)_serial.available();
}
bool writeByte(uint8_t byte) {
return (bool) _serial.write(byte);
return (bool)_serial.write(byte);
}
bool readByte(uint8_t* out) {
int data = _serial.read();
if (data == -1) return false;
*out = data;
return true;
int data = _serial.read();
if (data == -1)
return false;
*out = data;
return true;
}
uint8_t getRX() {
@@ -43,20 +47,20 @@ class SoftwareUart : public Uart {
}
SoftwareSerial* toSoftwareSerial() {
return &_serial;
return &_serial;
}
};
void SoftwareUart::begin() {
_started = true;
_serial.begin(getBaudRate());
_started = true;
_serial.begin(getBaudRate());
};
void SoftwareUart::end() {
_started = false;
_serial.end();
_started = false;
_serial.end();
};
void SoftwareUart::flush() {
_serial.flush();
_serial.flush();
}
struct State {
@@ -64,13 +68,15 @@ struct State {
SoftwareUart* uart;
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
auto state = getState(ctx);
uint8_t rx = getValue<input_RX>(ctx);
uint8_t tx = getValue<input_TX>(ctx);
long baud = (long) getValue<input_BAUD>(ctx);
long baud = (long)getValue<input_BAUD>(ctx);
state->uart = new (state->mem) SoftwareUart(rx, tx, baud);
emitValue<output_UART>(ctx, state->uart);
}

View File

@@ -3,10 +3,12 @@ struct State {
HardwareUart* uart;
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
auto state = getState(ctx);
state->uart = new (state->mem) HardwareUart(Serial, (uint32_t) getValue<input_BAUD>(ctx));
state->uart = new (state->mem) HardwareUart(Serial, (uint32_t)getValue<input_BAUD>(ctx));
emitValue<output_UART>(ctx, state->uart);
}

View File

@@ -3,10 +3,12 @@ struct State {
HardwareUart* uart;
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
auto state = getState(ctx);
state->uart = new (state->mem) HardwareUart(Serial1, (uint32_t) getValue<input_BAUD>(ctx));
state->uart = new (state->mem) HardwareUart(Serial1, (uint32_t)getValue<input_BAUD>(ctx));
emitValue<output_UART>(ctx, state->uart);
}

View File

@@ -3,10 +3,12 @@ struct State {
HardwareUart* uart;
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
auto state = getState(ctx);
state->uart = new (state->mem) HardwareUart(Serial2, (uint32_t) getValue<input_BAUD>(ctx));
state->uart = new (state->mem) HardwareUart(Serial2, (uint32_t)getValue<input_BAUD>(ctx));
emitValue<output_UART>(ctx, state->uart);
}

View File

@@ -3,10 +3,12 @@ struct State {
HardwareUart* uart;
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
auto state = getState(ctx);
state->uart = new (state->mem) HardwareUart(Serial3, (uint32_t) getValue<input_BAUD>(ctx));
state->uart = new (state->mem) HardwareUart(Serial3, (uint32_t)getValue<input_BAUD>(ctx));
emitValue<output_UART>(ctx, state->uart);
}

View File

@@ -5,7 +5,8 @@ private:
uint8_t _tx;
public:
UsbUart(Serial_ serial, long baud = 9600) : Uart(baud) {
UsbUart(Serial_ serial, long baud = 9600)
: Uart(baud) {
_serial = serial;
}
@@ -54,13 +55,13 @@ void UsbUart::flush() {
_serial.flush();
}
template<typename T>
template <typename T>
struct ChooseUartWrapper {
using UartT = HardwareUart;
using UartT = HardwareUart;
};
template<>
template <>
struct ChooseUartWrapper<Serial_> {
using UartT = UsbUart;
using UartT = UsbUart;
};
struct State {
@@ -68,7 +69,9 @@ struct State {
ChooseUartWrapper<typeof SerialUSB>::UartT* uart;
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
auto state = getState(ctx);

View File

@@ -4,10 +4,12 @@ struct State {
HardwareUart* uart;
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
auto state = getState(ctx);
state->uart = new (state->mem) HardwareUart(SERIAL_PORT_HARDWARE_OPEN, (uint32_t) getValue<input_BAUD>(ctx));
state->uart = new (state->mem) HardwareUart(SERIAL_PORT_HARDWARE_OPEN, (uint32_t)getValue<input_BAUD>(ctx));
emitValue<output_UART>(ctx, state->uart);
}

View File

@@ -1,7 +1,9 @@
struct State {
};
// clang-format off
{{ GENERATED_CODE }}
// clang-format on
void evaluate(Context ctx) {
if (!isInputDirty<input_SEND>(ctx))