Add a another two examples of patterns
[PoPiPaint.git] / CanvasModel.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2014  Antonio Ospite <ao2@ao2.it>
4 #
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.
9 #
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.
14 #
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/>.
17
18 import sys
19 from array import array
20 import wx
21 from wx.lib.pubsub import setuparg1
22 from wx.lib.pubsub import pub
23
24
25 class Canvas:
26
27     def __init__(self, width, height, internal_radius, scale=10):
28         self.width = width
29         self.height = height
30         self.scale = scale
31
32         self.internal_radius = float(internal_radius * scale)
33         self.external_radius = float(height + internal_radius) * scale
34
35         self.radius = float(height * scale)
36
37         self.ring_width = self.radius / height
38         self.sector_width = 360. / width
39
40         self.Reset()
41
42     def Reset(self):
43         self.pixels_array = array('B', [0] * self.width * self.height * 3)
44         self.last_pixel = None
45
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)
49         return (x, y)
50
51     def toPolar(self, x, y):
52         r = y * self.ring_width + self.internal_radius
53         theta = x * self.sector_width
54         return (r, theta)
55
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
59             return
60
61         x, y = self.toCartesian(r, theta)
62
63         self.last_pixel = (x, y)
64
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]
69
70         # now tell anyone who cares that the value has been changed
71         pub.sendMessage("NEW PIXEL", self.last_pixel)
72
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)]
76         return tuple(color)
77
78     def getColors(self):
79         colors = set()
80         for x in range(self.width):
81             for y in range(self.height):
82                 colors.add(self.getPixelColor(x, y))
83         return list(colors)
84
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")
89
90         colors = self.getColors()
91
92         output_file.write("const PROGMEM prog_uint8_t animationData[]  = {\n")
93
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))
96
97         output_file.write("// Color table section. Each entry is 3 bytes. length: %d bytes\n" % (len(colors) * 3))
98
99         color_map = {}
100         for i, c in enumerate(colors):
101             output_file.write(" %3d, %3d, %3d,\n" % (c[0], c[1], c[2]))
102             color_map[c] = i
103
104         output_file.write("// Pixel runs section. Each pixel run is 2 bytes. length: -1 bytes\n")
105
106         for x in range(self.width):
107             run_count = 0
108             for y in range(self.height):
109                 new_color = color_map[self.getPixelColor(x, y)]
110                 if run_count == 0:
111                     current_color = new_color
112
113                 if current_color != new_color:
114                     output_file.write(" %3d, %3d,\n" % (run_count, current_color))
115                     run_count = 1
116                     current_color = new_color
117                 else:
118                     run_count += 1
119
120             output_file.write(" %3d, %3d,\n" % (run_count, current_color))
121
122         output_file.write("};\n\n")
123
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")
127         output_file.close()