2023年10月12日 星期四

The PS/2 Mouse Interface

  

The PS/2 Mouse Interface
This article is Copyright 2001, Adam Chapweske 

Electrical Interface / Protocol:

The PS/2 mouse uses the same protocol as the PS/2 (AT) keyboard.  This standard originally appeared in the IBM technical reference manual, but I am not aware of any current official publication of this standard.  However, you may click here for the (detailed) information I have gathered about that protocol.

Inputs, Resolution, and Scaling:

The standard PS/2 mouse supports the following inputs: X (right/left) movement, Y (up/down) movement, left button, middle button, and right button. The mouse reads these inputs at a regular freqency and updates various counters and flags to reflect movement and button states.  There are many PS/2 pointing devices that have additional inputs and may report data differently than described in this document.  One popular extension I cover later in this document is the Microsoft Intellimouse, which includes support for the standard inputs as well as a scrolling wheel and two additional buttons.

The standard mouse has two counters that keep track of movement: the X-movement counter and the Y-movement counter.  These  are 9-bit 2's complement values and each has an associated overflow flag.  Their contents, along with the state of the three mouse buttons, are sent to the host in the form of a 3-byte movement data packet (as described in the next section.)  The movement counters represent the amount of movement that has occurred since the last movment data packet was sent to the host.

When the mouse reads its inputs, it records the current state of its buttons, then checks for movement. If movement has occurred, it increments (for +X or +Y movement) or decrements (for -X or -Y movement) its X and/or Y movement counters. If either of the counters has overflowed, it sets the appropriate overflow flag.

The parameter that determines the amount by which the movement counters are incremented/decremented is the resolution. The default resolution is 4 counts/mm and the host may change that value using the "Set Resolution" (0xE8) command.

There is a parameter that does not effect the movement counters, but does effect the reported(1) value of these counters.  This parameter is scaling.  By default, the mouse uses 1:1 scaling, which has no effect on the reported mouse movement.  However, the host may select 1:2 scaling by sending the "Set Scaling 2:1" (0xE7) command.  If 2:1 scaling is enabled, the mouse will apply the following algorithm to the counters before sending their contents to the host:
 
 

Movement Counter
Reported Movement
0
0
1
1
2
1
3
3
4
6
5
9
N > 5
2 * N

Movement Data Packet:

The standard PS/2 mouse sends movement (and button) information to the host using the following 3-byte packet (4):

 

Byte 1 
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Y overflow
X overflow
Y sign bit
X sign bit
Always 1
Middle Btn
Right Btn
Left Btn
Byte 2
X Movement
Byte 3 
Y Movement
The movement counters are 9-bit 2's complement integers, where the most significant bit appears as a sign bit in Byte 1 of the movement data packet. These counters are updated when the mouse reads its input and finds movement has occurred. Their value is the amount of movement that has occurred since the last movement data packet was sent to the host (ie, after a packet is sent to the host, the movement counters are reset.) The range of values that can be expressed by the movement counters is -255 to +255. If this range is exceeded, the appropriate overflow bit is set and the counter is not incremented/decremented until it is reset.

As I mentioned earlier, the movement counters are reset whenever a movement data packet is successfully sent to the host. They are also reset after the mouse receives any command from the host other than the "Resend" (0xFE) command.

Modes of Operation:

Data reporting is handled according to the mode in which the mouse is operating.  There are four standard modes of operation: 

  • Reset - The mouse enters Reset mode at power-up or after receiving the "Reset" (0xFF) command. 
  • Stream - This is the default mode (after Reset finishes executing) and is the mode in which most software uses the mouse.  If the host has previously set the mouse to Remote mode, it may re-enter Stream mode by sending the "Set Stream Mode" (0xEA) command to the mouse. 
  • Remote - Remote mode is useful in some situations and may be entered by sending the "Set Remote Mode" (0xF0) command to the mouse. 
  • Wrap - This mode isn't particularly useful except for testing the connection between the mouse and its host.  Wrap mode may be entered by sending the "Set Wrap Mode" (0xEE) command to the mouse.  To exit Wrap mode, the host must issue the "Reset" (0xFF) command or "Reset Wrap Mode" (0xEC) command.  If the "Reset" (0xFF) command is recieved, the mouse will enter Reset mode.  If the "Reset Wrap Mode" (0xEC) command is received, the mouse will enter the mode it was in prior to Wrap Mode.
(Note: The mouse may also enter "extended" modes of operation, as described later in this document.  However, this is not a feature of the standard PS/2 mouse.)

Reset Mode:

The mouse enters reset mode at power-on or in response to the "Reset" (0xFF) command. After entring this mode, the mouse performs a diagnostic self-test referred to as BAT (Basic Assurance Test) and sets the follwing default values:

  • Sample Rate - 100 samples/sec
  • Resolution - 4 counts/mm
  • Scaling - 1:1
  • Data Reporting Disabled
It then sends a BAT completion code of either 0xAA (BAT successful) or 0xFC (Error). If the host receives a response other than 0xAA, it may cycle the mouse's power supply, causing the mouse to reset and re-execute its BAT.

