Skip to content

Commit dd56627

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

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

numberpad.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -731,15 +731,39 @@ def config_get(key, key_default):
731731
def send_value_to_touchpad_via_i2c(value):
732732
global device_id, device_addr
733733

734+
data = [0x05, 0x00, 0x3d, 0x03, 0x06, 0x00, 0x07, 0x00,
735+
0x0d, 0x14, 0x03, int(value, 16), 0xad]
736+
734737
try:
735738
path = f"/dev/i2c-{device_id}"
739+
736740
with I2C(path) as i2c:
737-
data = [0x05, 0x00, 0x3d, 0x03, 0x06, 0x00, 0x07, 0x00,
738-
0x0d, 0x14, 0x03, int(value, 16), 0xad]
739741
msg = I2C.Message(data)
740742
i2c.transfer(device_addr, [msg])
743+
744+
log.debug("Used python-periphery on %s...", path)
745+
746+
return True
747+
741748
except Exception as e:
742-
log.error('Error during sending via i2c: "%s"', e)
749+
log.debug("periphery.I2C failed: %s; falling back to i2ctransfer", e)
750+
751+
try:
752+
hex_data = [f"0x{b:02x}" for b in data]
753+
cmd = ["i2ctransfer", "-f", "-y", str(device_id), f"w{len(data)}@0x{device_addr:x}"] + hex_data
754+
755+
log.debug("Trying I2C via i2ctransfer: %s", " ".join(cmd))
756+
subprocess.run(cmd, check=True, capture_output=True)
757+
log.debug("I2C transfer successful via i2ctransfer")
758+
return True
759+
760+
except subprocess.CalledProcessError as e:
761+
stderr = e.stderr.decode().strip() if e.stderr else str(e)
762+
log.error("i2ctransfer failed: %s", stderr)
763+
except Exception as e:
764+
log.error("Error during fallback I2C transfer: %s", e)
765+
766+
return False
743767

744768

745769
def parse_value_from_config(value):
@@ -1070,8 +1094,14 @@ def get_compose_key_start_events_for_unicode_string(reset_udev = True):
10701094
i2c = I2C(path)
10711095
i2c.close()
10721096
except Exception as e:
1073-
log.error("Can't open the I2C bus connection (id: %s): %s", device_id, e)
1074-
sys.exit(1)
1097+
log.debug("periphery.I2C failed: %s, trying raw command: open...", e)
1098+
try:
1099+
with open(path, "rb+", buffering=0) as f:
1100+
pass
1101+
log.debug(f"Successfully opened {path}")
1102+
except Exception as e2:
1103+
log.error("Can not open the I2C bus connection (id: %s): %s", device_id, e2)
1104+
sys.exit(1)
10751105

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

0 commit comments

Comments
 (0)