update Home PC 22 May 2026

This commit is contained in:
chanweehewsonos
2026-05-22 19:08:33 +08:00
parent aa91ee38fa
commit ed6e5c507d
857 changed files with 78641 additions and 49 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,464 @@
import sys, machine, time, json
import binascii
config = {'type': 'undefined', 'no': 'undefined', 'count': 0}
config_file = "config.json"
##########################################################
command_info = [
{ "cmd": "ping", "descript": "return OK if mico controller board works normal" },
{ "cmd": "reset", "descript": "reboot mico controller board" },
{ "cmd": "get_config", "descript": "read configuration from mico controller board",
"parameters": [
{ "config_item_name": "config item to read, if not provide will return all config values" }
],
"example": "> get_config count\n < 0\n > get_config\n < {'no': 'undefined', 'type': 'undefined', 'count': 0}"
},
{ "cmd": "set_config", "descript": "set configuration to mico controller board",
"parameters": [
{ "config_item_name": "config item name" },
{ "config_item_value": "config item value " }
],
"example": ">set_config sn 'CONTROLLER-001'"
},
{ "cmd": "gpio_init", "descript": "initial gpio pin",
"parameters": [
{ "pin_num": "gpio pin number" },
{ "gpio direction": "0 - input, 1 - output" },
{ "initial status": "initial status of gpio pin, 0 - low, 1 - high" },
{ "pull control": "UP - enable pull up, DOWN - enable pull down, others - disable pull function"}
],
"example": ">gpio_init 1 1 1 UP"
},
{ "cmd": "gpio_set", "descript": "set gpio pin status",
"parameters": [
{ "pin_num": "gpio pin number" },
{ "pin status": "initial status of gpio pin, 0 - low, 1 - high" }
],
"example": ">gpio_set 1 0"
},
{ "cmd": "gpio_get", "descript": "get gpio pin status",
"parameters": [
{ "pin_num": "gpio pin number" }
],
"example": ">gpio_get 1"
},
{ "cmd": "i2c_cleanup", "descript": "release i2c bus" },
{ "cmd": "i2c_scan", "descript": "scan i2c devices" },
{ "cmd": "i2c_read", "descript": "perform read operation from i2c device",
"parameters": [
{ "device": "i2c slave device address" },
{ "length": "bytes to read" }
],
"example": ">i2c_read 0x20 1\n <succeed: 0xFF"
},
{ "cmd": "i2c_read_add", "descript": "perform read operation from i2c device with register address",
"parameters": [
{ "device": "i2c slave device address" },
{ "addr": "register address" },
{ "length": "bytes to read" }
],
"example": ">i2c_read_add 0x20 0 2\n <succeed: 0xFF 0x01"
},
{ "cmd": "i2c_write", "descript": "perform write operation to i2c device ",
"parameters": [
{ "device": "i2c slave device address" },
{ "data": "bytes to write" }
],
"example": ">i2c_write 0x20 0x01 0x02 0x03 ... 0x10"
},
{ "cmd": "i2c_write_add", "descript": "perform write operation to i2c device with register address",
"parameters": [
{ "device": "i2c slave device address" },
{ "addr": "register address" },
{ "data": "bytes to write" }
],
"example": ">i2c_write 0x20 0x12 0x01 0x02 0x03 ... 0x10"
},
{ "cmd": "spi_setup", "descript": "initial spi bus",
"parameters": [
{ "clk_freq": "ispi clock frequence" },
{ "sck_pin": "sck pin io number" },
{ "mosi_pin": "mosi pin io number" },
{ "miso_pin": "miso pin io number" },
{ "cs_pin": "cs pin io number, -1 if don't want spi controller toggle cs pin" },
{ "polarity": "polarity setting for spi bus" },
{ "phase": "phase setting for spi bus" }
],
"example": ">spi_setup 100000 2 3 0 1 0 1"
},
{ "cmd": "spi_cleanup", "descript": "release spi bus" },
{ "cmd": "spi_read", "descript": "perform read operation from spi device",
"parameters": [
{ "length": "bytes to read" }
],
"example": ">spi_read 3\n <succeed: 0xFF 0xAA 0xBB"
},
{ "cmd": "spi_write", "descript": "perform write operation to spi device ",
"parameters": [
{ "data": "bytes to write" }
],
"example": ">spi_write 0x01 0x02 0x03 ... 0x10"
},
{ "cmd": "spi_write_read", "descript": "perform write and then read to spi device ",
"parameters": [
{ "length": "bytes to read" },
{ "data": "bytes to write" }
],
"example": ">spi_write_read 3 0x01 0x02 0x03 ... 0x10\n <succeed: 0xFF 0xAA 0xBB"
},
]
##########################################################
def load_config():
global config
try:
f = open(config_file,'r')
config = json.load(f)
f.close()
except:
print('config file not exist, load default settings!')
save_config()
def save_config():
f = open(config_file,'w')
json.dump(config, f)
f.close()
def bytes_to_str(bytes):
return ",".join([f"0x{b:02x}" for b in bytes])
def str_to_int(str):
if str.startswith("0x"):
return int(str, 16)
else:
return int(str, 10)
def print_help():
for command in command_info:
print(f"{command['cmd']} - {command['descript']}")
if 'parameters' in command.keys():
print(' parameters:')
for parameter in command['parameters']:
parameter_name = list(parameter.keys())[0]
print(f" {parameter_name}: {parameter[parameter_name]}")
if 'example' in command.keys():
print(' example:')
print(f' {command['example']}')
if 'parameters' in command.keys() or 'example' in command.keys():
print('')
##########################################################
load_config()
time.sleep_ms(3000)
i2c_controller = None
spi_controller = None
spi_cs_pin = None
gpio_dict = {}
######################################################################
## Program PCM-1864 0x4a, 0x4b
######################################################################
i2c_controller = machine.I2C(0, sda=machine.Pin(0, machine.Pin.OPEN_DRAIN, machine.Pin.PULL_UP), scl=machine.Pin(1, machine.Pin.OUT, machine.Pin.PULL_UP), freq=100000)
#0x4a
i2c_controller.writeto_mem(0x4a, 0x06, bytes([0x10]))
i2c_controller.writeto_mem(0x4a, 0x07, bytes([0x10]))
i2c_controller.writeto_mem(0x4a, 0x08, bytes([0x20]))
i2c_controller.writeto_mem(0x4a, 0x09, bytes([0x20]))
i2c_controller.writeto_mem(0x4a, 0x0B, bytes([0x03]))
i2c_controller.writeto_mem(0x4a, 0x0C, bytes([0x01]))
i2c_controller.writeto_mem(0x4a, 0x0D, bytes([0x80]))
i2c_controller.writeto_mem(0x4a, 0x20, bytes([0x03]))
i2c_controller.writeto_mem(0x4a, 0x21, bytes([0x03]))
i2c_controller.writeto_mem(0x4a, 0x22, bytes([0x00]))
i2c_controller.writeto_mem(0x4a, 0x23, bytes([0x01]))
#0x4b
i2c_controller.writeto_mem(0x4b, 0x06, bytes([0x10]))
i2c_controller.writeto_mem(0x4b, 0x07, bytes([0x10]))
i2c_controller.writeto_mem(0x4b, 0x08, bytes([0x20]))
i2c_controller.writeto_mem(0x4b, 0x09, bytes([0x20]))
i2c_controller.writeto_mem(0x4b, 0x0B, bytes([0x03]))
i2c_controller.writeto_mem(0x4b, 0x0C, bytes([0x01]))
i2c_controller.writeto_mem(0x4b, 0x0D, bytes([0x00]))
i2c_controller.writeto_mem(0x4b, 0x20, bytes([0x03]))
i2c_controller.writeto_mem(0x4b, 0x21, bytes([0x03]))
i2c_controller.writeto_mem(0x4b, 0x22, bytes([0x00]))
i2c_controller.writeto_mem(0x4b, 0x23, bytes([0x01]))
i2c_controller = None
print('Sonos Micro Controller V1.0')
print("PyLang", sys.version_info, ";", sys.implementation)
print('=======================================')
while True:
input_string = str(input())
args = input_string.split(" ")
if len(args) >= 1:
if args[0] == 'help' or args[0] == '?':
print_help()
elif args[0] == 'ping': # check if command line is available
print('OK')
elif args[0] == 'reset':
machine.reset()
elif args[0] == 'get_config':
print(config)
elif args[0] == 'set_config':
if args[1] != None and args[2] != None:
config[args[1]] = args[2]
save_config()
else:
print('invalid arguments for set command: ', input_string)
######################################################################
## GPIO control commands
######################################################################
elif args[0] == 'gpio_init':
# gpio_init 1 1 1 UP
try:
pin_num = str_to_int(args[1])
pin_direction = machine.Pin.OUT if str_to_int(args[2]) == 1 else machine.Pin.IN
init_status = str_to_int(args[3])
pull_control = None
if len(args) >= 5:
if args[4].upper().strip() == 'UP':
pull_control = machine.Pin.PULL_UP
elif args[4].upper().strip() == 'DOWN':
pull_control = machine.Pin.PULL_DOWN
else:
print('failed: invalid pull control ', args[4])
gpio_dict[pin_num] = machine.Pin(pin_num, pin_direction, pull_control)
if pin_direction == machine.Pin.OUT:
gpio_dict[pin_num].value(init_status)
print('succeed: ', gpio_dict[pin_num])
except Exception as e:
print('failed: ', e)
elif args[0] == 'gpio_set':
try:
pin_num = str_to_int(args[1])
pin_status = str_to_int(args[2])
if pin_num in gpio_dict:
gpio_dict[pin_num].value(pin_status)
print('succeed: ')
else:
print(f'failed: pin not initialized - {pin_num}')
except Exception as e:
print('failed: ', e)
elif args[0] == 'gpio_get':
try:
pin_num = str_to_int(args[1])
if pin_num in gpio_dict:
print('succeed: ', gpio_dict[pin_num].value())
else:
print(f'failed: pin not initialized - {pin_num}')
except Exception as e:
print('failed: ', e)
######################################################################
## I2C controller commands
######################################################################
elif args[0] == 'i2c_setup':
# ====== i2c controller setup ======
# PiPico: i2c_setup 100000 0 1 # 100000 - i2c clock frequence, 0 - sda pin io number, 1 - scl pin io number
# ESP32: i2c_setup 100000 8 9 # 100000 - i2c clock frequence, 8 - sda pin io number, 9 - scl pin io number
try:
if i2c_controller is not None:
i2c_controller = None
clk_freq = str_to_int(args[1])
sda_pin = str_to_int(args[2])
scl_pin = str_to_int(args[3])
i2c_controller = machine.I2C(0, sda=machine.Pin(sda_pin, machine.Pin.OPEN_DRAIN, machine.Pin.PULL_UP), scl=machine.Pin(scl_pin, machine.Pin.OUT, machine.Pin.PULL_UP), freq=clk_freq) # Frequency in Hz
print('succeed: ', i2c_controller)
except Exception as e:
print('failed: ', e)
elif args[0] == 'i2c_cleanup':
# ====== i2c controller setup ======
try:
if i2c_controller is not None:
i2c_controller = None
print('succeed: ')
except Exception as e:
print('failed: ', e)
elif args[0] == 'i2c_scan':
# ====== scan I2C devices ======
try:
devices = i2c_controller.scan()
if len(devices) == 0:
print('failed: ', "no I2C devices found")
else:
print('succeed: ', bytes_to_str(devices))
except Exception as e:
print('failed: ', e)
elif args[0] == 'i2c_read':
# ====== i2c data read ======
# i2c_read 0x20 1 # 0x20 - device_address, 1 - read length
try:
device = str_to_int(args[1])
length = str_to_int(args[2])
data = i2c_controller.readfrom(device, length)
print('succeed: ', data.hex(','))
except Exception as e:
print('failed: ', e)
elif args[0] == 'i2c_read_add':
# ====== i2c data read address ======
# i2c_read_add 0x20 0 2 # 0x20 - device_address, 0 - register address, 2 - read length
try:
device = str_to_int(args[1])
addr = str_to_int(args[2])
length = str_to_int(args[3])
data = i2c_controller.readfrom_mem(device, addr, length)
print('succeed: ', bytes_to_str(data))
except Exception as e:
print('failed: ', e)
elif args[0] == 'i2c_write':
# ====== i2c data write ======
# i2c_write 0x20 0x01 0x02 0x03 ... 0x10 # 0x20 - device_address, 0x01 ~ 0x10 - data to write to device
try:
device = str_to_int(args[1])
data = bytearray()
for i in range(2, len(args)):
data.append(str_to_int(args[i]))
i2c_controller.writeto(device, data)
print('succeed: ')
except Exception as e:
print('failed: ', e)
elif args[0] == 'i2c_write_add':
# ====== i2c data write address ======
# i2c_write_add 0x20 0x12 0x01 0x02 0x03 ... 0x10 # 0x20 - device_address, 0x12 - register address, 0x01 ~ 0x10 - data to write to device
try:
device = str_to_int(args[1])
addr = str_to_int(args[2])
data = bytearray()
for i in range(3, len(args)):
data.append(str_to_int(args[i]))
i2c_controller.writeto_mem(device, addr, data)
print('succeed: ')
except Exception as e:
print('failed: ', e)
######################################################################
## SPI controller commands
######################################################################
elif args[0] == 'spi_setup':
# ====== spi controller setup ======
# PiPico: spi_setup 100000 2 3 0 1 0 1 # param#1: 100000 - spi clock frequence
# # param#2: 2 - sck pin io number
# # param#3: 3 - mosi pin io number
# # param#4: 0 - miso pin io number
# # param#5: 1 - cs pin io number, -1 if don't want spi controller toggle cs pin
# # param#6: 0 - polarity setting
# # param#7: 1 - phase setting
#
try:
if spi_controller is not None:
spi_controller = None
if spi_cs_pin is not None:
spi_cs_pin = None
clk_freq = str_to_int(args[1])
spi_sck_pin = machine.Pin(str_to_int(args[2]))
spi_mosi_pin = machine.Pin(str_to_int(args[3]))
spi_miso_pin = machine.Pin(str_to_int(args[4]))
spi_cs_pin = machine.Pin(str_to_int(args[5]), machine.Pin.OUT)
polarity = str_to_int(args[6])
phase = str_to_int(args[7])
spi_controller = machine.SPI(0, baudrate=clk_freq, polarity=polarity, phase=phase, sck=spi_sck_pin, mosi=spi_mosi_pin, miso=spi_miso_pin)
print('succeed: ', spi_controller)
except Exception as e:
print('failed: ', e)
elif args[0] == 'spi_cleanup':
# ====== spi controller setup ======
try:
if spi_controller is not None:
spi_controller = None
if spi_cs_pin is not None:
spi_cs_pin = None
print('succeed: ')
except Exception as e:
print('failed: ', e)
elif args[0] == 'spi_read':
# ====== spi data read ======
# spi_read 3 # 3 - read length
try:
length = str_to_int(args[1])
# active cs pin if required
if spi_cs_pin is not None:
spi_cs_pin.value(0)
data = spi_controller.read(length)
# deactive cs pin if required
if spi_cs_pin is not None:
spi_cs_pin.value(1)
print('succeed: ', data.hex(','))
except Exception as e:
print('failed: ', e)
elif args[0] == 'spi_write':
# ====== spi data write ======
# spi_write 0x01 0x02 0x03 ... 0x10 # 0x01 ~ 0x10 - data to write to device
try:
data = bytearray()
for i in range(1, len(args)):
data.append(str_to_int(args[i]))
# active cs pin if required
if spi_cs_pin is not None:
spi_cs_pin.value(0)
spi_controller.write(data)
# deactive cs pin if required
if spi_cs_pin is not None:
spi_cs_pin.value(1)
print('succeed: ')
except Exception as e:
print('failed: ', e)
elif args[0] == 'spi_write_read':
# ====== spi data read ======
# spi_write_read 3 0x01 0x02 0x03 ... 0x10 # 3 - read length, 0x01 ~ 0x10 - data to write to device
try:
read_length = str_to_int(args[1])
# active cs pin if required
if spi_cs_pin is not None:
spi_cs_pin.value(0)
write_data = bytearray()
for i in range(2, len(args)):
write_data.append(str_to_int(args[i]))
spi_controller.write(write_data)
read_data = spi_controller.read(read_length)
# deactive cs pin if required
if spi_cs_pin is not None:
spi_cs_pin.value(1)
print('succeed: ', read_data.hex(','))
except Exception as e:
print('failed: ', e)
else:
print('unknow command: ', args[0])

