- Main
- Tutorials
- Library
- Samples

- File List
- Globals

Namespaces | Functions | Variables
pwm.py File Reference

PWM Sample More...

Go to the source code of this file.

## Namespaces
| pwm

## Functions
def | main ()

## Variables
int | DO_MODE_PWM = 1

## Detailed Description

PWM Sample

Copyright
  2019 Moxa Inc. All rights reserved.

Date
  2019-02-11

Author
  William Chang

Version
  V1.0
  1 '''
  2 Copyright (C) 2019 Moxa Inc. All rights reserved.
  3 SPDX-License-Identifier: Apache-2.0
  4
  5 PWM Python Sample Application
  6
  7 Date Author Comment
  8 2019-02-11 William Chang Created it.
  9 '''
  10
  11
  19
  20 from ioThinx_4530 import ioThinx_4530_API
  21 import argparse
  22
  23 DO_MODE_PWM = 1
  24
  25
  26 def main():
  27 parser = argparse.ArgumentParser(description="PWM sample program.")
  28 parser.add_argument("-s", "--slot", dest="do_slot", type=int, default=3)
  29 parser.add_argument("-c", "--channel_start", dest="pwm_channel_start", type=int, default=0)
  30 parser.add_argument("-n", "--channel_count", dest="pwm_channel_count", type=int, default=2)
  31 args = parser.parse_args()
  32
  33 do_slot = args.do_slot
  34 pwm_channel_start = args.pwm_channel_start
  35 pwm_channel_count = args.pwm_channel_count
  36 print("DO slot = {}".format(do_slot))
  37 print("PWM start channel = {}".format(pwm_channel_start))
  38 print("PWM channel count = {}".format(pwm_channel_count))
  39
  40 # initialize ioThinx I/O
  41 device = ioThinx_4530_API.ioThinx_4530_API()
  42
  43 # temporarily set config
  44 do_modes = [DO_MODE_PWM] * pwm_channel_count
  45 device.ioThinx_DO_Config_SetModes(do_slot, pwm_channel_start, pwm_channel_count, do_modes)
  46
  47 pwm_counts = [0] * pwm_channel_count # infinite
  48 device.ioThinx_DO_Config_SetPwmCounts(do_slot, pwm_channel_start, pwm_channel_count, pwm_counts)
  49 pwm_freqs = [5] * pwm_channel_count
  50 pwm_dutys = [50] * pwm_channel_count
  51 device.ioThinx_DO_Config_SetPwmConfigures(do_slot, pwm_channel_start, pwm_channel_count, pwm_freqs, pwm_dutys)
  52
  53 # reload config
  54 device.ioThinx_IO_Config_Reload()
  55
  56 # start pwm
  57 pwm_starts = [0] * 4
  58 for i in range(pwm_channel_start, pwm_channel_start + pwm_channel_count):
  59 pwm_starts[i] = 1
  60 device.ioThinx_DO_SetPwmStarts(do_slot, pwm_starts)
  61
  62 # set value
  63 while True:
  64 pwm_dutys[0] = 0 if (pwm_dutys[0] >= 100) else pwm_dutys[0] + 10 # runtime adjust the duty cycle
  65 device.ioThinx_DO_SetPwmConfigures(do_slot, pwm_channel_start, pwm_channel_count, pwm_freqs, pwm_dutys)
  66 for i in range(pwm_channel_count):
  67 print("[ {}:{}] frequency = {}, duty cycle = {}".format(do_slot, pwm_channel_start + i, pwm_freqs[i], pwm_dutys[i]))
  68
  69 if input("Press 'q' to exit. other keys to continue") == 'q':
  70 break
  71
  72 device.ioThinx_DO_SetPwmStops(do_slot, pwm_starts)
  73
  74
  75 if __name__ == '__main__':
  76 main()

Definition in file pwm.py.