moons-motor 0.1.4__py3-none-any.whl → 0.1.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
moons_motor/motor.py CHANGED
@@ -13,6 +13,66 @@ import threading
13
13
 
14
14
  from dataclasses import dataclass
15
15
 
16
+ import logging
17
+
18
+
19
+ class ColoredFormatter(logging.Formatter):
20
+ # Define ANSI escape codes for colors and reset
21
+ grey = "\x1b[38;21m"
22
+ yellow = "\x1b[33;21m"
23
+ red = "\x1b[31;21m"
24
+ bold_red = "\x1b[31;1m"
25
+ bold_yellow = "\x1b[33;1m"
26
+ bold_green = "\x1b[32;1m"
27
+ bold_blue = "\x1b[34;1m"
28
+ bold_cyan = "\x1b[36;1m"
29
+ bold_magenta = "\x1b[35;1m"
30
+ reset = "\x1b[0m"
31
+
32
+ # Define the base format string
33
+ format_time = "%(asctime)s"
34
+ format_header = " [%(levelname)s]"
35
+ format_base = " %(message)s"
36
+
37
+ # Map log levels to colored format strings
38
+ FORMATS = {
39
+ logging.DEBUG: format_time
40
+ + bold_cyan
41
+ + format_header
42
+ + reset
43
+ + format_base
44
+ + reset,
45
+ logging.INFO: format_time
46
+ + bold_green
47
+ + format_header
48
+ + reset
49
+ + format_base
50
+ + reset,
51
+ logging.WARNING: format_time
52
+ + bold_yellow
53
+ + format_header
54
+ + reset
55
+ + format_base
56
+ + reset,
57
+ logging.ERROR: format_time
58
+ + bold_red
59
+ + format_header
60
+ + reset
61
+ + format_base
62
+ + reset,
63
+ logging.CRITICAL: format_time
64
+ + bold_magenta
65
+ + format_header
66
+ + reset
67
+ + format_base
68
+ + reset,
69
+ }
70
+
71
+ def format(self, record):
72
+ log_fmt = self.FORMATS.get(record.levelno)
73
+ formatter = logging.Formatter(log_fmt)
74
+ return formatter.format(record)
75
+
16
76
 
17
77
  class StepperModules:
18
78
  STM17S_3RN = "STM17S-3RN"
@@ -22,6 +82,9 @@ class StepperModules:
22
82
  class StepperCommand:
23
83
  JOG: str = "CJ" # Start jogging
24
84
  JOG_SPEED: str = "JS" # Jogging speed (Need to set before start jogging)
85
+ JOG_ACCELERATION: str = (
86
+ "JA" # Jogging acceleration (Need to set before start jogging)
87
+ )
25
88
  CHANGE_JOG_SPEED: str = "CS" # Change jogging speed while jogging
26
89
  STOP_JOG: str = "SJ" # Stop jogging with deceleration
27
90
  STOP: str = "ST" # Stop immediately (No deceleration)
@@ -90,6 +153,13 @@ class MoonsStepper(Subject):
90
153
  "?",
91
154
  "@",
92
155
  ]
156
+ # Configure logging
157
+ logger = logging.getLogger(__name__)
158
+ logger.setLevel(logging.INFO)
159
+
160
+ ch = logging.StreamHandler()
161
+ ch.setFormatter(ColoredFormatter())
162
+ logger.addHandler(ch)
93
163
 
