5d3b6fc2b1691e6077346b725b35e200d41ac01f
Stefan Schuermans implement UDP packet output

Stefan Schuermans authored 7 years ago

1) import socket
2) import struct
3) 
Stefan Schuermans implement reading image data

Stefan Schuermans authored 7 years ago

4) from PIL import Image
5) 
Stefan Schuermans parsing config file complete

Stefan Schuermans authored 7 years ago

6) from mapping import Mapping
7) 
8) 
9) class Distributor(object):
10) 
11)     def __init__(self, distri_no, outputs, pixels):
12)         """create a new EtherPix distributor
13)            distri_no: distributor number
14)            outputs: number of outputs
15)            pixels: number of pixels per output"""
16)         self._distri_no = distri_no
17)         self._outputs = outputs
18)         self._pixels = pixels
19)         # default address
20)         self._addr = ("10.70.80.%u" % distri_no, 2323)
21)         # default color mapping
22)         self._mappings = []
23)         for c in range(Mapping.CHANNELS):
24)             self._mappings.append(Mapping())
25)         # pixel coordinates: all unknown
26)         self._pixel_coords = [[None] * self._pixels] * self._outputs
Stefan Schuermans implement UDP packet output

Stefan Schuermans authored 7 years ago

27)         # header for UDP packets
28)         self._udp_hdr = struct.pack("!I4H", 0x23542666,
29)                                     outputs, pixels, Mapping.CHANNELS, 255)
30)         # initial image data is cleared
31)         self.data_clear()
Stefan Schuermans parsing config file complete

Stefan Schuermans authored 7 years ago

32) 
33)     def check_output_no(self, output_no):
34)         """check output number, return True if okay, False if not okay"""
35)         return output_no >= 0 and output_no < self._outputs
36) 
37)     def get_outputs(self):
38)         """get number of outputs"""
39)         return self._outputs
40) 
41)     def get_pixels(self):
42)         """get number of pixels per output"""
43)         return self._pixels
44) 
45)     def set_addr(self, addr):
46)         """set distributor address"""
47)         self._addr = addr
48) 
Stefan Schuermans implement UDP packet output

Stefan Schuermans authored 7 years ago

49)     def data_clear(self):
50)         """clear image data, i.e. set entire image to black"""
51)         # prepare message buffer with all pixels cleared (black)
52)         clr = "".join([self._mappings[channel].table[0]
53)                        for channel in range(Mapping.CHANNELS)])
54)         self._buffer = self._udp_hdr + (clr * (self._outputs * self._pixels))
55) 
56)     def data_image(self, image):
57)         """set image data"""
Stefan Schuermans implement reading image data

Stefan Schuermans authored 7 years ago

58)         # get image size
59)         (width, height) = image.size
Stefan Schuermans implement UDP packet output

Stefan Schuermans authored 7 years ago

60)         # collect pixels from image and assemble message in buffer
61)         clr = [0] * Mapping.CHANNELS
62)         data = []
63)         for output_pixel_coords in self._pixel_coords:
Stefan Schuermans implement reading image data

Stefan Schuermans authored 7 years ago

64)             for x_y in output_pixel_coords:
65)                 if x_y is None:
66)                     pix = clr # pixel coordinates not known -> cleared
Stefan Schuermans implement UDP packet output

Stefan Schuermans authored 7 years ago

67)                 else:
Stefan Schuermans implement reading image data

Stefan Schuermans authored 7 years ago

68)                     (x, y) = x_y
69)                     if x < 0 or x >= width or y < 0 or y >= height:
70)                         pix = clr # outside of image -> cleared
71)                     else:
72)                         # get pixel from image
73)                         pix = image.getpixel(x_y)
74)                 # add pixel to pixel data
Stefan Schuermans implement UDP packet output

Stefan Schuermans authored 7 years ago

75)                 data += [self._mappings[channel].table[pix[channel]]
76)                                         for channel in range(Mapping.CHANNELS)]
Stefan Schuermans implement reading image data

Stefan Schuermans authored 7 years ago

77)         # build UDP message from pixel data
Stefan Schuermans implement UDP packet output

Stefan Schuermans authored 7 years ago

78)         self._buffer = self._udp_hdr + "".join(data)
79) 
80)     def send(self, socket):
81)         """send image data to actual distributor module using UDP"""
82)         try:
83)             socket.sendto(self._buffer, self._addr)
84)         except:
85)             pass
86)