@@ -63,28 +63,35 @@ def __init__(self, data_pin, units_pin, timeout=1.0):
6363 self .units_pin = DigitalInOut (units_pin )
6464 self .units_pin .switch_to_output ()
6565 self .dymo = PulseIn (data_pin , maxlen = 96 , idle_state = True )
66- # units we're measuring
67- self .units = None
68- # is the measurement stable?
6966 self .stable = None
70- # the weight of what we're measuring
71- self .weight = None
67+ self .units = None
68+
69+ @property
70+ def weight (self ):
71+ """Weight in grams"""
72+ weight = self .get_scale_data ()
73+ #print('debugg: ', weight)
74+ if self .units == OUNCES :
75+ weight = weight * 28.35
76+ self .units = 'oz'
77+ elif self .units == GRAMS :
78+ self .units = 'g'
79+ return weight
7280
73- def toggle_unit_button (self , switch_unit = False ):
81+ def toggle_unit_button (self , switch_units = False ):
7482 """Toggles the unit button on the dymo.
75- :param bool switch_unit : Simulates pressing the unit button.
83+ :param bool switch_units : Simulates pressing the units button.
7684 """
77- if switch_unit : # press the toggle button once
78- self .units_pin .value = 1
79- time .sleep (2 )
80- self .units_pin .value = 0
81- time .sleep (2 )
85+ toggle_times = 0
86+ if switch_units : # press the button once
87+ toggle_amt = 2
8288 else : # toggle and preserve current unit state
83- for toggle in range (2 ):
84- self .units_pin .value = 1
85- time .sleep (2 )
86- self .units_pin .value = 0
87- time .sleep (2 )
89+ toggle_amt = 4
90+ while toggle_times < toggle_amt :
91+ self .units_pin .value ^= 1
92+ print ('toggle: ' , self .units_pin .value )
93+ time .sleep (2 )
94+ toggle_times += 1
8895
8996 def get_scale_data (self ):
9097 """Read a pulse of SPI data on a pin that corresponds to DYMO scale
@@ -125,18 +132,17 @@ def get_scale_data(self):
125132 if data_bytes [8 ] != 0x1C or data_bytes [9 ] != 0 or data_bytes [10 ] \
126133 or data_bytes [11 ] != 0 :
127134 raise RuntimeError ("Bad data capture" )
135+
128136 # parse out the data_bytes
129137 self .stable = data_bytes [2 ] & 0x4
130138 self .units = data_bytes [3 ]
131- self . weight = data_bytes [5 ] + (data_bytes [6 ] << 8 )
139+ weight = data_bytes [5 ] + (data_bytes [6 ] << 8 )
132140 if data_bytes [2 ] & 0x1 :
133- self . weight *= - 1
141+ weight *= - 1
134142 print ('Tare - press the tare button to reset the scale to zero.' )
135143 if self .units == OUNCES :
136144 if data_bytes [4 ] & 0x80 :
137145 data_bytes [4 ] -= 0x100
138146 print ('Tare - press the tare button to reset the scale to zero.' )
139- self .weight *= 10 ** data_bytes [4 ]
140- self .units = "oz"
141- elif self .units == GRAMS :
142- self .units = "g"
147+ weight *= 10 ** data_bytes [4 ]
148+ return weight
0 commit comments