Skip to content

Commit 9c0101c

Browse files
authored
Merge pull request #3 from fourstix/new_properties
New properties for moved timestamp, pressed timestamp and difference.
2 parents 2c9f697 + 513e7dc commit 9c0101c

File tree

4 files changed

+65
-59
lines changed

4 files changed

+65
-59
lines changed

examples/example5_timestamps.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
21
# This is example is for the SparkFun Qwiic Single Twist.
32
# SparkFun sells these at its website: www.sparkfun.com
43
# Do you like this library? Help support SparkFun. Buy a board!
54
# https://www.sparkfun.com/products/15083
65

76
"""
8-
Qwiic Twist Example 5 - example1_timestampss.py
9-
Written by Gaston Williams, June 19th, 2019
7+
Qwiic Twist Example 5 - example5_timestamps.py
8+
Written by Gaston Williams, June 21st, 2019
109
Based on Arduino code written by
1110
Nathan Seidle @ Sparkfun, December 3rd, 2018
1211
The Qwiic Twist is an I2C controlled RGB Rotary Encoder produced by sparkfun
@@ -48,9 +47,9 @@
4847
while True:
4948
print('Count: ' + str(twist.count))
5049
if twist.moved:
51-
print('Last Twist time: ' + str(twist.time_since_last_movement(False)))
50+
print('Last Twist time: ' + str(twist.time_since_last_movement))
5251
if twist.clicked:
53-
print('Last Button time: ' + str(twist.time_since_last_press(False)))
52+
print('Last Button time: ' + str(twist.time_since_last_press))
5453
if twist.pressed:
5554
print('Pressed!')
5655
sleep(1)

examples/example6_difference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
try:
4747
while True:
4848
print('Count: ' + str(twist.count))
49-
print('Difference: ' + str(twist.get_difference()))
49+
print('Difference: ' + str(twist.difference))
5050
sleep(0.250)
5151

5252
except KeyboardInterrupt:

examples/example7_set_count.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
Qwiic Twist Example 7 - example7_set_count.py
8-
Written by Gaston Williams, June 19th, 2019
8+
Written by Gaston Williams, June 21st, 2019
99
Based on Arduino code written by
1010
Nathan Seidle @ Sparkfun, December 3rd, 2018
1111
The Qwiic Twist is an I2C controlled RGB Rotary Encoder produced by sparkfun
@@ -46,7 +46,7 @@
4646
try:
4747
while True:
4848
print('Count: ' + str(twist.count))
49-
print('Difference: ' + str(twist.get_difference()))
49+
print('Difference: ' + str(twist.difference))
5050
sleep(0.250)
5151

5252
except KeyboardInterrupt:

sparkfun_qwiictwist.py

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
5959
QWIIC_TWIST_ADDR = const(0x3F) # default I2C Address
6060
QWIIC_TWIST_ID = const(0x5c) # value returned by id register
6161

62-
63-
6462
# private constants
6563

6664
# bit constants
@@ -88,6 +86,16 @@
8886
_TWIST_TURN_INT_TIMEOUT = const(0x16)
8987
_TWIST_CHANGE_ADDRESS = const(0x18)
9088

89+
# private functions
90+
def _signed_int16(value):
91+
# convert a 16-bit value into a signed integer
92+
result = value
93+
94+
if result & (1<<15):
95+
result -= 1<<16
96+
97+
return result
98+
9199
# class
92100
class Sparkfun_QwiicTwist:
93101
"""CircuitPython class for the Sparkfun QwiicTwist RGB Rotary Encoder"""
@@ -157,13 +165,53 @@ def clicked(self):
157165

158166
return clicked
159167

168+
@property
169+
def difference(self):
170+
"""
171+
Return the difference in number of clicks since previous check.
172+
The value is cleared after it is read.
173+
"""
174+
value = self._read_register16(_TWIST_DIFFERENCE)
175+
diff = _signed_int16(value)
176+
177+
self._write_register16(_TWIST_DIFFERENCE, 0)
178+
179+
return diff
180+
181+
@property
182+
def time_since_last_movement(self):
183+
"""Return the number of milliseconds since the last encoder movement"""
184+
# unsigned 16-bit value
185+
elapsed_time = self._read_register16(_TWIST_LAST_ENCODER_EVENT)
186+
187+
# This value seems to be cleared regardless
188+
# Clearing it sometimes returns 0 after write, so commenting out
189+
# Clear the current value
190+
# self._write_register16(_TWIST_LAST_ENCODER_EVENT, 0)
191+
192+
return elapsed_time
193+
194+
@property
195+
def time_since_last_press(self):
196+
"""Return the number of milliseconds since the last button press and release"""
197+
# unsigned 16-bit value
198+
elapsed_time = self._read_register16(_TWIST_LAST_BUTTON_EVENT)
199+
200+
# This value seems to be cleared regardless
201+
# Clearing it sometimes returns 0 after write, so commenting out
202+
# Clear the current value if requested
203+
# self._write_register16(_TWIST_LAST_BUTTON_EVENT, 0)
204+
205+
return elapsed_time
206+
207+
160208
# public properties (read-write)
161209

162210
@property
163211
def count(self):
164212
"""Returns the number of indents since the user turned the knob."""
165213
value = self._read_register16(_TWIST_COUNT)
166-
return self._signed_int16(value)
214+
return _signed_int16(value)
167215

168216

169217
@count.setter
@@ -205,7 +253,7 @@ def blue(self, value):
205253
def red_connection(self):
206254
"""Get the value of the red LED connection"""
207255
value = self._read_register16(_TWIST_CONNECT_RED)
208-
return self._signed_int16(value)
256+
return _signed_int16(value)
209257

210258
@red_connection.setter
211259
def red_connection(self, value):
@@ -221,13 +269,13 @@ def green_connection(self):
221269
def green_connection(self, value):
222270
"""Set the value of the green LED connection"""
223271
value = self._write_register16(_TWIST_CONNECT_GREEN, value)
224-
return self._signed_int16(value)
272+
return _signed_int16(value)
225273

226274
@property
227275
def blue_connection(self):
228276
"""Get the value of the blue LED connection."""
229277
value = self._read_register16(_TWIST_CONNECT_BLUE)
230-
return self._signed_int16(value)
278+
return _signed_int16(value)
231279

232280
@blue_connection.setter
233281
def blue_connection(self, value):
@@ -246,35 +294,7 @@ def int_timeout(self, value):
246294
the end of knob turning and interrupt firing."""
247295
self._write_register16(_TWIST_TURN_INT_TIMEOUT, value)
248296

