@@ -63,28 +63,35 @@ def __init__(self, data_pin, units_pin, timeout=1.0):
63
63
self .units_pin = DigitalInOut (units_pin )
64
64
self .units_pin .switch_to_output ()
65
65
self .dymo = PulseIn (data_pin , maxlen = 96 , idle_state = True )
66
- # units we're measuring
67
- self .units = None
68
- # is the measurement stable?
69
66
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
72
80
73
- def toggle_unit_button (self , switch_unit = False ):
81
+ def toggle_unit_button (self , switch_units = False ):
74
82
"""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.
76
84
"""
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
82
88
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
88
95
89
96
def get_scale_data (self ):
90
97
"""Read a pulse of SPI data on a pin that corresponds to DYMO scale
@@ -125,18 +132,17 @@ def get_scale_data(self):
125
132
if data_bytes [8 ] != 0x1C or data_bytes [9 ] != 0 or data_bytes [10 ] \
126
133
or data_bytes [11 ] != 0 :
127
134
raise RuntimeError ("Bad data capture" )
135
+
128
136
# parse out the data_bytes
129
137
self .stable = data_bytes [2 ] & 0x4
130
138
self .units = data_bytes [3 ]
131
- self . weight = data_bytes [5 ] + (data_bytes [6 ] << 8 )
139
+ weight = data_bytes [5 ] + (data_bytes [6 ] << 8 )
132
140
if data_bytes [2 ] & 0x1 :
133
- self . weight *= - 1
141
+ weight *= - 1
134
142
print ('Tare - press the tare button to reset the scale to zero.' )
135
143
if self .units == OUNCES :
136
144
if data_bytes [4 ] & 0x80 :
137
145
data_bytes [4 ] -= 0x100
138
146
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