94
164
  def __init__(
95
165
  self,
@@ -179,7 +249,7 @@ class MoonsStepper(Subject):
179
249
  if self.only_simulate:
180
250
  self.Opened = True
181
251
  self.device = f"Simulate-{self.universe}"
182
- print(f"{self.device} connected")
252
+ MoonsStepper.logger.info(f"{self.device} connected")
183
253
  if callback:
184
254
  callback(self.device, self.Opened)
185
255
  return
@@ -206,10 +276,10 @@ class MoonsStepper(Subject):
206
276
  self.Opened = False
207
277
  if self.ser.is_open:
208
278
  self.Opened = True
209
- print(f"[bold green]Device connected[/bold green]: {self.device}")
279
+ MoonsStepper.logger.info(f"Device connected: {self.device}")
210
280
 
211
281
  except Exception as e:
212
- print(f"[bold red]Device error:[/bold red] {e} ")
282
+ MoonsStepper.logger.error(f"Device error: {e} ")
213
283
  self.Opened = False
214
284
 
215
285
  ports = list(list_ports.comports())
@@ -222,7 +292,7 @@ class MoonsStepper(Subject):
222
292
  m = re.match(
223
293
  r"USB\s*VID:PID=(\w+):(\w+)\s*SER=([A-Za-z0-9]*)", p.usb_info()
224
294
  )
225
- print(p.usb_info())
295
+ MoonsStepper.logger.info(p.usb_info())
226
296
  if (
227
297
  m
228
298
  and m.group(1) == self.VID
@@ -230,8 +300,8 @@ class MoonsStepper(Subject):
230
300
  # and m.group(3) == self.SERIAL_NUM
231
301
  ):
232
302
  if m.group(3) == self.SERIAL_NUM or self.SERIAL_NUM == "":
233
- print(
234
- f"[bold yellow]Device founded:[/bold yellow] {p.description} | VID: {m.group(1)} | PID: {m.group(2)} | SER: {m.group(3)}"
303
+ MoonsStepper.logger.info(
304
+ f"Device founded: {p.description} | VID: {m.group(1)} | PID: {m.group(2)} | SER: {m.group(3)}"
235
305
  )
236
306
 
237
307
  self.device = p.description
@@ -249,7 +319,7 @@ class MoonsStepper(Subject):
249
319
  callback(self.device, self.Opened)
250
320
 
251
321
  if not self.Opened:
252
- print(f"[bold red]Device not found[/bold red]")
322
+ MoonsStepper.logger.error(f"Device not found")
253
323
  if callback:
254
324
  callback(self.device, self.Opened)
255
325
 
@@ -264,7 +334,7 @@ class MoonsStepper(Subject):
264
334
  self.listen = False
265
335
  self.is_sending = False
266
336
  self.Opened = False
267
- print(f"Simulate-{self.universe} disconnected")
337
+ MoonsStepper.logger.info(f"Simulate-{self.universe} disconnected")
268
338
  return
269
339
  if self.ser is not None and self.ser.is_open:
270
340
  self.listen = False
@@ -272,7 +342,7 @@ class MoonsStepper(Subject):
272
342
  self.Opened = False
273
343
  self.ser.flush()
274
344
  self.ser.close()
275
- print(f"[bold red]Device disconnected[/bold red]: {self.device}")
345
+ MoonsStepper.logger.info(f"Device disconnected: {self.device}")
276
346
 
277
347
  def send(self, command, eol=b"\r"):
278
348
  if (self.ser != None and self.ser.is_open) or self.only_simulate:
@@ -281,16 +351,16 @@ class MoonsStepper(Subject):
281
351
  if self.ser is not None or not self.only_simulate:
282
352
  self.ser.write(self.temp_cmd.encode("ascii"))
283
353
  if self.is_log_message:
284
- print(
285
- f"[bold green]Send to {self.device}:[/bold green] {self.temp_cmd}"
286
- )
354
+ MoonsStepper.logger.debug(f"Send to {self.device}: {self.temp_cmd}")
287
355
  super().notify_observers(f"{self.universe}-{self.temp_cmd}")
288
356
  else:
289
- print(f"Target device is not opened. Command: {command}")
357
+ MoonsStepper.logger.debug(
358
+ f"Target device is not opened. Command: {command}"
359
+ )
290
360
 
291
361
  def send_command(self, address="", command="", value=None):
292
362
  if command == "":
293
- print("Command can't be empty")
363
+ MoonsStepper.logger.warning("Command can't be empty")
294
364
  return
295
365
  if value is not None:
296
366
  command = self.addressed_cmd(address, command + str(value))
@@ -327,13 +397,13 @@ class MoonsStepper(Subject):
327
397
 
328
398
  def handle_recv(self, response):
329
399
  if "*" in response:
330
- print(f"[bold green](o)buffered_ack[/bold green]")
400
+ MoonsStepper.logger.info(f"(o)buffered_ack")
331
401
  elif "%" in response:
332
- print(f"[bold green](v)success_ack[/bold green]")
402
+ MoonsStepper.logger.info(f"(v)success_ack")
333
403
  elif "?" in response:
334
- print(f"[bold red](x)fail_ack[/bold red]")
404
+ MoonsStepper.logger.info(f"(x)fail_ack")
335
405
  else:
336
- print(f"[bold blue]Received from {self.device}: [/bold blue]", response)
406
+ MoonsStepper.logger.info(f"Received from {self.device}: ", response)
337
407
  self.recvQueue.put_nowait(response)
338
408
 
339
409
  if "=" in response:
@@ -362,14 +432,14 @@ class MoonsStepper(Subject):
362
432
 
363
433
  def check_status(response):
364
434
  result = MoonsStepper.process_response(response)
365
- print(f"[bold blue]Status check result:[/bold blue] {result}")
435
+ MoonsStepper.logger.info(f"Status check result: {result}")
366
436
  if "H" not in result["value"]:
367
- print("[bold green]Motor is homed.[/bold green]")
437
+ MoonsStepper.logger.info("Motor is homed.")
368
438
  if onComplete: # Call the onComplete callback if provided
369
439
  onComplete(result)
370
440
  homing_complete.set() # Signal that homing is complete
371
441
  else:
372
- print("[bold yellow]Motor is not homed yet.[/bold yellow]")
442
+ MoonsStepper.logger.info("Motor is not homed yet.")
373
443
 
374
444
  def check_homing_complete():
375
445
  while not homing_complete.is_set(): # Loop until homing is complete
@@ -445,5 +515,6 @@ class MoonsStepper(Subject):
445
515
  # SERIAL => 上次已知父系(尾巴+A) 或是事件分頁
446
516
  # reg USB\s*VID:PID=(\w+):(\w+)\s*SER=([A-Za-z0-9]+)
447
517
 
518
+
448
519
  # serial_num 裝置例項路徑
449
520
  # TD(Tramsmit Delay) = 15
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: moons_motor
3
- Version: 0.1.4
3
+ Version: 0.1.5
4
4
  Summary: This is a python library for controlling the Moons' motor through the serial port.
5
5
  Author-email: miroc <mike8503111@gmail.com>
6
6
  Project-URL: Repository, https://github.com/miroc99/moons_motor.git
@@ -1,11 +1,11 @@
1
1
  moons_motor/__init__.py,sha256=qOpsRwizV-DpKSvNzyvj8ju3cs6vwgIICur1Oe6sxOA,27
2
- moons_motor/motor.py,sha256=ZXXX6Ie-PL5QN8O-JpMVGh9xgYSsQpJM0BsqiVbtdns,15521
2
+ moons_motor/motor.py,sha256=LujQRwyndbeowiGcjvfPt_L8j3B2582hDJLYMD-8suM,17381
3
3
  moons_motor/observer.py,sha256=PXzuPYKRb2HpjArJcD8HakYIPfFGAs1uBDIL8PSizgA,124
4
4
  moons_motor/simulate.py,sha256=J0y1fZhoOim9i-BAkprxnPern1SAdkDfKPqT2MWyDwU,2561
5
5
  moons_motor/status.py,sha256=jXQZFZTt9ugHktkWKLII8MpEQQaeO-UjlwTrrP4LJNE,2872
6
6
  moons_motor/subject.py,sha256=L_GS6fvJTeX7X23o3T92oiZ4rtLVKA2OEd9GpHn_Dz4,445
7
- moons_motor-0.1.4.dist-info/licenses/LICENSE,sha256=nsYjO800SjIjI85y2kVHR5mC3tca2vs4kK_BhNe89bM,1074
8
- moons_motor-0.1.4.dist-info/METADATA,sha256=OfYNsukji4MOjMxTBk87GHo1Y4GH5eQFkfpLY8czulE,1403
9
- moons_motor-0.1.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
10
- moons_motor-0.1.4.dist-info/top_level.txt,sha256=0dE-CR5_NYBw34jHIDGQNWpMllzO6mtUIuKyRv_rJLg,12
11
- moons_motor-0.1.4.dist-info/RECORD,,
7
+ moons_motor-0.1.5.dist-info/licenses/LICENSE,sha256=nsYjO800SjIjI85y2kVHR5mC3tca2vs4kK_BhNe89bM,1074
8
+ moons_motor-0.1.5.dist-info/METADATA,sha256=enjd1RbUxi1c2mzwIAXenUEu-KegcywTe70SoIXiwQs,1403
9
+ moons_motor-0.1.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
10
+ moons_motor-0.1.5.dist-info/top_level.txt,sha256=0dE-CR5_NYBw34jHIDGQNWpMllzO6mtUIuKyRv_rJLg,12
11
+ moons_motor-0.1.5.dist-info/RECORD,,