Skip to content

Commit

Permalink
Add konami for turning headlights on and off
Browse files Browse the repository at this point in the history
Feature: Add konami codes for turning LED headlights on and off
 >
 To turn headlights on: LEFT LEFT RIGHT
 >
 To turn headlights off: RIGHT LEFT LEFT
  • Loading branch information
lukash committed Oct 20, 2024
1 parent 223e5db commit 2d7edb9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/lcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ void lcm_init(LcmData *lcm, CfgHwLeds *hw_cfg) {
}

void lcm_configure(LcmData *lcm, const CfgLeds *cfg) {
if (!lcm->enabled) {
return;
}

if (!cfg->on) {
lcm->brightness = 0.0f;
lcm->brightness_idle = 0.0f;
Expand Down
45 changes: 42 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ static const FootpadSensorState flywheel_konami_sequence[] = {
FS_LEFT, FS_NONE, FS_RIGHT, FS_NONE, FS_LEFT, FS_NONE, FS_RIGHT
};

static const FootpadSensorState headlights_on_konami_sequence[] = {
FS_LEFT, FS_NONE, FS_LEFT, FS_NONE, FS_RIGHT
};

static const FootpadSensorState headlights_off_konami_sequence[] = {
FS_RIGHT, FS_NONE, FS_RIGHT, FS_NONE, FS_LEFT
};

// This is all persistent state of the application, which will be allocated in init. It
// is put here because variables can only be read-only when this program is loaded
// in flash without virtual memory in RAM (as all RAM already is dedicated to the
Expand Down Expand Up @@ -182,6 +190,8 @@ typedef struct {
float rc_current;

Konami flywheel_konami;
Konami headlights_on_konami;
Konami headlights_off_konami;
} data;

static void brake(data *d);
Expand Down Expand Up @@ -339,6 +349,16 @@ static void configure(data *d) {
d->beeper_enabled = d->float_conf.is_beeper_enabled;

konami_init(&d->flywheel_konami, flywheel_konami_sequence, sizeof(flywheel_konami_sequence));
konami_init(
&d->headlights_on_konami,
headlights_on_konami_sequence,
sizeof(headlights_on_konami_sequence)
);
konami_init(
&d->headlights_off_konami,
headlights_off_konami_sequence,
sizeof(headlights_off_konami_sequence)
);

reconfigure(d);

Expand All @@ -349,6 +369,11 @@ static void configure(data *d) {
}
}

static void leds_headlights_switch(CfgLeds *cfg_leds, LcmData *lcm, bool headlights_on) {
cfg_leds->headlights_on = headlights_on;
lcm_configure(lcm, cfg_leds);
}

static void reset_vars(data *d) {
motor_data_reset(&d->motor);
atr_reset(&d->atr);
Expand Down Expand Up @@ -1377,6 +1402,22 @@ static void refloat_thd(void *arg) {
}
}

if (d->float_conf.hardware.leds.type != LED_TYPE_NONE) {
if (!d->leds.cfg->headlights_on &&
konami_check(
&d->headlights_on_konami, &d->leds, &d->footpad_sensor, d->current_time
)) {
leds_headlights_switch(&d->float_conf.leds, &d->lcm, true);
}

if (d->leds.cfg->headlights_on &&
konami_check(
&d->headlights_off_konami, &d->leds, &d->footpad_sensor, d->current_time
)) {
leds_headlights_switch(&d->float_conf.leds, &d->lcm, false);
}
}

if (d->current_time - d->disengage_timer > 10) {
// 10 seconds of grace period between flipping the board over and allowing darkride
// mode
Expand Down Expand Up @@ -2337,9 +2378,7 @@ static void lights_control_request(CfgLeds *leds, uint8_t *buffer, size_t len, L
leds->headlights_on = value & 0x2;
}

if (lcm->enabled) {
lcm_configure(lcm, leds);
}
lcm_configure(lcm, leds);
}
}

Expand Down

0 comments on commit 2d7edb9

Please sign in to comment.