Example 1: Basic Readings

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