JoyC Hat

支持以下产品:

JoyCHat

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 JoyCHat
 10
 11
 12circle0 = None
 13circle1 = None
 14label0 = None
 15label1 = None
 16label2 = None
 17label3 = None
 18i2c0 = None
 19hat_joyc_0 = None
 20
 21
 22value = None
 23in_min = None
 24in_max = None
 25out_min = None
 26out_range = None
 27x = 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 x, y, last_x, last_y, circle0, circle1, label0, label1, label2, label3, i2c0, hat_joyc_0
 36    return int((value - in_min) * out_range / (in_max - in_min) + out_min)
 37
 38
 39def setup():
 40    global \
 41        circle0, \
 42        circle1, \
 43        label0, \
 44        label1, \
 45        label2, \
 46        label3, \
 47        i2c0, \
 48        hat_joyc_0, \
 49        x, \
 50        out_range, \
 51        out_min, \
 52        y, \
 53        value, \
 54        in_min, \
 55        in_max, \
 56        last_x, \
 57        last_y
 58
 59    M5.begin()
 60    circle0 = Widgets.Circle(67, 120, 50, 0xFFFFFF, 0x000000)
 61    circle1 = Widgets.Circle(67, 120, 4, 0xFFFFFF, 0xFFFFFF)
 62    label0 = Widgets.Label("X:", 6, 185, 1.0, 0x74F707, 0x222222, Widgets.FONTS.DejaVu18)
 63    label1 = Widgets.Label("Y:", 6, 212, 1.0, 0x74F707, 0x222222, Widgets.FONTS.DejaVu18)
 64    label2 = Widgets.Label("0", 25, 185, 1.0, 0x74F707, 0x222222, Widgets.FONTS.DejaVu18)
 65    label3 = Widgets.Label("0", 21, 212, 1.0, 0x74F707, 0x222222, Widgets.FONTS.DejaVu18)
 66
 67    i2c0 = I2C(0, scl=Pin(26), sda=Pin(0), freq=100000)
 68    hat_joyc_0 = JoyCHat(i2c0, 0x38)
 69    hat_joyc_0.swap_x(True)
 70    hat_joyc_0.swap_y(True)
 71    last_x = 67
 72    last_y = 120
 73
 74
 75def loop():
 76    global \
 77        circle0, \
 78        circle1, \
 79        label0, \
 80        label1, \
 81        label2, \
 82        label3, \
 83        i2c0, \
 84        hat_joyc_0, \
 85        x, \
 86        out_range, \
 87        out_min, \
 88        y, \
 89        value, \
 90        in_min, \
 91        in_max, \
 92        last_x, \
 93        last_y
 94    M5.update()
 95    x = last_x + map_to_range(hat_joyc_0.get_x(0), -128, 127, -43, 86)
 96    y = last_y + map_to_range(hat_joyc_0.get_y(0), -128, 127, -43, 86)
 97    circle1.setCursor(x=x, y=y)
 98    if hat_joyc_0.get_button_status(0):
 99        circle1.setColor(color=0xFF0000, fill_c=0xFF0000)
100    else:
101        circle1.setColor(color=0xFFFFFF, fill_c=0x6600CC)
102    label2.setText(str(hat_joyc_0.get_x(0)))
103    label3.setText(str(hat_joyc_0.get_y(0)))
104
105
106if __name__ == "__main__":
107    try:
108        setup()
109        while True:
110            loop()
111    except (Exception, KeyboardInterrupt) as e:
112        try:
113            from utility import print_error_msg
114
115            print_error_msg(e)
116        except ImportError:
117            print("please update to latest firmware")

UiFlow2 应用示例

example.png

stickc_plus2_joyc_example.m5f2

class JoyCHat

Constructors

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

创建 JoyCHat 类的新实例。

参数:
  • i2c – I2C 总线

  • address – I2C 地址

UIFLOW2:

init.png

Methods

JoyCHat.get_x_raw(channel: int = 0) int

获取 x 轴原始值。

参数:

channel – 0 或 1

返回:

x 轴值

UIFLOW2:

get_x_raw.png

JoyCHat.get_y_raw(channel: int = 0) int

获取原始 y 轴值。

参数:

channel – 0 或 1

返回:

y 轴数值

UIFLOW2:

get_y_raw.png

JoyCHat.get_x(channel: int = 0) int

获取 x 轴值。

参数:

channel – 0 或 1

返回:

x 轴值

UIFLOW2:

get_x.png

JoyCHat.get_y(channel: int = 0) int

获取 y 轴值。

参数:

channel – 0 或 1

返回:

y 轴数值

UIFLOW2:

get_y.png

JoyCHat.swap_x(swap: bool = True) None

交换 x 轴方向

参数:

swap – True or False

UIFLOW2:

swap_x.png

JoyCHat.swap_y(swap: bool = True) None

交换 y 轴方向

参数:

swap – True or False

UIFLOW2:

swap_y.png

JoyCHat.get_button_status(channel: int = 0) bool

获取按键状态。

参数:

channel – 0 或 1

返回:

True or False

UIFLOW2:

get_button_status.png

JoyCHat.fill_color() None

用一种颜色填充屏幕。

UIFLOW2:

fill_color.png