249-
# public functions
250-
251-
def get_difference(self, clear=True):
252-
""""Return the number of clicks since last check."""
253-
value = self._read_register16(_TWIST_DIFFERENCE)
254-
diff = self._signed_int16(value)
255-
256-
if clear:
257-
self._write_register16(_TWIST_DIFFERENCE, 0)
258-
return diff
259-
260-
def time_since_last_movement(self, clear=True):
261-
"""Return the number of milliseconds since the last encoder movement"""
262-
# unsigned 16-bit value
263-
elapsed_time = self._read_register16(_TWIST_LAST_ENCODER_EVENT)
264-
265-
# Clear the current value if requested
266-
if clear:
267-
self._write_register16(_TWIST_LAST_ENCODER_EVENT, 0)
268-
return elapsed_time
269-
270-
def time_since_last_press(self, clear=True):
271-
"""Return the number of milliseconds since the last button press and release"""
272-
# unsigned 16-bit value
273-
elapsed_time = self._read_register16(_TWIST_LAST_BUTTON_EVENT)
274-
# Clear the current value if requested
275-
if clear:
276-
self._write_register16(_TWIST_LAST_BUTTON_EVENT, 0)
277-
return elapsed_time
297+
# public methods
278298

279299
def clear_interrupts(self):
280300
"""Clears the moved, clicked, and pressed bits"""
@@ -318,25 +338,12 @@ def change_address(self, new_address):
318338

319339
# No i2c begin function is needed since I2Cdevice class takes care of that
320340

321-
# private functions
322-
323-
def _signed_int16(self, value):
324-
# convert a 16-bit value into a signed integer
325-
result = value
326-
if self._debug:
327-
print('Unsigned 16-bit value = ' + str(result))
328-
329-
if result & (1<<15):
330-
result -= 1<<16
331-
332-
if self._debug:
333-
print('Signed 16-bit value = ' + str(result))
334-
return result
341+
# private methods
335342

336343
def _read_register8(self, addr):
337344
# Read and return a byte from the specified 8-bit register address.
338345
with self._device as device:
339-
device.write(bytes([addr & 0xFF]))
346+
device.write(bytes([addr & 0xFF]), stop=False)
340347
result = bytearray(1)
341348
device.readinto(result)
342349
if self._debug:
@@ -353,7 +360,7 @@ def _write_register8(self, addr, value):
353360
def _read_register16(self, addr):
354361
# Read and return a 16-bit value from the specified 8-bit register address.
355362
with self._device as device:
356-
device.write(bytes([addr & 0xFF]))
363+
device.write(bytes([addr & 0xFF]), stop=False)
357364
result = bytearray(2)
358365
device.readinto(result)
359366
if self._debug:

0 commit comments

Comments
 (0)