forked from J0EK3R/mkconnect-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsoletest.py
More file actions
182 lines (151 loc) · 5.91 KB
/
consoletest.py
File metadata and controls
182 lines (151 loc) · 5.91 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/python
# to run: sudo python -i consoletest.py
import sys
import time
print('Script: consoletest.py')
print('Platform: ' + sys.platform)
sys.path.append("Tracer")
from Tracer.Tracer import Tracer
from Tracer.TracerConsole import TracerConsole
sys.path.append("Advertiser")
# uncomment to choose advertiser
if (sys.platform == 'linux'):
#from Advertiser.AdvertiserHCITool import AdvertiserHCITool as Advertiser
from Advertiser.AdvertiserBTMgmt import AdvertiserBTMgmt as Advertiser
#from Advertiser.AdvertiserBTSocket import AdvertiserBTSocket as Advertiser
pass
elif (sys.platform == 'rp2'):
from Advertiser.AdvertiserMicroPython import AdvertiserMicroPython as Advertiser
pass
elif (sys.platform == 'win32'):
from Advertiser.AdvertiserDummy import AdvertiserDummy as Advertiser
else:
raise Exception('unsupported platform')
sys.path.append("MouldKing")
from MouldKing.MouldKing import MouldKing
# instantiate Tracer
tracer = TracerConsole()
# instantiate Advertiser
advertiser = Advertiser()
advertiser.SetTracer(tracer)
# Set Tracer for all MouldKing Hubs
MouldKing.SetTracer(tracer)
MouldKing.SetAdvertiser(advertiser)
# save pre-instantiated objects in local variables
hub0 = MouldKing.Module6_0.Device0
hub1 = MouldKing.Module6_0.Device1
hub2 = MouldKing.Module6_0.Device2
hub3 = MouldKing.Module4_0.Device0
hub4 = MouldKing.Module4_0.Device1
hub5 = MouldKing.Module4_0.Device2
def _getChannelId(channel):
switch={
'A': 0,
'B': 1,
'C': 2,
'D': 3,
'E': 4,
'F': 5,
}
return switch.get(channel,"")
def _getHubId(deviceId):
# MK6
if deviceId == 0:
return hub0
elif deviceId == 1:
return hub1
elif deviceId == 2:
return hub2
# MK4
elif deviceId == 3:
return hub3
elif deviceId == 4:
return hub4
elif deviceId == 5:
return hub5
else:
raise Exception("deviceId 0..5")
def _automate(deviceId: int, channel: int):
userinput = input("\nDo you want to test channel "+ str(channel) +" ? enter y/n\n")
if (userinput != str("y")):
return
tracer.TraceInfo("HUB: "+ str(deviceId) +", FORWARD : Power ramp up from 0 to 100% on channel :" + str(channel))
for percent in range(0, 110, 10):
tracer.TraceInfo("Power : " + str(percent) + "%")
mkcontrol(deviceId,channel, percent/100)
time.sleep(1)
mkstop(deviceId)
tracer.TraceInfo("HUB: "+ str(deviceId) +", REVERSE: Power ramp up from 0 to 100% on channel :" + str(channel))
for percent in range(-0, -110, -10):
tracer.TraceInfo("Power : " + str(percent) + "%")
mkcontrol(deviceId,channel,percent/100)
time.sleep(1)
mkstop(deviceId)
def mkbtstop():
"""
stop bluetooth advertising
"""
advertiser.AdvertisementStop()
# hcitool_args1 = hcitool_path + ' -i hci0 cmd 0x08 0x000a 00' + ' &> /dev/null'
# if platform.system() == 'Linux':
# subprocess.run(hcitool_args1, shell=True, executable="/bin/bash")
# elif platform.system() == 'Windows':
# print('Connect command :')
# print(hcitool_args1)
# else:
# print('Unsupported OS')
return
def mkconnect(deviceId: int=0):
"""
send the bluetooth connect telegram to switch the MouldKing hubs in bluetooth mode
press the button on the hub(s) and the flashing of status led should switch from blue-green to blue
"""
hub = _getHubId(deviceId)
rawdata = hub.Connect()
return
def mkstop(deviceId: int=0):
hub = _getHubId(deviceId)
rawdata = hub.Stop()
return
def mkcontrol(deviceId: int=0, channel: int=0, powerAndDirection: float=0):
hub = _getHubId(deviceId)
rawdata = hub.SetChannel(channel, powerAndDirection)
return
def test_hub(hubId: int=0):
tracer.TraceInfo("HUB "+ str(hubId) +" connecting")
mkconnect()
time.sleep(1)
for index in range(6):
_automate(hubId, index) # start channel 0 = A
tracer.TraceInfo("Channel change requested")
time.sleep(1)
def help():
tracer.TraceInfo("Available commands:")
tracer.TraceInfo(" help() : print available commands")
tracer.TraceInfo(" hints() : print hints and examples")
tracer.TraceInfo(" mkconnect() : Initiate hub control by sending bluetooth connect telegram")
tracer.TraceInfo(" mkstop(hubId) : Stop ALL motors")
tracer.TraceInfo(" mkcontrol(hubId, channel, powerAndDirection) : Control a specific hub, channel, power and motor direction")
tracer.TraceInfo(" test_hub(hubId) : run automated tests on each channels")
tracer.TraceInfo(" mkbtstop() : stop bluetooth advertising")
def hints():
tracer.TraceInfo("HINTS:")
tracer.TraceInfo("If run on windows, commands are shown but not executed (hcitool dependency)")
tracer.TraceInfo()
tracer.TraceInfo("For connecting:")
tracer.TraceInfo(" Switch MK6.0 Hubs on - led is flashing green/blue")
tracer.TraceInfo(" mkconnect() to send the bluetooth connect telegram. All hubs switch to bluetooth mode")
tracer.TraceInfo(" by short-pressing the button on MK6.0 Hubs you can choose the hubId")
tracer.TraceInfo(" hubId=0 - one Led flash")
tracer.TraceInfo(" hubId=1 - two Led flashs")
tracer.TraceInfo(" hubId=2 - three Led flashs")
tracer.TraceInfo()
tracer.TraceInfo("ex: test_hub(0), mkcontrol(0, 0, 0.5); mkcontrol(0, 1, -1, True)")
tracer.TraceInfo(" the minus sign - indicate reverse motor direction")
##################################################################
# Entry point when script is started by python -i consoletest.py
help()
tracer.TraceInfo()
hints()
tracer.TraceInfo()
tracer.TraceInfo("Ready to execute commands\n")