ex3.erl

text/x-erlang ex3.erl — 1.2 KB

Continguts del fitxer

-module(ex3).
-export([botonera/0,bucle/2,proxy/3,bucle_proxy/2]).

botonera()->
    B = proxy(8,g,self()),%Big
    L = proxy(2,p,self()),%Little
    bucle(B,L).

bucle(B,L)->
    receive
		{p,clicked,Pis}->
			if 
				(Pis == 0) -> encen(B,0);
				(Pis == 1) -> encen(B,1)
			end,
			bucle(B,L);
		{g,clicked,Pis}->
			if
				(Pis rem 2 =:= 0) -> apaga(B,0);
				(Pis rem 2 =:= 1) -> apaga(B,1)
			end,
			bucle(B, L);
		{g, abort} ->
			L!close,
			receive	_ -> "ok82"	end;
		{p, abort} ->
			B!close,
				receive	_ -> "ok28"	end

	end.



encen(B, Pis) ->
    if
		Pis < 6 -> %Mentres no has acabat ves incrementant Pis
			B!{light_on,Pis},
			encen(B, Pis+2);
		Pis >= 6 -> % Si es mes gran que 7, per tant ja és l'ultim, 8, has acabat, torna al bucle
			B!{light_on,Pis}
    end.

apaga(B, Pis)->
	if
		Pis < 6 ->
			B!{light_off,Pis},
			apaga(B, Pis+2);
		Pis >= 6 ->
			B!{light_off,Pis}
    end.		



proxy(N,Nom,Pid)->%N es la mida total
    B = spawn(ex3,bucle_proxy,[Nom,Pid]),
    bots:nou(N,B).

bucle_proxy(Nom,Pid)->
    receive
		{clicked,Pis} ->
			Pid!{Nom,clicked,Pis},
			bucle_proxy(Nom,Pid);
		abort ->
			Pid!{Nom,abort},
			% io:format("Proxi de la botonera ~p tancada ~n", [Nom]),
			ok
    end.