1#!/usr/bin/env python3
2#-----------------------------------------------------------------------------
3# qwiic_kx13x_ex4.py
4#
5# Simple example for the Qwiic KX132/4 Accelerometer using hardware interrupts
6# to indicate that the buffer is full and ready to be read.
7#------------------------------------------------------------------------
8#
9# Written by SparkFun Electronics, April 2021
10#
11# This python library supports the SparkFun Electroncis qwiic
12# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
13# board computers.
14#
15# More information on qwiic is at https://www.sparkfun.com/qwiic
16#
17# Do you like this library? Help support SparkFun. Buy a board!
18#
19#==================================================================================
20# Copyright (c) 2021 SparkFun Electronics
21#
22# Permission is hereby granted, free of charge, to any person obtaining a copy
23# of this software and associated documentation files (the "Software"), to deal
24# in the Software without restriction, including without limitation the rights
25# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26# copies of the Software, and to permit persons to whom the Software is
27# furnished to do so, subject to the following conditions:
28#
29# The above copyright notice and this permission notice shall be included in all
30# copies or substantial portions of the Software.
31#
32# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38# SOFTWARE.
39#==================================================================================
40# Example 4: Using the Buffer
41
42from __future__ import print_function
43import qwiic_kx13x
44import time
45import sys
46import RPi.GPIO
47
48def runExample():
49
50 print("\nSparkFun KX13X Accelerometer Example 1\n")
51 # myKx = qwiic_kx13x.QwiicKX134() # If using the KX134 un-comment this line and replace other instances of "kx132" with "kx134"
52 myKx = qwiic_kx13x.QwiicKX132()
53
54 if myKx.connected == False:
55 print("The Qwiic KX13X Accelerometer device isn't connected to the system. Please check your connection", \
56 file=sys.stderr)
57 return
58
59 if myKx.begin():
60 print("Ready.")
61 else:
62 print("Make sure you're using the KX132 and not the KX134")
63
64 # myKx.set_range(myKx.KX132_RANGE8G) # Update the range of the data output.
65 myKx.initialize(myKx.BUFFER_SETTINGS) # Load basic settings
66
67 dataReadyPin = 5
68 GPIO.setmode(GPIO.BCM)
69 GPIO.setup(dataReadyPin, GPIO.IN)
70
71 while True:
72
73 if GPIO.INPUT(dataReadyPin) == 1: # When the buffer is full, the pin will go high
74
75 myKx.get_accel_data()
76 print("X: {0}g Y: {1}g Z: {2}g".format(myKx.kx132_accel.x,
77 myKx.kx132_accel.y,
78 myKx.kx132_accel.z))
79
80 time.sleep(.02) #Set delay to 1/Output Data Rate which is by default 50Hz 1/50 = .02
81
82if __name__ == '__main__':
83 try:
84 runExample()
85 except (KeyboardInterrupt, SystemExit) as exErr:
86 print("\nEnding Example 1")
87 sys.exit(0)