Skip to content

Commit

Permalink
Fix compile error (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
yakumo-saki committed Mar 4, 2023
1 parent f6495ba commit 2512cd0
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 28 deletions.
7 changes: 7 additions & 0 deletions include/ConfigClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ class Config {
return parseBooleanString(val);
}

// 設定値を取得する(int)
int getAsInteger(String key) {
String val = configMap.get(key);
int i = val.toInt();
return i;
}

// set 普通のキー
ConfigSetResult set(String key, String value, bool haltOnNoKey = true) {
// debuglog("[Config] key=" + key + " value=" + value);
Expand Down
63 changes: 43 additions & 20 deletions include/SimpleMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,47 @@ struct SimpleMapData {
};

/**
* Map-Like array
* Map-Like array.
* NOTE: Key is always string.
* typename T is typename of Data
*/
template <typename T> class SimpleMap {

private:
static const int NOT_FOUND = -128;

std::vector<SimpleMapData<T>> vector;
std::vector<SimpleMapData<T>> values;

/**
* Returns index of key.
* @return index of key. if not found return NOT_FOUND (-128)
*/
int findIndex(String key) {
for (unsigned int i = 0; i < values.size(); i++) {
// debuglog("NOW=" + vector[i].key + " target=" + key);
if (values[i].key == key) return i;
}
return NOT_FOUND;
}

public:

// すべて削除
void clear() {
vector.clear();
values.clear();
}

// return all keys
/** return all keys in map */
std::vector<String> getKeys() {
std::vector<String> ret;
// debuglog("SimpleMap keys start");
for (auto itr : vector) {
for (auto itr : values) {
// debuglog("config creation add key=" + itr.key);
ret.push_back(itr.key);
}
return ret;
}

int findIndex(String key) {
for (unsigned int i = 0; i < vector.size(); i++) {
// debuglog("NOW=" + vector[i].key + " target=" + key);
if (vector[i].key == key) return i;
}
return NOT_FOUND;
}

bool hasKey(String key) {
return (findIndex(key) != NOT_FOUND);
}
Expand All @@ -60,7 +66,7 @@ template <typename T> class SimpleMap {
T temp;
return temp;
}
return vector[idx].value;
return values[idx].value;
}

// @param create Create new key or not.
Expand All @@ -74,29 +80,46 @@ template <typename T> class SimpleMap {

SimpleMapData<T> data {key, value};
if (idx == NOT_FOUND) {
vector.push_back(data);
values.push_back(data);
} else {
vector[idx] = data;
values[idx] = data;
}

// debuglog("[SimpleMap] SET KEY=" + key + " value=" + value);

return true;
}

// @param failIfExist Fail if key is already exist
// @return success or not
/**
* put new item to map.
* @param failIfExist true -> Fail if key is already exist. false -> overwrite
* @return success or not
*/
bool put(String key, const T& value, bool failIfExist = false) {
int idx = this->findIndex(key);
if (idx != NOT_FOUND && failIfExist) return false;

SimpleMapData<T> data {key, value};
if (idx == NOT_FOUND) {
vector.push_back(data);
values.push_back(data);
} else {
vector[idx] = data;
values[idx] = data;
}
return true;
}

// remove key from map.
// @return success or not (not means key not found)
bool remove(String key) {

for (auto it = values.begin(); it != values.end(); ++it) {
if (it->key == key) {
values.erase(it);
return true;
}
}

return false;
}

};
12 changes: 12 additions & 0 deletions include/display/brightnessManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <Arduino.h>

extern const String MOD_BRIGHTNESS_AUTODIMMER;
extern const String MOD_BRIGHTNESS_TIMEDIMMER;

// ディスプレイの明るさを変更したい時に明るさとモジュール名を登録する
// 実際の明るさは優先順位を加味して決まる
void registerBrightness(String moduleName, int brightness);

// ディスプレイの明るさセットリクエストを削除する
// 登録されていないモジュール名の場合は何も起きない
void unregisterBrightness(String moduleName);
10 changes: 3 additions & 7 deletions src/display/autodimmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "log.h"
#include "config/config.h"
#include "ConfigClass.h"
#include "display/display.h"
#include "display/brightnessManager.h"

unsigned int belowThreasholdSecond = 0;

Expand All @@ -35,7 +35,7 @@ void autodimmer_loop() {
displog("Dimmer disable. restore brightness.");

dimming = false;
disp_set_brightness(lastBrightness);
unregisterBrightness(MOD_BRIGHTNESS_AUTODIMMER);
}

return;
Expand All @@ -53,10 +53,6 @@ void autodimmer_loop() {
// 減光する
dimming = true;

displog("Dimmer Enable. set Brightness = 0");

// 本当はこの時点の明るさがほしいが取得できないので仮おき
lastBrightness = config->get(ConfigNames::DISPLAY_BRIGHTNESS).toInt();
disp_set_brightness(0);
registerBrightness(MOD_BRIGHTNESS_AUTODIMMER, 0);
}
}
53 changes: 53 additions & 0 deletions src/display/brightnessManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Auto dimmer
//
#include <Arduino.h>

#include "display/display_util.h"

#include "global.h"
#include "log.h"
#include "config/config.h"
#include "ConfigClass.h"
#include "display/display.h"
#include "SimpleMap.h"

SimpleMap<int> brightnessRegisterMap;

extern const String MOD_BRIGHTNESS_AUTODIMMER = "AUTODIMMER";
extern const String MOD_BRIGHTNESS_TIMEDIMMER = "TIMEDIMMER";

// 優先順位順に最初に見つけた明るさをセットする
void _applyBrightness() {

int brightness = -1;

if (brightnessRegisterMap.hasKey(MOD_BRIGHTNESS_TIMEDIMMER)) {
brightness = brightnessRegisterMap.get(MOD_BRIGHTNESS_TIMEDIMMER);
} else if (brightnessRegisterMap.hasKey(MOD_BRIGHTNESS_AUTODIMMER)) {
brightness = brightnessRegisterMap.get(MOD_BRIGHTNESS_AUTODIMMER);
}

if (brightness != -1) {
// timedimmer or autodimmer is active(dimming)
disp_set_brightness(brightness);
} else {
// no dimmers are active.
disp_set_brightness(config->getAsInteger(ConfigNames::DISPLAY_BRIGHTNESS));
}

}

// ディスプレイの明るさを変更したい時に明るさとモジュール名を登録する
// 実際の明るさは優先順位を加味して決まる
void registerBrightness(String moduleName, int brightness) {
brightnessRegisterMap.put(moduleName, brightness, false);
_applyBrightness();
}

// ディスプレイの明るさセットリクエストを削除する
// 登録されていないモジュール名の場合は何も起きない
void unregisterBrightness(String moduleName) {
brightnessRegisterMap.remove(moduleName);
_applyBrightness();
}
3 changes: 2 additions & 1 deletion src/network/api/v1/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ void http_handle_power() {
void http_handle_brightness() {
String value = server.arg("value");
int brightness = value.toInt();
// brightnessManagerに投げたいところだが、一時的に変更する用途だろうと思われるので直接変更
String msg = disp_set_brightness(brightness);

DynamicJsonDocument json(100);
json["command"] = "DISPLAY_BRIGHTNESS";
json["success"] = true;
json["msg"] = msg;
json["msg"] = msg + "\nDont forget to change config if you like new brightness!";

String jsonStr;
serializeJson(json, jsonStr);
Expand Down

0 comments on commit 2512cd0

Please sign in to comment.