forked from TheAxeMan301/PptIrcBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpptcontrol.py
165 lines (132 loc) · 4.42 KB
/
pptcontrol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import sys
import os
import time
import re
from threading import Thread
from Queue import Queue
def debug(msg):
print msg
pass
NopChar = 0x0000
#************************
#* Character mappings *
#************************
# Returns a MSB representation of the SNES button map
def makeCharMap():
mapping = {}
mapping.update({
'b': 0,
'y': 1,
'start': 2,
'select': 3,
'u': 4,
'd': 5,
'l': 6,
'r': 7,
'a': 8,
'x': 9,
'l_shoulder': 10,
'r_shoulder': 11
})
return mapping
CharMap = makeCharMap()
def encodeChar(chatChar=None):
# Encode a char as a literal button press
charId = CharMap.get(chatChar)
if charId:
#return 0xFFFF - (1 << 15 - charId)
return 1 << 15 - charId
else:
return NopChar
class TextPipeHandler(Thread):
"""Reads the input from the replay pipe and adds to the line queues.
Decides when to drop chat if it gets too backed up.
"""
def __init__(self, chatQueue, pipeName):
super(TextPipeHandler, self).__init__()
self.chatQueue = chatQueue
if not os.path.exists(pipeName):
os.mkfifo(pipeName)
self.readPipe = open(pipeName, 'r')
def readNextLine(self):
"""Read the next line, halting everything until something comes"""
while True:
line = self.readPipe.readline()
#If we read from flushed pipe then wait a moment before moving on
if line == '':
time.sleep(0.1)
continue
return line.rstrip('\n')
def run(self):
"""Listen on the pipe. On reading something add it to the appropriate queue"""
while True:
line = self.readNextLine()
debug('chat line: ' + line)
self.chatQueue.put(line)
class BitStreamer(object):
"""Manages the stream of commands to send"""
def __init__(self, pipeName=None):
self.chatQueue = Queue()
#If we got a pipe name then start the pipe handler thread
#It will add to the queues as it reads text from chat
if pipeName is not None:
pipeThread = TextPipeHandler(self.chatQueue, pipeName)
pipeThread.start()
#Translate next line into a list of chars or emotes to send
self.chatChars = []
def readChatQueue(self):
"""Grab a line of chat text"""
if self.chatQueue.empty():
return
line = self.chatQueue.get()
self.chatChars = line
debug("Parsed chat line: " + str(self.chatChars))
def getBitsToSend(self):
"""Check our char queues and get the bits to send"""
#include a chat char
if(self.chatChars):
encoded = encodeChar(self.chatChars[0])
debug("Button pressed: %s, %s" % (self.chatChars[0], decodeBits(encoded)))
self.chatChars = ''
return encoded & 0xFFFF
else:
return NopChar
def getNextBits(self):
"""Send the next set of bits based on incoming text.
Red's chat gets priority. We can send a chat char with his.
"""
#If we have no text from chat or red see if there is more
#available in the Queue
if len(self.chatChars) == 0:
self.readChatQueue()
#This is the stream that goes to replay
#Swap the byte order to match what the hardware expects
return swapBytes(self.getBitsToSend())
def swapBytes(num):
return ((num << 8) & 0xFF00) | ((num >> 8) & 0x00FF)
def decodeBits(bits):
"""Debugging decode of 16 bits. Convert to binary string e.g. 00111011011010101010"""
return format(bits, '#018b')[2:]
class BitStreamerTestThread(Thread):
"""Tests the BitStreamer by printing out its output"""
def __init__(self, bs):
super(BitStreamerTestThread, self).__init__()
self.bs = bs
def run(self):
#For testing we grab an stream input every 1/10 of a second
for i in xrange(1000):
time.sleep(0.1)
# print decodeBits(self.bs.getNextBits())
def main():
"""For testing, display control output"""
if '--test' in sys.argv:
import doctest
res = doctest.testmod()
# print res
sys.exit(1 if res.failed else 0)
bs = BitStreamer('pipe_test')
thread = BitStreamerTestThread(bs)
thread.daemon = True
thread.start()
if __name__ == "__main__":
main()