1#!/usr/bin/env python
2#-----------------------------------------------------------------------------
3# ex3_qwiic_adxl313_auto_sleep.py
4#
5# Simple Example for the Qwiic ADXL313 DeviceSet that shows how to use Autosleep feature.
6# First, setup THRESH_INACT, TIME_INACT, and participating axis.
7# These settings will determine when the unit will go into autosleep mode and save power!
8# We are only going to use the x-axis (and are disabling y-axis and z-axis).
9# This is so you can place the board "flat" inside your project,
10# and we can ignore gravity on z-axis.
11#------------------------------------------------------------------------
12#
13# Written by SparkFun Electronics, October 2020
14#
15# This python library supports the SparkFun Electroncis qwiic
16# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
17# board computers.
18#
19# More information on qwiic is at https://www.sparkfun.com/qwiic
20#
21# Do you like this library? Help support SparkFun. Buy a board!
22#
23#==================================================================================
24# Copyright (c) 2019 SparkFun Electronics
25#
26# Permission is hereby granted, free of charge, to any person obtaining a copy
27# of this software and associated documentation files (the "Software"), to deal
28# in the Software without restriction, including without limitation the rights
29# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30# copies of the Software, and to permit persons to whom the Software is
31# furnished to do so, subject to the following conditions:
32#
33# The above copyright notice and this permission notice shall be included in all
34# copies or substantial portions of the Software.
35#
36# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42# SOFTWARE.
43#==================================================================================
44# Example 3
45#
46
47from __future__ import print_function
48import qwiic_adxl313
49import time
50import sys
51
52def runExample():
53
54 print("\nSparkFun Adxl313 Example 3 - Setup Autosleep and then only print values when it's awake.\n")
55 myAdxl = qwiic_adxl313.QwiicAdxl313()
56
57 if myAdxl.connected == False:
58 print("The Qwiic ADXL313 device isn't connected to the system. Please check your connection", \
59 file=sys.stderr)
60 return
61 else:
62 print("Device connected successfully.")
63
64 myAdxl.standby() # Must be in standby before changing settings.
65 # This is here just in case we already had sensor powered and/or
66 # configured from a previous setup.
67
68 myAdxl.setRange(myAdxl.ADXL313_RANGE_4_G)
69
70 # setup activity sensing options
71 myAdxl.setActivityX(True) # enable x-axis participation in detecting activity
72 myAdxl.setActivityY(False) # disable y-axis participation in detecting activity
73 myAdxl.setActivityZ(False) # disable z-axis participation in detecting activity
74 myAdxl.setActivityThreshold(10) # 0-255 (62.5mg/LSB)
75
76 # setup inactivity sensing options
77 myAdxl.setInactivityX(True) # enable x-axis participation in detecting inactivity
78 myAdxl.setInactivityY(False) # disable y-axis participation in detecting inactivity
79 myAdxl.setInactivityZ(False) # disable z-axis participation in detecting inactivity
80 myAdxl.setInactivityThreshold(10) # 0-255 (62.5mg/LSB)
81 myAdxl.setTimeInactivity(5) # 0-255 (1sec/LSB)
82
83 myAdxl.ActivityINT(1)
84 myAdxl.InactivityINT(1)
85
86 myAdxl.autosleepOn()
87
88 myAdxl.measureModeOn()
89
90 while True:
91 myAdxl.updateIntSourceStatuses(); # this will update all INTSOURCE statuses.
92
93 if myAdxl.ADXL313_INTSOURCE_INACTIVITY:
94 print("Inactivity detected.")
95 time.sleep(1)
96 if myAdxl.ADXL313_INTSOURCE_DATAREADY:
97 myAdxl.readAccel() # read all axis from sensor, note this also updates all instance variables
98 print(\
99 '{: 06d}'.format(myAdxl.x)\
100 , '\t', '{: 06d}'.format(myAdxl.y)\
101 , '\t', '{: 06d}'.format(myAdxl.z)\
102 )
103 else:
104 print("Device is asleep (dataReady is reading false)")
105 time.sleep(0.05)
106
107if __name__ == '__main__':
108 try:
109 runExample()
110 except (KeyboardInterrupt, SystemExit) as exErr:
111 print("\nEnding Example 1")
112 sys.exit(0)
113
114