Compare commits
2
Commits
150708564c
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d2769c0aa6 | ||
|
|
f3982221a1 |
No files matched your search
@@ -0,0 +1,55 @@
|
|||||||
|
class Base64:
|
||||||
|
def __init__(self):
|
||||||
|
# Base64 alphabet
|
||||||
|
self.alphabet = (
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||||
|
)
|
||||||
|
|
||||||
|
def encode(self, data: str) -> str:
|
||||||
|
# Convert string to bytes and get binary representation
|
||||||
|
binary = "".join([format(b, "08b") for b in data.encode()])
|
||||||
|
|
||||||
|
# Pad with zeros if necessary
|
||||||
|
padding = len(binary) % 6
|
||||||
|
if padding:
|
||||||
|
binary += "0" * (6 - padding)
|
||||||
|
|
||||||
|
# Split into 6-bit chunks and convert to decimal
|
||||||
|
chunks = [binary[i : i + 6] for i in range(0, len(binary), 6)]
|
||||||
|
result = "".join(self.alphabet[int(chunk, 2)] for chunk in chunks)
|
||||||
|
|
||||||
|
# Add padding characters
|
||||||
|
padding = len(data) % 3
|
||||||
|
if padding:
|
||||||
|
result += "=" * (3 - padding)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def decode(self, data: str) -> str:
|
||||||
|
# Remove padding characters
|
||||||
|
data = data.rstrip("=")
|
||||||
|
|
||||||
|
# Convert to binary
|
||||||
|
binary = "".join([format(self.alphabet.index(c), "06b") for c in data])
|
||||||
|
|
||||||
|
# Remove padding zeros
|
||||||
|
padding = len(binary) % 8
|
||||||
|
if padding:
|
||||||
|
binary = binary[:-padding]
|
||||||
|
|
||||||
|
# Convert binary to bytes and then to string
|
||||||
|
bytes_data = [int(binary[i : i + 8], 2) for i in range(0, len(binary), 8)]
|
||||||
|
return bytes(bytes_data).decode()
|
||||||
|
|
||||||
|
|
||||||
|
# Example usage
|
||||||
|
def example():
|
||||||
|
b64 = Base64()
|
||||||
|
original = "Hello, World!"
|
||||||
|
encoded = b64.encode(original)
|
||||||
|
print(f"Original: {original}")
|
||||||
|
print(f"Encoded: {encoded}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
example()
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import secrets
|
||||||
|
|
||||||
|
|
||||||
|
class MicroUUID:
|
||||||
|
def __init__(self):
|
||||||
|
self.uuid = self.generate_uuid()
|
||||||
|
|
||||||
|
def generate_uuid(self):
|
||||||
|
# Generate a random UUID (version 4)
|
||||||
|
random_bytes = secrets.token_bytes(16)
|
||||||
|
# Set the version to 4 (randomly generated UUID)
|
||||||
|
random_bytes = bytearray(random_bytes)
|
||||||
|
random_bytes[6] = (random_bytes[6] & 0x0F) | 0x40
|
||||||
|
# Set the variant to RFC 4122
|
||||||
|
random_bytes[8] = (random_bytes[8] & 0x3F) | 0x80
|
||||||
|
uid = "".join(f"{byte:02x}" for byte in random_bytes)
|
||||||
|
return f"{uid[0:8]}-{uid[8:12]}-{uid[12:16]}-{uid[16:20]}-{uid[20:32]}"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.uuid
|
||||||
|
|
||||||
|
|
||||||
|
# Example usage:
|
||||||
|
def example():
|
||||||
|
uuid_instance = MicroUUID()
|
||||||
|
print(f"Generated UUID: {uuid_instance}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
example()
|
||||||
@@ -4,7 +4,11 @@ from M5 import *
|
|||||||
import requests2
|
import requests2
|
||||||
import network
|
import network
|
||||||
import time
|
import time
|
||||||
|
from micro_base64 import Base64
|
||||||
|
from micro_uuid import MicroUUID
|
||||||
|
|
||||||
|
base64 = Base64()
|
||||||
|
uuid4 = MicroUUID
|
||||||
|
|
||||||
|
|
||||||
image1 = None
|
image1 = None
|
||||||
@@ -13,62 +17,76 @@ http_req = None
|
|||||||
wlan = None
|
wlan = None
|
||||||
|
|
||||||
|
|
||||||
# Describe this function...
|
|
||||||
def update():
|
def update():
|
||||||
global image1, status, http_req, wlan
|
cmd = base64.encode("RUNUPDATE")
|
||||||
http_req = requests2.post('https://traps.moe:7432', json={'COMMAND':'changeme'}, headers={'Content-Type': 'application/json','idempotency-key':'changeme'})
|
global image1, status, http_req, wlan
|
||||||
|
http_req = requests2.post(
|
||||||
|
"https://traps.moe:7432",
|
||||||
|
json={"COMMAND": cmd},
|
||||||
|
headers={
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"idempotency-key": uuid4(),
|
||||||
|
"User-Agent": "Kopling/0.1",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def setup():
|
def setup():
|
||||||
global image1, status, http_req, wlan
|
global image1, status, http_req, wlan
|
||||||
|
|
||||||
M5.begin()
|
M5.begin()
|
||||||
Widgets.setRotation(0)
|
Widgets.setRotation(0)
|
||||||
Widgets.fillScreen(0x000000)
|
Widgets.fillScreen(0x000000)
|
||||||
image1 = Widgets.Image("/flash/res/img/hourglass.png", 1, 26, scale_x=0.5, scale_y=0.5)
|
image1 = Widgets.Image(
|
||||||
status = Widgets.Label("READY", 25, 107, 1.0, 0xffffff, 0x000000, Widgets.FONTS.DejaVu24)
|
"/flash/res/img/hourglass.png", 1, 26, scale_x=0.5, scale_y=0.5
|
||||||
|
)
|
||||||
|
status = Widgets.Label(
|
||||||
|
"READY", 25, 107, 1.0, 0xFFFFFF, 0x000000, Widgets.FONTS.DejaVu24
|
||||||
|
)
|
||||||
|
|
||||||
Speaker.setVolumePercentage(0.5)
|
Speaker.setVolumePercentage(0.5)
|
||||||
image1.setVisible(False)
|
image1.setVisible(False)
|
||||||
Speaker.begin()
|
Speaker.begin()
|
||||||
wlan = network.WLAN(network.STA_IF)
|
wlan = network.WLAN(network.STA_IF)
|
||||||
wlan.active(True)
|
wlan.active(True)
|
||||||
wlan.connect('Play-Well Staff', 'Jigabits2024')
|
wlan.connect("Play-Well Staff", "Jigabits2024")
|
||||||
Widgets.fillScreen(0x33ffff)
|
Widgets.fillScreen(0x33FFFF)
|
||||||
status.setColor(0x000000, 0x33ffff)
|
status.setColor(0x000000, 0x33FFFF)
|
||||||
status.setText(str('WIFI OK'))
|
status.setText(str("WIFI OK"))
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
time.timezone('GMT-8')
|
time.timezone("GMT-8")
|
||||||
Speaker.tone(6000, 100)
|
Speaker.tone(6000, 100)
|
||||||
Speaker.tone(5000, 100)
|
Speaker.tone(5000, 100)
|
||||||
Widgets.fillScreen(0x33ff33)
|
Widgets.fillScreen(0x33FF33)
|
||||||
status.setColor(0x000000, 0x33ff33)
|
status.setColor(0x000000, 0x33FF33)
|
||||||
status.setText(str('READY'))
|
status.setText(str("READY"))
|
||||||
|
|
||||||
|
|
||||||
def loop():
|
def loop():
|
||||||
global image1, status, http_req, wlan
|
global image1, status, http_req, wlan
|
||||||
M5.update()
|
M5.update()
|
||||||
if BtnA.isPressed():
|
if BtnA.isPressed():
|
||||||
Speaker.tone(8000, 50)
|
Speaker.tone(8000, 50)
|
||||||
Widgets.fillScreen(0xff0000)
|
update()
|
||||||
image1.setVisible(True)
|
Widgets.fillScreen(0xFF0000)
|
||||||
time.sleep(15)
|
image1.setVisible(True)
|
||||||
Speaker.tone(2000, 50)
|
time.sleep(15)
|
||||||
Speaker.tone(3000, 100)
|
Speaker.tone(2000, 50)
|
||||||
image1.setVisible(False)
|
Speaker.tone(3000, 100)
|
||||||
Widgets.fillScreen(0x33ff33)
|
image1.setVisible(False)
|
||||||
status.setVisible(True)
|
Widgets.fillScreen(0x33FF33)
|
||||||
|
status.setVisible(True)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
try:
|
|
||||||
setup()
|
|
||||||
while True:
|
|
||||||
loop()
|
|
||||||
except (Exception, KeyboardInterrupt) as e:
|
|
||||||
try:
|
try:
|
||||||
from utility import print_error_msg
|
setup()
|
||||||
print_error_msg(e)
|
while True:
|
||||||
except ImportError:
|
loop()
|
||||||
print("please update to latest firmware")
|
except (Exception, KeyboardInterrupt) as e:
|
||||||
|
try:
|
||||||
|
from utility import print_error_msg
|
||||||
|
|
||||||
|
print_error_msg(e)
|
||||||
|
except ImportError:
|
||||||
|
print("please update to latest firmware")
|
||||||
Reference in new issue
Block a user