Browse Source

Ajout fichier

master
Manfraid 3 years ago
parent
commit
369a6e788d
1 changed files with 184 additions and 0 deletions
  1. 184
    0
      RPi/GPIO.py

+ 184
- 0
RPi/GPIO.py View File

@@ -0,0 +1,184 @@
1
+RPI_REVISION = 1
2
+VERSION = 1
3
+
4
+BOARD = 0
5
+BCM = 1
6
+
7
+IN = 0
8
+OUT = 1
9
+
10
+INPUT = 0
11
+OUTPUT = 1
12
+SPI = 2
13
+I2C = 3
14
+HARD_PWM = 4
15
+SERIAL = 5
16
+UNKNOWN = -1
17
+
18
+PUD_DOWN = 0
19
+PUD_UP = 1
20
+PUD_OFF = -1
21
+
22
+LOW = 0
23
+HIGH = 1
24
+
25
+FALLING = 0
26
+RISING = 1
27
+BOTH = 2
28
+
29
+
30
+debug = False
31
+gpiomode = MODE_UNKNOWN;
32
+
33
+pintogpio = [
34
+    [-1, -1, -1, 0, -1, 1, -1, 4, 14, -1, 15, 17, 18, 21, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7],
35
+    [-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7]
36
+]
37
+
38
+direction = [-1 for _ in range(0,54)]
39
+state = [False for _ in range(0,54)]
40
+
41
+def setmode(mode):
42
+    """
43
+    There are two ways of numbering the IO pins on a Raspberry Pi within RPi.GPIO. The first is using the BOARD numbering system. This refers to the pin numbers on the P1 header of the Raspberry Pi board. The advantage of using this numbering system is that your hardware will always work, regardless of the board revision of the RPi. You will not need to rewire your connector or change your code.
44
+    The second numbering system is the BCM numbers. This is a lower level way of working - it refers to the channel numbers on the Broadcom SOC. You have to always work with a diagram of which channel number goes to which pin on the RPi board. Your script could break between revisions of Raspberry Pi boards.
45
+    To specify which you are using using (mandatory):
46
+    :param mode:
47
+    :return:
48
+    """
49
+    global gpiomode
50
+    if mode in [BOARD,BCM]:
51
+        gpiomode = mode
52
+    return
53
+
54
+def setup(channel, inout, pull_up_down=PUD_OFF):
55
+    """
56
+    You need to set up every channel you are using as an input or an output. To configure a channel as an input:
57
+    :param channel:
58
+    :param mode:
59
+    :param initial:
60
+    :param pull_up_down:
61
+    :return:
62
+    """
63
+    global direction
64
+    if gpiomode == MODE_UNKNOWN:
65
+        print("Set mode first!")
66
+        raise Exception('InvalidModeException')
67
+    elif gpiomode == BOARD:
68
+        channel = pintogpio[REV][channel]
69
+    direction[channel] = inout
70
+    if debug:
71
+        print(direction)
72
+        print(state)
73
+    return None
74
+
75
+
76
+def gpio_function(pin):
77
+    """
78
+    Shows the function of a GPIO channel.
79
+    will return a value from: GPIO.INPUT, GPIO.OUTPUT, GPIO.SPI, GPIO.I2C, GPIO.HARD_PWM, GPIO.SERIAL, GPIO.UNKNOWN
80
+    :param pin:
81
+    :return: GPIO.INPUT, GPIO.OUTPUT, GPIO.SPI, GPIO.I2C, GPIO.HARD_PWM, GPIO.SERIAL, GPIO.UNKNOWN
82
+    """
83
+    return None
84
+
85
+def cleanup():
86
+    """
87
+    At the end any program, it is good practice to clean up any resources you might have used. This is no different with RPi.GPIO.
88
+    By returning all channels you have used back to inputs with no pull up/down, you can avoid accidental damage to your RPi by shorting out the pins.
89
+    Note that this will only clean up GPIO channels that your script has used.
90
+    :param channel: It is possible that you only want to clean up one channel, leaving some set up when your program exits
91
+    :return:
92
+    """
93
+    global direction, state
94
+    direction = [-1 for _ in range(0,54)]
95
+    state = [0 for _ in range(0,54)]
96
+    if debug:
97
+        print(direction)
98
+        print(state)
99
+    return None
100
+
101
+def input(channel):
102
+    """
103
+    To read the value of a GPIO pin:
104
+    :param channel:
105
+    :return:
106
+    """
107
+    if gpiomode == BOARD:
108
+        channel = pintogpio[REV][channel]
109
+    return state[channel]
110
+
111
+def output(channel, mode):
112
+    """
113
+    To set the output state of a GPIO pin:
114
+    :param channel:
115
+    :return:
116
+    """
117
+    global state
118
+    if gpiomode == BOARD:
119
+        channel = pintogpio[REV][channel]
120
+    if direction[channel] is not OUT:
121
+        raise Exception('NotAnOutputException')
122
+    else:
123
+        state[channel] = mode
124
+    if debug:
125
+        print(state[channel])
126
+    return None
127
+
128
+
129
+def wait_for_edge(channel, edge_type):
130
+    """
131
+    The wait_for_edge() function is designed to block execution of your program until an edge is detected.
132
+    :param channel:
133
+    :param edge_type:
134
+    :return:
135
+    """
136
+    pass
137
+
138
+
139
+def add_event_detect(channel, edge_type, callback=None, bouncetime=0):
140
+    """
141
+    The event_detected() function is designed to be used in a loop with other things, but unlike polling it is not going to miss the change in state of an input while the CPU is busy working on other things.
142
+    This could be useful when using something like Pygame or PyQt where there is a main loop listening and responding to GUI events in a timely basis.
143
+    :param channel:
144
+    :param edge_type:
145
+    :return:
146
+    """
147
+    pass
148
+
149
+
150
+def add_event_callback(channel, callback, bouncetime=0):
151
+    pass
152
+
153
+
154
+def remove_event_detect(channel):
155
+    """
156
+    If for some reason, your program no longer wishes to detect edge events, it is possible to stop them
157
+    :param channel:
158
+    :return:
159
+    """
160
+    pass
161
+
162
+
163
+class PWM(object):
164
+    """
165
+    :param channel:
166
+    :param frequency:
167
+    To create a PWM instance:
168
+    :return:
169
+    """
170
+    def __init__(self,channel, frequency):
171
+        pass
172
+
173
+    def start(self,dc=10000):
174
+        pass
175
+
176
+    def ChangeFrequency(self,freq):
177
+        pass
178
+
179
+    def ChangeDutyCycle(self,dc):
180
+        pass
181
+
182
+    def stop(self):
183
+        pass
184
+

Loading…
Cancel
Save