diff --git a/sapin_noel.bin b/sapin_noel.bin index 3da8cfb..8d42b46 100755 Binary files a/sapin_noel.bin and b/sapin_noel.bin differ diff --git a/sapin_noel.ml b/sapin_noel.ml index dcaeace..951f1ca 100644 --- a/sapin_noel.ml +++ b/sapin_noel.ml @@ -3,18 +3,34 @@ open ShiftReg;; let _ = (* On choisit le mode d'affichage Phys *) - setupPhys (); + ignore(setupPhys ()); (* reg : pin_value = p_v, pin_shift = p_s, pin_apply = p_a *) (* On crée le register *) - let reg = genReg 11 13 15 ~invert: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%!"; - findLedNumber reg leds; - while true do - + (* 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 *) + for i = 0 to 6 do + clearLeds leds; for k = 0 to (Array.length leds) - 1 do leds.(k) <- true; applyReg reg leds; @@ -23,14 +39,16 @@ let _ = done; for k = (Array.length leds) - 2 downto 0 do leds.(k) <- true; - applyReg reg leds; - delay 100; + 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; - clearLeds leds; - done + done; + findLedNumber reg leds; ;; diff --git a/shiftReg.ml b/shiftReg.ml index 2f53c1a..8316254 100644 --- a/shiftReg.ml +++ b/shiftReg.ml @@ -6,10 +6,16 @@ *) 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, + 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} +type reg = {p_v : int; p_s : int; p_a : int; 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 write pin value = digitalWrite pin (if value then 1 else 0) (** The first thing to do is setupPhys (). This function put in OUTPUT mode the outputs and return back a bool array which represent the output of registers (begining with the first LED of the first shift register) **) @@ -17,44 +23,73 @@ let initReg ?nb_reg:(nb_reg = 1) reg = pinMode reg.p_v 1; (* mode output *) pinMode reg.p_s 1; pinMode reg.p_a 1; + write reg.p_v false; + write reg.p_s false; + write reg.p_a false; Array.make (8*nb_reg) false (* return back an array for all pieces *) -let write pin value = digitalWrite pin (if value then 1 else 0) - -let genReg ?invert:(invert = false) pin_value pin_shift pin_apply = - {p_v = pin_value; p_s = pin_shift; p_a = pin_apply; invert = invert} - -(** This function apply all modifications to 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 *) + write reg.p_s true +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 = write reg.p_a false; for i = (Array.length leds) - 1 downto 0 do - write reg.p_s false; - write reg.p_v (leds.(i) <> reg.invert); (* On inverse si besoin *) - write reg.p_s true; + shift reg leds.(i) done; - write reg.p_a true - - + validate reg + +let applyRegPulse reg leds ?d_t:(d_t = 3000) time = + let t = Unix.gettimeofday () in + let first_time = ref true in + (* Clear the leds *) + write reg.p_a false; + for i = (Array.length leds) - 1 downto 0 do + shift reg false + done; + (* It create a one at the very beginning *) + shift reg true; + while !first_time || Unix.gettimeofday () -. t < time do + for i = 0 to (Array.length leds) - 1 do + (* We add a zero (we need only one true on the line *) + if leds.(i) then begin + (* On valide en attendant un petit coup *) + validate reg; + delayMicroseconds d_t; + end; + shift reg false; + done; + first_time := false; + shift reg true; + done + (** Don't forget to apply it with applyReg after **) let clearLeds leds = Array.iteri (fun i x -> (leds.(i) <- false)) leds let lightLeds leds = Array.iteri (fun i x -> (leds.(i) <- true)) leds -(** This function is usefull to find a LED in a logarithm time **) -let makeIntervalArray leds a b = - Printf.printf "%d;%d" a b; - Array.iteri - (fun i x -> leds.(i) <- ((a <= i) && (i < b)) ) - leds - -let printArray t = +let printBoolArray t = for k = 0 to Array.length t - 1 do Printf.printf "%b;" t.(k) done; Printf.printf "\n%!\n" + +(** This function is usefull to find a LED in a logarithm time **) let findLedNumber reg ?time_answer:(time_answer = 3) leds0 = + let makeIntervalArray leds a b = + Printf.printf "%d;%d" a b; + Array.iteri + (fun i x -> leds.(i) <- ((a <= i) && (i < b)) ) + leds + in let n = Array.length leds0 in let leds = Array.make n false in let i = ref 0 in @@ -64,7 +99,6 @@ let findLedNumber reg ?time_answer:(time_answer = 3) leds0 = let middle = !i + (!j - !i)/2 in makeIntervalArray leds !i middle; applyReg reg leds; - printArray leds; Printf.printf "\nLighted ? (1 = Yes, other = no) %!"; let res = input_line stdin in if res = "1" then @@ -77,7 +111,6 @@ let findLedNumber reg ?time_answer:(time_answer = 3) leds0 = Printf.printf "\nI think it's this LED : %d.\n%!" !i; clearLeds leds; leds.(!i) <- true; - printArray leds; applyReg reg leds; Unix.sleep time_answer end