The pulse mode is written in reg now.

This commit is contained in:
Tobias Bora 2013-12-08 00:18:56 +01:00
parent 6a7384ae0f
commit 076fc58545
3 changed files with 65 additions and 54 deletions

Binary file not shown.

View File

@ -1,54 +1,56 @@
open WiringPiOcaml;; open WiringPiOcaml;;
open ShiftReg;; open ShiftReg;;
(* Ici on utilise des fonctions plus souples mais moins agréables pour les tests. Pour voir les fonctions classiques, cf anim_01 *)
let test reg leds =
lightLeds leds;
(* Pour montrer la différence entre les deux modes *)
Printf.printf "The pulse mode...\n%!";
applyRegPulse reg leds 2.;
Printf.printf "The classic mode...\n%!";
applyRegAll reg leds;
Unix.sleep 2;
Printf.printf "Only one led :\n%!";
clearLeds leds;
leds.(1) <- true;
Printf.printf "The pulse mode...\n%!";
applyRegPulse reg leds 2.;
Printf.printf "The classic mode...\n%!";
applyRegAll reg leds;
Unix.sleep 2
(* S'inspirer de ce schéma pour les animations *)
let anim_01 reg leds n =
for i = 0 to n do
clearLeds leds;
for k = 0 to (Array.length leds) - 1 do
leds.(k) <- true;
applyReg reg leds 0.1;
leds.(k) <- false
done;
for k = (Array.length leds) - 2 downto 0 do
leds.(k) <- true;
applyReg reg leds 0.1;
leds.(k) <- false
done;
lightLeds leds;
applyReg reg leds 1.;
done
let _ = let _ =
(* On choisit le mode d'affichage Phys *) (* On choisit le mode d'affichage Phys *)
ignore(setupPhys ()); ignore(setupPhys ());
(* reg : pin_value = p_v, pin_shift = p_s, pin_apply = p_a *) (* reg : pin_value = p_v, pin_shift = p_s, pin_apply = p_a *)
(* On crée le register *) (* On crée le register *)
(* En mode pulse : let reg = genReg 11 13 15 ~pulse:true in *)
let reg = genReg 11 13 15 in let reg = genReg 11 13 15 in
(* On initialise *) (* On initialise *)
let leds = initReg reg ~nb_reg:1 in let leds = initReg reg ~nb_reg:1 in
(* On boucle pour afficher les leds unes par unes *)
Printf.printf "début\n%!"; Printf.printf "Début\n%!";
(* On remplit le tableau avec true *)
lightLeds leds;
(* Pour montrer la différence entre les deux modes *)
(* The pulse mode... *)
applyRegPulse reg leds 2.;
(* The classic mode... *)
applyReg reg leds;
Unix.sleep 2;
(* Only one led : *)
clearLeds leds;
leds.(1) <- true;
(* Pulse *)
applyRegPulse reg leds 2.;
(* classic *)
applyReg reg leds;
Unix.sleep 2;
(* And a pretty animation *) (* And a pretty animation *)
for i = 0 to 6 do anim_01 reg leds max_int;
clearLeds leds; (* findLedNumber reg leds *)
for k = 0 to (Array.length leds) - 1 do
leds.(k) <- true;
applyReg reg leds;
delay 100;
leds.(k) <- false
done;
for k = (Array.length leds) - 2 downto 0 do
leds.(k) <- true;
applyRegPulse reg leds 0.1;
(* applyReg reg leds; *)
(* delay 100; *)
leds.(k) <- false
done;
lightLeds leds;
(* applyRegPulse reg leds 1.; *)
applyReg reg leds;
Unix.sleep 1;
done;
findLedNumber reg leds;
;;

View File

@ -8,12 +8,12 @@ open WiringPiOcaml
(** reg : (pin_value = p_v, pin_shift = p_s, pin_apply = p_a). It is (** reg : (pin_value = p_v, pin_shift = p_s, pin_apply = p_a). It is
used to contain the informations about connections. used to contain the informations about connections.
The invert variable is used in order to revert the mode (false = lighted, The invert variable is used in order to revert the mode (false = lighted,
true = not lighted) **) true = not lighted). The pulse function is only used with applyReg **)
type reg = {p_v : int; p_s : int; p_a : int; invert : bool;} type reg = {p_v : int; p_s : int; p_a : int; pulse : bool; invert : bool;}
let genReg ?invert:(invert = false) pin_value pin_shift pin_apply = let genReg ?pulse:(pulse = false) ?invert:(invert = false) pin_value pin_shift pin_apply =
{p_v = pin_value; p_s = pin_shift; p_a = pin_apply; invert} {p_v = pin_value; p_s = pin_shift; p_a = pin_apply; pulse; invert}
let write pin value = digitalWrite pin (if value then 1 else 0) let write pin value = digitalWrite pin (if value then 1 else 0)
@ -28,7 +28,7 @@ let initReg ?nb_reg:(nb_reg = 1) reg =
write reg.p_a false; write reg.p_a false;
Array.make (8*nb_reg) false (* return back an array for all pieces *) Array.make (8*nb_reg) false (* return back an array for all pieces *)
(* Functions related to basic action of the register *) (** Functions related to basic action of the register **)
let shift reg value = let shift reg value =
write reg.p_s false; write reg.p_s false;
write reg.p_v (value <> reg.invert); (* On inverse si besoin *) write reg.p_v (value <> reg.invert); (* On inverse si besoin *)
@ -36,15 +36,16 @@ let shift reg value =
let validate reg = let validate reg =
write reg.p_a true; write reg.p_a true;
write reg.p_a false write reg.p_a false
(** This function apply all modifications to the register in the same time **) (** This function apply all modifications to the register in the same time **)
let applyReg reg leds = let applyRegAll reg leds =
write reg.p_a false; write reg.p_a false;
for i = (Array.length leds) - 1 downto 0 do for i = (Array.length leds) - 1 downto 0 do
shift reg leds.(i) shift reg leds.(i)
done; done;
validate reg validate reg
(** This function opens and closes very quickly each LED, one after the other**)
let applyRegPulse reg leds ?d_t:(d_t = 3000) time = let applyRegPulse reg leds ?d_t:(d_t = 3000) time =
let t = Unix.gettimeofday () in let t = Unix.gettimeofday () in
let first_time = ref true in let first_time = ref true in
@ -61,7 +62,7 @@ let applyRegPulse reg leds ?d_t:(d_t = 3000) time =
if leds.(i) then begin if leds.(i) then begin
(* On valide en attendant un petit coup *) (* On valide en attendant un petit coup *)
validate reg; validate reg;
delayMicroseconds d_t; (* delayMicroseconds d_t; *)
end; end;
shift reg false; shift reg false;
done; done;
@ -69,6 +70,16 @@ let applyRegPulse reg leds ?d_t:(d_t = 3000) time =
shift reg true; shift reg true;
done done
(** Generic function which choose the good mode (Pulse or not) and wait.
(time in seconds, float) **)
let applyReg reg leds time =
if reg.pulse then
applyRegPulse reg leds time
else begin
applyRegAll reg leds;
delay (int_of_float (time *. 1000.))
end
(** Don't forget to apply it with applyReg after **) (** Don't forget to apply it with applyReg after **)
let clearLeds leds = let clearLeds leds =
Array.iteri (fun i x -> (leds.(i) <- false)) leds Array.iteri (fun i x -> (leds.(i) <- false)) leds
@ -79,7 +90,7 @@ let printBoolArray t =
for k = 0 to Array.length t - 1 do for k = 0 to Array.length t - 1 do
Printf.printf "%b;" t.(k) Printf.printf "%b;" t.(k)
done; done;
Printf.printf "\n%!\n" Printf.printf "\n%!"
(** This function is usefull to find a LED in a logarithm time **) (** This function is usefull to find a LED in a logarithm time **)
@ -95,23 +106,21 @@ let findLedNumber reg ?time_answer:(time_answer = 3) leds0 =
let i = ref 0 in let i = ref 0 in
let j = ref n in let j = ref n in
while !i < (!j - 1) do while !i < (!j - 1) do
Printf.printf "%d;%d" !i !j;
let middle = !i + (!j - !i)/2 in let middle = !i + (!j - !i)/2 in
makeIntervalArray leds !i middle; makeIntervalArray leds !i middle;
applyReg reg leds; applyRegAll reg leds;
Printf.printf "\nLighted ? (1 = Yes, other = no) %!"; Printf.printf "\nLighted ? (1 = Yes, other = no) %!";
let res = input_line stdin in let res = input_line stdin in
if res = "1" then if res = "1" then
j := middle j := middle
else else
i := middle; i := middle;
Printf.printf "%d;%d" !i !j;
done; done;
if time_answer > 0 then begin if time_answer > 0 then begin
Printf.printf "\nI think it's this LED : %d.\n%!" !i; Printf.printf "\nI think it's this LED : %d.\n%!" !i;
clearLeds leds; clearLeds leds;
leds.(!i) <- true; leds.(!i) <- true;
applyReg reg leds; applyRegAll reg leds;
Unix.sleep time_answer Unix.sleep time_answer
end end