Following the BAT completion code (0xAA or 0xFC), the mouse sends its device ID of 0x00. This distinguishes it from a keyboard, or a mouse in an extended mode. I have read documents saything the host is not supposed to transmit any data until it receives a device ID.  However I've found that some BIOS's will send the "Reset" (0xFF) command immediately following the 0xAA received after a power-on reset.

After the mouse has sent its device ID to the host, it will enter Stream Mode.  Note that one of the default values set by the mouse is "Data Reporting Disabled".  This means the mouse will not send any movement data packets to the host until the "Enable Data Reporting" (0xF4) command is received.

Stream Mode:

In stream mode, the mouse sends movement data when it detects movement or a change in state of one or more mouse buttons. The maximum rate at which this data reporting may occur is known as the sample rate.  This parameter ranges from 10 samples/sec to 200 samples/sec. Its default value is 100 samples/sec and the host may change that value by using the "Set Sample Rate" (0xF3) command.  Stream mode is the default mode of operation.

Remote Mode:

In this mode, the mouse reads its inputs and updates its counters/flags at the current sampling rate, but it only notifies the host of movement (and change in button state) when that information is requested by the host. The host does this by issuing the "Read Data" (0xEB) command. After receiveing this command, the mouse will send a movement data packet, and reset its movement counters.

Wrap Mode:

This is an "echoing" mode in which every byte received by the mouse is sent back to the host. Even if the byte represents a valid command, the mouse will not respond to that command--it will only echo that byte back to the host. There are two exceptions to this: the "Reset" (0xFF) command and "Reset Wrap Mode" (0xEC) command. The mouse treats these as valid commands and does not echo them back to the host.

Intellimouse Extensions:

A popular extension to the standard PS/2 mouse is the Microsoft Intellimouse.  This includes support for a total of five mouse buttons and three axises of movement (right-left, up-down, and a scrolling wheel).  These additional features require the use of a 4-byte movement data packet rather than the standard 3-byte packet.  Since standard PS/2 mouse drivers cannot recognize this packet format, the Microsoft Intellimouse is required to operate exactly like a standard PS/2 mouse unless it knows the drivers support the extended packet format.  This way, if a Microsoft Intellimouse is used on a computer which only supports the standard PS/2 mouse, the Microsoft Intellimouse will still function, except for its scrolling wheel and 4th and 5th buttons.

The Microsoft Intellimouse operates just like a standard PS/2 mouse (ie, it uses a 3-byte movement data packet, responds to all commands in the same way as a standard PS/2 mouse, and reports a device ID of 0x00.)  To enter scrolling wheel mode, the host sends the following command sequence:

Set sample rate 200
Set sample rate 100
Set sample rate 80
The host then issues the "Get device ID" command (0xF2) and waits for a response.  If a standard PS/2 mouse (ie, non-Intellimouse) is attached, it will respond with a device ID of 0x00.  In this case, the host will recognize the fact that the mouse does have a scrolling wheel and will continue to treat it as a standard PS/2 mouse.  However, if a Microsoft Intellimouse is attached, it will respond with an ID of 0x03.  This tells the host that the attached pointing device has a scrolling wheel and the host will then expect the mouse to use the following 4-byte movement data packet:
 

Byte 1 
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Y overflow
X overflow
Y sign bit
X sign bit
Always 1
Middle Btn
Right Btn
Left Btn
Byte 2
X Movement
Byte 3 
Y Movement
Byte 4
Z Movement
Z Movement is a 2's complement number that represents the scrolling wheel's movement since the last data report.  Valid values are in the range of -8 to +7. This means the number is actually represented only by the least significant four bits; the upper four bits act only as sign extension bits.
To enter scrolling wheel + 5 button mode, the host sends the following command sequence:
Set sample rate 200
Set sample rate 200
Set sample rate 80
The host then issues the "Get device ID" command (0xF2) and waits for a response.  A Microsoft Intellimouse will respond with a device ID of 0x04, then use the following 4-byte movement data packet:
 

Byte 1 
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Y overflow
X overflow
Y sign bit
X sign bit
Always 1
Middle Btn
Right Btn
Left Btn
Byte 2
X Movement
Byte 3 
Y Movement
Byte 4
Always 0
Always 0
5th Btn
4th Btn
Z3
Z2
Z1
Z0
Z0-Z3 is a 2's complement number which represents the amount of movement that has occurred since the last data report.  Valid values range from -8 to +7.
4th Btn: 1 = 4th mouse button is pressed; 0 = 4th mouse button is not pressed.
5th Btn: 1 = 5th mouse button is pressed; 0 = 5th mouse button is not pressed.
You may have seen mice with two scrolling wheels--one vertical and the other horizontal.  These mice use the Microsoft Intellimouse data packet format as described above.  If the vertical wheel is scrolled upward, the Z-counter is incremented by one and if that wheel is scrolled down, the Z-counter is decremented by one.  This is normal operation for a scrolling wheel.  However, if the horizontal wheel is scrolled right, the Z-counter is incremented by two and if it is scrolled left, the Z-counter is decremented by two.  This seems like an odd way to implement the second scrolling wheel, but it works since the placement of the two wheels make it impossible to use both of them at the same time (and if you try to trick the software and use both at the same time, it will ignore the horizontal wheel.)

