From 910ce1b86b04b5fb2055c3d8c53dd5e0aee23e59 Mon Sep 17 00:00:00 2001 From: Maciej Kozlowski <> Date: Fri, 24 May 2019 17:53:11 +0200 Subject: [PATCH] kod od kupy --- __pycache__/flaschen.cpython-37.pyc | Bin 0 -> 2141 bytes flaschen.py | 68 +++++++++ kupa.py | 212 ++++++++++++++++++++++++++++ 3 files changed, 280 insertions(+) create mode 100644 __pycache__/flaschen.cpython-37.pyc create mode 100644 flaschen.py create mode 100755 kupa.py diff --git a/__pycache__/flaschen.cpython-37.pyc b/__pycache__/flaschen.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5d72788dcad550653d41883e08a7ff3dc1487085 GIT binary patch literal 2141 zcmb7FUuzsU5SRAft@rHwF>c&HDTp+tzT()XC8bYYd-Ma8KI*kl@K@+dJEOg`O%n)}t%pQ?!#0!fpEfUM!MuN;TE@#3AY73vbz?iTcqWjfd<)(kxqvV%zc>Z6Ci?g zDJQ7iwRw{}+&!jUhkLwnOu8=j`3$Tc55U%}_BXOf#eI>FzwEFL8SRPQFiC`DJXM1% z8nHAlg-oJYu(BVOOo^N;7O@0X*?t@)}AWjT~X>rXCr*F`So)hY<>6e+wjrj z?>kSm8{)2R#6_NqxYW*0k>=VHd0cRzXL_SjL{dg0ZD%3}Z61}8ZiamkaUnyHwPv)V zM3!g=%SJo+_!3bjeI$r8i9H2F#4}wlDGI>ruSC8N;=k9|UO?HYwY?}WQ?>R;>_bup zdyw9>Z?;}`Uasv92hrNJR96Qh?T2BSr)3x}z=24RQDAq1z}hre z0NDiLqc$78rZo~7`nk|DKH99qK4ax@kU_Ud*s@$<+kl2v9Rb^qvY}7_i91-%*ojQ_ zZnkW*UXYf3hGi{3JSX@r=JU(~uH{~;DJ&=X4ic;Y*t!9WriYr2x~>UJd-WwviPhu- z)UhG_bzsyrAjH2-9jFw?dKc(C9nYRu38=*w4-Q-rccwt*fx#y}!6KP-e|Rcu{P7n2 z>X=s;4ge$Y26g+IzRjest>V?MfDr^u&g%z) zYNORaKw!HLLmh + +import socket + +class Flaschen(object): + '''A Framebuffer display interface that sends a frame via UDP.''' + + def __init__(self, host, port, width, height, layer=5, transparent=False): + ''' + + Args: + host: The flaschen taschen server hostname or ip address. + port: The flaschen taschen server port number. + width: The width of the flaschen taschen display in pixels. + height: The height of the flaschen taschen display in pixels. + layer: The layer of the flaschen taschen display to write to. + transparent: If true, black(0, 0, 0) will be transparent and show the layer below. + ''' + self.width = width + self.height = height + self.layer = layer + self.transparent = transparent + self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self._sock.connect((host, port)) + header = ''.join(["P6\n", + "%d %d\n" % (self.width, self.height), + "255\n"]).encode('utf-8') + footer = ''.join(["0\n", + "0\n", + "%d\n" % self.layer]).encode('utf-8') + self._data = bytearray(width * height * 3 + len(header) + len(footer)) + self._data[0:len(header)] = header + self._data[-1 * len(footer):] = footer + self._header_len = len(header) + + def set(self, x, y, color): + '''Set the pixel at the given coordinates to the specified color. + + Args: + x: x offset of the pixel to set + y: y offset of the piyel to set + color: A 3 tuple of (r, g, b) color values, 0-255 + ''' + if x >= self.width or y >= self.height or x < 0 or y < 0: + return + if color == (0, 0, 0) and not self.transparent: + color = (1, 1, 1) + + offset = (x + y * self.width) * 3 + self._header_len + self._data[offset] = color[0] + self._data[offset + 1] = color[1] + self._data[offset + 2] = color[2] + + def send(self): + '''Send the updated pixels to the display.''' + self._sock.send(self._data) diff --git a/kupa.py b/kupa.py new file mode 100755 index 0000000..6785916 --- /dev/null +++ b/kupa.py @@ -0,0 +1,212 @@ +import paho.mqtt.client as mqtt +import requests +import time +import os +import flaschen +import threading +import time + +state = { + 'startup': 0, + 'last_change': None, +} + +p=None + +UDP_IP = '10.8.0.146' +UDP_PORT = 1337 +isKupa=True + +letter_k=[ + 4, + ["1001", + "1010", + "1100", + "1010", + "1001"] +] + +letter_u=[ + 4, + ["1001", + "1001", + "1001", + "1001", + "0110"] +] + +letter_p=[ + 4, + ["1110", + "1001", + "1110", + "1000", + "1000"] +] + +letter_a=[ + 4, + ["0110", + "1001", + "1111", + "1001", + "1001"] +] + +letter_space=[ + 4, + ["0000", + "0000", + "0000", + "0000", + "0000"] +] + +ft = flaschen.Flaschen(UDP_IP, UDP_PORT,8, 5) + +current_text_offset=0 + +def get_char_pixel_length(ch): + l = { + 'k': letter_k[0], + 'u': letter_u[0], + 'p': letter_p[0], + 'a': letter_a[0], + ' ': letter_space[0] + }.get(ch, 0) + return l + 1 + +def get_char_pixel_bitmap(ch): + l = { + 'k': letter_k[1], + 'u': letter_u[1], + 'p': letter_p[1], + 'a': letter_a[1], + ' ': letter_space[1] + }.get(ch, ["0000","0000","0000","0000","0000"]) + return l + +def get_string_pixel_length(st): + l=0 + for c in st: + l += get_char_pixel_length(c) + + return l + +def print_letter(ch): + white=(255,255,255) + for x in range(0,ch[0]): + for y in range(0,5): + if ch[1][y][x]=='1': + ft.set(x,y,white) + +def get_text_vline(st, i): + done=False + letter=0 + offset=0 + previous_offset = 0 + if i >= get_string_pixel_length(st): + return['0','0','0','0','0'] + while not done: + offset +=get_char_pixel_length(st[letter]) + if i < offset-1: + + done=True + vline = [get_char_pixel_bitmap(st[letter])[0][i-previous_offset],get_char_pixel_bitmap(st[letter])[1][i-previous_offset],get_char_pixel_bitmap(st[letter])[2][i-previous_offset],get_char_pixel_bitmap(st[letter])[3][i-previous_offset], get_char_pixel_bitmap(st[letter])[4][i-previous_offset]] + return vline + elif i == offset-1: + + vline = ['0','0','0','0','0'] + done=True + else: + + letter += 1 + previous_offset = offset + return vline + +def prepare_text_block(st): + l=get_string_pixel_length(st) + #l+=8 # add full clear screen for text rotation + block=[] + i=0 + for vline in range(0,l): + block.append(get_text_vline(st, i)) + i+=1 + + return block + + +def print_text(block): + for x in range(current_text_offset, current_text_offset+8): + for y in range(0, ft.height): + if block[x][y] == '1': + ft.set(x-current_text_offset, y, (255,255,255)) + +def render_thread(name): + global current_text_offset + text=" kupa " + print("we start sraning") + block = prepare_text_block(text) + while isKupa: + for y in range(0, ft.height): + for x in range(0, ft.width): + ft.set(x, y, (255, 0, 0)) + print_text(block) + #print_letter(letter_k) + ft.send() + time.sleep(0.125) + current_text_offset+=1 + if (current_text_offset >= get_string_pixel_length(text)-8): + current_text_offset = 0 + +# The callback for when the client receives a CONNACK response from the server. +def on_connect(client, userdata, flags, rc): + print("Connected with result code "+str(rc)) + global state + state['startup'] = time.time() + + # Subscribing in on_connect() means that if we lose the connection and + # reconnect then subscriptions will be renewed. + client.subscribe("iot/247a1d/#") + +# The callback for when a PUBLISH message is received from the server. +def on_message(client, userdata, msg): + global state + global isKupa + if time.time() - state['startup'] > 1.0: + print(msg.topic+" "+str(msg.payload)) + if msg.topic.endswith('shitting/state'): + print(msg.payload.decode()) + s = msg.payload.decode() == 'true' + + toilet_state = 'OCCUPIED' if s else 'FREE' + shitting_period = time.time() - state['last_change'] if state['last_change'] else None + + try: + if s == True: + p = threading.Thread(target=render_thread, args=(1,)) + isKupa=True + print("isKupa is on") + p.start() + else: + print("isKupa is off") + isKupa=False + + except Exception as exc: + print(exc) + + + state['last_change'] = time.time() + + +client = mqtt.Client() +client.on_connect = on_connect +client.on_message = on_message + +client.connect("iot.waw.hackerspace.pl", 1883, 60) + +# Blocking call that processes network traffic, dispatches callbacks and +# handles reconnecting. +# Other loop*() functions are available that give a threaded interface and a +# manual interface. +client.loop_forever()