Skip to content

Commit

Permalink
feat(ale): move control sensing logic to a common handler, incorporat…
Browse files Browse the repository at this point in the history
…e horn and bell usage
  • Loading branch information
YoRyan committed Jul 24, 2024
1 parent 88a642d commit 5b792b3
Show file tree
Hide file tree
Showing 21 changed files with 64 additions and 141 deletions.
20 changes: 19 additions & 1 deletion src/lib/alerter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,29 @@ export function create({
}: {
e: FrpEngine;
acknowledge: frp.Behavior<boolean>;
acknowledgeStream: frp.Stream<any>;
acknowledgeStream?: frp.Stream<any>;
cutIn: frp.Behavior<boolean>;
countdownS?: number;
penaltyS?: number;
}): frp.Stream<AlerterState> {
acknowledgeStream ??= frp.compose(
e.createOnCvChangeStream(),
frp.filter(([name]) => {
switch (name) {
case "ThrottleAndBrake":
case "VirtualThrottle":
case "TrainBrakeControl":
case "VirtualBrake":
case "Horn":
case "VirtualHorn":
case "Bell":
case "VirtualBell":
return true;
default:
return false;
}
})
);
countdownS ??= defaultCountdownS;
penaltyS ??= defaultPenaltyS;

Expand Down
9 changes: 1 addition & 8 deletions src/lib/shared/multilevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,7 @@ export function onInit(me: FrpEngine, version: Version) {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "ThrottleAndBrake" || name === "VirtualBrake")
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
undefined
);
const alerterState = frp.stepper(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), undefined);
// Safety system sounds
const upgradeSound$ = frp.compose(
me.createPlayerWithKeyUpdateStream(),
Expand Down
9 changes: 1 addition & 8 deletions src/lib/shared/p32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,7 @@ export function onInit(me: FrpEngine, isAmtrak: boolean) {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "VirtualThrottle" || name === "TrainBrakeControl")
);
const alerter$ = frp.compose(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
frp.hub()
);
const alerter$ = frp.compose(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), frp.hub());
const alerterState = frp.stepper(alerter$, undefined);
alerter$(({ alarm }) => {
me.rv.SetControlValue("AlerterVisual", alarm ? 1 : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "VirtualThrottle" || name === "VirtualBrake")
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
undefined
);
const alerterState = frp.stepper(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), undefined);
// Safety system sounds
const upgradeSound$ = frp.compose(
me.createPlayerWithKeyUpdateStream(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "VirtualThrottle" || name === "VirtualBrake")
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
undefined
);
const alerterState = frp.stepper(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), undefined);
// Safety system sounds
const upgradeSound$ = frp.compose(
me.createPlayerWithKeyUpdateStream(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "VirtualThrottle" || name === "VirtualBrake")
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
undefined
);
const alerterState = frp.stepper(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), undefined);
// Safety system sounds
const upgradeSound$ = frp.compose(
me.createPlayerWithKeyUpdateStream(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = me.createOnCvChangeStreamFor("ThrottleAndBrake");
const alerter$ = frp.compose(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
frp.hub()
);
const alerter$ = frp.compose(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), frp.hub());
const alerterState = frp.stepper(alerter$, undefined);
// Safety system sounds
// Unfortunately, we cannot display the AWS symbol without also playing the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "VirtualThrottle" || name === "VirtualBrake")
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
undefined
);
const alerterState = frp.stepper(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), undefined);
// Safety system sounds
const upgradeSound$ = frp.compose(
me.createPlayerWithKeyUpdateStream(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "ThrottleAndBrake" || name === "VirtualBrake")
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
undefined
);
const alerterState = frp.stepper(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), undefined);
// Safety system sounds
const upgradeSound$ = frp.compose(
me.createPlayerWithKeyUpdateStream(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "ThrottleAndBrake" || name === "VirtualBrake")
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
undefined
);
const alerterState = frp.stepper(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), undefined);
// Safety system sounds
const upgradeSound$ = frp.compose(
me.createPlayerWithKeyUpdateStream(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "ThrottleAndBrake" || name === "VirtualBrake")
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
undefined
);
const alerterState = frp.stepper(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), undefined);
// Safety system sounds
const upgradeSound$ = frp.compose(
me.createPlayerWithKeyUpdateStream(),
Expand Down Expand Up @@ -229,6 +222,9 @@ const me = new FrpEngine(() => {
me.rv.SetControlValue("AWS", aws ? 1 : 0);
});

// Set platform door height.
fx.setLowPlatformDoorsForEngine(me, false);

// Manual door control
njt.createManualDoorsPopup(me);
const passengerDoors = njt.createManualDoorsBehavior(me);
Expand Down Expand Up @@ -631,9 +627,6 @@ const me = new FrpEngine(() => {
me.rv.ActivateNode("LightsRed", on);
});

// Set platform door height.
fx.setLowPlatformDoorsForEngine(me, false);

// Destination signs
njt.createDestinationSignSelector(me);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "ThrottleAndBrake" || name === "VirtualBrake")
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
undefined
);
const alerterState = frp.stepper(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), undefined);
// Safety system sounds
const upgradeEvents$ = frp.compose(
aduEvents$,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "ThrottleAndBrake" || name === "TrainBrakeControl")
);
const alerter$ = frp.compose(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
frp.hub()
);
const alerter$ = frp.compose(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), frp.hub());
const alerterState = frp.stepper(alerter$, undefined);
alerter$(({ alarm }) => {
me.rv.SetControlValue("AlerterVisual", alarm ? 1 : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,19 @@ const me = new FrpEngine(() => {
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(
([name]) =>
name === (isFanRailer ? "NewVirtualThrottle" : "VirtualThrottle") || name === "TrainBrakeControl"
)
frp.filter(([name, value]) => {
switch (name) {
case isFanRailer ? "NewVirtualThrottle" : "VirtualThrottle":
case "TrainBrakeControl":
case "Horn":
case "Bell":
return true;
case "Headlights":
return value > 0.5 && value < 1.5;
default:
return false;
}
})
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,19 @@ const me = new FrpEngine(() => {
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "VirtualThrottle" || name === "VirtualBrake")
frp.filter(([name, value]) => {
switch (name) {
case "VirtualThrottle":
case "TrainBrakeControl":
case "Horn":
case "Bell":
return true;
case "Headlights":
return value > 0.5 && value < 1.5;
default:
return false;
}
})
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = me.createOnCvChangeStreamFor("ThrottleAndBrake");
const alerter$ = frp.compose(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
frp.hub()
);
const alerter$ = frp.compose(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), frp.hub());
const alerterState = frp.stepper(alerter$, undefined);
// Safety system sounds
// Unfortunately, we cannot display the AWS symbol without also playing the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "ThrottleAndBrake" || name === "VirtualBrake")
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
undefined
);
const alerterState = frp.stepper(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), undefined);
// Safety system sounds
const isAlarm = frp.liftN(
(aduState, alerterState) => (aduState?.atcAlarm || aduState?.acsesAlarm || alerterState?.alarm) ?? false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "VirtualThrottle" || name === "VirtualBrake")
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
undefined
);
const alerterState = frp.stepper(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), undefined);
// Safety system sounds
const upgradeEvents$ = frp.compose(
aduEvents$,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,7 @@ const me = new FrpEngine(() => {
});
const aduState = frp.stepper(aduStateHub$, undefined);
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "VirtualThrottle" || name === "TrainBrakeControl")
);
const alerterState = frp.stepper(
ale.create({ e: me, acknowledge, acknowledgeStream: alerterReset$, cutIn: alerterCutIn }),
undefined
);
const alerterState = frp.stepper(ale.create({ e: me, acknowledge, cutIn: alerterCutIn }), undefined);
// Safety system sounds
const upgradeEvents$ = frp.compose(
aduEvents$,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,10 @@ const me = new FrpEngine(() => {
}
});
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "VirtualThrottle" || name === "VirtualBrake")
);
const alerterState = frp.stepper(
ale.create({
e: me,
acknowledge,
acknowledgeStream: alerterReset$,
cutIn: frp.liftN((cutIn, powered) => cutIn && powered, alerterCutIn, isPoweredOn),
}),
undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,10 @@ const me = new FrpEngine(() => {
}
});
// Alerter
const alerterReset$ = frp.compose(
me.createOnCvChangeStream(),
frp.filter(([name]) => name === "VirtualThrottle" || name === "VirtualBrake")
);
const alerterState = frp.stepper(
ale.create({
e: me,
acknowledge,
acknowledgeStream: alerterReset$,
cutIn: frp.liftN((cutIn, powered) => cutIn && powered, alerterCutIn, isPoweredOn),
}),
undefined
Expand Down

0 comments on commit 5b792b3

Please sign in to comment.