Example 3 - This example demonstrates of polling the “data ready” bit, i.e. software interrupt.

examples/qwiic_kx13x_ex3.py
 1#!/usr/bin/env python3
 2#-----------------------------------------------------------------------------
 3# qwiic_kx13x_ex3.py
 4#
 5# Simple example for the Qwiic KX132/4 Accelerometer using software interrupts
 6# to indicate that data is ready.
 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 3: Using software interrupts.
41
42from __future__ import print_function
43import qwiic_kx13x
44import time
45import sys
46
47def runExample():
48
49    print("\nSparkFun KX13X Accelerometer Example 1\n")
50    # myKx = qwiic_kx13x.QwiicKX134() # If using the KX134 un-comment this line and replace other instances of "kx132" with "kx134"
51    myKx = qwiic_kx13x.QwiicKX132()
52
53    if myKx.connected == False:
54        print("The Qwiic KX13X Accelerometer device isn't connected to the system. Please check your connection", \
55                file=sys.stderr)
56        return
57
58    if myKx.begin():
59        print("Ready.")
60    else:
61        print("Make sure you're using the KX132 and not the KX134")
62
63    # myKx.set_range(myKx.KX132_RANGE8G) # Update the range of the data output.
64    myKx.initialize(myKx.SOFT_INT_SETTINGS) # Load basic settings 
65
66    while True:
67            
68        if myKx.data_trigger():
69
70            myKx.get_accel_data()
71            print("X: {0}g Y: {1}g Z: {2}g".format(myKx.kx132_accel.x,
72                                                   myKx.kx132_accel.y,
73                                                   myKx.kx132_accel.z))
74
75        time.sleep(.02) #Set delay to 1/Output Data Rate which is by default 50Hz 1/50 = .02
76
77if __name__ == '__main__':
78	try:
79		runExample()
80	except (KeyboardInterrupt, SystemExit) as exErr:
81		print("\nEnding Example 1")
82		sys.exit(0)