1#!/usr/bin/env python
2#-----------------------------------------------------------------------------
3# Example_02_SingleShot.py
4#
5# Simple Example for the Qwiic AS6212 Device
6#
7# This example uses the Single Shot Feature of the device.
8# It puts the sensor into sleep mode, and then in order to take
9# each reading, it calls the trigger_single_shot_conversion() function.
10# This allows us to take single readings on demand and really
11# keep power use to a minimum.
12#
13# Note, in the basic readings example, we are "waking up" the sensor
14# (by turning sleep mode off), and then it enters continuous reading mode,
15# and so the sensor will continue to make conversions at the set conversion cycle time (4Hz).
16# This uses more power, but can be useful if you want to setup an alert, and can
17# be even finer tuned by setting up the amount of desired consecutive faults.
18#
19# Note, using single shot readings like in this example, can also
20# allow you to poll the SS bit (and know when the conversion is complete)
21# SS bit = 0 = No conversion ongoing / conversion finished
22# SS bit = 1 = Start single shot conversion / conversion ongoing
23# This can allow you to immediate start another conversion, and increase
24# the amount of conversions you demand.
25#
26# As the device exhibits a very short conversion time (~36ms-51ms), the effective conversion
27# rate can be increased by setting the single shot bit repetitively after a conversion has finished.
28# However, it has to be ensured that the additional power is limited, otherwise self-heating
29# effects have to be considered.
30#
31#------------------------------------------------------------------------
32#
33# Written by Pete Lewis, SparkFun Electronics, Aug 2021
34#
35# Thanks to Alex Wende and Lori Croster @ SparkFun Electronics
36# for code examples from TMP102 Python Package, May 2021
37# (https://github.com/sparkfun/Qwiic_TMP102_Py)
38#
39# Thanks to Brandon Williams. This library was based off his
40# original library created 07/15/2020 and can be found here:
41# https://github.com/will2055/AS6212-Arduino-Library/
42#
43# Thanks to Madison Chodikov @ SparkFun Electronics
44# for code examples from TMP117 Arduino Library
45# (https://github.com/sparkfun/SparkFun_TMP117_Arduino_Library)
46#
47# This python library supports the SparkFun Electroncis qwiic
48# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
49# board computers.
50#
51# More information on qwiic is at https://www.sparkfun.com/qwiic
52#
53# Do you like this library? Help support SparkFun. Buy a board!
54#
55#==================================================================================
56# Copyright (c) 2021 SparkFun Electronics
57#
58# Permission is hereby granted, free of charge, to any person obtaining a copy
59# of this software and associated documentation files (the "Software"), to deal
60# in the Software without restriction, including without limitation the rights
61# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
62# copies of the Software, and to permit persons to whom the Software is
63# furnished to do so, subject to the following conditions:
64#
65# The above copyright notice and this permission notice shall be included in all
66# copies or substantial portions of the Software.
67#
68# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
69# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
70# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
71# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
72# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
73# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
74# SOFTWARE.
75#==================================================================================
76# Example 2
77#
78
79from __future__ import print_function
80import qwiic_as6212
81import time
82import sys
83
84def runExample():
85
86 print("\nSparkFun Qwiic AS6212 Sensor Example 2 - Single Shot Readings\n")
87 myTempSensor = qwiic_as6212.QwiicAs6212Sensor()
88
89 if myTempSensor.is_connected == False:
90 print("The Qwiic AS6212 Sensor device isn't connected to the system. Please check your connection", \
91 file=sys.stderr)
92 return
93
94 myTempSensor.begin()
95 time.sleep(1)
96
97 print("Initialized.")
98
99 myTempSensor.set_sleep_mode(1) # turn sleep mode on (1)
100 print("Sleep mode ON")
101 time.sleep(1)
102
103 while True:
104 myTempSensor.trigger_single_shot_conversion() # trigger SS
105
106 #wait for conversion to complete (~51ms)
107 conversionTime = 0
108 while myTempSensor.get_single_shot_status() == 1:
109 conversionTime += 1
110 time.sleep(0.001) # 1ms
111
112 tempF = myTempSensor.read_temp_f()
113
114 print("Temperature: %.2fF \t Conversion time: %ims" % (tempF, conversionTime))
115 time.sleep(1)
116
117if __name__ == '__main__':
118 try:
119 runExample()
120 except (KeyboardInterrupt, SystemExit) as exErr:
121 print("\nEnding Example 1")
122 sys.exit(0)