View File

@@ -0,0 +1,431 @@
import sys, machine, time, json
import binascii
config = {'type': 'undefined', 'no': 'undefined', 'count': 0}
config_file = "config.json"
##########################################################
command_info = [
{ "cmd": "ping", "descript": "return OK if mico controller board works normal" },
{ "cmd": "reset", "descript": "reboot mico controller board" },
{ "cmd": "get_config", "descript": "read configuration from mico controller board",
"parameters": [
{ "config_item_name": "config item to read, if not provide will return all config values" }
],
"example": "> get_config count\n < 0\n > get_config\n < {'no': 'undefined', 'type': 'undefined', 'count': 0}"
},
{ "cmd": "set_config", "descript": "set configuration to mico controller board",
"parameters": [
{ "config_item_name": "config item name" },
{ "config_item_value": "config item value " }
],
"example": ">set_config sn 'CONTROLLER-001'"
},
{ "cmd": "gpio_init", "descript": "initial gpio pin",
"parameters": [
{ "pin_num": "gpio pin number" },
{ "gpio direction": "0 - input, 1 - output" },
{ "initial status": "initial status of gpio pin, 0 - low, 1 - high" },
{ "pull control": "UP - enable pull up, DOWN - enable pull down, others - disable pull function"}
],
"example": ">gpio_init 1 1 1 UP"
},
{ "cmd": "gpio_set", "descript": "set gpio pin status",
"parameters": [
{ "pin_num": "gpio pin number" },
{ "pin status": "initial status of gpio pin, 0 - low, 1 - high" }
],
"example": ">gpio_set 1 0"
},
{ "cmd": "gpio_get", "descript": "get gpio pin status",
"parameters": [
{ "pin_num": "gpio pin number" }
],
"example": ">gpio_get 1"
},
{ "cmd": "i2c_cleanup", "descript": "release i2c bus" },
{ "cmd": "i2c_scan", "descript": "scan i2c devices" },
{ "cmd": "i2c_read", "descript": "perform read operation from i2c device",
"parameters": [
{ "device": "i2c slave device address" },
{ "length": "bytes to read" }
],
"example": ">i2c_read 0x20 1\n <succeed: 0xFF"
},
{ "cmd": "i2c_read_add", "descript": "perform read operation from i2c device with register address",
"parameters": [
{ "device": "i2c slave device address" },
{ "addr": "register address" },
{ "length": "bytes to read" }
],
"example": ">i2c_read_add 0x20 0 2\n <succeed: 0xFF 0x01"
},
{ "cmd": "i2c_write", "descript": "perform write operation to i2c device ",
"parameters": [
{ "device": "i2c slave device address" },
{ "data": "bytes to write" }
],
"example": ">i2c_write 0x20 0x01 0x02 0x03 ... 0x10"
},
{ "cmd": "i2c_write_add", "descript": "perform write operation to i2c device with register address",
"parameters": [
{ "device": "i2c slave device address" },
{ "addr": "register address" },
{ "data": "bytes to write" }
],
"example": ">i2c_write 0x20 0x12 0x01 0x02 0x03 ... 0x10"
},
{ "cmd": "spi_setup", "descript": "initial spi bus",
"parameters": [
{ "clk_freq": "ispi clock frequence" },
{ "sck_pin": "sck pin io number" },
{ "mosi_pin": "mosi pin io number" },
{ "miso_pin": "miso pin io number" },
{ "cs_pin": "cs pin io number, -1 if don't want spi controller toggle cs pin" },
{ "polarity": "polarity setting for spi bus" },
{ "phase": "phase setting for spi bus" }
],
"example": ">spi_setup 100000 2 3 0 1 0 1"
},
{ "cmd": "spi_cleanup", "descript": "release spi bus" },
{ "cmd": "spi_read", "descript": "perform read operation from spi device",
"parameters": [
{ "length": "bytes to read" }
],
"example": ">spi_read 3\n <succeed: 0xFF 0xAA 0xBB"
},
{ "cmd": "spi_write", "descript": "perform write operation to spi device ",
"parameters": [
{ "data": "bytes to write" }
],
"example": ">spi_write 0x01 0x02 0x03 ... 0x10"
},
{ "cmd": "spi_write_read", "descript": "perform write and then read to spi device ",
"parameters": [
{ "length": "bytes to read" },
{ "data": "bytes to write" }
],
"example": ">spi_write_read 3 0x01 0x02 0x03 ... 0x10\n <succeed: 0xFF 0xAA 0xBB"
},
]
##########################################################
def load_config():
global config
try:
f = open(config_file,'r')
config = json.load(f)
f.close()
except:
print('config file not exist, load default settings!')
save_config()
def save_config():
f = open(config_file,'w')
json.dump(config, f)
f.close()
def bytes_to_str(bytes):
return ",".join([f"0x{b:02x}" for b in bytes])
def str_to_int(str):
if str.startswith("0x"):
return int(str, 16)
else:
return int(str, 10)
def print_help():
for command in command_info:
print(f"{command['cmd']} - {command['descript']}")
if 'parameters' in command.keys():
print(' parameters:')
for parameter in command['parameters']:
parameter_name = list(parameter.keys())[0]
print(f" {parameter_name}: {parameter[parameter_name]}")
if 'example' in command.keys():
print(' example:')
print(f' {command['example']}')
if 'parameters' in command.keys() or 'example' in command.keys():
print('')
##########################################################
load_config()
i2c_controller = None
spi_controller = None
spi_cs_pin = None
gpio_dict = {}
print('Sonos Micro Controller V1.0')
print("PyLang", sys.version_info, ";", sys.implementation)
print('=======================================')
while True:
input_string = str(input())
args = input_string.split(" ")
if len(args) >= 1:
if args[0] == 'help' or args[0] == '?':
print_help()
elif args[0] == 'ping': # check if command line is available
print('OK')
elif args[0] == 'reset':
machine.reset()
elif args[0] == 'get_config':
print(config)
elif args[0] == 'set_config':
if args[1] != None and args[2] != None:
config[args[1]] = args[2]
save_config()
else:
print('invalid arguments for set command: ', input_string)
######################################################################
## GPIO control commands
######################################################################
elif args[0] == 'gpio_init':
# gpio_init 1 1 1 UP
try:
pin_num = str_to_int(args[1])
pin_direction = machine.Pin.OUT if str_to_int(args[2]) == 1 else machine.Pin.IN
init_status = str_to_int(args[3])
pull_control = None
if len(args) >= 5:
if args[4].upper().strip() == 'UP':
pull_control = machine.Pin.PULL_UP
elif args[4].upper().strip() == 'DOWN':
pull_control = machine.Pin.PULL_DOWN
else:
print('failed: invalid pull control ', args[4])
gpio_dict[pin_num] = machine.Pin(pin_num, pin_direction, pull_control)
if pin_direction == machine.Pin.OUT:
gpio_dict[pin_num].value(init_status)
print('succeed: ', gpio_dict[pin_num])
except Exception as e:
print('failed: ', e)
elif args[0] == 'gpio_set':
try:
pin_num = str_to_int(args[1])
pin_status = str_to_int(args[2])
if pin_num in gpio_dict:
gpio_dict[pin_num].value(pin_status)
print('succeed: ')
else:
print(f'failed: pin not initialized - {pin_num}')
except Exception as e:
print('failed: ', e)
elif args[0] == 'gpio_get':
try:
pin_num = str_to_int(args[1])
if pin_num in gpio_dict:
print('succeed: ', gpio_dict[pin_num].value())
else:
print(f'failed: pin not initialized - {pin_num}')
except Exception as e:
print('failed: ', e)
######################################################################
## I2C controller commands
######################################################################
elif args[0] == 'i2c_setup':
# ====== i2c controller setup ======
# PiPico: i2c_setup 100000 0 1 # 100000 - i2c clock frequence, 0 - sda pin io number, 1 - scl pin io number
# ESP32: i2c_setup 100000 8 9 # 100000 - i2c clock frequence, 8 - sda pin io number, 9 - scl pin io number
try:
if i2c_controller is not None:
i2c_controller = None
clk_freq = str_to_int(args[1])
sda_pin = str_to_int(args[2])
scl_pin = str_to_int(args[3])
i2c_controller = machine.I2C(0, sda=machine.Pin(sda_pin, machine.Pin.OPEN_DRAIN, machine.Pin.PULL_UP), scl=machine.Pin(scl_pin, machine.Pin.OUT, machine.Pin.PULL_UP), freq=clk_freq) # Frequency in Hz
print('succeed: ', i2c_controller)
except Exception as e:
print('failed: ', e)
elif args[0] == 'i2c_cleanup':
# ====== i2c controller setup ======
try:
if i2c_controller is not None:
i2c_controller = None
print('succeed: ')
except Exception as e:
print('failed: ', e)
elif args[0] == 'i2c_scan':
# ====== scan I2C devices ======
try:
devices = i2c_controller.scan()
if len(devices) == 0:
print('failed: ', "no I2C devices found")
else:
print('succeed: ', bytes_to_str(devices))
except Exception as e:
print('failed: ', e)
elif args[0] == 'i2c_read':
# ====== i2c data read ======
# i2c_read 0x20 1 # 0x20 - device_address, 1 - read length
try:
device = str_to_int(args[1])
length = str_to_int(args[2])
data = i2c_controller.readfrom(device, length)
print('succeed: ', data.hex(','))
except Exception as e:
print('failed: ', e)
elif args[0] == 'i2c_read_add':
# ====== i2c data read address ======
# i2c_read_add 0x20 0 2 # 0x20 - device_address, 0 - register address, 2 - read length
try:
device = str_to_int(args[1])
addr = str_to_int(args[2])
length = str_to_int(args[3])
data = i2c_controller.readfrom_mem(device, addr, length)
print('succeed: ', bytes_to_str(data))
except Exception as e:
print('failed: ', e)
elif args[0] == 'i2c_write':
# ====== i2c data write ======
# i2c_write 0x20 0x01 0x02 0x03 ... 0x10 # 0x20 - device_address, 0x01 ~ 0x10 - data to write to device
try:
device = str_to_int(args[1])
data = bytearray()
for i in range(2, len(args)):
data.append(str_to_int(args[i]))
i2c_controller.writeto(device, data)
print('succeed: ')
except Exception as e:
print('failed: ', e)
elif args[0] == 'i2c_write_add':
# ====== i2c data write address ======
# i2c_write_add 0x20 0x12 0x01 0x02 0x03 ... 0x10 # 0x20 - device_address, 0x12 - register address, 0x01 ~ 0x10 - data to write to device
try:
device = str_to_int(args[1])
addr = str_to_int(args[2])
data = bytearray()
for i in range(3, len(args)):
data.append(str_to_int(args[i]))
i2c_controller.writeto_mem(device, addr, data)
print('succeed: ')
except Exception as e:
print('failed: ', e)
######################################################################
## SPI controller commands
######################################################################
elif args[0] == 'spi_setup':
# ====== spi controller setup ======
# PiPico: spi_setup 100000 2 3 0 1 0 1 # param#1: 100000 - spi clock frequence
# # param#2: 2 - sck pin io number
# # param#3: 3 - mosi pin io number
# # param#4: 0 - miso pin io number
# # param#5: 1 - cs pin io number, -1 if don't want spi controller toggle cs pin
# # param#6: 0 - polarity setting
# # param#7: 1 - phase setting
#
try:
if spi_controller is not None:
spi_controller = None
if spi_cs_pin is not None:
spi_cs_pin = None
clk_freq = str_to_int(args[1])
spi_sck_pin = machine.Pin(str_to_int(args[2]))
spi_mosi_pin = machine.Pin(str_to_int(args[3]))
spi_miso_pin = machine.Pin(str_to_int(args[4]))
spi_cs_pin = machine.Pin(str_to_int(args[5]), machine.Pin.OUT)
polarity = str_to_int(args[6])
phase = str_to_int(args[7])
spi_controller = machine.SPI(0, baudrate=clk_freq, polarity=polarity, phase=phase, sck=spi_sck_pin, mosi=spi_mosi_pin, miso=spi_miso_pin)
print('succeed: ', spi_controller)
except Exception as e:
print('failed: ', e)
elif args[0] == 'spi_cleanup':
# ====== spi controller setup ======
try:
if spi_controller is not None:
spi_controller = None
if spi_cs_pin is not None:
spi_cs_pin = None
print('succeed: ')
except Exception as e:
print('failed: ', e)
elif args[0] == 'spi_read':
# ====== spi data read ======
# spi_read 3 # 3 - read length
try:
length = str_to_int(args[1])
# active cs pin if required
if spi_cs_pin is not None:
spi_cs_pin.value(0)
data = spi_controller.read(length)
# deactive cs pin if required
if spi_cs_pin is not None:
spi_cs_pin.value(1)
print('succeed: ', data.hex(','))
except Exception as e:
print('failed: ', e)
elif args[0] == 'spi_write':
# ====== spi data write ======
# spi_write 0x01 0x02 0x03 ... 0x10 # 0x01 ~ 0x10 - data to write to device
try:
data = bytearray()
for i in range(1, len(args)):
data.append(str_to_int(args[i]))
# active cs pin if required
if spi_cs_pin is not None:
spi_cs_pin.value(0)
spi_controller.write(data)
# deactive cs pin if required
if spi_cs_pin is not None:
spi_cs_pin.value(1)
print('succeed: ')
except Exception as e:
print('failed: ', e)
elif args[0] == 'spi_write_read':
# ====== spi data read ======
# spi_write_read 3 0x01 0x02 0x03 ... 0x10 # 3 - read length, 0x01 ~ 0x10 - data to write to device
try:
read_length = str_to_int(args[1])
# active cs pin if required
if spi_cs_pin is not None:
spi_cs_pin.value(0)
write_data = bytearray()
for i in range(2, len(args)):
write_data.append(str_to_int(args[i]))
spi_controller.write(write_data)
read_data = spi_controller.read(read_length)
# deactive cs pin if required
if spi_cs_pin is not None:
spi_cs_pin.value(1)
print('succeed: ', read_data.hex(','))
except Exception as e:
print('failed: ', e)
else:
print('unknow command: ', args[0])

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,91 @@
<?xml version='1.0' encoding='UTF-8'?>
<Library LVVersion="18008000">
<Property Name="NI.Lib.Icon" Type="Bin">'!#!!!!!!!)!"1!&amp;!!!-!%!!!@````]!!!!"!!%!!!(]!!!*Q(C=\&gt;7R=2MR%!81N=?"5Q&lt;/07RB7W!,&lt;'&amp;&lt;9+K1,7Q,&lt;)%N&lt;!NMA3X)DW?-RJ(JQ"I\%%Z,(@`BA#==ZB3RN;]28_,V7@PWW`:R`&gt;HV*SU_WE@\N_XF[3:^^TX\+2YP)D7K6;G-RV3P)R`ZS%=_]J'XP/5N&lt;XH,7V\SEJ?]Z#5P?=J4HP+5JTTFWS%0?=B$DD1G(R/.1==!IT.+D)`B':\B'2Z@9XC':XC':XBUC?%:HO%:HO&amp;R7QT0]!T0]!S0I4&lt;*&lt;)?=:XA-(]X40-X40-VDSGC?"GC4N9(&lt;)"D2,L;4ZGG?ZH%;T&gt;-]T&gt;-]T?.S.%`T.%`T.)^&lt;NF8J4@-YZ$S'C?)JHO)JHO)R&gt;"20]220]230[;*YCK=ASI2F=)1I.Z5/Z5PR&amp;)^@54T&amp;5TT&amp;5TQO&lt;5_INJ6Z;"[(H#&gt;ZEC&gt;ZEC&gt;Z$"(*ETT*ETT*9^B)HO2*HO2*(F.&amp;]C20]C2)GN4UE1:,.[:/+5A?0^NOS?UJ^3&lt;*\9B9GT@7JISVW7*NIFC&lt;)^:$D`5Q9TWE7)M@;V&amp;D,6;M29DVR]6#R],%GC47T9_/=@&gt;Z5V&gt;V57&gt;V5E&gt;V5(OV?^T[FTP?\`?YX7ZRP6\D=LH%_8S/U_E5R_-R$I&gt;$\0@\W/VW&lt;[_"\Y[X&amp;],0^^+,]T_J&gt;`J@_B_]'_.T`$KO.@I"XC-_N!!!!!!</Property>
<Property Name="NI.Lib.SourceVersion" Type="Int">402685952</Property>
<Property Name="NI.Lib.Version" Type="Str">1.0.0.0</Property>
<Property Name="NI.LV.All.SourceOnly" Type="Bool">false</Property>
<Item Name="Common" Type="Folder">
<Property Name="NI.SortType" Type="Int">3</Property>
<Item Name="Setting Parse.vi" Type="VI" URL="../Common/Setting Parse.vi"/>
<Item Name="Append Message Line.vi" Type="VI" URL="../Common/Append Message Line.vi"/>
<Item Name="Number Array To String.vi" Type="VI" URL="../Common/Number Array To String.vi"/>
</Item>
<Item Name="I2C" Type="Folder">
<Property Name="NI.SortType" Type="Int">3</Property>
<Item Name="I2CController.lvclass" Type="LVClass" URL="../I2C/I2CController.lvclass"/>
<Item Name="CP2112_I2C.lvclass" Type="LVClass" URL="../I2C/CP2112_I2C/CP2112_I2C.lvclass"/>
<Item Name="Micro_I2C.lvclass" Type="LVClass" URL="../I2C/Micro_I2C/Micro_I2C.lvclass"/>
<Item Name="I2C Controller UI.vi" Type="VI" URL="../I2C/I2C Controller UI.vi"/>
</Item>
<Item Name="SPI" Type="Folder">
<Property Name="NI.SortType" Type="Int">3</Property>
<Item Name="SPIController.lvclass" Type="LVClass" URL="../SPI/SPIController.lvclass"/>
<Item Name="Micro_SPI.lvclass" Type="LVClass" URL="../SPI/Micro_SPI/Micro_SPI.lvclass"/>
<Item Name="SPI Controller UI.vi" Type="VI" URL="../SPI/SPI Controller UI.vi"/>
</Item>
<Item Name="Tests" Type="Folder">
<Property Name="NI.SortType" Type="Int">3</Property>
<Item Name="I2C" Type="Folder">
<Item Name="ADS1115" Type="Folder">
<Item Name="ADS1115_AUTO_RANGE_I2C.vi" Type="VI" URL="../Tests/I2C/ADS1115/ADS1115_AUTO_RANGE_I2C.vi"/>
<Item Name="ADS1115_CONFIG_I2C.vi" Type="VI" URL="../Tests/I2C/ADS1115/ADS1115_CONFIG_I2C.vi"/>
<Item Name="ADS1115_I2C.vi" Type="VI" URL="../Tests/I2C/ADS1115/ADS1115_I2C.vi"/>
<Item Name="ADS1115_MEASURE.vi" Type="VI" URL="../Tests/I2C/ADS1115/ADS1115_MEASURE.vi"/>
</Item>
<Item Name="AD5272" Type="Folder">
<Item Name="AD5272_I2C.vi" Type="VI" URL="../Tests/I2C/AD5272/AD5272_I2C.vi"/>
<Item Name="AD5272 Write Register.vi" Type="VI" URL="../Tests/I2C/AD5272/AD5272 Write Register.vi"/>
<Item Name="Control Register Typedef.ctl" Type="VI" URL="../Tests/I2C/AD5272/Control Register Typedef.ctl"/>
<Item Name="Command Register Typedef.ctl" Type="VI" URL="../Tests/I2C/AD5272/Command Register Typedef.ctl"/>
</Item>
<Item Name="INA228" Type="Folder">
<Item Name="INA228 Read Bus Voltage.vi" Type="VI" URL="../Tests/I2C/INA228/INA228 Read Bus Voltage.vi"/>
<Item Name="INA228 Read Current.vi" Type="VI" URL="../Tests/I2C/INA228/INA228 Read Current.vi"/>
<Item Name="INA228 Read Register.vi" Type="VI" URL="../Tests/I2C/INA228/INA228 Read Register.vi"/>
<Item Name="INA228 Read Shunt Voltage.vi" Type="VI" URL="../Tests/I2C/INA228/INA228 Read Shunt Voltage.vi"/>
<Item Name="INA228 Read Temperature.vi" Type="VI" URL="../Tests/I2C/INA228/INA228 Read Temperature.vi"/>
<Item Name="INA228 Write Register.vi" Type="VI" URL="../Tests/I2C/INA228/INA228 Write Register.vi"/>
<Item Name="INA228 Write Configuration Register.vi" Type="VI" URL="../Tests/I2C/INA228/INA228 Write Configuration Register.vi"/>
<Item Name="INA228 Write ADC Configuration Register.vi" Type="VI" URL="../Tests/I2C/INA228/INA228 Write ADC Configuration Register.vi"/>
<Item Name="INA228 Write DIAG_ALRT Register Register.vi" Type="VI" URL="../Tests/I2C/INA228/INA228 Write DIAG_ALRT Register Register.vi"/>
<Item Name="INA228 Read DIAG_ALRT Register Register.vi" Type="VI" URL="../Tests/I2C/INA228/INA228 Read DIAG_ALRT Register Register.vi"/>
<Item Name="INA228 Register Typedef.ctl" Type="VI" URL="../Tests/I2C/INA228/INA228 Register Typedef.ctl"/>
<Item Name="INA228_I2C.vi" Type="VI" URL="../Tests/I2C/INA228/INA228_I2C.vi"/>
</Item>
<Item Name="MCP23017_I2C.vi" Type="VI" URL="../Tests/I2C/MCP23017_I2C.vi"/>
<Item Name="TCA9548A_I2C.vi" Type="VI" URL="../Tests/I2C/TCA9548A_I2C.vi"/>
<Item Name="TCS3472_I2C.vi" Type="VI" URL="../Tests/I2C/TCS3472_I2C.vi"/>
<Item Name="MCP4725_I2C.vi" Type="VI" URL="../Tests/I2C/MCP4725_I2C.vi"/>
<Item Name="FXL6408_I2C.vi" Type="VI" URL="../Tests/I2C/FXL6408_I2C.vi"/>
<Item Name="IS31FL3195_I2C.vi" Type="VI" URL="../Tests/I2C/IS31FL3195_I2C.vi"/>
<Item Name="I2C_Controller_GPIO.vi" Type="VI" URL="../Tests/I2C/I2C_Controller_GPIO.vi"/>
</Item>
<Item Name="SPI" Type="Folder">
<Item Name="ADS1220" Type="Folder">
<Item Name="ADS1220_SPI Read Register.vi" Type="VI" URL="../Tests/SPI/ADS1220/ADS1220_SPI Read Register.vi"/>
<Item Name="ADS1220_SPI Read Temperature.vi" Type="VI" URL="../Tests/SPI/ADS1220/ADS1220_SPI Read Temperature.vi"/>
<Item Name="ADS1220_SPI Read Voltage.vi" Type="VI" URL="../Tests/SPI/ADS1220/ADS1220_SPI Read Voltage.vi"/>
<Item Name="ADS1220_SPI Write Configuration Register 0.vi" Type="VI" URL="../Tests/SPI/ADS1220/ADS1220_SPI Write Configuration Register 0.vi"/>
<Item Name="ADS1220_SPI Write Configuration Register 1.vi" Type="VI" URL="../Tests/SPI/ADS1220/ADS1220_SPI Write Configuration Register 1.vi"/>
<Item Name="ADS1220_SPI Write Configuration Register 2.vi" Type="VI" URL="../Tests/SPI/ADS1220/ADS1220_SPI Write Configuration Register 2.vi"/>
<Item Name="ADS1220_SPI Write Configuration Register 3.vi" Type="VI" URL="../Tests/SPI/ADS1220/ADS1220_SPI Write Configuration Register 3.vi"/>
<Item Name="ADS1220_SPI Write Register.vi" Type="VI" URL="../Tests/SPI/ADS1220/ADS1220_SPI Write Register.vi"/>
<Item Name="ADS1220_SPI_Test.vi" Type="VI" URL="../Tests/SPI/ADS1220/ADS1220_SPI_Test.vi"/>
</Item>
<Item Name="ADS1256" Type="Folder">
<Item Name="ADS1256_SPI_Test.vi" Type="VI" URL="../Tests/SPI/ADS1256/ADS1256_SPI_Test.vi"/>
<Item Name="ADS1256_REGISTERS.ctl" Type="VI" URL="../Tests/SPI/ADS1256/ADS1256_REGISTERS.ctl"/>
<Item Name="ADS1256_COMMANDS.ctl" Type="VI" URL="../Tests/SPI/ADS1256/ADS1256_COMMANDS.ctl"/>
<Item Name="ADS1256_RATE.ctl" Type="VI" URL="../Tests/SPI/ADS1256/ADS1256_RATE.ctl"/>
<Item Name="ADS1256_GAIN.ctl" Type="VI" URL="../Tests/SPI/ADS1256/ADS1256_GAIN.ctl"/>
<Item Name="ADS1256 Write Register.vi" Type="VI" URL="../Tests/SPI/ADS1256/ADS1256 Write Register.vi"/>
<Item Name="ADS1256 Read Register.vi" Type="VI" URL="../Tests/SPI/ADS1256/ADS1256 Read Register.vi"/>
<Item Name="ADS1256 Write Command.vi" Type="VI" URL="../Tests/SPI/ADS1256/ADS1256 Write Command.vi"/>
<Item Name="ADS1256 Self Calibration.vi" Type="VI" URL="../Tests/SPI/ADS1256/ADS1256 Self Calibration.vi"/>
<Item Name="ADS1256 Read Single Conversion.vi" Type="VI" URL="../Tests/SPI/ADS1256/ADS1256 Read Single Conversion.vi"/>
<Item Name="ADS1256 Read Data.vi" Type="VI" URL="../Tests/SPI/ADS1256/ADS1256 Read Data.vi"/>
</Item>
<Item Name="DAC8830_SPI_Test.vi" Type="VI" URL="../Tests/SPI/DAC8830_SPI_Test.vi"/>
</Item>
</Item>
</Library>

Some files were not shown because too many files have changed in this diff Show More