Skip to content

Commit

Permalink
Implement basic features
Browse files Browse the repository at this point in the history
  • Loading branch information
mnishiguchi committed Aug 23, 2021
1 parent 42b6e0a commit f409aee
Show file tree
Hide file tree
Showing 17 changed files with 749 additions and 30 deletions.
138 changes: 138 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
name: CI
on: [push, pull_request]
jobs:
deps:
name: Dependencies
runs-on: ubuntu-latest
strategy:
matrix:
include:
- elixir: 1.11
otp: 23
- elixir: 1.12
otp: 24
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.6.0
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup
uses: erlef/setup-elixir@v1
with:
elixir-version: ${{ matrix.elixir }}
otp-version: ${{ matrix.otp }}

- name: Retrieve Cached Dependencies
uses: actions/cache@v2
id: mix-cache
with:
path: |
deps
_build
priv/plts
key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('mix.lock') }}

- name: Install Dependencies
if: steps.mix-cache.outputs.cache-hit != 'true'
run: |
mkdir -p priv/plts
mix local.rebar --force
mix local.hex --force
mix deps.get
mix deps.compile
mix dialyzer --plt
static_code_analysis:
name: Static Code Analysis
needs: deps
runs-on: ubuntu-latest
strategy:
matrix:
include:
- elixir: 1.11
otp: 23
- elixir: 1.12
otp: 24
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.6.0
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup
uses: erlef/setup-elixir@v1
with:
elixir-version: ${{ matrix.elixir }}
otp-version: ${{ matrix.otp }}

- name: Retrieve Cached Dependencies
uses: actions/cache@v2
id: mix-cache
with:
path: |
deps
_build
priv/plts
key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('mix.lock') }}

- name: Check Code Format
run: mix format --check-formatted

- name: Run Credo
run: mix credo

- name: Run Dialyzer
run: mix dialyzer --no-check --ignore-exit-status

unit_tests:
name: Unit Tests
needs: deps
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- elixir: 1.11
otp: 23
- elixir: 1.12
otp: 24
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.6.0
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup
uses: erlef/setup-elixir@v1
with:
elixir-version: ${{ matrix.elixir }}
otp-version: ${{ matrix.otp }}

- name: Retrieve Cached Dependencies
uses: actions/cache@v2
id: mix-cache
with:
path: |
deps
_build
priv/plts
key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('mix.lock') }}

- name: Run test
run: mix test --trace --slowest 10
23 changes: 23 additions & 0 deletions .github/workflows/hex.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Hex
on:
push:
tags: ['v*']
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: erlef/setup-elixir@v1
with:
otp-version: '24'
elixir-version: '1.12'
- uses: actions/cache@v2
with:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{ runner.os }}-mix-
- run: mix deps.get
- run: mix hex.publish --yes
env:
HEX_API_KEY: ${{ secrets.HEX_API_KEY }}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2021 Masatoshi Nishiguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
53 changes: 41 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,49 @@
# SCD4X

