Fattoresaimon / I2CEncoderV2.1

GNU General Public License v3.0
108 stars 24 forks source link

Connection list to I2C device: OSError: [Errno 121] Remote I/O error #19

Open maltejahn opened 1 year ago

maltejahn commented 1 year ago

Hi,

i bought version 2.1 and i am trying to run it on a raspberry 4 later in combination with the led ring (both connected to the RPi). I2C is set to 100kHz in the config.txt

Now it occurs after some random turns of the encoder that the, i think, mc of the encoder crashes because i2cdetect -y 1 still sees my LED ring, but the encoder is gone after the error:

Encoder min!
sum: 1289
Traceback (most recent call last):
  File "/home/pi/i2cring/./drehencoder.py", line 58, in Encoder_INT
    encoder.updateStatus()
  File "/home/pi/i2cring/i2cEncoderLibV2.py", line 192, in updateStatus
    self.eventCaller (self.onChange)
  File "/home/pi/i2cring/i2cEncoderLibV2.py", line 167, in eventCaller
    event()
  File "/home/pi/i2cring/./drehencoder.py", line 19, in EncoderChange
    temp = encoder.readCounter32()
  File "/home/pi/i2cring/i2cEncoderLibV2.py", line 298, in readCounter32
    return (self.readEncoder32(REG_CVALB4))
  File "/home/pi/i2cring/i2cEncoderLibV2.py", line 624, in readEncoder32
    data = self.i2cbus.read_i2c_block_data(self.i2cadd, add, 4)
  File "/home/pi/.local/lib/python3.9/site-packages/smbus2/smbus2.py", line 617, in read_i2c_block_data
    ioctl(self.fd, I2C_SMBUS, msg)
OSError: [Errno 121] Remote I/O error
Traceback (most recent call last):
  File "/home/pi/i2cring/./drehencoder.py", line 58, in Encoder_INT
    encoder.updateStatus()
  File "/home/pi/i2cring/i2cEncoderLibV2.py", line 171, in updateStatus
    self.stat = self.readEncoder8(REG_ESTATUS)
  File "/home/pi/i2cring/i2cEncoderLibV2.py", line 610, in readEncoder8
    data[0] = self.i2cbus.read_byte_data(self.i2cadd, add)
  File "/home/pi/.local/lib/python3.9/site-packages/smbus2/smbus2.py", line 433, in read_byte_data
    ioctl(self.fd, I2C_SMBUS, msg)
OSError: [Errno 121] Remote I/O error
^CTraceback (most recent call last):
  File "/home/pi/i2cring/./drehencoder.py", line 114, in <module>
    pass
KeyboardInterrupt

After this error i have to disconnect the Encoder/LED Ring. It doesnt help to just rerun the script.

I tried it multiple times to see if there is a kinf of pattern. But it crashes after 700.... 3000 clicks. Slow or fast does also seem to do the same. Any ideas?

import smbus2
import RPi.GPIO as GPIO
from time import sleep
import i2cEncoderLibV2
#import ledring

oldEncValue = 0
sum=0

minLedLoudness = 34
maxLedLoudness = 0

def EncoderChange():
    encoder.writeLEDG(100)
    global sum
    sum = sum +1
    print('sum: %d' % (sum))
    temp = encoder.readCounter32()
    oldEncValue = temp
    print ('Changed: %d' % (temp))

    #ledring.LEDRing_Set_RGB(temp,0x12345)

    #ledring.LEDRing_Set_RGB(temp+1,0)
    #ledring.LEDRing_Set_RGB(temp-1,0)
#    if temp >= minLedLoudness:
#        temp = minLedLoudness
#    ledring.LEDRing_loudness(temp,0x50)
    #encoder.writeLEDG(0)

def EncoderPush():
    #encoder.writeLEDB(100)
    print ('Encoder Pushed!')
    #encoder.writeLEDB(0)

def EncoderDoublePush():
    #encoder.writeLEDB(100)
    #encoder.writeLEDG(100)
    print ('Encoder Double Push!')
    #encoder.writeLEDB(0)
    #encoder.writeLEDG(0)

def EncoderMax():
    #encoder.writeLEDR(100)
    print ('Encoder max!')
    #encoder.writeLEDR(0)

def EncoderMin():
    #encoder.writeLEDR(100)
    print ('Encoder min!')
    #encoder.writeLEDR(0)

def Encoder_INT(self):
    encoder.updateStatus()

GPIO.setmode(GPIO.BCM)
bus = smbus2.SMBus(1)
INT_pin = 4
GPIO.setup(INT_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

encoder = i2cEncoderLibV2.i2cEncoderLibV2(bus, 0x50)
#ledring= ledring.ledRing(bus, 0x28)
#ledring.setup()

#ledring.LEDRing_ClearAll()
#ledring.LEDRing_Set_RGB(1,0x12345)
#ledring.LEDRing_Set_BLUE(34,0x60)
#ledring.LEDRing_Set_BLUE(47,0x60)

encconfig = (i2cEncoderLibV2.INT_DATA | i2cEncoderLibV2.WRAP_DISABLE | i2cEncoderLibV2.DIRE_RIGHT | i2cEncoderLibV2.IPUP_ENABLE | i2cEncoderLibV2.RMOD_X1 | i2cEncoderLibV2.RGB_ENCODER)
encoder.begin(encconfig)

encoder.writeCounter(0)
encoder.writeMax(47)
encoder.writeMin(0)
encoder.writeStep(1)
encoder.writeAntibouncingPeriod(8)
encoder.writeDoublePushPeriod(50)
encoder.writeGammaRLED(i2cEncoderLibV2.GAMMA_2)
encoder.writeGammaGLED(i2cEncoderLibV2.GAMMA_2)
encoder.writeGammaBLED(i2cEncoderLibV2.GAMMA_2)

encoder.onChange = EncoderChange
encoder.onButtonPush = EncoderPush
encoder.onButtonDoublePush = EncoderDoublePush
encoder.onMax = EncoderMax
encoder.onMin = EncoderMin

encoder.autoconfigInterrupt()
print ('Board ID code: 0x%X' % (encoder.readIDCode()))
print ('Board Version: 0x%X' % (encoder.readVersion()))

encoder.writeRGBCode(0x640000)
sleep(0.3)
encoder.writeRGBCode(0x006400)
sleep(0.3)
encoder.writeRGBCode(0x000064)
sleep(0.3)
encoder.writeRGBCode(0x00)

GPIO.add_event_detect(INT_pin, GPIO.FALLING, callback=Encoder_INT, bouncetime=10)

while True:
  #  if GPIO.input(INT_pin) == False: #
   #     Encoder_INT() #
    pass
maltejahn commented 1 year ago

Hi,

also another problem is the 100 percent usage when using the example. Probably due to this:

while True:
  #  if GPIO.input(INT_pin) == False: #
   #     Encoder_INT() #
    time.sleep(0.00001) <- added by me
    pass

I added some sleep (so about 20 pervent load by python), also played aroung with the value.

The problem, that the mc isnt responding anymore, remains. Is the buffer of the PIC mc too small? It seems that it crash mor often when rotating fast. But sometimes it also crashes when doing slot rotation