Skip to content

Commit 7924e00

Browse files
committed
#254 used command open and i2ctransfer as alternative how communicate via i2c
1 parent befe9d2 commit 7924e00

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

numberpad.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,42 @@ def send_value_to_touchpad_via_i2c(value):
741741
except Exception as e:
742742
log.error('Error during sending via i2c: "%s"', e)
743743

744+
def send_value_to_touchpad_via_i2c(value):
745+
global device_id, device_addr
746+
747+
data = [0x05, 0x00, 0x3d, 0x03, 0x06, 0x00, 0x07, 0x00,
748+
0x0d, 0x14, 0x03, int(value, 16), 0xad]
749+
750+
try:
751+
path = f"/dev/i2c-{device_id}"
752+
753+
with I2C(path) as i2c:
754+
msg = I2C.Message(data)
755+
i2c.transfer(device_addr, [msg])
756+
757+
log.debug("Used python-periphery on %s...", path)
758+
759+
return True
760+
761+
except Exception as e:
762+
log.debug("periphery.I2C failed: %s; falling back to i2ctransfer", e)
763+
764+
try:
765+
hex_data = [f"0x{b:02x}" for b in data]
766+
cmd = ["i2ctransfer", "-f", "-y", str(device_id), f"w{len(data)}@0x{device_addr:x}"] + hex_data
767+
768+
log.debug("Trying I2C via i2ctransfer: %s", " ".join(cmd))
769+
subprocess.run(cmd, check=True, capture_output=True)
770+
log.debug("I2C transfer successful via i2ctransfer")
771+
return True
772+
773+
except subprocess.CalledProcessError as e:
774+
stderr = e.stderr.decode().strip() if e.stderr else str(e)
775+
log.error("i2ctransfer failed: %s", stderr)
776+
except Exception as e:
777+
log.error("Error during fallback I2C transfer: %s", e)
778+
779+
return False
744780

745781
def parse_value_from_config(value):
746782
if value == '0':
@@ -1070,8 +1106,14 @@ def get_compose_key_start_events_for_unicode_string(reset_udev = True):
10701106
i2c = I2C(path)
10711107
i2c.close()
10721108
except Exception as e:
1073-
log.error("Can't open the I2C bus connection (id: %s): %s", device_id, e)
1074-
sys.exit(1)
1109+
log.debug("periphery.I2C failed: %s, trying raw command: open...", e)
1110+
try:
1111+
with open(path, "rb+", buffering=0) as f:
1112+
pass
1113+
log.debug(f"Successfully opened {path}")
1114+
except Exception as e2:
1115+
log.error("Can not open the I2C bus connection (id: %s): %s", device_id, e2)
1116+
sys.exit(1)
10751117

10761118
# Start monitoring the touchpad
10771119
fd_t = open('/dev/input/event' + str(touchpad), 'rb')

0 commit comments

Comments
 (0)