|
1 | 1 | """Test program how to use library without installing the library, |
2 | 2 | DO NOT USE THIS FILE, USE EXAMPLES INSTEAD""" |
3 | 3 |
|
| 4 | +from timeit import default_timer as timer |
| 5 | + |
| 6 | +import logging |
4 | 7 | import sys |
5 | 8 | import asyncio |
6 | 9 |
|
| 10 | +from science_mode_4.device_i24 import DeviceI24 |
7 | 11 | from science_mode_4.device_p24 import DeviceP24 |
| 12 | +from science_mode_4.dyscom.ads129x.ads129x_config_register_1 import Ads129xOutputDataRate, Ads129xPowerMode |
| 13 | +from science_mode_4.dyscom.dyscom_get_operation_mode import PacketDyscomGetAckOperationMode |
| 14 | +from science_mode_4.dyscom.dyscom_send_live_data import PacketDyscomSendLiveData |
| 15 | +from science_mode_4.dyscom.dyscom_types import DyscomFilterType, DyscomGetType, DyscomInitParams, DyscomPowerModulePowerType, DyscomPowerModuleType, DyscomSignalType |
| 16 | +from science_mode_4.protocol.commands import Commands |
| 17 | +from science_mode_4.protocol.types import ResultAndError |
| 18 | +from science_mode_4.utils import logger |
8 | 19 | from science_mode_4.utils.serial_port_connection import SerialPortConnection |
| 20 | +from science_mode_4.utils.usb_connection import UsbConnection |
9 | 21 |
|
10 | 22 |
|
11 | 23 |
|
12 | 24 | async def main() -> int: |
13 | 25 | """Main function""" |
14 | 26 |
|
15 | | - devices = SerialPortConnection.list_science_mode_device_ports() |
16 | | - connection = SerialPortConnection(devices[0].device) |
17 | | - # devices = UsbConnection.list_science_mode_devices() |
18 | | - # connection = UsbConnection(devices[0]) |
| 27 | + logger().disabled = True |
| 28 | + |
| 29 | + logger().setLevel(logging.DEBUG) |
| 30 | + # devices = SerialPortConnection.list_science_mode_device_ports() |
| 31 | + # connection = SerialPortConnection(devices[0].device) |
| 32 | + devices = UsbConnection.list_science_mode_devices() |
| 33 | + connection = UsbConnection(devices[0]) |
19 | 34 | # connection = NullConnection() |
20 | 35 | connection.open() |
21 | 36 |
|
22 | | - device = DeviceP24(connection) |
| 37 | + device = DeviceI24(connection) |
23 | 38 | await device.initialize() |
24 | | - general = device.get_layer_general() |
25 | | - print(f"Device id: {general.device_id}") |
26 | | - print(f"Firmware version: {general.firmware_version}") |
27 | | - print(f"Science mode version: {general.science_mode_version}") |
| 39 | + # get dyscom layer to call dyscom level commands |
| 40 | + dyscom = device.get_layer_dyscom() |
| 41 | + |
| 42 | + # call enable measurement power module for measurement |
| 43 | + await dyscom.power_module(DyscomPowerModuleType.MEASUREMENT, DyscomPowerModulePowerType.SWITCH_ON) |
| 44 | + # call init with 4k sample rate and enable signal types |
| 45 | + init_params = DyscomInitParams() |
| 46 | + init_params.filter = DyscomFilterType.PREDEFINED_FILTER_2 |
| 47 | + init_params.signal_type = [DyscomSignalType.BI, DyscomSignalType.EMG_1,\ |
| 48 | + DyscomSignalType.EMG_2, DyscomSignalType.BREATHING, DyscomSignalType.TEMPERATURE] |
| 49 | + init_params.register_map_ads129x.config_register_1.output_data_rate = Ads129xOutputDataRate.HR_MODE_4_KSPS__LP_MODE_2_KSPS |
| 50 | + init_params.register_map_ads129x.config_register_1.power_mode = Ads129xPowerMode.HIGH_RESOLUTION |
| 51 | + await dyscom.init(init_params) |
| 52 | + |
| 53 | + # start dyscom measurement |
| 54 | + await dyscom.start() |
| 55 | + |
| 56 | + start_time = timer() |
| 57 | + total_count = 0 |
| 58 | + |
| 59 | + # loop for some time |
| 60 | + for x in range(1000): |
| 61 | + # check operation mode from time to time, this function is not waiting for response |
| 62 | + # so we have to handle it by ourself later |
| 63 | + if x % 100 == 0: |
| 64 | + dyscom.send_get_operation_mode() |
| 65 | + |
| 66 | + live_data_counter = 0 |
| 67 | + while True: |
| 68 | + # process all available packages |
| 69 | + ack = dyscom.packet_buffer.get_packet_from_buffer(live_data_counter == 0) |
| 70 | + if ack: |
| 71 | + # because there are multiple get commands, we need to additionally check kind, |
| 72 | + # which is always associated DyscomGetType |
| 73 | + if ack.command == Commands.DL_GET_ACK and ack.kind == DyscomGetType.OPERATION_MODE: |
| 74 | + om_ack: PacketDyscomGetAckOperationMode = ack |
| 75 | + print(f"Operation mode {om_ack.operation_mode.name}") |
| 76 | + # check if measurement is still active |
| 77 | + if om_ack.result_error != ResultAndError.NO_ERROR: |
| 78 | + break |
| 79 | + elif ack.command == Commands.DL_SEND_LIVE_DATA: |
| 80 | + live_data_counter += 1 |
| 81 | + total_count += 1 |
| 82 | + |
| 83 | + sld: PacketDyscomSendLiveData = ack |
| 84 | + if sld.status_error: |
| 85 | + print(f"SendLiveData status error {sld.samples}") |
| 86 | + break |
| 87 | + |
| 88 | + else: |
| 89 | + # print(f"Live data acknowledges per iteration {live_data_counter}") |
| 90 | + break |
| 91 | + |
| 92 | + # await asyncio.sleep(0.01) |
| 93 | + |
| 94 | + # print stats |
| 95 | + end_time = timer() |
| 96 | + print(f"Samples: {total_count}, duration: {end_time - start_time}, sample rate: {total_count / (end_time - start_time)}") |
| 97 | + |
| 98 | + # stop measurement |
| 99 | + await dyscom.stop() |
| 100 | + # turn power module off |
| 101 | + await dyscom.power_module(DyscomPowerModuleType.MEASUREMENT, DyscomPowerModulePowerType.SWITCH_OFF) |
28 | 102 |
|
29 | 103 | connection.close() |
30 | 104 |
|
|
0 commit comments