@@ -13,13 +13,23 @@ def __init__(self, baud, port, timeout=None):
1313 self .timeout = timeout
1414 self .output = []
1515 self .input = []
16+ self .is_open = True
1617
1718 def flush (self ):
1819 pass
1920
2021 def write (self , line ):
2122 self .output .append (line )
2223
24+ def isOpen (self ):
25+ return self .is_open
26+
27+ def close (self ):
28+ if self .is_open :
29+ self .is_open = False
30+ else :
31+ raise ValueError ('Mock serial port is already closed.' )
32+
2333 def readline (self ):
2434 """
2535 @TODO: This does not take timeout into account at all.
@@ -44,7 +54,15 @@ def push_line(self, line, term='\r\n'):
4454LSBFIRST = "LSBFIRST"
4555
4656
47- class TestArduino (unittest .TestCase ):
57+ class ArduinoTestCase (unittest .TestCase ):
58+
59+ def setUp (self ):
60+ from Arduino .arduino import Arduino
61+ self .mock_serial = MockSerial (9600 , '/dev/ttyACM0' )
62+ self .board = Arduino (sr = self .mock_serial )
63+
64+
65+ class TestArduino (ArduinoTestCase ):
4866
4967 def parse_cmd_sr (self , cmd_str ):
5068 assert cmd_str [0 ] == '@'
@@ -58,10 +76,10 @@ def parse_cmd_sr(self, cmd_str):
5876 args = args_str .split ('%' )
5977 return cmd , args
6078
61- def setUp (self ):
62- from Arduino . arduino import Arduino
63- self . mock_serial = MockSerial ( 9600 , '/dev/ttyACM0' )
64- self .board = Arduino ( sr = self . mock_serial )
79+ def test_close (self ):
80+ self . board . close ()
81+ # Call again, should skip calling close.
82+ self .board . close ( )
6583
6684 def test_version (self ):
6785 from Arduino .arduino import build_cmd_str
@@ -172,5 +190,71 @@ def test_analogWrite(self):
172190 build_cmd_str ('aw' , (pin , value )))
173191
174192
193+ class TestServos (ArduinoTestCase ):
194+
195+ def test_attach (self ):
196+ from Arduino .arduino import build_cmd_str
197+ pin = 10
198+ position = 0
199+ self .mock_serial .push_line (position )
200+ servo_min = 544
201+ servo_max = 2400
202+ self .board .Servos .attach (pin , min = servo_min , max = servo_max )
203+ self .assertEquals (self .mock_serial .output [0 ],
204+ build_cmd_str ('sva' , (pin , servo_min , servo_max )))
205+
206+ def test_detach (self ):
207+ from Arduino .arduino import build_cmd_str
208+ pin = 10
209+ position = 0
210+ # Attach first.
211+ self .mock_serial .push_line (position )
212+ self .board .Servos .attach (pin )
213+ self .mock_serial .reset_mock ()
214+ self .board .Servos .detach (pin )
215+ self .assertEquals (self .mock_serial .output [0 ],
216+ build_cmd_str ('svd' , (position ,)))
217+
218+ def test_write (self ):
219+ from Arduino .arduino import build_cmd_str
220+ pin = 10
221+ position = 0
222+ angle = 90
223+ # Attach first.
224+ self .mock_serial .push_line (position )
225+ self .board .Servos .attach (pin )
226+ self .mock_serial .reset_mock ()
227+ self .board .Servos .write (pin , angle )
228+ self .assertEquals (self .mock_serial .output [0 ],
229+ build_cmd_str ("svw" , (position , angle )))
230+
231+ def test_writeMicroseconds (self ):
232+ from Arduino .arduino import build_cmd_str
233+ pin = 10
234+ position = 0
235+ microseconds = 1500
236+ # Attach first.
237+ self .mock_serial .push_line (position )
238+ self .board .Servos .attach (pin )
239+ self .mock_serial .reset_mock ()
240+ self .board .Servos .writeMicroseconds (pin , microseconds )
241+ self .assertEquals (self .mock_serial .output [0 ],
242+ build_cmd_str ("svwm" , (position , microseconds )))
243+
244+ def test_read (self ):
245+ from Arduino .arduino import build_cmd_str
246+ pin = 10
247+ position = 0
248+ angle = 90
249+ # Attach first.
250+ self .mock_serial .push_line (position )
251+ self .board .Servos .attach (pin )
252+ self .mock_serial .reset_mock ()
253+ self .mock_serial .push_line (angle )
254+ self .assertEquals (self .board .Servos .read (pin ), angle )
255+ self .assertEquals (self .mock_serial .output [0 ],
256+ build_cmd_str ("svr" , (position ,)))
257+
258+
175259if __name__ == '__main__' :
176260 unittest .main ()
0 commit comments