-
Notifications
You must be signed in to change notification settings - Fork 25
Fixes Issue#5 and use adafruit_register #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
33e0755
- Fix values for _CONFIG_BADCRES and add missing values
barbudor ee35938
Update sample to use new properties and clean-up printed output (alig…
barbudor bb439c1
fix pylint errors
barbudor 8ecba9f
add 1 comment
barbudor c024b6a
Clean-up and improvements
barbudor c2e1e32
Clean-up and improvements
barbudor 938995d
fixing pylint errors
barbudor 9774ba3
Removed useless `class Reset`
barbudor 3056c2c
removed un-necessary `const()` keyword
barbudor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,42 @@ | ||
import time | ||
"""Sample code and test for adafruit_in219""" | ||
|
||
from board import SCL, SDA | ||
import busio | ||
import time | ||
import board | ||
from adafruit_ina219 import ADCResolution, BusVoltageRange, INA219 | ||
|
||
import adafruit_ina219 | ||
|
||
i2c_bus = busio.I2C(SCL, SDA) | ||
i2c_bus = board.I2C() | ||
|
||
ina219 = adafruit_ina219.INA219(i2c_bus) | ||
ina219 = INA219(i2c_bus) | ||
|
||
print("ina219 test") | ||
|
||
# display some of the advanced field (just to test) | ||
print("Config register:") | ||
print(" bus_voltage_range: 0x%1X" % ina219.bus_voltage_range) | ||
print(" gain: 0x%1X" % ina219.gain) | ||
print(" bus_adc_resolution: 0x%1X" % ina219.bus_adc_resolution) | ||
print(" shunt_adc_resolution: 0x%1X" % ina219.shunt_adc_resolution) | ||
print(" mode: 0x%1X" % ina219.mode) | ||
print("") | ||
|
||
# optional : change configuration to use 32 samples averaging for both bus voltage and shunt voltage | ||
ina219.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S | ||
ina219.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S | ||
# optional : change voltage range to 16V | ||
ina219.bus_voltage_range = BusVoltageRange.RANGE_16V | ||
|
||
# measure and display loop | ||
while True: | ||
print("Bus Voltage: {} V".format(ina219.bus_voltage)) | ||
print("Shunt Voltage: {} mV".format(ina219.shunt_voltage / 1000)) | ||
print("Load Voltage: {} V".format(ina219.bus_voltage + ina219.shunt_voltage)) | ||
print("Current: {} mA".format(ina219.current)) | ||
bus_voltage = ina219.bus_voltage # voltage on V- (load side) | ||
shunt_voltage = ina219.shunt_voltage # voltage between V+ and V- across the shunt | ||
current = ina219.current # current in mA | ||
|
||
# INA219 measure bus voltage on the load side. So PSU voltage = bus_voltage + shunt_voltage | ||
print("PSU Voltage: {:6.3f} V".format(bus_voltage + shunt_voltage)) | ||
print("Shunt Voltage: {:9.6f} V".format(shunt_voltage)) | ||
print("Load Voltage: {:6.3f} V".format(bus_voltage)) | ||
print("Current: {:9.6f} A".format(current/1000)) | ||
print("") | ||
|
||
time.sleep(2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
Adafruit-Blinka | ||
adafruit-circuitpython-busdevice | ||
adafruit-circuitpython-register |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
author='Adafruit Industries', | ||
author_email='[email protected]', | ||
|
||
install_requires=['Adafruit-Blinka', 'adafruit-circuitpython-busdevice'], | ||
install_requires=['Adafruit-Blinka', 'adafruit-circuitpython-busdevice', 'adafruit-circuitpython-register'], | ||
|
||
# Choose your license | ||
license='MIT', | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as its one bit, this could just be True or False instead of a custom class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it doesn't make any sense to use
const()
inside classes — it only works with global variables.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ladyada, indeed this one is useless and should be removed. I will do it as soon as no more review is expected.
Hi @deshipu, I'm dupplicating what I've seen in IN260 : https://github.com/adafruit/Adafruit_CircuitPython_INA260/blob/master/adafruit_ina260.py
Using classes for constant allows to use meaning full names such as
Mode.CONTINUOUS
I believe this is the same as
DigitalInOut.Direction.OUTPUT
My understanding is that a
const()
will use less memory than a variable.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it makes sense to keep this one too ?
class BusVoltageRange:
"""Constants for bus_voltage_range"""
RANGE_16V = const(0x00) # set bus voltage range to 16V
RANGE_32V = const(0x01) # set bus voltage range to 32V (default)
May be renaming
bus_voltage_range
asbus_voltage_range_32V
and useFalse
/True
?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@barbudor
const()
works by replacing every occurrence of a variable with the number itself before compiling the code. It only works with globals, because otherwise it would need to know the runtime context of the variable to be able to tell if it's really this variable or some other local variable that just happens to be named the same. It makes the code slightly faster by avoiding variable lookup.Since the global variable still needs to be created, so that it can be accessed by any other modules that import this module, it doesn't use less memory. For it to use less memory, you would need to: 1. use global variables, as already mentioned, 2. name them with an underscore as the first character, so that they can't be used by external modules. Then, and only then, it makes sense to use
const()
. Otherwise I would recommend removing it to avoid confusion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More details: micropython/micropython#573