更新MINI的固件,包括(绘制指针,网络SSL,DS18X20,知心,BLE-NOT)

This commit is contained in:
dahanzimin
2024-09-27 21:45:58 +08:00
committed by 王立帮
parent d28ec53f56
commit 443c901d74
9 changed files with 390 additions and 59 deletions

Binary file not shown.

View File

@@ -1,56 +1,52 @@
#ADXL345
import time
import ustruct
from micropython import const
DATA_FORMAT = const(0x31)
BW_RATE = const(0x2c)
POWER_CTL = const(0x2d)
INT_ENABLE = const(0x2E)
OFSX = const(0x1e)
OFSY = const(0x1f)
OFSZ = const(0x20)
class ADXL345:
def __init__(self, i2c, address=0X53):
self._device = i2c
self._address = address
for addr in self._device.scan():
if self._device.readfrom_mem(addr, 0, 1)[0] == 0xe5:
self._address = addr
break
else:
raise AttributeError("Cannot find a ADXL345")
self._wreg(DATA_FORMAT,0x2B) #16g量程
self._wreg(BW_RATE,0x0A) #数据输出速度为100Hz
self._wreg(INT_ENABLE,0x00) #不使用中断
self._wreg(OFSX,0x00)
self._wreg(OFSY,0x00)
self._wreg(OFSZ,0x00)
self._wreg(POWER_CTL,0x08) #链接使能,测量模式
time.sleep(0.5)
def readXYZ(self):
x, = ustruct.unpack('<h', self._rreg(0x32,2))
y, = ustruct.unpack('<h', self._rreg(0x34,2))
z, = ustruct.unpack('<h', self._rreg(0x36,2))
return (x/256,y/256,z/256)
def readX(self):
return self.readXYZ()[0]
def readY(self):
return self.readXYZ()[1]
def readZ(self):
return self.readXYZ()[2]
def _wreg(self, reg, val):
'''Write memory address'''
self._device.writeto_mem(self._address,reg,val.to_bytes(1, 'little'))
def _rreg(self, reg,nbytes=1):
'''Read memory address'''
return self._device.readfrom_mem(self._address, reg, nbytes)[0] if nbytes<=1 else self._device.readfrom_mem(self._address, reg, nbytes)[0:nbytes]
#ADXL345
import time
import ustruct
from micropython import const
DATA_FORMAT = const(0x31)
BW_RATE = const(0x2c)
POWER_CTL = const(0x2d)
INT_ENABLE = const(0x2E)
OFSX = const(0x1e)
OFSY = const(0x1f)
OFSZ = const(0x20)
class ADXL345:
def __init__(self, i2c, address=0X53):
self._device = i2c
self._address = address
if self._rreg(0x0) != 0xe5:
raise AttributeError("Cannot find a ADXL345")
self._wreg(DATA_FORMAT,0x2B) #16g量程
self._wreg(BW_RATE,0x0A) #数据输出速度为100Hz
self._wreg(INT_ENABLE,0x00) #不使用中断
self._wreg(OFSX,0x00)
self._wreg(OFSY,0x00)
self._wreg(OFSZ,0x00)
self._wreg(POWER_CTL,0x08) #链接使能,测量模式
time.sleep(0.5)
def readXYZ(self):
x, = ustruct.unpack('<h', self._rreg(0x32,2))
y, = ustruct.unpack('<h', self._rreg(0x34,2))
z, = ustruct.unpack('<h', self._rreg(0x36,2))
return (x/256,y/256,z/256)
def readX(self):
return self.readXYZ()[0]
def readY(self):
return self.readXYZ()[1]
def readZ(self):
return self.readXYZ()[2]
def _wreg(self, reg, val):
'''Write memory address'''
self._device.writeto_mem(self._address,reg,val.to_bytes(1, 'little'))
def _rreg(self, reg,nbytes=1):
'''Read memory address'''
return self._device.readfrom_mem(self._address, reg, nbytes)[0] if nbytes<=1 else self._device.readfrom_mem(self._address, reg, nbytes)[0:nbytes]

View File

