from m5stack import * from m5stack_ui import * from uiflow import * from m5mqtt import M5mqtt import machine import time import unit screen = M5Screen() screen.clean_screen() screen.set_screen_bg_color(0x000000) cardkb_0 = unit.get(unit.CARDKB, unit.PORTA) value = None numberOfDice = None brokerreply = None text = None glitches = None armorAmount = None phsyDmgAmount = None stunDmgAmount = None penalty = None successes = None sixes = None penaltyStun = None i = None random2 = None diceList = None stats = M5Btn(text='STATS', x=20, y=24, w=280, h=50, bg_c=0xFFFFFF, text_c=0x000000, font=FONT_MONT_26, parent=None) dice = M5Btn(text='DICE', x=20, y=95, w=280, h=50, bg_c=0xFFFFFF, text_c=0x000000, font=FONT_MONT_26, parent=None) comms = M5Btn(text='COMMS', x=20, y=165, w=280, h=50, bg_c=0xFFFFFF, text_c=0x000000, font=FONT_MONT_26, parent=None) armor = M5Label('Armor', x=41, y=24, color=0xffffff, font=FONT_MONT_22, parent=None) armorNumber = M5Label('0', x=118, y=25, color=0xffffff, font=FONT_MONT_22, parent=None) subArmor = M5Btn(text='-', x=162, y=15, w=40, h=40, bg_c=0xFFFFFF, text_c=0x000000, font=FONT_MONT_30, parent=None) addArmor = M5Btn(text='+', x=212, y=15, w=40, h=40, bg_c=0xFFFFFF, text_c=0x000000, font=FONT_MONT_30, parent=None) physDmg = M5Label('Physical Damage', x=30, y=66, color=0xffffff, font=FONT_MONT_22, parent=None) physDmgNum = M5Label('0', x=41, y=101, color=0xffffff, font=FONT_MONT_22, parent=None) subPhysDmg = M5Btn(text='-', x=80, y=92, w=40, h=40, bg_c=0xFFFFFF, text_c=0x000000, font=FONT_MONT_30, parent=None) numberDiceSlider = M5Slider(x=60, y=65, w=200, h=12, min=1, max=30, bg_c=0xa0a0a0, color=0x8a08b0, parent=None) addPhysDmg = M5Btn(text='+', x=130, y=92, w=40, h=40, bg_c=0xFFFFFF, text_c=0x000000, font=FONT_MONT_30, parent=None) stunDmg = M5Label('Stun Damage', x=30, y=147, color=0xffffff, font=FONT_MONT_22, parent=None) numberDiceLabel = M5Label('Dice', x=61, y=8, color=0xffffff, font=FONT_MONT_22, parent=None) physPenalty = M5Label('0', x=240, y=99, color=0xe00b0b, font=FONT_MONT_30, parent=None) numDice = M5Label('0', x=220, y=24, color=0xffffff, font=FONT_MONT_26, parent=None) stunDmgNum = M5Label('0', x=41, y=180, color=0xffffff, font=FONT_MONT_22, parent=None) numSuccess = M5Label('0', x=41, y=95, color=0x27ca1f, font=FONT_MONT_48, parent=None) subStunDmg = M5Btn(text='-', x=80, y=175, w=40, h=40, bg_c=0xFFFFFF, text_c=0x000000, font=FONT_MONT_30, parent=None) numSix = M5Label('0', x=138, y=95, color=0x1f57ca, font=FONT_MONT_48, parent=None) addStunDmg = M5Btn(text='+', x=130, y=175, w=40, h=40, bg_c=0xFFFFFF, text_c=0x000000, font=FONT_MONT_30, parent=None) stunPenalty = M5Label('0', x=239, y=180, color=0xe00b0b, font=FONT_MONT_30, parent=None) numGlitch = M5Label('0', x=230, y=95, color=0xe51313, font=FONT_MONT_48, parent=None) hello = M5Btn(text='SEND', x=20, y=165, w=280, h=50, bg_c=0x37085c, text_c=0xffffff, font=FONT_MONT_26, parent=None) rollDice = M5Btn(text='ROLL', x=20, y=165, w=280, h=50, bg_c=0x710c5f, text_c=0xffffff, font=FONT_MONT_26, parent=None) showtext = M5Label('0', x=30, y=31, color=0xffffff, font=FONT_MONT_18, parent=None) start = M5Btn(text='START', x=20, y=95, w=130, h=50, bg_c=0xFFFFFF, text_c=0x000000, font=FONT_MONT_26, parent=None) end = M5Btn(text='END', x=170, y=95, w=130, h=50, bg_c=0xFFFFFF, text_c=0x000000, font=FONT_MONT_26, parent=None) reply = M5Label('reply', x=30, y=59, color=0xffffff, font=FONT_MONT_18, parent=None) import random from numbers import Number import math def upRange(start, stop, step): while start <= stop: yield start start += abs(step) def downRange(start, stop, step): while start >= stop: yield start start -= abs(step) # Describe this function... def homeScreen(): global value, numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, i, random2, diceList, PWM0 stats.set_hidden(False) stats.set_bg_color(0x663366) dice.set_hidden(False) dice.set_bg_color(0x993399) comms.set_hidden(False) comms.set_bg_color(0xcc33cc) # Describe this function... def gettext(): global value, numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, i, random2, diceList, PWM0 pass # Describe this function... def commScreen(): global value, numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, i, random2, diceList, PWM0 hello.set_hidden(False) start.set_hidden(False) end.set_hidden(False) showtext.set_hidden(False) reply.set_hidden(False) text = '' # Describe this function... def diceScreen(): global value, numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, i, random2, diceList, PWM0 numberDiceLabel.set_hidden(False) numDice.set_hidden(False) numSuccess.set_hidden(False) numSix.set_hidden(False) numGlitch.set_hidden(False) rollDice.set_hidden(False) numberDiceSlider.set_hidden(False) physDmgNum.set_text(str(phsyDmgAmount)) stunDmgNum.set_text(str(stunDmgAmount)) # Describe this function... def statsScreen(): global value, numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, i, random2, diceList, PWM0 armor.set_hidden(False) armorNumber.set_hidden(False) physDmg.set_hidden(False) physDmgNum.set_hidden(False) physPenalty.set_hidden(False) stunDmg.set_hidden(False) stunDmgNum.set_hidden(False) stunPenalty.set_hidden(False) subArmor.set_hidden(False) addArmor.set_hidden(False) subPhysDmg.set_hidden(False) addPhysDmg.set_hidden(False) subStunDmg.set_hidden(False) addStunDmg.set_hidden(False) armorNumber.set_text(str(armorAmount)) physDmgNum.set_text(str(phsyDmgAmount)) stunDmgNum.set_text(str(stunDmgAmount)) def numberDiceSlider_changed(value): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 numberOfDice = numberDiceSlider.get_value() numDice.set_text(str(numberOfDice)) pass numberDiceSlider.changed(numberDiceSlider_changed) def end_pressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) text = cardkb_0.keyString showtext.set_text(str(text)) pass end.pressed(end_pressed) def start_pressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) text = 'Press ESC to clear' showtext.set_text(str(text)) pass start.pressed(start_pressed) def hello_pressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) m5mqtt.publish(str('INSERT YOUR FEED HERE'), str(text), 1) text = '' pass hello.pressed(hello_pressed) def rollDice_pressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) glitches = 0 sixes = 0 successes = 0 if numberOfDice != 0: for i in (1 <= float(numberOfDice)) and upRange(1, float(numberOfDice), 1) or downRange(1, float(numberOfDice), 1): random2 = random.randint(1, 6) if random2 == 1: glitches = glitches + 1 if random2 > 4: successes = successes + 1 if random2 == 6: sixes = sixes + 1 numSuccess.set_text(str(successes)) numSix.set_text(str(sixes)) numGlitch.set_text(str(glitches)) numberOfDice = 0 numDice.set_text(str(numberOfDice)) numberDiceSlider.set_value(0) pass rollDice.pressed(rollDice_pressed) def stats_pressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) stats.set_hidden(True) dice.set_hidden(True) comms.set_hidden(True) statsScreen() pass stats.pressed(stats_pressed) def addArmor_pressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) armorAmount = (armorAmount if isinstance(armorAmount, Number) else 0) + 1 armorNumber.set_text(str(armorAmount)) pass addArmor.pressed(addArmor_pressed) def dice_pressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) stats.set_hidden(True) dice.set_hidden(True) comms.set_hidden(True) diceScreen() pass dice.pressed(dice_pressed) def subArmor_pressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) armorAmount = (armorAmount if isinstance(armorAmount, Number) else 0) + -1 armorNumber.set_text(str(armorAmount)) pass subArmor.pressed(subArmor_pressed) def comms_pressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) stats.set_hidden(True) dice.set_hidden(True) comms.set_hidden(True) commScreen() pass comms.pressed(comms_pressed) def subPhysDmg_pressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) phsyDmgAmount = (phsyDmgAmount if isinstance(phsyDmgAmount, Number) else 0) + -1 penalty = math.floor(phsyDmgAmount / 3) physDmgNum.set_text(str(phsyDmgAmount)) physPenalty.set_text(str(penalty)) pass subPhysDmg.pressed(subPhysDmg_pressed) def addPhysDmg_pressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) phsyDmgAmount = (phsyDmgAmount if isinstance(phsyDmgAmount, Number) else 0) + 1 penalty = math.floor(phsyDmgAmount / 3) physDmgNum.set_text(str(phsyDmgAmount)) physPenalty.set_text(str(penalty)) pass addPhysDmg.pressed(addPhysDmg_pressed) def subStunDmg_pressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) stunDmgAmount = (stunDmgAmount if isinstance(stunDmgAmount, Number) else 0) + -1 penaltyStun = math.floor(stunDmgAmount / 3) stunDmgNum.set_text(str(stunDmgAmount)) stunPenalty.set_text(str(penaltyStun)) pass subStunDmg.pressed(subStunDmg_pressed) def addStunDmg_pressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) stunDmgAmount = (stunDmgAmount if isinstance(stunDmgAmount, Number) else 0) + 1 penaltyStun = math.floor(stunDmgAmount / 3) stunDmgNum.set_text(str(stunDmgAmount)) stunPenalty.set_text(str(penaltyStun)) pass addStunDmg.pressed(addStunDmg_pressed) def fun_Momtoast_feeds_broker_(topic_data): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 brokerreply = topic_data reply.set_text(str(brokerreply)) pass def buttonB_wasPressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) armor.set_hidden(True) armorNumber.set_hidden(True) physDmg.set_hidden(True) physDmgNum.set_hidden(True) physPenalty.set_hidden(True) stunDmgNum.set_hidden(True) stunDmg.set_hidden(True) stunPenalty.set_hidden(True) numberDiceLabel.set_hidden(True) numDice.set_hidden(True) numSuccess.set_hidden(True) numSix.set_hidden(True) numGlitch.set_hidden(True) showtext.set_hidden(True) reply.set_hidden(True) subArmor.set_hidden(True) addArmor.set_hidden(True) subPhysDmg.set_hidden(True) addPhysDmg.set_hidden(True) subStunDmg.set_hidden(True) addStunDmg.set_hidden(True) rollDice.set_hidden(True) hello.set_hidden(True) start.set_hidden(True) end.set_hidden(True) numberDiceSlider.set_hidden(True) homeScreen() pass btnB.wasPressed(buttonB_wasPressed) def buttonA_wasPressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) penalty = 0 penaltyStun = 0 phsyDmgAmount = 0 stunDmgAmount = 0 armorAmount = 0 physDmgNum.set_text(str(phsyDmgAmount)) physPenalty.set_text(str(penalty)) stunDmgNum.set_text(str(stunDmgAmount)) stunPenalty.set_text(str(penaltyStun)) armorNumber.set_text(str(armorAmount)) pass btnA.wasPressed(buttonA_wasPressed) def buttonC_wasPressed(): global numberOfDice, brokerreply, text, glitches, armorAmount, phsyDmgAmount, stunDmgAmount, penalty, successes, sixes, penaltyStun, random2, diceList, i, PWM0 power.setVibrationEnable(True) wait(0.2) power.setVibrationEnable(False) successes = 0 sixes = 0 glitches = 0 numberOfDice = 0 numSuccess.set_text(str(successes)) numSix.set_text(str(sixes)) numGlitch.set_text(str(glitches)) numDice.set_text(str(numberOfDice)) numberDiceSlider.set_value(0) pass btnC.wasPressed(buttonC_wasPressed) screen.set_screen_bg_color(0x000000) armor.set_hidden(True) armorNumber.set_hidden(True) physDmg.set_hidden(True) physDmgNum.set_hidden(True) physPenalty.set_hidden(True) stunDmgNum.set_hidden(True) stunDmg.set_hidden(True) stunPenalty.set_hidden(True) numberDiceLabel.set_hidden(True) numDice.set_hidden(True) numSuccess.set_hidden(True) numSix.set_hidden(True) numGlitch.set_hidden(True) showtext.set_hidden(True) reply.set_hidden(True) subArmor.set_hidden(True) addArmor.set_hidden(True) subPhysDmg.set_hidden(True) addPhysDmg.set_hidden(True) subStunDmg.set_hidden(True) addStunDmg.set_hidden(True) rollDice.set_hidden(True) hello.set_hidden(True) start.set_hidden(True) end.set_hidden(True) armorAmount = 0 phsyDmgAmount = 0 penalty = 0 stunDmgAmount = 0 diceList = [] numberDiceSlider.set_hidden(True) homeScreen() m5mqtt = M5mqtt('YOUR API KEY', 'YOUR MQTT SERVER', 1883, 'YOUR USERNAME', 'YOUR API KEY', 300) m5mqtt.subscribe(str('YOUR SUBSCRIBE FEED'), fun_YOUR SUBSCRIBE FEED_) m5mqtt.start() PWM0 = machine.PWM(25, freq=100, duty=50, timer=0) while True: for i in range(5, 101, 5): PWM0.duty(i) wait_ms(50) for i in range(100, 4, -5): PWM0.duty(i) wait_ms(50)