**TODO: Add description**
[![Hex version](https://img.shields.io/hexpm/v/scd4x.svg 'Hex version')](https://hex.pm/packages/scd4x)
[![API docs](https://img.shields.io/hexpm/v/scd4x.svg?label=docs 'API docs')](https://hexdocs.pm/scd4x)
[![CI](https://github.com/mnishiguchi/scd4x/actions/workflows/ci.yml/badge.svg)](https://github.com/mnishiguchi/scd4x/actions/workflows/ci.yml)
[![Hex](https://github.com/mnishiguchi/scd4x/actions/workflows/hex.yml/badge.svg)](https://github.com/mnishiguchi/scd4x/actions/workflows/hex.yml)

## Installation
Use [Sensirion SCD4X](https://www.sensirion.com/en/environmental-sensors/carbon-dioxide-sensors/carbon-dioxide-sensor-scd4x) [CO2](https://en.wikipedia.org/wiki/Carbon_dioxide) sensor in Elixir.

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `scd4x` to your list of dependencies in `mix.exs`:
## Usage

### Start the sensor server

```elixir
iex> {:ok, scd} = SCD4X.start_link(bus_name: "i2c-1")
{:ok, #PID<0.1407.0>}
```

### Single shot measurement

```elixir
def deps do
[
{:scd4x, "~> 0.1.0"}
]
end
iex> SCD4X.measure_single_shot(scd)
{:ok,
%SCD4X.Measurement{
co2_ppm: 638,
humidity_rh: 70.49713134765625,
temperature_c: 26.63848876953125,
timestamp_ms: 400768
}}
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/scd4x](https://hexdocs.pm/scd4x).
### Periodical measurement

```elixir
iex> SCD4X.start_periodic_measurement(scd)
:ok

iex> SCD4X.measure(scd)
{:ok,
%SCD4X.Measurement{
co2_ppm: 612,
humidity_rh: 59.07440185546875,
temperature_c: 30.331497192382812,
timestamp_ms: 620482
}}

SCD4X.stop_periodic_measurement(scd)
:ok
```
95 changes: 87 additions & 8 deletions lib/scd4x.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,97 @@
defmodule SCD4X do
@moduledoc """
Documentation for `SCD4X`.
Use Sensirion SCD4X CO2 sensor in Elixir
"""

@doc """
Hello world.
use GenServer

## Examples
require Logger

iex> SCD4X.hello()
:world
@type options() :: [GenServer.option() | {:bus_name, bus_name}]

@typedoc """
Which I2C bus to use (defaults to `"i2c-1"`)
"""
def hello do
:world
@type bus_name :: binary

@default_bus_name "i2c-1"
@bus_address 0x62

@spec start_link(options()) :: GenServer.on_start()
def start_link(init_arg \\ []) do
gen_server_opts =
Keyword.take(init_arg, [:name, :debug, :timeout, :spawn_opt, :hibernate_after])

GenServer.start_link(__MODULE__, init_arg, gen_server_opts)
end

@spec start_periodic_measurement(GenServer.server()) :: :ok | {:error, any}
def start_periodic_measurement(server) do
GenServer.call(server, :start_periodic_measurement)
end

@spec stop_periodic_measurement(GenServer.server()) :: :ok | {:error, any}
def stop_periodic_measurement(server) do
GenServer.call(server, :stop_periodic_measurement)
end

@spec measure(GenServer.server()) :: {:ok, SCD4X.Measurement.t()} | {:error, any}
def measure(server) do
GenServer.call(server, :measure, 10_000)
end

@spec measure_single_shot(GenServer.server()) :: {:ok, SCD4X.Measurement.t()} | {:error, any}
def measure_single_shot(server) do
GenServer.call(server, :measure_single_shot, 10_000)
end

@impl GenServer
def init(init_arg) do
bus_name = init_arg[:bus_name] || @default_bus_name
bus_address = @bus_address

Logger.info(
"[SCD4X] Starting on bus #{bus_name} at address #{inspect(bus_address, base: :hex)}"
)

with {:ok, transport} <-
SCD4X.Transport.I2C.start_link(bus_name: bus_name, bus_address: bus_address),
{:ok, serial_number} <- SCD4X.Comm.serial_number(transport) do
Logger.info("[SCD4X] Initializing sensor #{serial_number}")

state = %{
serial_number: serial_number,
transport: transport
}

{:ok, state}
else
_error ->
{:stop, "Error connecting to the sensor"}
end
end

@impl GenServer
def handle_call(:start_periodic_measurement, _from, state) do
response = SCD4X.Comm.start_periodic_measurement(state.transport)
{:reply, response, state}
end

@impl GenServer
def handle_call(:stop_periodic_measurement, _from, state) do
response = SCD4X.Comm.stop_periodic_measurement(state.transport)
{:reply, response, state}
end

@impl GenServer
def handle_call(:measure, _from, state) do
response = SCD4X.Comm.read_measurement(state.transport)
{:reply, response, state}
end

@impl GenServer
def handle_call(:measure_single_shot, _from, state) do
response = SCD4X.Comm.read_measurement_single_shot(state.transport)
{:reply, response, state}
end
end
Loading

0 comments on commit f409aee

Please sign in to comment.