Skip to content

Commit

Permalink
Light Modes #160
Browse files Browse the repository at this point in the history
* Add more options for controllig external light output
  • Loading branch information
danielnilsson9 committed Aug 8, 2023
1 parent 61148f7 commit da80b65
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 53 deletions.
25 changes: 13 additions & 12 deletions src/firmware/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/firmware/cfgstore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion src/firmware/cfgstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down
42 changes: 25 additions & 17 deletions src/tool/Model/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public enum AssistFlagsType : byte
SpeedOverride = 0x40
};

public enum ThrottleGlobalSpeedLimit
public enum ThrottleGlobalSpeedLimitOptions
{
Disabled = 0x00,
Enabled = 0x01,
Expand All @@ -101,6 +101,14 @@ public enum WalkModeData
BatteryPercent = 3
}

public enum LightsModeOptions
{
Default = 0,
Disabled = 1,
AlwaysOn = 2,
BrakeLight = 3
}


public class AssistLevel
{
Expand Down Expand Up @@ -163,7 +171,7 @@ public uint MaxCurrentLimitAmps
public TemperatureSensor UseTemperatureSensor;

// lights
public bool LightsAlwaysOn;
public LightsModeOptions LightsMode;

// speed sensor
public float WheelSizeInch;
Expand All @@ -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
Expand Down Expand Up @@ -215,7 +223,7 @@ public Configuration(BbsfwConnection.Controller target)
UsePushWalk = false;
UseTemperatureSensor = TemperatureSensor.All;

LightsAlwaysOn = false;
LightsMode = LightsModeOptions.Default;

WheelSizeInch = 0;
NumWheelSensorSignals = 0;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
28 changes: 21 additions & 7 deletions src/tool/View/SystemView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
</TextBlock>
</TextBlock.ToolTip>
</TextBlock>
<ComboBox Grid.Column="2" Grid.Row="4" Margin="0 8 0 0" Width="120" Height="20" HorizontalAlignment="Right" ItemsSource="{Binding ConfigVm.ThrottleGlobalSpeedLimitOptions}" SelectedItem="{Binding ConfigVm.ThrottleGlobalSpeedLimitOpt, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox Grid.Column="2" Grid.Row="4" Margin="0 8 0 0" Padding="6 2 6 0" Width="120" Height="20" HorizontalAlignment="Right" ItemsSource="{Binding ConfigVm.ThrottleGlobalSpeedLimitOptions}" SelectedItem="{Binding ConfigVm.ThrottleGlobalSpeedLimit, UpdateSourceTrigger=PropertyChanged}" />

<TextBlock Grid.Column="0" Grid.Row="5" Margin="0 8 0 0" Text="Global Speed Limit (%):">
<TextBlock.ToolTip>
Expand Down Expand Up @@ -150,7 +150,7 @@
<TextBlock Grid.Row="0" Text="Pedal Assist" FontSize="18" FontWeight="Bold" />

<TextBlock Grid.Column="0" Grid.Row="1" Margin="0 10 0 0" Text="Start Delay (°):" />
<ComboBox Grid.Column="2" Grid.Row="1" Margin="0 10 0 0" Width="60" Height="20" HorizontalAlignment="Right" ItemsSource="{Binding ConfigVm.PasStartDelayOptions, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding ConfigVm.PasStartDelayDegrees}" />
<ComboBox Grid.Column="2" Grid.Row="1" Margin="0 10 0 0" Padding="6 2 6 0" Width="60" Height="20" HorizontalAlignment="Right" ItemsSource="{Binding ConfigVm.PasStartDelayOptions, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding ConfigVm.PasStartDelayDegrees}" />

<TextBlock Grid.Column="0" Grid.Row="2" Margin="0 8 0 0" Text="Stop Delay (ms):" />
<TextBox Grid.Column="2" Grid.Row="2" Margin="0 8 0 0" Width="60" HorizontalAlignment="Right" Text="{Binding ConfigVm.PasStopDelayMilliseconds, UpdateSourceTrigger=PropertyChanged}" />
Expand Down Expand Up @@ -219,11 +219,25 @@
</TextBlock>
</TextBlock.ToolTip>
</TextBlock>
<ComboBox Grid.Column="2" Grid.Row="4" Margin="0 8 0 0" Width="80" Height="20" HorizontalAlignment="Right" ItemsSource="{Binding ConfigVm.TemperatureSensorOptions}" SelectedItem="{Binding ConfigVm.UseTemperatureSensor, UpdateSourceTrigger=PropertyChanged}" />

<TextBlock Grid.Column="0" Grid.Row="5" Margin="0 8 0 0" Text="Lights Always On:" />
<CheckBox Grid.Column="2" Grid.Row="5" Margin="0 8 0 0" HorizontalAlignment="Right" IsChecked="{Binding ConfigVm.LightsAlwaysOn, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox Grid.Column="2" Grid.Row="4" Margin="0 8 0 0" Padding="6 2 6 0" Width="80" Height="20" HorizontalAlignment="Right" ItemsSource="{Binding ConfigVm.TemperatureSensorOptions}" SelectedItem="{Binding ConfigVm.UseTemperatureSensor, UpdateSourceTrigger=PropertyChanged}" />

<TextBlock Grid.Column="0" Grid.Row="5" Margin="0 8 0 0" Text="Lights Mode:">
<TextBlock.ToolTip>
<TextBlock Width="400" TextWrapping="Wrap">
Options for controlling external lights output.
<LineBreak />
<LineBreak />
<Run FontWeight="Bold">Default</Run> - Controlled by display.
<LineBreak />
<Run FontWeight="Bold">Disabled</Run> - Lights output disabled.
<LineBreak />
<Run FontWeight="Bold">Always On</Run> - Lights output always on.
<LineBreak />
<Run FontWeight="Bold">Brake Light</Run> - Lights output enabled while braking.
</TextBlock>
</TextBlock.ToolTip>
</TextBlock>
<ComboBox Grid.Column="2" Grid.Row="5" Margin="0 8 0 0" Padding="6 2 6 0" Width="100" Height="20" HorizontalAlignment="Right" ItemsSource="{Binding ConfigVm.LightsModeOptions}" SelectedItem="{Binding ConfigVm.LightsMode, UpdateSourceTrigger=PropertyChanged}" />
</Grid>

<Grid Margin="0 20 0 0">
Expand Down Expand Up @@ -327,7 +341,7 @@
</TextBlock>
</TextBlock.ToolTip>
</TextBlock>
<ComboBox Grid.Column="2" Grid.Row="1" Margin="0 8 0 0" Width="120" Height="20" HorizontalAlignment="Right" ItemsSource="{Binding ConfigVm.WalkModeDataDisplayOptions}" SelectedItem="{Binding ConfigVm.WalkModeDataDisplay, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox Grid.Column="2" Grid.Row="1" Margin="0 8 0 0" Padding="6 2 6 0" Width="120" Height="20" HorizontalAlignment="Right" ItemsSource="{Binding ConfigVm.WalkModeDataDisplayOptions}" SelectedItem="{Binding ConfigVm.WalkModeDataDisplay, UpdateSourceTrigger=PropertyChanged}" />

</Grid>

Expand Down
42 changes: 27 additions & 15 deletions src/tool/ViewModel/ConfigurationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,21 @@ public static List<Configuration.TemperatureSensor> TemperatureSensorOptions
new ValueItemViewModel<Configuration.WalkModeData>(Configuration.WalkModeData.BatteryPercent, "Battery Level (%)")
};

