Skip to content

Commit 6707886

Browse files
committed
Simple Elixir Port manager process
1 parent 888a441 commit 6707886

File tree

6 files changed

+36
-47
lines changed

6 files changed

+36
-47
lines changed

Makefile

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@
55
# CFLAGS compiler flags for compiling all C files
66
# LDFLAGS linker flags for linking all binaries
77

8-
# Initialize some variables if not set
9-
LDFLAGS ?=
10-
CFLAGS ?= -O2 -Wall -Wextra -Wno-unused-parameter -g
8+
LDFLAGS += -lzbar -ljpeg
9+
CFLAGS += -Wall -std=gnu99
1110
CC ?= $(CROSSCOMPILE)-gcc
1211

13-
DEFAULT_TARGETS ?= priv priv/zbar_scanner
14-
15-
# Link in all of the VideoCore libraries
16-
LDFLAGS +=-lzbar -ljpeg
17-
1812
SRC=src/zbar_scanner.c
1913
OBJ=$(SRC:.c=.o)
2014

15+
DEFAULT_TARGETS ?= priv priv/zbar_scanner
16+
2117
.PHONY: all clean
2218

2319
all: $(DEFAULT_TARGETS)

config/config.exs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1 @@
1-
# This file is responsible for configuring your application
2-
# and its dependencies with the aid of the Mix.Config module.
31
use Mix.Config
4-
5-
# This configuration is loaded before any dependency and is restricted
6-
# to this project. If another project depends on this project, this
7-
# file won't be loaded nor affect the parent project. For this reason,
8-
# if you want to provide default values for your application for
9-
# 3rd-party users, it should be done in your "mix.exs" file.
10-
11-
# You can configure for your application as:
12-
#
13-
# config :zbar, key: :value
14-
#
15-
# And access this configuration in your application as:
16-
#
17-
# Application.get_env(:zbar, :key)
18-
#
19-
# Or configure a 3rd-party app:
20-
#
21-
# config :logger, level: :info
22-
#
23-
24-
# It is also possible to import configuration files, relative to this
25-
# directory. For example, you can emulate configuration per environment
26-
# by uncommenting the line below and defining dev.exs, test.exs and such.
27-
# Configuration from the imported file will override the ones defined
28-
# here (which is why it is important to import them last).
29-
#
30-
# import_config "#{Mix.env}.exs"

lib/zbar.ex

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
defmodule Zbar do
22
@moduledoc """
3-
Documentation for Zbar.
3+
Scan one or more barcodes found in a JPEG image using the zbar library.
44
"""
5+
require Logger
56

67
@doc """
7-
Hello world.
8+
Returns {exit_status, "binary output from zbar_scanner"}
9+
"""
10+
def scan(filename) do
11+
{:spawn_executable, zbar_binary()}
12+
|> Port.open([
13+
{:args, [filename]},
14+
:binary,
15+
:stream,
16+
:exit_status,
17+
:use_stdio,
18+
:stderr_to_stdout
19+
])
20+
|> handle_output()
21+
end
822

9-
## Examples
23+
defp handle_output(port, buffer \\ "") do
24+
receive do
25+
{^port, {:data, data}} ->
26+
Logger.debug(buffer <> to_string(data))
27+
handle_output(port, buffer <> to_string(data))
28+
{^port, {:exit_status, status}} ->
29+
{status, buffer}
30+
end
31+
end
1032

11-
iex> Zbar.hello
12-
:world
33+
defp zbar_binary, do: Path.join(:code.priv_dir(:zbar), "zbar_scanner")
1334

14-
"""
15-
def hello do
16-
:world
17-
end
1835
end

mix.exs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ defmodule Zbar.Mixfile do
44
def project do
55
[app: :zbar,
66
version: "0.1.0",
7+
description: "Scan one or more barcodes from a JPEG image",
78
elixir: "~> 1.4",
9+
make_clean: ["clean"],
10+
compilers: [:elixir_make | Mix.compilers()],
811
build_embedded: Mix.env == :prod,
912
start_permanent: Mix.env == :prod,
1013
compilers: [:elixir_make] ++ Mix.compilers,
@@ -29,6 +32,6 @@ defmodule Zbar.Mixfile do
2932
#
3033
# Type "mix help deps" for more examples and options
3134
defp deps do
32-
[]
35+
[{:elixir_make, "~> 0.4", runtime: false}]
3336
end
3437
end

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
%{"elixir_make": {:hex, :elixir_make, "0.4.0", "992f38fabe705bb45821a728f20914c554b276838433349d4f2341f7a687cddf", [], [], "hexpm"}}

src/zbar_scanner.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <unistd.h>
22
#include <stdio.h>
33
#include <stdlib.h>
4+
#include <stdint.h>
45
#include <err.h>
56

67
#include <jpeglib.h>

0 commit comments

Comments
 (0)