-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtesting.py
More file actions
102 lines (77 loc) · 2.46 KB
/
testing.py
File metadata and controls
102 lines (77 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import utime
from machine import Pin
from encoder.hall_sensor import HallEffectLatching
h1 = HallEffectLatching(Pin(14))
h2 = HallEffectLatching(Pin(12))
# == Run functions == :
def continuous_run():
while True:
print("\rHall Values: {} | {}".format(h1.value(), h2.value()), end=" ")
utime.sleep_ms(50)
def event_only():
def print_event(t, e):
if not e == 0:
print("{} EVENT UPDATE: {} [{}ms ago]".format(t, *e))
while True:
# Read:
h1.value(); h2.value()
e1, e2 = h1.get_new_event(), h2.get_new_event()
print_event(1, e1)
print_event(2, e2)
def dir_print_run():
last_handled = None
while True:
h1.value(); h2.value()
if (not h1.last_event == last_handled) and h1.last_event == h2.last_event:
last_handled = h1.last_event
diff = utime.ticks_diff(h2.last_event_ms, h1.last_event_ms)
if diff > 0:
print("1 -> 2")
elif diff < 0:
print("2 -> 1")
else:
print("neither??")
def enc_tick_count_run():
last_handled_1_ms = None
last_handled_2_ms = None
tick_count = 0
while True:
h1.value(); h2.value()
if ((not h1.last_event_ms == last_handled_1_ms or not h2.last_event_ms == last_handled_2_ms)
and h1.last_event == h2.last_event):
last_handled_1_ms = h1.last_event_ms
last_handled_2_ms = h2.last_event_ms
diff = utime.ticks_diff(h2.last_event_ms, h1.last_event_ms)
if diff > 0:
tick_count += 1
elif diff < 0:
tick_count -= 1
print("\rTicks: {}".format(tick_count), end="")
def usb_serial_comm():
import utime
from machine import UART
#u = UART(0, baudrate=115200)
u = UART(0)
u.write(b'START\n')
for i in range(0, 10):
s = 'DATA:{{"i":{}}}\n'.format(i)
u.write(bytearray(s))
utime.sleep_ms(1000)
def usb_serial_ticks():
import utime
from machine import UART
from encoder import DualHallEncoder
u = UART(0, baudrate=115200)
u.write(b'START\n')
e = DualHallEncoder(h1, h2)
while True:
if e.detect_tick():
u.write(bytearray('DATA:{{"ticks":{}}}\n'.format(e.ticks)))
# Executes on uproc start:
def main():
#continuous_run()
#event_only()
#dir_print_run()
#enc_tick_count_run()
#usb_serial_comm()
usb_serial_ticks()