An adaptive interface control method, system and storage medium for a dual-mode trackball
Patent Information
- Application Number
- CN202611202418.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-10
- Publication Date
- 2026-10-09
AI Technical Summary
[0003]然而,当前市面上的轨迹球产品在接口兼容性和信号可靠性方面仍存在显著不足
[0014]本发明提供了一种双模轨迹球的自适应接口控制方法、系统和存储介质,通过实时获取切换开关的第一标志状态和总线信号的第二标志状态,并在第一标志状态与当前运行协议栈不匹配或第二标志状态指示冲突时触发协议切换事件,进而执行协议栈重装载,按预设接口优先级顺序执行握手序列以确认目标接口的有效性,锁定并更新协议栈后采集轨迹球位移信号和按键状态信号并按更新后的协议栈封装为数据帧输出至主机。本发明实现了轨迹球在带电运行过程中无需断电即可自适应完成接口协议切换,同时有效提升轨迹球设备在不同接口标准下的自适应能力和工业场景中的操作连续性。
Smart Images

Figure CN122883705A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of industrial control and computer peripheral technology, and more specifically, to an adaptive interface control method, system, and storage medium for a dual-mode trackball. Background Technology
[0002] With the rapid development of industrial automation control, medical imaging diagnosis and precision CAD drawing, trackballs, as high-precision positioning input devices that do not require large-area moving planes, are widely used in professional scenarios such as machine tool control consoles and CT imaging workstations.
[0003] However, current trackball products on the market still have significant shortcomings in terms of interface compatibility and signal reliability. First, trackballs with a single PS / 2 interface can only be compatible with older industrial control hosts and cannot be directly connected to modern industrial computers without PS / 2 interfaces. Furthermore, the PS / 2 protocol has a low signal transmission rate, resulting in noticeable cursor delay during large displacement operations. Long-term use of the mechanical encoder can lead to brush wear, causing pulse signal distortion and a continuous decrease in positioning accuracy. Second, while trackballs with a single USB interface are compatible with modern hosts, they cannot be used directly with older industrial control terminals and medical equipment that only have a PS / 2 interface. An external USB-to-PS / 2 converter is required, but this converter is prone to introducing signal interference and pulse loss, causing cursor drift. Additionally, the external converter increases wiring complexity and exhibits poor stability in humid industrial environments and strong electromagnetic fields. Third, the few existing products with dual interfaces only achieve passive switching at the hardware level, lacking a software protocol adaptation mechanism. Users must restart the device after switching interfaces for the new interface to take effect, failing to achieve real-time switching and severely impacting the efficiency of continuous operation in industrial settings. Finally, the power supply circuit of ordinary trackball products lacks multi-level protection design, and is susceptible to overvoltage, static electricity and surge impacts in industrial wide voltage input scenarios, which can lead to damage to the main control chip and insufficient long-term operational reliability.
[0004] Therefore, there is an urgent need for a trackball control solution that can achieve real-time adaptive switching between USB and PS2 interfaces, has automatic protocol adaptation capabilities, and high anti-interference capabilities. Summary of the Invention
[0005] In view of the above problems, the purpose of this invention is to provide an adaptive interface control method, system and storage medium for a dual-mode trackball. By acquiring the first flag state of the switching switch and the second flag state of the bus signal in real time, and triggering a protocol switching event when the states do not match or the bus conflicts, the method performs protocol stack reloading and handshake sequence confirmation. This enables the trackball to adaptively complete interface protocol switching without power interruption during operation, effectively avoiding signal interference problems caused by external converters and improving operational continuity.
[0006] The first aspect of this invention provides an adaptive interface control method for a dual-mode trackball, the method comprising: Obtain the first flag state of the switch and the second flag state of the bus signal; If the first flag state matches the currently running protocol stack, and the second flag state indicates that it is in an idle state, then the current protocol stack is maintained. If the first flag state does not match the currently running protocol stack, or the second flag state indicates a conflict, a protocol switching event is triggered. In response to the protocol switching event, stop the current protocol stack from sending and receiving data, clear the communication buffer, and reload the protocol stack for the target interface; The protocol stack reloading executes the handshake sequence according to the preset interface priority order. The first handshake sequence sends the device identification request according to the first interface protocol timing and waits for the first feature response. The second handshake sequence sends the device identification request according to the second interface protocol timing and waits for the second feature response. When the first feature response or the second feature response is confirmed, the protocol stack is locked and updated. Once the updated protocol stack takes effect, trackball displacement signals and button status signals are collected, encapsulated into data frames according to the updated protocol stack, and transmitted to the host.
[0007] In this solution, obtaining the first flag state of the switching switch and the second flag state of the bus signal specifically includes: After power-on initialization, the level value of the switch is read, and the first flag state is generated based on the level value. The first flag state includes a first mode code or a second mode code. The data line level and clock line level of the first interface are detected in parallel. If the data line level and the clock line level are both at a high level, the second flag state is determined to be idle. If the data line level or the clock line level is in a low level state for a duration exceeding a preset conflict determination time, then the second flag state is determined to be a conflict state. When the transition edge of the data line level or the clock line level of the first interface is captured, the second flag state is updated to bus active state; Based on the first flag state and the second flag state, the matching status of the currently running protocol stack and the priority order of the target interface are determined.
[0008] In this solution, after triggering the protocol switching event, the following is also included: Pause the interrupt service routine of the currently running protocol stack and disable the enable signal sent by the interface corresponding to the currently running protocol stack; Force the protocol stack state machine of the currently running protocol stack to jump to the standby state and release the communication peripheral resources occupied by the currently running protocol stack; The protocol type of the target interface is parsed, and the corresponding protocol stack instance is called from the preset protocol stack library according to the protocol type; Load the protocol stack instance into the runtime area and configure the communication parameters corresponding to the target interface, wherein the communication parameters include the communication rate, data bit width, and stop bit count; After the communication parameters are configured, the send enable signal and interrupt service routine of the target interface are restored, and the handshake sequence for reloading the protocol stack is started.
[0009] In this scheme, the protocol stack reloading executes the handshake sequence according to a preset interface priority order, specifically including: The preferred interface and alternative interfaces are determined based on the first flag status; The first handshake request is sent sequentially on the preferred interface, and the first timeout timer is started. If a standard response code corresponding to the first handshake request is received on the preferred interface before the first timeout timer expires, the first feature response is confirmed to be valid, and the protocol stack corresponding to the preferred interface is locked. If the first timeout timer expires, a second handshake request is sent on the alternative interface, and a second timeout timer is started. If a standard response code corresponding to the second handshake request is received on the alternative interface before the second timeout timer expires, the second characteristic response is confirmed to be valid, and the protocol stack corresponding to the alternative interface is locked. If the second timeout timer times out, the handshake sequence is repeated until the preset maximum number of retries is reached.
[0010] In this solution, the step of encapsulating the data into a data frame according to the updated protocol stack specifically includes: Obtain the pulse count value and direction flag bit of the trackball X-axis encoder, and the pulse count value and direction flag bit of the Y-axis encoder; Obtain the current scan code of the button diode matrix, and generate a button status flag bit group based on the current scan code; If the updated protocol stack is of the first protocol type, then the pulse count value, the direction flag bit and the key status flag bit group are arranged according to the first data packet format to generate a first data frame, wherein the first data frame includes a device descriptor and a report descriptor; If the updated protocol stack is the second protocol type, then the pulse count value, the direction flag bit and the key status flag bit group are arranged according to the second data packet format to generate a second data frame, wherein the second data frame contains a device identifier and a status byte; The generated data frame is stored in the send buffer of the corresponding interface, triggering a send completion interrupt.
[0011] This solution also includes the following during the operation of the currently running protocol stack: The current level value of the switching switch is read according to the set polling cycle, and the current level value is compared with the historical level value. If the current level value is inconsistent with the historical level value, it is determined that the first flag state has changed, and the data transmission task of the currently running protocol stack is immediately suspended. After determining that the first flag state has changed, the second flag state is forcibly set to a conflict state, triggering the protocol switching event; After the protocol stack is reloaded, the task is sent based on the updated protocol stack data. If the current voltage level is consistent with the historical voltage level, the current running protocol stack remains unchanged, and the data acquisition, encapsulation, and transmission process continues.
[0012] A second aspect of the present invention provides an adaptive interface control system for a dual-mode trackball, including an adaptive interface control method program for the dual-mode trackball, wherein the adaptive interface control method program for the dual-mode trackball, when executed by the processor, implements the following steps: Obtain the first flag state of the switch and the second flag state of the bus signal; If the first flag state matches the currently running protocol stack, and the second flag state indicates that it is in an idle state, then the current protocol stack is maintained. If the first flag state does not match the currently running protocol stack, or the second flag state indicates a conflict, a protocol switching event is triggered. In response to the protocol switching event, stop the current protocol stack from sending and receiving data, clear the communication buffer, and reload the protocol stack for the target interface; The protocol stack reloading executes the handshake sequence according to the preset interface priority order. The first handshake sequence sends the device identification request according to the first interface protocol timing and waits for the first feature response. The second handshake sequence sends the device identification request according to the second interface protocol timing and waits for the second feature response. When the first feature response or the second feature response is confirmed, the protocol stack is locked and updated. Once the updated protocol stack takes effect, trackball displacement signals and button status signals are collected, encapsulated into data frames according to the updated protocol stack, and transmitted to the host.
[0013] A third aspect of the present invention provides a computer-readable storage medium comprising an adaptive interface control method program for a dual-mode trackball, wherein when the adaptive interface control method program for the dual-mode trackball is executed by a processor, it implements the steps of the adaptive interface control method for the dual-mode trackball as described in any of the preceding claims.
[0014] This invention provides an adaptive interface control method, system, and storage medium for a dual-mode trackball. It acquires the first flag state of the switch and the second flag state of the bus signal in real time. When the first flag state does not match the currently running protocol stack or the second flag state indicates a conflict, a protocol switching event is triggered. This leads to protocol stack reloading, execution of a handshake sequence according to a preset interface priority order to confirm the validity of the target interface, locking and updating the protocol stack, acquiring trackball displacement signals and button status signals, and encapsulating them into data frames according to the updated protocol stack for output to the host. This invention enables the trackball to adaptively switch interface protocols without power interruption during energized operation, effectively improving the trackball device's adaptability under different interface standards and operational continuity in industrial scenarios. Attached Figure Description
[0015] To more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings used in the embodiments will be briefly described below. It should be understood that the following drawings only show some embodiments of the present invention and should not be regarded as a limitation on the scope.
[0016] Figure 1 A circuit diagram of a multi-level protection power supply module is shown; Figure 2 A circuit diagram of an APM32 main control module is shown. Figure 3 A circuit diagram of a trackball rotation encoding module is shown; Figure 4 A circuit diagram of a dual-interface output module and a mode switching DIP switch is shown. Figure 5 A flowchart of an adaptive interface control method for a dual-mode trackball according to the present invention is shown; Figure 6 A block diagram of an adaptive interface control system for a dual-mode trackball according to the present invention is shown. Detailed Implementation
[0017] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0018] Unless otherwise defined, all terms (including technical and scientific terms) used in embodiments of this invention shall have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains. It should also be understood that terms such as those defined in a common dictionary shall be interpreted as having a meaning consistent with their meaning in the context of the relevant art, and not as being interpreted in an idealized or highly formalized sense, unless expressly defined in this embodiment of the invention.
[0019] The terms "first," "second," and similar words used in the embodiments of this invention do not indicate any order, quantity, or importance, but are merely used to distinguish different components. Terms such as "an," "a," or "the" do not indicate a quantity limitation, but rather indicate the presence of at least one. Similarly, terms such as "including" or "comprising" mean that the element or object preceding the word encompasses the elements or objects listed after the word and their equivalents, without excluding other elements or objects. Terms such as "connected" or "linked" are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect. The steps preceding or following the steps in the method of the embodiments of this invention are not necessarily performed precisely in sequence. Instead, various steps can be processed in reverse order or simultaneously. Furthermore, other operations can be added to these processes, or one or more steps can be removed from these processes.
[0020] In addition, the functional modules in the various embodiments of the present invention can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.
[0021] An adaptive interface control circuit for a dual-mode trackball, the control circuit comprising a multi-level protection power supply module, an APM32 main control module, a mode switching DIP switch, a dual-interface output module, a trackball rotation encoding module, and a button diode matrix module.
[0022] Figure 1 A circuit diagram of a multi-level protection power supply module is shown, such as... Figure 1As shown, the input terminal of the multi-level protection power supply module is connected to an external power source, and its output terminal outputs the operating voltage. Specifically, the input terminal of the multi-level protection power supply module is connected to an external industrial power source. It internally includes a varistor, a TVS transient voltage suppressor, a common-mode inductor, and multi-level filter capacitors, which are used to perform surge suppression, electrostatic discharge, and noise filtering on the input voltage. Its output terminal outputs a stable operating voltage.
[0023] Figure 2 A circuit diagram of an APM32 main control module is shown, such as... Figure 2 As shown, the power supply terminal of the APM32 main control module is connected to the operating voltage. The APM32 main control circuit has a built-in hardware quadrature counting channel, a USB physical layer interface, and a UART interface. Specifically, the APM32 main control module, as the core logic processing unit, has its power supply terminal connected to the output terminal of the multi-level protection power supply module to obtain the operating voltage. This main control circuit has a built-in hardware quadrature counting channel, a USB physical layer interface, and a UART interface. The hardware quadrature counting channel is used to directly acquire quadrature pulse signals, the USB physical layer interface is used to support the USB communication protocol, and the UART interface is used to support the PS / 2 communication protocol.
[0024] Figure 3 A circuit diagram of a trackball rotation encoding module is shown, as follows: Figure 3 As shown, the signal output terminal of the trackball rotation encoding module is connected to the input terminal of the hardware quadrature counting channel. Specifically, the signal output terminal of the trackball rotation encoding circuit is connected to the input terminal of the hardware quadrature counting channel. This circuit includes an X-axis encoder, a Y-axis encoder, and a roller encoder, which are used to convert the rotational motion of the trackball into A / B phase quadrature pulse signals and send them to the main control circuit.
[0025] Figure 4 A circuit diagram of a dual-interface output module and a mode switching DIP switch is shown, as follows: Figure 4 As shown, the dual-interface output module includes a first interface and a second interface. The data pins and clock pins of the first interface are connected to the corresponding pins of the UART interface, and the differential data pins of the second interface are connected to the corresponding pins of the USB physical layer interface. A mode switching DIP switch has its level output terminal connected to the interrupt input terminal of the APM32 main control circuit. Specifically, the dual-interface output circuit includes a first interface and a second interface. The data pins and clock pins of the first interface are connected to the corresponding pins of the UART interface to support PS / 2 protocol transmission, and the differential data pins of the second interface are connected to the corresponding pins of the USB physical layer interface to support USB protocol transmission.
[0026] The button diode matrix module has its scan output connected to the GPIO input of the APM32 main control circuit. Specifically, the scan output of the button diode matrix circuit is connected to the GPIO input of the APM32 main control circuit. This circuit uses a diode isolation array structure to collect the button action signals of the left button, right button, and scroll wheel middle button. The level output of the mode switching DIP switch is connected to the interrupt input of the APM32 main control circuit to provide the main control circuit with a level signal indicating the physical switching intention.
[0027] During operation, the APM32 main control circuit responds to the level change interrupt of the mode switching DIP switch and the level change of the bus signal of the dual-interface output circuit, initiates the protocol stack switching operation process, and after the protocol switching is completed, encapsulates the displacement signal collected by the trackball rotation encoding circuit and the key signal collected by the key diode matrix circuit according to the switched protocol stack, and finally outputs it to the outside through the dual-interface output circuit.
[0028] Figure 5 A flowchart of an adaptive interface control method for a dual-mode trackball according to the present invention is shown.
[0029] like Figure 5 As shown, the first aspect of this invention discloses an adaptive interface control method for a dual-mode trackball, the method comprising: S502, obtain the first flag state of the switch and the second flag state of the bus signal; S504, if the first flag state matches the currently running protocol stack, and the second flag state indicates that it is in an idle state, then the current protocol stack is maintained; S506, If the first flag state does not match the currently running protocol stack, or the second flag state indicates a conflict, then a protocol switching event is triggered. S508, in response to the protocol switching event, stop the data transmission and reception of the current protocol stack, clear the communication buffer, and reload the protocol stack for the target interface; S510, the protocol stack reloading executes the handshake sequence according to the preset interface priority order, wherein the first handshake sequence sends the device identification request according to the first interface protocol timing and waits for the first feature response, and the second handshake sequence sends the device identification request according to the second interface protocol timing and waits for the second feature response; S512, when the first feature response or the second feature response is confirmed, the protocol stack is locked and updated; S514, after the updated protocol stack takes effect, collects trackball displacement signals and button status signals, encapsulates them into data frames according to the updated protocol stack, and transmits them to the host.
[0030] It should be noted that this embodiment is applied to the execution of a control circuit built based on the APM32 main control chip. After the system is powered on and initialized, the main control chip first reads the current level value of the mode switching DIP switch through its GPIO pin to obtain the first flag state, and then detects the data line level and clock line level of the first and second interfaces in the dual-interface output circuit in parallel through its USB physical layer interface and UART interface to obtain the second flag state. The state machine inside the main control chip makes decisions based on the matching of the first flag state with the currently running protocol stack and the bus occupancy status indicated by the second flag state. When the first flag state matches the currently running protocol stack and the second flag state indicates an idle state, the state machine maintains the current protocol stack unchanged, and the trackball normally collects displacement signals and button signals and encapsulates and outputs them according to the current protocol. When the first flag state changes or the second flag state indicates a bus conflict, the state machine triggers a protocol switching event, immediately stops all data transmission and reception operations of the current protocol stack, and clears the data residue in the communication buffer. Subsequently, the main control chip initiates the protocol stack reloading process according to the preset interface priority order. Specifically, it first sends a device identification request on the first interface according to the first interface protocol timing and waits for the corresponding feature response. If no response is received within the timeout period, it sends a device identification request on the second interface according to the second interface protocol timing and waits for the corresponding feature response. When the main control chip successfully confirms the corresponding feature response on any interface, it immediately locks the protocol stack corresponding to that interface and updates the currently running protocol stack to that protocol stack. After the protocol stack takes effect, the main control chip collects the displacement signal output by the trackball rotation encoding circuit through a hardware quadrature counter, and scans the button status signal output by the button diode matrix circuit through GPIO. It encapsulates the displacement signal and button status signal into a data frame according to the data packet format corresponding to the updated protocol stack, and finally transmits it to the host through the corresponding interface in the dual-interface output circuit. This implementation realizes real-time adaptive switching of the interface protocol of the trackball in the powered-on state through the above process, without power failure or restart, effectively ensuring the control continuity of industrial field equipment during continuous operation.
[0031] According to an embodiment of the present invention, obtaining the first flag state of the switching switch and the second flag state of the bus signal specifically includes: After power-on initialization, the level value of the switch is read, and the first flag state is generated based on the level value. The first flag state includes a first mode code or a second mode code. The data line level and clock line level of the first interface are detected in parallel. If the data line level and the clock line level are both at a high level, the second flag state is determined to be idle. If the data line level or the clock line level is in a low level state for a duration exceeding a preset conflict determination time, then the second flag state is determined to be a conflict state. When the transition edge of the data line level or the clock line level of the first interface is captured, the second flag state is updated to bus active state; Based on the first flag state and the second flag state, the matching status of the currently running protocol stack and the priority order of the target interface are determined.
[0032] It should be noted that in this embodiment, after the system powers on and completes initialization configuration, the APM32 main control chip immediately reads the level signal output by the mode switching DIP switch through its GPIO port. If the level is high, a first mode code is generated as the first flag state; if it is low, a second mode code is generated as the first flag state. Simultaneously, the main control chip performs parallel detection on the first and second interfaces through its UART and USB physical layer interfaces, respectively: For the first interface, the main control chip continuously monitors the level states of its data line pins and clock line pins. When both signal lines are detected to be high simultaneously, the bus is determined to be in an idle state. When any signal line is detected to be low for a duration exceeding a preset conflict determination time, the bus is determined to be in a conflict state. When a level transition edge is detected on the data line or clock line, the second flag state is updated to the bus active state. After completing the above detection, the main control chip determines the currently desired protocol type based on the mode code carried in the first flag state, and determines the current availability of each interface based on the bus state carried in the second flag state. Combining both, it determines whether the currently running protocol stack matches the first flag state, and simultaneously determines the priority order of the target interfaces during protocol switching. This implementation ensures that the main control chip can perceive external switching intentions and bus physical status in real time through the above-described parallel detection and status determination process, providing an accurate status input basis for subsequent protocol switching decisions.
[0033] According to an embodiment of the present invention, after triggering the protocol switching event, the method further includes: Pause the interrupt service routine of the currently running protocol stack and disable the enable signal sent by the interface corresponding to the currently running protocol stack; Force the protocol stack state machine of the currently running protocol stack to jump to the standby state and release the communication peripheral resources occupied by the currently running protocol stack; The protocol type of the target interface is parsed, and the corresponding protocol stack instance is called from the preset protocol stack library according to the protocol type; Load the protocol stack instance into the runtime area and configure the communication parameters corresponding to the target interface, wherein the communication parameters include the communication rate, data bit width, and number of stop bits; After the communication parameters are configured, the send enable signal and interrupt service routine of the target interface are restored, and the handshake sequence for reloading the protocol stack is started.
[0034] It should be noted that, in this embodiment, when the state machine inside the main control chip determines that a protocol switching event needs to be triggered, the main control chip first performs a suspension operation on the currently running protocol stack. Specifically, it suspends the interrupt service routine corresponding to the currently running protocol stack and writes a disable instruction to the transmit enable control register of the current interface to disable the data transmission function of that interface. Subsequently, the main control chip forcibly jumps the state machine of the currently running protocol stack from the running state to the standby state and releases the communication peripheral resources occupied by the protocol stack back to the system resource pool. These communication peripheral resources include, but are not limited to, transmit buffers, receive buffers, and DMA transfer channels. After the resource release is completed, the main control chip parses the protocol type identifier corresponding to the target interface and calls the corresponding protocol stack instance code from the protocol stack library preset in the firmware storage area according to the protocol type identifier. The main control chip loads the protocol stack instance into the system runtime area and configures the communication parameters according to the hardware characteristics of the target interface, including the communication rate, data bit width, and number of stop bits. After all communication parameters are configured, the main control chip restores the transmit enable signal and interrupt service routine of the target interface, and then starts the protocol stack reload handshake sequence. This implementation method, through the above-described orderly resource switching process, ensures the complete release and reallocation of system resources during the protocol switching process, avoiding resource conflicts and data corruption during the switching process.
[0035] According to an embodiment of the present invention, the protocol stack reloading executes the handshake sequence according to a preset interface priority order, specifically including: The preferred interface and alternative interfaces are determined based on the first flag status; The first handshake request is sent sequentially on the preferred interface, and the first timeout timer is started. If a standard response code corresponding to the first handshake request is received on the preferred interface before the first timeout timer expires, the first feature response is confirmed to be valid, and the protocol stack corresponding to the preferred interface is locked. If the first timeout timer expires, a second handshake request is sent on the alternative interface, and a second timeout timer is started. If a standard response code corresponding to the second handshake request is received on the alternative interface before the second timeout timer expires, the second characteristic response is confirmed to be valid, and the protocol stack corresponding to the alternative interface is locked. If the second timeout timer times out, the handshake sequence is repeated until the preset maximum number of retries is reached.
[0036] It should be noted that in this embodiment, after the main control chip initiates the protocol stack reload handshake sequence, it first determines the preferred interface and the alternative interface based on the current first flag state. The preferred interface corresponds to the interface corresponding to the target mode indicated by the first flag state, and the alternative interface corresponds to another interface. The main control chip sends a first handshake request message on the preferred interface according to the protocol timing of that interface, and immediately starts a first timeout timer after sending. The main control chip then enters a waiting state, listening for data reception events on the preferred interface. If the main control chip successfully receives the standard response code corresponding to the first handshake request on the preferred interface before the first timeout timer expires, it confirms that the first characteristic response is valid and immediately locks the protocol stack corresponding to the preferred interface as the currently running protocol stack. If no valid response is received after the first timeout timer expires, the main control chip switches to the alternative interface handshake process, sends a second handshake request on the alternative interface, and starts a second timeout timer. If the corresponding standard response code is received before the second timeout timer expires, it confirms that the second characteristic response is valid and locks the alternative interface protocol stack. If the second timeout timer also times out, the main control chip repeats the above handshake sequence until the preset retry limit is reached. This implementation improves the handshake success rate and system fault tolerance through a timeout retry mechanism and a priority scheduling mechanism.
[0037] According to an embodiment of the present invention, the step of encapsulating the data frame according to the updated protocol stack specifically includes: Obtain the pulse count value and direction flag bit of the trackball X-axis encoder, and the pulse count value and direction flag bit of the Y-axis encoder; Obtain the current scan code of the button diode matrix, and generate a button status flag bit group based on the current scan code; If the updated protocol stack is of the first protocol type, then the pulse count value, the direction flag bit and the key status flag bit group are arranged according to the first data packet format to generate a first data frame, wherein the first data frame includes a device descriptor and a report descriptor; If the updated protocol stack is the second protocol type, then the pulse count value, the direction flag bit and the key status flag bit group are arranged according to the second data packet format to generate a second data frame, wherein the second data frame contains a device identifier and a status byte; The generated data frame is stored in the send buffer of the corresponding interface, triggering a send completion interrupt.
[0038] It should be noted that in this embodiment, after the protocol stack update is completed and takes effect, the main control chip initiates the data acquisition and encapsulation process. The main control chip reads the pulse count value and its corresponding direction flag bit output by the trackball X-axis encoder and the corresponding direction flag bit output by the Y-axis encoder through its hardware quadrature counter. At the same time, it obtains the current key scan code by scanning the key diode matrix circuit through the GPIO port, and parses the pressed or released state of each key according to the scan code to generate a key status flag bit group. After completing the data acquisition, the main control chip determines the type of the updated protocol stack: if it is the first protocol type, it arranges and assembles the X-axis pulse count value, Y-axis pulse count value, X-axis direction flag bit, Y-axis direction flag bit and key status flag bit group into a first data frame according to the first data packet format, and adds a device descriptor and a report descriptor to the header of the data frame; if it is the second protocol type, it arranges and assembles the above displacement data and key data into a second data frame according to the second data packet format, and embeds a device identifier and a status byte in the data frame. After the data frame is assembled, the main control chip stores it in the corresponding interface's transmit buffer and sends a trigger signal to the transmit controller to initiate the actual data transmission operation. Upon completion of transmission, a transmission completion interrupt is generated to notify the main control chip. This implementation achieves compatible output for two different interface protocols using a protocol-adaptive data encapsulation process.
[0039] According to an embodiment of the present invention, during the operation of the currently running protocol stack, the following is further included: The current level value of the switching switch is read according to the set polling cycle, and the current level value is compared with the historical level value. If the current level value is inconsistent with the historical level value, it is determined that the first flag state has changed, and the data transmission task of the currently running protocol stack is immediately suspended. After determining that the first flag state has changed, the second flag state is forcibly set to a conflict state, triggering the protocol switching event; After the protocol stack is reloaded, the task is sent based on the updated protocol stack data. If the current voltage level is consistent with the historical voltage level, the current running protocol stack remains unchanged, and the data acquisition, encapsulation, and transmission process continues.
[0040] It should be noted that, in this embodiment, during the normal operation of the current protocol stack, the main control chip continuously performs a running status monitoring task. The main control chip periodically reads the current level value of the mode switching DIP switch through the GPIO port according to a preset polling cycle, and compares this current level value with the historical level value recorded before the polling cycle. If the comparison result is consistent, it indicates that the switch state has not changed, and the main control chip maintains the current running protocol stack unchanged, continuing to execute the acquisition, encapsulation, and transmission process of the trackball displacement signal and button status signal. If the comparison result is inconsistent, it indicates that the user has toggled the switch during device operation. At this time, the main control chip determines that the first flag state has changed, immediately suspends all data transmission tasks of the currently running protocol stack, and pauses the transmission of any data frames to the host. Subsequently, the main control chip forcibly sets the second flag state to a conflict state, thereby triggering the execution of the protocol switching event. After the protocol switching event is triggered, the main control chip completes the protocol stack reloading according to the aforementioned process, and resumes the execution of the data transmission task according to the updated protocol stack after the reloading is completed. This implementation achieves real-time response to user switching operations by continuously monitoring the status of the switch during operation, and avoids sending data in the wrong format to the host during protocol switching by suspending the data transmission task.
[0041] It is worth mentioning that the protocol stack reload execution process also includes: When neither the first handshake sequence nor the second handshake sequence receives the first feature response or the second feature response, the current count value is incremented. If the current count value does not exceed the preset retry threshold, the handshake sequence is repeated according to the interface priority order; If the current count value exceeds the preset retry threshold, the target interface is marked as unavailable and a degradation process is executed. The degradation process is as follows: read the historical protocol stack operation record, and restore the protocol stack corresponding to the most recent successful handshake recorded in the historical protocol stack operation record as the current running protocol stack. After restoring the currently running protocol stack, the protocol stack is maintained until the next protocol switching event is triggered, and handshake retry for the target interface is suspended while the target interface is marked as unavailable.
[0042] It should be noted that, in this embodiment, during the protocol stack reloading process, when the main control chip detects that neither the first handshake sequence nor the second handshake sequence has received a valid characteristic response, it determines that the current handshake attempt has failed. The main control chip increments the internally maintained handshake count and compares this count with a preset retry threshold. If the current count does not exceed the retry threshold, the main control chip clears the receive and transmit buffers of each interface and re-executes the handshake sequence according to the interface priority order. If the current count exceeds the retry threshold, the main control chip marks the current target interface as unavailable and initiates a degradation process. In the degradation process, the main control chip accesses the historical protocol stack execution record stored in non-volatile memory, extracts the interface type information of the most recently successfully handshake from the record, and then restores the corresponding protocol stack as the currently running protocol stack based on the interface type. After the protocol stack restoration is completed, the main control chip maintains the protocol stack in continuous operation until the next protocol switching event is triggered. While the target interface is marked as unavailable, the main control chip suspends all handshake retry operations for that target interface to avoid wasting system resources due to invalid retries. This implementation, through a degradation processing mechanism and a historical record backtracking mechanism, ensures that the trackball remains available even when the target interface fails or cannot be connected, effectively improving the fault tolerance capability of the device.
[0043] Figure 6 A block diagram of an adaptive interface control system for a dual-mode trackball according to the present invention is shown.
[0044] like Figure 6 As shown, the second aspect of the present invention discloses an adaptive interface control system 6 for a dual-mode trackball, including a memory 61 and a processor 62. The memory includes an adaptive interface control method program for the dual-mode trackball. When the processor executes the adaptive interface control method program for the dual-mode trackball, it performs the following steps: Obtain the first flag state of the switch and the second flag state of the bus signal; If the first flag state matches the currently running protocol stack, and the second flag state indicates that it is in an idle state, then the current protocol stack is maintained. If the first flag state does not match the currently running protocol stack, or the second flag state indicates a conflict, a protocol switching event is triggered. In response to the protocol switching event, stop the current protocol stack from sending and receiving data, clear the communication buffer, and reload the protocol stack for the target interface; The protocol stack reloading executes the handshake sequence according to the preset interface priority order. The first handshake sequence sends the device identification request according to the first interface protocol timing and waits for the first feature response. The second handshake sequence sends the device identification request according to the second interface protocol timing and waits for the second feature response. When the first feature response or the second feature response is confirmed, the protocol stack is locked and updated. Once the updated protocol stack takes effect, trackball displacement signals and button status signals are collected, encapsulated into data frames according to the updated protocol stack, and transmitted to the host.
[0045] It should be noted that this embodiment is applied to the execution of a control circuit built based on the APM32 main control chip. After the system is powered on and initialized, the main control chip first reads the current level value of the mode switching DIP switch through its GPIO pin to obtain the first flag state, and then detects the data line level and clock line level of the first and second interfaces in the dual-interface output circuit in parallel through its USB physical layer interface and UART interface to obtain the second flag state. The state machine inside the main control chip makes decisions based on the matching of the first flag state with the currently running protocol stack and the bus occupancy status indicated by the second flag state. When the first flag state matches the currently running protocol stack and the second flag state indicates an idle state, the state machine maintains the current protocol stack unchanged, and the trackball normally collects displacement signals and button signals and encapsulates and outputs them according to the current protocol. When the first flag state changes or the second flag state indicates a bus conflict, the state machine triggers a protocol switching event, immediately stops all data transmission and reception operations of the current protocol stack, and clears the data residue in the communication buffer. Subsequently, the main control chip initiates the protocol stack reloading process according to the preset interface priority order. Specifically, it first sends a device identification request on the first interface according to the first interface protocol timing and waits for the corresponding feature response. If no response is received within the timeout period, it sends a device identification request on the second interface according to the second interface protocol timing and waits for the corresponding feature response. When the main control chip successfully confirms the corresponding feature response on any interface, it immediately locks the protocol stack corresponding to that interface and updates the currently running protocol stack to that protocol stack. After the protocol stack takes effect, the main control chip collects the displacement signal output by the trackball rotation encoding circuit through a hardware quadrature counter, and scans the button status signal output by the button diode matrix circuit through GPIO. It encapsulates the displacement signal and button status signal into a data frame according to the data packet format corresponding to the updated protocol stack, and finally transmits it to the host through the corresponding interface in the dual-interface output circuit. This implementation realizes real-time adaptive switching of the interface protocol of the trackball in the powered-on state through the above process, without power failure or restart, effectively ensuring the control continuity of industrial field equipment during continuous operation.
[0046] According to an embodiment of the present invention, obtaining the first flag state of the switching switch and the second flag state of the bus signal specifically includes: After power-on initialization, the level value of the switch is read, and the first flag state is generated based on the level value. The first flag state includes a first mode code or a second mode code. The data line level and clock line level of the first interface are detected in parallel. If the data line level and the clock line level are both at a high level, the second flag state is determined to be idle. If the data line level or the clock line level is in a low level state for a duration exceeding a preset conflict determination time, then the second flag state is determined to be a conflict state. When the transition edge of the data line level or the clock line level of the first interface is captured, the second flag state is updated to bus active state; Based on the first flag state and the second flag state, the matching status of the currently running protocol stack and the priority order of the target interface are determined.
[0047] It should be noted that in this embodiment, after the system powers on and completes initialization configuration, the APM32 main control chip immediately reads the level signal output by the mode switching DIP switch through its GPIO port. If the level is high, a first mode code is generated as the first flag state; if it is low, a second mode code is generated as the first flag state. Simultaneously, the main control chip performs parallel detection on the first and second interfaces through its UART and USB physical layer interfaces, respectively: For the first interface, the main control chip continuously monitors the level states of its data line pins and clock line pins. When both signal lines are detected to be high simultaneously, the bus is determined to be in an idle state. When any signal line is detected to be low for a duration exceeding a preset conflict determination time, the bus is determined to be in a conflict state. When a level transition edge is detected on the data line or clock line, the second flag state is updated to the bus active state. After completing the above detection, the main control chip determines the currently desired protocol type based on the mode code carried in the first flag state, and determines the current availability of each interface based on the bus state carried in the second flag state. Combining both, it determines whether the currently running protocol stack matches the first flag state, and simultaneously determines the priority order of the target interfaces during protocol switching. This implementation ensures that the main control chip can perceive external switching intentions and bus physical status in real time through the above-described parallel detection and status determination process, providing an accurate status input basis for subsequent protocol switching decisions.
[0048] According to an embodiment of the present invention, after triggering the protocol switching event, the method further includes: Pause the interrupt service routine of the currently running protocol stack and disable the enable signal sent by the interface corresponding to the currently running protocol stack; Force the protocol stack state machine of the currently running protocol stack to jump to the standby state and release the communication peripheral resources occupied by the currently running protocol stack; The protocol type of the target interface is parsed, and the corresponding protocol stack instance is called from the preset protocol stack library according to the protocol type; Load the protocol stack instance into the runtime area and configure the communication parameters corresponding to the target interface, wherein the communication parameters include the communication rate, data bit width, and number of stop bits; After the communication parameters are configured, the send enable signal and interrupt service routine of the target interface are restored, and the handshake sequence for reloading the protocol stack is started.
[0049] It should be noted that, in this embodiment, when the state machine inside the main control chip determines that a protocol switching event needs to be triggered, the main control chip first performs a suspension operation on the currently running protocol stack. Specifically, it suspends the interrupt service routine corresponding to the currently running protocol stack and writes a disable instruction to the transmit enable control register of the current interface to disable the data transmission function of that interface. Subsequently, the main control chip forcibly jumps the state machine of the currently running protocol stack from the running state to the standby state and releases the communication peripheral resources occupied by the protocol stack back to the system resource pool. These communication peripheral resources include, but are not limited to, transmit buffers, receive buffers, and DMA transfer channels. After the resource release is completed, the main control chip parses the protocol type identifier corresponding to the target interface and calls the corresponding protocol stack instance code from the protocol stack library preset in the firmware storage area according to the protocol type identifier. The main control chip loads the protocol stack instance into the system runtime area and configures the communication parameters according to the hardware characteristics of the target interface, including the communication rate, data bit width, and number of stop bits. After all communication parameters are configured, the main control chip restores the transmit enable signal and interrupt service routine of the target interface, and then starts the protocol stack reload handshake sequence. This implementation method, through the above-described orderly resource switching process, ensures the complete release and reallocation of system resources during the protocol switching process, avoiding resource conflicts and data corruption during the switching process.
[0050] According to an embodiment of the present invention, the protocol stack reloading executes the handshake sequence according to a preset interface priority order, specifically including: The preferred interface and alternative interfaces are determined based on the first flag status; The first handshake request is sent sequentially on the preferred interface, and the first timeout timer is started. If a standard response code corresponding to the first handshake request is received on the preferred interface before the first timeout timer expires, the first feature response is confirmed to be valid, and the protocol stack corresponding to the preferred interface is locked. If the first timeout timer expires, a second handshake request is sent on the alternative interface, and a second timeout timer is started. If a standard response code corresponding to the second handshake request is received on the alternative interface before the second timeout timer expires, the second characteristic response is confirmed to be valid, and the protocol stack corresponding to the alternative interface is locked. If the second timeout timer times out, the handshake sequence is repeated until the preset maximum number of retries is reached.
[0051] It should be noted that in this embodiment, after the main control chip initiates the protocol stack reload handshake sequence, it first determines the preferred interface and the alternative interface based on the current first flag state. The preferred interface corresponds to the interface corresponding to the target mode indicated by the first flag state, and the alternative interface corresponds to another interface. The main control chip sends a first handshake request message on the preferred interface according to the protocol timing of that interface, and immediately starts a first timeout timer after sending. The main control chip then enters a waiting state, listening for data reception events on the preferred interface. If the main control chip successfully receives the standard response code corresponding to the first handshake request on the preferred interface before the first timeout timer expires, it confirms that the first characteristic response is valid and immediately locks the protocol stack corresponding to the preferred interface as the currently running protocol stack. If no valid response is received after the first timeout timer expires, the main control chip switches to the alternative interface handshake process, sends a second handshake request on the alternative interface, and starts a second timeout timer. If the corresponding standard response code is received before the second timeout timer expires, it confirms that the second characteristic response is valid and locks the alternative interface protocol stack. If the second timeout timer also times out, the main control chip repeats the above handshake sequence until the preset retry limit is reached. This implementation improves the handshake success rate and system fault tolerance through a timeout retry mechanism and a priority scheduling mechanism.
[0052] According to an embodiment of the present invention, the step of encapsulating the data frame according to the updated protocol stack specifically includes: Obtain the pulse count value and direction flag bit of the trackball X-axis encoder, and the pulse count value and direction flag bit of the Y-axis encoder; Obtain the current scan code of the button diode matrix, and generate a button status flag bit group based on the current scan code; If the updated protocol stack is of the first protocol type, then the pulse count value, the direction flag bit and the key status flag bit group are arranged according to the first data packet format to generate a first data frame, wherein the first data frame includes a device descriptor and a report descriptor; If the updated protocol stack is the second protocol type, then the pulse count value, the direction flag bit and the key status flag bit group are arranged according to the second data packet format to generate a second data frame, wherein the second data frame contains a device identifier and a status byte; The generated data frame is stored in the send buffer of the corresponding interface, triggering a send completion interrupt.
[0053] It should be noted that in this embodiment, after the protocol stack update is completed and takes effect, the main control chip initiates the data acquisition and encapsulation process. The main control chip reads the pulse count value and its corresponding direction flag bit output by the trackball X-axis encoder and the corresponding direction flag bit output by the Y-axis encoder through its hardware quadrature counter. At the same time, it obtains the current key scan code by scanning the key diode matrix circuit through the GPIO port, and parses the pressed or released state of each key according to the scan code to generate a key status flag bit group. After completing the data acquisition, the main control chip determines the type of the updated protocol stack: if it is the first protocol type, it arranges and assembles the X-axis pulse count value, Y-axis pulse count value, X-axis direction flag bit, Y-axis direction flag bit and key status flag bit group into a first data frame according to the first data packet format, and adds a device descriptor and a report descriptor to the header of the data frame; if it is the second protocol type, it arranges and assembles the above displacement data and key data into a second data frame according to the second data packet format, and embeds a device identifier and a status byte in the data frame. After the data frame is assembled, the main control chip stores it in the corresponding interface's transmit buffer and sends a trigger signal to the transmit controller to initiate the actual data transmission operation. Upon completion of transmission, a transmission completion interrupt is generated to notify the main control chip. This implementation achieves compatible output for two different interface protocols using a protocol-adaptive data encapsulation process.
[0054] According to an embodiment of the present invention, during the operation of the currently running protocol stack, the following is further included: The current level value of the switching switch is read according to the set polling cycle, and the current level value is compared with the historical level value. If the current level value is inconsistent with the historical level value, it is determined that the first flag state has changed, and the data transmission task of the currently running protocol stack is immediately suspended. After determining that the first flag state has changed, the second flag state is forcibly set to a conflict state, triggering the protocol switching event; After the protocol stack is reloaded, the task is sent based on the updated protocol stack data. If the current voltage level is consistent with the historical voltage level, the current running protocol stack remains unchanged, and the data acquisition, encapsulation, and transmission process continues.
[0055] It should be noted that, in this embodiment, during the normal operation of the current protocol stack, the main control chip continuously performs a running status monitoring task. The main control chip periodically reads the current level value of the mode switching DIP switch through the GPIO port according to a preset polling cycle, and compares this current level value with the historical level value recorded before the polling cycle. If the comparison result is consistent, it indicates that the switch state has not changed, and the main control chip maintains the current running protocol stack unchanged, continuing to execute the acquisition, encapsulation, and transmission process of the trackball displacement signal and button status signal. If the comparison result is inconsistent, it indicates that the user has toggled the switch during device operation. At this time, the main control chip determines that the first flag state has changed, immediately suspends all data transmission tasks of the currently running protocol stack, and pauses the transmission of any data frames to the host. Subsequently, the main control chip forcibly sets the second flag state to a conflict state, thereby triggering the execution of the protocol switching event. After the protocol switching event is triggered, the main control chip completes the protocol stack reloading according to the aforementioned process, and resumes the execution of the data transmission task according to the updated protocol stack after the reloading is completed. This implementation achieves real-time response to user switching operations by continuously monitoring the status of the switch during operation, and avoids sending data in the wrong format to the host during protocol switching by suspending the data transmission task.
[0056] It is worth mentioning that the protocol stack reload execution process also includes: When neither the first handshake sequence nor the second handshake sequence receives the first feature response or the second feature response, the current count value is incremented. If the current count value does not exceed the preset retry threshold, the handshake sequence is repeated according to the interface priority order; If the current count value exceeds the preset retry threshold, the target interface is marked as unavailable and a degradation process is executed. The degradation process is as follows: read the historical protocol stack operation record, and restore the protocol stack corresponding to the most recent successful handshake recorded in the historical protocol stack operation record as the current running protocol stack. After restoring the currently running protocol stack, the protocol stack is maintained until the next protocol switching event is triggered, and handshake retry for the target interface is suspended while the target interface is marked as unavailable.
[0057] It should be noted that, in this embodiment, during the protocol stack reloading process, when the main control chip detects that neither the first handshake sequence nor the second handshake sequence has received a valid characteristic response, it determines that the current handshake attempt has failed. The main control chip increments the internally maintained handshake count and compares this count with a preset retry threshold. If the current count does not exceed the retry threshold, the main control chip clears the receive and transmit buffers of each interface and re-executes the handshake sequence according to the interface priority order. If the current count exceeds the retry threshold, the main control chip marks the current target interface as unavailable and initiates a degradation process. In the degradation process, the main control chip accesses the historical protocol stack execution record stored in non-volatile memory, extracts the interface type information of the most recently successfully handshake from the record, and then restores the corresponding protocol stack as the currently running protocol stack based on the interface type. After the protocol stack restoration is completed, the main control chip maintains the protocol stack in continuous operation until the next protocol switching event is triggered. While the target interface is marked as unavailable, the main control chip suspends all handshake retry operations for that target interface to avoid wasting system resources due to invalid retries. This implementation, through a degradation processing mechanism and a historical record backtracking mechanism, ensures that the trackball remains available even when the target interface fails or cannot be connected, effectively improving the fault tolerance capability of the device.
[0058] A third aspect of the present invention provides a computer-readable storage medium comprising an adaptive interface control method program for a dual-mode trackball, wherein when the adaptive interface control method program for the dual-mode trackball is executed by a processor, it implements the steps of the adaptive interface control method for the dual-mode trackball as described in any of the preceding claims.
[0059] In summary, this invention provides an adaptive interface control method, system, and storage medium for a dual-mode trackball. It acquires the first flag state of the switch and the second flag state of the bus signal in real time. When the first flag state does not match the currently running protocol stack or the second flag state indicates a conflict, a protocol switching event is triggered. This leads to protocol stack reloading, execution of a handshake sequence according to a preset interface priority order to confirm the validity of the target interface, locking and updating the protocol stack, acquiring trackball displacement signals and button status signals, and encapsulating them into data frames according to the updated protocol stack for output to the host. This invention enables the trackball to adaptively switch interface protocols without power interruption during energized operation, effectively improving the trackball device's adaptability under different interface standards and operational continuity in industrial scenarios.
[0060] If the aforementioned functions are implemented as software functional modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0061] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. An adaptive interface control method for a dual-mode trackball, applied to an adaptive interface control circuit for a dual-mode trackball, the control circuit comprising a multi-level protection power supply module, an APM32 main control module, a mode switching DIP switch, a dual-interface output module, a trackball rotation encoding module, and a button diode matrix module; The APM32 main control module responds to the level change of the mode switching DIP switch and the bus level change of the dual interface output module, performs a protocol stack switching operation, and outputs the displacement signal collected by the trackball rotation encoding module and the key signal collected by the key diode matrix module after being encapsulated by the switched protocol stack. Its features are, The method includes: Obtain the first flag state of the switch and the second flag state of the bus signal; If the first flag state matches the currently running protocol stack, and the second flag state indicates that it is in an idle state, then the current protocol stack is maintained. If the first flag state does not match the currently running protocol stack, or the second flag state indicates a conflict, a protocol switching event is triggered. In response to the protocol switching event, stop the current protocol stack from sending and receiving data, clear the communication buffer, and reload the protocol stack for the target interface; The protocol stack reloading executes the handshake sequence according to the preset interface priority order. The first handshake sequence sends the device identification request according to the first interface protocol timing and waits for the first feature response. The second handshake sequence sends the device identification request according to the second interface protocol timing and waits for the second feature response. When the first feature response or the second feature response is confirmed, the protocol stack is locked and updated. Once the updated protocol stack takes effect, trackball displacement signals and button status signals are collected, encapsulated into data frames according to the updated protocol stack, and transmitted to the host.
2. The adaptive interface control method for a dual-mode trackball according to claim 1, characterized in that, The acquisition of the first flag state of the switching switch and the second flag state of the bus signal specifically includes: After power-on initialization, the level value of the switch is read, and the first flag state is generated based on the level value. The first flag state includes a first mode code or a second mode code. The data line level and clock line level of the first interface are detected in parallel. If the data line level and the clock line level are both at a high level, the second flag state is determined to be idle. If the data line level or the clock line level is in a low level state for a duration exceeding a preset conflict determination time, then the second flag state is determined to be a conflict state. When the transition edge of the data line level or the clock line level of the first interface is captured, the second flag state is updated to bus active state; Based on the first flag state and the second flag state, the matching status of the currently running protocol stack and the priority order of the target interface are determined.
3. The adaptive interface control method for a dual-mode trackball according to claim 1, characterized in that, Following the trigger protocol switching event, the following is also included: Pause the interrupt service routine of the currently running protocol stack and disable the enable signal sent by the interface corresponding to the currently running protocol stack; Force the protocol stack state machine of the currently running protocol stack to jump to the standby state and release the communication peripheral resources occupied by the currently running protocol stack; The protocol type of the target interface is parsed, and the corresponding protocol stack instance is called from the preset protocol stack library according to the protocol type; Load the protocol stack instance into the runtime area and configure the communication parameters corresponding to the target interface, wherein the communication parameters include the communication rate, data bit width, and number of stop bits; After the communication parameters are configured, the send enable signal and interrupt service routine of the target interface are restored, and the handshake sequence for reloading the protocol stack is started.
4. The adaptive interface control method for a dual-mode trackball according to claim 1, characterized in that, The protocol stack reloading executes the handshake sequence according to a preset interface priority order, specifically including: The preferred interface and alternative interfaces are determined based on the first flag status; The first handshake request is sent sequentially on the preferred interface, and the first timeout timer is started. If a standard response code corresponding to the first handshake request is received on the preferred interface before the first timeout timer expires, the first feature response is confirmed to be valid, and the protocol stack corresponding to the preferred interface is locked. If the first timeout timer expires, a second handshake request is sent on the alternative interface, and a second timeout timer is started. If a standard response code corresponding to the second handshake request is received on the alternative interface before the second timeout timer expires, the second characteristic response is confirmed to be valid, and the protocol stack corresponding to the alternative interface is locked. If the second timeout timer times out, the handshake sequence is repeated until the preset maximum number of retries is reached.
5. The adaptive interface control method for a dual-mode trackball according to claim 1, characterized in that, The process of encapsulating data frames according to the updated protocol stack specifically includes: Obtain the pulse count value and direction flag bit of the trackball X-axis encoder, and the pulse count value and direction flag bit of the Y-axis encoder; Obtain the current scan code of the button diode matrix, and generate a button status flag bit group based on the current scan code; If the updated protocol stack is of the first protocol type, then the pulse count value, the direction flag bit and the key status flag bit group are arranged according to the first data packet format to generate a first data frame, wherein the first data frame includes a device descriptor and a report descriptor; If the updated protocol stack is the second protocol type, then the pulse count value, the direction flag bit and the key status flag bit group are arranged according to the second data packet format to generate a second data frame, wherein the second data frame contains a device identifier and a status byte; The generated data frame is stored in the send buffer of the corresponding interface, triggering a send completion interrupt.
6. The adaptive interface control method for a dual-mode trackball according to claim 1, characterized in that, During the execution of the currently running protocol stack, it also includes: The current level value of the switching switch is read according to the set polling cycle, and the current level value is compared with the historical level value. If the current level value is inconsistent with the historical level value, it is determined that the first flag state has changed, and the data transmission task of the currently running protocol stack is immediately suspended. After determining that the first flag state has changed, the second flag state is forcibly set to a conflict state, triggering the protocol switching event; After the protocol stack is reloaded, the task is sent based on the updated protocol stack data. If the current voltage level is consistent with the historical voltage level, the current running protocol stack remains unchanged, and the data acquisition, encapsulation, and transmission process continues.
7. An adaptive interface control system for a dual-mode trackball, characterized in that, The system includes a memory and a processor. The memory includes an adaptive interface control method program for a dual-mode trackball. When the processor executes the adaptive interface control method program for the dual-mode trackball, it performs the following steps: Obtain the first flag state of the switch and the second flag state of the bus signal; If the first flag state matches the currently running protocol stack, and the second flag state indicates that it is in an idle state, then the current protocol stack is maintained. If the first flag state does not match the currently running protocol stack, or the second flag state indicates a conflict, a protocol switching event is triggered. In response to the protocol switching event, stop the current protocol stack from sending and receiving data, clear the communication buffer, and reload the protocol stack for the target interface; The protocol stack reloading executes the handshake sequence according to the preset interface priority order. The first handshake sequence sends the device identification request according to the first interface protocol timing and waits for the first feature response. The second handshake sequence sends the device identification request according to the second interface protocol timing and waits for the second feature response. When the first feature response or the second feature response is confirmed, the protocol stack is locked and updated. Once the updated protocol stack takes effect, trackball displacement signals and button status signals are collected, encapsulated into data frames according to the updated protocol stack, and transmitted to the host.
8. The adaptive interface control system for a dual-mode trackball according to claim 7, characterized in that, The acquisition of the first flag state of the switching switch and the second flag state of the bus signal specifically includes: After power-on initialization, the level value of the switch is read, and the first flag state is generated based on the level value. The first flag state includes a first mode code or a second mode code. The data line level and clock line level of the first interface are detected in parallel. If the data line level and the clock line level are both at a high level, the second flag state is determined to be idle. If the data line level or the clock line level is in a low level state for a duration exceeding a preset conflict determination time, then the second flag state is determined to be a conflict state. When the transition edge of the data line level or the clock line level of the first interface is captured, the second flag state is updated to bus active state; Based on the first flag state and the second flag state, the matching status of the currently running protocol stack and the priority order of the target interface are determined.
9. The adaptive interface control system for a dual-mode trackball according to claim 7, characterized in that, Following the trigger protocol switching event, the following is also included: Pause the interrupt service routine of the currently running protocol stack and disable the enable signal sent by the interface corresponding to the currently running protocol stack; Force the protocol stack state machine of the currently running protocol stack to jump to the standby state and release the communication peripheral resources occupied by the currently running protocol stack; The protocol type of the target interface is parsed, and the corresponding protocol stack instance is called from the preset protocol stack library according to the protocol type; Load the protocol stack instance into the runtime area and configure the communication parameters corresponding to the target interface, wherein the communication parameters include the communication rate, data bit width, and number of stop bits; After the communication parameters are configured, the send enable signal and interrupt service routine of the target interface are restored, and the handshake sequence for reloading the protocol stack is started.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, The computer-readable storage medium includes an adaptive interface control method program for a dual-mode trackball. When the adaptive interface control method program for a dual-mode trackball is executed by a processor, it implements the steps of the adaptive interface control method for a dual-mode trackball as described in any one of claims 1 to 6.