]> mj.ucw.cz Git - home-hw.git/commitdiff
Waiting room: Microbit experiments
authorMartin Mares <mj@ucw.cz>
Sat, 19 Apr 2025 19:32:00 +0000 (21:32 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 19 Apr 2025 19:32:00 +0000 (21:32 +0200)
waiting/microbit/README [new file with mode: 0644]
waiting/microbit/Wukong.py [new file with mode: 0644]
waiting/microbit/main.py [new file with mode: 0644]

diff --git a/waiting/microbit/README b/waiting/microbit/README
new file mode 100644 (file)
index 0000000..07ba8e6
--- /dev/null
@@ -0,0 +1,18 @@
+pip install uflash ufs
+# Download latest release from https://github.com/microbit-foundation/micropython-microbit-v2
+
+(venv) mj@albireo:/tmp/upython$ pmount /dev/sdh
+(venv) mj@albireo:/tmp/upython$ cp micropython-microbit-v2.1.2.hex /media/sdh/
+# v1 není kompatibilní s deskami v2
+(venv) mj@albireo:/tmp/upython$ pumount /dev/sdh
+
+# REPL
+./kerm
+
+ufs put main.py
+
+# MicroPython doc:
+https://microbit-micropython.readthedocs.io/en/latest/
+
+# Wukong doc:
+https://www.elecfreaks.com/learn-en/microbitExtensionModule/wukong.html#software-programming-python-editor
diff --git a/waiting/microbit/Wukong.py b/waiting/microbit/Wukong.py
new file mode 100644 (file)
index 0000000..a2d7216
--- /dev/null
@@ -0,0 +1,90 @@
+from microbit import *
+
+WUKONG_ADDR = 0x10
+
+
+class WUKONG(object):
+    """基本描述
+
+    悟空多功能主控板
+
+    """
+
+    def __init__(self):
+        i2c.init()
+
+    def set_motors(self, motor, speed):
+        """基本描述
+
+        选择电机并且设置速度
+
+        Args:
+            motor (number): 选择第几个电机 1,2
+            speed (number): 设置电机速度 -100~100
+        """
+        if speed > 100 or speed < -100:
+            raise ValueError('speed error,-100~100')
+        if motor > 2 or motor < 1:
+            raise ValueError('select motor error,1,2,3,4')
+        if speed < 0:
+            i2c.write(WUKONG_ADDR, bytearray([motor, 0x02, speed * -1, 0]))
+        else:
+            i2c.write(WUKONG_ADDR, bytearray([motor, 0x01, speed, 0]))
+
+    def set_servo(self, servo, angle):
+        """基本描述
+
+        选择伺服电机并且设置角度/速度
+
+        Args:
+            servo (number): 选择第几个舵机(伺服电机)0,1,2,3,4,5,6,7
+            angle (number): 设置舵机角度 0~180
+        """
+        if servo > 7 or servo < 0:
+            raise ValueError('select servo error')
+        if angle > 180 or angle < 0:
+            raise ValueError('angle error,0~180')
+        if servo == 7:
+            i2c.write(WUKONG_ADDR, bytearray([0x10, angle, 0, 0]))
+        else:
+            i2c.write(WUKONG_ADDR, bytearray([servo + 3, angle, 0, 0]))
+
+    def set_light(self, light):
+        """基本描述
+
+        设置氛围灯亮度
+
+        Args:
+            light (number): 氛围灯亮度
+        """
+        i2c.write(WUKONG_ADDR, bytearray([0x12, light, 0, 0]))
+        sleep(100)
+        i2c.write(WUKONG_ADDR, bytearray([0x11, 160, 0, 0]))
+
+    def set_light_breath(self, br: bool):
+        """基本描述
+
+        设置氛围灯呼吸模式
+
+        Args:
+            br (bool): 氛围灯呼吸模式开关
+        """
+        if br:
+            i2c.write(WUKONG_ADDR, bytearray([0x11, 0, 0, 0]))
+            sleep(100)
+            i2c.write(WUKONG_ADDR, bytearray([0x12, 150, 0, 0]))
+        else:
+            i2c.write(WUKONG_ADDR, bytearray([0x12, 0, 0, 0]))
+            sleep(100)
+            i2c.write(WUKONG_ADDR, bytearray([0x11, 160, 0, 0]))
+
+
+if __name__ == '__main__':
+    wk = WUKONG()
+
+    wk.set_motors(1, 100)
+    wk.set_servo(1, 90)
+    if button_a.is_pressed():
+        wk.set_light_breath(False)
+    else:
+        wk.set_light_breath(True)
diff --git a/waiting/microbit/main.py b/waiting/microbit/main.py
new file mode 100644 (file)
index 0000000..4c3a942
--- /dev/null
@@ -0,0 +1,22 @@
+from microbit import *
+import neopixel
+import Wukong
+
+display.show(Image.HAPPY)
+
+np = neopixel.NeoPixel(pin16, 4)
+np[0] = (0, 15, 0)
+np.show()
+
+wk = Wukong.WUKONG()
+wk.set_light(10)
+wk.set_motors(1, 0)
+
+while True:
+    if button_a.is_pressed():
+        display.show(Image.SAD)
+        wk.set_servo(0, 90 // 2)
+    if button_b.is_pressed():
+        display.show(Image.HAPPY)
+        wk.set_servo(0, 0)
+    sleep(100)