Skip to content

Commit

Permalink
Merge pull request #163 from anakod/quickglue
Browse files Browse the repository at this point in the history
QuickGLUI and apps
  • Loading branch information
sharandac authored Dec 30, 2020
2 parents fbaa534 + bd4d6de commit e346dbd
Show file tree
Hide file tree
Showing 57 changed files with 3,713 additions and 800 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ deploy.sh
.gitignore
data/tile_map.ods
firmware_fork_list.json
src/my-ttgo-watch.ino.cpp
59 changes: 59 additions & 0 deletions src/app/IRController/IRButton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef IR_BUTTON_H
#define IR_BUTTON_H

#include "config.h"
#include <IRremoteESP8266.h>

#define RAW_CODE_BUFER_SIZE sizeof(uint16_t)*120

struct InfraButton
{
~InfraButton() {
free(raw);
raw = nullptr;
}
String name;
decode_type_t mode;
uint32_t code = 0;
uint16_t *raw = nullptr;
int rawLength = 0;
Button uiButton;

void resize(int size)
{
if (raw == nullptr)
raw = (uint16_t*)ps_malloc(RAW_CODE_BUFER_SIZE);
rawLength = size;
memset(raw, 0, sizeof(uint16_t)*rawLength);
}
void loadFrom(JsonObject& source)
{
mode = (decode_type_t)(int)source["m"];

if (source.containsKey("rn")) {
const char* renamed = source["rn"];
if (name != renamed && uiButton.isCreated())
uiButton.text(renamed);
name = renamed;
}

if (source.containsKey("hex")) {
const char* hex = source["hex"];
if (hex != nullptr)
code = strtoul(hex, NULL, 16);
}

if (mode == decode_type_t::RAW && source.containsKey("raw")) {
JsonArrayConst arr = source["raw"].as<JsonArray>();
if (!arr.isNull()) {
log_d("RAW size: %d", arr.size());
resize(arr.size());
for (int i=0; i < arr.size(); i++)
raw[i] = arr[i].as<uint16_t>();
}
}
}
};

#endif

118 changes: 118 additions & 0 deletions src/app/IRController/IRConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <IRremoteESP8266.h>
#include "IRConfig.h"

IRConfig::IRConfig() : BaseJsonConfig("ir-remote.json") {
count = 0;
// This file is too big for that!
prettyJson = false;
}

InfraButton* IRConfig::add(const char* name) {
void* pointer = ps_malloc(sizeof(InfraButton));
InfraButton* btn = new (pointer) InfraButton();
btn->name = name;
buttons[count++] = btn;
return btn;
}

void IRConfig::del(const char* name) {
bool found = false;
for (int i = 0; i < count; i++) {
if (buttons[i]->name == name) {
if (buttons[i]->uiButton.isCreated()) {
buttons[i]->uiButton.free();
}
delete buttons[i];
found = true;
}
if (found)
buttons[i] = buttons[i+1];
}
if (found)
count--;
}

InfraButton* IRConfig::get(const char* name) {
for (int i = 0; i < count; i++) {
if (buttons[i]->name == name)
return buttons[i];
}
return nullptr;
}

void IRConfig::sendListNames(BluetoothJsonResponse& response) {
auto nestedArray = response.createNestedArray("v");
for (int i = 0; i < count; i++) {
nestedArray.add(buttons[i]->name);
}
response.send();
}

void IRConfig::sendButtonEdit(BluetoothJsonResponse& response, const char* name) {
auto btn = get(name);
if (btn != nullptr) {
response["v"] = btn->name;
response["m"] = (int)btn->mode;
if (btn->mode == decode_type_t::RAW) {
auto nestedArray = response.createNestedArray("raw");
for (int i = 0; i < btn->rawLength; i++)
nestedArray.add(btn->raw[i]);
} else {
response["hex"] = String(btn->code, 16);
}
}
response.send();
}

bool IRConfig::onSave(JsonDocument& document) {
auto pagesArray = document.createNestedArray("pages");
auto main = pagesArray.createNestedObject();
for (int i = 0; i < count; i++) {
JsonObject btnRecord = main.createNestedObject(buttons[i]->name);
btnRecord["m"] = buttons[i]->mode;
String hex((uint32_t)buttons[i]->code, 16);
btnRecord["hex"] = hex;
if (buttons[i]->mode == decode_type_t::RAW) {
auto rawArray = btnRecord.createNestedArray("raw");
for (int j = 0; j < buttons[i]->rawLength; j++)
rawArray.add(buttons[i]->raw[j]);
}
}
document["defBtnHeight"] = defBtnHeight;
document["defBtnWidth"] = defBtnWidth;
document["defSpacing"] = defSpacing;

return true;
}

bool IRConfig::onLoad(JsonDocument& document) {
JsonArray pages = document["pages"].as<JsonArray>();
if (pages.isNull() || pages.size() < 1) return false;

JsonObject main = pages[0].as<JsonObject>(); // For now only first page supported
if (main.isNull()) return false;

for (JsonPair record : main) {
if (record.value().isNull()) continue;
// Create and load button
log_d("Loading ir button: %s", record.key().c_str());
auto btn = add(record.key().c_str());
JsonObject configuration = record.value().as<JsonObject>();
btn->loadFrom(configuration);
}

if (document.containsKey("defBtnHeight"))
defBtnHeight = document["defBtnHeight"];
if (document.containsKey("defBtnWidth"))
defBtnWidth = document["defBtnWidth"];
if (document.containsKey("defSpacing"))
defSpacing = document["defSpacing"];
if (defBtnHeight < 12 || defBtnHeight > 200)
defBtnHeight = 12;
if (defBtnWidth < 50 || defBtnWidth > LV_HOR_RES-10)
defBtnWidth = 50;
if (defBtnWidth < 0 || defBtnWidth > 100)
defSpacing = 5;

return true;
}
41 changes: 41 additions & 0 deletions src/app/IRController/IRConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef IR_CONFIG_H
#define IR_CONFIG_H

#include "config.h"
#include "quickglui/quickglui.h"
#include "IRButton.h"

#define MAX_IR_BUTTONS 16

class IRConfig : public BaseJsonConfig
{
public:
IRConfig();

InfraButton* add(const char* name);
void del(const char* name);
InfraButton* get(const char* name);

InfraButton* get(int id) { return buttons[id]; }
int totalCount() { return count; }

void sendListNames(BluetoothJsonResponse& target);
void sendButtonEdit(BluetoothJsonResponse& target, const char* name);

public:
int defBtnWidth = 95;
int defBtnHeight = 33;
int defSpacing = 3;

protected:
virtual bool onSave(JsonDocument& document);
virtual bool onLoad(JsonDocument& document);
virtual size_t getJsonBufferSize() { return 16000; }

protected:
InfraButton *buttons[MAX_IR_BUTTONS];
int count = 0;
};

#endif

Loading

0 comments on commit e346dbd

Please sign in to comment.