11"""Test program how to use library without installing the library,
22DO NOT USE THIS FILE, USE EXAMPLES INSTEAD"""
33
4+ from timeit import default_timer as timer
5+
46import logging
57import sys
68import asyncio
79
810from science_mode_4 .device_i24 import DeviceI24
911from 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
1018from science_mode_4 .utils import logger
1119from science_mode_4 .utils .serial_port_connection import SerialPortConnection
1220from science_mode_4 .utils .usb_connection import UsbConnection
1624async def main () -> int :
1725 """Main function"""
1826
19- # Serial
20- # 2025-04-22 15:38 - DEBUG - Outgoing data, length: 12, bytes: F0 81 55 81 59 81 4E 81 0C 04 3E 0F
21- # 2025-04-22 15:38 - DEBUG - Incoming data, length: 13, bytes: F0 81 55 81 58 81 D1 81 0A 04 43 00 0F
22- # USB
23- # 2025-04-22 15:45 - DEBUG - Outgoing data, length: 12, bytes: F0 81 55 81 59 81 4E 81 0C 04 3E 0F
24- # 2025-04-22 15:45 - DEBUG - Incoming data, length: 13, bytes: F0 81 55 81 58 81 D1 81 0A 04 43 00 0F
27+ logger ().disabled = True
28+
2529 logger ().setLevel (logging .DEBUG )
2630 # devices = SerialPortConnection.list_science_mode_device_ports()
2731 # connection = SerialPortConnection(devices[0].device)
@@ -32,10 +36,69 @@ async def main() -> int:
3236
3337 device = DeviceI24 (connection )
3438 await device .initialize ()
35- general = device .get_layer_general ()
36- print (f"Device id: { general .device_id } " )
37- print (f"Firmware version: { general .firmware_version } " )
38- 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 )
39102
40103 connection .close ()
41104
0 commit comments