@@ -0,0 +1,177 @@
"""
Bluetooth-HID-Keyboard
Micropython library for the Bluetooth-HID-Keyboard
=======================================================
#https://github.com/Heerkog/MicroPythonBLEHID
@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_PASSKEY_ACTION = const(31)
_PASSKEY_ACTION_INPUT = const(2)
_PASSKEY_ACTION_DISP = const(3)
_PASSKEY_ACTION_NUMCMP = const(4)
_HID_INPUT_REPORT = const(b'\x05\x01\t\x06\xa1\x01\x85\x01u\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')
_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), 0x01 | 0x02),
(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), 0x01 | 0x02),
(UUID(0x2908), 0x01 | 0x02),)),
(UUID(0x2A4D), FLAG_READ | FLAG_WRITE, (
(UUID(0x2902), 0x01 | 0x02),
(UUID(0x2908), 0x01 | 0x02),)),
(UUID(0x2A4E), FLAG_READ | FLAG_WRITE | FLAG_WRITE_NO_RESPONSE), ), )
class Keyboard:
def __init__(self, name=None, passkey=1234, battery_level=100):
if (name is '') or (name is None):
name = "Mixgo_" + self.mac[-6:].upper()
print("Keyboard 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'
handles = self._ble.gatts_register_services((_DIS, _BAS, _HIDS))
self._service_characteristics(handles)
self._payload = advertising_payload(name=name, services=[UUID(0x1812)], appearance=const(961))
self.advertise()
def _irq(self, event, data):
# Interrupt request callback function
if event == _IRQ_CENTRAL_CONNECT:
self.conn_handle, _, _ = data
print("Keyboard connected: ", self.conn_handle)
self.device_state = True
elif event == _IRQ_CENTRAL_DISCONNECT:
self.conn_handle = None
conn_handle, addr_type, addr = data
print("Keyboard 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)
#else:
#print("Unhandled IRQ event: ", event, data)
def _service_characteristics(self, handles):
(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.h_rep, _, h_d1, _, _, h_d2, 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, struct.pack("<BHHH", 0x01, 0xFE61, 0x01, 0x0123))
# Write BAS characteristics.
self._ble.gatts_write(self.h_bat, struct.pack("<B", self.battery_level))
self._ble.gatts_write(h_bfmt, b'\x04\x00\xad\x27\x01\x00\x00')
self._ble.gatts_write(h_ccc, b'\x00\x00')
# Write service characteristics
self._ble.gatts_write(h_info, b"\x01\x01\x00\x02")
self._ble.gatts_write(h_hid, _HID_INPUT_REPORT)
self._ble.gatts_write(h_d1, b'\x01\x01')
self._ble.gatts_write(h_d2, b'\x01\x02')
self._ble.gatts_write(h_proto, b"\x01")
def state(self):
return int.from_bytes(self.report, 'big')
def is_connected(self):
return self.device_state
def advertise(self, interval_us=100000):
print("Starting advertising")
self._ble.gap_advertise(interval_us, adv_data=self._payload)
def notify_hid(self, special=0, general=0, release=True):
if self.is_connected():
_keys = bytearray(6)
if type(general) in (tuple, list):
for i in range(len(general)):
if i > 5: break
_keys[i] = general[i]
else:
_keys[0] = general
# Pack the mouse state as described by the input report
self._ble.gatts_notify(self.conn_handle, self.h_rep, bytes([special & 0xFF, 0]) + _keys)
if release:
time.sleep_ms(10)
self._ble.gatts_notify(self.conn_handle, self.h_rep, b'\x00\x00\x00\x00\x00\x00\x00\x00')
def notify_battery(self, level):
if self.is_connected():
self.battery_level = max(min(level, 100), 0)
# Notifies the client by writing to the battery level handle.
self._ble.gatts_notify(self.conn_handle, self.h_bat, struct.pack("<B", self.battery_level))
def notify_str(self, string, delay=0):
for char in str(string):
char = max(min(ord(char), 127), 0)
self.notify_hid( 0x02 if _KEYCODE[char] >> 7 else 0x00, _KEYCODE[char] & 0x7F)
time.sleep_ms(20 + delay)
@property
def mac(self):
'''Get mac address'''
return hexlify(self._ble.config('mac')[1]).decode()

View File

@@ -0,0 +1,152 @@
"""
Bluetooth-HID-Mouse
Micropython library for the Bluetooth-HID-Mouse
=======================================================
#https://github.com/Heerkog/MicroPythonBLEHID
@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_MTU_EXCHANGED = const(21)
_IRQ_CONNECTION_UPDATE = const(27)
_IRQ_PASSKEY_ACTION = const(31)
_PASSKEY_ACTION_INPUT = const(2)
_PASSKEY_ACTION_DISP = const(3)
_PASSKEY_ACTION_NUMCMP = const(4)
_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')
_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), 0x01 | 0x02),
(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), 0x01 | 0x02),
(UUID(0x2908), 0x01 | 0x02), )),
(UUID(0x2A4E), FLAG_READ | FLAG_WRITE | FLAG_WRITE_NO_RESPONSE), ), )
class Mouse:
def __init__(self, name=None, passkey=1234, battery_level=100):
if (name is '') or (name is None):
name = "Mixgo_" + self.mac[-6:].upper()
print("Mouse 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
handles = self._ble.gatts_register_services((_DIS, _BAS, _HIDS))
self._service_characteristics(handles)
self._payload = advertising_payload(name=name, services=[UUID(0x1812)], appearance=const(962))
self.advertise()
def _irq(self, event, data):
# Interrupt request callback function
if event == _IRQ_CENTRAL_CONNECT:
self.conn_handle, _, _ = data
print("Mouse connected: ", self.conn_handle)
self.device_state = True
elif event == _IRQ_CENTRAL_DISCONNECT:
self.conn_handle = None
conn_handle, addr_type, addr = data
print("Mouse 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")
#else:
#print("Unhandled IRQ event: ", event, data)
def _service_characteristics(self, handles):
(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, h_ctrl, self.h_rep, _, h_d1, 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, struct.pack("<BHHH", 0x01, 0xFE61, 0x01, 0x0123))
# Write BAS characteristics.
self._ble.gatts_write(self.h_bat, struct.pack("<B", self.battery_level))
self._ble.gatts_write(h_bfmt, b'\x04\x00\xad\x27\x01\x00\x00')
self._ble.gatts_write(h_ccc, b'\x00\x00')
# Write service characteristics
self._ble.gatts_write(h_info, b"\x01\x01\x00\x02")
self._ble.gatts_write(h_hid, _HID_INPUT_REPORT)
self._ble.gatts_write(self.h_rep, b'\x00\x00\x00\x00')
self._ble.gatts_write(h_d1, b'\x01\x01')
self._ble.gatts_write(h_proto, b"\x01")
def is_connected(self):
return self.device_state
def advertise(self, interval_us=100000):
print("Starting advertising")
self._ble.gap_advertise(interval_us, adv_data=self._payload)
def notify_hid(self, keys=0, move=(0, 0), wheel=0, release=True):
if self.is_connected():
# Pack the mouse state as described by the input report
self._ble.gatts_notify(self.conn_handle, self.h_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.h_rep, b'\x00\x00\x00\x00')
def notify_battery(self, level):
if self.is_connected():
self.battery_level = max(min(level, 100), 0)
# Notifies the client by writing to the battery level handle.
self._ble.gatts_notify(self.conn_handle, self.h_bat, struct.pack("<B", self.battery_level))
@property
def mac(self):
'''Get mac address'''
return hexlify(self._ble.config('mac')[1]).decode()

View File

@@ -43,7 +43,7 @@ def request(method, url, data=None, json=None, headers={}, stream=None, parse_he
if proto == "http:":
port = 80
elif proto == "https:":
import ussl
import ssl
port = 443
else:
raise ValueError("Unsupported protocol: " + proto)
@@ -63,7 +63,7 @@ def request(method, url, data=None, json=None, headers={}, stream=None, parse_he
try:
s.connect(ai[-1])
if proto == "https:":
s = ussl.wrap_socket(s, server_hostname=host)
s = ssl.wrap_socket(s, server_hostname=host)
s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))
if not "Host" in headers:
s.write(b"Host: %s\r\n" % host)

View File

@@ -5,7 +5,7 @@ Micropython library for the MINI_WCH(TOUCH*2, MIC*1, Buzzer*1, PWM*2, Matrix8x12
=======================================================
@dahanzimin From the Mixly Team
"""
import time
import time, math
from esp import flash_read
from micropython import const
from framebuf import FrameBuffer, MONO_VLSB
@@ -215,6 +215,12 @@ class BOT035(FrameBuffer):
self.show()
time.sleep_ms(speed)
def pointern(self, x=_LEDS_W // 2, y=_LEDS_H // 2, l=_LEDS_H // 2, angle=0):
radian = math.radians(angle)
self.fill(0)
self.line(x, y, round(x + l * math.sin(radian)), round(y - l * math.cos(radian)), 1)
self.show()
def _wreg(self, reg, val):
'''Write memory address'''
self._i2c.writeto_mem(_BOT035_ADDRESS, reg, val.to_bytes(1, 'little'))