Files
mixly3/boards/default/micropython_esp32s3/build/lib/soar_bot.py

107 lines
3.8 KiB
Python

"""
SOAR_WCH
Micropython library for the SOAR_WCH
=======================================================
@dahanzimin From the Mixly Team
"""
import time
from micropython import const
_BOT035_ADDRESS = const(0x13)
_BOT5_TOUCH = const(0x01)
_BOT035_ADC = const(0x05)
_BOT035_OPA = const(0x09)
_BOT035_PWM = const(0x0B)
_BOT035_CMD = const(0x11)
_BOT035_STA = const(0x12)
_BOT035_KB = const(0x13)
_BOT035_MS = const(0x17)
_BOT035_STR = const(0x1B)
class BOT035:
def __init__(self, i2c_bus):
self._i2c = i2c_bus
self._touchs = [self.touch(0), self.touch(1)]
self.reset()
def _wreg(self, reg, val, digit=1):
'''Write memory address'''
self._i2c.writeto_mem(_BOT035_ADDRESS, reg, val.to_bytes(digit, 'little'))
def _rreg(self, reg, nbytes=1):
'''Read memory address'''
self._i2c.writeto(_BOT035_ADDRESS, reg.to_bytes(1, 'little'))
return int.from_bytes(self._i2c.readfrom(_BOT035_ADDRESS, nbytes), 'little')
def reset(self):
self._i2c.writeto_mem(_BOT035_ADDRESS, _BOT035_PWM, b' Ndddd\xc0')
def touch(self, index, value=None):
index = max(min(index, 1), 0)
touch = 4095 - self._rreg(_BOT5_TOUCH + index * 2, 2)
return touch > value if value else touch
def touched(self, index, value=600):
return self.touch(index, value)
def touch_slide(self):
values = []
for i in range(20):
values.append((self.touch(1) - self._touchs[1]) - (self.touch(0) - self._touchs[0]))
return round(sorted(values)[10] / 10)
def brightness(self, index):
index = max(min(index, 1), 0)
return 4095 - self._rreg(_BOT035_ADC + index * 2, 2)
def amp_pga(self, k=1, b=155, pga=3, dp=0):
"""0:4x,1:8x,2:16x,3:32x"""
self._wreg(_BOT035_CMD, (self._rreg(_BOT035_CMD) & 0x3F) | (pga & 0x03) << 6)
return round((self._rreg(_BOT035_OPA, 2) * k - b) / (2 ** (pga + 2)), dp)
def usben(self, index=1, duty=None, freq=None):
index = max(min(index, 4), 1)
if duty is not None:
self._wreg(_BOT035_PWM + index + 1, int(max(min(duty, 100), 0)))
if freq is not None:
self._wreg(_BOT035_PWM, max(min(freq, 65535), 10), 2)
if freq is None and duty is None:
return self._rreg(_BOT035_PWM + index + 1), self._rreg(_BOT035_PWM ,2)
def tft_brightness(self, brightness=None):
if brightness is None:
return round((self._rreg(_BOT035_CMD) & 0x3F) * 1.587)
else:
brightness = round(brightness* 0.63)
self._wreg(_BOT035_CMD, (self._rreg(_BOT035_CMD) & 0xC0) | max(min(brightness, 63), 0))
def hid_keyboard(self, special=0, general=0, release=True):
self._buf = bytearray(4)
self._buf[0] = special
if type(general) in (tuple, list):
for i in range(len(general)):
if i > 2: break
self._buf[i + 1] = general[i]
else:
self._buf[1] = general
self._i2c.writeto_mem(_BOT035_ADDRESS, _BOT035_KB, self._buf)
if release:
time.sleep_ms(10)
self._i2c.writeto_mem(_BOT035_ADDRESS, _BOT035_KB, bytes(4))
def hid_keyboard_str(self, string, delay=0):
for char in str(string):
self._wreg(_BOT035_STR, ord(char) & 0xFF)
time.sleep_ms(20 + delay)
def hid_keyboard_state(self):
state = self._rreg(_BOT035_STA)
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:
time.sleep_ms(10)
self._i2c.writeto_mem(_BOT035_ADDRESS, _BOT035_MS, bytes(4))