Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
GyverLibs committed Oct 7, 2024
1 parent 493de0a commit b09f7c5
Show file tree
Hide file tree
Showing 17 changed files with 1,333 additions and 730 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.piopm
476 changes: 365 additions & 111 deletions README.md

Large diffs are not rendered by default.

165 changes: 165 additions & 0 deletions examples/demo_data/demo_data.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include <Arduino.h>

// вывод всех виджетов в две группы, одна с БД, вторая с переменными
// переменные для удобства запакованы в структуру

#define WIFI_SSID ""
#define WIFI_PASS ""

#ifdef ESP8266
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif

#include <GyverDBFile.h>
#include <LittleFS.h>
GyverDBFile db(&LittleFS, "/data2.db");

#include <SettingsGyver.h>
SettingsGyver sett("My Settings", &db);

DB_KEYS(
kk,
label,
led,
paragr,
input,
inputc,
number,
pass,
color,
sw,
datew,
timew,
datetime,
slider,
sel,
conf,
btn);

struct Data {
// String label = "label";
// bool led = 1;
String paragr = "paragraph";
String input = "input";
char inputc[20] = "input";
int number = 123456;
String pass = "pass";
uint32_t color = 0xff0000;
bool sw = true;
uint32_t datew = 1728123055;
uint32_t timew = 12 * 3600 + 30 * 60;
uint32_t datetime = 1728123055;
float slider = 33;
uint8_t sel = 1;
};

Data data;
bool cfm_f = false;

void build(sets::Builder& b) {
{
sets::Group g(b, "database");

b.Label(kk::label);
b.LED(kk::led);
b.Paragraph(kk::paragr);
b.Input(kk::input);
b.Number(kk::number);
b.Pass(kk::pass);
b.Color(kk::color);
b.Switch(kk::sw);
b.Date(kk::datew);
b.Time(kk::timew);
b.DateTime(kk::datetime);
b.Slider(kk::slider);
b.Select(kk::sel, "", "foo;bar;test");
if (b.Button(kk::btn)) Serial.println("btn 0");
}
{
sets::Group g(b, "variables");

// b.Label("", data.label);
// b.LED("", data.led);
b.Label("", data.input);
b.LED("", data.sw);
b.Paragraph("", data.paragr);
b.Input("", &data.input);
b.Input("", AnyPtr(data.inputc, 20));
b.Number("", &data.number);
b.Pass("", &data.pass);
b.Color("", &data.color);
b.Switch("", &data.sw);
b.Date("", &data.datew);
b.Time("", &data.timew);
b.DateTime("", &data.datetime);
b.Slider("", 0, 100, 0.5, "", &data.slider);
b.Select("", "foo;bar;test", &data.sel);
if (b.Button()) Serial.println("btn 1");
}

if (b.Button("Confirm")) cfm_f = true;

if (b.Confirm(kk::conf, "Confirm")) {
Serial.println(b.build.value);
}
}

void update(sets::Updater& u) {
if (cfm_f) {
cfm_f = false;
u.update(kk::conf);
}
}

void setup() {
Serial.begin(115200);
Serial.println();

// ======== WIFI ========

// STA
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
uint8_t tries = 20;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (!--tries) break;
}
Serial.println();
Serial.print("Connected: ");
Serial.println(WiFi.localIP());

// ======== SETTINGS ========
sett.begin();
sett.onBuild(build);
sett.onUpdate(update);

// ======== DATABASE ========
#ifdef ESP32
LittleFS.begin(true);
#else
LittleFS.begin();
#endif
db.begin();

db.init(kk::label, "label");
db.init(kk::led, 1);
db.init(kk::paragr, "paragraph");
db.init(kk::input, "input");
db.init(kk::number, 123456);
db.init(kk::pass, "pass");
db.init(kk::color, 0xff0000);
db.init(kk::sw, true);
db.init(kk::datew, 1728123055);
db.init(kk::timew, 12 * 3600 + 30 * 60);
db.init(kk::datetime, 1728123055);
db.init(kk::slider, 33);
db.init(kk::sel, 1);
}

