From da80b65090a1700ea8ac89c5d966bc1ef63af9b2 Mon Sep 17 00:00:00 2001 From: Daniel Nilsson Date: Tue, 8 Aug 2023 20:41:50 +0200 Subject: [PATCH] Light Modes #160 * Add more options for controllig external light output --- src/firmware/app.c | 25 ++++++------ src/firmware/cfgstore.c | 2 +- src/firmware/cfgstore.h | 7 +++- src/tool/Model/Configuration.cs | 42 ++++++++++++-------- src/tool/View/SystemView.xaml | 28 +++++++++---- src/tool/ViewModel/ConfigurationViewModel.cs | 42 +++++++++++++------- 6 files changed, 93 insertions(+), 53 deletions(-) diff --git a/src/firmware/app.c b/src/firmware/app.c index 3f4ca65..b45e5c2 100644 --- a/src/firmware/app.c +++ b/src/firmware/app.c @@ -90,7 +90,7 @@ void app_init() { motor_disable(); lights_disable(); - lights_set(g_config.lights_always_on); + lights_set(g_config.lights_mode == LIGHTS_MODE_ALWAYS_ON); lvc_voltage_x100 = g_config.low_cut_off_v * 100u; @@ -184,7 +184,7 @@ void app_process() motor_disable(); } - if (motor_status() & MOTOR_ERROR_LVC) + if (g_config.lights_mode == LIGHTS_MODE_DISABLED /*|| (motor_status() & MOTOR_ERROR_LVC) */) { lights_disable(); } @@ -242,12 +242,7 @@ void app_set_lights(bool on) } else { - if (g_config.lights_always_on) - { - on = true; - } - - if (last_light_state != on) + if (g_config.lights_mode == LIGHTS_MODE_DEFAULT && last_light_state != on) { last_light_state = on; eventlog_write_data(EVT_DATA_LIGHTS, on); @@ -793,14 +788,20 @@ bool apply_shift_sensor_interrupt(uint8_t* target_current) #endif bool apply_brake(uint8_t* target_current) -{ - if (brake_is_activated()) +{ + bool is_braking = brake_is_activated(); + + if (g_config.lights_mode == LIGHTS_MODE_BRAKE_LIGHT) + { + lights_set(is_braking); + } + + if (is_braking) { *target_current = 0; - return true; } - return false; + return is_braking; } void apply_current_ramp_up(uint8_t* target_current, bool enable) diff --git a/src/firmware/cfgstore.c b/src/firmware/cfgstore.c index a8a6073..aca8bba 100644 --- a/src/firmware/cfgstore.c +++ b/src/firmware/cfgstore.c @@ -165,7 +165,7 @@ static void load_default_config() g_config.use_push_walk = 1; g_config.use_temperature_sensor = TEMPERATURE_SENSOR_CONTR | TEMPERATURE_SENSOR_MOTOR; - g_config.lights_always_on = 0; + g_config.lights_mode = LIGHTS_MODE_DEFAULT; g_config.wheel_size_inch_x10_u16l = (uint8_t)280; g_config.wheel_size_inch_x10_u16h = (uint8_t)(280 >> 8); diff --git a/src/firmware/cfgstore.h b/src/firmware/cfgstore.h index f867914..02f7d65 100644 --- a/src/firmware/cfgstore.h +++ b/src/firmware/cfgstore.h @@ -49,6 +49,11 @@ #define THROTTLE_GLOBAL_SPEED_LIMIT_ENABLED 1 #define THROTTLE_GLOBAL_SPEED_LIMIT_STD_LVLS 2 +#define LIGHTS_MODE_DEFAULT 0 +#define LIGHTS_MODE_DISABLED 1 +#define LIGHTS_MODE_ALWAYS_ON 2 +#define LIGHTS_MODE_BRAKE_LIGHT 3 + #define CONFIG_VERSION 4 #define PSTATE_VERSION 1 @@ -84,7 +89,7 @@ typedef struct uint8_t use_shift_sensor; uint8_t use_push_walk; uint8_t use_temperature_sensor; - uint8_t lights_always_on; + uint8_t lights_mode; // speed sensor uint8_t wheel_size_inch_x10_u16l; diff --git a/src/tool/Model/Configuration.cs b/src/tool/Model/Configuration.cs index 0e9bc77..232505d 100644 --- a/src/tool/Model/Configuration.cs +++ b/src/tool/Model/Configuration.cs @@ -78,7 +78,7 @@ public enum AssistFlagsType : byte SpeedOverride = 0x40 }; - public enum ThrottleGlobalSpeedLimit + public enum ThrottleGlobalSpeedLimitOptions { Disabled = 0x00, Enabled = 0x01, @@ -101,6 +101,14 @@ public enum WalkModeData BatteryPercent = 3 } + public enum LightsModeOptions + { + Default = 0, + Disabled = 1, + AlwaysOn = 2, + BrakeLight = 3 + } + public class AssistLevel { @@ -163,7 +171,7 @@ public uint MaxCurrentLimitAmps public TemperatureSensor UseTemperatureSensor; // lights - public bool LightsAlwaysOn; + public LightsModeOptions LightsMode; // speed sensor public float WheelSizeInch; @@ -179,7 +187,7 @@ public uint MaxCurrentLimitAmps public uint ThrottleStartMillivolts; public uint ThrottleEndMillivolts; public uint ThrottleStartPercent; - public ThrottleGlobalSpeedLimit ThrottleGlobalSpeedLimitOpt; + public ThrottleGlobalSpeedLimitOptions ThrottleGlobalSpeedLimit; public uint ThrottleGlobalSpeedLimitPercent; // shift interrupt options @@ -215,7 +223,7 @@ public Configuration(BbsfwConnection.Controller target) UsePushWalk = false; UseTemperatureSensor = TemperatureSensor.All; - LightsAlwaysOn = false; + LightsMode = LightsModeOptions.Default; WheelSizeInch = 0; NumWheelSensorSignals = 0; @@ -229,7 +237,7 @@ public Configuration(BbsfwConnection.Controller target) ThrottleStartMillivolts = 0; ThrottleEndMillivolts = 0; ThrottleStartPercent = 0; - ThrottleGlobalSpeedLimitOpt = ThrottleGlobalSpeedLimit.Disabled; + ThrottleGlobalSpeedLimit = ThrottleGlobalSpeedLimitOptions.Disabled; ThrottleGlobalSpeedLimitPercent = 0; ShiftInterruptDuration = 0; @@ -336,8 +344,8 @@ public bool ParseFromBufferV1(byte[] buffer) UseShiftSensor = true; ShiftInterruptDuration = 600; ShiftInterruptCurrentThresholdPercent = 10; - LightsAlwaysOn = false; - ThrottleGlobalSpeedLimitOpt = ThrottleGlobalSpeedLimit.Disabled; + LightsMode = LightsModeOptions.Default; + ThrottleGlobalSpeedLimit = ThrottleGlobalSpeedLimitOptions.Disabled; ThrottleGlobalSpeedLimitPercent = 100; return true; @@ -409,8 +417,8 @@ public bool ParseFromBufferV2(byte[] buffer) UseShiftSensor = true; ShiftInterruptDuration = 600; ShiftInterruptCurrentThresholdPercent = 10; - LightsAlwaysOn = false; - ThrottleGlobalSpeedLimitOpt = ThrottleGlobalSpeedLimit.Disabled; + LightsMode = LightsModeOptions.Default; + ThrottleGlobalSpeedLimit = ThrottleGlobalSpeedLimitOptions.Disabled; ThrottleGlobalSpeedLimitPercent = 100; return true; @@ -482,8 +490,8 @@ public bool ParseFromBufferV3(byte[] buffer) } // apply default settings for non existing options in version - LightsAlwaysOn = false; - ThrottleGlobalSpeedLimitOpt = ThrottleGlobalSpeedLimit.Disabled; + LightsMode = LightsModeOptions.Default; + ThrottleGlobalSpeedLimit = ThrottleGlobalSpeedLimitOptions.Disabled; ThrottleGlobalSpeedLimitPercent = 100; return true; @@ -513,7 +521,7 @@ public bool ParseFromBufferV4(byte[] buffer) UsePushWalk = br.ReadBoolean(); UseTemperatureSensor = (TemperatureSensor)br.ReadByte(); - LightsAlwaysOn = br.ReadBoolean(); + LightsMode = (LightsModeOptions)br.ReadByte(); WheelSizeInch = br.ReadUInt16() / 10f; NumWheelSensorSignals = br.ReadByte(); @@ -526,7 +534,7 @@ public bool ParseFromBufferV4(byte[] buffer) ThrottleStartMillivolts = br.ReadUInt16(); ThrottleEndMillivolts = br.ReadUInt16(); ThrottleStartPercent = br.ReadByte(); - ThrottleGlobalSpeedLimitOpt = (ThrottleGlobalSpeedLimit)br.ReadByte(); + ThrottleGlobalSpeedLimit = (ThrottleGlobalSpeedLimitOptions)br.ReadByte(); ThrottleGlobalSpeedLimitPercent = br.ReadByte(); ShiftInterruptDuration = br.ReadUInt16(); @@ -581,7 +589,7 @@ public byte[] WriteToBuffer() bw.Write(UsePushWalk); bw.Write((byte)UseTemperatureSensor); - bw.Write(LightsAlwaysOn); + bw.Write((byte)LightsMode); bw.Write((UInt16)(WheelSizeInch * 10)); bw.Write((byte)NumWheelSensorSignals); @@ -594,7 +602,7 @@ public byte[] WriteToBuffer() bw.Write((UInt16)ThrottleStartMillivolts); bw.Write((UInt16)ThrottleEndMillivolts); bw.Write((byte)ThrottleStartPercent); - bw.Write((byte)ThrottleGlobalSpeedLimitOpt); + bw.Write((byte)ThrottleGlobalSpeedLimit); bw.Write((byte)ThrottleGlobalSpeedLimitPercent); bw.Write((UInt16)ShiftInterruptDuration); @@ -642,7 +650,7 @@ public void CopyFrom(Configuration cfg) UseShiftSensor = cfg.UseShiftSensor; UsePushWalk = cfg.UsePushWalk; UseTemperatureSensor = cfg.UseTemperatureSensor; - LightsAlwaysOn = cfg.LightsAlwaysOn; + LightsMode = cfg.LightsMode; WheelSizeInch = cfg.WheelSizeInch; NumWheelSensorSignals = cfg.NumWheelSensorSignals; MaxSpeedKph = cfg.MaxSpeedKph; @@ -653,7 +661,7 @@ public void CopyFrom(Configuration cfg) ThrottleStartMillivolts = cfg.ThrottleStartMillivolts; ThrottleEndMillivolts = cfg.ThrottleEndMillivolts; ThrottleStartPercent = cfg.ThrottleStartPercent; - ThrottleGlobalSpeedLimitOpt = cfg.ThrottleGlobalSpeedLimitOpt; + ThrottleGlobalSpeedLimit = cfg.ThrottleGlobalSpeedLimit; ThrottleGlobalSpeedLimitPercent = cfg.ThrottleGlobalSpeedLimitPercent; ShiftInterruptDuration = cfg.ShiftInterruptDuration; ShiftInterruptCurrentThresholdPercent = cfg.ShiftInterruptCurrentThresholdPercent; diff --git a/src/tool/View/SystemView.xaml b/src/tool/View/SystemView.xaml index 09a52ee..7bc38d4 100644 --- a/src/tool/View/SystemView.xaml +++ b/src/tool/View/SystemView.xaml @@ -110,7 +110,7 @@ - + @@ -150,7 +150,7 @@ - + @@ -219,11 +219,25 @@ - - - - + + + + + Options for controlling external lights output. + + + Default - Controlled by display. + + Disabled - Lights output disabled. + + Always On - Lights output always on. + + Brake Light - Lights output enabled while braking. + + + + @@ -327,7 +341,7 @@ - + diff --git a/src/tool/ViewModel/ConfigurationViewModel.cs b/src/tool/ViewModel/ConfigurationViewModel.cs index 736be77..779f80e 100644 --- a/src/tool/ViewModel/ConfigurationViewModel.cs +++ b/src/tool/ViewModel/ConfigurationViewModel.cs @@ -57,12 +57,21 @@ public static List TemperatureSensorOptions new ValueItemViewModel(Configuration.WalkModeData.BatteryPercent, "Battery Level (%)") }; - public static List> ThrottleGlobalSpeedLimitOptions { get; } = - new List> + public static List> ThrottleGlobalSpeedLimitOptions { get; } = + new List> { - new ValueItemViewModel(Configuration.ThrottleGlobalSpeedLimit.Disabled, "Disabled"), - new ValueItemViewModel(Configuration.ThrottleGlobalSpeedLimit.Enabled, "Enabled"), - new ValueItemViewModel(Configuration.ThrottleGlobalSpeedLimit.StandardLevels, "Standard Levels"), + new ValueItemViewModel(Configuration.ThrottleGlobalSpeedLimitOptions.Disabled, "Disabled"), + new ValueItemViewModel(Configuration.ThrottleGlobalSpeedLimitOptions.Enabled, "Enabled"), + new ValueItemViewModel(Configuration.ThrottleGlobalSpeedLimitOptions.StandardLevels, "Standard Levels"), + }; + + public static List> LightsModeOptions { get; } = + new List> + { + new ValueItemViewModel(Configuration.LightsModeOptions.Default, "Default"), + new ValueItemViewModel(Configuration.LightsModeOptions.Disabled, "Disabled"), + new ValueItemViewModel(Configuration.LightsModeOptions.AlwaysOn, "Always On"), + new ValueItemViewModel(Configuration.LightsModeOptions.BrakeLight, "Brake Light"), }; @@ -257,15 +266,18 @@ public Configuration.TemperatureSensor UseTemperatureSensor } } - public bool LightsAlwaysOn + public ValueItemViewModel LightsMode { - get { return _config.LightsAlwaysOn; } + get + { + return LightsModeOptions.FirstOrDefault((e) => e.Value == _config.LightsMode); + } set { - if (_config.LightsAlwaysOn != value) + if (_config.LightsMode != value.Value) { - _config.LightsAlwaysOn = value; - OnPropertyChanged(nameof(LightsAlwaysOn)); + _config.LightsMode = value.Value; + OnPropertyChanged(nameof(LightsMode)); } } } @@ -309,18 +321,18 @@ public uint ThrottleStartCurrentPercent } } - public ValueItemViewModel ThrottleGlobalSpeedLimitOpt + public ValueItemViewModel ThrottleGlobalSpeedLimit { get { - return ThrottleGlobalSpeedLimitOptions.FirstOrDefault((e) => e.Value == _config.ThrottleGlobalSpeedLimitOpt); + return ThrottleGlobalSpeedLimitOptions.FirstOrDefault((e) => e.Value == _config.ThrottleGlobalSpeedLimit); } set { - if (_config.ThrottleGlobalSpeedLimitOpt != value.Value) + if (_config.ThrottleGlobalSpeedLimit != value.Value) { - _config.ThrottleGlobalSpeedLimitOpt = value.Value; - OnPropertyChanged(nameof(ThrottleGlobalSpeedLimitOpt)); + _config.ThrottleGlobalSpeedLimit = value.Value; + OnPropertyChanged(nameof(ThrottleGlobalSpeedLimit)); } } }