Command Set:

The following are the only commands that may be sent to the mouse... If the mouse is in Stream mode, the host should disable data reporting (command 0xF5) before sending any other commands...

  • 0xFF (Reset) - The mouse responds to this command with "acknowledge" (0xFA) then enters Reset Mode.
  • 0xFE (Resend) - The host sends this command whenever it receives invalid data from the mouse. The mouse responds by resending the last(2) packet(3) it sent to the host.   If the mouse responds to the "Resend" command with another invalid packet, the host may either issue another "Resend" command, issue an "Error" command, cycle the mouse's power supply to reset the mouse, or it may inhibit communication (by bringing the Clock line low).  The action taken depends on the host.
  • 0xF6 (Set Defaults) - The mouse responds with "acknowledge" (0xFA) then loads the following values:  Sampling rate = 100, Resolution = 4 counts/mm, Scaling = 1:1, Disable Data Reporting.  The mouse then resets its movement counters and enters stream mode.
  • 0xF5 (Disable Data Reporting) - The mouse responds with "acknowledge" (0xFA) then disables data reporting and resets its movement counters.  This only effects data reporting in Stream mode and does not disable sampling.  Disabled stream mode funcions the same as remote mode.
  • 0xF4 (Enable Data Reporting) - The mouse responds with "acknowledge" (0xFA) then enables data reporting and resets its movement counters.  This command may be issued while the mouse is in Remote Mode (or Stream mode), but it will only effect data reporting in Stream mode.
  • 0xF3 (Set Sample Rate) - The mouse responds with "acknowledge" (0xFA) then reads one more byte from the host.  The mouse saves this byte as the new sample rate. After receiving the sample rate, the mouse again responds with "acknowledge" (0xFA) and resets its movement counters.  Valid sample rates are 10, 20, 40, 60, 80, 100, and 200 samples/sec.
  • 0xF2 (Get Device ID) - The mouse responds with "acknowledge" (0xFA) followed by its device ID (0x00 for the standard PS/2 mouse.)  The mouse should also reset its movement counters.
  • 0xF0 (Set Remote Mode) - The mouse responds with "acknowledge" (0xFA) then resets its movement counters and enters remote mode.
  • 0xEE (Set Wrap Mode) - The mouse responds with "acknowledge" (0xFA) then resets its movement counters and  enters wrap mode.
  • 0xEC (Reset Wrap Mode) - The mouse responds with "acknowledge" (0xFA) then resets its movement counters and enters the mode it was in prior to wrap mode (Stream Mode or Remote Mode.)
  • 0xEB (Read Data) - The mouse responds with acknowledge (0xFA) then sends a movement data packet. This is the only way to read data in Remote Mode.  After the data packets has been successfully sent, it resets its movement counters.
  • 0xEA (Set Stream Mode) - The mouse responds with "acknowledge" then resets its movement counters and enters steram mode.
  • 0xE9 (Status Request) - The mouse responds with "acknowledge" then sends the following 3-byte status packet (then resets its movement counters.): 
    •  

      Byte 1 
      Bit 7
      Bit 6
      Bit 5
      Bit 4
      Bit 3
      Bit 2
      Bit 1
      Bit 0
      Always 0
      Mode
      Enable
      Scaling
      Always 0
      Left Btn
      Middle Btn
      Right Btn
      Byte 2
      Resolution
      Byte 3 
      Sample Rate

      Right, Middle, Left Btn = 1 if button pressed; 0 if button is not pressed.
      Scaling = 1 if scaling is 2:1; 0 if scaling is 1:1. (See commands 0xE7 and 0xE6)
      Enable = 1 if data reporting is enabled; 0 if data reporting is disabled. (See commands 0xF5 and 0xF4)
      Mode = 1 if Remote Mode is enabled; 0 if Stream mode is enabled. (See commands 0xF0 and 0xEA)
       

  • 0xE8 (Set Resolution) - The mouse responds with acknowledge (0xFA) then reads one byte from the host and again responds with acknowledge (0xFA) then resets its movement counters.  The byte read from the host determines the resolution as follows: 
     
    Byte Read from Host
    Resolution
    0x00
    1 count/mm
    0x01
    2 count/mm
    0x02
    4 count/mm
    0x03
    8 count/mm
  • 0xE7 (Set Scaling 2:1) - The mouse responds with acknowledge (0xFA) then enables 2:1 scaling (discussed earlier in this document.)
  • 0xE6 (Set Scaling 1:1) - The mouse responds with acknowledge (0xFA) then enables 1:1 scaling (discussed earlier in this document.)
The only commands the standard PS/2 mouse will send to the host are the "Resend" (0xFE) and "Error" (0xFC) commands.  They both work the same as they do as host-to-device commands. 

Initialization:

