Commit 2a21c54b authored by mik's avatar mik
Browse files

upload

parent 8fbc80b3
import ugfx
import time
import badge
import wifi
import appglue
import uos
import ubinascii
from umqtt.simple import MQTTClient
class WinkeKatze:
# |\_/|
# (. .)
# =w= (\
# / ^ \//
# (|| ||)
# ,""_""_ .
# |\_/|
# (. .)
# =w=
# / ^ \\
# (|| ||)\\
# ,""_""_(/.
def draw_cat(self,cat='up',footer="https://devlol.org - press A to wink all other cats", nick=''):
i=2
if (cat == 'up'):
drawcat = self.cat
elif (cat == 'down'):
drawcat = self.catdown
ugfx.clear(ugfx.WHITE)
for line in drawcat:
i += 1
j=0
for char in line:
j +=1
ugfx.string(8*j, 12*i,char, self.font, ugfx.BLACK)
print (line)
if footer:
i +=1
ugfx.string(8,12*i,footer,self.font,ugfx.BLACK)
if nick:
ugfx.string(110,90,("%s winks" % (str(self.nick))),self.font,ugfx.BLACK)
ugfx.string(110,0,self.owner,"PermanentMarker36",ugfx.BLACK)
ugfx.flush()
def sub_cb(self,topic,msg):
print ("received mqtt message topic: %s, payload: %s" % (topic,msg))
if msg == b'WINK3':
for count in [1,2,3]:
print ("wink")
self.draw_cat(cat="down",nick=self.nick)
badge.leds_send_data(''.join(['\0\0\0\50' for i in range(6)]))
badge.vibrator_activate(127)
time.sleep(0.5)
self.draw_cat(nick=self.nick)
badge.leds_send_data(''.join(['\0\0\0\0' for i in range(6)]))
time.sleep(0.5)
if topic.endswith('/nick'):
print ("setting received nick to %s" % msg.decode("utf-8"))
self.nick = msg.decode("utf-8")
def btn_select(self,pressed):
if pressed:
print("Returning to homescreen")
badge.leds_disable()
appglue.start_app("")
return 0
def btn_a(self,pressed):
if pressed:
print ("publish nick %s to topic %s/nick" % ( self.owner, self.topic))
self.c.publish(("%s/nick" % self.topic), self.owner)
print ("publish WINK3 to topic %s" % ( self.topic))
self.c.publish(("%s" % self.topic), "WINK3")
def btn_b(self,pressed):
pass
def btn_up(self,pressed):
pass
def btn_down(self,pressed):
pass
def btn_left(self,pressed):
pass
def btn_right(self,pressed):
pass
def btn_start(self,pressed):
pass
def __init__(self, font="fixed_10x20",server='mqtt.devlol.org',topic='devlol/h19/mainroom/winkekatze'):
self.cat = [' |\_/| ', ' (. .) ', ' =w= (\ ', ' / ^ \// ', ' (|| ||) ', ' ,""_""_ .']
self.catdown =[' |\_/| ', ' (. .) ', ' =w= ', ' / ^ \\\\ ', ' (|| ||)\\', ' ,""_""_(/.']
self.font = font
self.clientname = 'SHA2017Badge' + str(ubinascii.hexlify(uos.urandom(5)))
self.server = server
self.topic = topic
self.nick = ''
badge.init()
badge.leds_enable()
#workaround for emulator
try:
self.owner = badge.nvs_get_str("owner","name","anonycat")
except:
self.owner ="anonycat"
ugfx.init()
wifi.init()
while not wifi.sta_if.isconnected():
time.sleep(1)
print ("waiting for wifi")
pass
print ("connecting to mqtt server:%s clientname:%s" %(self.server,self.clientname))
self.c = MQTTClient(self.clientname,self.server)
self.c.set_callback(self.sub_cb)
self.c.connect()
self.c.subscribe("%s/#" % self.topic)
ugfx.clear(ugfx.BLACK)
ugfx.flush
ugfx.clear(ugfx.WHITE)
ugfx.flush
ugfx.input_init()
ugfx.input_attach(ugfx.JOY_UP,lambda pressed: self.btn_up(pressed))
ugfx.input_attach(ugfx.JOY_DOWN,lambda pressed: self.btn_down(pressed))
ugfx.input_attach(ugfx.JOY_LEFT,lambda pressed: self.btn_left(pressed))
ugfx.input_attach(ugfx.JOY_RIGHT,lambda pressed: self.btn_right(pressed))
ugfx.input_attach(ugfx.BTN_SELECT,lambda pressed: self.btn_select(pressed))
ugfx.input_attach(ugfx.BTN_START,lambda pressed: self.btn_start(pressed))
ugfx.input_attach(ugfx.BTN_A,lambda pressed: self.btn_a(pressed))
ugfx.input_attach(ugfx.BTN_B,lambda pressed: self.btn_b(pressed))
self.draw_cat()
while True:
time.sleep(0.5)
self.c.check_msg()
wk = WinkeKatze()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment