Skip to content

Commit

Permalink
Cleanup to language menu, better wifi timeout, screensaver bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAbata committed Oct 2, 2023
1 parent cb173d2 commit b651e02
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 136 deletions.
Binary file modified examples/.DS_Store
Binary file not shown.
36 changes: 20 additions & 16 deletions examples/languages/de.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
{
"CHART": "DIAG",
"STOP": "STOPPEN",
"AUTO": "AUTO",
"Manual": "Manuell",
"Pres: %s": "Drük: %s",
"Sens: %s": "Empf: %s",
"Denied": "Bestritten",
"Edging Settings": "Kanteneinstellungen",
"Orgasm Settings": "Orgasmus-Einstellungen",
"UI Settings": "UI-Einstellungen",
"User Profiles": "Benutzerprofil",
"Networking": "Netzwerk",
"Update": "Aktualisieren",
"System Info": "Systeminformationen",
"BACK": "ZURÜCK",
"ENTER": "EINGEBEN"
"language": "Deutsch",
"code": "DE",
"keys": {
"CHART": "DIAG",
"STOP": "STOPPEN",
"AUTO": "AUTO",
"Manual": "Manuell",
"Pres: %s": "Drük: %s",
"Sens: %s": "Empf: %s",
"Denied": "Bestritten",
"Edging Settings": "Kanteneinstellungen",
"Orgasm Settings": "Orgasmus-Einstellungen",
"UI Settings": "UI-Einstellungen",
"User Profiles": "Benutzerprofil",
"Networking": "Netzwerk",
"Update": "Aktualisieren",
"System Info": "Systeminformationen",
"BACK": "ZURÜCK",
"ENTER": "EINGEBEN"
}
}
36 changes: 20 additions & 16 deletions examples/languages/es.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
{
"CHART": "CUADRO",
"STOP": "DETENER",
"AUTO": "AUTO",
"Manual": "Manual",
"Pres: %s": "Pres: %s",
"Sens: %s": "Sens: %s",
"Denied": "Denegado",
"Edging Settings": "Conf de Bordes",
"Orgasm Settings": "Conf del Orgasmo",
"UI Settings": "Conf de UI",
"User Profiles": "Perfiles de Usuario",
"Networking": "Redes",
"Update": "Actualizar",
"System Info": "Información del Sistema",
"BACK": "ATRÁS",
"ENTER": "INGRESAR"
"language": "Español",
"code": "ES",
"keys": {
"CHART": "CUADRO",
"STOP": "DETENER",
"AUTO": "AUTO",
"Manual": "Manual",
"Pres: %s": "Pres: %s",
"Sens: %s": "Sens: %s",
"Denied": "Denegado",
"Edging Settings": "Conf de Bordes",
"Orgasm Settings": "Conf del Orgasmo",
"UI Settings": "Conf de UI",
"User Profiles": "Perfiles de Usuario",
"Networking": "Redes",
"Update": "Actualizar",
"System Info": "Información del Sistema",
"BACK": "ATRÁS",
"ENTER": "INGRESAR"
}
}
36 changes: 20 additions & 16 deletions examples/languages/fr.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
{
"CHART": "GRAPHIQUE",
"STOP": "ARRÊT",
"AUTO": "AUTO",
"Manual": "Manuel",
"Pres: %s": "Presse",
"Sens: %s": "Détection : %s",
"Denied": "Refusé",
"Edging Settings": "Paramètres de bordure",
"Orgasm Settings": "Paramètres d'orgasme",
"UI Settings": "Paramètres de l'interface utilisateur",
"User Profiles": "Des profils d'utilisateurs",
"Networking": "La mise en réseau",
"Update": "Mise à jour",
"System Info": "Information système",
"BACK": "DOS",
"ENTER": "ENTRER"
"language": "Français",
"code": "FR",
"keys": {
"CHART": "GRAPHIQUE",
"STOP": "ARRÊT",
"AUTO": "AUTO",
"Manual": "Manuel",
"Pres: %s": "Presse",
"Sens: %s": "Détection : %s",
"Denied": "Refusé",
"Edging Settings": "Paramètres de bordure",
"Orgasm Settings": "Paramètres d'orgasme",
"UI Settings": "Paramètres de l'interface utilisateur",
"User Profiles": "Des profils d'utilisateurs",
"Networking": "La mise en réseau",
"Update": "Mise à jour",
"System Info": "Information système",
"BACK": "DOS",
"ENTER": "ENTRER"
}
}
36 changes: 20 additions & 16 deletions examples/languages/ja.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
{
"CHART": "チャート",
"STOP": "ストップ",
"AUTO": "自動",
"Manual": "マニュアル",
"Pres: %s": "プレス: %s",
"Sens: %s": "センス: %s",
"Denied": "拒否された",
"Edging Settings": "縁取り設定",
"Orgasm Settings": "オーガズム設定",
"UI Settings": "UI 設定",
"User Profiles": "ユーザープロファイル",
"Networking": "ネットワーキング",
"Update": "アップデート",
"System Info": "システム情報",
"BACK": "戻る",
"ENTER": "入力"
"language": "日本語",
"code": "JA",
"keys": {
"CHART": "チャート",
"STOP": "ストップ",
"AUTO": "自動",
"Manual": "マニュアル",
"Pres: %s": "プレス: %s",
"Sens: %s": "センス: %s",
"Denied": "拒否された",
"Edging Settings": "縁取り設定",
"Orgasm Settings": "オーガズム設定",
"UI Settings": "UI 設定",
"User Profiles": "ユーザープロファイル",
"Networking": "ネットワーキング",
"Update": "アップデート",
"System Info": "システム情報",
"BACK": "戻る",
"ENTER": "入力"
}
}
12 changes: 8 additions & 4 deletions examples/languages/robot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
file = File.read(ARGV[0])
json = JSON.parse(file)
translated = {}
lang = File.basename(ARGV[0], '.json')

