|
| 1 | +## |
| 2 | +## This file is part of the libsigrokdecode project. |
| 3 | +## |
| 4 | +## Copyright (C) 2023 Marshal Horn <kamocat@gmail.com> |
| 5 | +## |
| 6 | +## This program is free software; you can redistribute it and/or modify |
| 7 | +## it under the terms of the GNU General Public License as published by |
| 8 | +## the Free Software Foundation; either version 2 of the License, or |
| 9 | +## (at your option) any later version. |
| 10 | +## |
| 11 | +## This program is distributed in the hope that it will be useful, |
| 12 | +## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +## GNU General Public License for more details. |
| 15 | +## |
| 16 | +## You should have received a copy of the GNU General Public License |
| 17 | +## along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 | +## |
| 19 | + |
| 20 | +import sigrokdecode as srd |
| 21 | + |
| 22 | +class Ps2Packet: |
| 23 | + def __init__(self, val, host=False, pok=False, ack=False): |
| 24 | + self.val = val #byte value |
| 25 | + self.host = host #Host transmissions |
| 26 | + self.pok = pok #Parity ok |
| 27 | + self.ack = ack #Acknowlege ok for host transmission. |
| 28 | + |
| 29 | +class Decoder(srd.Decoder): |
| 30 | + api_version = 3 |
| 31 | + id = 'mouse' |
| 32 | + name = 'PS/2 Mouse' |
| 33 | + longname = 'PS/2 Mouse' |
| 34 | + desc = 'PS/2 mouse interface.' |
| 35 | + license = 'gplv2+' |
| 36 | + inputs = ['ps2_packet'] |
| 37 | + outputs = [] |
| 38 | + tags = ['PC'] |
| 39 | + binary = ( |
| 40 | + ('bytes', 'Bytes without explanation'), |
| 41 | + ('movement', 'Explanation of mouse movement and clicks'), |
| 42 | + ) |
| 43 | + annotations = ( |
| 44 | + ('Movement', 'Mouse movement packets'), |
| 45 | + ) |
| 46 | + annotation_rows = ( |
| 47 | + ('mov', 'Mouse Movement',(0,)), |
| 48 | + ) |
| 49 | + |
| 50 | + def __init__(self): |
| 51 | + self.reset() |
| 52 | + |
| 53 | + def reset(self): |
| 54 | + self.packets = [] |
| 55 | + self.es = 0 |
| 56 | + self.ss = 0 |
| 57 | + |
| 58 | + def start(self): |
| 59 | + self.out_binary = self.register(srd.OUTPUT_BINARY) |
| 60 | + self.out_ann = self.register(srd.OUTPUT_ANN) |
| 61 | + |
| 62 | + def metadata(self,key,value): |
| 63 | + if key == srd.SRD_CONF_SAMPLERATE: |
| 64 | + self.samplerate = value |
| 65 | + |
| 66 | + def mouse_movement(self): |
| 67 | + if len(self.packets) >= 3: |
| 68 | + if not self.packets[0].host: |
| 69 | + msg = '' |
| 70 | + [flags,x,y] = [p.val for p in self.packets[:3]] |
| 71 | + if flags & 1: |
| 72 | + msg += 'L' |
| 73 | + if flags & 2: |
| 74 | + msg += 'M' |
| 75 | + if flags & 4: |
| 76 | + msg += 'R' |
| 77 | + if flags & 0x10: |
| 78 | + x = x-256 |
| 79 | + if flags & 0x20: |
| 80 | + y = y-256 |
| 81 | + if x != 0: |
| 82 | + msg += ' X%+d' % x |
| 83 | + if flags & 0x40: |
| 84 | + msg += '!!' |
| 85 | + if y != 0: |
| 86 | + msg += ' Y%+d' % y |
| 87 | + if flags & 0x80: |
| 88 | + msg += '!!' |
| 89 | + if msg == '': |
| 90 | + msg = 'No Movement' |
| 91 | + ustring = ('\n' + msg).encode('UTF-8') |
| 92 | + self.put(self.ss,self.es,self.out_binary, [1,ustring] ) |
| 93 | + self.put(self.ss,self.es,self.out_ann, [0,[msg]] ) |
| 94 | + |
| 95 | + |
| 96 | + def print_packets(self): |
| 97 | + self.mouse_movement() |
| 98 | + tag = "Host: " if self.packets[-1].host else "Mouse:" |
| 99 | + octets = ' '.join(["%02X" % x.val for x in self.packets]) |
| 100 | + unicode_string = ("\n"+tag+" "+octets).encode('UTF-8') |
| 101 | + self.put(self.ss,self.es,self.out_binary, [0,unicode_string] ) |
| 102 | + self.reset() |
| 103 | + |
| 104 | + def mouse_ack(self,ss,es): |
| 105 | + self.put(ss,es,self.out_binary, [0,b' ACK'] ) |
| 106 | + |
| 107 | + def decode(self,startsample,endsample,data): |
| 108 | + if len(self.packets) == 0: |
| 109 | + self.ss = startsample |
| 110 | + elif data.host != self.packets[-1].host: |
| 111 | + self.print_packets() |
| 112 | + self.ss = startsample #Packets were cleared, need to set startsample again |
| 113 | + if data.val == 0xFA and not data.host: |
| 114 | + #Special case: acknowledge byte from mouse |
| 115 | + self.mouse_ack(startsample,endsample) |
| 116 | + self.reset() |
| 117 | + return |
| 118 | + |
| 119 | + self.packets.append(data) |
| 120 | + self.es = endsample |
| 121 | + #Mouse streaming packets are in 3s |
| 122 | + #Timing is not guaranteed because host can hold the clock at any point |
| 123 | + if len(self.packets)>2: |
| 124 | + self.print_packets() |
0 commit comments