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