Skip to content

Instantly share code, notes, and snippets.

@shritesh
Last active March 19, 2020 16:13
Show Gist options
  • Select an option

  • Save shritesh/7e7d31488c04ba118c3e15d5e65607d5 to your computer and use it in GitHub Desktop.

Select an option

Save shritesh/7e7d31488c04ba118c3e15d5e65607d5 to your computer and use it in GitHub Desktop.

Revisions

  1. shritesh revised this gist Mar 19, 2020. 1 changed file with 9 additions and 12 deletions.
    21 changes: 9 additions & 12 deletions reason_p5_stopwatch.re
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,6 @@
    type p5;

    type sketch = {
    mutable setup: unit => unit,
    mutable draw: unit => unit,
    };
    type sketch;

    type button;

    @@ -16,6 +13,8 @@ type state =
    [@bs.module] [@bs.new] external createP5: (sketch => unit) => p5 = "p5";
    [@bs.send] external createCanvas: (sketch, int, int) => unit = "createCanvas";
    [@bs.send] external noCanvas: sketch => unit = "noCanvas";
    [@bs.set] external onSetup: (sketch, unit => unit) => unit = "setup";
    [@bs.set] external onDraw: (sketch, unit => unit) => unit = "draw";
    [@bs.send] external millis: sketch => int = "millis";
    [@bs.send] external createButton: (sketch, string) => button = "createButton";
    [@bs.send] external html: (button, string) => unit = "html";
    @@ -36,17 +35,14 @@ let makeSketch = s => {
    | _ => ()
    };

    s.setup = (
    () => {
    s->onSetup(() => {
    s->noCanvas;
    let btn = s->createButton("Start");
    btn->mousePressed(btnPressed);
    appState := NotStarted(btn);
    }
    );
    });

    s.draw = (
    () =>
    s->onDraw(() =>
    switch (appState^) {
    | Running(btn, time) =>
    let seconds = float(millis(s) - time) /. 1000.0;
    @@ -55,7 +51,8 @@ let makeSketch = s => {
    btn->html(str ++ " Seconds");
    | _ => ()
    }
    );
    );
    };

    createP5(makeSketch);
    createP5(makeSketch);

  2. shritesh revised this gist Mar 14, 2020. 1 changed file with 9 additions and 11 deletions.
    20 changes: 9 additions & 11 deletions reason_p5_stopwatch.re
    Original file line number Diff line number Diff line change
    @@ -26,37 +26,35 @@ external mousePressed: (button, unit => unit) => unit = "mousePressed";
    let makeSketch = s => {
    let appState = ref(Initializing);

    let btnPressed = () => {
    let btnPressed = () =>
    switch (appState^) {
    | NotStarted(btn) => appState := Running(btn, millis(s))
    | Running(btn, time) => appState := Stopped(btn, time)
    | Stopped(btn, _) =>
    appState := NotStarted(btn);
    html(btn, "Start");
    btn->html("Start");
    | _ => ()
    };
    };

    s.setup = (
    () => {
    noCanvas(s);
    let btn = createButton(s, "Start");
    mousePressed(btn, btnPressed);
    s->noCanvas;
    let btn = s->createButton("Start");
    btn->mousePressed(btnPressed);
    appState := NotStarted(btn);
    }
    );

    s.draw = (
    () => {
    () =>
    switch (appState^) {
    | Running(btn, time) =>
    let seconds = float(millis(s) - time) /. 1000.0;
    let str = nfc(s, seconds, 2);
    let str = s->nfc(seconds, 2);

    html(btn, str ++ " Seconds");
    btn->html(str ++ " Seconds");
    | _ => ()
    };
    }
    }
    );
    };

  3. shritesh renamed this gist Mar 10, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. shritesh created this gist Mar 10, 2020.
    63 changes: 63 additions & 0 deletions reason_stopwatch.re
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    type p5;

    type sketch = {
    mutable setup: unit => unit,
    mutable draw: unit => unit,
    };

    type button;

    type state =
    | Initializing
    | NotStarted(button)
    | Running(button, int)
    | Stopped(button, int);

    [@bs.module] [@bs.new] external createP5: (sketch => unit) => p5 = "p5";
    [@bs.send] external createCanvas: (sketch, int, int) => unit = "createCanvas";
    [@bs.send] external noCanvas: sketch => unit = "noCanvas";
    [@bs.send] external millis: sketch => int = "millis";
    [@bs.send] external createButton: (sketch, string) => button = "createButton";
    [@bs.send] external html: (button, string) => unit = "html";
    [@bs.send]
    external mousePressed: (button, unit => unit) => unit = "mousePressed";
    [@bs.send] external nfc: (sketch, float, int) => string = "nfc";

    let makeSketch = s => {
    let appState = ref(Initializing);

    let btnPressed = () => {
    switch (appState^) {
    | NotStarted(btn) => appState := Running(btn, millis(s))
    | Running(btn, time) => appState := Stopped(btn, time)
    | Stopped(btn, _) =>
    appState := NotStarted(btn);
    html(btn, "Start");
    | _ => ()
    };
    };

    s.setup = (
    () => {
    noCanvas(s);
    let btn = createButton(s, "Start");
    mousePressed(btn, btnPressed);
    appState := NotStarted(btn);
    }
    );

    s.draw = (
    () => {
    switch (appState^) {
    | Running(btn, time) =>
    let seconds = float(millis(s) - time) /. 1000.0;
    let str = nfc(s, seconds, 2);

    html(btn, str ++ " Seconds");
    | _ => ()
    };
    }
    );
    };

    createP5(makeSketch);