diff --git a/sapin_noel.bin b/sapin_noel.bin index 8d42b46..89b53b7 100755 Binary files a/sapin_noel.bin and b/sapin_noel.bin differ diff --git a/sapin_noel.ml b/sapin_noel.ml index 951f1ca..8d3f578 100644 --- a/sapin_noel.ml +++ b/sapin_noel.ml @@ -1,54 +1,56 @@ open WiringPiOcaml;; 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 _ = (* On choisit le mode d'affichage Phys *) ignore(setupPhys ()); (* reg : pin_value = p_v, pin_shift = p_s, pin_apply = p_a *) (* On crée le register *) + (* En mode pulse : let reg = genReg 11 13 15 ~pulse:true in *) let reg = genReg 11 13 15 in (* On initialise *) let leds = initReg reg ~nb_reg:1 in - (* On boucle pour afficher les leds unes par unes *) - 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; + Printf.printf "Début\n%!"; (* And a pretty animation *) - for i = 0 to 6 do - clearLeds 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; - -;; + anim_01 reg leds max_int; + (* findLedNumber reg leds *) diff --git a/shiftReg.ml b/shiftReg.ml index 8316254..761088d 100644 --- a/shiftReg.ml +++ b/shiftReg.ml @@ -8,12 +8,12 @@ open WiringPiOcaml (** reg : (pin_value = p_v, pin_shift = p_s, pin_apply = p_a). It is used to contain the informations about connections. The invert variable is used in order to revert the mode (false = lighted, - true = not lighted) **) -type reg = {p_v : int; p_s : int; p_a : int; invert : bool;} + true = not lighted). The pulse function is only used with applyReg **) +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 = - {p_v = pin_value; p_s = pin_shift; p_a = pin_apply; invert} +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; pulse; invert} 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; 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 = write reg.p_s false; write reg.p_v (value <> reg.invert); (* On inverse si besoin *) @@ -36,15 +36,16 @@ let shift reg value = let validate reg = write reg.p_a true; write reg.p_a false - + (** 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; for i = (Array.length leds) - 1 downto 0 do shift reg leds.(i) done; 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 t = Unix.gettimeofday () 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 (* On valide en attendant un petit coup *) validate reg; - delayMicroseconds d_t; + (* delayMicroseconds d_t; *) end; shift reg false; done; @@ -69,6 +70,16 @@ let applyRegPulse reg leds ?d_t:(d_t = 3000) time = shift reg true; 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 **) let clearLeds 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 Printf.printf "%b;" t.(k) done; - Printf.printf "\n%!\n" + Printf.printf "\n%!" (** 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 j = ref n in while !i < (!j - 1) do - Printf.printf "%d;%d" !i !j; let middle = !i + (!j - !i)/2 in makeIntervalArray leds !i middle; - applyReg reg leds; + applyRegAll reg leds; Printf.printf "\nLighted ? (1 = Yes, other = no) %!"; let res = input_line stdin in if res = "1" then j := middle else i := middle; - Printf.printf "%d;%d" !i !j; done; if time_answer > 0 then begin Printf.printf "\nI think it's this LED : %d.\n%!" !i; clearLeds leds; leds.(!i) <- true; - applyReg reg leds; + applyRegAll reg leds; Unix.sleep time_answer end