public static List<ValueItemViewModel<Configuration.ThrottleGlobalSpeedLimit>> ThrottleGlobalSpeedLimitOptions { get; } =
new List<ValueItemViewModel<Configuration.ThrottleGlobalSpeedLimit>>
public static List<ValueItemViewModel<Configuration.ThrottleGlobalSpeedLimitOptions>> ThrottleGlobalSpeedLimitOptions { get; } =
new List<ValueItemViewModel<Configuration.ThrottleGlobalSpeedLimitOptions>>
{
new ValueItemViewModel<Configuration.ThrottleGlobalSpeedLimit>(Configuration.ThrottleGlobalSpeedLimit.Disabled, "Disabled"),
new ValueItemViewModel<Configuration.ThrottleGlobalSpeedLimit>(Configuration.ThrottleGlobalSpeedLimit.Enabled, "Enabled"),
new ValueItemViewModel<Configuration.ThrottleGlobalSpeedLimit>(Configuration.ThrottleGlobalSpeedLimit.StandardLevels, "Standard Levels"),
new ValueItemViewModel<Configuration.ThrottleGlobalSpeedLimitOptions>(Configuration.ThrottleGlobalSpeedLimitOptions.Disabled, "Disabled"),
new ValueItemViewModel<Configuration.ThrottleGlobalSpeedLimitOptions>(Configuration.ThrottleGlobalSpeedLimitOptions.Enabled, "Enabled"),
new ValueItemViewModel<Configuration.ThrottleGlobalSpeedLimitOptions>(Configuration.ThrottleGlobalSpeedLimitOptions.StandardLevels, "Standard Levels"),
};

public static List<ValueItemViewModel<Configuration.LightsModeOptions>> LightsModeOptions { get; } =
new List<ValueItemViewModel<Configuration.LightsModeOptions>>
{
new ValueItemViewModel<Configuration.LightsModeOptions>(Configuration.LightsModeOptions.Default, "Default"),
new ValueItemViewModel<Configuration.LightsModeOptions>(Configuration.LightsModeOptions.Disabled, "Disabled"),
new ValueItemViewModel<Configuration.LightsModeOptions>(Configuration.LightsModeOptions.AlwaysOn, "Always On"),
new ValueItemViewModel<Configuration.LightsModeOptions>(Configuration.LightsModeOptions.BrakeLight, "Brake Light"),
};


Expand Down Expand Up @@ -257,15 +266,18 @@ public Configuration.TemperatureSensor UseTemperatureSensor
}
}

public bool LightsAlwaysOn
public ValueItemViewModel<Configuration.LightsModeOptions> 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));
}
}
}
Expand Down Expand Up @@ -309,18 +321,18 @@ public uint ThrottleStartCurrentPercent
}
}

public ValueItemViewModel<Configuration.ThrottleGlobalSpeedLimit> ThrottleGlobalSpeedLimitOpt
public ValueItemViewModel<Configuration.ThrottleGlobalSpeedLimitOptions> 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));
}
}
}
Expand Down

0 comments on commit da80b65

Please sign in to comment.