block CoffeeMachine
parameter Real T = 1; // clock
InputInteger Customer2Machine;
OutputInteger Machine2Customer;
/*
0: nop
1: enough coins inserted
2: drink dispensed
3: done
*/
Integer state;
/*
0: waiting for coins
1: waiting for selection
2: dispensing
3: refund/change
*/
algorithm
when initial() then
state := 0;
Machine2Customer := 0;
elsewhen sample(0, T) then
if (pre(state) == 0) and (Customer2Machine == 1)
then // customer has inserted enough coins
state := 1;
Machine2Customer := 1;
elseif (pre(state) == 1) and (Customer2Machine == 2) // drink selected
then // drink selected
state := 2; // dispensing drink
Machine2Customer := 0;
elseif (pre(state) == 1) and (Customer2Machine == 3) // cancel transaction
then // refund
state := 3; // refund/change
Machine2Customer := 0;
elseif (pre(state) == 2) // drink dispensed
then // drink dispensed
state := 0;
Machine2Customer := 2;
elseif (pre(state) == 3) // refund/change
then // refund
state := 0;
Machine2Customer := 3; // done
else state := pre(state);
Machine2Customer := pre(Machine2Customer);
end if;
end when;
end CoffeeMachine;