co2minimon

Get temperature and CO2 concentration data from a CO2Mini sensor.
git clone git://git.amin.space/co2minimon.git
Log | Files | Refs | README | LICENSE

commit b7e7219cba00a2bf3639e3af0c7d2c3ccfc7f517
parent d04fa108b53d9c0a24b3a42c21257281240e7c61
Author: amin <dev@aminmesbah.com>
Date:   Sat, 15 Jan 2022 07:35:48 +0000

Clean things up and add build script

FossilOrigin-Name: 1624eff05f7304cc969340ada73683191c1c7941581f87e17d614eff73c95938
Diffstat:
M.gitignore | 2+-
Abuild.sh | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmain.c | 8+++++---
3 files changed, 77 insertions(+), 4 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1 @@ -a.out +out/ diff --git a/build.sh b/build.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +set -e # fail if any command has a non-zero exit status +set -u # fail if any undefined variable is referenced +set -o pipefail # propagate failure exit status through a pipeline +shopt -s globstar nullglob # enable recursive and null globbing + +exe_name="co2minimon" +build_dir="./out" + +cc=clang +source_files=("main.c") +cflags=("-std=c11" "-D_XOPEN_SOURCE=700" "-Wall" "-Wextra" "-Wpedantic" "-Wsign-compare" "-Wswitch-enum") +debug_flags=("-g" "-O0") +release_flags=("-O2" "-Os") + +build_release() { + release_dir="${build_dir}/release" + release_path="${release_dir}/${exe_name}" + mkdir -p "$release_dir" + ( + set -x; + "$cc" "${cflags[@]}" "${release_flags[@]}" "${source_files[@]}" -o "$release_path" + ) +} + +build_debug() { + debug_dir="${build_dir}/debug" + debug_path="${debug_dir}/${exe_name}" + mkdir -p "$debug_dir" + ( + set -x; + "$cc" "${cflags[@]}" "${debug_flags[@]}" "${source_files[@]}" -o "$debug_path" + ) +} + +usage() { + echo "build.sh - Build ${exe_name}" + echo " " + echo "build.sh [options]" + echo " " + echo "options:" + echo "-h, --help show help" + echo "-d, --debug build only debug executable" +} + +debug_build=false + +while [[ $# -gt 0 ]]; do + case "$1" in + -h|--help) + usage + exit 0 + ;; + -d|--debug) + debug_build=true + shift + ;; + *) + break + ;; + esac +done + +if [ "$debug_build" = true ]; then + build_debug +else + build_release +fi + +exit 0 diff --git a/main.c b/main.c @@ -7,6 +7,7 @@ #include <fcntl.h> #include <signal.h> #include <sys/ioctl.h> +#include <sys/stat.h> #include <unistd.h> // https://www.kernel.org/doc/Documentation/hid/hidraw.txt @@ -16,6 +17,7 @@ static bool running = true; void stop_running(int sig) { + (void)sig; running = false; } @@ -25,7 +27,7 @@ int main(void) { { struct sigaction act = { - .sa_handler = stop_running; + .sa_handler = stop_running, }; sigaction(SIGINT, &act, NULL); sigaction(SIGKILL, &act, NULL); @@ -35,8 +37,8 @@ int main(void) // NOTE: Included udev rules create this symlink to the appropriate hidraw // entry. const char *hid_file_path = "/dev/co2mini0"; - const char *temperature_file_path = "/tmp/co2mini_temp"; - const char *co2_file_path = "/tmp/co2mini_co2"; + const char *temperature_file_path = "/tmp/co2minimon_temp"; + const char *co2_file_path = "/tmp/co2minimon_co2"; int device_handle = -1;