Toggle Navigation
Hatchery
Eggs
png_test
png_test.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
png_test.py
raw
Content
import ugfx import time import badge import wifi import appglue import uos import ubinascii from umqtt.simple import MQTTClient class Png_Test: # |\_/| # (. .) # =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,80,("%s winks" % (str(self.nick1))),self.font,ugfx.BLACK) ugfx.string(110,70,("%s winks" % (str(self.nick2))),self.font,ugfx.BLACK) ugfx.string(110,60,("%s winks" % (str(self.nick3))),self.font,ugfx.BLACK) ugfx.string(110,50,("%s winks" % (str(self.nick4))),self.font,ugfx.BLACK) ugfx.string(110,0,self.owner,"PermanentMarker36",ugfx.BLACK) ugfx.flush() def new_wink(self,msg): self.nick4 = self.nick3 self.nick3 = self.nick2 self.nick2 = self.nick1 self.nick1 = self.nick self.nick = msg.decode("utf-8") 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.new_wink(msg) #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 = '' self.nick1 = ' X ' self.nick2 = ' X X ' self.nick3 = ' X X ' self.nick4 = ' X X ' 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() pn = Png_Test()