-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.py
More file actions
70 lines (51 loc) · 1.72 KB
/
printer.py
File metadata and controls
70 lines (51 loc) · 1.72 KB
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
import serial
import os
from collections import deque
from atom.api import Atom, Unicode, observe, Typed, Property, Int, List, Bool
import enaml
from enaml.qt.qt_application import QtApplication
class Printer(Atom):
port = Unicode()
baudrate = Unicode()
# List of all lines to be sent to the printer
buffer = Typed(deque, ())
# The user selected path to send to the printer
path = Unicode(os.path.expanduser('~'))
# Whether or not the current print is paused
paused = Bool()
s = Typed(serial.Serial)
### Observers ############################################################
@observe('path')
def _path_changed(self, change):
print 'path changed!!!', self.path
if os.path.isfile(self.path):
self.load_file(self.path)
### Printer interface ####################################################
def connect(self):
port, baudrate = self.port, int(self.baudrate)
self.s = serial.Serial(port, baudrate)
def load_file(self, filename):
with open(filename) as f:
self.buffer = deque(f.readlines())
print self.buffer
def empty_buffer(self):
self.paused = False
while len(self.buffer) != 0:
line = self.buffer.popleft()
if '@pause' in line:
self.pause()
return
if not line.endswith('\n'):
line += '\n'
print 'SENDING:', line
self.s.write(line)
def pause(self):
self.paused = True
if __name__ == '__main__':
with enaml.imports():
from view import View
printer = Printer()
app = QtApplication()
view = View(model=printer)
view.show()
app.start()