Advertisement
silver2row

Configure_PWM_GPIO_Other_Stuff

Mar 19th, 2023
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. # Use for PWM on /dev/bone/pwm/
  4.  
  5. from pathlib import Path
  6. #from NewMotor_TB6600 import Pwm  # see https://pastebin.com/R70P1wAn
  7. from time import sleep
  8. import gpiod
  9. # import os
  10. import sys
  11.  
  12. pwmPeriod = '500000'
  13. PWMPATH1 = '/dev/bone/pwm/1/a'
  14. PWMPATH2 = '/dev/bone/pwm/1/b'
  15.  
  16. CHIP1 = 'gpiochip1'
  17. LINE_OUT = [28]
  18.  
  19. CHIP0 = 'gpiochip0'
  20. LINE_IN = [26]
  21.  
  22. chip1 = gpiod.Chip(CHIP1)
  23. chip0 = gpiod.Chip(CHIP0)
  24.  
  25. lineOUT = chip1.get_lines(LINE_OUT)
  26. lineOUT.request(consumer=' ', type=gpiod.LINE_REQ_DIR_OUT)
  27.  
  28. lineIN = chip0.get_lines(LINE_IN)
  29. lineIN.request(consumer=' ', type=gpiod.LINE_REQ_EV_RISING_EDGE)
  30.  
  31. #pwm1aPeriod = Path('/dev/bone/pwm/1/a/')
  32. #pwm1aDuty_Cycle = Path('/dev/bone/pwm/1/a/duty_cycle')
  33. #pwm1aEnable = Path('/dev/bone/pwm/1/a/enable')
  34.  
  35. #pwm1bPeriod = Path('/dev/bone/pwm/1/b/period')
  36. #pwm1bDuty_Cycle = Path('/dev/bone/pwm/1/a/duty_cycle')
  37. #pwm1bEnable = Path('/dev/bone/pwm/1/a/enable')
  38.  
  39. f = open(PWMPATH1+'/period', 'w')
  40. f.write(pwmPeriod)
  41. #f.close()
  42.  
  43. f = open(PWMPATH1+'/enable', 'w')
  44. f.write('0')
  45. #f.close()
  46.  
  47. f = open(PWMPATH2+'/period', 'w')
  48. f.write(pwmPeriod)
  49. #f.close()
  50.  
  51. f = open(PWMPATH2+'/enable', 'w')
  52. f.write('0')
  53. #f.close()
  54.  
  55. duty_cycle = 10000
  56.  
  57. f = open(PWMPATH1+'/duty_cycle', 'w')
  58. f = open(PWMPATH2+'/duty_cycle', 'w')
  59. try:
  60.     while True:
  61.         port = int(input("Please type 0 or 1 : "))
  62.         if lineIN == 0:
  63.             f = open(PWMPATH1+'/enable', 'rw')
  64.             f.write('0')
  65.             f = open(PWMPATH2+'/enable', 'rw')
  66.             f.write('0')
  67.             sleep(0.5)
  68.             lineOUT.set_values([0])
  69.  
  70.         elif lineIN == 1:
  71.             f = open(PWMPATH1+'/enable', 'rw')
  72.             f.write('0')
  73.             f = open(PWMPATH2+'/enable', 'rw')
  74.             f.write('0')
  75.             sleep(0.5)
  76.             lineOut.set_values([0])
  77.  
  78.         elif port == 0:
  79.             lineOUT.set_values([1])
  80.             f = open(PWMPATH1+'/duty_cycle', 'w')
  81.             f = open(PWMPATH1+'/enable', 'w')
  82.             f.write('1')
  83.             sleep(2.5)
  84.             f.write('0')
  85.  
  86.         elif port == 1:
  87.             lineOUT.set_values([1])
  88.             f = open(PWMPATH2+'/duty_cycle', 'w')
  89.             f = open(PWMPATH2+'/enable', 'w')
  90.             f.write('1')
  91.             sleep(2.5)
  92.             f.write('0')
  93.  
  94. except KeyboardInterrupt:
  95.     f = open(PWMPATH1+'/enable', 'w')
  96.     f.write('0')
  97.     f.close()
  98.     f = open(PWMPATH2+'/enable', 'w')
  99.     f.write('0')
  100.     f.close()
  101.     print("Kosher Salt!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement