kod od kupy

master
Maciej Kozlowski 2019-05-24 17:53:11 +02:00
commit 910ce1b86b
3 changed files with 280 additions and 0 deletions

Binary file not shown.

68
flaschen.py Normal file
View File

@ -0,0 +1,68 @@
# -*- mode: python; c-basic-offset: 2; indent-tabs-mode: nil; -*-
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://gnu.org/licenses/gpl-2.0.txt>
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)

212
kupa.py Executable file
View File

@ -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()