ex_co2_mini v0.1.0 ExCO2Mini.Reader

Reads data packets from the USB CO2 sensor, decodes them, and sends events to the subscribed process(es).

Due to the ioctl calls required, this module will open a Port to a tiny C wrapper, rather than reading the device directly.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor

Starts reading from the USB CO2 sensor device

Starts sending data values to a target process

Link to this section Functions

Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

start_link(opts)

Starts reading from the USB CO2 sensor device.

opts is a keyword list. It accepts all of the options that GenServer.start_link/3 does, as well as the following:

  • opts[:device] (required) — The path to the CO2 device, e.g. /dev/hidraw0 or a symlink to the device.
  • opts[:subscribers] — A list of initial subscribers (default: none).

    • This can be used to save a call to subscribe/2, and/or to ensure correct data routing in the case of a supervisor restart.
  • opts[:send_from_name] — If true, then opts[:name] must be included as well. Messages sent by this reader will use that name instead of the PID.

Returns {:ok, pid} once the reader (and wrapper process) has been successfully started.

Link to this function

subscribe(reader, target \\ self())

Starts sending data values to a target process.

target can be a PID or a registered name (atom or module).

A given name or PID can only be subscribed once. Subsequent calls to this function will be effectively ignored, and target will still only receive one message per data packet. However, if a process may receive multiple messages per packet if subscribed under both its PID and name.