Added ifndef/def/endif protection to C headers.
[stripchart.git] / stripchart.py
1 # stripchart - tools for quick and dirty data plotting.
2 #
3 # Copyright (C) 2008-2009 William Trevor King
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 # See the GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 # 02111-1307, USA.
19 #
20 # The author may be contacted at <wking@drexel.edu> on the Internet, or
21 # write to Trevor King, Drexel University, Physics Dept., 3141 Chestnut St.,
22 # Philadelphia PA 19104, USA.
23
24 import os
25
26 class stripchart :
27     def __init__(self, pipename='strip_pipe', maxY=10, minY=-10,
28                  npoints=100, title='chart', axes=True) :
29         self.pipename = pipename
30         self.maxY = maxY
31         self.minY = minY
32         self.npoints = npoints
33         self.title = title
34         self.axes = axes
35         self.status = 'closed'
36         self.pipe = None
37     def __del__(self) :
38         self.close()
39     def open(self, debug=False) :
40         if self.status == 'closed' :
41             os.mkfifo(self.pipename)
42             cmd = "stripchart -t '%s' -p '%s' -u -M '%g' -m '%g' -n '%d'" % \
43                 (self.title, self.pipename, self.maxY, self.minY,
44                  self.npoints)
45             if self.axes == False :
46                 cmd += " -a"
47             cmd += " &"
48             if debug :
49                 print cmd
50             os.system(cmd)
51             self.pipe = open(self.pipename,'w')
52             self.status = 'open'
53     def close(self) :
54         if self.status == 'open' :
55             print >> self.pipe, "q"
56             self.pipe.close()
57             self.pipe = None
58             self.status = 'closed'
59     def _add_point(self, point) :
60         print >> self.pipe, point
61     def _flush(self) :
62         self.pipe.flush()
63     def add_point(self, point) :
64         self._add_point(point)
65         self._flush()