Advertisement
silver2row

Your Source Works...Mine?

Jul 7th, 2022
1,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.29 KB | None | 0 0
  1. ERROR:
  2.  
  3. ./ServoFile.py
  4. Traceback (most recent call last):
  5.   File "/home/debian/ServoCape/./ServoFile.py", line 8, in <module>
  6.     pwm_controller = PCA9685(i2c_bus, 0b1000000)
  7.   File "/home/debian/ServoCape/S_Lib.py", line 24, in __init__
  8.     self.write_reg(MODE1, 1 << 5)  # initialize MODE1 register
  9.   File "/home/debian/ServoCape/S_Lib.py", line 33, in write_reg
  10.     return self.bus.write_byte_data(self.addr, reg, value)
  11.   File "/home/debian/.local/lib/python3.9/site-packages/smbus2/smbus2.py", line 455, in write_byte_data
  12.     ioctl(self.fd, I2C_SMBUS, msg)
  13. OSError: [Errno 16] Device or resource busy
  14.  
  15. FROM:
  16.  
  17. from smbus2 import SMBus
  18. from time import sleep
  19.  
  20. # relevant registers
  21. MODE1       = 0x00
  22. MODE2       = 0x01
  23. LED         = 0x06
  24. ALL_LED     = 0xFA
  25. PRE_SCALE   = 0xFE
  26.  
  27. '''
  28. def pwm_reg( output ):
  29.    if output == 'all':
  30.        return ALL_LED
  31.    else:
  32.        assert output in range(0, 16)
  33.        return LED + 4 * output
  34. '''
  35.  
  36. class PCA9685:
  37.     def __init__(self, bus, addr):
  38.         self.addr = 0b1000000 | addr
  39.         self.bus = bus
  40.         self.write_reg(MODE1, 1 << 5)  # initialize MODE1 register
  41.         sleep(500e-6)  # wait 500us to allow oscillator to power up
  42.  
  43.     def read_reg(self, reg):
  44.         assert reg in range(0, 256)
  45.         return self.bus.read_byte_data(self.addr, reg)
  46.  
  47.     def write_reg(self, reg, value):
  48.         assert reg in range(0, 256)
  49.         return self.bus.write_byte_data(self.addr, reg, value)
  50.  
  51.     def read_regs(self, reg, count):
  52.         assert reg in range(0, 256)
  53.         assert count in range(1, 257-reg)
  54.         return self.bus.read_i2c_block_data(self.addr, reg, count)
  55.  
  56.     def write_regs(self, reg, values):
  57.         assert reg in range(0, 256)
  58.         return self.bus.write_i2c_block_data(self.addr, reg, values)
  59.  
  60.     def get_pwm(self, output):
  61.         [on_l, on_h, off_l, off_h] = self.read_regs(pwm_reg(output), 4)
  62.         on = on_l | on_h << 8
  63.         off = off_l | off_h << 8
  64.         return (off, on)
  65.  
  66.     def set_pwm(self, output, off, on=0):
  67.         assert on in range(0, 4097)
  68.         assert off in range(0, 4097)
  69.         assert on <= off
  70.         on_l = on & 0xff
  71.         on_h = on >> 8
  72.         off_l = off & 0xff
  73.         off_h = off >> 8
  74.         self.write_regs(pwm_reg(output), [on_l, on_h, off_l, off_h])
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement