Functions
pwm Namespace Reference

Functions

def main ()
 

Detailed Description

Copyright (C) 2019 Moxa Inc. All rights reserved.
SPDX-License-Identifier: Apache-2.0

PWM Python Sample Application

Date          Author            Comment
2019-02-11    William Chang     Created it.

Function Documentation

◆ main()

def pwm.main ( )

Definition at line 28 of file pwm.py.

28 def main():
29  parser = argparse.ArgumentParser(description="PWM sample program.")
30  parser.add_argument("-s", "--slot", dest="do_slot", type=int, default=3)
31  parser.add_argument("-c", "--channel_start", dest="pwm_channel_start", type=int, default=0)
32  parser.add_argument("-n", "--channel_count", dest="pwm_channel_count", type=int, default=2)
33  args = parser.parse_args()
34 
35  do_slot = args.do_slot
36  pwm_channel_start = args.pwm_channel_start
37  pwm_channel_count = args.pwm_channel_count
38  print("DO slot = {}".format(do_slot))
39  print("PWM start channel = {}".format(pwm_channel_start))
40  print("PWM channel count = {}".format(pwm_channel_count))
41 
42  # initialize ioThinx I/O
43  device = ioThinx_4530_API.ioThinx_4530_API()
44 
45  # temporarily set config
46  do_modes = [DO_MODE_PWM] * pwm_channel_count
47  device.ioThinx_DO_Config_SetModes(do_slot, pwm_channel_start, pwm_channel_count, do_modes)
48 
49  pwm_counts = [0] * pwm_channel_count # infinite
50  device.ioThinx_DO_Config_SetPwmCounts(do_slot, pwm_channel_start, pwm_channel_count, pwm_counts)
51  pwm_freqs = [5] * pwm_channel_count
52  pwm_dutys = [50] * pwm_channel_count
53  device.ioThinx_DO_Config_SetPwmConfigures(do_slot, pwm_channel_start, pwm_channel_count, pwm_freqs, pwm_dutys)
54 
55  # reload config
56  device.ioThinx_IO_Config_Reload()
57 
58  # start pwm
59  pwm_starts = [0] * 4
60  for i in range(pwm_channel_start, pwm_channel_start + pwm_channel_count):
61  pwm_starts[i] = 1
62  device.ioThinx_DO_SetPwmStarts(do_slot, pwm_starts)
63 
64  # set value
65  while True:
66  pwm_dutys[0] = 0 if (pwm_dutys[0] >= 100) else pwm_dutys[0] + 10 # runtime adjust the duty cycle
67  device.ioThinx_DO_SetPwmConfigures(do_slot, pwm_channel_start, pwm_channel_count, pwm_freqs, pwm_dutys)
68  for i in range(pwm_channel_count):
69  print("[ {}:{}] frequency = {}, duty cycle = {}".format(do_slot, pwm_channel_start + i, pwm_freqs[i], pwm_dutys[i]))
70 
71  if input("Press 'q' to exit. other keys to continue") == 'q':
72  break
73 
74  device.ioThinx_DO_SetPwmStops(do_slot, pwm_starts)
75 
76 
def main()
Definition: misc.py:32