co2minimon

Get temperature and CO2 concentration data from a CO2Mini sensor.
Log | Files | Refs | README | LICENSE

commit 8eeae9f8955ad6dd121f30b651921456bc13f291
parent 4646200c37d152830ee44c219d9facaf08ef93ba
Author: Amin Mesbah <dev@aminmesbah.com>
Date:   Fri, 14 Jan 2022 23:35:49 -0800

Clean things up and add build script

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;