CASP Python Libraries
This documentation primarily focuses on the usage of functions from 'casp', 'casp_be', 'casp_gpio' and 'casp_udio' namespaces.
Loading...
Searching...
No Matches
casp_gpio.py
1"""! This module defines functions to communicate with target hardware (i.e micro-controller board, SBC etc.) that is programmed with CASP GPIO protocol. Both serial and ethernet communication interfaces are supported. All functions in this module uses global object \ref _casp_process.casp_gpio_proc from \ref _casp_process.py module.
2"""
3
4import importlib
5from _casp_process import casp_gpio_proc
6import casp_utils as utils
7
8def Init():
9 """! Initializes CASP GPIO subprocess. This is the first function to be called before using any other function in this module.
10 """
11 casp_gpio_proc.init()
12
13def Close():
14 """! Terminates CASP GPIO subprocess and releases the resources.
15 """
16 casp_gpio_proc.close()
17
18def ConnectSerial(serial_port: str="COM3", enable_dtr: bool=True, send_recv_delay: int=1, baud: int=115200) -> bool:
19 """! Initializes CASP GPIO connection process with the target over serial port. Performs initial protocol hand shake and keep things ready for data read/write operations.
20
21 User can use \ref SerialPorts() to know available serial ports on the device.
22
23 Arguments:
24
25 serial_port: Set serial port to which device is connected.
26
27 enable_dtr: Set to True when serial data terminal ready (DTR) control signal is to be used. Default value is True. Set to False if device is not detectable (generally on old hardware).
28
29 send_recv_delay: Set send receive time delay in milli seconds. Generally set between 1 to 5 msec. Smaller values reduces latency at the cost of increased CPU utilization and vice versa. Default value is 1 msec.
30
31 baud: Set baud rate in bits per second. Default value is 115200 bps.
32
33 Return Value:
34
35 Returns True if connection is successful. Else returns False.
36 """
37 print(f"CASP: Connecting to serial device...")
38 response = casp_gpio_proc.write("casp.connect.serial", [serial_port, str(baud), str(int(enable_dtr)), str(send_recv_delay), "0", "0"])
39 if len(response) == 0:
40 response = "CASP: Empty response."
41 strs = utils.SplitString(response.strip())
42 print(f"CASP: {strs[0]}")
43 ack:bool = False
44 if len(strs) > 1:
45 ack = bool(strs[1])
46 return ack
47
48def ConnectEth(ip: str, port: int=42001, tcp: bool=False, send_recv_delay: int=1) -> bool:
49 """! Initializes CASP GPIO connection process with the target over ethernet. Performs initial protocol hand shake and keep things ready for data read/write operations.
50
51 Arguments:
52
53 ip: Set IP address of the target device. Both IP4 and IP6 are supported.
54
55 port: Set port number of the target device. Mostly, better leave it to default.
56
57 tcp: Set to True when TCP protocol is to be used else UDP protocol will be used during communication.
58
59 send_recv_delay: Set send receive time delay in milli seconds. Generally set between 1 to 5 msec. Smaller values reduces latency at the cost of increased CPU utilization and vice versa. Default value is 1 msec.
60
61 Return Value:
62
63 Returns True if connection is successful. Else returns False.
64 """
65 print(f"CASP: Connecting to ethernet device...")
66 response = casp_gpio_proc.write("casp.connect.eth", [ip, str(port), str(int(tcp)), str(send_recv_delay), "0", "0"])
67 if len(response) == 0:
68 response = "CASP: Empty response."
69 strs = utils.SplitString(response.strip())
70 print(f"CASP: {strs[0]}")
71 ack:bool = False
72 if len(strs) > 1:
73 ack = bool(strs[1])
74 return ack
75
77 """! Displays available serial ports on the host. User can use this function before using the \ref ConnectSerial() function.
78 """
79 response = casp_gpio_proc.write("casp.serial.ports")
80 if len(response) == 0:
81 response = "Empty response."
82 print(f"CASP: {response.strip()}")
83
85 """! Opens 'CASP User Data' directory from user's Documents folder.
86 """
87 response = casp_gpio_proc.write("casp.dir.user_data")
88 print(f"CASP: {response.strip()}")
89
91 """! Opens CASP tutorials directory.
92 """
93 response = casp_gpio_proc.write("casp.dir.tutorials")
94 print(f"CASP: {response.strip()}")
95
97 """! Opens CASP examples directory.
98 """
99 response = casp_gpio_proc.write("casp.dir.examples")
100 print(f"CASP: {response.strip()}")
101
103 """! Opens CASP Python Libraries documentation.
104 """
105 response = casp_proc.write("casp.help.python")
106
107def Read(pin_name: str) -> int:
108 """! Reads target GPIO raw pin value and returns the value as an integer.
109
110 Arguments:
111
112 pin_name: Pin number/name from the selected module in 'casp_gpio_pins' folder from which the value is to be read.
113
114 Return Value:
115
116 Returns the pin value received from the target as an integer.
117 """
118 response = casp_gpio_proc.write("casp.read", [pin_name])
119 if len(response) == 0:
120 response = "Empty response."
121 strs = utils.SplitString(response.strip())
122 #print(f"CASP: {strs[0]}")
123 val:int = 0
124 if len(strs) > 1:
125 val = int(strs[1])
126 return val
127
128def Write(pin_name: str, value: int) -> bool:
129 """! Writes value to the target's GPIO pin and returns whether the write operation is successful or not.
130
131 Arguments:
132
133 pin_name: Pin number/name from the selected module in 'casp_gpio_pins' folder to which the value is written.
134
135 value: Value to write.
136
137 Return Value:
138
139 Returns whether the write operation is successful or not.
140 """
141 response = casp_gpio_proc.write("casp.write", [pin_name, str(value)])
142 if len(response) == 0:
143 response = "Empty response."
144 strs = utils.SplitString(response.strip())
145 #print(f"CASP: {strs[0]}")
146 ack:bool = False
147 if len(strs) > 1:
148 ack = bool(strs[1])
149 return ack
150
FolderExamples()
Opens CASP examples directory.
Definition casp_gpio.py:96
FolderUserData()
Opens 'CASP User Data' directory from user's Documents folder.
Definition casp_gpio.py:84
SerialPorts()
Displays available serial ports on the host.
Definition casp_gpio.py:76
HelpPython()
Opens CASP Python Libraries documentation.
Definition casp_gpio.py:102
FolderTutorials()
Opens CASP tutorials directory.
Definition casp_gpio.py:90
bool ConnectSerial(str serial_port="COM3", bool enable_dtr=True, int send_recv_delay=1, int baud=115200)
Initializes CASP GPIO connection process with the target over serial port.
Definition casp_gpio.py:18
bool Write(str pin_name, int value)
Writes value to the target's GPIO pin and returns whether the write operation is successful or not.
Definition casp_gpio.py:128
Init()
Initializes CASP GPIO subprocess.
Definition casp_gpio.py:8
Close()
Terminates CASP GPIO subprocess and releases the resources.
Definition casp_gpio.py:13
int Read(str pin_name)
Reads target GPIO raw pin value and returns the value as an integer.
Definition casp_gpio.py:107
bool ConnectEth(str ip, int port=42001, bool tcp=False, int send_recv_delay=1)
Initializes CASP GPIO connection process with the target over ethernet.
Definition casp_gpio.py:48