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 Publisher as pub
26 def __init__(self, width, height, internal_radius, scale=10):
31 self.internal_radius = float(internal_radius * scale)
32 self.external_radius = float(height + internal_radius) * scale
34 self.radius = float(height * scale)
36 self.ring_width = self.radius / height
37 self.sector_width = 360. / width
42 self.pixels_array = array('B', [0] * self.width * self.height * 3)
43 self.last_pixel = None
45 def toCartesian(self, r, theta):
46 x = int(theta / self.sector_width)
47 y = int(r / self.ring_width) - int(self.internal_radius / self.scale)
50 def toPolar(self, x, y):
51 r = y * self.ring_width + self.internal_radius
52 theta = x * self.sector_width
55 def setPixelColor(self, r, theta, color):
56 if r < self.internal_radius or r > self.external_radius:
57 print "invalid coordinates (r: %f)" % r
60 x, y = self.toCartesian(r, theta)
62 self.last_pixel = (x, y)
64 offset = y * self.width * 3 + x * 3
65 self.pixels_array[offset + 0] = color[0]
66 self.pixels_array[offset + 1] = color[1]
67 self.pixels_array[offset + 2] = color[2]
69 # now tell anyone who cares that the value has been changed
70 pub.sendMessage("NEW PIXEL", self.last_pixel)
72 def getPixelColor(self, x, y):
73 offset = y * self.width * 3 + x * 3
74 color = [self.pixels_array[offset + i] for i in range(0, 3)]
79 for x in range(self.width):
80 for y in range(self.height):
81 colors.add(self.getPixelColor(x, y))
84 # The code below has been copied from the C implementation in PatternPaint:
85 # https://github.com/Blinkinlabs/PatternPaint
86 def saveAsAnimation(self, filename):
87 output_file = open(filename, "w")
89 colors = self.getColors()
91 output_file.write("const PROGMEM prog_uint8_t animationData[] = {\n")
93 output_file.write("// Length of the color table - 1, in bytes. length: 1 byte\n")
94 output_file.write(" %d,\n" % (len(colors) - 1))
96 output_file.write("// Color table section. Each entry is 3 bytes. length: %d bytes\n" % (len(colors) * 3))
99 for i, c in enumerate(colors):
100 output_file.write(" %3d, %3d, %3d,\n" % (c[0], c[1], c[2]))
103 output_file.write("// Pixel runs section. Each pixel run is 2 bytes. length: -1 bytes\n")
105 for x in range(self.width):
107 for y in range(self.height):
108 new_color = color_map[self.getPixelColor(x, y)]
110 current_color = new_color
112 if current_color != new_color:
113 output_file.write(" %3d, %3d,\n" % (run_count, current_color))
115 current_color = new_color
119 output_file.write(" %3d, %3d,\n" % (run_count, current_color))
121 output_file.write("};\n\n")
123 output_file.write("#define NUM_FRAMES %d\n" % self.width)
124 output_file.write("#define NUM_LEDS %d\n" % self.height)
125 output_file.write("Animation animation(NUM_FRAMES, animationData, NUM_LEDS);\n")