json.each do |key, _|
json['keys'].each do |key, _|
puts "Translating \"#{key}\"..."
translated[key] = `trans -b en:#{lang} \"#{key}\"`.strip
translated[key] = `trans -b en:#{json['code'].downcase} \"#{key}\"`.strip
end

final = JSON.pretty_generate(translated)
final = JSON.pretty_generate({
language: json['language'],
code: json['code'],
keys: translated
})

puts final
36 changes: 20 additions & 16 deletions examples/languages/ru.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
{
"CHART": "ДИАГРАММА",
"STOP": "ОСТАНАВЛИВАТЬСЯ",
"AUTO": "АВТО",
"Manual": "Руководство",
"Pres: %s": "Нажимать",
"Sens: %s": "Чувствительность: %s",
"Denied": "Отклонен",
"Edging Settings": "Настройки окантовки",
"Orgasm Settings": "Настройки оргазма",
"UI Settings": "Настройки пользовательского интерфейса",
"User Profiles": "Профили пользователей",
"Networking": "Сеть",
"Update": "Обновлять",
"System Info": "Системная информация",
"BACK": "НАЗАД",
"ENTER": "ВХОДИТЬ"
"language": "Русский",
"code": "RU",
"keys": {
"CHART": "ДИАГРАММА",
"STOP": "ОСТАНАВЛИВАТЬСЯ",
"AUTO": "АВТО",
"Manual": "Руководство",
"Pres: %s": "Нажимать",
"Sens: %s": "Чувствительность: %s",
"Denied": "Отклонен",
"Edging Settings": "Настройки окантовки",
"Orgasm Settings": "Настройки оргазма",
"UI Settings": "Настройки пользовательского интерфейса",
"User Profiles": "Профили пользователей",
"Networking": "Сеть",
"Update": "Обновлять",
"System Info": "Системная информация",
"BACK": "НАЗАД",
"ENTER": "ВХОДИТЬ"
}
}
2 changes: 1 addition & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static const char* CONFIG_FILENAME = "/config.json";
#define WIFI_KEY_MAX_LEN 64

// Some Experiments
#define EOM_BETA 1
// #define EOM_BETA 1
#define I18N_USE_CJSON_DICT 1

// System Defaults
Expand Down
1 change: 1 addition & 0 deletions include/menus/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern const ui_menu_t UPDATE_MENU;
extern const ui_menu_t ACCESSORY_PORT_MENU;
extern const ui_menu_t BLUETOOTH_SCAN_MENU;
extern const ui_menu_t CONNECTED_ACCESSORIES_MENU;
extern const ui_menu_t UI_LANGUAGE_MENU;

#ifdef __cplusplus
}
Expand Down
5 changes: 4 additions & 1 deletion src/config_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ esp_err_t config_load_from_sd(const char* path, config_t* cfg) {
}