void loop() {
sett.tick();
}
6 changes: 3 additions & 3 deletions examples/test/test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ enum kk : size_t {
// билдер! Тут строится наше окно настроек
void build(sets::Builder& b) {
// можно узнать, было ли действие по виджету
if (b.build().isAction()) {
if (b.build.isAction()) {
Serial.print("Set: 0x");
Serial.print(b.build().id(), HEX);
Serial.print(b.build.id, HEX);
Serial.print(" = ");
Serial.println(b.build().value());
Serial.println(b.build.value);
}

// группа. beginGroup всегда вернёт true для удобства организации кода
Expand Down
6 changes: 3 additions & 3 deletions examples/wificonnect/wificonnect.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ SettingsESP sett("WiFi config", &db);

DB_KEYS(
kk,
DB_KEY(wifi_ssid),
DB_KEY(wifi_pass),
DB_KEY(apply));
wifi_ssid,
wifi_pass,
apply);

void build(sets::Builder& b) {
{
Expand Down
6 changes: 3 additions & 3 deletions examples/wificonnector/wificonnector.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ SettingsESP sett("WiFi config", &db);

DB_KEYS(
kk,
DB_KEY(wifi_ssid),
DB_KEY(wifi_pass),
DB_KEY(apply));
wifi_ssid,
wifi_pass,
apply);

void build(sets::Builder& b) {
{
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Settings
version=1.0.24
version=1.1.0
author=AlexGyver <alex@alexgyver.ru>
maintainer=AlexGyver <alex@alexgyver.ru>
sentence=Simple UI webface builder for esp8266/esp32
Expand Down
4 changes: 4 additions & 0 deletions src/SettingsAsync.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

class SettingsAsync : public sets::SettingsBase {
public:
#ifndef SETT_NO_DB
SettingsAsync(const String &title = "", GyverDB *db = nullptr) : sets::SettingsBase(title, db), server(80) {}
#else
SettingsAsync(const String &title = "") : sets::SettingsBase(title), server(80) {}
#endif

void begin(bool useDns = true) {
if (useDns) _dns.begin();
Expand Down
40 changes: 32 additions & 8 deletions src/SettingsBase.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#pragma once
#include <Arduino.h>
#include <FS.h>
#include <GyverDB.h>
#include <StringUtils.h>

#ifndef SETT_NO_DB
#include <GyverDB.h>
#endif

#ifdef ESP8266
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif

#include "core/SettingsBase_class.h"
#include "core/builder.h"
#include "core/colors.h"
#include "core/containers.h"
Expand All @@ -28,7 +30,11 @@ class SettingsBase {
typedef std::function<void(Updater& upd)> UpdateCallback;

public:
#ifndef SETT_NO_DB
SettingsBase(const String& title = "", GyverDB* db = nullptr) : _title(title), _db(db) {
#else
SettingsBase(const String& title = "") : _title(title) {
#endif
useAutoUpdates(true);
}

Expand All @@ -47,15 +53,19 @@ class SettingsBase {
_updPeriod = prd;
}

// подключить базу данных
// подключить базу данных
#ifndef SETT_NO_DB
void attachDB(GyverDB* db) {
_db = db;
}
#endif

// использовать автоматические обновления из БД (при изменении записи новое значение отправится в браузер) (умолч. true)
void useAutoUpdates(bool use) {
_dbupdates = use;
#ifndef SETT_NO_DB
if (_db) _db->useUpdates(use);
#endif
}

// обработчик билда
Expand All @@ -70,7 +80,9 @@ class SettingsBase {

// тикер, вызывать в родительском классе
void tick() {
#ifndef SETT_NO_DB
if (_db) _db->tick();
#endif
if (_rst) {
delay(3000);
ESP.restart();
Expand Down Expand Up @@ -98,7 +110,6 @@ class SettingsBase {
void parse(Text passh, Text action, Text idtxt, Text value) {
size_t id = idtxt.toInt32HEX();
bool granted = authenticate(passh);
thisSettings = this;

switch (action.hash()) {
case SH("discover"): {
Expand All @@ -116,14 +127,16 @@ class SettingsBase {
break;

case SH("set"):
#ifndef SETT_NO_DB
if (_db) {
if (_dbupdates) _db->useUpdates(false);
_db->update(id, value);
if (_dbupdates) _db->useUpdates(true);
}
#endif
if (_build_cb) {
Build action(Build::Type::Set, granted, id, value);
Builder b(action);
Builder b(this, action);
_build_cb(b);
if (b.isReload()) _sendBuild(granted);
else _answerEmpty();
Expand All @@ -135,7 +148,7 @@ class SettingsBase {
case SH("click"):
if (_build_cb) {
Build action(Build::Type::Click, granted, id);
Builder b(action);
Builder b(this, action);
_build_cb(b);
if (b.isReload()) _sendBuild(granted);
else _answerEmpty();
Expand All @@ -145,13 +158,18 @@ class SettingsBase {
break;

case SH("ping"):
#ifndef SETT_NO_DB
if (_upd_cb || _db) {
#else
if (_upd_cb) {
#endif
Packet p;
Updater upd(p);
p.beginObj();
p.addCode(Code::type, Code::update);
p.addUint(Code::rssi, constrain(2 * (WiFi.RSSI() + 100), 0, 100));
p.beginArr(Code::content);
#ifndef SETT_NO_DB
if (_db && _dbupdates) {
while (_db->updatesAvailable()) {
size_t id = _db->updateNext();
Expand All @@ -162,6 +180,7 @@ class SettingsBase {
p.endObj();
}
}
#endif
if (_upd_cb) _upd_cb(upd);
p.endArr();
p.endObj();
Expand All @@ -188,15 +207,16 @@ class SettingsBase {
}
break;
}
thisSettings = nullptr;
}

private:
BuildCallback _build_cb = nullptr;
UpdateCallback _upd_cb = nullptr;
String _title;
size_t _passh = 0;
#ifndef SETT_NO_DB
GyverDB* _db = nullptr;
#endif
uint16_t _updPeriod = 2500;
bool _dbupdates = true;
bool _rst = false;
Expand Down Expand Up @@ -237,7 +257,11 @@ class SettingsBase {
#endif
p.beginArr(Code::content);
Build action(Build::Type::Build, granted);
Builder builder(action, _db, &p);
#ifndef SETT_NO_DB
Builder builder(this, action, &p, _db);
#else
Builder builder(this, action, &p);
#endif
_build_cb(builder);
p.endArr();
p.endObj();
Expand Down
4 changes: 4 additions & 0 deletions src/SettingsESP.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

class SettingsESP : public sets::SettingsBase {
public:
#ifndef SETT_NO_DB
SettingsESP(const String& title = "", GyverDB* db = nullptr) : sets::SettingsBase(title, db), server(80) {}
#else
SettingsESP(const String& title = "") : sets::SettingsBase(title), server(80) {}
#endif

void begin(bool useDns = true) {
if (useDns) _dns.begin();
Expand Down
Loading

0 comments on commit b09f7c5

Please sign in to comment.