3 # Copyright (C) 2014 Antonio Ospite <ao2@ao2.it>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 from array import array
21 from wx.lib.pubsub import setuparg1
22 from wx.lib.pubsub import pub
27 def __init__(self, width, height, internal_radius, scale=10):
32 self.internal_radius = float(internal_radius * scale)
33 self.external_radius = float(height + internal_radius) * scale
35 self.radius = float(height * scale)
37 self.ring_width = self.radius / height
38 self.sector_width = 360. / width
43 self.pixels_array = array('B', [0] * self.width * self.height * 3)
44 self.last_pixel = None
46 def toCartesian(self, r, theta):
47 x = int(theta / self.sector_width)
48 y = int(r / self.ring_width) - int(self.internal_radius / self.scale)
51 def toPolar(self, x, y):
52 r = y * self.ring_width + self.internal_radius
53 theta = x * self.sector_width
56 def setPixelColor(self, r, theta, color):
57 if r < self.internal_radius or r > self.external_radius:
58 print "invalid coordinates (r: %f)" % r
61 x, y = self.toCartesian(r, theta)
63 self.last_pixel = (x, y)
65 offset = y * self.width * 3 + x * 3
66 self.pixels_array[offset + 0] = color[0]
67 self.pixels_array[offset + 1] = color[1]
68 self.pixels_array[offset + 2] = color[2]
70 # now tell anyone who cares that the value has been changed
71 pub.sendMessage("NEW PIXEL", self.last_pixel)
73 def getPixelColor(self, x, y):
74 offset = y * self.width * 3 + x * 3
75 color = [self.pixels_array[offset + i] for i in range(0, 3)]
80 for x in range(self.width):
81 for y in range(self.height):
82 colors.add(self.getPixelColor(x, y))
85 # The code below has been copied from the C implementation in PatternPaint:
86 # https://github.com/Blinkinlabs/PatternPaint
87 def saveAsAnimation(self, filename):
88 output_file = open(filename, "w")
90 colors = self.getColors()
92 output_file.write("const PROGMEM prog_uint8_t animationData[] = {\n")
94 output_file.write("// Length of the color table - 1, in bytes. length: 1 byte\n")
95 output_file.write(" %d,\n" % (len(colors) - 1))
97 output_file.write("// Color table section. Each entry is 3 bytes. length: %d bytes\n" % (len(colors) * 3))
100 for i, c in enumerate(colors):
101 output_file.write(" %3d, %3d, %3d,\n" % (c[0], c[1], c[2]))
104 output_file.write("// Pixel runs section. Each pixel run is 2 bytes. length: -1 bytes\n")
106 for x in range(self.width):
108 for y in range(self.height):
109 new_color = color_map[self.getPixelColor(x, y)]
111 current_color = new_color
113 if current_color != new_color:
114 output_file.write(" %3d, %3d,\n" % (run_count, current_color))
116 current_color = new_color
120 output_file.write(" %3d, %3d,\n" % (run_count, current_color))
122 output_file.write("};\n\n")
124 output_file.write("#define NUM_FRAMES %d\n" % self.width)
125 output_file.write("#define NUM_LEDS %d\n" % self.height)
126 output_file.write("Animation animation(NUM_FRAMES, animationData, NUM_LEDS);\n")