Sign in Join
Wiki copytemporary

Raspberry Pi : BerryClip: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11# Raspberry Pi : BerryClip
22
33![](/wiki-images/3/3f/Pi_berryclip.png)
44
55The BerryClip 6 LED Board is a add-on board for the Raspberry Pi. It consists of a PCB populated with 6 coloured LEDs, one switch and one buzzer. It is easy to attach to the Pi’s GPIO header.
66
77## Hardware
88
99![](/wiki-images/b/bd/Pi_berryclip_sch.png)
1010
1111The following list shows the mapping between the components, the header pins and the GPIO references :
1212
1313- LED 1 - Pin 7 - GPIO4
1414- LED 2 - Pin 11 - GPIO17
1515- LED 3 - Pin 15 - GPIO22
1616- LED 4 - Pin 19 - GPIO10
1717- LED 5 - Pin 21 - GPIO9
1818- LED 6 - Pin 23 - GPIO11
1919- Buzzer - Pin 24 - GPIO8
2020- Switch - Pin 26 - GPIO7
2121
2222| GPIO | Pin | | Pin | GPIO |
2323|--------|-----|-----|-----|--------|
2424| GPIO7 | 26 | | 25 | GND |
2525| GPIO8 | 24 | | 23 | GPIO11 |
2626| GPIO25 | 22 | | 21 | GPIO9 |
2727| GND | 20 | | 19 | GPIO10 |
2828| GPIO24 | 18 | | 17 |  +3.3V |
2929| GPIO23 | 16 | | 15 | GPIO22 |
3030| GND | 14 | | 13 | GPIO27 |
3131| GPIO18 | 12 | | 11 | GPIO17 |
3232| GPIO15 | 10 | | 9 | GND |
3333| GPIO14 | 8 | | 7 | GPIO4 |
3434| GND | 6 | | 5 | GPIO3 |
3535|  +5V | 4 | | 3 | GPIO2 |
3636|  +5V | 2 | | 1 |  +3.3V |
3737
3838## Software
3939
4040![](/wiki-images/c/c5/Pi_berryclip_uml.png)
4141
4242The following example lists a program that controls berryClip.
4343
4444The code requires to be run as root (eg. sudo ./berryclipdemo).
4545
4646| Level | | Unit |
4747|----|----|----|
4848| 1 | User interface, Application level | unit1.pas, unit1.lfm, berryclipdemo.lpr |
4949| 2 | Board level | uberryclip.pas |
5050| 3 | | uraspberrypi.pas |
5151| 4 | I/O driver | pi_io_unit.pas |
5252
5353### User interface
5454
5555![](/wiki-images/e/e5/Pi_berryclipdemo.png)
5656
5757The user interface consists of the buttons that control the LEDs and buzzer. In addition, the switch status is displayed on LED. LEDs state on the circuit board is displayed with the LEDs on the form. The berryclipdemo program uses the following components: [[TApplicationProperties]], [[TButton]], [[TShape]] and [[TToggleBox]].
5858
5959#### unit1.pas
6060
6161``` pascal
6262unit Unit1;
6363
6464{$mode objfpc}{$H+}
6565
6666interface
6767
6868uses
6969 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
7070 ExtCtrls, uberryclip;
7171
7272type
7373
7474 { TForm1 }
7575
7676 TForm1 = class(TForm)
7777 ApplicationProperties1: TApplicationProperties;
7878 Button1: TButton;
7979 Led4ToggleBox: TToggleBox;
8080 Led6ToggleBox: TToggleBox;
8181 Led2ToggleBox: TToggleBox;
8282 Led3ToggleBox: TToggleBox;
8383 Led1ToggleBox: TToggleBox;
8484 Led5ToggleBox: TToggleBox;
8585 Shape1: TShape;
8686 Shape2: TShape;
8787 Shape3: TShape;
8888 Shape4: TShape;
8989 Shape5: TShape;
9090 Shape6: TShape;
9191 Shape7: TShape;
9292 procedure ApplicationProperties1Idle(Sender: TObject; var Done: Boolean);
9393 procedure Button1MouseDown(Sender: TObject; Button: TMouseButton;
9494 Shift: TShiftState; X, Y: Integer);
9595 procedure Button1MouseUp(Sender: TObject; Button: TMouseButton;
9696 Shift: TShiftState; X, Y: Integer);
9797 procedure FormCreate(Sender: TObject);
9898 procedure FormDestroy(Sender: TObject);
9999 procedure Led1ToggleBoxChange(Sender: TObject);
100100 procedure Led2ToggleBoxChange(Sender: TObject);
101101 procedure Led3ToggleBoxChange(Sender: TObject);
102102 procedure Led4ToggleBoxChange(Sender: TObject);
103103 procedure Led5ToggleBoxChange(Sender: TObject);
104104 procedure Led6ToggleBoxChange(Sender: TObject);
105105 private
106106 { private declarations }
107107 Berry_Clip:TBerryClip;
108108 public
109109 { public declarations }
110110 end;
111111
112112var
113113 Form1: TForm1;
114114
115115implementation
116116
117117{$R *.lfm}
118118
119119{ TForm1 }
120120
121121procedure TForm1.ApplicationProperties1Idle(Sender: TObject; var Done: Boolean);
122122begin
123123 if Berry_Clip.ReadSwitch
124124 then shape7.Brush.Color:=clBlack
125125 else shape7.Brush.Color:=clWhite;
126126end;
127127
128128procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton;
129129 Shift: TShiftState; X, Y: Integer);
130130begin
131131 Berry_Clip.SetBuzzerOn;
132132end;
133133
134134procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton;
135135 Shift: TShiftState; X, Y: Integer);
136136begin
137137 Berry_Clip.SetBuzzerOff;
138138end;
139139
140140procedure TForm1.FormCreate(Sender: TObject);
141141begin
142142 Berry_Clip := TBerryClip.Create;
143143end;
144144
145145procedure TForm1.FormDestroy(Sender: TObject);
146146begin
147147 Berry_Clip.Free;
148148end;
149149
150150procedure TForm1.Led1ToggleBoxChange(Sender: TObject);
151151begin
152152 if Led1ToggleBox.Checked then
153153 begin
154154 Berry_Clip.SetLedOn(1);
155155 shape1.Brush.Color:=clRed ;
156156 end
157157 else
158158 begin
159159 Berry_Clip.SetLedOff(1);
160160 shape1.Brush.Color:=clWhite;
161161 end;
162162end;
163163
164164procedure TForm1.Led2ToggleBoxChange(Sender: TObject);
165165begin
166166 if Led2ToggleBox.Checked then
167167 begin
168168 Berry_Clip.SetLedOn(2);
169169 shape2.Brush.Color:=clRed;
170170 end
171171 else
172172 begin
173173 Berry_Clip.SetLedOff(2);
174174 shape2.Brush.Color:=clWhite;
175175 end;
176176end;
177177
178178procedure TForm1.Led3ToggleBoxChange(Sender: TObject);
179179begin
180180 if Led3ToggleBox.Checked then
181181 begin
182182 Berry_Clip.SetLedOn(3);
183183 shape3.Brush.Color:=clYellow;
184184 end
185185 else
186186 begin
187187 Berry_Clip.SetLedOff(3);
188188 shape3.Brush.Color:=clWhite;
189189 end;
190190end;
191191
192192procedure TForm1.Led4ToggleBoxChange(Sender: TObject);
193193begin
194194 if Led4ToggleBox.Checked then
195195 begin
196196 Berry_Clip.SetLedOn(4);
197197 shape4.Brush.Color:=clYellow;
198198 end
199199 else
200200 begin
201201 Berry_Clip.SetLedOff(4);
202202 shape4.Brush.Color:=clWhite;
203203 end;
204204end;
205205
206206procedure TForm1.Led5ToggleBoxChange(Sender: TObject);
207207begin
208208 if Led5ToggleBox.Checked then
209209 begin
210210 Berry_Clip.SetLedOn(5);
211211 shape5.Brush.Color:=clGreen;
212212 end
213213 else
214214 begin
215215 Berry_Clip.SetLedOff(5);
216216 shape5.Brush.Color:=clWhite;
217217 end;
218218end;
219219
220220procedure TForm1.Led6ToggleBoxChange(Sender: TObject);
221221begin
222222 if Led6ToggleBox.Checked then
223223 begin
224224 Berry_Clip.SetLedOn(6);
225225 shape6.Brush.Color:=clGreen;
226226 end
227227 else
228228 begin
229229 Berry_Clip.SetLedOff(6);
230230 shape6.Brush.Color:=clWhite;
231231 end;
232232end;
233233
234234end.
235235```
236236
237237#### unit1.lfm
238238
239-``` pascal
239+``` lfm
240240object Form1: TForm1
241241 Left = 269
242242 Height = 184
243243 Top = 175
244244 Width = 176
245245 Caption = 'BerryClip'
246246 ClientHeight = 184
247247 ClientWidth = 176
248248 OnCreate = FormCreate
249249 OnDestroy = FormDestroy
250250 object Led2ToggleBox: TToggleBox
251251 Left = 8
252252 Height = 24
253253 Top = 32
254254 Width = 128
255255 Caption = 'Led2ToggleBox'
256256 OnChange = Led2ToggleBoxChange
257257 TabOrder = 0
258258 end
259259 object Led1ToggleBox: TToggleBox
260260 Left = 8
261261 Height = 24
262262 Top = 8
263263 Width = 128
264264 Caption = 'Led1ToggleBox'
265265 OnChange = Led1ToggleBoxChange
266266 TabOrder = 1
267267 end
268268 object Led3ToggleBox: TToggleBox
269269 Left = 8
270270 Height = 24
271271 Top = 56
272272 Width = 128
273273 Caption = 'Led3ToggleBox'
274274 OnChange = Led3ToggleBoxChange
275275 TabOrder = 2
276276 end
277277 object Led4ToggleBox: TToggleBox
278278 Left = 8
279279 Height = 24
280280 Top = 80
281281 Width = 128
282282 Caption = 'Led4ToggleBox'
283283 OnChange = Led4ToggleBoxChange
284284 TabOrder = 3
285285 end
286286 object Led5ToggleBox: TToggleBox
287287 Left = 8
288288 Height = 24
289289 Top = 104
290290 Width = 128
291291 Caption = 'Led5ToggleBox'
292292 OnChange = Led5ToggleBoxChange
293293 TabOrder = 4
294294 end
295295 object Led6ToggleBox: TToggleBox
296296 Left = 8
297297 Height = 24
298298 Top = 128
299299 Width = 128
300300 Caption = 'Led6ToggleBox'
301301 OnChange = Led6ToggleBoxChange
302302 TabOrder = 5
303303 end
304304 object Shape1: TShape
305305 Left = 144
306306 Height = 18
307307 Top = 12
308308 Width = 16
309309 Shape = stCircle
310310 end
311311 object Shape2: TShape
312312 Left = 144
313313 Height = 18
314314 Top = 34
315315 Width = 16
316316 Shape = stCircle
317317 end
318318 object Shape3: TShape
319319 Left = 144
320320 Height = 18
321321 Top = 58
322322 Width = 16
323323 Shape = stCircle
324324 end
325325 object Shape4: TShape
326326 Left = 144
327327 Height = 18
328328 Top = 82
329329 Width = 16
330330 Shape = stCircle
331331 end
332332 object Shape5: TShape
333333 Left = 144
334334 Height = 18
335335 Top = 106
336336 Width = 16
337337 Shape = stCircle
338338 end
339339 object Shape6: TShape
340340 Left = 144
341341 Height = 18
342342 Top = 130
343343 Width = 16
344344 Shape = stCircle
345345 end
346346 object Shape7: TShape
347347 Left = 144
348348 Height = 18
349349 Top = 158
350350 Width = 16
351351 Shape = stCircle
352352 end
353353 object Button1: TButton
354354 Left = 8
355355 Height = 25
356356 Top = 151
357357 Width = 64
358358 Caption = 'Buzzer'
359359 OnMouseDown = Button1MouseDown
360360 OnMouseUp = Button1MouseUp
361361 TabOrder = 6
362362 end
363363 object ApplicationProperties1: TApplicationProperties
364364 OnIdle = ApplicationProperties1Idle
365365 left = 72
366366 top = 152
367367 end
368368end
369369```
370370
371371### Main
372372
373373``` pascal
374374program berryclipdemo;
375375
376376{$mode objfpc}{$H+}
377377
378378uses
379379 {$IFDEF UNIX}{$IFDEF UseCThreads}
380380 cthreads,
381381 {$ENDIF}{$ENDIF}
382382 Interfaces, // this includes the LCL widgetset
383383 Forms, Unit1, pi_io_unit, uberryclip, uraspberrypi
384384 { you can add units after this };
385385
386386{$R *.res}
387387
388388begin
389389 RequireDerivedFormResource := True;
390390 Application.Initialize;
391391 Application.CreateForm(TForm1, Form1);
392392 Application.Run;
393393end.
394394```
395395
396396### Board
397397
398398#### uberryclip.pas
399399
400400``` pascal
401401unit uberryclip;
402402
403403{$mode objfpc}{$H+}
404404
405405interface
406406
407407uses
408408 Classes, SysUtils,
409409 uRaspberrypi;
410410type
411411
412412 { TBerryClip }
413413
414414 TBerryClip = class
415415 private
416416 RaspBerryPi : TRaspBerryPi;
417417 public
418418 constructor Create;
419419 procedure SetLedOn( led_no :integer );
420420 procedure SetLedOff( led_no :integer );
421421 procedure SetBuzzerOn;
422422 procedure SetBuzzerOff;
423423 function ReadSwitch:boolean;
424424 destructor Destroy; override;
425425 end;
426426implementation
427427
428428{ TBerryClip }
429429
430430constructor TBerryClip.Create;
431431begin
432432 RaspBerryPi := TRaspBerryPi.Create;
433433 RaspBerryPi.SetGpioPinOutput(GPIO4); // Led 1
434434 RaspBerryPi.SetGpioPinOutput(GPIO9); // Led 2
435435 RaspBerryPi.SetGpioPinOutput(GPIO10);// Led 3
436436 RaspBerryPi.SetGpioPinOutput(GPIO11);// Led 4
437437 RaspBerryPi.SetGpioPinOutput(GPIO17);// Led 5
438438 RaspBerryPi.SetGpioPinOutput(GPIO22);// Led 6
439439 RaspBerryPi.SetGpioPinInput(GPIO7); // Switch
440440 RaspBerryPi.SetGpioPinOutput(GPIO8); // Buzzer
441441end;
442442
443443procedure TBerryClip.SetLedOn(led_no: integer);
444444begin
445445 case led_no of
446446 1 : RaspBerryPi.SetGpioBit(GPIO4);
447447 2 : RaspBerryPi.SetGpioBit(GPIO17);
448448 3 : RaspBerryPi.SetGpioBit(GPIO22);
449449 4 : RaspBerryPi.SetGpioBit(GPIO10);
450450 5 : RaspBerryPi.SetGpioBit(GPIO9);
451451 6 : RaspBerryPi.SetGpioBit(GPIO11);
452452 end;
453453end;
454454
455455procedure TBerryClip.SetLedOff(led_no: integer);
456456begin
457457 case led_no of
458458 1 : RaspBerryPi.ClearGpioBit(GPIO4);
459459 2 : RaspBerryPi.ClearGpioBit(GPIO17);
460460 3 : RaspBerryPi.ClearGpioBit(GPIO22);
461461 4 : RaspBerryPi.ClearGpioBit(GPIO10);
462462 5 : RaspBerryPi.ClearGpioBit(GPIO9);
463463 6 : RaspBerryPi.ClearGpioBit(GPIO11);
464464 end;
465465
466466end;
467467
468468procedure TBerryClip.SetBuzzerOn;
469469begin
470470 RaspBerryPi.SetGpioBit(GPIO8);
471471end;
472472
473473procedure TBerryClip.SetBuzzerOff;
474474begin
475475 RaspBerryPi.ClearGpioBit(GPIO8);
476476end;
477477
478478function TBerryClip.ReadSwitch: boolean;
479479begin
480480 result := RaspBerryPi.GetGpioBit(GPIO7);
481481end;
482482
483483destructor TBerryClip.Destroy;
484484begin
485485 RaspBerryPi.Free;
486486 inherited Destroy;
487487end;
488488
489489end.
490490```
491491
492492#### uraspberrypi.pas
493493
494494``` pascal
495495unit uraspberrypi;
496496
497497{$mode objfpc}{$H+}
498498
499499interface
500500
501501uses
502502 Classes, SysUtils, pi_io_unit, dialogs;
503503
504504const
505505
506506 GPIO4 = 4;
507507 GPIO7 = 7;
508508 GPIO8 = 8;
509509 GPIO9 = 9;
510510 GPIO10= 10;
511511 GPIO11= 11;
512512 GPIO17= 17;
513513 GPIO22= 22;
514514
515515type
516516
517517 { TRaspBerryPi }
518518
519519 TRaspBerryPi = class
520520 private
521521 io_port: T_IO_Port;
522522 public
523523 constructor Create;
524524 procedure ClearGpioBit(no:byte);
525525 function GetGpioBit(no:byte):boolean;
526526 procedure SetGpioBit(no:byte);
527527 procedure SetGpioPinInput( no:byte);
528528 procedure SetGpioPinOutput( no:byte);
529529 destructor Destroy; override;
530530 end;
531531
532532implementation
533533
534534{ TRaspBerryPi }
535535
536536constructor TRaspBerryPi.Create;
537537begin
538538 if not MapIo
539539 then ShowMessage('Error mapping gpio')
540540 else
541541 begin
542542 io_port := T_IO_Port.Create;
543543 io_port.f_gpio := CreatePort(GPIO_BASE);
544544 end ;
545545end;
546546
547547procedure TRaspBerryPi.ClearGpioBit(no: byte);
548548begin
549549 io_port.ClearBit(no);
550550end;
551551
552552function TRaspBerryPi.GetGpioBit(no: byte): boolean;
553553begin
554554 result := io_port.GetBit(no);
555555end;
556556
557557procedure TRaspBerryPi.SetGpioBit(no: byte);
558558begin
559559 io_port.SetBit(no);
560560end;
561561
562562procedure TRaspBerryPi.SetGpioPinInput(no: byte);
563563begin
564564 io_port.SetPinMode(no,INPUT);
565565end;
566566
567567procedure TRaspBerryPi.SetGpioPinOutput(no: byte);
568568begin
569569 io_port.SetPinMode(no,OUTPUT);
570570end;
571571
572572destructor TRaspBerryPi.Destroy;
573573begin
574574 io_port.ClearBit(GPIO4);
575575 io_port.ClearBit(GPIO9);
576576 io_port.ClearBit(GPIO10);
577577 io_port.ClearBit(GPIO11);
578578 io_port.ClearBit(GPIO17);
579579 io_port.ClearBit(GPIO22);
580580 io_port.ClearBit(GPIO8);
581581 UnMapIo(io_port);
582582 io_port.free;
583583 inherited Destroy;
584584end;
585585
586586end.
587587```
588588
589589#### pi_io_unit.pas
590590
591591``` pascal
592592unit pi_io_unit;
593593// This code is tested only RPI2B Processor
594594{$mode objfpc}{$H+}
595595
596596interface
597597
598598uses
599599 Classes, SysUtils, Unix, BaseUnix;
600600
601601const
602602 BCM2709_PBASE= $3F000000; // Peripheral Base in Bytes (RPI2B Processor)
603603 // BCM2708_PBASE= $20000000; // Peripheral Base (maybe RPI1 )
604604 REG_GPIO = BCM2709_PBASE div $1000; // $3F000 ;
605605 PAGE_SIZE = $1000;
606606
607607 GPIO_BASE = (REG_GPIO + $200);
608608
609609 INPUT = 0;
610610 OUTPUT = 1;
611611
612612 GPSET = $1C; // GPIO Pin Output Set
613613 GPCLR = $28; // GPIO Pin Output Clear
614614 GPLEV = $34; // GPIO Pin Level
615615
616616type
617617
618618 { T_IO_Port }
619619
620620 T_IO_Port = class
621621 private
622622 public
623623 f_gpio: PLongWord;
624624 procedure SetPinMode(a_io_pin, a_mode: byte);
625625 function GetBit(a_io_pin : byte):boolean;inline;
626626 procedure ClearBit(a_io_pin : byte);inline;
627627 procedure SetBit(a_io_pin : byte);inline;
628628 end;
629629
630630 function MapIo:boolean;// creates io mapping
631631 procedure UnMapIo(a_Map: T_IO_Port);// close io
632632 function CreatePort(PortGpio: LongWord): PLongWord;
633633
634634implementation
635635
636636var
637637 fd: integer;// /dev/mem file handle
638638
639639function MapIo: boolean;
640640begin
641641 result := true;
642642 fd := fpopen('/dev/mem', O_RdWr or O_Sync);
643643 if fd < 0 then result := false; // unsuccessful mapping
644644end;
645645
646646procedure UnMapIo( a_Map: T_IO_Port);
647647begin
648648 if a_Map.f_gpio <> nil then
649649 begin
650650 fpMUnmap(a_Map.f_gpio, PAGE_SIZE);
651651 a_Map.f_gpio := nil;
652652 end;
653653end;
654654
655655function CreatePort(PortGpio: LongWord): PLongWord;
656656begin
657657 result := FpMmap(nil, PAGE_SIZE, PROT_READ or PROT_WRITE, MAP_SHARED, fd, PortGpio);
658658end;
659659
660660procedure T_IO_Port.SetPinMode(a_io_pin, a_mode: byte);
661661var
662662 fSel, shift : byte;
663663 iof : PLongWord;
664664begin
665665 fSel := (a_io_pin div $A)*4 ;
666666 shift := (a_io_pin mod $A)*3 ;
667667 iof := Pointer(LongWord(Self.f_gpio)+fSel);
668668 case a_mode of
669669 INPUT : iof^ := iof^ and ($FFFFFFFF - (7 shl shift));
670670 OUTPUT : iof^ := iof^ and ($FFFFFFFF - (7 shl shift)) or (1 shl shift);
671671 end;
672672end;
673673
674674procedure T_IO_Port.SetBit(a_io_pin : byte);
675675var
676676 iof : PLongWord;
677677begin
678678 iof := Pointer(LongWord(Self.f_gpio) + GPSET + (a_io_pin shr 5) shl 2);
679679 iof^ := 1 shl a_io_pin;
680680end;
681681
682682procedure T_IO_Port.ClearBit(a_io_pin : byte);
683683var
684684 iof : PLongWord;
685685begin
686686 iof := Pointer(LongWord(Self.f_gpio) + GPCLR + (a_io_pin shr 5) shl 2);
687687 iof^ := 1 shl a_io_pin;
688688end;
689689
690690function T_IO_Port.GetBit(a_io_pin : byte):boolean;
691691var
692692 iof : PLongWord;
693693begin
694694 result := true;
695695 iof := Pointer(LongWord(Self.f_gpio) + GPLEV + (a_io_pin shr 5) shl 2);
696696 if (iof^ and (1 shl a_io_pin)) = 0 then result := false;
697697end;
698698end.
699699```
700700
701701---
702702*From the Free Pascal wiki, page "Raspberry Pi : BerryClip", 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).*

Back to history