Initial
This commit is contained in:
24
espurna/libs/EmbedisWrap.h
Executable file
24
espurna/libs/EmbedisWrap.h
Executable file
@ -0,0 +1,24 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// Wrap class around Embedis (settings & terminal)
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Embedis.h"
|
||||
|
||||
class EmbedisWrap : public Embedis {
|
||||
|
||||
public:
|
||||
|
||||
EmbedisWrap(Stream& stream, size_t buflen = 128, size_t argvlen = 8): Embedis(stream, buflen, argvlen) {}
|
||||
|
||||
unsigned char getCommandCount() {
|
||||
return commands.size();
|
||||
}
|
||||
|
||||
String getCommandName(unsigned int i) {
|
||||
if (i < commands.size()) return commands[i].name;
|
||||
return String();
|
||||
}
|
||||
|
||||
};
|
85
espurna/libs/StreamInjector.h
Executable file
85
espurna/libs/StreamInjector.h
Executable file
@ -0,0 +1,85 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// Stream Injector
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Stream.h>
|
||||
|
||||
class StreamInjector : public Stream {
|
||||
|
||||
public:
|
||||
|
||||
typedef std::function<void(uint8_t ch)> writeCallback;
|
||||
|
||||
StreamInjector(Stream& serial, size_t buflen = 128) : _stream(serial), _buffer_size(buflen) {
|
||||
_buffer = new char[buflen];
|
||||
}
|
||||
|
||||
~StreamInjector() {
|
||||
delete[] _buffer;
|
||||
}
|
||||
|
||||
virtual void callback(writeCallback c) {
|
||||
_callback = c;
|
||||
}
|
||||
|
||||
virtual size_t write(uint8_t ch) {
|
||||
if (_callback) _callback(ch);
|
||||
return _stream.write(ch);
|
||||
}
|
||||
|
||||
virtual int read() {
|
||||
int ch = _stream.read();
|
||||
if (ch == -1) {
|
||||
if (_buffer_read != _buffer_write) {
|
||||
ch = _buffer[_buffer_read];
|
||||
_buffer_read = (_buffer_read + 1) % _buffer_size;
|
||||
}
|
||||
}
|
||||
return ch;
|
||||
}
|
||||
|
||||
virtual int available() {
|
||||
unsigned int bytes = _stream.available();
|
||||
if (_buffer_read > _buffer_write) {
|
||||
bytes += (_buffer_write - _buffer_read + _buffer_size);
|
||||
} else if (_buffer_read < _buffer_write) {
|
||||
bytes += (_buffer_write - _buffer_read);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
virtual int peek() {
|
||||
int ch = _stream.peek();
|
||||
if (ch == -1) {
|
||||
if (_buffer_read != _buffer_write) {
|
||||
ch = _buffer[_buffer_read];
|
||||
}
|
||||
}
|
||||
return ch;
|
||||
}
|
||||
|
||||
virtual void flush() {
|
||||
_stream.flush();
|
||||
_buffer_read = _buffer_write;
|
||||
}
|
||||
|
||||
virtual void inject(char *data, size_t len) {
|
||||
for (int i=0; i<len; i++) {
|
||||
_buffer[_buffer_write] = data[i];
|
||||
_buffer_write = (_buffer_write + 1) % _buffer_size;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Stream& _stream;
|
||||
char * _buffer;
|
||||
unsigned char _buffer_size;
|
||||
unsigned char _buffer_write = 0;
|
||||
unsigned char _buffer_read = 0;
|
||||
writeCallback _callback = NULL;
|
||||
|
||||
|
||||
};
|
87
espurna/libs/WebSocketIncommingBuffer.h
Executable file
87
espurna/libs/WebSocketIncommingBuffer.h
Executable file
@ -0,0 +1,87 @@
|
||||
/*
|
||||
|
||||
WebSocketIncommingBuffer
|
||||
|
||||
Code by Hermann Kraus (https://bitbucket.org/hermr2d2/)
|
||||
and slightly modified.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define MAX_WS_MSG_SIZE 4000
|
||||
typedef std::function<void(AsyncWebSocketClient *client, uint8_t *data, size_t len)> AwsMessageHandler;
|
||||
|
||||
class WebSocketIncommingBuffer {
|
||||
|
||||
public:
|
||||
WebSocketIncommingBuffer(AwsMessageHandler cb, bool terminate_string = true, bool cb_on_fragments = false) :
|
||||
_cb(cb),
|
||||
_terminate_string(terminate_string),
|
||||
_cb_on_fragments(cb_on_fragments),
|
||||
_buffer(0)
|
||||
{}
|
||||
|
||||
~WebSocketIncommingBuffer() {
|
||||
if (_buffer) delete _buffer;
|
||||
}
|
||||
|
||||
void data_event(AsyncWebSocketClient *client, AwsFrameInfo *info, uint8_t *data, size_t len) {
|
||||
|
||||
if ((info->final || _cb_on_fragments)
|
||||
&& !_terminate_string
|
||||
&& info->index == 0
|
||||
&& info->len == len) {
|
||||
|
||||
/* The whole message is in a single frame and we got all of it's
|
||||
data therefore we can parse it without copying the data first.*/
|
||||
_cb(client, data, len);
|
||||
|
||||
} else {
|
||||
|
||||
if (info->len > MAX_WS_MSG_SIZE) return;
|
||||
|
||||
/* Check if previous fragment was discarded because it was too long. */
|
||||
//if (!_cb_on_fragments && info->num > 0 && !_buffer) return;
|
||||
|
||||
if (!_buffer) _buffer = new std::vector<uint8_t>();
|
||||
|
||||
if (info->index == 0) {
|
||||
//New frame => preallocate memory
|
||||
if (_cb_on_fragments) {
|
||||
_buffer->reserve(info->len + 1);
|
||||
} else {
|
||||
/* The current fragment would lead to a message which is
|
||||
too long. So discard everything received so far. */
|
||||
if (info->len + _buffer->size() > MAX_WS_MSG_SIZE) {
|
||||
delete _buffer;
|
||||
_buffer = 0;
|
||||
return;
|
||||
} else {
|
||||
_buffer->reserve(info->len + _buffer->size() + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//assert(_buffer->size() == info->index);
|
||||
_buffer->insert(_buffer->end(), data, data+len);
|
||||
if (info->index + len == info->len
|
||||
&& (info->final || _cb_on_fragments)) {
|
||||
|
||||
// Frame/message complete
|
||||
if (_terminate_string) _buffer->push_back(0);
|
||||
_cb(client, _buffer->data(), _buffer->size());
|
||||
_buffer->clear();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
AwsMessageHandler _cb;
|
||||
bool _cb_on_fragments;
|
||||
bool _terminate_string;
|
||||
std::vector<uint8_t> *_buffer;
|
||||
|
||||
};
|
33
espurna/libs/pwm.h
Executable file
33
espurna/libs/pwm.h
Executable file
@ -0,0 +1,33 @@
|
||||
#ifndef __PWM_H__
|
||||
#define __PWM_H__
|
||||
|
||||
/*pwm.h: function and macro definition of PWM API , driver level */
|
||||
/*user_light.h: user interface for light API, user level*/
|
||||
/*user_light_adj: API for color changing and lighting effects, user level*/
|
||||
|
||||
|
||||
/*NOTE!! : DO NOT CHANGE THIS FILE*/
|
||||
|
||||
/*SUPPORT UP TO 8 PWM CHANNEL*/
|
||||
//#define PWM_CHANNEL_NUM_MAX 8
|
||||
|
||||
struct pwm_param {
|
||||
uint32 period;
|
||||
uint32 freq;
|
||||
uint32 duty[PWM_CHANNEL_NUM_MAX]; //PWM_CHANNEL<=8
|
||||
};
|
||||
|
||||
|
||||
/* pwm_init should be called only once, for now */
|
||||
void pwm_init(uint32 period, uint32 *duty,uint32 pwm_channel_num,uint32 (*pin_info_list)[3]);
|
||||
void pwm_start(void);
|
||||
|
||||
void pwm_set_duty(uint32 duty, uint8 channel);
|
||||
uint32 pwm_get_duty(uint8 channel);
|
||||
void pwm_set_period(uint32 period);
|
||||
uint32 pwm_get_period(void);
|
||||
|
||||
uint32 get_pwm_version(void);
|
||||
void set_pwm_debug_en(uint8 print_en);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user