GeezerVison is showing github.com — Added support for different HW revisions · LeoJr2015/python@2b3b2f3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: GPIO_GUI/gpio.py +72-47Lines changed: 72 additions & 47 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,6 +45,7 @@ def get(self): | 45 | 45 | """Returns the current state of the LED""" | 46 | 46 | return self.state | 47 | 47 |
| | 48 | +## Future Functionality | 48 | 49 | ##class gpioEdit(tkSimpleDialog.Dialog): | 49 | 50 | ## """Dialog to be expanded to support advanced gpio features like | 50 | 51 | ## - Pull Up / Pull Down Resistor Config | @@ -79,7 +80,7 @@ class GPIO(Frame): | 79 | 80 | def __init__(self,parent,pin=0,name=None,**kw): | 80 | 81 | self.pin = pin | 81 | 82 | if name == None: | 82 | | - self.name = "GPIO %s" % (str(self.pin)) | | 83 | + self.name = "GPIO %02d" % (self.pin) | 83 | 84 | Frame.__init__(self,parent,width=150,height=20,relief=SUNKEN,bd=1,padx=5,pady=5) | 84 | 85 | ##Future capability | 85 | 86 | ##self.bind('<Double-Button-1>', lambda e, s=self: self._configurePin(e.y)) | @@ -95,14 +96,15 @@ def __init__(self,parent,pin=0,name=None,**kw): | 95 | 96 | self.mode_sel.grid(column=1,row=0) | 96 | 97 | self.set_state.grid(column=2,row=0) | 97 | 98 | self.current_mode = StringVar() | 98 | | - self.lblCurrentMode = Label(self,textvariable=self.current_mode) | 99 | | - self.lblCurrentMode.grid(column=1,row=1) | 100 | 99 | self.led.grid(column=3,row=0) | 101 | 100 |
| 102 | 101 | self.set_state.config(state=DISABLED) | 103 | | - function = self.updateCurrentFunction() | | 102 | + function = self.getPinFunctionName() | 104 | 103 | if function not in ['Input','Output']: | | 104 | + self.mode_sel.delete(0,'end') | | 105 | + self.mode_sel.insert(0,function) | 105 | 106 | self.mode_sel['state'] = DISABLED | | 107 | + | 106 | 108 |
| 107 | 109 | ## def _configurePin(self, y): | 108 | 110 | ## """Future capability to setup pull up/down""" | @@ -125,9 +127,9 @@ def setMode(self): | 125 | 127 | self.set_state.config(state=NORMAL) | 126 | 128 | pi.setup(self.pin,pi.OUT) | 127 | 129 | self.updateInput() | 128 | | - self.updateCurrentFunction() | 129 | 130 |
| 130 | | - def getPinFunctionName(self,pin): | | 131 | + def getPinFunctionName(self): | | 132 | + pin = self.pin | 131 | 133 | functions = {pi.IN:'Input', | 132 | 134 | pi.OUT:'Output', | 133 | 135 | pi.I2C:'I2C', | @@ -136,15 +138,6 @@ def getPinFunctionName(self,pin): | 136 | 138 | pi.SERIAL:'Serial', | 137 | 139 | pi.UNKNOWN:'Unknown'} | 138 | 140 | return functions[pi.gpio_function(pin)] | 139 | | - | 140 | | - | 141 | | - | 142 | | - def setName(self,name=None): | 143 | | - """Sets a new name for the GPIO port | 144 | | - setName("Hot Tub")""" | 145 | | - if name == None: | 146 | | - name = "GPIO ",str(self.pin) | 147 | | - self.Label.config(text=name) | 148 | 141 |
| 149 | 142 | ## Future Functionality | 150 | 143 | ## def setPullUp(self,pullup): | @@ -175,11 +168,6 @@ def updateInput(self): | 175 | 168 | state = pi.input(self.pin) | 176 | 169 | self.state = state | 177 | 170 | self.updateLED() | 178 | | - def updateCurrentFunction(self): | 179 | | - pinFunction = self.getPinFunctionName(self.pin) | 180 | | - self.current_mode.set(pinFunction) | 181 | | - return pinFunction | 182 | | - ##print("GPIO %s is an %s" % (self.pin,pinFunction)) | 183 | 171 |
| 184 | 172 |
| 185 | 173 | class App(Frame): | @@ -188,35 +176,11 @@ def __init__(self,parent=None, **kw): | 188 | 176 | self.parent = parent | 189 | 177 | pi.setmode(pi.BCM) | 190 | 178 | self.ports = [] | 191 | | - ############################################################ | 192 | | - ### UPDATE gpio TO SELECT WHICH GPIO YOU WISH TO CONTROL ### | 193 | | - ### Format is (BCM Pin Number, Row, Column) ### | 194 | | - ### Row and Column set where each widget should be ### | 195 | | - ### positioned in the app. ### | 196 | | - ############################################################ | 197 | | - gpio = ((2,0,0), | 198 | | - (3,1,0), | 199 | | - (4,2,0), | 200 | | - (7,3,0), | 201 | | - (8,4,0), | 202 | | - (9,5,0), | 203 | | - (10,6,0), | 204 | | - (11,7,0), | 205 | | - (14,8,0), | 206 | | - (15,0,1), | 207 | | - (17,1,1), | 208 | | - (18,2,1), | 209 | | - (22,3,1), | 210 | | - (23,4,1), | 211 | | - (24,5,1), | 212 | | - (25,6,1), | 213 | | - (27,7,1)) | 214 | | - num_of_gpio = len(gpio) | 215 | | - #################################################################### | 216 | | - col = 0 | | 179 | + ## Get the RPI Hardware dependant list of GPIO | | 180 | + gpio = self.getRPIVersionGPIO() | 217 | 181 | for num,(p,r,c) in enumerate(gpio): | 218 | 182 | self.ports.append(GPIO(self,pin=p)) | 219 | | - self.ports[col-1].grid(row=r,column=c) | | 183 | + self.ports[-1].grid(row=r,column=c) | 220 | 184 | self.update() | 221 | 185 |
| 222 | 186 | def onClose(self): | @@ -238,6 +202,66 @@ def update(self): | 238 | 202 | """Runs every 100ms to update the state of the GPIO inputs""" | 239 | 203 | self.readStates() | 240 | 204 | self._timer = self.after(100,self.update) | | 205 | + def getRPIVersionGPIO(self): | | 206 | + """Returns the GPIO hardware config for different Pi versions | | 207 | + Currently supports layout 1 and 3""" | | 208 | + gpio = ((2,0,0), | | 209 | + (3,1,0), | | 210 | + (4,2,0), | | 211 | + (7,3,0), | | 212 | + (8,4,0), | | 213 | + (9,5,0), | | 214 | + (10,6,0), | | 215 | + (11,7,0), | | 216 | + (14,8,0), | | 217 | + (15,0,1), | | 218 | + (17,1,1), | | 219 | + (18,2,1), | | 220 | + (22,3,1), | | 221 | + (23,4,1), | | 222 | + (24,5,1), | | 223 | + (25,6,1), | | 224 | + (27,7,1)) | | 225 | + gpio3 = ((2,0,0), | | 226 | + (3,1,0), | | 227 | + (4,2,0), | | 228 | + (17,3,0), | | 229 | + (27,4,0), | | 230 | + (22,5,0), | | 231 | + (10,6,0), | | 232 | + (9,7,0), | | 233 | + (11,8,0), | | 234 | + (5,9,0), | | 235 | + (6,10,0), | | 236 | + (13,11,0), | | 237 | + (19,12,0), | | 238 | + (26,13,0), | | 239 | + (14,0,1), | | 240 | + (15,1,1), | | 241 | + (18,2,1), | | 242 | + (23,3,1), | | 243 | + (24,4,1), | | 244 | + (25,5,1), | | 245 | + (8,6,1), | | 246 | + (7,7,1), | | 247 | + (12,8,1), | | 248 | + (16,9,1), | | 249 | + (20,10,1), | | 250 | + (21,11,1)) | | 251 | + if pi.RPI_REVISION == 3: | | 252 | + gpio = gpio3 | | 253 | + self.parent.title('Raspberry Pi GPIO - A+/B+/2B+') | | 254 | + elif pi.RPI_REVISION == 2: | | 255 | + #Change this when I know the pins on RPi GPIO Version 2 | | 256 | + #gpio = gpio2 | | 257 | + self.parent.title('Raspberry Pi GPIO - A/B Rev2') | | 258 | + elif pi.RPI_REVISION == 1: | | 259 | + self.parent.title('Raspberry Pi GPIO - A/B') | | 260 | + else: | | 261 | + self.parent.title('Raspberry Pi GPIO - Unknown Version') | | 262 | + ##Assume same config as A+/B+/2B+ | | 263 | + gpio = gpio3 | | 264 | + return gpio | 241 | 265 |
| 242 | 266 |
| 243 | 267 | def main(): | @@ -247,6 +271,7 @@ def main(): | 247 | 271 | a.grid() | 248 | 272 | """When the window is closed, run the onClose function.""" | 249 | 273 | root.protocol("WM_DELETE_WINDOW",a.onClose) | | 274 | + root.resizable(False,False) | 250 | 275 | root.mainloop() | 251 | 276 |
| 252 | 277 |
| |
|