From 1104708583b60c4eb824a0cfe0f6f29255726e39 Mon Sep 17 00:00:00 2001 From: Hideaki Tai Date: Sat, 27 Jun 2020 16:16:07 +0900 Subject: [PATCH] support more boards --- Artnet.h | 110 +++- examples/WiFi/receiver/receiver.ino | 2 +- examples/WiFi/send_receive/send_receive.ino | 2 +- examples/WiFi/sender/sender.ino | 2 +- util/ArxContainer/ArxContainer.h | 553 ++++++++++++++++++ util/ArxContainer/LICENSE | 21 + util/ArxContainer/README.md | 166 ++++++ util/ArxContainer/library.json | 20 + util/ArxContainer/library.properties | 9 + util/TeensyDirtySTLErrorSolution/LICENSE | 21 + util/TeensyDirtySTLErrorSolution/README.md | 57 ++ .../TeensyDirtySTLErrorSolution.h | 65 ++ 12 files changed, 995 insertions(+), 33 deletions(-) create mode 100644 util/ArxContainer/ArxContainer.h create mode 100644 util/ArxContainer/LICENSE create mode 100644 util/ArxContainer/README.md create mode 100644 util/ArxContainer/library.json create mode 100644 util/ArxContainer/library.properties create mode 100644 util/TeensyDirtySTLErrorSolution/LICENSE create mode 100644 util/TeensyDirtySTLErrorSolution/README.md create mode 100644 util/TeensyDirtySTLErrorSolution/TeensyDirtySTLErrorSolution.h diff --git a/Artnet.h b/Artnet.h index 830661d..392b3f0 100644 --- a/Artnet.h +++ b/Artnet.h @@ -7,22 +7,70 @@ // Packet Definition : https://art-net.org.uk/structure/streaming-packets/artdmx-packet-definition/ #include -#ifndef __AVR__ - #include - #include - #include -#else - #include "RingBuffer.h" + +#if defined(ARDUINO_ARCH_AVR)\ + || defined(ARDUINO_ARCH_MEGAAVR)\ + || defined(ARDUINO_ARCH_SAM)\ + || defined(ARDUINO_ARCH_SAMD)\ + || defined(ARDUINO_spresense_ast) + #define ARDUINOOSC_DISABLE_STL +#endif + +#if defined(ESP_PLATFORM)\ + || defined(ESP8266)\ + || defined(ARDUINO_AVR_UNO_WIFI_REV2)\ + || defined(ARDUINO_SAMD_MKRWIFI1010)\ + || defined(ARDUINO_SAMD_MKRVIDOR4000)\ + || defined(ARDUINO_SAMD_MKR1000)\ + || defined(ARDUINO_SAMD_NANO_33_IOT) + #define ARDUINOOSC_ENABLE_WIFI +#endif + +#if defined(TEENSYDUINO)\ + || defined(ESP8266)\ + || defined(ARDUINOOSC_DISABLE_STL) + #define ARDUINOOSC_ENABLE_ETHER #endif -#ifdef ESP_PLATFORM - #include - #include -#elif defined (ESP8266) - #include - #include -#elif defined (TEENSYDUINO) || defined (__AVR__) + +#if !defined (ARDUINOOSC_ENABLE_WIFI)\ + && !defined (ARDUINOOSC_ENABLE_ETHER)\ + && !defined (ARDUINOOSC_DISABLE_STL) + #error THIS PLATFORM HAS NO WIFI OR ETHERNET OR NOT SUPPORTED ARCHITECTURE. PLEASE LET ME KNOW! +#endif + +#ifdef ARDUINOOSC_ENABLE_WIFI + #ifdef ESP_PLATFORM + #include + #include + #elif defined (ESP8266) + #include + #include + #elif defined (ARDUINO_AVR_UNO_WIFI_REV2)\ + || defined(ARDUINO_SAMD_MKRWIFI1010)\ + || defined(ARDUINO_SAMD_MKRVIDOR4000)\ + || defined(ARDUINO_SAMD_NANO_33_IOT) + #include + #include + #include + #elif defined (ARDUINO_SAMD_MKR1000) + #include + #include + #include + #endif +#endif // ARDUINOOSC_ENABLE_WIFI + +#ifdef ARDUINOOSC_ENABLE_ETHER #include #include + #include "util/TeensyDirtySTLErrorSolution/TeensyDirtySTLErrorSolution.h" +#endif // ARDUINOOSC_ENABLE_ETHER + +#ifdef ARDUINOOSC_DISABLE_STL + #include "util/ArxContainer/ArxContainer.h" +#else + #include + #include + #include #endif @@ -102,7 +150,7 @@ namespace arduino static constexpr uint8_t NUM_PIXELS_PER_UNIV { 170 }; - #ifdef __AVR__ +#ifdef ARDUINOOSC_DISABLE_STL template class Array { @@ -114,16 +162,16 @@ namespace arduino uint8_t operator[] (const size_t i) const { return buffer[i]; } uint8_t& operator[] (const size_t i) { return buffer[i]; } }; - #endif +#endif template class Sender_ { - #ifdef __AVR__ +#ifdef ARDUINOOSC_DISABLE_STL Array packet; - #else +#else std::array packet; - #endif +#endif const char* ip; uint16_t port {DEFAULT_PORT}; @@ -233,17 +281,18 @@ namespace arduino template class Receiver_ { - #ifdef __AVR__ +#ifdef ARDUINOOSC_DISABLE_STL typedef void (*CallbackType)(uint8_t* data, uint16_t size); struct Map { uint32_t universe; CallbackType func; }; - using CallbackMap = RingBuffer; + arx::vector v; + using CallbackMap = arx::vector; Array packet; - #else +#else using CallbackType = std::function; struct Map { uint32_t universe; CallbackType func; }; using CallbackMap = std::vector; std::array packet; - #endif +#endif IPAddress remote_ip; uint16_t remote_port; @@ -412,14 +461,15 @@ namespace arduino } } -#if defined (ESP_PLATFORM) || defined (ESP8266) -using Artnet = arduino::artnet::Manager; -using ArtnetSender = arduino::artnet::Sender; -using ArtnetReceiver = arduino::artnet::Receiver; -#elif defined (TEENSYDUINO) || defined (__AVR__) -using Artnet = arduino::artnet::Manager; -using ArtnetSender = arduino::artnet::Sender; -using ArtnetReceiver = arduino::artnet::Receiver; +#ifdef ARDUINOOSC_ENABLE_WIFI + using ArtnetWiFi = arduino::artnet::Manager; + using ArtnetWiFiSender = arduino::artnet::Sender; + using ArtnetWiFiReceiver = arduino::artnet::Receiver; +#endif +#ifdef ARDUINOOSC_ENABLE_ETHER + using Artnet = arduino::artnet::Manager; + using ArtnetSender = arduino::artnet::Sender; + using ArtnetReceiver = arduino::artnet::Receiver; #endif #endif // ARDUINO_ARTNET_H diff --git a/examples/WiFi/receiver/receiver.ino b/examples/WiFi/receiver/receiver.ino index 1b47e28..4830cc8 100644 --- a/examples/WiFi/receiver/receiver.ino +++ b/examples/WiFi/receiver/receiver.ino @@ -7,7 +7,7 @@ const IPAddress ip(192, 168, 1, 201); const IPAddress gateway(192, 168, 1, 1); const IPAddress subnet(255, 255, 255, 0); -ArtnetReceiver artnet; +ArtnetWiFiReceiver artnet; uint32_t universe1 = 1; uint32_t universe2 = 2; diff --git a/examples/WiFi/send_receive/send_receive.ino b/examples/WiFi/send_receive/send_receive.ino index e735475..2bc6ae1 100644 --- a/examples/WiFi/send_receive/send_receive.ino +++ b/examples/WiFi/send_receive/send_receive.ino @@ -7,7 +7,7 @@ const IPAddress ip(192, 168, 1, 201); const IPAddress gateway(192, 168, 1, 1); const IPAddress subnet(255, 255, 255, 0); -Artnet artnet; +ArtnetWiFi artnet; const uint16_t size = 512; uint8_t data[size]; diff --git a/examples/WiFi/sender/sender.ino b/examples/WiFi/sender/sender.ino index de415b0..171ba00 100644 --- a/examples/WiFi/sender/sender.ino +++ b/examples/WiFi/sender/sender.ino @@ -7,7 +7,7 @@ const IPAddress ip(192, 168, 1, 201); const IPAddress gateway(192, 168, 1, 1); const IPAddress subnet(255, 255, 255, 0); -ArtnetSender artnet; +ArtnetWiFiSender artnet; uint32_t universe = 1; const uint16_t size = 512; diff --git a/util/ArxContainer/ArxContainer.h b/util/ArxContainer/ArxContainer.h new file mode 100644 index 0000000..cbff0f2 --- /dev/null +++ b/util/ArxContainer/ArxContainer.h @@ -0,0 +1,553 @@ +#pragma once + +#ifndef ARX_RINGBUFFER_H +#define ARX_RINGBUFFER_H + +#include + +#if defined(ARDUINO_ARCH_AVR)\ + || defined(ARDUINO_ARCH_MEGAAVR)\ + || defined(ARDUINO_ARCH_SAM)\ + || defined(ARDUINO_ARCH_SAMD)\ + || defined(ARDUINO_spresense_ast) + #define ARX_CONTAINER_DISABLED +#endif + +#ifdef ARX_CONTAINER_DISABLED + +#ifndef ARX_VECTOR_DEFAULT_SIZE +#define ARX_VECTOR_DEFAULT_SIZE 16 +#endif // ARX_VECTOR_DEFAULT_SIZE + +#ifndef ARX_DEQUE_DEFAULT_SIZE +#define ARX_DEQUE_DEFAULT_SIZE 16 +#endif // ARX_DEQUE_DEFAULT_SIZE + +#ifndef ARX_MAP_DEFAULT_SIZE +#define ARX_MAP_DEFAULT_SIZE 16 +#endif // ARX_MAP_DEFAULT_SIZE + +#ifndef ARX_TYPE_TRAITS_INITIALIZER_LIST_DEFINED +#define ARX_TYPE_TRAITS_INITIALIZER_LIST_DEFINED +namespace std +{ + template + class initializer_list + { + private: + const T* array; + size_t len; + initializer_list(const T* a, size_t l) : array(a), len(l) {} + public: + initializer_list() : array(nullptr), len(0) {} + size_t size() const { return len; } + const T *begin() const { return array; } + const T *end() const { return array + len; } + }; +} +#endif // ARX_TYPE_TRAITS_INITIALIZER_LIST_DEFINED + +namespace arx { + + namespace container + { + namespace detail + { + template + T&& move(T& t){ return static_cast(t); } + } + } + + template + class RingBuffer + { + protected: + + T queue_[N]; + int head_ {0}; + int tail_ {0}; + + public: + + RingBuffer() + : queue_() + , head_ {0} + , tail_{0} + { + } + + RingBuffer(std::initializer_list lst) + : queue_() + , head_(0) + , tail_(0) + { + for (auto it = lst.begin() ; it != lst.end() ; ++it) + { + queue_[tail_] = *it; + ++tail_; + } + } + + // copy + explicit RingBuffer(const RingBuffer& r) + : queue_() + , head_(r.head_) + , tail_(r.tail_) + { + for (size_t i = 0; i < r.size(); ++i) + queue_[i] = r.queue_[i]; + } + RingBuffer& operator= (const RingBuffer& r) + { + head_ = r.head_; + tail_ = r.tail_; + for (size_t i = 0; i < r.size(); ++i) + queue_[i] = r.queue_[i]; + return *this; + } + + // move + RingBuffer(RingBuffer&& r) + { + head_ = container::detail::move(r.head_); + tail_ = container::detail::move(r.tail_); + for (size_t i = 0; i < r.size(); ++i) + queue_[i] = container::detail::move(r.queue_[i]); + } + + RingBuffer& operator= (RingBuffer&& r) + { + head_ = container::detail::move(r.head_); + tail_ = container::detail::move(r.tail_); + for (size_t i = 0; i < r.size(); ++i) + queue_[i] = container::detail::move(r.queue_[i]); + return *this; + } + + virtual ~RingBuffer() {} + + using iterator = T*; + using const_iterator = const T*; + + size_t capacity() const { return N; }; + size_t size() const { return tail_ - head_; } + inline const T* data() const { return &(get(head_)); } + T* data() { return &(get(head_)); } + bool empty() const { return tail_ == head_; } + void clear() { head_ = 0; tail_ = 0; } + + void pop() { pop_front(); } + void pop_front() + { + if (size() == 0) return; + if (size() == 1) clear(); + else head_++; + } + void pop_back() + { + if (size() == 0) return; + if (size() == 1) clear(); + else tail_--; + } + + void push(const T& data) { push_back(data); } + void push(T&& data) { push_back(data); } + void push_back(const T& data) + { + get(tail_++) = data; + if (size() > N) ++head_; + }; + void push_back(T&& data) + { + get(tail_++) = data; + if (size() > N) ++head_; + }; + void push_front(const T& data) + { + get(head_--) = data; + if (size() > N) --tail_; + }; + void push_front(T&& data) + { + get(head_--) = data; + if (size() > N) --tail_; + }; + void emplace(const T& data) { push(data); } + void emplace(T&& data) { push(data); } + void emplace_back(const T& data) { push_back(data); } + void emplace_back(T&& data) { push_back(data); } + + const T& front() const { return get(head_); }; + T& front() { return get(head_); }; + + const T& back() const { return get(tail_ - 1); } + T& back() { return get(tail_ - 1); } + + const T& operator[] (size_t index) const { return get(head_ + (int)index); } + T& operator[] (size_t index) { return get(head_ + (int)index); } + + iterator begin() { return ptr(head_); } + iterator end() { return (queue_ + tail_); } + + const_iterator begin() const { return (const_iterator)ptr(head_); } + const_iterator end() const { return (const_iterator)(queue_ + tail_); } + + iterator erase(iterator p) + { + if (p == end()) return p; + for (T* pos = p + 1; pos != end(); ++pos) + *(pos - 1) = *pos; + --tail_; + return p; + } + + 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(); + } + } + + void assign(const_iterator first, const_iterator end) + { + clear(); + const char* p = first; + while (p != end) push(*p++); + } + + void shrink_to_fit() + { + // dummy + } + + void reserve(size_t n) + { + (void)n; + // dummy + } + + void insert(const_iterator pos, const_iterator first, const_iterator last) + { + if (pos != end()) + { + size_t sz = 0; + { + iterator it = (iterator)first; + for (; it != last; ++it) ++sz; + } + iterator it = end() + sz - 1; + for (int i = sz; i > 0; --i, --it) + { + *it = *(it - sz); + } + it = (iterator)pos; + for (size_t i = 0; i < sz; ++i) + { + *it = *(first + i); + } + } + else + { + iterator it = (iterator)first; + for (; it != last; ++it) + { + push_back(*it); + } + } + } + + private: + + T& get(const int i) + { + if (i >= 0) return queue_[i % N]; + else return queue_[N - abs(i % N)]; + } + + const T& get(const int i) const + { + if (i >= 0) return queue_[i % N]; + else return queue_[N - abs(i % N)]; + } + + T* ptr(const int i) + { + if (i >= 0) return (T*)(queue_ + i % N); + else return (T*)(queue_ + N - abs(i % N)); + } + + const T* ptr(const int i) const + { + if (i >= 0) return (T*)(queue_ + i % N); + else return (T*)(queue_ + N - abs(i % N)); + } + + }; + + template + bool operator== (const RingBuffer& x, const RingBuffer& y) + { + if (x.size() != y.size()) return false; + for (size_t i = 0; i < x.size(); ++i) + if (x[i] != y[i]) return false; + return true; + } + + template + bool operator!= (const RingBuffer& x, const RingBuffer& y) + { + return !(x == y); + } + + + template + struct vector : public RingBuffer + { + vector() : RingBuffer() { } + vector(std::initializer_list lst) : RingBuffer(lst) { } + + // copy + vector(const vector& r) : RingBuffer(r) { } + + vector& operator= (const vector& r) + { + RingBuffer::operator=(r); + return *this; + } + + // move + vector(vector&& r) : RingBuffer(r) { } + + vector& operator= (vector&& r) + { + RingBuffer::operator=(r); + return *this; + } + + virtual ~vector() {} + + private: + using RingBuffer::pop; + using RingBuffer::pop_front; + using RingBuffer::push; + using RingBuffer::push_front; + using RingBuffer::emplace; + }; + + + template + struct deque : public RingBuffer + { + deque() : RingBuffer() { } + deque(std::initializer_list lst) : RingBuffer(lst) { } + + // copy + deque(const deque& r) : RingBuffer(r) { } + + deque& operator= (const deque& r) + { + RingBuffer::operator=(r); + return *this; + } + + // move + deque(deque&& r) : RingBuffer(r) { } + + deque& operator= (deque&& r) + { + RingBuffer::operator=(r); + return *this; + } + + virtual ~deque() {} + + private: + using RingBuffer::capacity; + using RingBuffer::pop; + using RingBuffer::push; + using RingBuffer::emplace; + using RingBuffer::assign; + using RingBuffer::begin; + using RingBuffer::end; + }; + + + template + struct pair + { + T1 first; + T2 second; + }; + + template + pair make_pair(const T1& t1, const T2& t2) + { + return {t1, t2}; + }; + + template + bool operator== (const pair& x, const pair& y) + { + return (x.first == y.first) && (x.second == y.second); + } + + template + bool operator!= (const pair& x, const pair& y) + { + return !(x == y); + } + + + template + struct map : public RingBuffer, N> + { + using iterator = typename RingBuffer, N>::iterator; + using const_iterator = typename RingBuffer, N>::const_iterator; + + map() : RingBuffer, N>() { } + map(std::initializer_list> lst) : RingBuffer, N>(lst) { } + + // copy + map(const map& r) : RingBuffer, N>(r) { } + + map& operator= (const map& r) + { + RingBuffer, N>::operator=(r); + return *this; + } + + // move + map(map&& r) : RingBuffer(r) { } + + map& operator= (map&& r) + { + RingBuffer, N>::operator=(r); + return *this; + } + + virtual ~map() {} + + const_iterator find(const Key& key) const + { + for (size_t i = 0; i < this->size(); ++i) + { + const_iterator it = this->begin() + i; + if (key == it->first) + return it; + } + return this->end(); + } + + pair insert(const Key& key, const T& t) + { + bool b {false}; + iterator it = find(key); + if (it == this->end()) + { + this->push(make_pair(key, t)); + b = true; + it = this->end() - 1; + } + return {it, b}; + } + + pair insert(const pair& p) + { + bool b {false}; + const_iterator it = find(p.first); + if (it == this->end()) + { + this->push(p); + b = true; + it = this->end() - 1; + } + return {(iterator)it, b}; + } + + pair emplace(const Key& key, const T& t) + { + return insert(key, t); + } + + pair emplace(const pair& p) + { + return insert(p); + } + + const T& at(const Key& key) const + { + // iterator it = find(key); + // if (it != this->end()) return it->second; + // return T(); + return find(key)->second; + } + + T& at(const Key& key) + { + // iterator it = find(key); + // if (it != this->end()) return it->second; + // return T(); + return find(key)->second; + } + + iterator erase(const iterator it) + { + iterator i = (iterator)find(it->first); + if (i != this->end()) + { + return this->erase(i); + } + return this->end(); + } + + iterator erase(const size_t index) + { + if (index < this->size()) + { + iterator it = this->begin() + index; + return this->erase(it); + } + return this->end(); + } + + const T& operator[] (const Key& key) + { + const_iterator it = find(key); + if (it != this->end()) return it->second; + + insert(::arx::make_pair(key, T())); + return this->back().second; + } + + private: + + using RingBuffer, N>::capacity; + using RingBuffer, N>::data; + using RingBuffer, N>::pop; + using RingBuffer, N>::pop_front; + using RingBuffer, N>::pop_back; + using RingBuffer, N>::push; + using RingBuffer, N>::push_back; + using RingBuffer, N>::push_front; + using RingBuffer, N>::emplace_back; + using RingBuffer, N>::front; + using RingBuffer, N>::back; + using RingBuffer, N>::resize; + using RingBuffer, N>::assign; + using RingBuffer, N>::shrink_to_fit; + }; + +} // namespace arx + +template +using ArxRingBuffer = arx::RingBuffer; + + +#endif // ARX_CONTAINER_DISABLED +#endif // ARX_RINGBUFFER_H diff --git a/util/ArxContainer/LICENSE b/util/ArxContainer/LICENSE new file mode 100644 index 0000000..8c86460 --- /dev/null +++ b/util/ArxContainer/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Hideaki Tai + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/util/ArxContainer/README.md b/util/ArxContainer/README.md new file mode 100644 index 0000000..478a147 --- /dev/null +++ b/util/ArxContainer/README.md @@ -0,0 +1,166 @@ +# ArxContainer + +C++ container-like classes (vector, map, etc.) for Arduino which cannot use STL + +## Note + +`ArxContainer` is C++ container-__like__ classes for Arduino. +All of the functions is not supported currently. +Detail of these containers are described in Detail section. + +## Supported Container Types + +- `vector` +- `map` (`pair`) +- `deque` + + +## Supported Boards + +This library is currently enabled only if you use following architecture. +Please use C++ Standard Template Library for other boards. + +- AVR (Uno, Nano, Mega, etc.) +- MEGAAVR (Uno WiFi, Nano Ecery, etc.) +- SAMD (Zero, MKR, M0, etc.) +- SPRESENSE + + +## Usage + +### vector + +```C++ +// initialize with initializer_list +arx::vector vs {1, 2, 3}; + +// add contents +for (size_t i = 4; i <= 5; ++i) + vs.push_back(i); + +// index access +for (size_t i = 0; i < vs.size(); ++i) + Serial.println(vs[i]); + +// range-based access +for (const auto& v : vs) + Serial.println(v); +``` + +### map + +``` C++ +// initialize with initializer_list +arx::map mp {{"one", 1}, {"two", 2}}; + +// add contents +mp.insert("three", 3); +mp["four"] = 4; + +// range based access +for (const auto& m : mp) +{ + Serial.print("{"); + Serial.print(m.first); Serial.print(","); + Serial.print(m.second); + Serial.println("}"); +} + +// key access +Serial.print("one = "); Serial.println(mp["one"]); +Serial.print("two = "); Serial.println(mp["two"]); +Serial.print("three = "); Serial.println(mp["three"]); +Serial.print("four = "); Serial.println(mp["four"]); +``` + +### deque + +```C++ +// initialize with initializer_list +arx::deque dq {1, 2, 3}; + +// add contents +for (int i = 4; i <= 5; ++i) + dq.push_back(i); + +// index access +for (int i = 0; i < dq.size(); ++i) + Serial.print(dq[i]); +``` + + +## Detail + +`ArxContainer` is C++ container-__like__ classes for Arduino. +This library is based on `arx::RingBuffer` and `arx::xxxx` is limited-size container. +`arx::RingBuffer` can be used as: + +```C++ +ArxRingBuffer buffer; + +buffer.push(1); +buffer.push(2); +buffer.push(3); + +for(size_t i = 0; i < buffer.size(); ++i) + Serial.println(buffer[i]); + +buffer.pop(); + +for(auto& b : buffer) + Serial.println(b); +``` + +`arx::xxxx` are derived from `RingBuffer` and defined as: + +``` C++ +namespace arx { + template + struct vector : public RingBuffer + + template + struct map : public RingBuffer, N> + + template + struct deque : public RingBuffer +} +``` + +So range-based loop cannot be applyed to `arx::deque` (iterator is not continuous because it is based on `RingBuffer`). + + +### Manage Size Limit of Container + +Global default size of container can be changed by defining these macros before `#include `. + +``` C++ +#define ARX_VECTOR_DEFAULT_SIZE XX // default: 16 +#define ARX_MAP_DEFAULT_SIZE XX // default: 16 +#define ARX_DEQUE_DEFAULT_SIZE XX // default: 16 +``` + +Or you can change each container size by template argument. + +``` C++ +arx::vector vs; +arx::map ms; +arx::deque ds; +``` + +## Roadmap + +This library will be updated if I want to use more container interfaces on supported boards shown above. +PRs are welcome! + + +## Used Inside of + +- [Packetizer](https://github.com/hideakitai/Packetizer) +- [MsgPack](https://github.com/hideakitai/MsgPack) +- [MsgPacketizer](https://github.com/hideakitai/MsgPacketizer) +- [ArduinoOSC](https://github.com/hideakitai/ArduinoOSC) + + +## License + +MIT diff --git a/util/ArxContainer/library.json b/util/ArxContainer/library.json new file mode 100644 index 0000000..1ddeef3 --- /dev/null +++ b/util/ArxContainer/library.json @@ -0,0 +1,20 @@ +{ + "name": "ArxContainer", + "keywords": "ringbuffer, vector, deque, map", + "description": "C++ container-like classes (vector, map, etc.) for Arduino which cannot use STL", + "repository": + { + "type": "git", + "url": "https://github.com/hideakitai/ArxContainer.git" + }, + "authors": + { + "name": "Hideaki Tai", + "url": "https://github.com/hideakitai", + "maintainer": true + }, + "version": "0.3.6", + "license": "MIT", + "frameworks": "arduino", + "platforms": "*" +} diff --git a/util/ArxContainer/library.properties b/util/ArxContainer/library.properties new file mode 100644 index 0000000..cfa6d96 --- /dev/null +++ b/util/ArxContainer/library.properties @@ -0,0 +1,9 @@ +name=ArxContainer +version=0.3.6 +author=hideakitai +maintainer=hideakitai +sentence=C++ container-like classes (vector, map, etc.) for Arduino which cannot use STL +paragraph=C++ container-like classes (vector, map, etc.) for Arduino which cannot use STL +category=Data Storage +url=https://github.com/hideakitai +architectures=* diff --git a/util/TeensyDirtySTLErrorSolution/LICENSE b/util/TeensyDirtySTLErrorSolution/LICENSE new file mode 100644 index 0000000..6f5269b --- /dev/null +++ b/util/TeensyDirtySTLErrorSolution/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Hideaki Tai + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/util/TeensyDirtySTLErrorSolution/README.md b/util/TeensyDirtySTLErrorSolution/README.md new file mode 100644 index 0000000..166a80f --- /dev/null +++ b/util/TeensyDirtySTLErrorSolution/README.md @@ -0,0 +1,57 @@ +# TeensyDirtySTLErrorSolution + +dirty solution to use STL in teensy + +## Introduction + +Sometimes we want to use STL on Teensy, but Teensy 3.x in Arduino IDE does not support STL by default. Though STL is enabled on Teensy 4.x, but you have to enable it manually on Teensy 3.x. If you use PlatformIO, you can also use STL on Teensy 3.x. + +| | Teensy 4.x | Teensy 3.x | +| ----------- | ----------------- | ----------------- | +| PlatformIO | Almost Compilable | Almost Compilable | +| Arduino IDE | Almost Compilable | NOT Compilable | + + +## Enable STL in Arduino IDE + +To enable STL on Teensy 3.x using Arduino IDE, add `-lstdc++` to `build flags`, which is defined in `boards.txt`. Many STL can be used by adding this flag. You have to do this procedure every time you upgrade the version of Teensyduino. + +| OS | default location | +| ------- | --------------------------------------------------------------- | +| macOS | `Arduino.app/Contents/Java/hardware/teensy/avr/boards.txt` | +| Windows | `C:\Program Files (x86)\Arduino\hardware\teensy\avr\boards.txt` | + + +#### Before + +``` boards.txt + teensy36.build.flags.libs=-larm_cortexM4lf_math -lm + teensy35.build.flags.libs=-larm_cortexM4lf_math -lm + teensy31.build.flags.libs=-larm_cortexM4l_math -lm +``` + +#### After + +``` boards.txt + teensy36.build.flags.libs=-larm_cortexM4lf_math -lm -lstdc++ + teensy35.build.flags.libs=-larm_cortexM4lf_math -lm -lstdc++ + teensy31.build.flags.libs=-larm_cortexM4l_math -lm -lstdc++ +``` + +## Do you still have compile error? + +Yes, maybe you still have compilation error in both 4.x and 3.x for some STLs... Then you should add this library (header file) to your project. Some dirty solution I found is included in this header file. + +``` C++ +#include +``` + +## Warning + +This method is not recommended because STL is not enabled by default... (if you know the reason, please let me know). I am not responsible for any trouble that may occur as a result of the use of this library. Please use at your own risk. + + +## License + +MIT + diff --git a/util/TeensyDirtySTLErrorSolution/TeensyDirtySTLErrorSolution.h b/util/TeensyDirtySTLErrorSolution/TeensyDirtySTLErrorSolution.h new file mode 100644 index 0000000..b50c6a3 --- /dev/null +++ b/util/TeensyDirtySTLErrorSolution/TeensyDirtySTLErrorSolution.h @@ -0,0 +1,65 @@ +#pragma once + +#include + +#ifdef TEENSYDUINO +#ifndef TEENSYDUINO_DIRTY_STL_ERROR_SOLUTION +#define TEENSYDUINO_DIRTY_STL_ERROR_SOLUTION + + +#if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) // TEENSY 3.1/3.2, 3.5, 3.6 + +extern "C" +{ + int _write() { return -1; } +} + +#elif defined(__IMXRT1062__) // TEENSY4.0 + +extern "C" +{ + void *__exidx_start __attribute__((__visibility__ ("hidden"))); + void *__exidx_end __attribute__((__visibility__ ("hidden"))); +} + +#else + + #error NOT SUPPORTED TEENSY VERSION + +#endif + + +// ----- template ----- +// +// #if defined(__MK20DX256__) // TEENSY3.1/3.2 +// #elif defined(__MK64FX512__) // TEENSY3.5 +// #elif defined(__MK66FX1M0__) // TEENSY3.6 +// #elif defined(__IMXRT1062__) // TEENSY4.0 +// +// extern "C" +// { +// int _getpid() { return -1; } +// int _kill(int pid, int sig) { return -1; } +// int _write() { return -1; } +// void *__exidx_start __attribute__((__visibility__ ("hidden"))); +// void *__exidx_end __attribute__((__visibility__ ("hidden"))); +// int _getpid();// { return -1; } +// int _kill(int pid, int sig);// { return -1; } +// int _write() { return -1; } +// void *__exidx_start __attribute__((__visibility__ ("hidden"))); +// void *__exidx_end __attribute__((__visibility__ ("hidden"))); +// } +// +// // copied from https://github.com/gcc-mirror/ +// namespace std +// { +// void __throw_bad_alloc() { _GLIBCXX_THROW_OR_ABORT(bad_alloc()); } +// void __throw_length_error(const char* __s __attribute__((unused))) { _GLIBCXX_THROW_OR_ABORT(length_error(_(__s))); } +// void __throw_bad_function_call() { _GLIBCXX_THROW_OR_ABORT(bad_function_call()); } +// void _Rb_tree_decrement(std::_Rb_tree_node_base* a) {} +// void _Rb_tree_insert_and_rebalance(bool, std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::_Rb_tree_node_base&) {} +// } + +#endif // TEENSYDUINO_DIRTY_STL_ERROR_SOLUTION +#endif // TEENSYDUINO +