JoyC Hat
支持以下产品:
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 应用示例
stickc_plus2_joyc_example.m5f2










