Created
May 28, 2019 15:49
-
-
Save rosterloh/ce6420058fa2bb82a3bd45fc52865f5a to your computer and use it in GitHub Desktop.
CircuitPlayground Express Current Logger
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
# boot.py | |
# Set Circuit Playground Express flash chip to program writeable | |
# If toggle switch is right, | |
# flash is program writeable and file access is frozen | |
# If toggle switch is left, | |
# flash chip file access ok, file writes give an error | |
# via Dan Conley | |
# https://learn.adafruit.com/cpu-temperature-logging-with-circuit-python/ | |
# writing-to-the-filesystem | |
# 2018 Mike Barela for Getting Started with Circuit Playground Express | |
import storage | |
from adafruit_circuitplayground.express import cpx | |
storage.remount("/", cpx.switch) |
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
""" | |
File based message handler for CircuitPython logging. | |
Adafruit invests time and resources providing this open source code. | |
Please support Adafruit and open source hardware by purchasing | |
products from Adafruit! | |
Written by Dave Astels for Adafruit Industries | |
Copyright (c) 2018 Adafruit Industries | |
Licensed under the MIT license. | |
All text above must be included in any redistribution. | |
""" | |
#pylint:disable=missing-super-argument | |
# Example: | |
# | |
# | |
# from file_handler import FileHandler | |
# import adafruit_logging as logging | |
# l = logging.getLogger('file') | |
# l.addHandler(FileHandler('log.txt')) | |
# l.level = logging.ERROR | |
# l.error("test") | |
from adafruit_logging import LoggingHandler | |
from adafruit_circuitplayground.express import cpx | |
import time | |
class FileHandler(LoggingHandler): | |
def __init__(self, filename): | |
"""Create an instance. | |
:param filename: the name of the file to which to write messages | |
""" | |
self._filename = filename | |
self._log_buffer = [] | |
# Clear the file and add the header | |
with open(filename, 'w') as f: | |
f.write("Time (s), Current (mA)\n") | |
def format(self, level, msg): | |
"""Generate a string to log. | |
:param level: The level at which to log | |
:param msg: The core message | |
""" | |
return str(time.monotonic()) + "," + str(msg) + "\n" | |
def emit(self, level, msg): | |
"""Generate the message and write it to the File. | |
:param level: The level at which to log | |
:param msg: The core message | |
""" | |
if len(self._log_buffer) == 10: | |
try: | |
with open(self._filename, 'a') as f: | |
while len(self._log_buffer) > 0: | |
f.write(self._log_buffer.pop(0)) | |
except OSError as e: | |
# set NeoPixel 1 off and blink NeoPixel 0 (status) depending on | |
# the OS error | |
cpx.pixels[1] = (0, 0, 0) # Blank NeoPixel 1 | |
message_color = (99, 0, 0) # Red for generic problem | |
if e.args[0] == 28: # Device out of space | |
message_color = (228, 160, 40) # set to Orange | |
elif e.args[0] == 30: # Device is read only | |
message_color = (181, 90, 0) # set to Yellow | |
for x in range(1, 10): # Flash message 10 seconds | |
cpx.pixels[0] = message_color | |
time.sleep(1) | |
cpx.pixels[0] = (0, 0, 0) | |
time.sleep(1) | |
else: | |
self._log_buffer.append(self.format(level, msg)) |
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
import time | |
import board | |
from adafruit_ina260 import INA260, Mode | |
from adafruit_circuitplayground.express import cpx | |
from adafruit_debouncer import Debouncer | |
from file_handler import FileHandler | |
import adafruit_logging | |
i2c = board.I2C() | |
ina260 = INA260(i2c) | |
max_i = .0 | |
btn_a = Debouncer(cpx._a) | |
btn_b = Debouncer(cpx._b) | |
if cpx.switch: | |
# Green = Writable | |
cpx.pixels[9] = (0, 1, 0) | |
else: | |
# Red = Read-Only | |
cpx.pixels[9] = (1, 0, 0) | |
logging = False | |
if not cpx.switch: | |
l = adafruit_logging.getLogger('file') | |
l.addHandler(FileHandler('current_log.csv')) | |
l.level = adafruit_logging.INFO | |
while True: | |
btn_a.update() | |
if btn_a.fell and not logging: | |
cpx.red_led = True | |
logging = True | |
btn_b.update() | |
if btn_b.fell and logging: | |
cpx.red_led = False | |
logging = False | |
if logging: | |
ina260.mode = Mode.TRIGGERED | |
i = ina260.current | |
if not cpx.switch: | |
# Write to CSV | |
l.info("%.4f" % i) | |
else: | |
# Use Mu Plotter | |
print((i, max_i, time.monotonic())) | |
if i > max_i: | |
max_i = i | |
#time.sleep(0.001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment