1
+ """
2
+ Python C Extension Module WiringPi OOK
3
+
4
+ Send OOK pulse train to digital GPIO using wiringPi C library
5
+
6
+ See: https://github.com/latchdevel/raspicode
7
+
8
+ Copyright (c) 2022-2024 Jorge Rivera. All right reserved.
9
+ License GNU Lesser General Public License v3.0.
10
+ """
11
+
12
+ __version__ = "1.1"
13
+ __author__ = 'Jorge Rivera'
14
+ __doc__ = 'Python C Extension Module to send OOK pulse train to digital GPIO using wiringPi library.\n See: https://github.com/latchdevel/raspicode'
15
+
16
+ # Force error if not build Python C Extension Module "wiringpiook"
17
+ try :
18
+ from wiringpiook import wiringpiook as _wiringpiook_c
19
+ except :
20
+ raise ("Must build wiringpiook module." )
21
+
22
+ # Remove wiringpiook from namespace
23
+ if 'wiringpiook' in dir ():
24
+ del wiringpiook
25
+
26
+ # TX limits from 'wiringpiook.c'
27
+ MAX_PULSE_LENGTH = _wiringpiook_c .MAX_PULSE_LENGTH # Max pulse length (microseconds)
28
+ MAX_PULSE_COUNT = _wiringpiook_c .MAX_PULSE_COUNT # Max pulse count
29
+ MAX_TX_TIME = _wiringpiook_c .MAX_TX_TIME # Max TX time (milliseconds)
30
+ MAX_TX_REPEATS = _wiringpiook_c .MAX_TX_REPEATS # Max TX repeats
31
+ DEFAULT_REPEATS = _wiringpiook_c .DEFAULT_REPEATS # Default TX repeats
32
+
33
+ # Error return codes for tx(bcm_gpio, pulse_list, repeats = DEFAULT_REPEATS)
34
+ _NO_ERROR = 0
35
+ _ERROR_UNKNOWN = - 1
36
+ _ERROR_INVALID_PULSE_COUNT = - 2
37
+ _ERROR_PULSETRAIN_OOD = - 3
38
+ _ERROR_INVALID_PULSE_LENGTH = - 4
39
+ _ERROR_INVALID_TX_TIME = - 5
40
+
41
+ # Redefines the Python function tx() to add parameter checks and basic docstrings
42
+ def tx (bcm_gpio : int , pulse_list : list , repeats : int = DEFAULT_REPEATS ) -> int :
43
+ """
44
+ Transmission of the pulse train to a GPIO
45
+
46
+ Parameters:
47
+ - bcm_gpio: Native Broadcom GPIO number from 2 to 27
48
+ - pulse_list: List of integers, whose length must be even
49
+ - repeats: Number of transmission repeats
50
+
51
+ Returns:
52
+ - Positive integer as transmission time in milliseconds or negative error code
53
+ """
54
+
55
+ if not isinstance (bcm_gpio ,int ):
56
+ raise TypeError ("bcm_gpio must be an integer number." )
57
+
58
+ if not isinstance (pulse_list ,list ):
59
+ raise TypeError ("pulse_list must be a list." )
60
+
61
+ if not isinstance (repeats ,int ):
62
+ raise TypeError ("repeats must be an integer number." )
63
+
64
+ if ((bcm_gpio < 2 ) or (bcm_gpio > 27 )):
65
+ raise ValueError ("invalid gpio number, must be >=2 and <=27." )
66
+
67
+ if ((repeats < 1 ) or (repeats > MAX_TX_REPEATS )):
68
+ raise ValueError ("invalid repeats, must be >=1 and <={}." .format (MAX_TX_REPEATS ))
69
+
70
+ for pulse in pulse_list :
71
+ if not isinstance (pulse ,int ):
72
+ raise TypeError ("list items must be integer numbers." )
73
+
74
+ if ((len (pulse_list )) < 1 or (len (pulse_list )) > MAX_PULSE_COUNT ):
75
+ return _ERROR_INVALID_PULSE_COUNT
76
+
77
+ if (len (pulse_list ) % 2 != 0 ):
78
+ return _ERROR_PULSETRAIN_OOD
79
+
80
+ tx_time = 0
81
+
82
+ for pulse in pulse_list :
83
+ if ((pulse > 0 ) and (pulse < MAX_PULSE_LENGTH )):
84
+ tx_time = tx_time + pulse
85
+ if (tx_time * repeats > MAX_TX_TIME * 1000 ):
86
+ return _ERROR_INVALID_TX_TIME
87
+ else :
88
+ return _ERROR_INVALID_PULSE_LENGTH
89
+
90
+ return _wiringpiook_c .tx (bcm_gpio , pulse_list , repeats )
0 commit comments