Skip to content

Commit f00f9ca

Browse files
author
brentru
committed
put back object for scale data, changed class names, removed digitalio init from code, returns in GRAMS
1 parent 0298400 commit f00f9ca

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

adafruit_dymoscale.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939

4040
import time
4141
from pulseio import PulseIn
42-
from digitalio import DigitalInOut
4342
from micropython import const
4443

4544
OUNCES = const(0x0B) # data in weight is in ounces
@@ -49,7 +48,14 @@
4948
__version__ = "0.0.0-auto.0"
5049
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_scale.git"
5150

52-
class Scale:
51+
# pylint: disable=too-few-public-methods
52+
class ScaleReading:
53+
"""Dymo Scale Data"""
54+
units = None # what units we're measuring
55+
stable = None # is the measurement stable?
56+
weight = None # the weight!
57+
58+
class DYMOScale:
5359
"""Interface to a DYMO postal scale.
5460
"""
5561
def __init__(self, data_pin, units_pin, timeout=1.0):
@@ -60,19 +66,18 @@ def __init__(self, data_pin, units_pin, timeout=1.0):
6066
"""
6167
self.timeout = timeout
6268
# set up the toggle pin
63-
self.units_pin = DigitalInOut(units_pin)
64-
self.units_pin.switch_to_output()
69+
self.units_pin = units_pin
70+
# set up the dymo data pin
6571
self.dymo = PulseIn(data_pin, maxlen=96, idle_state=True)
66-
self.tare = False
67-
self.stable = None
68-
self.units = None
6972

7073
@property
7174
def weight(self):
7275
"""Weight in grams"""
73-
weight = self.get_scale_data()
74-
self.units = 'g'
75-
return weight
76+
reading = self.get_scale_data()
77+
if reading.units == OUNCES:
78+
reading.weight *= 28.35
79+
reading.units = GRAMS
80+
return reading
7681

7782
def toggle_unit_button(self, switch_units=False):
7883
"""Toggles the unit button on the dymo.
@@ -134,17 +139,15 @@ def get_scale_data(self):
134139
if data_bytes[8] != 0x1C or data_bytes[9] != 0 or data_bytes[10] \
135140
or data_bytes[11] != 0:
136141
raise RuntimeError("Bad data capture")
137-
142+
reading = ScaleReading()
138143
# parse out the data_bytes
139-
self.stable = data_bytes[2] & 0x4
140-
self.units = data_bytes[3]
141-
weight = data_bytes[5] + (data_bytes[6] << 8)
144+
reading.stable = data_bytes[2] & 0x4
145+
reading.units = data_bytes[3]
146+
reading.weight = data_bytes[5] + (data_bytes[6] << 8)
142147
if data_bytes[2] & 0x1:
143-
self.tare = True
144-
weight *= -1
145-
elif self.units == OUNCES:
148+
reading.weight *= -1
149+
if reading.units == OUNCES:
146150
if data_bytes[4] & 0x80:
147-
self.tare = True
148151
data_bytes[4] -= 0x100
149-
#weight *= 10 ** data_bytes[4]
150-
return weight
152+
reading.weight *= 10 ** data_bytes[4]
153+
return reading

examples/dymoscale_simpletest.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import board
2-
from adafruit_dymoscale import Scale
2+
import digitalio
3+
import adafruit_dymoscale
34

45
# initialize the dymo scale
5-
dymo = Scale(board.D3, board.D4)
6+
units_pin = digitalio.DigitalInOut(board.D3)
7+
units_pin.switch_to_output()
8+
dymo = adafruit_dymoscale.DYMOScale(board.D4, units_pin)
69

710
while True:
8-
print("{:0.1f} {}".format(dymo.weight, dymo.units))
11+
reading = dymo.weight
12+
text = "{} g".format(reading.weight)
13+
print(text)
914
# to avoid sleep mode, we'll toggle the units pin.
1015
# if we don't want to switch the units on the next read...
1116
dymo.toggle_unit_button()

0 commit comments

Comments
 (0)