patlite.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import sys
3 import socket
4 import binascii
5 from contextlib import closing
6 
7 class PatliteState(object):
8  """ This is a patlite state class """
9 
10  # target
11  class Target(object):
12  LIGHT_RED = 0
13  LIGHT_YELLOW = 1
14  LIGHT_GREEN = 2
15  LIGHT_BLUE = 3
16  LIGHT_WHITE = 4
17  BUZZER = 5
18  __slots__ = []
19 
20  # light state
21  class LightState(object):
22  OFF = '\x00'
23  ON = '\x01'
24  FLASH_1 = '\x02'
25  FLASH_2 = '\x03'
26  REMAIN = '\x09'
27  __slots__ = []
28 
29  # buzzer state
30  class BuzzerState(object):
31  OFF = '\x00'
32  ON_1 = '\x01'
33  ON_2 = '\x02'
34  ON_3 = '\x03'
35  ON_4 = '\x04'
36  REMAIN = '\x09'
37  __slots__ = []
38 
39  def int2code(self, i):
40  if i == 0: return '\x00'
41  elif i == 1: return '\x01'
42  elif i == 2: return '\x02'
43  elif i == 3: return '\x03'
44  elif i == 4: return '\x04'
45  else: return '\x09'
46 
47  def code2int(self, c):
48  if c == '\x00': return 0
49  elif c == '\x01': return 1
50  elif c == '\x02': return 2
51  elif c == '\x03': return 3
52  elif c == '\x04':return 4
53  else: return 9
54 
55  def clear(self):
56  self.state = \
57  self.LightState.REMAIN + \
58  self.LightState.REMAIN + \
59  self.LightState.REMAIN + \
60  self.LightState.REMAIN + \
61  self.LightState.REMAIN + \
62  self.BuzzerState.REMAIN
63 
64  def __init__(self, state=None):
65  if state is None:
66  self.clear()
67  else:
68  self.state = state
69 
70  def red(self, value):
71  self.state = value + self.state[1:]
72  return self.state
73 
74  def yellow(self, value):
75  self.state = self.state[0] + value + self.state[2:]
76  return self.state
77 
78  def green(self, value):
79  self.state = self.state[:2] + value + self.state[3:]
80  return self.state
81 
82  def blue(self, value):
83  self.state = self.state[:3] + value + self.state[4:]
84  return self.state
85 
86  def white(self, value):
87  self.state = self.state[:4] + value + self.state[5:]
88  return self.state
89 
90  def buzzer(self, value):
91  self.state = self.state[:5] + value
92  return self.state
93 
94  def set_from_int(self, target, state):
95  if not self.is_valid(target, self.int2code(state)):
96  raise
97  self.state = self.state[:target] + self.int2code(state) + self.state[target+1:]
98 
99  def is_valid(self, target, state):
100  # TODO to be implemented
101  return True
102 
103  def __repr__(self):
104  return "<PatliteState '%s'>" % (''.join(['%x ' % ord(s) for s in self.state]))
105 
106 
107 
108 class Patlite(object):
109  """ This is a controller for a network patlite """
110 
111  # write command
112  WRITE_HEADER = '\x58\x58\x53\x00\x00\x06'
113  # write response
114  ACK = '\x06'
115  NAK = '\x15'
116 
117  # read command
118  READ = '\x58\x58\x47\x00\x00\x00'
119 
120  # clear command
121  CLEAR = '\x58\x58\x43\x00\x00\x00'
122 
123  def __init__(self, host, port=10000, timeout=1):
124  self.host = host
125  self.port = port
126  self.timeout = timeout
127  self.bufsize = 512
128  self.sock = None
130 
131  def __enter__(self):
132  self.open()
133 
134  def __exit__(self, exc_type, exc_value, traceback):
135  self.close()
136  return False
137 
138  def open(self):
139  self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
140  self.sock.settimeout(self.timeout)
141  return self.sock.connect((self.host, self.port))
142 
143  def close(self):
144  return self.sock.close()
145 
146  def write(self, state=None):
147  ret = None
148  if state is None:
149  state = self.state
150  try:
151  self.sock.sendall(self.WRITE_HEADER + state.state)
152  ret = self.sock.recv(self.bufsize)
153  except socket.timeout:
154  print "timeout"
155  if (ret != self.ACK):
156  return False
157  return True
158 
159  def read(self):
160  try:
161  self.sock.sendall(self.READ)
162  tmp_state = PatliteState(self.sock.recv(self.bufsize))
163  except socket.timeout:
164  print "timeout"
165  return None
166  return tmp_state
167 
168  def clear(self):
169  ret = None
170  try:
171  self.sock.sendall(self.CLEAR)
172  ret = self.sock.recv(self.bufsize)
173  except socket.timeout:
174  print "timeout"
175  return False
176  if (ret != self.ACK):
177  return False
178  return True
179 
180  def red(self, value):
181  self.state.clear()
182  self.state.red(value)
183  return self.write()
184 
185  def yellow(self, value):
186  self.state.clear()
187  self.state.yellow(value)
188  return self.write()
189 
190  def green(self, value):
191  self.state.clear()
192  self.state.green(value)
193  return self.write()
194 
195  def blue(self, value):
196  self.state.clear()
197  self.state.blue(value)
198  return self.write()
199 
200  def white(self, value):
201  self.state.clear()
202  self.state.white(value)
203  return self.write()
204 
205  def buzzer(self, value):
206  self.state.clear()
207  self.state.buzzer(value)
208  return self.write()
209 
210 
211 def test():
212  patlite = Patlite("10.68.0.10")
213  with patlite:
214  patlite.red(patlite.state.LightState.ON)
215  patlite.yellow(patlite.state.LightState.ON)
216  patlite.green(patlite.state.LightState.ON)
217 
218 if __name__ == '__main__':
219  test()
220 
def buzzer(self, value)
Definition: patlite.py:205
def blue(self, value)
Definition: patlite.py:82
def blue(self, value)
Definition: patlite.py:195
string READ
Definition: patlite.py:118
def open(self)
Definition: patlite.py:138
def clear(self)
Definition: patlite.py:168
def write(self, state=None)
Definition: patlite.py:146
def buzzer(self, value)
Definition: patlite.py:90
def __init__(self, state=None)
Definition: patlite.py:64
def __enter__(self)
Definition: patlite.py:131
def white(self, value)
Definition: patlite.py:86
def read(self)
Definition: patlite.py:159
def red(self, value)
Definition: patlite.py:180
def red(self, value)
Definition: patlite.py:70
def __exit__(self, exc_type, exc_value, traceback)
Definition: patlite.py:134
def is_valid(self, target, state)
Definition: patlite.py:99
def code2int(self, c)
Definition: patlite.py:47
def yellow(self, value)
Definition: patlite.py:74
string WRITE_HEADER
Definition: patlite.py:112
def green(self, value)
Definition: patlite.py:78
def set_from_int(self, target, state)
Definition: patlite.py:94
def close(self)
Definition: patlite.py:143
def green(self, value)
Definition: patlite.py:190
def test()
Definition: patlite.py:211
def yellow(self, value)
Definition: patlite.py:185
def __repr__(self)
Definition: patlite.py:103
def int2code(self, i)
Definition: patlite.py:39
def white(self, value)
Definition: patlite.py:200
def clear(self)
Definition: patlite.py:55
string ACK
Definition: patlite.py:114
def __init__(self, host, port=10000, timeout=1)
Definition: patlite.py:123
string CLEAR
Definition: patlite.py:121


rospatlite
Author(s): Takuya Nakaoka
autogenerated on Wed Jul 10 2019 03:47:15