Skip to content

Commit

Permalink
style option modes
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Mar 27, 2024
1 parent 043b7e0 commit bf4b9e6
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 39 deletions.
18 changes: 15 additions & 3 deletions ProffieOS.ino
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ class NoLED;
#include "modes/bool_setting.h"
#include "modes/color_menues.h"
#include "modes/sorted_list_menues.h"
#include "modes/style_option_modes.h"

BladeConfig* current_config = nullptr;
class BladeBase* GetPrimaryBlade() {
Expand All @@ -655,6 +656,17 @@ int GetBladeNumber(BladeBase *blade) {
return 0;
}

#define RETURN_BLADE_BY_NUMBER(N) case N: return current_config->blade##N;

// 1 is first blade
BladeBase* GetBladeByNumber(int n) {
if (!current_config) return nullptr;
switch (n) {
ONCEPERBLADE(RETURN_BLADE_BY_NUMBER);
}
return nullptr;
}

const char* GetSaveDir() {
if (!current_config) return "";
if (!current_config->save_dir) return "";
Expand Down Expand Up @@ -693,9 +705,9 @@ int prop_GetBulletCount() {
}
#endif

class Color16 GetColorArg(int blade, int arg) { return prop.GetColorArg(blade, arg); }
void SetArg(int blade, int arg, const char* argument) { prop.SetArg(blade, arg, argument); }
void SetColorArg(int blade, int arg, Color16 color) { prop.SetColorArg(blade, arg, color); }
const char* GetStyle(int blade) { return prop.GetStyle(blade); }
void SetStyle(int blade, LSPtr<char> style);
void SetStyle(int blade, LSPtr<char> style) { prop.SetStyle(blade, std::move(style)); }
void SetFont(const char* font) { prop.SetFont(font); }
void SetTrack(const char* track) { prop.SetTrack(track); }
const char* GetFont() { return prop.GetFont(); }
Expand Down
14 changes: 12 additions & 2 deletions common/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class Color8 {
enum Byteorder {
NONE = 0,

R = 0x1,
G = 0x2,
B = 0x3,

// RGB colors
BGR=0x321,
BRG=0x312,
Expand Down Expand Up @@ -80,11 +84,17 @@ class Color8 {
};

static int num_bytes(int byteorder) {
return byteorder <= 0xfff ? 3 : 4;
return
byteorder <= 0xf ? 1 :
byteorder <= 0xfff ? 3 :
4;
}

static constexpr int inline_num_bytes(int byteorder) __attribute__((always_inline)) {
return byteorder <= 0xfff ? 3 : 4;
return
byteorder <= 0xf ? 1 :
byteorder <= 0xfff ? 3 :
4;
}


Expand Down
2 changes: 1 addition & 1 deletion modes/color_menues.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct ColorBlueMode : public SPEC::GammaMode {
template<class SPEC, class MENU>
struct SaveColorMenuEntry : public MenuEntry {
void say(int entry) override {
getSL<SPEC>->SaySave();
getSL<SPEC>()->SaySave();
}
void select(int entry) override {
getPtr<MENU>()->save();
Expand Down
2 changes: 0 additions & 2 deletions modes/smooth_mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ struct SmoothMode : public SPEC::SelectCancelMode {
float last_angle_;
};

// TODO: Smooth mode without wraparound.

} // namespace mode

#endif
74 changes: 71 additions & 3 deletions modes/style_argument_helpers.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,83 @@
#ifndef MODE_STYLE_ARGUMENT_HELPERS
#define MODE_STYLE_ARGUMENT_HELPERS

const char* GetStyle(int blade);
void SetStyle(int blade, LSPtr<char> style);

namespace mode {

int menu_current_blade = 1;
int menu_current_arg = 0;

bool isTimeArg(int arg) {
switch (arg) {
case IGNITION_TIME_ARG:
case IGNITION_DELAY_ARG:
case RETRACTION_TIME_ARG:
case RETRACTION_DELAY_ARG:
return true;
}
}

bool GetArg(int blade, int arg, char* argspace) {
return style_parser.GetArgument(GetStyle(blade), arg + 2, argspace);
}
void SetArg(int blade, int arg, const char* argument) {
SetStyle(blade, style_parser.SetArgument(GetStyle(blade), arg + 2, argument));
}

Color16 GetColorArg(int blade, int arg) {
char argspace[32];
if (GetArg(blade, arg, argspace)) {
char* tmp;
int r = strtol(argspace, &tmp, 0);
int g = strtol(tmp+1, &tmp, 0);
int b = strtol(tmp+1, NULL, 0);
return Color16(r,g,b);
}
return Color16(65535,0,0);
}

void SetColorArg(int blade, int arg, Color16 color) {
char tmp[32];
itoa(color.r, tmp, 10);
strcat(tmp, ",");
itoa(color.g, tmp + strlen(tmp), 10);
strcat(tmp, ",");
itoa(color.b, tmp + strlen(tmp), 10);

SetArg(blade, arg, tmp);
}

int GetIntArg(int blade, int arg) {
char argspace[32];
if (GetArg(blade, arg, argspace)) {
return strtol(argspace, nullptr, 0);
}
return -1;
}

void SetIntArg(int blade, int arg, int value) {
char tmp[32];
itoa(value, tmp, 10);
SetArg(blade, arg, tmp);
}

BladeStyle* GetCurrentBladeStyle() {
BladeBase* blade = GetBladeByNumber(menu_current_blade);
if (!blade) return nullptr;
return blade->current_style();
}

int GetMaxStyleArg() {
BladeStyle* style = GetCurrentBladeStyle();
if (!style) return ARG_MAX_UNKNOWN;
return style->get_max_arg(menu_current_arg);
}


} // namespace mode

Color16 GetColorArg(int blade, int arg);
void SetArg(int blade, int arg, const char* argument);
void SetColorArg(int blade, int arg, Color16 color);


#endif
114 changes: 114 additions & 0 deletions modes/style_option_modes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#ifndef MODES_STYLE_OPTION_MODES_H
#define MODES_STYLE_OPTION_MODES_H

namespace mode {

template<class SPEC>
class SelectArgSmoothMode : public SPEC::SmoothMode {
public:
int get() override { return GetIntArg(menu_current_blade, menu_current_arg); }
int set(int x) {
value_ = x;
if (!getSL<SPEC>()->busy()) {
getSL<SPEC>()->SayWhole(x * 100 / 32768);
getSL<SPEC>()->SayPercent();
}
}

void select() override {
SPEC::SmoothMode::select();
SetIntArg(menu_current_blade, menu_current_arg, value_);
popMode();
}

private:
int value_ = 0;
};

template<class SPEC>
class SelectArgTime : public SPEC::SmoothMode {
public:
int get() override { return GetIntArg(menu_current_blade, menu_current_arg); }
float t(int x) { return powf(x / 32768.0f, 2.0) * 30.0; }
int set(int x) {
value_ = x;
if (!getSL<SPEC>()->busy()) {
getSL<SPEC>()->SayNumber(t(x), SAY_DECIMAL);
getSL<SPEC>()->SaySeconds();
}
}

void select() override {
SPEC::SmoothMode::select();
SetIntArg(menu_current_blade, menu_current_arg, (int)(t(value_) * 1000));
popMode();
}

private:
int value_ = 0;
};

template<class SPEC>
class SelectArgNumber : public SPEC::MenuBase {
public:
void activate(bool onreturn) override {
SPEC::MenuBase::activate(onreturn);
if (!onreturn) {
int max = GetMaxStyleArg();
if (max < 0) max = 32768;
max_ = max;
this->pos_ = GetIntArg(menu_current_blade, menu_current_arg);
// TODO: What if pos_ > max_ ?
}
}
void say() override {
getSL<SPEC>()->SayWhole(this->pos_);
}
uint16_t size() override { return max_; }

void select() override {
SPEC::SmoothMode::select();
SetIntArg(menu_current_blade, menu_current_arg, this->pos_);
popMode();
}
private:
uint16_t max_;
};

template<class SPEC>
class SelectArgMode : public SPEC::MenuBase {
public:
void activate(bool onreturn) override {
// TODO: Set pos_ to something reasonable?
arginfo_ = style_parser.GetArgInfo(GetStyle(menu_current_blade));
SPEC::MenuBase::activate(onreturn);
}
int size() override { return arginfo_.used(); }
void say() override {
getSL<SPEC>()->SayArgument(getCurrentArgument());
}

void select() override {
menu_current_arg = getCurrentArgument();
if (arginfo_.iscolor(menu_current_arg)) {
pushMode<SPEC::SelectArgColor>();
} else {
int max_arg = GetMaxStyleArg();
if (max_arg == 32768 || max_arg == 32767) {
pushMode<SPEC::SelectArgSmooth>();
} else if (max_arg <= 0 && isTimeArg(menu_current_arg)) {
pushMode<SPEC::SelectArgTime>();
} else {
pushMode<SPEC::SelectArgNumber>();
}
}
}

protected:
int getCurrentArgument() { return arginfo_.nth(this->pos_); }
ArgInfo arginfo_;
};

} // namespace mode

#endif
32 changes: 5 additions & 27 deletions props/prop_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -1714,36 +1714,14 @@ class PropBase : CommandParser, Looper, protected SaberBase, public ModeInterfac
}
virtual bool Event2(enum BUTTON button, EVENT event, uint32_t modifiers) = 0;


Color16 GetColorArg(int blade, int arg) {
char argspace[32];
if (style_parser.GetArgument(current_preset_.GetStyle(blade), arg + 2, argspace)) {
char* tmp;
int r = strtol(argspace, &tmp, 0);
int g = strtol(tmp+1, &tmp, 0);
int b = strtol(tmp+1, NULL, 0);
return Color16(r,g,b);
}
return Color16(65535,0,0);
const char* GetStyle(int blade) {
return current_preset_.GetStyle(blade);
}

void SetArg(int blade, int arg, const char* argument) {
current_preset_.SetStyle(blade, style_parser.SetArgument(current_preset_.GetStyle(blade), arg + 2, argument));
}

void SetColorArg(int blade, int arg, Color16 color) {
char tmp[32];
itoa(color.r, tmp, 10);
strcat(tmp, ",");
itoa(color.g, tmp + strlen(tmp), 10);
strcat(tmp, ",");
itoa(color.b, tmp + strlen(tmp), 10);

SetArg(blade, arg, tmp);

void SetStyle(int blade, LSPtr<char> style) {
current_preset_.SetStyle(blade, std::move(style));
current_preset_.Save();
}

void SetFont(const char* font) {
current_preset_.font = mkstr(font);
current_preset_.Save();
Expand Down
1 change: 0 additions & 1 deletion styles/get_arg_max.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ struct GetArgMaxT<ARG, TrSelect, IntArg<ARG, DEFAULT_VALUE>, REST...> {
static const int value = combine_args(A, B);
};


template<int ARG, int DEFAULT_VALUE, int MAX>
struct GetArgMax< SingleValueAdapter<ModSVF<IntArgSVF<ARG, DEFAULT_VALUE>, IntSVF<MAX> >>, ARG> { static const int value = MAX; };

Expand Down
12 changes: 12 additions & 0 deletions styles/style_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ class StyleParser : public CommandParser {
return ap.nth(arg);
}

// Returns the ArgInfo for this style.
ArgInfo GetArgInfo(const char* str) {
NamedStyle* style = FindStyle(str);
if (!style) return ArgInfo();
GetUsedArgsParser ap(SkipWord(str));
CurrentArgParser = &ap;
delete style->style_allocator->make();
// Ignore the two "builtin" arguments
if (FirstWord(str, "builtin") && ap.used() <= 2) return ArgInfo();
return ap.getArgInfo();
}

// Get the Nth argument of a style string.
// The output will be copied to |output|.
// If the string itself doesn't contain that argument, the style
Expand Down

0 comments on commit bf4b9e6

Please sign in to comment.