Skip to content

Commit 20a8b45

Browse files
author
brentru
committed
add strict check for header MSB, only toggle the units pin every 2 minutes
1 parent 20b77c4 commit 20a8b45

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

adafruit_dymoscale.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ def get_scale_data(self):
133133
the_byte |= bits[byte_n*8 + bit_n]
134134
data_bytes[byte_n] = the_byte
135135
# do some very basic data checking
136-
if data_bytes[0] != 3 or data_bytes[1] != 3 or data_bytes[7] != 4:
136+
if data_bytes[0] != 3 and data_bytes[0] != 2: # check the MSB (differs between DYMO scale models)
137137
raise RuntimeError("Bad data capture")
138-
139-
if data_bytes[8] != 0x1C or data_bytes[9] != 0 or data_bytes[10] \
140-
or data_bytes[11] != 0:
138+
if data_bytes[1] != 3 or data_bytes[7] != 4 or data_bytes[8] != 0x1C:
139+
raise RuntimeError("Bad data capture")
140+
if data_bytes[9] != 0 or data_bytes[10] or data_bytes[11] != 0:
141141
raise RuntimeError("Bad data capture")
142142
reading = ScaleReading()
143143
# parse out the data_bytes

examples/dymoscale_simpletest.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
import board
23
import digitalio
34
import adafruit_dymoscale
@@ -7,13 +8,16 @@
78
units_pin.switch_to_output()
89
dymo = adafruit_dymoscale.DYMOScale(board.D4, units_pin)
910

11+
# take a reading of the current time
12+
time_stamp = time.monotonic()
13+
1014
while True:
1115
reading = dymo.weight
1216
text = "{} g".format(reading.weight)
1317
print(text)
14-
# to avoid sleep mode, we'll toggle the units pin.
15-
# if we don't want to switch the units on the next read...
16-
dymo.toggle_unit_button()
17-
18-
# if we do want to switch the units on the next read...
19-
# dymo.toggle_unit_button(switch_units=True)
18+
# to avoid sleep mode, toggle the units pin every 2 mins.
19+
if (time.monotonic() - time_stamp) > 120:
20+
print('toggling units button...')
21+
dymo.toggle_unit_button()
22+
# reset the time
23+
time_stamp = time.monotonic()

0 commit comments

Comments
 (0)