Skip to content

Commit

Permalink
Fix, style, add Travis test, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Mar 11, 2019
1 parent 1c8f0d5 commit 23a3be0
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 262 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ env:
#- TEST_PLATFORM="STM32F1"
- TEST_PLATFORM="teensy35"
- TEST_PLATFORM="linux_native"
- TEST_PLATFORM="esp32"

addons:
apt:
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/HAL_ESP32/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
#include <rom/rtc.h>
#include <driver/adc.h>
#include <esp_adc_cal.h>
#include "ESPAsyncWebServer.h"

#include "../../inc/MarlinConfigPre.h"

#if ENABLED(WIFISUPPORT)
#include <ESPAsyncWebServer.h>
#include "wifi.h"
#if ENABLED(OTASUPPORT)
#include "ota.h"
Expand Down
5 changes: 1 addition & 4 deletions Marlin/src/HAL/HAL_ESP32/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@
#include "i2s.h"

#include "HAL_timers_ESP32.h"
#include "WebSocketSerial.h"

#include "ESPAsyncWebServer.h"

#include "wifi.h"
#include "WebSocketSerial.h"

// --------------------------------------------------------------------------
// Defines
Expand Down
208 changes: 67 additions & 141 deletions Marlin/src/HAL/HAL_ESP32/WebSocketSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#ifdef ARDUINO_ARCH_ESP32

#include "../../inc/MarlinConfig.h"

#if ENABLED(WIFISUPPORT)

#include "WebSocketSerial.h"
#include "AsyncTCP.h"
#include "ESPAsyncWebServer.h"
#include "wifi.h"

#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

