Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 205 lines (159 sloc) 5.022 kb
25ae5b0 @ladyada pattern inserter - find first empty pattern storage bank
ladyada authored
1 #!/usr/bin/env python
2
3 # Copyright 2009 Steve Conklin
4 # steve at conklinhouse dot com
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 import sys
21 import brother
99f4171 @ladyada Created the 'program entry' byte pattern
ladyada authored
22 import Image
23 import array
24
25 TheImage = None
26
4711b35 @ladyada created the nibblized/byteized pattern stitching data
ladyada authored
27 ##################
99f4171 @ladyada Created the 'program entry' byte pattern
ladyada authored
28
4711b35 @ladyada created the nibblized/byteized pattern stitching data
ladyada authored
29 def roundeven(val):
30 return (val+(val%2))
31
32 def roundeight(val):
33 if val % 8:
34 return val + (8-(val%8))
35 else:
36 return val
37
38 def roundfour(val):
39 if val % 4:
40 return val + (4-(val%4))
41 else:
42 return val
43
44 def nibblesPerRow(stitches):
45 # there are four stitches per nibble
46 # each row is nibble aligned
47 return(roundfour(stitches)/4)
48
49 def bytesPerPattern(stitches, rows):
50 nibbs = rows * nibblesPerRow(stitches)
51 bytes = roundeven(nibbs)/2
52 return bytes
53
54 def bytesForMemo(rows):
55 bytes = roundeven(rows)/2
56 return bytes
57
58 ##############
99f4171 @ladyada Created the 'program entry' byte pattern
ladyada authored
59
25ae5b0 @ladyada pattern inserter - find first empty pattern storage bank
ladyada authored
60
61 version = '1.0'
62
e201cd5 @ladyada pattern program entry insertion seems to work OK
ladyada authored
63 if len(sys.argv) < 4:
64 print 'Usage: %s oldbrotherfile image.bmp newbrotherfile' % sys.argv[0]
25ae5b0 @ladyada pattern inserter - find first empty pattern storage bank
ladyada authored
65 sys.exit()
66
99f4171 @ladyada Created the 'program entry' byte pattern
ladyada authored
67 imgfile = sys.argv[2]
25ae5b0 @ladyada pattern inserter - find first empty pattern storage bank
ladyada authored
68
69 bf = brother.brotherFile(sys.argv[1])
70
e201cd5 @ladyada pattern program entry insertion seems to work OK
ladyada authored
71 outfile = open(sys.argv[3], 'w')
72
25ae5b0 @ladyada pattern inserter - find first empty pattern storage bank
ladyada authored
73 pats = bf.getPatterns()
74
75 # find first unused pattern bank
76 patternbank = 100
77 for i in range(99):
78 bytenum = i*7
79 if (bf.getIndexedByte(bytenum) != 0x1):
80 print "first unused pattern bank #", i
81 patternbank = i
82 break
83
84 if (patternbank == 100):
85 print "sorry, no free banks!"
86 exit
99f4171 @ladyada Created the 'program entry' byte pattern
ladyada authored
87
88 # ok got a bank, now lets figure out how big this thing we want to insert is
89 TheImage = Image.open(imgfile)
90 TheImage.load()
91
92 im_size = TheImage.size
93 width = im_size[0]
94 print "width:",width
95 height = im_size[1]
96 print "height:", height
97
98 # debugging stuff here
99 x = 0
100 y = 0
101
102 x = width - 1
103 while x > 0:
104 value = TheImage.getpixel((x,y))
105 if value:
106 sys.stdout.write('* ')
107 else:
108 sys.stdout.write(' ')
109 #sys.stdout.write(str(value))
110 x = x-1
111 if x == 0: #did we hit the end of the line?
112 y = y+1
113 x = width - 1
114 print " "
115 if y == height:
116 break
117 # debugging stuff done
118
119 # create the program entry
120 progentry = []
121 progentry.append(0x1) # is used
122 progentry.append(0x0) # no idea what this is
123 progentry.append( (int(width / 100) << 4) | (int((width%100) / 10) & 0xF) )
124 progentry.append( (int(width % 10) << 4) | (int(height / 100) & 0xF) )
125 progentry.append( (int((height % 100)/10) << 4) | (int(height % 10) & 0xF) )
126
127 # now we have to pick out a 'program name'
128 patternnum = 901 # start with 901? i dunno
129 for pat in pats:
130 if (pat["number"] >= patternnum):
131 patternnum = pat["number"] + 1
132
133 #print patternnum
134 progentry.append(int(patternnum / 100) & 0xF)
135 progentry.append( (int((patternnum % 100)/10) << 4) | (int(patternnum % 10) & 0xF) )
136
137 print "Program entry: ",map(hex, progentry)
4711b35 @ladyada created the nibblized/byteized pattern stitching data
ladyada authored
138
139 # now to make the actual, yknow memo+pattern data
140
141 # the memo seems to be always blank. i have no idea really
142 pattentry = []
143 for i in range(bytesForMemo(height)):
144 pattentry.append(0x0)
145
146 # now for actual real live pattern data!
147 pattmemnibs = []
148 for r in range(height):
149 row = [] # we'll chunk in bits and then put em into nibbles
150 for s in range(width):
151 value = TheImage.getpixel((s,r))
152 if (value != 0):
153 row.append(1)
154 else:
155 row.append(0)
156 #print row
157 # turn it into nibz
158 for s in range(roundfour(width) / 4):
159 n = 0
160 for nibs in range(4):
161 #print "row size = ", len(row), "index = ",s*4+nibs
162
163 if (len(row) == (s*4+nibs)):
164 break # padding!
165
166
167 if (row[s*4 + nibs]):
168 n |= 1 << nibs
169 pattmemnibs.append(n)
170 print hex(n),
171 print
172
173
174 if (len(pattmemnibs) % 2):
175 # odd nibbles, buffer to a byte
176 pattmemnibs.append(0x0)
177
178 print len(pattmemnibs), "nibbles of data"
179
180 # turn into bytes
181 pattmem = []
182 for i in range (len(pattmemnibs) / 2):
183 pattmem.append( pattmemnibs[i*2] | (pattmemnibs[i*2 + 1] << 4))
184
185 print map(hex, pattmem)
186 # whew.
e201cd5 @ladyada pattern program entry insertion seems to work OK
ladyada authored
187
188 # now to insert this data into the file by creating a NEW file
189
190 patternbankptr = patternbank*7
191
192 # write old original patterns
193 for i in range (patternbankptr):
194 # copy & paste this data
195 outfile.write(chr(bf.getIndexedByte(i)))
196
197 # write the new pattern!@
198 for i in range(7):
199 outfile.write(chr(progentry[i]))
200
201 for i in range(patternbankptr+7, 2048):
202 outfile.write(chr(bf.getIndexedByte(i)))
203
204 outfile.close()
Something went wrong with that request. Please try again.