|
| 1 | +import machine |
| 2 | +import time |
| 3 | +from machine import SoftI2C, Pin |
| 4 | + |
| 5 | +uart = machine.UART(1, 460966, tx = 8, rx = 9, timeout=200) |
| 6 | +i2c = SoftI2C(scl=Pin(7), sda=Pin(6), freq=400000) |
| 7 | + |
| 8 | +number_map=[ |
| 9 | +bytearray([0xf8,0x88,0x88,0x88,0xF8]), |
| 10 | +bytearray([0,0,0xf8,0,0]), |
| 11 | +bytearray([0xb8,0xa8,0xa8,0xa8,0xe8]), |
| 12 | +bytearray([0xa8,0xa8,0xa8,0xa8,0xf8]), |
| 13 | +bytearray([0xe0,0x20,0x20,0x20,0xf8]), |
| 14 | +bytearray([0xe8,0xa8,0xa8,0xa8,0xb8]), |
| 15 | +bytearray([0xf8,0xa8,0xa8,0xa8,0xb8]), |
| 16 | +bytearray([0x80,0x80,0x80,0x80,0xf8]), |
| 17 | +bytearray([0xf8,0xa8,0xa8,0xa8,0xf8]), |
| 18 | +bytearray([0xe0,0xa0,0xa0,0xa0,0xf8]), |
| 19 | +] |
| 20 | +letter_map=[] |
| 21 | + |
| 22 | +def calc_checksum(data : bytearray): |
| 23 | + sum = 0 |
| 24 | + for i in range(0,len(data)-1): |
| 25 | + sum+=data[i] |
| 26 | + return sum&0xff |
| 27 | + |
| 28 | +def uart_write(addr, data:bytearray) -> num: |
| 29 | + #与国产MCU通信,格式为头帧0x90+地址+数据长度+数据+累加和校验值 |
| 30 | + #如通信成功会返回3bytes数据b'\x91\x08\x05',有时通信会卡死,原因未知,连续写两次则必定成功,奇怪??? |
| 31 | + if(len(data)==0): |
| 32 | + return False |
| 33 | + byteToWrite = bytearray([0x90]) |
| 34 | + byteToWrite.append(addr) |
| 35 | + byteToWrite.append(len(data)) |
| 36 | + byteToWrite.extend(data) |
| 37 | + byteToWrite.append(0) |
| 38 | + byteToWrite[len(byteToWrite)-1]=calc_checksum(byteToWrite) |
| 39 | + for i in range(0,2): |
| 40 | + uart.write(byteToWrite) |
| 41 | + ansbytes = uart.read(3) |
| 42 | + try: |
| 43 | + if len(ansbytes)==3: |
| 44 | + return ansbytes[2] |
| 45 | + else: |
| 46 | + print("write error: ", ansbytes) |
| 47 | + return False |
| 48 | + except: |
| 49 | + pass |
| 50 | + |
| 51 | +def led_rect(): |
| 52 | + databytes = bytearray([0xf8,0xf8,0xf8,0x88,0xF8]) |
| 53 | + uart_write(0x08, databytes) |
| 54 | + return |
| 55 | + |
| 56 | +def led_array_demo(): |
| 57 | + databytes = bytearray([0xf8,0xf8,0xf8,0xf8,0xf8]) |
| 58 | + for i in range(0,10): |
| 59 | + for j in range(0,5): |
| 60 | + databytes[j]=databytes[j]<<1 |
| 61 | + if databytes[j] == 0: |
| 62 | + databytes[j] = 0x08 |
| 63 | + uart_write(0x08, databytes) |
| 64 | + time.sleep_ms(100) |
| 65 | + return |
| 66 | + |
| 67 | +def show_number(n): |
| 68 | + uart_write(0x08, number_map[n]) |
| 69 | + |
| 70 | +for i in range(0,10): |
| 71 | + show_number(i) |
| 72 | + time.sleep(0.5) |
| 73 | + |
| 74 | + |
| 75 | + |
0 commit comments