From 17a9b328e7d4af09f13e6420b6265c864d8246a1 Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Tue, 4 May 2021 21:21:23 -0400 Subject: [PATCH 1/5] advance_example --- docs/examples.rst | 10 +++ examples/fakerequests_advancedtest.py | 66 +++++++++++++++++++ examples/fakerequests_i2c_database.txt | 14 ++++ .../fakerequests_i2c_database.txt.license | 3 + 4 files changed, 93 insertions(+) create mode 100644 examples/fakerequests_advancedtest.py create mode 100644 examples/fakerequests_i2c_database.txt create mode 100644 examples/fakerequests_i2c_database.txt.license diff --git a/docs/examples.rst b/docs/examples.rst index c3fe9c1..401ad19 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -6,3 +6,13 @@ Ensure your device works with this simple test. .. literalinclude:: ../examples/fakerequests_simpletest.py :caption: examples/fakerequests_simpletest.py :linenos: + +Advanced I2C test +----------------- + +Uses the fakerequests capabilities to create a small I2C temperature sensors database, and +look for the correct one.I + +.. literalinclude:: ../examples/fakerequests_advancedtest.py + :caption: examples/examples/fakerequests_advancedtest.py + :linenos: diff --git a/examples/fakerequests_advancedtest.py b/examples/fakerequests_advancedtest.py new file mode 100644 index 0000000..33ad7de --- /dev/null +++ b/examples/fakerequests_advancedtest.py @@ -0,0 +1,66 @@ +# SPDX-FileCopyrightText: 2021 Jose David M +# +# SPDX-License-Identifier: Unlicense +""" +Example showing the use of Fake_requests to access a Temperature Sensor information +Database. Inspired on the I2C buddy and a Discussion with Hugo Dahl +""" +import board +from adafruit_fakerequests import Fake_Requests + +# Create the fakerequest request and get the temperature sensor definitions +# It will look through the database and print the name of the sensor and +# the temperature +response = Fake_Requests("fakerequests_i2c_database.txt") +definitions = response.text.split("\n") + +# We create the i2c object and set a flag to let us know if the sensor is found +found = False +i2c = board.I2C() + +# We look for all the sensor address and added to a list +print("Looking for addresses") +i2c.unlock() # used here, to avoid problems with the I2C bus +i2c.try_lock() +sensor_address = int(i2c.scan()[-1]) +print("Sensor address is:", hex(sensor_address)) +i2c.unlock() # unlock the bus + +# Create an empty list for the sensors found in the database +sensor_choices = list() + +# Compare the sensor found vs the database. this is done because +# we could have the case that the same address corresponds to +# two or more temperature sensors +for sensor in definitions: + elements = sensor.split(",") + if int(elements[0]) == sensor_address: + sensor_choices.append(sensor) + +# This is the main logic to found the sensor and try to +# initiate it. It would raise some exceptions depending +# on the situation. As an example this is not perfect +# and only serves to show the library capabilities +# and nothing more +for find_sensor in sensor_choices: + module = find_sensor.split(",") + package = module[2] + class_name = str(module[3]).strip(" ") + try: + module = __import__(package) + variable = getattr(module, class_name) + try: + sensor = variable(i2c) + print( + f"The sensor {class_name} gives a temperature of {sensor.temperature} Celsius" + ) + found = True + except ValueError: + pass + except ImportError: + raise Exception(f"Could not find the module {package} in your lib folder.") + +if found: + print("Congratulations") +else: + print("We could not find a valid Temperature Sensor") diff --git a/examples/fakerequests_i2c_database.txt b/examples/fakerequests_i2c_database.txt new file mode 100644 index 0000000..e2b73f4 --- /dev/null +++ b/examples/fakerequests_i2c_database.txt @@ -0,0 +1,14 @@ +0x18,MCP9808 temp sensor,adafruit_mcp9808,MCP9808 +0x37,PCT2075 Temperature Sensor,adafruit_pct2075,PCT2075 +0x40,Si7021 Humidity/Temp sensor,adafruit_si7021,SI7021 +0x40,HTU21D-F Humidity/Temp Sensor,adafruit_htu21d,HTU21D +0x40,HTU31D breakout,adafruit_htu31d,HTU31D +0x44,SHT31 Humidity/Temp sensor,adafruit_shtc3,SHTC3 +0x38,AHT20 Temperature & Humidity Sensor,adafruit_ahtx0,AHTx0 +0x5C,AM2320 Humidity/Temp sensor,adafruit_am2320,AM2320 +0x44,SHT4x temperature and humidity sensors,adafruit_sht4x, SHT4x +0x70,SHTC3 Humidity and Temperature Sensor,adafruit_shtc3,SHTC3 +0x48,TMP117 Temperature sensor,adafruit_tmp117,TMP117 +0x60,MPL115A2 I2C Barometric Pressure/Temperature Sensor,adafruit_mpl115a2,MPL115A2 +0x48,TC74 Digital Temperature Sensor,adafruit_tc74,TC74 +0x48,ADT7410 analog temperature Sensor,adafruit_adt7410,ADT7410 diff --git a/examples/fakerequests_i2c_database.txt.license b/examples/fakerequests_i2c_database.txt.license new file mode 100644 index 0000000..1d84276 --- /dev/null +++ b/examples/fakerequests_i2c_database.txt.license @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2021 Jose David M +# +# SPDX-License-Identifier: Unlicense From a43310297de9bedfa8a0039c54b1c86520c097e0 Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Tue, 4 May 2021 21:34:22 -0400 Subject: [PATCH 2/5] using new exception definition --- examples/fakerequests_advancedtest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/fakerequests_advancedtest.py b/examples/fakerequests_advancedtest.py index 33ad7de..f2c5f86 100644 --- a/examples/fakerequests_advancedtest.py +++ b/examples/fakerequests_advancedtest.py @@ -57,8 +57,8 @@ found = True except ValueError: pass - except ImportError: - raise Exception(f"Could not find the module {package} in your lib folder.") + except Exception as e: + raise ImportError(f"Could not find the module {package} in your lib folder.") from e if found: print("Congratulations") From 96f4212dbe108f5eeb9818df9bae8b425819397e Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Tue, 4 May 2021 21:39:21 -0400 Subject: [PATCH 3/5] black_after_the corrections_on_try_except --- examples/fakerequests_advancedtest.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/fakerequests_advancedtest.py b/examples/fakerequests_advancedtest.py index f2c5f86..f4aa9c9 100644 --- a/examples/fakerequests_advancedtest.py +++ b/examples/fakerequests_advancedtest.py @@ -58,7 +58,9 @@ except ValueError: pass except Exception as e: - raise ImportError(f"Could not find the module {package} in your lib folder.") from e + raise ImportError( + f"Could not find the module {package} in your lib folder." + ) from e if found: print("Congratulations") From 99973422acc000f3263c7cf29c6f11f44cfb5a87 Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Tue, 4 May 2021 22:49:28 -0400 Subject: [PATCH 4/5] correcting_link_in_example.rst --- docs/examples.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples.rst b/docs/examples.rst index 401ad19..493d1c4 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -14,5 +14,5 @@ Uses the fakerequests capabilities to create a small I2C temperature sensors dat look for the correct one.I .. literalinclude:: ../examples/fakerequests_advancedtest.py - :caption: examples/examples/fakerequests_advancedtest.py + :caption: examples/fakerequests_advancedtest.py :linenos: From ceb349e1daa059fc50bc3f67a0a7750fdcc3265b Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Fri, 7 May 2021 14:02:01 -0400 Subject: [PATCH 5/5] removing f-strings --- examples/fakerequests_advancedtest.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/fakerequests_advancedtest.py b/examples/fakerequests_advancedtest.py index f4aa9c9..af65ae5 100644 --- a/examples/fakerequests_advancedtest.py +++ b/examples/fakerequests_advancedtest.py @@ -52,14 +52,16 @@ try: sensor = variable(i2c) print( - f"The sensor {class_name} gives a temperature of {sensor.temperature} Celsius" + "The sensor {} gives a temperature of {} Celsius".format( + class_name, sensor.temperature + ) ) found = True except ValueError: pass except Exception as e: raise ImportError( - f"Could not find the module {package} in your lib folder." + "Could not find the module {} in your lib folder.".format(package) ) from e if found: