master
Hideaki Tai 5 years ago
parent e103bb68c4
commit 795905b01f

@ -162,17 +162,21 @@ namespace arduino
uint8_t operator[] (const size_t i) const { return buffer[i]; } uint8_t operator[] (const size_t i) const { return buffer[i]; }
uint8_t& operator[] (const size_t i) { return buffer[i]; } uint8_t& operator[] (const size_t i) { return buffer[i]; }
}; };
typedef void (*CallbackType)(uint8_t* data, uint16_t size);
struct Map { uint32_t universe; CallbackType func; };
using CallbackMap = arx::vector<Map, 8>;
#else
template <uint16_t SIZE>
using Array = std::array<uint8_t, SIZE>;
using CallbackType = std::function<void(uint8_t* data, uint16_t size)>;
struct Map { uint32_t universe; CallbackType func; };
using CallbackMap = std::vector<Map>;
#endif #endif
template <typename S> template <typename S>
class Sender_ class Sender_
{ {
#ifdef ARTNET_DISABLE_STL
Array<PACKET_SIZE> packet; Array<PACKET_SIZE> packet;
#else
std::array<uint8_t, PACKET_SIZE> packet;
#endif
const char* ip; const char* ip;
uint16_t port {DEFAULT_PORT}; uint16_t port {DEFAULT_PORT};
uint8_t target_net {0}; uint8_t target_net {0};
@ -281,19 +285,7 @@ namespace arduino
template <typename S> template <typename S>
class Receiver_ class Receiver_
{ {
#ifdef ARTNET_DISABLE_STL
typedef void (*CallbackType)(uint8_t* data, uint16_t size);
struct Map { uint32_t universe; CallbackType func; };
arx::vector<Map, 8> v;
using CallbackMap = arx::vector<Map, 8>;
Array<PACKET_SIZE> packet; Array<PACKET_SIZE> packet;
#else
using CallbackType = std::function<void(uint8_t* data, uint16_t size)>;
struct Map { uint32_t universe; CallbackType func; };
using CallbackMap = std::vector<Map>;
std::array<uint8_t, PACKET_SIZE> packet;
#endif
IPAddress remote_ip; IPAddress remote_ip;
uint16_t remote_port; uint16_t remote_port;
CallbackMap callbacks; CallbackMap callbacks;
@ -315,7 +307,7 @@ namespace arduino
{ {
if (opcode(d) == OPC(OpCode::Dmx)) if (opcode(d) == OPC(OpCode::Dmx))
{ {
// TODO: std::move... memmove(packet.data(), d, size);
memcpy(packet.data(), d, size); memcpy(packet.data(), d, size);
remote_ip = stream->S::remoteIP(); remote_ip = stream->S::remoteIP();
remote_port = (uint16_t)stream->S::remotePort(); remote_port = (uint16_t)stream->S::remotePort();

@ -1,122 +0,0 @@
#pragma once
#ifndef RINGBUFFER_H
#define RINGBUFFER_H
template<typename T, size_t SIZE>
class RingBuffer
{
public:
inline size_t capacity() const { return SIZE; };
inline size_t size() const { return (tail_ - head_); };
inline const T* data() const { return &(queue_[head_]); }
inline T* data() { return &(queue_[head_]); }
inline bool empty() const { return tail_ == head_; };
inline void clear() { head_ = 0; tail_ = 0; };
inline void pop()
{
if (size() == 0) return;
if (size() == 1) clear();
else head_++;
};
inline void pop_back()
{
if (size() == 0) return;
if (size() == 1) clear();
else tail_--;
};
inline void push(const T& data)
{
queue_[(tail_++) % SIZE] = data;
if (size() > SIZE) head_++;
};
inline void push(T&& data)
{
queue_[(tail_++) % SIZE] = data;
if (size() > SIZE) head_++;
};
inline void push_back(const T& data)
{
queue_[(tail_++) % SIZE] = data;
if (size() > SIZE) head_++;
};
inline void push_back(T&& data)
{
queue_[(tail_++) % SIZE] = data;
if (size() > SIZE) head_++;
};
inline const T& front() const // throw(Exception)
{
// if(empty()) throw Exception();
return *(queue_ + head_ % SIZE);
};
inline T& front() // throw(Exception)
{
// if(empty()) throw Exception();
return *(queue_ + head_ % SIZE);
};
inline const T& back() const // throw(Exception)
{
// if(empty()) throw Exception();
return *(queue_ + (SIZE + tail_ - 1) % SIZE);
}
inline T& back() // throw(Exception)
{
// if(empty()) throw Exception();
return *(queue_ + (SIZE + tail_ - 1) % SIZE);
}
inline const T& operator[] (uint8_t index) const
{
return *(queue_ + (head_ + index) % SIZE);
}
inline T& operator[] (uint8_t index)
{
return *(queue_ + (head_ + index) % SIZE);
}
inline const T* begin() const { return &(queue_[head_]); }
inline T* begin() { return &(queue_[head_]); }
inline const T* end() const { return &(queue_[tail_]); }
inline T* end() { return &(queue_[tail_]); }
inline T* erase(T* p)
{
if (p == end()) return p;
for (T* pos = p + 1; pos != end(); ++pos)
*(pos - 1) = *pos;
--tail_;
return p;
}
inline void resize(size_t sz)
{
size_t s = size();
if (sz > size())
{
for (size_t i = 0; i < sz - s; ++i) push(T());
}
else if (sz < size())
{
for (size_t i = 0; i < s - sz; ++i) pop();
}
}
inline void assign(const T* const first, const T* const end)
{
clear();
const char* p = first;
while (p != end) push(*p++);
}
private:
T queue_[SIZE];
volatile size_t head_ {0};
volatile size_t tail_ {0};
};
#endif // RINGBUFFER_H
Loading…
Cancel
Save