KMKfw / kmk_firmware

Clackety Keyboards Powered by Python
https://kmkfw.zulipchat.com
Other
1.33k stars 460 forks source link

[BUG] Title #880

Closed hxse closed 10 months ago

hxse commented 10 months ago

I use helios rp2040, when i use SerialACE send text on Pc, i get result out of my expetation.

send_kmk: bytearray(b'switch_layer 0\n')
result_kmk: b'code.py | 8.2.6\x1b\\'

I get 8.2.6\x1b\\, It seems to be a runtime version, it is so strange, out of my expetation. Any suggestion, Why return this thing?

my serialace2.py

from usb_cdc import data

from kmk.modules import Module

# from kmk.utils import Debug
from kmk.keys import KC

# debug = Debug(__name__)

def callback(keyboard, data: bytearray):
    print("receive", data)
    text: str = "".join([chr(i) for i in data])
    args = text.split(" ")

    if args[0] == "switch_layer":
        num = int(args[1])
        keyboard.tap_key(KC.TO(num))
        return f"success: {text}"
    return

class SerialACE(Module):
    buffer = bytearray()

    def during_bootup(self, keyboard):
        keyboard.tap_key(KC.DF(1))
        try:
            data.timeout = 0
        except AttributeError:
            pass

    def before_matrix_scan(self, keyboard):
        pass

    def after_matrix_scan(self, keyboard):
        # keyboard.tap_key(KC.DF(1))
        pass

    def process_key(self, keyboard, key, is_pressed, int_coord):
        return key

    def before_hid_send(self, keyboard):
        # Serial.data isn't initialized.
        if not data:
            return

        # Nothing to parse.
        if data.in_waiting == 0:
            return

        self.buffer.extend(data.read())
        idx = self.buffer.find(b"\n")

        # No full command yet.
        if idx == -1:
            return

        # Split off command and evaluate.
        line = self.buffer[:idx]
        self.buffer = self.buffer[idx + 1 :]

        try:
            # if debug.enabled:
            # debug(f"eval({line})")

            # ret = eval(line, {'keyboard': keyboard,'KC':KC})
            # eval so danger, run in callback

            res = callback(keyboard, line)

            data.write(bytearray(str(res) + "\n"))
        except Exception as err:
            # if debug.enabled:
            # debug(f"error: {err}")
            data.write(bytearray("error: " + str(err) + "\n"))

    def after_hid_send(self, keyboard):
        pass

    def on_powersave_enable(self, keyboard):
        pass

    def on_powersave_disable(self, keyboard):
        pass
xs5871 commented 10 months ago

If you submit a bug, please:

  1. Use the bug report template and actually fill it out as intended. There's a reason we're asking for specific information. This bug report doesn't even have a title...
  2. Make sure it's actually a bug. Asking for help or support is not a bug. If in doubt, bring up your issue in the zulip chat first.

That being said, looks like you're connected to the wrong serial interface.

hxse commented 10 months ago

@xs5871 sorry, I lost my head

hxse commented 9 months ago
import usb_cdc
usb_cdc.enable(data=True)

add code to boot.py and connect to the another port, solved my question.