Created
April 23, 2019 19:07
-
-
Save TobleMiner/d760675a575ec70249266ab872398f0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e -o pipefail | |
BUS="1" | |
hex_to_byte() { | |
echo "$((16#$1))" | |
} | |
extract_byte() { | |
echo "$(hex_to_byte "$(cut -d' ' -f$1 | egrep -io '[0-9a-f]+$')")" | |
} | |
read_reg() { | |
local reg="$1" | |
local xfer="$(i2ctransfer -f -y $BUS "r2@0x${reg}")" | |
local cmd=$((16#$reg * 2 + 1)) | |
local data="$(extract_byte 1 <<< "$xfer")" | |
local csum="$(extract_byte 2 <<< "$xfer")" | |
local csum_expect=$(((cmd + data) % 256)) | |
[ "$csum" -eq "$csum_expect" ] || { | |
return 1 | |
} | |
echo "$data" | |
} | |
configure() { | |
cat <<EOF | |
graph_title CO2 concentration | |
graph_args --base 1000 --lower-limit 0 | |
graph_vlabel ppm | |
graph_info Current CO2 concentration at my desk | |
graph_category environment | |
ppm.label CO2 concentration | |
ppm.draw LINE1 | |
ppm.colour 000000 | |
EOF | |
} | |
fetch() { | |
low="$(read_reg 70)" | |
high="$(read_reg 78)" | |
echo ppm.value $((high * 256 + low)) | |
} | |
case $1 in | |
config) configure;; | |
*) fetch;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment