Pulse mode created.
This commit is contained in:
parent
76be9195ee
commit
d65bed4fa6
BIN
sapin_noel.bin
BIN
sapin_noel.bin
Binary file not shown.
|
@ -3,18 +3,34 @@ open ShiftReg;;
|
||||||
|
|
||||||
let _ =
|
let _ =
|
||||||
(* On choisit le mode d'affichage Phys *)
|
(* On choisit le mode d'affichage Phys *)
|
||||||
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 *)
|
||||||
let reg = genReg 11 13 15 ~invert:true 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 *)
|
(* On boucle pour afficher les leds unes par unes *)
|
||||||
|
|
||||||
Printf.printf "début\n%!";
|
Printf.printf "début\n%!";
|
||||||
findLedNumber reg leds;
|
(* On remplit le tableau avec true *)
|
||||||
while true do
|
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
|
for k = 0 to (Array.length leds) - 1 do
|
||||||
leds.(k) <- true;
|
leds.(k) <- true;
|
||||||
applyReg reg leds;
|
applyReg reg leds;
|
||||||
|
@ -23,14 +39,16 @@ let _ =
|
||||||
done;
|
done;
|
||||||
for k = (Array.length leds) - 2 downto 0 do
|
for k = (Array.length leds) - 2 downto 0 do
|
||||||
leds.(k) <- true;
|
leds.(k) <- true;
|
||||||
applyReg reg leds;
|
applyRegPulse reg leds 0.1;
|
||||||
delay 100;
|
(* applyReg reg leds; *)
|
||||||
|
(* delay 100; *)
|
||||||
leds.(k) <- false
|
leds.(k) <- false
|
||||||
done;
|
done;
|
||||||
lightLeds leds;
|
lightLeds leds;
|
||||||
|
(* applyRegPulse reg leds 1.; *)
|
||||||
applyReg reg leds;
|
applyReg reg leds;
|
||||||
Unix.sleep 1;
|
Unix.sleep 1;
|
||||||
clearLeds leds;
|
done;
|
||||||
done
|
findLedNumber reg leds;
|
||||||
|
|
||||||
;;
|
;;
|
||||||
|
|
77
shiftReg.ml
77
shiftReg.ml
|
@ -6,10 +6,16 @@
|
||||||
*)
|
*)
|
||||||
open WiringPiOcaml
|
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. The invert
|
used to contain the informations about connections.
|
||||||
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) **)
|
||||||
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
|
(** 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) **)
|
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,23 +23,51 @@ let initReg ?nb_reg:(nb_reg = 1) reg =
|
||||||
pinMode reg.p_v 1; (* mode output *)
|
pinMode reg.p_v 1; (* mode output *)
|
||||||
pinMode reg.p_s 1;
|
pinMode reg.p_s 1;
|
||||||
pinMode reg.p_a 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 *)
|
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)
|
(* 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
|
||||||
|
|
||||||
let genReg ?invert:(invert = false) pin_value pin_shift pin_apply =
|
(** This function apply all modifications to the register in the same time **)
|
||||||
{p_v = pin_value; p_s = pin_shift; p_a = pin_apply; invert = invert}
|
|
||||||
|
|
||||||
(** This function apply all modifications to the register **)
|
|
||||||
let applyReg reg leds =
|
let applyReg 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
|
||||||
write reg.p_s false;
|
shift reg leds.(i)
|
||||||
write reg.p_v (leds.(i) <> reg.invert); (* On inverse si besoin *)
|
|
||||||
write reg.p_s true;
|
|
||||||
done;
|
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 **)
|
(** Don't forget to apply it with applyReg after **)
|
||||||
let clearLeds leds =
|
let clearLeds leds =
|
||||||
|
@ -41,20 +75,21 @@ let clearLeds leds =
|
||||||
let lightLeds leds =
|
let lightLeds leds =
|
||||||
Array.iteri (fun i x -> (leds.(i) <- true)) leds
|
Array.iteri (fun i x -> (leds.(i) <- true)) leds
|
||||||
|
|
||||||
(** This function is usefull to find a LED in a logarithm time **)
|
let printBoolArray t =
|
||||||
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 =
|
|
||||||
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%!\n"
|
||||||
|
|
||||||
|
|
||||||
|
(** This function is usefull to find a LED in a logarithm time **)
|
||||||
let findLedNumber reg ?time_answer:(time_answer = 3) leds0 =
|
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 n = Array.length leds0 in
|
||||||
let leds = Array.make n false in
|
let leds = Array.make n false in
|
||||||
let i = ref 0 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
|
let middle = !i + (!j - !i)/2 in
|
||||||
makeIntervalArray leds !i middle;
|
makeIntervalArray leds !i middle;
|
||||||
applyReg reg leds;
|
applyReg reg leds;
|
||||||
printArray 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
|
||||||
|
@ -77,7 +111,6 @@ let findLedNumber reg ?time_answer:(time_answer = 3) leds0 =
|
||||||
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;
|
||||||
printArray leds;
|
|
||||||
applyReg reg leds;
|
applyReg reg leds;
|
||||||
Unix.sleep time_answer
|
Unix.sleep time_answer
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue