MiniJoyC Hat

以下产品受支持:

MiniJoyCHat

MicroPython 应用示例

  1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
  2#
  3# SPDX-License-Identifier: MIT
  4
  5import os, sys, io
  6import M5
  7from M5 import *
  8from hardware import *
  9from hat import MiniJoyHat
 10
 11
 12circle0 = None
 13circle1 = None
 14label0 = None
 15label1 = None
 16label2 = None
 17label3 = None
 18i2c0 = None
 19hat_minijoyc_0 = None
 20
 21
 22x = None
 23value = None
 24in_min = None
 25in_max = None
 26out_min = None
 27out_range = None
 28y = None
 29last_x = None
 30last_y = None
 31
 32
 33# Describe this function...
 34def map_to_range(value, in_min, in_max, out_min, out_range):
 35    global \
 36        x, \
 37        y, \
 38        last_x, \
 39        last_y, \
 40        circle0, \
 41        circle1, \
 42        label0, \
 43        label1, \
 44        label2, \
 45        label3, \
 46        i2c0, \
 47        hat_minijoyc_0
 48    return int((value - in_min) * out_range / (in_max - in_min) + out_min)
 49
 50
 51def setup():
 52    global \
 53        circle0, \
 54        circle1, \
 55        label0, \
 56        label1, \
 57        label2, \
 58        label3, \
 59        i2c0, \
 60        hat_minijoyc_0, \
 61        x, \
 62        out_range, \
 63        out_min, \
 64        y, \
 65        value, \
 66        in_min, \
 67        in_max, \
 68        last_x, \
 69        last_y
 70
 71    M5.begin()
 72    circle0 = Widgets.Circle(67, 120, 50, 0xFFFFFF, 0x000000)
 73    circle1 = Widgets.Circle(67, 120, 4, 0xFFFFFF, 0xFFFFFF)
 74    label0 = Widgets.Label("X:", 6, 185, 1.0, 0x74F707, 0x222222, Widgets.FONTS.DejaVu18)
 75    label1 = Widgets.Label("Y:", 6, 212, 1.0, 0x74F707, 0x222222, Widgets.FONTS.DejaVu18)
 76    label2 = Widgets.Label("0", 25, 185, 1.0, 0x74F707, 0x222222, Widgets.FONTS.DejaVu18)
 77    label3 = Widgets.Label("0", 21, 212, 1.0, 0x74F707, 0x222222, Widgets.FONTS.DejaVu18)
 78
 79    i2c0 = I2C(0, scl=Pin(26), sda=Pin(0), freq=100000)
 80    hat_minijoyc_0 = MiniJoyHat(i2c0, 0x54)
 81    hat_minijoyc_0.swap_y(True)
 82    last_x = 67
 83    last_y = 120
 84
 85
 86def loop():
 87    global \
 88        circle0, \
 89        circle1, \
 90        label0, \
 91        label1, \
 92        label2, \
 93        label3, \
 94        i2c0, \
 95        hat_minijoyc_0, \
 96        x, \
 97        out_range, \
 98        out_min, \
 99        y, \
100        value, \
101        in_min, \
102        in_max, \
103        last_x, \
104        last_y
105    M5.update()
106    x = last_x + map_to_range(hat_minijoyc_0.get_x(), -128, 127, -43, 86)
107    y = last_y + map_to_range(hat_minijoyc_0.get_y(), -128, 127, -43, 86)
108    circle1.setCursor(x=x, y=y)
109    if hat_minijoyc_0.get_button_status():
110        circle1.setColor(color=0xFF0000, fill_c=0xFF0000)
111    else:
112        circle1.setColor(color=0xFFFFFF, fill_c=0x6600CC)
113    label2.setText(str(hat_minijoyc_0.get_x()))
114    label3.setText(str(hat_minijoyc_0.get_y()))
115
116
117if __name__ == "__main__":
118    try:
119        setup()
120        while True:
121            loop()
122    except (Exception, KeyboardInterrupt) as e:
123        try:
124            from utility import print_error_msg
125
126            print_error_msg(e)
127        except ImportError:
128            print("please update to latest firmware")

UiFlow2 应用示例

example.png

stickc_plus2_mini_joy_example.m5f2

class MiniJoyHat

Constructors

class MiniJoyHat(i2c, address: int | list | tuple = 0x38)

创建 MiniJoyHat 类的新实例。

参数:
  • i2c – I2C 总线

  • address – I2C 地址

UIFLOW2:

init.png

Methods

MiniJoyHat.get_x_raw() int

获取原始 x 轴数值。

返回:

x 轴值

UIFLOW2:

get_x_raw.png

MiniJoyHat.get_y_raw() int

获取 y 轴的原始值。

返回:

y 轴数值

UIFLOW2:

get_y_raw.png

MiniJoyHat.get_x() int

获取 x 轴的值。

返回:

x 轴值

UIFLOW2:

get_x.png

MiniJoyHat.get_y() int

获取 y 轴值。

返回:

y 轴数值

UIFLOW2:

get_y.png

MiniJoyHat.swap_x(swap: bool = True) None

交换 x 轴方向

参数:

swap – True or False

UIFLOW2:

swap_x.png

MiniJoyHat.swap_y(swap: bool = True) None

交换 y 轴方向

参数:

swap – True or False

UIFLOW2:

swap_y.png

MiniJoyHat.get_button_status() bool

获取按键状态。

返回:

True or False

UIFLOW2:

get_button_status.png

MiniJoyHat.get_firmware_version() str

获取固件版本。

返回:

固件版本

UIFLOW2:

get_firmware_version.png

MiniJoyHat.set_i2c_address(address: int) None

设置 I2C 地址。

参数:

address – 0x01 ~ 0x7F

UIFLOW2:

set_i2c_address.png