struct ring_buffer_r {
unsigned char buffer[RX_BUFFER_SIZE];
Expand All @@ -48,73 +52,57 @@ static bool _written;

AsyncWebSocket ws("/ws"); // access at ws://[esp ip]/ws

static void addToBuffer(AwsFrameInfo *info, uint8_t *data, size_t len){
for(size_t i=0; i < len; i++){
const ring_buffer_pos_t t = rx_buffer.tail;
ring_buffer_pos_t h = rx_buffer.head;
FORCE_INLINE int next_rx_index(const int i) { return (ring_buffer_pos_t)(i + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1); }
FORCE_INLINE int next_tx_index(const int i) { return (ring_buffer_pos_t)(i + 1) & (ring_buffer_pos_t)(TX_BUFFER_SIZE - 1); }

// Get the next element
ring_buffer_pos_t n = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
static void addToBuffer(uint8_t * const data, const size_t len) {
for (size_t i = 0; i < len; i++) {
ring_buffer_pos_t h = rx_buffer.head;
const ring_buffer_pos_t t = rx_buffer.tail, n = next_rx_index(h);

if(n != t){
rx_buffer.buffer[h] = data[i];
h = n;
}
if (n != t) { rx_buffer.buffer[h] = data[i]; h = n; }

// TODO: buffer is full, handle?

rx_buffer.head = h;
}
}

static void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
//Handle WebSocket event
if(type == WS_EVT_CONNECT){
//client connected
client->ping();
} else if(type == WS_EVT_DISCONNECT){
//client disconnected
} else if(type == WS_EVT_ERROR){
//error was received from the other end
} else if(type == WS_EVT_PONG){
//pong message was received (in response to a ping request maybe)
} else if(type == WS_EVT_DATA){
//data packet
AwsFrameInfo * info = (AwsFrameInfo*)arg;
if (info->opcode == WS_TEXT || info->message_opcode == WS_TEXT) {
addToBuffer(info, data, len);
// Handle WebSocket event
static void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
switch (type) {
case WS_EVT_CONNECT: client->ping(); break; // client connected
case WS_EVT_DISCONNECT: // client disconnected
case WS_EVT_ERROR: // error was received from the other end
case WS_EVT_PONG: break; // pong message was received (in response to a ping request maybe)
case WS_EVT_DATA: { // data packet
AwsFrameInfo * info = (AwsFrameInfo*)arg;
if (info->opcode == WS_TEXT || info->message_opcode == WS_TEXT)
addToBuffer(data, len);
}
}
}

// Public Methods
void WebSocketSerial::begin(const long baud_setting) {
// attach AsyncWebSocket
ws.onEvent(onEvent);
server.addHandler(&ws);
server.addHandler(&ws); // attach AsyncWebSocket
}

void WebSocketSerial::end() {
}
void WebSocketSerial::end() { }

int WebSocketSerial::peek(void) {
const int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
return v;
}

int WebSocketSerial::read(void) {
const ring_buffer_pos_t h = rx_buffer.head;
ring_buffer_pos_t t = rx_buffer.tail;

// If nothing to read, return now
if (h == t) return -1;

int v = rx_buffer.buffer[t];
const ring_buffer_pos_t h = rx_buffer.head, t = rx_buffer.tail;
if (h == t) return -1; // Nothing to read? Return now

t = (ring_buffer_pos_t)(t + 1) & (RX_BUFFER_SIZE - 1);
const int v = rx_buffer.buffer[t];

// Advance tail
rx_buffer.tail = t;
rx_buffer.tail = (ring_buffer_pos_t)(t + 1) & (RX_BUFFER_SIZE - 1); // Advance tail

return v;
}
Expand All @@ -129,7 +117,8 @@ void WebSocketSerial::flush(void) {
rx_buffer.tail = rx_buffer.head;
}

// #if TX_BUFFER_SIZE > 0
#if TX_BUFFER_SIZE

void WebSocketSerial::write(const uint8_t c) {
_written = true;

Expand All @@ -147,112 +136,51 @@ void WebSocketSerial::flush(void) {

void WebSocketSerial::flushTx(void) {
ws.textAll("flushTx");
if (!_written) {
return;
}
if (!_written) return;
}
// #else
// void WebSocketSerial::write(const uint8_t c) {
// _written = true;
// }

// void WebSocketSerial::flushTx(void) {
// if (!_written) return;
// }
// #endif

/**
* Imports from print.h
*/

void WebSocketSerial::print(char c, int base) {
print((long)c, base);
}
#else

void WebSocketSerial::print(unsigned char b, int base) {
print((unsigned long)b, base);
}
//void WebSocketSerial::write(const uint8_t c) { _written = true; }
//void WebSocketSerial::flushTx(void) { if (!_written) return; }

void WebSocketSerial::print(int n, int base) {
print((long)n, base);
}
#endif

void WebSocketSerial::print(unsigned int n, int base) {
print((unsigned long)n, base);
}
/**
* Imports from print.h
*/

void WebSocketSerial::print(char c, int base) { print((long)c, base); }
void WebSocketSerial::print(unsigned char b, int base) { print((unsigned long)b, base); }
void WebSocketSerial::print(int n, int base) { print((long)n, base); }
void WebSocketSerial::print(unsigned int n, int base) { print((unsigned long)n, base); }
void WebSocketSerial::print(long n, int base) {
if (base == 0)
write(n);
else if (base == 10) {
if (n < 0) {
print('-');
n = -n;
}
if (n < 0) { print('-'); n = -n; }
printNumber(n, 10);
}
else
printNumber(n, base);
}

void WebSocketSerial::print(unsigned long n, int base) {
if (base == 0) write(n);
else printNumber(n, base);
if (base == 0) write(n); else printNumber(n, base);
}

void WebSocketSerial::print(double n, int digits) {
printFloat(n, digits);
}

void WebSocketSerial::println(void) {
print('\r');
print('\n');
}

void WebSocketSerial::println(const String& s) {
print(s);
println();
}

void WebSocketSerial::println(const char c[]) {
print(c);
println();
}

void WebSocketSerial::println(char c, int base) {
print(c, base);
println();
}
void WebSocketSerial::print(double n, int digits) { printFloat(n, digits); }

void WebSocketSerial::println(unsigned char b, int base) {
print(b, base);
println();
}

void WebSocketSerial::println(int n, int base) {
print(n, base);
println();
}

void WebSocketSerial::println(unsigned int n, int base) {
print(n, base);
println();
}

void WebSocketSerial::println(long n, int base) {
print(n, base);
println();
}

void WebSocketSerial::println(unsigned long n, int base) {
print(n, base);
println();
}

void WebSocketSerial::println(double n, int digits) {
print(n, digits);
println();
}
void WebSocketSerial::println(void) { print('\r'); print('\n'); }
void WebSocketSerial::println(const String& s) { print(s); println(); }
void WebSocketSerial::println(const char c[]) { print(c); println(); }
void WebSocketSerial::println(char c, int base) { print(c, base); println(); }
void WebSocketSerial::println(unsigned char b, int base) { print(b, base); println(); }
void WebSocketSerial::println(int n, int base) { print(n, base); println(); }
void WebSocketSerial::println(unsigned int n, int base) { print(n, base); println(); }
void WebSocketSerial::println(long n, int base) { print(n, base); println(); }
void WebSocketSerial::println(unsigned long n, int base) { print(n, base); println(); }
void WebSocketSerial::println(double n, int digits) { print(n, digits); println(); }

// Private Methods

Expand All @@ -273,34 +201,32 @@ void WebSocketSerial::printNumber(unsigned long n, uint8_t base) {

void WebSocketSerial::printFloat(double number, uint8_t digits) {
// Handle negative numbers
if (number < 0.0) {
print('-');
number = -number;
}
if (number < 0.0) { print('-'); number = -number; }

// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i = 0; i < digits; ++i)
rounding *= 0.1;
// Use a lookup table for performance
constexpr double rounds[] = { 0.5, 0.05, 0.005, 0.0005, 0.00005, 0.000005, 0.0000005, 0.00000005 };
number += rounds[digits];

number += rounding;
//number += pow(10, -(digits + 1)); // slower single-line equivalent

// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
print(int_part);

// Print the decimal point, but only if there are digits beyond
double remainder = number - (double)int_part;
if (digits) {
print('.');
// Extract digits from the remainder one at a time
while (digits--) {
remainder *= 10.0;
int toPrint = int(remainder);
const int toPrint = int(remainder);
print(toPrint);
remainder -= toPrint;
}
}
}

#endif // ARDUINO_ARCH_ESP32
#endif // WIFISUPPORT
#endif // ARDUINO_ARCH_ESP32
15 changes: 6 additions & 9 deletions Marlin/src/HAL/HAL_ESP32/WebSocketSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#ifndef WEBSOCKET_SERIAL_H
#define WEBSOCKET_SERIAL_H
#pragma once

#include "../../inc/MarlinConfig.h"

Expand All @@ -38,16 +36,17 @@
#ifndef TX_BUFFER_SIZE
#define TX_BUFFER_SIZE 32
#endif
#if TX_BUFFER_SIZE <= 0
#error "TX_BUFFER_SIZE is required for the WebSocket."
#endif

#if RX_BUFFER_SIZE > 256
typedef uint16_t ring_buffer_pos_t;
#else
typedef uint8_t ring_buffer_pos_t;
#endif


class WebSocketSerial {

public:
WebSocketSerial() {};
static void begin(const long);
Expand All @@ -60,11 +59,11 @@ class WebSocketSerial {
static void write(const uint8_t c);

#if ENABLED(SERIAL_STATS_DROPPED_RX)
FORCE_INLINE static uint32_t dropped() { return 0; }
FORCE_INLINE static uint32_t dropped() { return 0; }
#endif

#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
FORCE_INLINE static int rxMaxEnqueued() { return 0; }
FORCE_INLINE static int rxMaxEnqueued() { return 0; }
#endif

FORCE_INLINE static void write(const char* str) { while (*str) write(*str++); }
Expand Down Expand Up @@ -98,5 +97,3 @@ class WebSocketSerial {
};

extern WebSocketSerial webSocketSerial;

#endif // WEBSOCKET_SERIAL_H
Loading

0 comments on commit 23a3be0

Please sign in to comment.