diff --git a/eye/lib/eye/camera.ex b/eye/lib/eye/camera.ex index 8d18959..64e3dcd 100644 --- a/eye/lib/eye/camera.ex +++ b/eye/lib/eye/camera.ex @@ -11,6 +11,10 @@ defmodule Eye.Camera do GenServer.call(__MODULE__, {:set_size, width, height}) end + def set_img_effect(effect) do + GenServer.call(__MODULE__, {:set_img_effect, effect}) + end + defdelegate next_frame(), to: Picam # GenServer API @@ -23,6 +27,7 @@ defmodule Eye.Camera do Logger.info("Configuring camera") conf = %Configuration{} Picam.set_size(conf.size.width, conf.size.height) + Picam.set_img_effect(conf.img_effect) {:ok, conf} end @@ -39,4 +44,15 @@ defmodule Eye.Camera do end end + def handle_call({:set_img_effect, effect}, _from, conf) do + case Picam.set_img_effect(effect) do + :ok -> + conf = %{conf | img_effect: effect} + {:reply, :ok, conf} + + err -> + {:reply, err, conf} + end + end + end diff --git a/eye/lib/eye/configuration.ex b/eye/lib/eye/configuration.ex index 7fbe07d..78ea546 100644 --- a/eye/lib/eye/configuration.ex +++ b/eye/lib/eye/configuration.ex @@ -7,16 +7,23 @@ defmodule Eye.Configuration do """ defstruct [ - size: %{width: 1280, height: 720} + size: %{width: 1280, height: 720}, + img_effect: :normal ] @typedoc @moduledoc @type t :: %__MODULE__{ - size: dimensions() + size: dimensions(), + img_effect: img_effect() } @type dimensions :: %{width: non_neg_integer(), height: non_neg_integer()} + @type img_effect :: + :normal + | :sketch + | :oilpaint + end diff --git a/eye_ui/assets/js/app.js b/eye_ui/assets/js/app.js index 995f659..289eac6 100644 --- a/eye_ui/assets/js/app.js +++ b/eye_ui/assets/js/app.js @@ -111,3 +111,30 @@ document.getElementById("1920x1080").onclick = (event) => { event.preventDefault(); setResolution(absintheSocket, 1920, 1080); }; + +// Image Mode Buttons + +const setImageMode = (socket, effect) => { + AbsintheSocket.send(socket, { + operation: `mutation { + imgEffect(effect: ${effect}) { + imgEffect + } + }` + }); +}; + +document.getElementById("normal").onclick = (event) => { + event.preventDefault(); + setImageMode(absintheSocket, "NORMAL"); +}; + +document.getElementById("sketch").onclick = (event) => { + event.preventDefault(); + setImageMode(absintheSocket, "SKETCH"); +}; + +document.getElementById("oilpaint").onclick = (event) => { + event.preventDefault(); + setImageMode(absintheSocket, "OILPAINT"); +}; diff --git a/eye_ui/lib/eye_ui/resolvers/camera.ex b/eye_ui/lib/eye_ui/resolvers/camera.ex index b8dde68..c409723 100644 --- a/eye_ui/lib/eye_ui/resolvers/camera.ex +++ b/eye_ui/lib/eye_ui/resolvers/camera.ex @@ -9,4 +9,9 @@ defmodule EyeUi.Resolvers.Camera do {:ok, Eye.Camera.get_config()} end + def set_img_effect(_parent, args, _resolution) do + Eye.Camera.set_img_effect(args[:effect]) + {:ok, Eye.Camera.get_config()} + end + end diff --git a/eye_ui/lib/eye_ui/schema.ex b/eye_ui/lib/eye_ui/schema.ex index 529bdab..68b1cd5 100644 --- a/eye_ui/lib/eye_ui/schema.ex +++ b/eye_ui/lib/eye_ui/schema.ex @@ -30,6 +30,12 @@ defmodule EyeUi.Schema do resolve &Resolvers.Camera.set_size/3 end + @desc "Set image effect" + field :img_effect, :camera_config do + arg :effect, :img_effect + resolve &Resolvers.Camera.set_img_effect/3 + end + end subscription name: "Subscription" do diff --git a/eye_ui/lib/eye_ui/schema/camera.ex b/eye_ui/lib/eye_ui/schema/camera.ex index 1e2738c..4068da9 100644 --- a/eye_ui/lib/eye_ui/schema/camera.ex +++ b/eye_ui/lib/eye_ui/schema/camera.ex @@ -4,6 +4,7 @@ defmodule EyeUi.Schema.CameraTypes do @desc "Camera configuration" object :camera_config do field :size, :dimensions + field :img_effect, :img_effect end @desc "Image dimensions" @@ -11,4 +12,11 @@ defmodule EyeUi.Schema.CameraTypes do field :width, :integer field :height, :integer end + + @desc "Image Effects" + enum :img_effect do + value :normal, as: :none, description: "No special effects" + value :sketch, as: :sketch, description: "Just like at the amusement park" + value :oilpaint, as: :oilpaint, description: "Just like Van Gogh" + end end diff --git a/eye_ui/lib/eye_ui_web/router.ex b/eye_ui/lib/eye_ui_web/router.ex index 74eb78b..50f9607 100644 --- a/eye_ui/lib/eye_ui_web/router.ex +++ b/eye_ui/lib/eye_ui_web/router.ex @@ -23,6 +23,8 @@ defmodule EyeUiWeb.Router do socket: EyeUiWeb.UserSocket, interface: :simple + forward "/snap", EyeUiWeb.SnapPlug + scope "/", EyeUiWeb do pipe_through :browser diff --git a/eye_ui/lib/eye_ui_web/snap_plug.ex b/eye_ui/lib/eye_ui_web/snap_plug.ex new file mode 100644 index 0000000..8bd0206 --- /dev/null +++ b/eye_ui/lib/eye_ui_web/snap_plug.ex @@ -0,0 +1,14 @@ +defmodule EyeUiWeb.SnapPlug do + import Plug.Conn + + def init(opts), do: opts + def call(conn, _opts) do + conn + |> put_resp_header("Age", "0") + |> put_resp_header("Cache-Control", "no-cache, private") + |> put_resp_header("Pragma", "no-cache") + |> put_resp_header("Content-Type", "image/jpeg") + |> send_resp(200, Eye.Camera.next_frame()) + end + +end diff --git a/eye_ui/lib/eye_ui_web/templates/page/index.html.eex b/eye_ui/lib/eye_ui_web/templates/page/index.html.eex index f841771..62f3570 100644 --- a/eye_ui/lib/eye_ui_web/templates/page/index.html.eex +++ b/eye_ui/lib/eye_ui_web/templates/page/index.html.eex @@ -6,6 +6,12 @@ 1280 x 720 1920 x 1080 + Snap +