Skip to content

Update to support latest version of logging.py #14

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,28 @@ Detailed logging of device register operations can be enabled with:
ina = INA219(SHUNT_OHMS, I2C(2), log_level=logging.DEBUG)
```

## Unit Testing

Some basic unit tests are provided which do not require an ina219 sensor to be connected.

Copy all the files in the _test_ and _unittest_ directories to matching directories on the device. From the REPL prompt run the tests with:
```python
import unittest
unittest.main("tests")
```
The result should be:
```
test_default (tests.TestConstructor) ... ok
test_with_max_expected_amps (tests.TestConstructor) ... ok
test_read_32v (tests.TestRead) ... ok
test_read_16v (tests.TestRead) ... ok
test_read_4_808v (tests.TestRead) ... ok
----------------------------------------------------------------------
Ran 5 tests

OK
<unittest.result.TestResult run=5 errors=0 failures=0>
```
## Coding Standard

This library adheres to the *PEP8* standard and follows the *idiomatic*
Expand Down
5 changes: 3 additions & 2 deletions esp32/example.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Example script for ESP32."""
from machine import Pin, I2C
from ina219 import INA219
from logging import INFO

SHUNT_OHMS = 0.1

i2c = I2C(-1, Pin(17), Pin(16))
i2c = I2C(1, scl=Pin(16), sda=Pin(17))
ina = INA219(SHUNT_OHMS, i2c, log_level=INFO)
ina.configure()

print("Bus Voltage: %.3f V" % ina.voltage())
print("Current: %.3f mA" % ina.current())
print("Power: %.3f mW" % ina.power())
print("Power: %.3f mW" % ina.power())
5 changes: 3 additions & 2 deletions esp8266/example.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Example script for ESP32."""
from machine import Pin, I2C
from ina219 import INA219
from logging import INFO

SHUNT_OHMS = 0.1

i2c = I2C(-1, Pin(5), Pin(4))
i2c = I2C(Pin(5), Pin(4))
ina = INA219(SHUNT_OHMS, i2c, log_level=INFO)
ina.configure()

print("Bus Voltage: %.3f V" % ina.voltage())
print("Current: %.3f mA" % ina.current())
print("Power: %.3f mW" % ina.power())
print("Power: %.3f mW" % ina.power())
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Example script.
"""Example script for Pyboard v1.1.

Edit the I2C interface constant to match the one you have
connected the sensor to.
Expand Down
6 changes: 4 additions & 2 deletions ina219.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def __init__(self, shunt_ohms, i2c, max_expected_amps=None,
calculations (optional).
"""
logging.basicConfig(level=log_level)
self._log = logging.getLogger("ina219")
self._log = logging.getLogger(__name__)
self._log.setLevel(log_level)

self._i2c = i2c
self._address = address
self._shunt_ohms = shunt_ohms
Expand Down Expand Up @@ -406,7 +408,7 @@ def __read_register(self, register, negative_value_supported=False):

def __log_register_operation(self, msg, register, value):
# performance optimisation
if logging._level == logging.DEBUG:
if self._log.isEnabledFor(logging.DEBUG):
binary = '{0:#018b}'.format(value)
self._log.debug("%s register 0x%02x: 0x%04x %s",
msg, register, value, binary)
Expand Down
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .test_constructor import *
from .test_read import *
1 change: 1 addition & 0 deletions unittest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory contains a copy of the standard Micropython unittest module [\_\_init\_\_.py](https://github.com/micropython/micropython-lib/blob/master/python-stdlib/unittest/unittest/__init__.py).
Loading