The PS/2 mouse is normally detected/initialized only when the computer is booting up.  That is, the mouse is not hot-pluggable and you must restart your computer whenever you add/remove a PS/2 mouse (furthermore, some motherboards may be damaged if you add/remove a PS/2 mouse while the computer is running.)

The initial detection of the PS/2 mouse occurrs during POST.  If a mouse is detected, the BIOS will allow the operating system to configure/enable the mouse.  Otherwise, it will inhibit communication on the mouse's bus.  If you boot the computer with a mouse attached, then detach/reattach the mouse while in Windows, the OS may be able to detect the mouse was reattached.  Microsoft tried to support this, but it only works about 50% of the time.

The following is the communication between my computer (running Win98SE) and mouse when it boots up with a standard PS/2 mouse attached.  It is fairly typical of how a PS/2 mouse is initialized and if you want to emulate a PS/2 mouse it must (at minimum) be able to support the following sequence of commands... 

  • Power-on Reset:
    Mouse: AA  Self-test passed
    Mouse: 00  Mouse ID
    Host:  FF  Reset command
    Mouse: FA  Acknowledge
    Mouse: AA  Self-test passed
    Mouse: 00  Mouse ID
    Host:  FF  Reset command
    Mouse: FA  Acknowledge
    Mouse: AA  Self-test passed
    Mouse: 00  Mouse ID
    Host:  FF  Reset command
    Mouse: FA  Acknowledge
    Mouse: AA  Self-test passed
    Mouse: 00  Mouse ID
    Host:  F3  Set Sample Rate   : Attempt to Enter Microsoft
    Mouse: FA  Acknowledge       : Scrolling Mouse mode
    Host:  C8  decimal 200       :
    Mouse: FA  Acknowledge       :
    Host:  F3  Set Sample Rate   :
    Mouse: FA  Acknowledge       :
    Host:  64  decimal 100       :
    Mouse: FA  Acknowledge       :
    Host:  F3  Set Sample Rate   :
    Mouse: FA  Acknowledge       :
    Host:  50  decimal 80        :
    Mouse: FA  Acknowledge       :
    Host:  F2  Read Device Type  :
    Mouse: FA  Acknowledge       :
    Mouse: 00  Mouse ID          : Response 03 if microsoft scrolling mouse
    Host:  F3  Set Sample Rate 
    Mouse: FA  Acknowledge
    Host:  0A  decimal 10
    Mouse: FA  Acknowledge
    Host:  F2  Read Device Type
    Mouse: FA  Acknowledge
    Mouse: 00  Mouse ID
    Host:  E8  Set resolution
    Mouse: FA  Acknowledge
    Host:  03  8 Counts/mm
    Mouse: FA  Acknowledge
    Host:  E6  Set Scaling 1:1
    Mouse: FA  Acknowledge
    Host:  F3  Set Sample Rate
    Mouse: FA  Acknowledge
    Host:  28  decimal 40
    Mouse: FA  Acknowledge
    Host:  F4  Enable
    Mouse: FA  Acknowledge
    Initialization complete...

    If I then press the Left Button...
    Mouse: 09 1 1 00001001; bit0 = Left button state; bit3 = always 1
    Mouse: 00 1 1 No X-movement
    Mouse: 00 1 1 No Y-movement
    ... and release the Left Button:
    Mouse: 08 0 1 00001000 bit0 = Left button state; bit3 = always 1
    Mouse: 00 1 1 No X-movement
    Mouse: 00 1 1 No Y-movement

The following is the communication between my computer (running Win98SE) and mouse when it boots up with an (emulated) Intellimouse... 
  • Power-on Reset:
    Mouse: AA  Self-test passed
    Mouse: 00  Mouse ID
    Host:  FF  Reset command
    Mouse: FA  Acknowledge
    Mouse: AA  Self-test passed
    Mouse: 00  Mouse ID
    Host:  FF  Reset command
    Mouse: FA  Acknowledge
    Mouse: AA  Self-test passed
    Mouse: 00  Mouse ID
    Host:  FF  Reset command
    Mouse: FA  Acknowledge
    Mouse: AA  Self-test passed
    Mouse: 00  Mouse ID
    Host:  F3  Set Sample Rate   : Attempt to Enter Microsoft 
    Mouse: FA  Acknowledge       : Scrolling Mouse mode
    Host:  C8  decimal 200       :
    Mouse: FA  Acknowledge       :
    Host:  F3  Set Sample Rate   :
    Mouse: FA  Acknowledge       :
    Host:  64  decimal 100       :
    Mouse: FA  Acknowledge       :
    Host:  F3  Set Sample Rate   :
    Mouse: FA  Acknowledge       :
    Host:  50  decimal 80        :
    Mouse: FA  Acknowledge       :
    Host:  F2  Read Device Type  :
    Mouse: FA  Acknowledge       :
    Mouse: 03  Mouse ID          : Response 03 if microsoft scrolling mouse
    Host:  E8  Set Resolution 
    Mouse: FA  Acknowledge 
    Host:  03  8 counts/mm
    Mouse: FA  Acknowledge
    Host:  E6  Set scaling 1:1
    Dev:   FA  Acknowledge
    Host:  F3  Set Sample Rate 
    Mouse: FA  Acknowledge
    Host:  28  decimal 40
    Mouse: FA  Acknowledge
    Host:  F4  Enable device 
    Mouse: FA  Acknowledge

    If I then press the left mouse button:
    Mouse: 09  00001001 bit0 = Left button state; bit3 = always 1
    Mouse: 00  No X-movement
    Mouse: 00  No Y-movement
    Mouse: 00  No Z-movement

    ...and then release the left mouse button button:
    Mouse: 08  00001000 bit0 = Left button state; bit3 = always 1
    Mouse: 00  No X-movement
    Mouse: 00  No Y-movement
    Mouse: 00  No Z-movement

