Last active
September 14, 2020 14:03
-
-
Save hankei6km/f1172e43701456715c1958436f860de1 to your computer and use it in GitHub Desktop.
ADXL345 x Raspberry Pi をシェルスクリプトで利用する https://hankei6km.github.io/posts/2020-09-adxl345_raspberryi_pi_shellscript/
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
#!/bin/sh | |
# | |
# Copyright (c) 2020 hankei6km | |
# Licensed under the MIT License. | |
# https://opensource.org/licenses/MIT | |
# https://www.analog.com/media/jp/technical-documentation/data-sheets/ADXL345_jp.pdf | |
BW_RATE="0x2c" | |
DATA_FORMAT="0x31" | |
FIFO_CTL="0x38" | |
POWER_CTL="0x2d" | |
INT_ENABLE="0x2E" | |
INT_MAP="0x2F" | |
THRESH_TAP="0x1D" | |
DUR="0x21" | |
LATENT="0x22" | |
WINDOW="0x23" | |
TAP_AXES="0x2A" | |
INT_SOURCE="0x30" | |
I2C_BUS="1" | |
CHIP_ADDR="0x1d" | |
INT1_PIN=4 | |
i2cset -y "${I2C_BUS}" "${CHIP_ADDR}" "${BW_RATE}" 0x0b b # 200Hz | |
i2cset -y "${I2C_BUS}" "${CHIP_ADDR}" "${DATA_FORMAT}" 0x00 b # +-2G, RES = 10bit, INT = Active High | |
i2cset -y "${I2C_BUS}" "${CHIP_ADDR}" "${FIFO_CTL}" 0x00 b # bypass | |
i2cset -y "${I2C_BUS}" "${CHIP_ADDR}" "${POWER_CTL}" 0x08 b # Measure 8Hz | |
i2cset -y "${I2C_BUS}" "${CHIP_ADDR}" "${THRESH_TAP}" 0x50 b | |
i2cset -y "${I2C_BUS}" "${CHIP_ADDR}" "${DUR}" 0x0f b | |
i2cset -y "${I2C_BUS}" "${CHIP_ADDR}" "${LATENT}" 0x050 b | |
i2cset -y "${I2C_BUS}" "${CHIP_ADDR}" "${WINDOW}" 0xF0 b | |
i2cset -y "${I2C_BUS}" "${CHIP_ADDR}" "${TAP_AXES}" 0x07 b # XYZ | |
# i2cget -y "${I2C_BUS}" "${CHIP_ADDR}" 0x32 > /dev/null | |
# i2cset -y "${I2C_BUS}" "${CHIP_ADDR}" "${INT_ENABLE}" 0x40 b # SINGLE_TAP | |
i2cset -y "${I2C_BUS}" "${CHIP_ADDR}" "${INT_ENABLE}" 0x20 b # DOUBLE_TAP | |
i2cset -y "${I2C_BUS}" "${CHIP_ADDR}" "${INT_MAP}" 0x00 b # INT1 | |
i2cget -y "${I2C_BUS}" "${CHIP_ADDR}" "${INT_SOURCE}" > /dev/null # CLEAR INT | |
while true ; do | |
gpio -g wfi "${INT1_PIN}" rising | |
# i2cget -y 1 0x1d "${INT_SOURCE}" | |
echo "double tap" | |
i2cget -y "${I2C_BUS}" "${CHIP_ADDR}" "${INT_SOURCE}" > /dev/null | |
done |
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
#!/bin/bash | |
# | |
# Copyright (c) 2020 hankei6km | |
# Licensed under the MIT License. | |
# https://opensource.org/licenses/MIT | |
# https://www.analog.com/media/jp/technical-documentation/data-sheets/ADXL345_jp.pdf | |
BW_RATE="0x2c" | |
DATA_FORMAT="0x31" | |
FIFO_CTL="0x38" | |
POWER_CTL="0x2d" | |
INT_ENABLE="0x2E" | |
INT_MAP="0x2F" | |
THRESH_TAP="0x1D" | |
DUR="0x21" | |
LATENT="0x22" | |
WINDOW="0x23" | |
TAP_AXES="0x2A" | |
INT_SOURCE="0x30" | |
DATA_X0="0x32" | |
DATA_X1="0x33" | |
DATA_Y0="0x34" | |
DATA_Y1="0x35" | |
DATA_Z0="0x36" | |
DATA_Z1="0x37" | |
I2C_BUS="1" | |
CHIP_ADDR="0x1d" | |
# https://stackoverflow.com/questions/21051149/bash-script-convert-signed-hex-to-signed-decimal | |
conv() { | |
RES=$(printf "%d" "${1}") | |
(( RES > 32769 )) && (( RES -= 65536 )) | |
echo "${RES}" | |
} | |
get_data() { | |
W=$(i2cget -y "${I2C_BUS}" "${CHIP_ADDR}" "${1}" "w") | |
echo $(conv "${W}") | |
} | |
i2cset -y "${I2C_BUS}" "${CHIP_ADDR}" "${DATA_FORMAT}" 0x00 b # +-2G RES = 10bit, INT = Active High | |
# Range 10bit = 1024 = +- 512 -> 1G = 256 | |
echo $(get_data "${DATA_X0}") | |
echo $(get_data "${DATA_Y0}") | |
echo $(get_data "${DATA_Z0}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment