diff --git a/boards/default_src/micropython/origin/build/lib/ble_hid.py b/boards/default_src/micropython/origin/build/lib/ble_hid.py new file mode 100644 index 00000000..9011df86 --- /dev/null +++ b/boards/default_src/micropython/origin/build/lib/ble_hid.py @@ -0,0 +1,235 @@ +""" +Bluetooth-HID + +Micropython library for the Bluetooth-HID(Compliant with equipment) +======================================================= +@dahanzimin From the Mixly Team +""" +import bluetooth +import struct, time +from micropython import const +from ubinascii import hexlify +from ble_advertising import advertising_payload +from bluetooth import UUID, FLAG_READ, FLAG_WRITE ,FLAG_NOTIFY, FLAG_WRITE_NO_RESPONSE + +_IRQ_CENTRAL_CONNECT = const(1) +_IRQ_CENTRAL_DISCONNECT = const(2) +_IRQ_GATTS_WRITE = const(3) +_IRQ_MTU_EXCHANGED = const(21) +_IRQ_CONNECTION_UPDATE = const(27) +_IRQ_GET_SECRET = const(29) +_IRQ_SET_SECRET = const(30) +_IRQ_PASSKEY_ACTION = const(31) +_PASSKEY_ACTION_INPUT = const(2) +_PASSKEY_ACTION_DISP = const(3) +_PASSKEY_ACTION_NUMCMP = const(4) + +#HID 鼠标、键盘、手柄设备报告描述符 +_HID_INPUT_REPORT = const(b'\x05\x01\t\x02\xa1\x01\x85\x01\t\x01\xa1\x00\x05\t\x19\x01)\x03\x15\x00%\x01\x95\x03u\x01\x81\x02\x95\x01u\x05\x81\x03\x05\x01\t0\t1\t8\x15\x81%\x7fu\x08\x95\x03\x81\x06\xc0\xc0\x05\x01\t\x06\xa1\x01\x85\x02u\x01\x95\x08\x05\x07\x19\xe0)\xe7\x15\x00%\x01\x81\x02\x95\x01u\x08\x81\x01\x95\x05u\x01\x05\x08\x19\x01)\x05\x91\x02\x95\x01u\x03\x91\x01\x95\x06u\x08\x15\x00%e\x05\x07\x19\x00)e\x81\x00\xc0\x05\x01\t\x04\xa1\x01\x85\x03\xa1\x00\t0\t1\x15\x81%\x7fu\x08\x95\x02\x81\x02\x05\t)\x08\x19\x01\x95\x08u\x01%\x01\x15\x00\x81\x02\xc0\xc0') +_KEYCODE = const(b'\x00\x00\x00\x00\x00\x00\x00\x00*+(\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00)\x00\x00\x00\x00,\x9e\xb4\xa0\xa1\xa2\xa44\xa6\xa7\xa5\xae6-78\'\x1e\x1f !"#$%&\xb33\xb6.\xb7\xb8\x9f\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d/10\xa3\xad5\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\xaf\xb1\xb0\xb5L') + +_DIS = (UUID(0x180A), ( (UUID(0x2A24), FLAG_READ), + (UUID(0x2A25), FLAG_READ), + (UUID(0x2A26), FLAG_READ), + (UUID(0x2A27), FLAG_READ), + (UUID(0x2A28), FLAG_READ), + (UUID(0x2A29), FLAG_READ), + (UUID(0x2A50), FLAG_READ), ), ) + +_BAS = (UUID(0x180F), ( (UUID(0x2A19), FLAG_READ | FLAG_NOTIFY, ((UUID(0x2902), 0x03), (UUID(0x2904), 0x01),)), ), ) + +_HIDS = (UUID(0x1812), ((UUID(0x2A4A), FLAG_READ), + (UUID(0x2A4B), FLAG_READ), + (UUID(0x2A4C), FLAG_READ | FLAG_WRITE | FLAG_WRITE_NO_RESPONSE), + (UUID(0x2A4D), FLAG_READ | FLAG_NOTIFY, ((UUID(0x2902), 0x03), (UUID(0x2908), 0x03),)), #鼠标 + (UUID(0x2A4D), FLAG_READ | FLAG_NOTIFY, ((UUID(0x2902), 0x03), (UUID(0x2908), 0x03),)), #键盘发送 + (UUID(0x2A4D), FLAG_READ | FLAG_WRITE, ((UUID(0x2902), 0x03), (UUID(0x2908), 0x03),)), #键盘状态 + (UUID(0x2A4D), FLAG_READ | FLAG_NOTIFY, ((UUID(0x2902), 0x03), (UUID(0x2908), 0x03),)), #游戏手柄 + (UUID(0x2A4E), FLAG_READ | FLAG_WRITE | FLAG_WRITE_NO_RESPONSE), ), ) + +class HID: + def __init__(self, name=None, passkey=1234, battery_level=100): + if (name is '') or (name is None): + name = "Mixgo_" + self.mac[-6:].upper() + print("HID Name:", name) + + self._ble = bluetooth.BLE() + self._ble.active(True) + self._ble.irq(self._irq) + self._ble.config(gap_name=name) + self._ble.config(mtu=23) + self.device_state = False + self.conn_handle = None + self.passkey = passkey + self.battery_level = battery_level + self.report = b'\x00' + try: + import ble_hid_key + self.keys = ble_hid_key.keys + except: + self.keys = {} + + handles = self._ble.gatts_register_services((_DIS, _BAS, _HIDS)) + self._service_characteristics(handles) + self._payload = advertising_payload(name=name, services=[UUID(0x1812)], appearance=const(960)) + self.advertise() + + def _irq(self, event, data): + # Interrupt request callback function + if event == _IRQ_CENTRAL_CONNECT: + self.conn_handle, _, _ = data + print("HID connected:", self.conn_handle) + self.device_state = True + elif event == _IRQ_CENTRAL_DISCONNECT: + self.conn_handle = None + conn_handle, addr_type, addr = data + print("HID disconnected:", conn_handle) + self.advertise() + self.device_state = False + elif event == _IRQ_MTU_EXCHANGED: + conn_handle, mtu = data + self._ble.config(mtu=mtu) + print("MTU exchanged:", mtu) + elif event == _IRQ_CONNECTION_UPDATE: + self.conn_handle, _, _, _, _ = data + print("Connection update") + elif event == _IRQ_PASSKEY_ACTION: + conn_handle, action, passkey = data + print("Passkey action", conn_handle, action, passkey) + if action == _PASSKEY_ACTION_NUMCMP: + self._ble.gap_passkey(conn_handle, action, False) + elif action == _PASSKEY_ACTION_DISP: + print("Displaying passkey") + self._ble.gap_passkey(conn_handle, action, self.passkey) + elif action == _PASSKEY_ACTION_INPUT: + print("Prompting for passkey") + self._ble.gap_passkey(conn_handle, action, None) + else: + print("Unknown action") + elif event == _IRQ_GATTS_WRITE: + conn_handle, attr_handle = data + self.report = self._ble.gatts_read(attr_handle) + elif event == _IRQ_SET_SECRET: + sec_type, key, value = data + key = sec_type, bytes(key) + value = bytes(value) if value else None + #print("Set secret: ", key, value) + if value is None: + if key in self.keys: + del self.keys[key] + self._key_secrets(self.keys) + return True + else: + return False + else: + self.keys[key] = value + self._key_secrets(self.keys) + return True + elif event == _IRQ_GET_SECRET: + sec_type, index, key = data + #print("Get secret: ", sec_type, index, bytes(key) if key else None) + if key is None: + i = 0 + for (t, _key), value in self.keys.items(): + if t == sec_type: + if i == index: + return value + i += 1 + return None + else: + key = sec_type, bytes(key) + return self.keys.get(key, None) + # else: + # print("Unhandled IRQ event: ", event, data) + + def _key_secrets(self, keys={}): + """Save pairing key""" + with open("ble_hid_key.py", "w+") as s_f: + s_f.write("keys=" + str(keys) + "\n") + + def _service_characteristics(self, handles): + """Write BAS&service characteristics.""" + (h_mod, h_ser, h_fwr, h_hwr, h_swr, h_man, h_pnp) = handles[0] + (self.h_bat, h_ccc, h_bfmt) = handles[1] + (h_info, h_hid, _, self.m_rep, _, h_d1, self.k_rep, _, h_d2, _, _, h_d3, self.j_rep, _, h_d4, h_proto) = handles[2] + # Write DIS characteristics. + self._ble.gatts_write(h_mod, b'1') + self._ble.gatts_write(h_ser, b'1') + self._ble.gatts_write(h_fwr, b'1') + self._ble.gatts_write(h_hwr, b'1') + self._ble.gatts_write(h_swr, b'1') + self._ble.gatts_write(h_man, b'Homebrew') + self._ble.gatts_write(h_pnp, b'\x01a\xfe\x01\x00#\x01') + # Write BAS characteristics. + self._ble.gatts_write(self.h_bat, struct.pack(" 5: break + _keys[i] = general[i] + else: + _keys[0] = general + self._ble.gatts_notify(self.conn_handle, self.k_rep, bytes([special & 0xFF, 0]) + _keys) + if release: + time.sleep_ms(10) + self._ble.gatts_notify(self.conn_handle, self.k_rep, b'\x00\x00\x00\x00\x00\x00\x00\x00') + + def keyboard_str(self, string, delay=0): + '''键盘发送ASCLL码''' + for char in str(string): + char = max(min(ord(char), 127), 0) + self.keyboard_notify( 0x02 if _KEYCODE[char] >> 7 else 0x00, _KEYCODE[char] & 0x7F) + time.sleep_ms(20 + delay) + + def mouse_notify(self, keys=0, move=(0, 0), wheel=0, release=True): + '''鼠标 按键*3 + 位移 + 滚轮''' + if self.is_connected(): + self._ble.gatts_notify(self.conn_handle, self.m_rep, bytes([keys & 0x0F, move[0] & 0xFF, move[1] & 0xFF, wheel & 0xFF])) + if release: + time.sleep_ms(10) + self._ble.gatts_notify(self.conn_handle, self.m_rep, b'\x00\x00\x00\x00') + + def Joystick_notify(self, keys=0, axes=(0, 0), release=False): + '''手柄 按键*8 + 摇杆''' + if self.is_connected(): + self._ble.gatts_notify(self.conn_handle, self.j_rep, bytes([axes[0] & 0xFF, axes[1] & 0xFF, keys & 0xFF])) + if release: + time.sleep_ms(10) + self._ble.gatts_notify(self.conn_handle, self.j_rep, b'\x00\x00\x00') + @property + def mac(self): + '''Get mac address''' + return hexlify(self._ble.config('mac')[1]).decode() diff --git a/boards/default_src/micropython/origin/build/lib/hx720.py b/boards/default_src/micropython/origin/build/lib/hx720.py new file mode 100644 index 00000000..43c6bc45 --- /dev/null +++ b/boards/default_src/micropython/origin/build/lib/hx720.py @@ -0,0 +1,67 @@ +""" +HX720/HX711 + +Micropython library for the HX720/HX711(Load Cell) +======================================================= +@dahanzimin From the Mixly Team +""" +import time +from machine import Pin +from micropython import const + +DATA_BITS = const(24) +READY_TIMEOUT_SEC = const(500) + +class HX720: + def __init__(self, sck_pin, dat_pin, scale=500, pulse_obj=1): + self._sck = Pin(sck_pin, Pin.OUT, value=0) + self._dat = Pin(dat_pin, Pin.IN, Pin.PULL_UP) + self._obj = min(max(pulse_obj, 1), 3) + self.scale = scale + self.tare() + + def _wait(self): + """超时响应报错""" + star = time.ticks_ms() + while not self.is_ready(): + if time.ticks_diff(time.ticks_ms(), star) > READY_TIMEOUT_SEC: + raise AttributeError("Cannot find a HX711/HX720") + + def is_ready(self): + """检查是否有数据可以读取""" + return self._dat.value() == 0 + + def set_scale(self, scale): + """设置比例因子""" + self.scale = scale + + def read_raw(self): + """读取传感器的原始数据""" + if not self.is_ready(): + self._wait() + raw_data = 0 + for _ in range(DATA_BITS): + self._sck.value(1) + self._sck.value(0) + raw_data = raw_data << 1 | self._dat.value() + + # 根据脉冲功能设置多读几次 + for _ in range(self._obj): + self._sck.value(1) + self._sck.value(0) + + return raw_data | 0xFF000000 if raw_data & 0x800000 else raw_data + + def tare(self, times=10): + """清零传感器""" + total = 0 + for _ in range(times): + total += self.read_raw() + self.offset = total / times + + def read_weight(self, times=5): + """读取重量数据,返回去掉偏移量的平均值""" + total = 0 + for _ in range(times): + total += self.read_raw() + return round((total / times - self.offset) / self.scale, 2) diff --git a/boards/default_src/micropython/origin/build/lib/mixgo_ai.py b/boards/default_src/micropython/origin/build/lib/mixgo_ai.py index a252b868..9925315f 100644 --- a/boards/default_src/micropython/origin/build/lib/mixgo_ai.py +++ b/boards/default_src/micropython/origin/build/lib/mixgo_ai.py @@ -35,7 +35,7 @@ class INFO: class AI: def __init__(self, uart, quick=False): self._uart=uart - self._uart.init(baudrate=115200) + self._uart.init(baudrate=115200, timeout=500) self._info=[] self._sdata=[] self._removal=quick diff --git a/boards/default_src/micropython/origin/build/lib/uframebuf.py b/boards/default_src/micropython/origin/build/lib/uframebuf.py index c7961740..33bb91fb 100644 --- a/boards/default_src/micropython/origin/build/lib/uframebuf.py +++ b/boards/default_src/micropython/origin/build/lib/uframebuf.py @@ -346,7 +346,7 @@ class FrameBuffer_Uincode(FrameBuffer_Base): size = max(round(size), 1) x =(self.width - width * size) // 2 if x is None else x y =(self.height - height * size) // 2 if y is None else y - self.fill_rect(x, y, width * size, height * size, 0) + if self.auto_show: self.fill_rect(x, y, width * size, height * size, 0) self.bitmap((buffer_info,(width, height)), x, y, size, color) if self.auto_show: self.show() @@ -371,33 +371,36 @@ class FrameBuffer_Uincode(FrameBuffer_Base): font_len = font_len + buffer[1][0] * size + space return font_len, font_buffer - def shows(self, data, space=0, center=True, x=0, y=None, size=None, color=0xffff): + def shows(self, data, space=0, center=True, x=0, y=None, size=None, color=0xffff, bg_color=0x0): """Display character""" if data is not None: if type(data) in [list, bytes, tuple, bytearray]: - self.fill(0) + if self.auto_show: self.fill(bg_color) self.set_buffer(data) if self.auto_show: self.show() else: yy = y - size = self.height // (self._font.height * 3) if size is None else size + if size is None: + font_len, font_buffer = self._take_buffer(str(data), space, 1) + size = min(self.width // font_len, self.height // self._font.height) size = max(round(size), 1) font_len, font_buffer = self._take_buffer(str(data), space, size) x = (self.width - font_len + space) // 2 if center else x y = (self.height - self._font.height * size) // 2 if y is None else y - if yy is None: - self.fill(0) - else: - self.fill_rect(x - 1, y - 1, font_len + 2, font_buffer[0][1][1] * size + 2, 0) + if self.auto_show: + if yy is None: + self.fill(bg_color) + else: + self.fill_rect(x - 1, y - 1, font_len + 2, font_buffer[0][1][1] * size + 2, bg_color) for buffer in font_buffer: #Display character self.bitmap(buffer, x, y, size, color) x = buffer[1][0] * size + x + space if self.auto_show: self.show() - def texts(self, data, space_x=0, space_y=1, x=0, y=0, size=1, color=0xffff): + def texts(self, data, space_x=0, space_y=1, x=0, y=0, size=1, color=0xffff, bg_color=0x0): size = max(round(size), 1) lines = data.split('\n') - self.fill(0) + if self.auto_show: self.fill(bg_color) for line in lines: for char in line: buffer = self._font.chardata(char) @@ -413,13 +416,13 @@ class FrameBuffer_Uincode(FrameBuffer_Base): y = self._font.height * size + y + space_y if self.auto_show: self.show() - def frame(self, data, delay=500, size=None, color=0xffff): + def frame(self, data, delay=500, size=None, color=0xffff, bg_color=0x0): """Display one frame per character""" if data is not None: if type(data) in [list, tuple]: for dat in data: if type(dat) in [list, bytes, tuple, bytearray]: - self.fill(0) + if self.auto_show: self.fill(bg_color) self.set_buffer(dat) self.show() time.sleep_ms(delay) @@ -430,12 +433,12 @@ class FrameBuffer_Uincode(FrameBuffer_Base): for buffer in font_buffer: x=(self.width - buffer[1][0] * size) // 2 y=(self.height - buffer[1][1] * size) // 2 - self.fill_rect(x - 1, y - 1, buffer[1][0] * size + 2, buffer[1][1] * size + 2, 0) + if self.auto_show: self.fill(bg_color) self.bitmap(buffer, x, y, size, color) self.show() time.sleep_ms(delay) - def scroll(self, data, space=0, speed=20, y=None, size=None, step= None, color=0xffff): + def scroll(self, data, space=0, speed=20, y=None, size=None, step= None, color=0xffff, bg_color=0x0): """Scrolling characters""" if data is not None: size = self.height // (self._font.height * 3) if size is None else size @@ -445,7 +448,7 @@ class FrameBuffer_Uincode(FrameBuffer_Base): for i in range(0, font_len - space + self.width, step): x = -i + self.width y = (self.height - self._font.height * size) // 2 if y is None else y - self.fill_rect(x - 1 , y - 1 , self.width -x + 2, font_buffer[0][1][1] * size + 2, 0) + if self.auto_show: self.fill_rect(x - 2 , y - 2 , self.width -x + 4, font_buffer[0][1][1] * size + 4, bg_color) for buffer in font_buffer: self.bitmap(buffer, x, y, size, color) x = buffer[1][0] * size + x + space diff --git a/boards/default_src/micropython_esp32s3/origin/build/lib/mixgo_sant.py b/boards/default_src/micropython_esp32s3/origin/build/lib/mixgo_sant.py index 2310c584..55797e2d 100644 --- a/boards/default_src/micropython_esp32s3/origin/build/lib/mixgo_sant.py +++ b/boards/default_src/micropython_esp32s3/origin/build/lib/mixgo_sant.py @@ -16,11 +16,26 @@ rtc_clock = RTC() #onboard_i2c = I2C(0) onboard_i2c = SoftI2C(scl=Pin(47), sda=Pin(48), freq=400000) +'''BOT035-Sensor''' +try : + import sant_bot + onboard_bot = sant_bot.BOT035(onboard_i2c) +except Exception as e: + print("Warning: Failed to communicate with BOT035 (Coprocessor) or",e) + '''SPI-onboard''' -onboard_spi = SPI(1, baudrate=50000000, polarity=0, phase=0) +onboard_spi = SPI(1, baudrate=80000000, polarity=1, phase=1) + +onboard_bot.tft_reset(0) +time.sleep_ms(50) +onboard_bot.tft_reset(1) +time.sleep_ms(150) + +'''SPI-onboard''' +onboard_spi = SPI(1, baudrate=80000000, polarity=1, phase=1) '''TFT/240*240''' -onboard_tft = st7789_cf.ST7789(onboard_spi, 240, 240, dc_pin=40, cs_pin=None, bl_pin=None, font_address=0xE00000) +onboard_tft = st7789_cf.ST7789(onboard_spi, 240, 240, dc_pin=40, backlight=onboard_bot.tft_brightness, font_address=0xE00000) '''ACC-Sensor''' try : @@ -57,13 +72,6 @@ try : except Exception as e: print("Warning: Failed to communicate with SPL06-001 (BPS) or",e) -'''BOT035-Sensor''' -try : - import sant_bot - onboard_bot = sant_bot.BOT035(onboard_i2c) -except Exception as e: - print("Warning: Failed to communicate with BOT035 (Coprocessor) or",e) - '''ASR-Sensor''' try : import ci130x @@ -77,7 +85,7 @@ onboard_rgb = NeoPixel(Pin(21), 4) '''1Buzzer-Music''' from music import MIDI -onboard_music =MIDI(16) +onboard_music = MIDI(16) '''5KEY_Sensor''' class KEYSensor: @@ -128,10 +136,10 @@ class Button(KEYSensor): B1key = Button(0) B2key = KEYSensor(15,0) -A1key = KEYSensor(15,2900) -A2key = KEYSensor(15,2300) -A3key = KEYSensor(15,1650) -A4key = KEYSensor(15,850) +A1key = KEYSensor(15,2300) +A2key = KEYSensor(15,1650) +A3key = KEYSensor(15,850) +A4key = KEYSensor(15,2900) '''2-LED''' class LED: diff --git a/boards/default_src/micropython_esp32s3/origin/build/lib/sant_bot.py b/boards/default_src/micropython_esp32s3/origin/build/lib/sant_bot.py index 73a369cc..c0ae9b09 100644 --- a/boards/default_src/micropython_esp32s3/origin/build/lib/sant_bot.py +++ b/boards/default_src/micropython_esp32s3/origin/build/lib/sant_bot.py @@ -12,7 +12,8 @@ _BOT035_ADDRESS = const(0x13) _BOT5_TOUCH = const(0x01) _BOT035_ADC = const(0x05) _BOT035_PWM = const(0x07) - +_BOT035_LED = const(0x0C) +_BOT035_CMD = const(0x0D) _BOT035_KB = const(0x10) _BOT035_MS = const(0x14) _BOT035_STR = const(0x18) @@ -60,6 +61,15 @@ class BOT035: if freq is None and duty is None: return self._rreg(_BOT035_PWM + index + 2), self._rreg(_BOT035_PWM) | self._rreg(_BOT035_PWM + 1) << 8 + def tft_brightness(self, brightness=None): + if brightness is None: + return self._rreg(_BOT035_LED) + else: + self._wreg(_BOT035_LED, max(min(brightness, 100), 0)) + + def tft_reset(self, value): + self._wreg(_BOT035_CMD, (self._rreg(_BOT035_CMD) & 0x7F) | (value << 7)) + def hid_keyboard(self, special=0, general=0, release=True): self._buf = bytearray(4) self._buf[0] = special @@ -79,6 +89,10 @@ class BOT035: self._wreg(_BOT035_STR, ord(char) & 0xFF) time.sleep_ms(20 + delay) + def hid_keyboard_state(self): + state = self._rreg(_BOT035_CMD) + return bool(state & 0x10), bool(state & 0x20), bool(state & 0x40) + def hid_mouse(self, keys=0, move=(0, 0), wheel=0, release=True): self._i2c.writeto_mem(_BOT035_ADDRESS, _BOT035_MS, bytes([keys & 0x0F, move[0] & 0xFF, move[1] & 0xFF, wheel & 0xFF])) if release: diff --git a/boards/default_src/micropython_esp32s3/origin/build/lib/st7789_cf.py b/boards/default_src/micropython_esp32s3/origin/build/lib/st7789_cf.py index d2cc7270..ec5ad213 100644 --- a/boards/default_src/micropython_esp32s3/origin/build/lib/st7789_cf.py +++ b/boards/default_src/micropython_esp32s3/origin/build/lib/st7789_cf.py @@ -3,8 +3,6 @@ ST7789/FrameBuffer MicroPython library for the ST7789(TFT-SPI) ======================================================= -#Preliminary composition 20240110 - @dahanzimin From the Mixly Team """ import time, uframebuf @@ -30,31 +28,23 @@ _CMD_COLMOD = const(0x3A) _CMD_MADCTL = const(0x36) class ST7789(uframebuf.FrameBuffer_Uincode): - def __init__(self, spi, width, height, dc_pin=None, cs_pin=None, bl_pin=None, font_address=0x700000): - if height != 240 or width not in [320, 240, 135]: - raise ValueError("Unsupported display. 320x240, 240x240 and 135x240 are supported.") + def __init__(self, spi, width, height, dc_pin=None, backlight=None, font_address=0x700000): self.spi = spi self.dc = Pin(dc_pin, Pin.OUT, value=1) - self.cs = Pin(cs_pin, Pin.OUT, value=1) if cs_pin is not None else None self._buffer = bytearray(width * height * 2) super().__init__(self._buffer, width, height, uframebuf.RGB565) self.font(font_address) self._init() self.show() - time.sleep_ms(200) - self._brightness = 0.6 - self.bl_led = PWM(Pin(bl_pin), duty_u16=int(self._brightness * 60000)) if bl_pin is not None else None + self._backlight = backlight + self.set_brightness(0.5) - def _write(self, cmd, dat = None): - if self.cs: self.cs.off() + def _write(self, cmd, dat=None): self.dc.off() self.spi.write(bytearray([cmd])) - if self.cs: self.cs.on() if dat is not None: - if self.cs: self.cs.off() self.dc.on() self.spi.write(dat) - if self.cs: self.cs.on() def _init(self): """Display initialization configuration""" @@ -74,24 +64,20 @@ class ST7789(uframebuf.FrameBuffer_Uincode): (0xD0, b'\xA4\xA1', 10), (0xE0, b'\xD0\x04\x0D\x11\x13\x2B\x3F\x54\x4C\x18\x0D\x0B\x1F\x23', 10), (0xE1, b'\xD0\x04\x0C\x11\x13\x2C\x3F\x44\x51\x2F\x1F\x1F\x20\x23', 10), - (0x21, None, 10), - (0x29, None, 10), - # (_CMD_INVOFF, None, 10), - # (_CMD_NORON, None, 10), - # (_CMD_DISPON, None, 200), + (_CMD_INVON, None, 10), + (_CMD_DISPON, None, 10), ]: self._write(cmd, data) if delay: time.sleep_us(delay) def get_brightness(self): - return self._brightness + return self._backlight() / 100 def set_brightness(self, brightness): if not 0.0 <= brightness <= 1.0: raise ValueError("Brightness must be a decimal number in the range: 0.0~1.0") - self._brightness = brightness - self.bl_led.duty_u16(int(brightness*60000)) + self._backlight(int(brightness * 100)) def color(self, red, green=None, blue=None): """ Convert red, green and blue values (0-255) into a 16-bit 565 encoding.""" @@ -102,6 +88,6 @@ class ST7789(uframebuf.FrameBuffer_Uincode): def show(self): """Refresh the display and show the changes.""" - self._write(_CMD_CASET, b'\x00\x00\x01\x3f') + self._write(_CMD_CASET, b'\x00\x00\x00\xef') self._write(_CMD_RASET, b'\x00\x00\x00\xef') self._write(_CMD_RAMWR, self._buffer)