After I downloaded/installed the Microsoft's Intellimouse drivers with support for the 4th and 5th buttons, the following sequence was found:
... (starts same as before) ...
Host:  F3  Set Sample Rate   : Attempt to Enter Microsoft 
Mouse: FA  Acknowledge       : Scrolling Mouse mode.
Host:  C8  decimal 200       :
Mouse: FA  Acknowledge       :
Host:  F3  Set Sample Rate   :
Mouse: FA  Acknowledge       :
Host:  64  decimal 100       :
Mouse: FA  Acknowledge       :
Host:  F3  Set Sample Rate   :
Mouse: FA  Acknowledge       :
Host:  50  decimal 80        :
Mouse: FA  Acknowledge       :
Host:  F2  Read Device Type  :
Mouse: FA  Acknowledge       :
Mouse: 03  Mouse ID          : Response 03 if microsoft scrolling mouse.
Host:  F3  Set Sample Rate   : Attempt to Enter Microsoft 5-button 
Mouse: FA  Acknowledge       : Scrolling Mouse mode.
Host:  C8  decimal 200       :
Mouse: FA  Acknowledge       :
Host:  F3  Set Sample Rate   :
Mouse: FA  Acknowledge       :
Host:  C8  decimal 200       :
Mouse: FA  Acknowledge       :
Host:  F3  Set Sample Rate   :
Mouse: FA  Acknowledge       :
Host:  50  decimal 80        :
Mouse: FA  Acknowledge       :
Host:  F2  Read Device Type  :
Mouse: FA  Acknowledge       :
Mouse: 04  Mouse ID         : Response 04 if 5-button scrolling mouse.
... rest of initialization same as before ...
Emulation/Interfacing:
  • Click here for routines that emulate a PS/2 mouse or keyboard
  • Click here for routines that emulate a PS/2 host (ie, interface a mouse/keyboard)
  • Click here for a fully-functional PS/2 mouse written for the PIC16F84.
If you want to build a truely fully-implemented mouse or host, you should implement all of the features described in this document (except for, of course, the Microsoft Intellimouse extensions, which are optional).  However, at an absolute minimum, your device should operate as follows:
To Emulate a Mouse:
  • Never send data when  the "Clock" line low.  If the host pulls the "Data" line low, prepare to read a byte from the host.
  • ~500 milliseconds after powerup, transmit "0xAA, 0x00".
  • Wait for the host to send the enable (0xF4) command before sending any movement/button data.
  • Emulate the various mouse functions as follows:
    Emulated Action
    Data sent to host
    Move up one
    0x08,0x00,0x01
    Move down one
    0x28,0x00,0xFF
    Move right one
    0x08,0x01,0x00
    Move left one
    0x18,0xFF,0x00
    Press left button
    0x09,0x00,0x00
    Release left button
    0x08,0x00,0x00
    Press middle button
    0x0C,0x00,0x00
    Release middle button
    0x08,0x00,0x00
    Press right button
    0x0A,0x00,0x00
    Release right button
    0x08,0x00,0x00
  • Respond to the "Reset" (0xFF) command with "0xFA" then goto the beginning of your program. (ie, send 0xAA, 0x00, then wait for the enable command before sending any movement/button data.)
  • Respond to the "Get Device ID" (0xF2) command with "0xFA, 0x00".
  • Respond to the "Status Request" (0xE9) command with "0xFA, 0x00, 0x02, 0x64".
  • Respond to all other commands with acknowledge (0xFA).
To Interface a Mouse:
  • Wait for the mouse to send "0xAA", then send the "Enable" (0xF4) command.
  • The mouse will then send a 3-byte movement packets as described earlier in this document.


Footnotes:

1) 2:1 scaling only applies to the automatic data reporting in Stream mode. It does not effect the reported data sent in response to the "Read Data" (0xEB) command.

2)  The mouse and host do not buffer "Resend" (0xFF) commands. This means "0xFE" will never be sent in response to the "Resend" command. 

3)   A "packet" may be a 3-byte movement data packet, a 4-byte movement data packet (for the Intellimouse), a 3-byte status packet (see "Status Request" [0xE9] command) a 2-byte completion-code-ID packet (0xAA,0x00 or 0xFC,0x00), or a 1-byte response to a command.

