39
39
40
40
import time
41
41
from pulseio import PulseIn
42
- from digitalio import DigitalInOut
43
42
from micropython import const
44
43
45
44
OUNCES = const (0x0B ) # data in weight is in ounces
49
48
__version__ = "0.0.0-auto.0"
50
49
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_scale.git"
51
50
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 :
53
59
"""Interface to a DYMO postal scale.
54
60
"""
55
61
def __init__ (self , data_pin , units_pin , timeout = 1.0 ):
@@ -60,19 +66,18 @@ def __init__(self, data_pin, units_pin, timeout=1.0):
60
66
"""
61
67
self .timeout = timeout
62
68
# 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
65
71
self .dymo = PulseIn (data_pin , maxlen = 96 , idle_state = True )
66
- self .tare = False
67
- self .stable = None
68
- self .units = None
69
72
70
73
@property
71
74
def weight (self ):
72
75
"""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
76
81
77
82
def toggle_unit_button (self , switch_units = False ):
78
83
"""Toggles the unit button on the dymo.
@@ -134,17 +139,15 @@ def get_scale_data(self):
134
139
if data_bytes [8 ] != 0x1C or data_bytes [9 ] != 0 or data_bytes [10 ] \
135
140
or data_bytes [11 ] != 0 :
136
141
raise RuntimeError ("Bad data capture" )
137
-
142
+ reading = ScaleReading ()
138
143
# 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 )
142
147
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 :
146
150
if data_bytes [4 ] & 0x80 :
147
- self .tare = True
148
151
data_bytes [4 ] -= 0x100
149
- # weight *= 10 ** data_bytes[4]
150
- return weight
152
+ reading . weight *= 10 ** data_bytes [4 ]
153
+ return reading
0 commit comments