1#!/usr/bin/env python
2#-----------------------------------------------------------------------------
3# ex2_qwiic_adxl313_set_range.py
4#
5# Simple Example for the Qwiic ADXL313 DeviceSet range of the sensor to 2G.
6# Then read values of x/y/z axis of the ADXL313 (via I2C), print them to terminal.
7# Note, other range options are: 0.5G, 1G[defaut], 2G or 4 G.
8# Except for custom range, this example uses default configuration (full resolution, 100Hz datarate).
9#------------------------------------------------------------------------
10#
11# Written by SparkFun Electronics, October 2020
12#
13# This python library supports the SparkFun Electroncis qwiic
14# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
15# board computers.
16#
17# More information on qwiic is at https://www.sparkfun.com/qwiic
18#
19# Do you like this library? Help support SparkFun. Buy a board!
20#
21#==================================================================================
22# Copyright (c) 2019 SparkFun Electronics
23#
24# Permission is hereby granted, free of charge, to any person obtaining a copy
25# of this software and associated documentation files (the "Software"), to deal
26# in the Software without restriction, including without limitation the rights
27# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28# copies of the Software, and to permit persons to whom the Software is
29# furnished to do so, subject to the following conditions:
30#
31# The above copyright notice and this permission notice shall be included in all
32# copies or substantial portions of the Software.
33#
34# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40# SOFTWARE.
41#==================================================================================
42# Example 2
43#
44
45from __future__ import print_function
46import qwiic_adxl313
47import time
48import sys
49
50def runExample():
51
52 print("\nSparkFun Adxl313 Example 2 - Set Range\n")
53 myAdxl = qwiic_adxl313.QwiicAdxl313()
54
55 if myAdxl.connected == False:
56 print("The Qwiic ADXL313 device isn't connected to the system. Please check your connection", \
57 file=sys.stderr)
58 return
59 else:
60 print("Device connected successfully.")
61
62 myAdxl.standby(); # Must be in standby before changing settings.
63 # This is here just in case we already had sensor powered and/or
64 # configured from a previous setup.
65
66 myAdxl.setRange(myAdxl.ADXL313_RANGE_2_G);
67
68 # Try some other range settings by uncommented your choice below
69 #myAdxl.setRange(myAdxl.ADXL313_RANGE_05_G);
70 #myAdxl.setRange(myAdxl.ADXL313_RANGE_1_G);
71 #myAdxl.setRange(myAdxl.ADXL313_RANGE_2_G);
72 #myAdxl.setRange(myAdxl.ADXL313_RANGE_4_G);
73
74 myAdxl.measureModeOn()
75
76 while True:
77 if myAdxl.dataReady():
78 myAdxl.readAccel() # read all axis from sensor, note this also updates all instance variables
79 print(\
80 '{: 06d}'.format(myAdxl.x)\
81 , '\t', '{: 06d}'.format(myAdxl.y)\
82 , '\t', '{: 06d}'.format(myAdxl.z)\
83 )
84 time.sleep(0.03)
85 else:
86 print("Waiting for data")
87 time.sleep(0.5)
88
89if __name__ == '__main__':
90 try:
91 runExample()
92 except (KeyboardInterrupt, SystemExit) as exErr:
93 print("\nEnding Example 1")
94 sys.exit(0)
95
96