4)  A little advice from my own experience...  Even though bit 3 of the first byte in a movement data packet is supposed to be set, some drivers (such as the standard PS/2 mouse driver included with Windows 98SE) don't care and just ignore that bit.  However, other drivers do check that bit and if it is not set, it is considered an error.  I mention this so that, if you're designing a mouse, you double-check that this bit is set in every movement data packet sent by your mouse.  If it is not, your mouse may work properly when you test it on your computer, but it may not work on other computers that use different mouse drivers. 

For example, if using MS Intellimouse drivers and bit 3 of the first byte in a movement data packet is not set, the driver will discard that packet, then send the "Disable Data Reporting"  (0xF5) command, followed by the "Set Defaults" (0xF6) command, then it will reinitialize the mouse using the same command sequence as it does when Windows boots up (see the "Initialization" section above.)

Other Sources / References:


    2023年10月11日 星期三

    3D 列印筆記

     

    火山口加熱塊

    有不同的稱呼,火山加熱頭/加熱塊。特點:特別的結構設計,比一般加熱塊增加約2倍的加熱行程,使線材能熔融更完全,增加層與層之間的貼合度。可直接安裝在創想喉管上。

    加熱棒和熱敏電阻為通用,只有火山頭噴嘴較長。還有專用的火山加熱頭矽膠保護套,用以阻隔外界冷風的影響,維持加熱塊溫度。

    起子充磁器 DIY

    參考 起子充磁器的原理

    結構看起來很簡單,只是兩塊磁鐵相對組立而已。主要是確定物理原理,如兩個磁鐵之間極性關係。


    2023年9月22日 星期五

    STM32 -- 使用 vial 及 vibl

    參考網址 


    必須使用特製的 st-flash,stlink-stm32f10-opt-bytes,提供 "program option bytes" 的功能。其在燒錄 vibl 後,可將對

    From the root of vial-qmk, run python3 util/vial_generate_keyboard_uid.py in order to generate a unique Vial keyboard ID:

    -----------------
    ajax@stm-dev /work/vial-qmk $ python3 util/vial_generate_keyboard_uid.py 
    #define VIAL_KEYBOARD_UID {0xA3, 0xA1, 0xFF, 0x29, 0xE9, 0x28, 0xCC, 0xCC}
    -----------------

    其

    -----------------
    ajax@stm-dev /work $ st-flash opterase
    st-flash 1.5.1
    2023-09-23T08:19:06 INFO usb.c: -- exit_dfu_mode
    2023-09-23T08:19:07 INFO common.c: Loading device parameters....
    2023-09-23T08:19:07 INFO common.c: Device connected is: F1 Medium-density device, id 0x20036410
    2023-09-23T08:19:07 INFO common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x20000 bytes (128 KiB) in pages of 1024 bytes
    2023-09-23T08:19:07 WARN common.c: Unlocking option flash

    ajax@stm-dev /work/vibl/bootloader/build $ st-flash write ../../misc/opt-head.bin 0x1FFFF800
    st-flash 1.5.1
    2023-09-23T08:21:33 INFO common.c: Loading device parameters....
    2023-09-23T08:21:33 INFO common.c: Device connected is: F1 Medium-density device, id 0x20036410
    2023-09-23T08:21:33 INFO common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0 bytes (0 KiB) in pages of 1024 bytes
    2023-09-23T08:21:33 WARN common.c: Unlocking option flash

    ajax@stm-dev /work/vibl/bootloader/build $ st-flash write bootloader-magic_tpk_68.bin  0x8000000
    ------------------

    使用 STM32 ST-LINK Utility 比較穩定可靠。另外,必須將開發板的 Boot0 切換到 1 (3.3V),ST-LINK Utility 才能連上。Boot0 不接時,會下拉成 0。但是使用 st-flash 則不用管 Boot0 的位置。

    使用 ST-LINK Utility 寫入的順序如下。

    • opt-default.bin (size: 0x0010) => 0x1FFFF800
    • opt-head.bin (size: 0x0002)  => 0x1FFFF800
    • bootloader-magic_tpk_68.bin => 0x8000000
    • opt-tail.bin => 0x1FFFF802

    在寫入保護之後,假若執行 "Erase Chip",會出現 "Some flash pages are write protected!" 的錯誤。

    再次從 0x8000000 寫入資料會顯示如下的資料。

    09:28:20 : [bootloader-magic_tpk_68.bin] opened successfully.
    09:28:20 : [bootloader-magic_tpk_68.bin] checksum : 0x00043E9E 
    09:28:29 : Some flash sectors are write protected.
               The write protection will be disabled then restored after flash programming
               in case there is no option bytes segment within the file.
    09:28:29 : Memory programmed in 0s and 343ms.
    09:28:29 : Verification...OK

    但反覆測試,不太確定正確的該如何做。假如不成功,就試另一種方法吧。


    2023年9月18日 星期一

    使用 SQL Server 的 BCP, 將 Visual FoxPro 的大量資料匯入到 SQL Server

    在微軟尚發行 32位元的 Windows 10 時,可以使用 SQL Server 2008 的匯入功能,配合微軟提供的 VFP odbc driver,將VFP的資料匯入到 SQL Server。但後來微軟只發行 64位元的 Windows OS 後, VFP odbc driver 就無法使用了,必須用其他方法匯入資料。

    自己寫程式一筆一筆 insert,是最直接明瞭的方法,但效率非常非常慢。最快的方式是使用微軟提供的 BCP (Bulk Copy Program),經測試,40多萬筆的資料,花費的時間在 10秒以內。

    參見 


    下面的文章是 VFP 愛用者社區討論中引用的微軟官方文件,但原始文件已下架。可能是使用翻譯軟體轉成中文,有一些不通順的地方,稍微做一些修改。

    -------------

    這是微軟提供的,看樣子要研究研究一下 bcp,因為還不太了解怎麼用 bcp
    http://support.microsoft.com/kb/153199/zh-tw

    如何使用 SQL Server 大量複製程式, 從 Visual FoxPro 大量的資料移動到 SQL Server

    文章編號 : 153199
    上次校閱 : 2007年2月12日
    版次 : 4.1

    結論

    本文告訴您如何使用大量複製程式 (BCP),Microsoft SQL Server 隨附的獨立公用程式。 如果您必須同時移動大量的資料至 SQL Server, 使用 Microsoft Visual FoxPro 「 SQL 轉換精靈 」 可能會太慢。 Bulk Copy Program 公用程式只能傳送資料, 它不會建立資料表。 您可以使用 「 SQL 轉換精靈 」 來建立資料表,或您自己用 ISQL / W 建立。 為了要讓 SQL 伺服器接受 (Accept) 使用 「 快速 」 版本的 BCP 資料,您必須執行 sp_dboption 系統預存程序將 select into / bulkcopy 選項設為 True


    逐步說明範例

    以下的範例步驟,是使用 BCP 公用程式將資料從一個 ASCII 檔案傳送至 SQL Server:

    1. 從 SQL Server 執行:
    sp_dboption Mydatabase,bulkcopy,true

    2. 從 Visual FoxPro 資料表建立使用定位點 (TAB) 分隔的 ASCII 檔案。 舉例來說,請使用低階的常式,以輸出到文字檔的欄位:
    =FPUTS(fhandle, emp_id + CHR(9) + ;
      last_name + CHR(9) + ;
      first_name + CHR(9) + ;
      title + CHR(9) + ;
      PADR(DTOC(birth_date)+' '+'12:00AM',16,' ') + CHR(9) + ;
      PADR(DTOC(hire_date)+' '+ '12:00AM',16,' ') + CHR(9) + ;
      address + CHR(9) + ;
      city + CHR(9) + ;
      region + CHR(9) + ;
      postalcode + CHR(9) + ;
      STR(salary,12,2) )


    3. 建立 bcp 格式檔案才能傳送資料。 例如, 從 SQL Server 下 bin 目錄:
    bcp mydatabase.dbo.employee out c:\temp\emp.txt /Sservename /Usa /Ppassword
    注意: 這個命令應該是在同一行。

    4. 請遵循提示, 和型別 \t 如為每個欄位欄位終端子。

    5. 出現提示時, 使用有意義的名稱, 例如 employee.fmt 儲存格式檔案。

    6. 使用 MS - DOS 編輯器來編輯  .fmt 檔。

    7. 在下列範例使用 employee.fmt 為變更為每個資料列第二欄為 SYBCHAR:
    6.0
    11
    1 SYBCHAR 0 6 "\t" 1 emp_id
    2 SYBCHAR 0 20 "\t" 2 last_name
    3 SYBCHAR 0 10 "\t" 3 first_name
    4 SYBCHAR 0 30 "\t" 4 title
    5 SYBCHAR 0 16 "\t" 5 birth_date
    6 SYBCHAR 0 16 "\t" 6 hire_date
    7 SYBCHAR 0 60 "\t" 7 address
    8 SYBCHAR 0 15 "\t" 8 city
    9 SYBCHAR 0 15 "\t" 9 region
    10 SYBCHAR 0 10 "\t" 10 postalcode
    11 SYBCHAR 0 8 "\r\n" 11 salary

    8. 檔案結尾 (EOF) 標記為變更第五資料行中的最後一列為 "\r\n"。

    9. 將 ASCII 檔案傳輸至 SQL Server 使用 BCP 如下所示:
    bcp mydatabase.dbo.employee in c:\temp\employ.txt /f employee.fmt /S servernamec /Usa /P"pwd"

    10. 回為使用步驟 1 以 FALSE 參數 FALSE, 完成時設定 Bulkcopy 選項。

    另請參閱

    如需詳細資訊, 請參閱 SQL Server Administrator ' s 系統 Guide。

    --------------------------------------------------------------------------------
    這篇文章中的資訊適用於:
    • Microsoft Visual FoxPro 3.0 Standard Edition
    • Microsoft Visual FoxPro 3.0b Standard Edition
    • Microsoft Visual FoxPro 6.0 Professional Edition
    • Microsoft Visual FoxPro 7.0 Professional Edition
    • Microsoft Visual FoxPro 8.0 Professional Edition
    • Microsoft Visual FoxPro 9.0 Professional Edition

    關鍵字: kb3rdparty kbhowto kbinterop KB153199 KbMtzh kbmt

    --------------------------------------------------------------------------------

    需下載並安裝 sqlcmd,才能執行 bcp

    --------
    C:\temp>bcp [rtcourse-tst].[caucgi].[dept2] out "c:\temp\tst.txt" -c -t  -S localhost -U agee
    密碼:
    開始複製...
    已複製 357 個資料列。
    網路封包大小 (位元組): 4096
    時間 (毫秒) 總計     : 1      平均 : (每秒 357000.00 資料列)
    C:\temp>bcp [rtcourse-tst].[caucgi].[dept2] format nul -c -f "c:\temp\tst.fmt" -c -t  -S localhost -U agee
    警告: -c 覆寫 -c。
    密碼:
    --------------


    2023年9月10日 星期日

    klipper - 多色列印

    參考 Prusaslicer 的 Color change。可以在要換顏色的層手動插入 G-code M600。 對應 M600 macro 要做的事,暫停列印,退出 filament,發出 "嗶" 聲。等換好線後,載入 filament,然後按恢復列印。

    Klipper 的 macro 可以參考 klipper/config/sample-macros.cfg,將對應 M600 的段落擷取如下

    ------------------------------------------------
    ######################################################################
    # Filament Change
    ######################################################################

    # M600: Filament Change. This macro will pause the printer, move the
    # tool to the change position, and retract the filament 50mm. Adjust
    # the retraction settings for your own extruder. After filament has
    # been changed, the print can be resumed from its previous position
    # with the "RESUME" gcode.

    [pause_resume]

    [gcode_macro M600]
    gcode:
        {% set X = params.X|default(50)|float %}
        {% set Y = params.Y|default(0)|float %}
        {% set Z = params.Z|default(10)|float %}
        SAVE_GCODE_STATE NAME=M600_state
        PAUSE
        G91
        G1 E-.8 F2700
        G1 Z{Z}
        G90
        G1 X{X} Y{Y} F3000
        G91
        G1 E-50 F1000
        RESTORE_GCODE_STATE NAME=M600_state
    ----------------
    --------------------------------

    G90 - Absolute Mode;G91 - Relative Mode

    G0, G1 - Linear Move

    G1 [E<pos>] [F<rate>] [X<pos>] [Y<pos>] [Z<pos>]






    2023年8月11日 星期五

    Arch Linux - 透過 Flatpak 使用 Application

     ArchLinux

    -------
    正在解決依賴關係…
    :: 有 7 個提供者可供 xdg-desktop-portal-impl:
    :: 軟體庫 extra
       1) xdg-desktop-portal-gnome  2) xdg-desktop-portal-gtk
       3) xdg-desktop-portal-hyprland  4) xdg-desktop-portal-kde  5) xdg-desktop-portal-lxqt
       6) xdg-desktop-portal-wlr  7) xdg-desktop-portal-xapp
    --------

    安裝 com.prusa3d.PrusaSlicer 後,執行時會出錯。

    ----------------
    $ flatpak run com.prusa3d.PrusaSlicer
    sh: warning: setlocale: LC_ALL: cannot change locale (zh_TW.UTF-8)
    --------------------------------------------------------------------------
    Message: 23:26:30: Starting PrusaSlicer flatpak with entrypoint script
    --------------------------------------------------------------------------
    /app/bin/entrypoint: line 12: warning: setlocale: LC_ALL: cannot change locale (zh_TW.UTF-8)
    Message: 23:26:30: WARN: Executing prusa-slicer with locale workaround zh_TW.UTF-8
    [ajax@aj-arch-pc ~]$ [2023-08-11 23:26:30.470612] [0x00007f30355fed40] [trace]   Initializing StaticPrintConfigs
    An error occured while setting up locale.
    You may need to reconfigure the missing locales, likely by running the "locale-gen" and "dpkg-reconfigure locales" commands.
    PrusaSlicer will now terminate.

    locale::facet::_S_create_c_locale name not valid
    ---------------

    使用 sh 進去看

    ------------------
    $ flatpak run  --command=sh com.prusa3d.PrusaSlicer
    sh: warning: setlocale: LC_ALL: cannot change locale (zh_TW.UTF-8)
    [com.prusa3d.PrusaSlicer ~]$ 
    ....
    [com.prusa3d.PrusaSlicer ~]$ ls -l /usr/lib/locale/zh_TW.utf8
    lrwxrwxrwx 1 nfsnobody nfsnobody 44 Aug 11 21:26 /usr/lib/locale/zh_TW.utf8 -> ../../share/runtime/locale/zh/lib/zh_TW.utf8
    [com.prusa3d.PrusaSlicer ~]$ ls /usr/lib/locale/../../share/runtime/locale/
    (此目錄 /usr/share/runtime/locale/ 是空的)
    ------------------


    網誌存檔