Skip to content

Commit

Permalink
Adds support for 'actions' to be prefs, new split util
Browse files Browse the repository at this point in the history
  • Loading branch information
t413 committed Jul 2, 2020
1 parent 5575c1b commit 80a457f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 59 deletions.
27 changes: 18 additions & 9 deletions lib/MPPTLib/publishable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ struct Pub : PubItem {
bool isAction() const override { return false; }
};

String prefGetString(Preferences&p, String key) {
char buf[128];
size_t l = p.getBytes(key.c_str(), buf, 128);
if (l == 0) return "";
buf[l] = 0; //null terminate
return String(buf);
}

template<> String Pub<double*>::toString() const { return String(*value, 3); }
template<> String Pub<bool* >::toString() const { return (*value)? "true":"false"; }
template<> String Pub<Action>::toString() const { return (value)(""); }
Expand All @@ -25,16 +33,13 @@ template<> String Pub<Action>::set(String v) { return (value)(v); }
template<> void const* Pub<Action>::val() const { return &value; }

template<> bool Pub<Action>::isAction()const { return true; }
template<> void Pub<Action>::save(Preferences&p) { }
template<> void Pub<Action>::load(Preferences&p) { }
template<> void Pub<Action>::save(Preferences&p) { String v = (value)(""); p.putBytes(key.c_str(), v.c_str(), v.length()); }
template<> void Pub<Action>::load(Preferences&p) { String v = prefGetString(p, key); if (v.length()) try { (value)(v); } catch(...) { } }
template<> String Pub<String*>::set(String v) { return (*value) = v; }
template<> String Pub<String*>::toString() const { return (*value); }
template<> void Pub<String*>::save(Preferences&p) { p.putBytes(key.c_str(), value->c_str(), value->length()); }
template<> void Pub<String*>::load(Preferences&p) {
char buf[128];
size_t l = p.getBytes(key.c_str(), buf, 128);
buf[l] = 0; //null terminate
(*value) = String(buf);
(*value) = prefGetString(p, key);
}

Publishable::Publishable() : lock_(xSemaphoreCreateMutex()) {
Expand Down Expand Up @@ -129,9 +134,13 @@ String Publishable::handleCmd(String cmd) {
String Publishable::handleSet(String key, String val) {
for (auto i : items_)
if (i.first == key) {
String ret = i.second->set(val);
i.second->dirty_ = true;
return (ret.length())? ret : ("set " + key + " to " + val);
try {
String ret = i.second->set(val);
i.second->dirty_ = true;
return (ret.length())? ret : ("set " + key + " to " + val);
} catch (std::runtime_error e) {
return "error setting '" + key + "' to '" + val + "': " + String(e.what());
}
}
return "unknown key " + key;
}
Expand Down
7 changes: 7 additions & 0 deletions lib/MPPTLib/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ String str(bool v) {
return v ? " WIN" : " FAIL";
}

StringPair split(const String &str, const String &del) {
int at = str.indexOf(del);
if (at >= 0) return StringPair(str.substring(0, at), str.substring(at + del.length()));
return StringPair(str, "");
}


// float lifepo4_soc[] = {13.4, 13.3, 13.28, 13.};

Publishable* pub_; //static
Expand Down
102 changes: 52 additions & 50 deletions lib/MPPTLib/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,67 @@ String str(const char *fmtStr, ...);
String str(const std::string &s);
String str(bool v);

typedef std::pair<String,String> StringPair;
StringPair split(const String &str, const String &del);

template<typename T, uint16_t Size>
class CircularArray {
T buf_[Size];
T *head_, *tail_;
uint16_t count_;
T buf_[Size];
T *head_, *tail_;
uint16_t count_;
public:
CircularArray() : head_(buf_), tail_(buf_), count_(0) { }
~CircularArray() { }
CircularArray() : head_(buf_), tail_(buf_), count_(0) { }
~CircularArray() { }

bool push_front(T v) {
if (head_ == buf_) head_ = buf_ + Size;
*--head_ = v;
if (count_ == Size) {
if (tail_-- == buf_) tail_ = buf_ + Size - 1;
return false;
} else {
if (count_++ == 0) tail_ = head_;
return true;
}
}
bool push_front(T v) {
if (head_ == buf_) head_ = buf_ + Size;
*--head_ = v;
if (count_ == Size) {
if (tail_-- == buf_) tail_ = buf_ + Size - 1;
return false;
} else {
if (count_++ == 0) tail_ = head_;
return true;
}
}

bool push_back(T v) {
if (++tail_ == buf_ + Size) tail_ = buf_;
*tail_ = v;
if (count_ == Size) {
if (++head_ == buf_ + Size) head_ = buf_;
return false;
} else {
if (count_++ == 0) head_ = tail_;
return true;
}
}
bool push_back(T v) {
if (++tail_ == buf_ + Size) tail_ = buf_;
*tail_ = v;
if (count_ == Size) {
if (++head_ == buf_ + Size) head_ = buf_;
return false;
} else {
if (count_++ == 0) head_ = tail_;
return true;
}
}

T pop_front() {
T res = *head_++;
if (head_ == buf_ + Size) head_ = buf_;
count_--;
return res;
}
T pop_front() {
T res = *head_++;
if (head_ == buf_ + Size) head_ = buf_;
count_--;
return res;
}

T pop_end() {
T res = *tail_--;
if (tail_ == buf_) tail_ = buf_ + Size - 1;
count_--;
return res;
}
T pop_end() {
T res = *tail_--;
if (tail_ == buf_) tail_ = buf_ + Size - 1;
count_--;
return res;
}

T& operator [] (uint16_t index) { return *(buf_ + ((head_ - buf_ + index) % Size)); }
T& front() { return *head_; } //TODO: add bounds checks
T& back() { return *tail_; }
T& operator [] (uint16_t index) { return *(buf_ + ((head_ - buf_ + index) % Size)); }
T& front() { return *head_; } //TODO: add bounds checks
T& back() { return *tail_; }

uint16_t inline size() const { return count_; }
uint16_t inline available() const { return Size - count_; }
bool inline empty() const { return count_ == 0; }
bool inline isFull() const { return count_ == Size; }
uint16_t inline size() const { return count_; }
uint16_t inline available() const { return Size - count_; }
bool inline empty() const { return count_ == 0; }
bool inline isFull() const { return count_ == Size; }

void inline clear() {
head_ = tail_ = buf_;
count_ = 0;
}
void inline clear() {
head_ = tail_ = buf_;
count_ = 0;
}
};

0 comments on commit 80a457f

Please sign in to comment.