Raspberry Pi : BerryClip/fi: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | # Raspberry Pi : BerryClip | |
| 2 | 2 | ||
| 3 | 3 |  | |
| 4 | 4 | ||
| 5 | 5 | BerryClip on kuusi (6) ledinen lisäkortti Raspberry Pi:hin. Se koostuu piirilevystä johon on sijoitettu kuusi LEDiä, yksi kytkin ja yksi summeri. Kortti on helppo liittää "raspin" GPIO liittimeen. | |
| 6 | 6 | ||
| 7 | 7 | ## Laitteisto | |
| 8 | 8 | ||
| 9 | 9 |  | |
| 10 | 10 | ||
| 11 | 11 | Alla olevassa luettelossa on kytketty komponentit vastaaviin liittimen nastoihin ja GPIO viitteisiin: | |
| 12 | 12 | ||
| 13 | 13 | - LED 1 - Pin 7 - GPIO4 | |
| 14 | 14 | - LED 2 - Pin 11 - GPIO17 | |
| 15 | 15 | - LED 3 - Pin 15 - GPIO22 | |
| 16 | 16 | - LED 4 - Pin 19 - GPIO10 | |
| 17 | 17 | - LED 5 - Pin 21 - GPIO9 | |
| 18 | 18 | - LED 6 - Pin 23 - GPIO11 | |
| 19 | 19 | - Summeri - Pin 24 - GPIO8 | |
| 20 | 20 | - Kytkin - Pin 26 - GPIO7 | |
| 21 | 21 | ||
| 22 | 22 | | GPIO | Pin | | Pin | GPIO | | |
| 23 | 23 | |--------|-----|-----|-----|--------| | |
| 24 | 24 | | GPIO7 | 26 | | 25 | GND | | |
| 25 | 25 | | GPIO8 | 24 | | 23 | GPIO11 | | |
| 26 | 26 | | GPIO25 | 22 | | 21 | GPIO9 | | |
| 27 | 27 | | GND | 20 | | 19 | GPIO10 | | |
| 28 | 28 | | GPIO24 | 18 | | 17 | +3.3V | | |
| 29 | 29 | | GPIO23 | 16 | | 15 | GPIO22 | | |
| 30 | 30 | | GND | 14 | | 13 | GPIO27 | | |
| 31 | 31 | | GPIO18 | 12 | | 11 | GPIO17 | | |
| 32 | 32 | | GPIO15 | 10 | | 9 | GND | | |
| 33 | 33 | | GPIO14 | 8 | | 7 | GPIO4 | | |
| 34 | 34 | | GND | 6 | | 5 | GPIO3 | | |
| 35 | 35 | | +5V | 4 | | 3 | GPIO2 | | |
| 36 | 36 | | +5V | 2 | | 1 | +3.3V | | |
| 37 | 37 | ||
| 38 | 38 | ## Ohjelmisto | |
| 39 | 39 | ||
| 40 | 40 |  | |
| 41 | 41 | ||
| 42 | 42 | Seuraava ohjelmalistaus esimerkki näyttää miten ohjataan BerryClip-levyä. | |
| 43 | 43 | ||
| 44 | 44 | Toimiakseen ohjelma pitää ajaa pääkäyttäjän (root) oikeuksilla (esim. sudo ./berryclipdemo). | |
| 45 | 45 | ||
| 46 | 46 | | Taso | | Käännösyksikkö | | |
| 47 | 47 | |------|------------------------------|-----------------------------------------| | |
| 48 | 48 | | 1 | Käyttöliittymä, sovellustaso | unit1.pas, unit1.lfm, berryclipdemo.lpr | | |
| 49 | 49 | | 2 | Levytaso | uberryclip.pas | | |
| 50 | 50 | | 3 | | uraspberrypi.pas | | |
| 51 | 51 | | 4 | I/O ajuri | pi_io_unit.pas | | |
| 52 | 52 | ||
| 53 | 53 | ### Käyttöliittymä | |
| 54 | 54 | ||
| 55 | 55 |  | |
| 56 | 56 | ||
| 57 | 57 | Käyttöliittymä sisältyy vaihtopainonapeista jotka ohjaa lediä ja painonapista joka soittaa summeria. Näiden lisäksi kytkimen tila näytetään lomakkeella olevalla "ledillä". Piirilevyjen ledien tila näytetään lomakkeella olevilla ledeillä. | |
| 58 | 58 | ||
| 59 | 59 | BerryClipDemo ohjelma käyttää seuraavia komponentteja: [[TApplicationProperties]], [[TButton/fi|TButton]], [[TShape]] ja [[TToggleBox]]. | |
| 60 | 60 | ||
| 61 | 61 | #### unit1.pas | |
| 62 | 62 | ||
| 63 | 63 | ``` pascal | |
| 64 | 64 | unit Unit1; | |
| 65 | 65 | ||
| 66 | 66 | {$mode objfpc}{$H+} | |
| 67 | 67 | ||
| 68 | 68 | interface | |
| 69 | 69 | ||
| 70 | 70 | uses | |
| 71 | 71 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, | |
| 72 | 72 | ExtCtrls, uberryclip; | |
| 73 | 73 | ||
| 74 | 74 | type | |
| 75 | 75 | ||
| 76 | 76 | { TForm1 } | |
| 77 | 77 | ||
| 78 | 78 | TForm1 = class(TForm) | |
| 79 | 79 | ApplicationProperties1: TApplicationProperties; | |
| 80 | 80 | Button1: TButton; | |
| 81 | 81 | Led4ToggleBox: TToggleBox; | |
| 82 | 82 | Led6ToggleBox: TToggleBox; | |
| 83 | 83 | Led2ToggleBox: TToggleBox; | |
| 84 | 84 | Led3ToggleBox: TToggleBox; | |
| 85 | 85 | Led1ToggleBox: TToggleBox; | |
| 86 | 86 | Led5ToggleBox: TToggleBox; | |
| 87 | 87 | Shape1: TShape; | |
| 88 | 88 | Shape2: TShape; | |
| 89 | 89 | Shape3: TShape; | |
| 90 | 90 | Shape4: TShape; | |
| 91 | 91 | Shape5: TShape; | |
| 92 | 92 | Shape6: TShape; | |
| 93 | 93 | Shape7: TShape; | |
| 94 | 94 | procedure ApplicationProperties1Idle(Sender: TObject; var Done: Boolean); | |
| 95 | 95 | procedure Button1MouseDown(Sender: TObject; Button: TMouseButton; | |
| 96 | 96 | Shift: TShiftState; X, Y: Integer); | |
| 97 | 97 | procedure Button1MouseUp(Sender: TObject; Button: TMouseButton; | |
| 98 | 98 | Shift: TShiftState; X, Y: Integer); | |
| 99 | 99 | procedure FormCreate(Sender: TObject); | |
| 100 | 100 | procedure FormDestroy(Sender: TObject); | |
| 101 | 101 | procedure Led1ToggleBoxChange(Sender: TObject); | |
| 102 | 102 | procedure Led2ToggleBoxChange(Sender: TObject); | |
| 103 | 103 | procedure Led3ToggleBoxChange(Sender: TObject); | |
| 104 | 104 | procedure Led4ToggleBoxChange(Sender: TObject); | |
| 105 | 105 | procedure Led5ToggleBoxChange(Sender: TObject); | |
| 106 | 106 | procedure Led6ToggleBoxChange(Sender: TObject); | |
| 107 | 107 | private | |
| 108 | 108 | { private declarations } | |
| 109 | 109 | Berry_Clip:TBerryClip; | |
| 110 | 110 | public | |
| 111 | 111 | { public declarations } | |
| 112 | 112 | end; | |
| 113 | 113 | ||
| 114 | 114 | var | |
| 115 | 115 | Form1: TForm1; | |
| 116 | 116 | ||
| 117 | 117 | implementation | |
| 118 | 118 | ||
| 119 | 119 | {$R *.lfm} | |
| 120 | 120 | ||
| 121 | 121 | { TForm1 } | |
| 122 | 122 | ||
| 123 | 123 | procedure TForm1.ApplicationProperties1Idle(Sender: TObject; var Done: Boolean); | |
| 124 | 124 | begin | |
| 125 | 125 | if Berry_Clip.ReadSwitch | |
| 126 | 126 | then shape7.Brush.Color:=clBlack | |
| 127 | 127 | else shape7.Brush.Color:=clWhite; | |
| 128 | 128 | end; | |
| 129 | 129 | ||
| 130 | 130 | procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton; | |
| 131 | 131 | Shift: TShiftState; X, Y: Integer); | |
| 132 | 132 | begin | |
| 133 | 133 | Berry_Clip.SetBuzzerOn; | |
| 134 | 134 | end; | |
| 135 | 135 | ||
| 136 | 136 | procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton; | |
| 137 | 137 | Shift: TShiftState; X, Y: Integer); | |
| 138 | 138 | begin | |
| 139 | 139 | Berry_Clip.SetBuzzerOff; | |
| 140 | 140 | end; | |
| 141 | 141 | ||
| 142 | 142 | procedure TForm1.FormCreate(Sender: TObject); | |
| 143 | 143 | begin | |
| 144 | 144 | Berry_Clip := TBerryClip.Create; | |
| 145 | 145 | end; | |
| 146 | 146 | ||
| 147 | 147 | procedure TForm1.FormDestroy(Sender: TObject); | |
| 148 | 148 | begin | |
| 149 | 149 | Berry_Clip.Free; | |
| 150 | 150 | end; | |
| 151 | 151 | ||
| 152 | 152 | procedure TForm1.Led1ToggleBoxChange(Sender: TObject); | |
| 153 | 153 | begin | |
| 154 | 154 | if Led1ToggleBox.Checked then | |
| 155 | 155 | begin | |
| 156 | 156 | Berry_Clip.SetLedOn(1); | |
| 157 | 157 | shape1.Brush.Color:=clRed ; | |
| 158 | 158 | end | |
| 159 | 159 | else | |
| 160 | 160 | begin | |
| 161 | 161 | Berry_Clip.SetLedOff(1); | |
| 162 | 162 | shape1.Brush.Color:=clWhite; | |
| 163 | 163 | end; | |
| 164 | 164 | end; | |
| 165 | 165 | ||
| 166 | 166 | procedure TForm1.Led2ToggleBoxChange(Sender: TObject); | |
| 167 | 167 | begin | |
| 168 | 168 | if Led2ToggleBox.Checked then | |
| 169 | 169 | begin | |
| 170 | 170 | Berry_Clip.SetLedOn(2); | |
| 171 | 171 | shape2.Brush.Color:=clRed; | |
| 172 | 172 | end | |
| 173 | 173 | else | |
| 174 | 174 | begin | |
| 175 | 175 | Berry_Clip.SetLedOff(2); | |
| 176 | 176 | shape2.Brush.Color:=clWhite; | |
| 177 | 177 | end; | |
| 178 | 178 | end; | |
| 179 | 179 | ||
| 180 | 180 | procedure TForm1.Led3ToggleBoxChange(Sender: TObject); | |
| 181 | 181 | begin | |
| 182 | 182 | if Led3ToggleBox.Checked then | |
| 183 | 183 | begin | |
| 184 | 184 | Berry_Clip.SetLedOn(3); | |
| 185 | 185 | shape3.Brush.Color:=clYellow; | |
| 186 | 186 | end | |
| 187 | 187 | else | |
| 188 | 188 | begin | |
| 189 | 189 | Berry_Clip.SetLedOff(3); | |
| 190 | 190 | shape3.Brush.Color:=clWhite; | |
| 191 | 191 | end; | |
| 192 | 192 | end; | |
| 193 | 193 | ||
| 194 | 194 | procedure TForm1.Led4ToggleBoxChange(Sender: TObject); | |
| 195 | 195 | begin | |
| 196 | 196 | if Led4ToggleBox.Checked then | |
| 197 | 197 | begin | |
| 198 | 198 | Berry_Clip.SetLedOn(4); | |
| 199 | 199 | shape4.Brush.Color:=clYellow; | |
| 200 | 200 | end | |
| 201 | 201 | else | |
| 202 | 202 | begin | |
| 203 | 203 | Berry_Clip.SetLedOff(4); | |
| 204 | 204 | shape4.Brush.Color:=clWhite; | |
| 205 | 205 | end; | |
| 206 | 206 | end; | |
| 207 | 207 | ||
| 208 | 208 | procedure TForm1.Led5ToggleBoxChange(Sender: TObject); | |
| 209 | 209 | begin | |
| 210 | 210 | if Led5ToggleBox.Checked then | |
| 211 | 211 | begin | |
| 212 | 212 | Berry_Clip.SetLedOn(5); | |
| 213 | 213 | shape5.Brush.Color:=clGreen; | |
| 214 | 214 | end | |
| 215 | 215 | else | |
| 216 | 216 | begin | |
| 217 | 217 | Berry_Clip.SetLedOff(5); | |
| 218 | 218 | shape5.Brush.Color:=clWhite; | |
| 219 | 219 | end; | |
| 220 | 220 | end; | |
| 221 | 221 | ||
| 222 | 222 | procedure TForm1.Led6ToggleBoxChange(Sender: TObject); | |
| 223 | 223 | begin | |
| 224 | 224 | if Led6ToggleBox.Checked then | |
| 225 | 225 | begin | |
| 226 | 226 | Berry_Clip.SetLedOn(6); | |
| 227 | 227 | shape6.Brush.Color:=clGreen; | |
| 228 | 228 | end | |
| 229 | 229 | else | |
| 230 | 230 | begin | |
| 231 | 231 | Berry_Clip.SetLedOff(6); | |
| 232 | 232 | shape6.Brush.Color:=clWhite; | |
| 233 | 233 | end; | |
| 234 | 234 | end; | |
| 235 | 235 | ||
| 236 | 236 | end. | |
| 237 | 237 | ``` | |
| 238 | 238 | ||
| 239 | 239 | #### unit1.lfm | |
| 240 | 240 | ||
| 241 | - | ||
| 241 | + | ``` lfm | |
| 242 | 242 | object Form1: TForm1 | |
| 243 | 243 | Left = 269 | |
| 244 | 244 | Height = 184 | |
| 245 | 245 | Top = 175 | |
| 246 | 246 | Width = 176 | |
| 247 | 247 | Caption = 'BerryClip' | |
| 248 | 248 | ClientHeight = 184 | |
| 249 | 249 | ClientWidth = 176 | |
| 250 | 250 | OnCreate = FormCreate | |
| 251 | 251 | OnDestroy = FormDestroy | |
| 252 | 252 | object Led2ToggleBox: TToggleBox | |
| 253 | 253 | Left = 8 | |
| 254 | 254 | Height = 24 | |
| 255 | 255 | Top = 32 | |
| 256 | 256 | Width = 128 | |
| 257 | 257 | Caption = 'Led2ToggleBox' | |
| 258 | 258 | OnChange = Led2ToggleBoxChange | |
| 259 | 259 | TabOrder = 0 | |
| 260 | 260 | end | |
| 261 | 261 | object Led1ToggleBox: TToggleBox | |
| 262 | 262 | Left = 8 | |
| 263 | 263 | Height = 24 | |
| 264 | 264 | Top = 8 | |
| 265 | 265 | Width = 128 | |
| 266 | 266 | Caption = 'Led1ToggleBox' | |
| 267 | 267 | OnChange = Led1ToggleBoxChange | |
| 268 | 268 | TabOrder = 1 | |
| 269 | 269 | end | |
| 270 | 270 | object Led3ToggleBox: TToggleBox | |
| 271 | 271 | Left = 8 | |
| 272 | 272 | Height = 24 | |
| 273 | 273 | Top = 56 | |
| 274 | 274 | Width = 128 | |
| 275 | 275 | Caption = 'Led3ToggleBox' | |
| 276 | 276 | OnChange = Led3ToggleBoxChange | |
| 277 | 277 | TabOrder = 2 | |
| 278 | 278 | end | |
| 279 | 279 | object Led4ToggleBox: TToggleBox | |
| 280 | 280 | Left = 8 | |
| 281 | 281 | Height = 24 | |
| 282 | 282 | Top = 80 | |
| 283 | 283 | Width = 128 | |
| 284 | 284 | Caption = 'Led4ToggleBox' | |
| 285 | 285 | OnChange = Led4ToggleBoxChange | |
| 286 | 286 | TabOrder = 3 | |
| 287 | 287 | end | |
| 288 | 288 | object Led5ToggleBox: TToggleBox | |
| 289 | 289 | Left = 8 | |
| 290 | 290 | Height = 24 | |
| 291 | 291 | Top = 104 | |
| 292 | 292 | Width = 128 | |
| 293 | 293 | Caption = 'Led5ToggleBox' | |
| 294 | 294 | OnChange = Led5ToggleBoxChange | |
| 295 | 295 | TabOrder = 4 | |
| 296 | 296 | end | |
| 297 | 297 | object Led6ToggleBox: TToggleBox | |
| 298 | 298 | Left = 8 | |
| 299 | 299 | Height = 24 | |
| 300 | 300 | Top = 128 | |
| 301 | 301 | Width = 128 | |
| 302 | 302 | Caption = 'Led6ToggleBox' | |
| 303 | 303 | OnChange = Led6ToggleBoxChange | |
| 304 | 304 | TabOrder = 5 | |
| 305 | 305 | end | |
| 306 | 306 | object Shape1: TShape | |
| 307 | 307 | Left = 144 | |
| 308 | 308 | Height = 18 | |
| 309 | 309 | Top = 12 | |
| 310 | 310 | Width = 16 | |
| 311 | 311 | Shape = stCircle | |
| 312 | 312 | end | |
| 313 | 313 | object Shape2: TShape | |
| 314 | 314 | Left = 144 | |
| 315 | 315 | Height = 18 | |
| 316 | 316 | Top = 34 | |
| 317 | 317 | Width = 16 | |
| 318 | 318 | Shape = stCircle | |
| 319 | 319 | end | |
| 320 | 320 | object Shape3: TShape | |
| 321 | 321 | Left = 144 | |
| 322 | 322 | Height = 18 | |
| 323 | 323 | Top = 58 | |
| 324 | 324 | Width = 16 | |
| 325 | 325 | Shape = stCircle | |
| 326 | 326 | end | |
| 327 | 327 | object Shape4: TShape | |
| 328 | 328 | Left = 144 | |
| 329 | 329 | Height = 18 | |
| 330 | 330 | Top = 82 | |
| 331 | 331 | Width = 16 | |
| 332 | 332 | Shape = stCircle | |
| 333 | 333 | end | |
| 334 | 334 | object Shape5: TShape | |
| 335 | 335 | Left = 144 | |
| 336 | 336 | Height = 18 | |
| 337 | 337 | Top = 106 | |
| 338 | 338 | Width = 16 | |
| 339 | 339 | Shape = stCircle | |
| 340 | 340 | end | |
| 341 | 341 | object Shape6: TShape | |
| 342 | 342 | Left = 144 | |
| 343 | 343 | Height = 18 | |
| 344 | 344 | Top = 130 | |
| 345 | 345 | Width = 16 | |
| 346 | 346 | Shape = stCircle | |
| 347 | 347 | end | |
| 348 | 348 | object Shape7: TShape | |
| 349 | 349 | Left = 144 | |
| 350 | 350 | Height = 18 | |
| 351 | 351 | Top = 158 | |
| 352 | 352 | Width = 16 | |
| 353 | 353 | Shape = stCircle | |
| 354 | 354 | end | |
| 355 | 355 | object Button1: TButton | |
| 356 | 356 | Left = 8 | |
| 357 | 357 | Height = 25 | |
| 358 | 358 | Top = 151 | |
| 359 | 359 | Width = 64 | |
| 360 | 360 | Caption = 'Buzzer' | |
| 361 | 361 | OnMouseDown = Button1MouseDown | |
| 362 | 362 | OnMouseUp = Button1MouseUp | |
| 363 | 363 | TabOrder = 6 | |
| 364 | 364 | end | |
| 365 | 365 | object ApplicationProperties1: TApplicationProperties | |
| 366 | 366 | OnIdle = ApplicationProperties1Idle | |
| 367 | 367 | left = 72 | |
| 368 | 368 | top = 152 | |
| 369 | 369 | end | |
| 370 | 370 | end | |
| 371 | 371 | </source> | |
| 372 | 372 | ||
| 373 | 373 | === Pääohjelma === | |
| 374 | 374 | <source> | |
| 375 | 375 | program berryclipdemo; | |
| 376 | 376 | ||
| 377 | 377 | {$mode objfpc}{$H+} | |
| 378 | 378 | ||
| 379 | 379 | uses | |
| 380 | 380 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 381 | 381 | cthreads, | |
| 382 | 382 | {$ENDIF}{$ENDIF} | |
| 383 | 383 | Interfaces, // this includes the LCL widgetset | |
| 384 | 384 | Forms, Unit1, pi_io_unit, uberryclip, uraspberrypi | |
| 385 | 385 | { you can add units after this }; | |
| 386 | 386 | ||
| 387 | 387 | {$R *.res} | |
| 388 | 388 | ||
| 389 | 389 | begin | |
| 390 | 390 | RequireDerivedFormResource := True; | |
| 391 | 391 | Application.Initialize; | |
| 392 | 392 | Application.CreateForm(TForm1, Form1); | |
| 393 | 393 | Application.Run; | |
| 394 | 394 | end. | |
| 395 | 395 | ``` | |
| 396 | 396 | ||
| 397 | 397 | ### Levy | |
| 398 | 398 | ||
| 399 | 399 | #### uberryclip.pas | |
| 400 | 400 | ||
| 401 | 401 | ``` pascal | |
| 402 | 402 | unit uberryclip; | |
| 403 | 403 | ||
| 404 | 404 | {$mode objfpc}{$H+} | |
| 405 | 405 | ||
| 406 | 406 | interface | |
| 407 | 407 | ||
| 408 | 408 | uses | |
| 409 | 409 | Classes, SysUtils, | |
| 410 | 410 | uRaspberrypi; | |
| 411 | 411 | type | |
| 412 | 412 | ||
| 413 | 413 | { TBerryClip } | |
| 414 | 414 | ||
| 415 | 415 | TBerryClip = class | |
| 416 | 416 | private | |
| 417 | 417 | RaspBerryPi : TRaspBerryPi; | |
| 418 | 418 | public | |
| 419 | 419 | constructor Create; | |
| 420 | 420 | procedure SetLedOn( led_no :integer ); | |
| 421 | 421 | procedure SetLedOff( led_no :integer ); | |
| 422 | 422 | procedure SetBuzzerOn; | |
| 423 | 423 | procedure SetBuzzerOff; | |
| 424 | 424 | function ReadSwitch:boolean; | |
| 425 | 425 | destructor Destroy; override; | |
| 426 | 426 | end; | |
| 427 | 427 | implementation | |
| 428 | 428 | ||
| 429 | 429 | { TBerryClip } | |
| 430 | 430 | ||
| 431 | 431 | constructor TBerryClip.Create; | |
| 432 | 432 | begin | |
| 433 | 433 | RaspBerryPi := TRaspBerryPi.Create; | |
| 434 | 434 | RaspBerryPi.SetGpioPinOutput(GPIO4); // Led 1 | |
| 435 | 435 | RaspBerryPi.SetGpioPinOutput(GPIO9); // Led 2 | |
| 436 | 436 | RaspBerryPi.SetGpioPinOutput(GPIO10);// Led 3 | |
| 437 | 437 | RaspBerryPi.SetGpioPinOutput(GPIO11);// Led 4 | |
| 438 | 438 | RaspBerryPi.SetGpioPinOutput(GPIO17);// Led 5 | |
| 439 | 439 | RaspBerryPi.SetGpioPinOutput(GPIO22);// Led 6 | |
| 440 | 440 | RaspBerryPi.SetGpioPinInput(GPIO7); // Switch | |
| 441 | 441 | RaspBerryPi.SetGpioPinOutput(GPIO8); // Buzzer | |
| 442 | 442 | end; | |
| 443 | 443 | ||
| 444 | 444 | procedure TBerryClip.SetLedOn(led_no: integer); | |
| 445 | 445 | begin | |
| 446 | 446 | case led_no of | |
| 447 | 447 | 1 : RaspBerryPi.SetGpioBit(GPIO4); | |
| 448 | 448 | 2 : RaspBerryPi.SetGpioBit(GPIO17); | |
| 449 | 449 | 3 : RaspBerryPi.SetGpioBit(GPIO22); | |
| 450 | 450 | 4 : RaspBerryPi.SetGpioBit(GPIO10); | |
| 451 | 451 | 5 : RaspBerryPi.SetGpioBit(GPIO9); | |
| 452 | 452 | 6 : RaspBerryPi.SetGpioBit(GPIO11); | |
| 453 | 453 | end; | |
| 454 | 454 | end; | |
| 455 | 455 | ||
| 456 | 456 | procedure TBerryClip.SetLedOff(led_no: integer); | |
| 457 | 457 | begin | |
| 458 | 458 | case led_no of | |
| 459 | 459 | 1 : RaspBerryPi.ClearGpioBit(GPIO4); | |
| 460 | 460 | 2 : RaspBerryPi.ClearGpioBit(GPIO17); | |
| 461 | 461 | 3 : RaspBerryPi.ClearGpioBit(GPIO22); | |
| 462 | 462 | 4 : RaspBerryPi.ClearGpioBit(GPIO10); | |
| 463 | 463 | 5 : RaspBerryPi.ClearGpioBit(GPIO9); | |
| 464 | 464 | 6 : RaspBerryPi.ClearGpioBit(GPIO11); | |
| 465 | 465 | end; | |
| 466 | 466 | ||
| 467 | 467 | end; | |
| 468 | 468 | ||
| 469 | 469 | procedure TBerryClip.SetBuzzerOn; | |
| 470 | 470 | begin | |
| 471 | 471 | RaspBerryPi.SetGpioBit(GPIO8); | |
| 472 | 472 | end; | |
| 473 | 473 | ||
| 474 | 474 | procedure TBerryClip.SetBuzzerOff; | |
| 475 | 475 | begin | |
| 476 | 476 | RaspBerryPi.ClearGpioBit(GPIO8); | |
| 477 | 477 | end; | |
| 478 | 478 | ||
| 479 | 479 | function TBerryClip.ReadSwitch: boolean; | |
| 480 | 480 | begin | |
| 481 | 481 | result := RaspBerryPi.GetGpioBit(GPIO7); | |
| 482 | 482 | end; | |
| 483 | 483 | ||
| 484 | 484 | destructor TBerryClip.Destroy; | |
| 485 | 485 | begin | |
| 486 | 486 | RaspBerryPi.Free; | |
| 487 | 487 | inherited Destroy; | |
| 488 | 488 | end; | |
| 489 | 489 | ||
| 490 | 490 | end. | |
| 491 | 491 | ``` | |
| 492 | 492 | ||
| 493 | 493 | #### uraspberrypi.pas | |
| 494 | 494 | ||
| 495 | 495 | ``` pascal | |
| 496 | 496 | unit uraspberrypi; | |
| 497 | 497 | ||
| 498 | 498 | {$mode objfpc}{$H+} | |
| 499 | 499 | ||
| 500 | 500 | interface | |
| 501 | 501 | ||
| 502 | 502 | uses | |
| 503 | 503 | Classes, SysUtils, pi_io_unit, dialogs; | |
| 504 | 504 | ||
| 505 | 505 | const | |
| 506 | 506 | ||
| 507 | 507 | GPIO4 = 4; | |
| 508 | 508 | GPIO7 = 7; | |
| 509 | 509 | GPIO8 = 8; | |
| 510 | 510 | GPIO9 = 9; | |
| 511 | 511 | GPIO10= 10; | |
| 512 | 512 | GPIO11= 11; | |
| 513 | 513 | GPIO17= 17; | |
| 514 | 514 | GPIO22= 22; | |
| 515 | 515 | ||
| 516 | 516 | type | |
| 517 | 517 | ||
| 518 | 518 | { TRaspBerryPi } | |
| 519 | 519 | ||
| 520 | 520 | TRaspBerryPi = class | |
| 521 | 521 | private | |
| 522 | 522 | io_port: T_IO_Port; | |
| 523 | 523 | public | |
| 524 | 524 | constructor Create; | |
| 525 | 525 | procedure ClearGpioBit(no:byte); | |
| 526 | 526 | function GetGpioBit(no:byte):boolean; | |
| 527 | 527 | procedure SetGpioBit(no:byte); | |
| 528 | 528 | procedure SetGpioPinInput( no:byte); | |
| 529 | 529 | procedure SetGpioPinOutput( no:byte); | |
| 530 | 530 | destructor Destroy; override; | |
| 531 | 531 | end; | |
| 532 | 532 | ||
| 533 | 533 | implementation | |
| 534 | 534 | ||
| 535 | 535 | { TRaspBerryPi } | |
| 536 | 536 | ||
| 537 | 537 | constructor TRaspBerryPi.Create; | |
| 538 | 538 | begin | |
| 539 | 539 | if not MapIo | |
| 540 | 540 | then ShowMessage('Error mapping gpio') | |
| 541 | 541 | else | |
| 542 | 542 | begin | |
| 543 | 543 | io_port := T_IO_Port.Create; | |
| 544 | 544 | io_port.f_gpio := CreatePort(GPIO_BASE); | |
| 545 | 545 | end ; | |
| 546 | 546 | end; | |
| 547 | 547 | ||
| 548 | 548 | procedure TRaspBerryPi.ClearGpioBit(no: byte); | |
| 549 | 549 | begin | |
| 550 | 550 | io_port.ClearBit(no); | |
| 551 | 551 | end; | |
| 552 | 552 | ||
| 553 | 553 | function TRaspBerryPi.GetGpioBit(no: byte): boolean; | |
| 554 | 554 | begin | |
| 555 | 555 | result := io_port.GetBit(no); | |
| 556 | 556 | end; | |
| 557 | 557 | ||
| 558 | 558 | procedure TRaspBerryPi.SetGpioBit(no: byte); | |
| 559 | 559 | begin | |
| 560 | 560 | io_port.SetBit(no); | |
| 561 | 561 | end; | |
| 562 | 562 | ||
| 563 | 563 | procedure TRaspBerryPi.SetGpioPinInput(no: byte); | |
| 564 | 564 | begin | |
| 565 | 565 | io_port.SetPinMode(no,INPUT); | |
| 566 | 566 | end; | |
| 567 | 567 | ||
| 568 | 568 | procedure TRaspBerryPi.SetGpioPinOutput(no: byte); | |
| 569 | 569 | begin | |
| 570 | 570 | io_port.SetPinMode(no,OUTPUT); | |
| 571 | 571 | end; | |
| 572 | 572 | ||
| 573 | 573 | destructor TRaspBerryPi.Destroy; | |
| 574 | 574 | begin | |
| 575 | 575 | io_port.ClearBit(GPIO4); | |
| 576 | 576 | io_port.ClearBit(GPIO9); | |
| 577 | 577 | io_port.ClearBit(GPIO10); | |
| 578 | 578 | io_port.ClearBit(GPIO11); | |
| 579 | 579 | io_port.ClearBit(GPIO17); | |
| 580 | 580 | io_port.ClearBit(GPIO22); | |
| 581 | 581 | io_port.ClearBit(GPIO8); | |
| 582 | 582 | UnMapIo(io_port); | |
| 583 | 583 | io_port.free; | |
| 584 | 584 | inherited Destroy; | |
| 585 | 585 | end; | |
| 586 | 586 | ||
| 587 | 587 | end. | |
| 588 | 588 | </synyaxhighlight> | |
| 589 | 589 | ||
| 590 | 590 | ==== pi_io_unit.pas ==== | |
| 591 | 591 | ||
| 592 | 592 | <syntaxhighlight lang=pascal> | |
| 593 | 593 | unit pi_io_unit; | |
| 594 | 594 | // This code is tested only RPI2B Processor | |
| 595 | 595 | {$mode objfpc}{$H+} | |
| 596 | 596 | ||
| 597 | 597 | interface | |
| 598 | 598 | ||
| 599 | 599 | uses | |
| 600 | 600 | Classes, SysUtils, Unix, BaseUnix; | |
| 601 | 601 | ||
| 602 | 602 | const | |
| 603 | 603 | BCM2709_PBASE= $3F000000; // Peripheral Base in Bytes (RPI2B Processor) | |
| 604 | 604 | // BCM2708_PBASE= $20000000; // Peripheral Base (maybe RPI1 ) | |
| 605 | 605 | REG_GPIO = BCM2709_PBASE div $1000; // $3F000 ; | |
| 606 | 606 | PAGE_SIZE = $1000; | |
| 607 | 607 | ||
| 608 | 608 | GPIO_BASE = (REG_GPIO + $200); | |
| 609 | 609 | ||
| 610 | 610 | INPUT = 0; | |
| 611 | 611 | OUTPUT = 1; | |
| 612 | 612 | ||
| 613 | 613 | GPSET = $1C; // GPIO Pin Output Set | |
| 614 | 614 | GPCLR = $28; // GPIO Pin Output Clear | |
| 615 | 615 | GPLEV = $34; // GPIO Pin Level | |
| 616 | 616 | ||
| 617 | 617 | type | |
| 618 | 618 | ||
| 619 | 619 | { T_IO_Port } | |
| 620 | 620 | ||
| 621 | 621 | T_IO_Port = class | |
| 622 | 622 | private | |
| 623 | 623 | public | |
| 624 | 624 | f_gpio: PLongWord; | |
| 625 | 625 | procedure SetPinMode(a_io_pin, a_mode: byte); | |
| 626 | 626 | function GetBit(a_io_pin : byte):boolean;inline; | |
| 627 | 627 | procedure ClearBit(a_io_pin : byte);inline; | |
| 628 | 628 | procedure SetBit(a_io_pin : byte);inline; | |
| 629 | 629 | end; | |
| 630 | 630 | ||
| 631 | 631 | function MapIo:boolean;// creates io mapping | |
| 632 | 632 | procedure UnMapIo(a_Map: T_IO_Port);// close io | |
| 633 | 633 | function CreatePort(PortGpio: LongWord): PLongWord; | |
| 634 | 634 | ||
| 635 | 635 | implementation | |
| 636 | 636 | ||
| 637 | 637 | var | |
| 638 | 638 | fd: integer;// /dev/mem file handle | |
| 639 | 639 | ||
| 640 | 640 | function MapIo: boolean; | |
| 641 | 641 | begin | |
| 642 | 642 | result := true; | |
| 643 | 643 | fd := fpopen('/dev/mem', O_RdWr or O_Sync); | |
| 644 | 644 | if fd < 0 then result := false; // unsuccessful mapping | |
| 645 | 645 | end; | |
| 646 | 646 | ||
| 647 | 647 | procedure UnMapIo( a_Map: T_IO_Port); | |
| 648 | 648 | begin | |
| 649 | 649 | if a_Map.f_gpio <> nil then | |
| 650 | 650 | begin | |
| 651 | 651 | fpMUnmap(a_Map.f_gpio, PAGE_SIZE); | |
| 652 | 652 | a_Map.f_gpio := nil; | |
| 653 | 653 | end; | |
| 654 | 654 | end; | |
| 655 | 655 | ||
| 656 | 656 | function CreatePort(PortGpio: LongWord): PLongWord; | |
| 657 | 657 | begin | |
| 658 | 658 | result := FpMmap(nil, PAGE_SIZE, PROT_READ or PROT_WRITE, MAP_SHARED, fd, PortGpio); | |
| 659 | 659 | end; | |
| 660 | 660 | ||
| 661 | 661 | procedure T_IO_Port.SetPinMode(a_io_pin, a_mode: byte); | |
| 662 | 662 | var | |
| 663 | 663 | fSel, shift : byte; | |
| 664 | 664 | iof : PLongWord; | |
| 665 | 665 | begin | |
| 666 | 666 | fSel := (a_io_pin div $A)*4 ; | |
| 667 | 667 | shift := (a_io_pin mod $A)*3 ; | |
| 668 | 668 | iof := Pointer(LongWord(Self.f_gpio)+fSel); | |
| 669 | 669 | case a_mode of | |
| 670 | 670 | INPUT : iof^ := iof^ and ($FFFFFFFF - (7 shl shift)); | |
| 671 | 671 | OUTPUT : iof^ := iof^ and ($FFFFFFFF - (7 shl shift)) or (1 shl shift); | |
| 672 | 672 | end; | |
| 673 | 673 | end; | |
| 674 | 674 | ||
| 675 | 675 | procedure T_IO_Port.SetBit(a_io_pin : byte); | |
| 676 | 676 | var | |
| 677 | 677 | iof : PLongWord; | |
| 678 | 678 | begin | |
| 679 | 679 | iof := Pointer(LongWord(Self.f_gpio) + GPSET + (a_io_pin shr 5) shl 2); | |
| 680 | 680 | iof^ := 1 shl a_io_pin; | |
| 681 | 681 | end; | |
| 682 | 682 | ||
| 683 | 683 | procedure T_IO_Port.ClearBit(a_io_pin : byte); | |
| 684 | 684 | var | |
| 685 | 685 | iof : PLongWord; | |
| 686 | 686 | begin | |
| 687 | 687 | iof := Pointer(LongWord(Self.f_gpio) + GPCLR + (a_io_pin shr 5) shl 2); | |
| 688 | 688 | iof^ := 1 shl a_io_pin; | |
| 689 | 689 | end; | |
| 690 | 690 | ||
| 691 | 691 | function T_IO_Port.GetBit(a_io_pin : byte):boolean; | |
| 692 | 692 | var | |
| 693 | 693 | iof : PLongWord; | |
| 694 | 694 | begin | |
| 695 | 695 | result := true; | |
| 696 | 696 | iof := Pointer(LongWord(Self.f_gpio) + GPLEV + (a_io_pin shr 5) shl 2); | |
| 697 | 697 | if (iof^ and (1 shl a_io_pin)) = 0 then result := false; | |
| 698 | 698 | end; | |
| 699 | 699 | end. | |
| 700 | 700 | ``` | |
| 701 | 701 | ||
| 702 | 702 | --- | |
| 703 | 703 | *From the Free Pascal wiki, page "Raspberry Pi : BerryClip/fi", as it stood on 2020-02-23 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