esp_err_t config_save_to_sd(const char* path, config_t* cfg) {
if (eom_hal_get_sd_size_bytes() > 0) return ESP_ERR_NO_MEM;
if (eom_hal_get_sd_size_bytes() <= 0) {
ESP_LOGW(TAG, "Request to save to SD without SD present.");
return ESP_ERR_NO_MEM;
}

cJSON* root = cJSON_CreateObject();
struct stat st;
Expand Down
20 changes: 17 additions & 3 deletions src/menus/networking_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "wifi_manager.h"
#include <stdio.h>

#define WIFI_TIMEOUT_TICKS 1000

static void on_wifi_state_save(int value, int final, UI_INPUT_ARG_TYPE arg) {
if (!final) return;

Expand Down Expand Up @@ -65,22 +67,34 @@ on_default_wifi_connect(const ui_menu_t* m, const ui_menu_item_t* item, UI_MENU_
if (err == ESP_OK) {
int ticks_wait = 0;

while (wifi_manager_get_status() != WIFI_MANAGER_CONNECTED && ticks_wait < 100) {
while (wifi_manager_get_status() != WIFI_MANAGER_CONNECTED &&
ticks_wait < WIFI_TIMEOUT_TICKS) {
vTaskDelay(1);
ticks_wait++;
if (ticks_wait % 10 == 0) ui_toast_append(".");
}

if (wifi_manager_get_status() != WIFI_MANAGER_CONNECTED) {
err = ticks_wait >= 100 ? ESP_ERR_WIFI_TIMEOUT : ESP_ERR_WIFI_NOT_CONNECT;
err = ticks_wait >= WIFI_TIMEOUT_TICKS ? ESP_ERR_WIFI_TIMEOUT
: ESP_ERR_WIFI_NOT_CONNECT;
}
}

if (err == ESP_OK) {
ui_toast(_("Connected!"));
ui_reenter_menu();
} else {
ui_toast(_("Failed to connect:\n%s"), esp_err_to_name(err));
char* errstr = NULL;

if (err == ESP_ERR_WIFI_TIMEOUT) {
errstr = _("WiFi connection took too long, gave up.");
} else if (err == ESP_ERR_WIFI_NOT_CONNECT) {
errstr = _("WiFi connection unstable?");
} else {
errstr = esp_err_to_name(err);
}

ui_toast(_("Failed to connect:\n%s"), errstr);
}
}
}
Expand Down
100 changes: 100 additions & 0 deletions src/menus/ui_language_menu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "cJSON.h"
#include "config.h"
#include "esp_log.h"
#include "ui/toast.h"
#include "ui/ui.h"
#include "util/fs.h"
#include "util/i18n.h"
#include <string.h>

static const char* TAG = "menu:ui_language";

static void on_language_select(
const struct ui_menu* m, const struct ui_menu_item* item, UI_MENU_ARG_TYPE menu_arg
) {
ui_toast_blocking(_("Saving..."));
const char* language = (const char*)item->arg;
strlcpy(Config.language_file_name, language == NULL ? "" : language, CONFIG_PATH_MAX);
config_enqueue_save(0);
i18n_deinit();

esp_err_t err = i18n_init();

if (err != ESP_OK) {
ui_toast("Invalid language file.");
} else {
ui_toast(_("Saved!"));
}
}

struct dir_result_arg {
const char* path;
int depth;
const ui_menu_t* menu;
};

static void _dir_result(const char* path, struct dirent* dir, void* argp) {
if (argp == NULL || path == NULL || dir == NULL) return;
struct dir_result_arg* arg = (struct dir_result_arg*)argp;
char full_path[CONFIG_PATH_MAX + 1];

snprintf(full_path, CONFIG_PATH_MAX, "%s/%s", arg->path, path);

if (dir->d_type == DT_DIR) {
if (arg->depth > 1) return;
ESP_LOGI(TAG, "Read Directory Recursive: %s/", full_path);

struct dir_result_arg dir_arg = {
.path = full_path,
.depth = arg->depth + 1,
.menu = arg->menu,
};

fs_read_dir(full_path, _dir_result, FS_READ_NO_HIDDEN | FS_READ_NO_FREE, &dir_arg);
return;
}

ESP_LOGI(TAG, "Read File: %s", full_path);
if (fs_strcmp_ext(dir->d_name, ".json")) return;

// Validate language:

char* buffer = fs_read_file(full_path);
if (buffer == NULL) return;

cJSON* root = cJSON_Parse(buffer);
free(buffer);

if (cJSON_IsObject(root)) {
cJSON* lang = cJSON_GetObjectItem(root, "language");

if (lang != NULL) {
char* lang_file = NULL;
// Dynamically allocate so we can add it as a menu arg, but also, skip the SD root to
// save space.
asiprintf(&lang_file, "%s", full_path + strlen(fs_sd_root()) + 1);

if (lang_file != NULL) {
ESP_LOGI(TAG, "Adding language: %s", lang->valuestring);

ui_menu_item_t* item = ui_menu_add_item(
arg->menu, lang->valuestring, on_language_select, (void*)lang_file
);

item->freer = free;
}
}
}

cJSON_Delete(root);
}

static void on_language_open(const ui_menu_t* m, UI_MENU_ARG_TYPE arg) {
ui_menu_add_item(m, "English", on_language_select, NULL);

struct dir_result_arg dir_arg = { .menu = m, .path = fs_sd_root(), .depth = 0 };

fs_read_dir(fs_sd_root(), _dir_result, FS_READ_NO_HIDDEN | FS_READ_NO_FREE, &dir_arg);
}

DYNAMIC_MENU(UI_LANGUAGE_MENU, "Language", on_language_open);
Loading

0 comments on commit b651e02

Please sign in to comment.