Skip to content

Commit 2d75871

Browse files
MikschoRetoStutzRetoStutz
authored
corrected MVC, now with more hints (#68)
* corrected MVC, now with more hints * new mording mvc lesson Co-authored-by: RetoStutz <[email protected]> Co-authored-by: RetoStutz <[email protected]>
1 parent 95407e0 commit 2d75871

File tree

17 files changed

+120
-104
lines changed

17 files changed

+120
-104
lines changed
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
type: choice
22
is_multiple_choice: true
33
options:
4-
- text: Ich kenne den groben Inhalt des Hardwarekataloges und weiss, wie ich diesen in meinem Projekt einsetzen kann.
4+
- text: "Ich kenne den groben Inhalt des Hardwarekataloges und weiss, wie ich diesen\
5+
\ in meinem Projekt einsetzen kann."
56
is_correct: true
6-
- text: Ich weiss, wo ich im Hardwarekatalog Hilfe zur elektrischen Montage der Bauteile bekomme.
7+
- text: "Ich weiss, wo ich im Hardwarekatalog Hilfe zur elektrischen Montage der Bauteile\
8+
\ bekomme."
79
is_correct: true
8-
- text: Ich kenne die Funktionen von SimpleLED und SimpleButton und kann diese in einer einfache Applikation verwenden.
10+
- text: Ich kenne die Funktionen von SimpleLED und SimpleButton und kann diese in
11+
einer einfache Applikation verwenden.
912
is_correct: true
1013
- text: Ich kann eine einfache Applikation auf dem Raspberry Pi starten.
1114
is_correct: true
1215
files:
13-
- name: src/Component.java
14-
visible: false
15-
- name: src/ErsteKomponente-ImplementationButton.run.xml
16-
visible: false
1716
- name: src/Main.java
1817
visible: true
19-
- name: src/PIN.java
20-
visible: false
2118
- name: src/SimpleButton.java
2219
visible: true
2320
- name: src/SimpleLED.java
2421
visible: true
22+
- name: src/PIN.java
23+
visible: true
24+
- name: src/ErsteKomponente-ImplementationButton.run.xml
25+
visible: true
26+
- name: src/Component.java
27+
visible: true
28+
local_check: true

Tutorial/Erste Komponente/Zusammenfassung/task-info.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ type: theory
22
files:
33
- name: src/Main.java
44
visible: true
5-
- name: src/Component.java
6-
visible: false
7-
- name: src/PIN.java
8-
visible: false
95
- name: src/SimpleButton.java
106
visible: true
11-
- name: src/ErsteKomponente-ImplementationButton.run.xml
12-
visible: false
137
- name: src/SimpleLED.java
148
visible: true
9+
- name: src/PIN.java
10+
visible: true
11+
- name: src/ErsteKomponente-ImplementationButton.run.xml
12+
visible: true
13+
- name: src/Component.java
14+
visible: true

Tutorial/ModelViewController/Kontroller/src/controller/Controller.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ public Controller(Model model) {
1212
}
1313

1414
public void pressButton(){
15-
//whenever the button is pressed, the model is updated
15+
//whenever the function pressbutton is called, the model will be updated
16+
//increment counter from model by one
1617
increase(model.counter);
1718

18-
//what happens when we press the button?
19+
//set value ledGlows to true
1920
setValue(model.ledGlows, true);
2021

2122
//using 'runLater' assures that new value is set on model
@@ -27,7 +28,8 @@ public void pressButton(){
2728
}
2829

2930
public void ledOff(){
30-
//what happens, when we lift the button?
31+
//whenever the function ledOff is called, the model will be updated
32+
// set value ledGlows to false
3133
setValue(model.ledGlows, false);
3234
}
3335

Tutorial/ModelViewController/Kontroller/src/view/View.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import view.components.helpers.PIN;
1010

1111
public class View extends PuiBase<Model, Controller> {
12-
//declare all hardware components attached to RaspPi
12+
//declare all hardware components attached to Pi
1313
//these are protected to give unit tests access to them
1414
protected SimpleButton button;
1515
protected SimpleLed led;
@@ -20,7 +20,7 @@ public View(Controller controller, Context pi4J) {
2020

2121
@Override
2222
public void initializeParts() {
23-
//Which components are we using?
23+
//Which components are we using? use the PIN PWM19 for the new component
2424
button = new SimpleButton(pi4J, PIN.D26, false);
2525
led = new SimpleLed(pi4J, PIN.PWM19);
2626
}
@@ -29,19 +29,20 @@ public void initializeParts() {
2929
public void shutdown() {
3030
//what is there to do when we shut down the app?
3131
button.deRegisterAll();
32+
led.off();
3233
super.shutdown();
3334
}
3435

3536
@Override
3637
public void setupUiToActionBindings(Controller controller) {
37-
//what happens, when we interact with the hardware?
38+
//which methods of the controller must be called on a hardware event
3839
button.onDown(controller::pressButton);
3940
button.onUp(controller::ledOff);
4041
}
4142

4243
@Override
4344
public void setupModelToUiBindings(Model model) {
44-
//what happens, when the model registered that we interacted with the components?
45+
//which event should be triggered when the model changes
4546
onChangeOf(model.counter)
4647
.execute((oldValue, newValue) -> {
4748
if (newValue != null) {

Tutorial/ModelViewController/Kontroller/task-info.yaml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ files:
55
- name: src/view/View.java
66
visible: true
77
placeholders:
8-
- offset: 446
8+
- offset: 442
99
length: 27
1010
placeholder_text: /* TODO declare the SimpleLed. */
11-
- offset: 726
11+
- offset: 762
1212
length: 40
1313
placeholder_text: /* TODO initialize the SimpleLed */
14-
- offset: 1134
14+
- offset: 918
15+
length: 42
16+
placeholder_text: /* TODO deregister the functions and shutdown the components.
17+
Don't forget to turn off the LED */
18+
- offset: 1206
1519
length: 32
1620
placeholder_text: /* TODO register on the event "onUp" from the button the function
1721
"ledOff" of the controller */
18-
- offset: 2137
22+
- offset: 1649
1923
length: 85
2024
placeholder_text: "/* TODO create a listener on the \"ledGlows\" of the model,\
2125
\ and toggle the led */"
@@ -32,10 +36,10 @@ files:
3236
- name: src/controller/Controller.java
3337
visible: true
3438
placeholders:
35-
- offset: 497
39+
- offset: 549
3640
length: 31
3741
placeholder_text: /* TODO refresh the model to turn on the led */
38-
- offset: 824
42+
- offset: 942
3943
length: 32
4044
placeholder_text: /* TODO refresh the model to turn off the led */
4145
- name: src/util/mvcbase/ConcurrentTaskQueue.java

Tutorial/ModelViewController/Kontroller/task.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
# Task 5/8: Kontroller
22
In dieser Aufgabe geht es darum, den Kontroller in der ModelViewController-App zu schreiben und die
33
View mit einer LED zu erweitern.
4-
5-
---
64
> **_Hinweis:_**
75
>
86
> Die Klassen *SimpleButton* und *SimpleLed* wurden bereits aus dem Hardwarekatalog kopiert und
97
> in den Ordner view.components eingefügt.
10-
---
118
129
## Aufgabe
13-
Die View und der Kontroller sollen implementiert werden.
10+
Um die Aufgabe zu erfüllen, müssen die Klassen *Controller.java* und *View.java* angepasst werden.
11+
> **_Hinweise:_**
12+
> Es existiert im Model neu die Variable *LedGlows*. Diese soll den Zustand der LED wiedergeben. TRUE bedeutet, die LED
13+
> leuchtet und FALSE bedeutet, die LED ist dunkel.
1414
1515
### Programmierung
16+
*Kontroller*
17+
- Auf das Ereignis Button gedrückt: aktualisiere im Model den Status von *ledGlows*.
18+
- Auf das Ereignis Button nicht mehr gedrückt: aktualisiere im Model den Status von *ledGlows*.
19+
20+
<div class="hint">
21+
Die Variablen des Models können mit <i>GetValue</i> und <i>SetValue</i> abgefragt oder geändert werden.
22+
</div>
23+
1624
*View*
1725
- Deklaration von SimpleLed.
1826
- Initialisation von SimpleLed.
19-
- Registration des Event *onUp* des Buttons auf die Funktion *ledOff* des Kontrollers.
20-
- Ein Listener auf die variable *LedGlows* des Models soll die LED steuern.
27+
- Registration der Funktion *ledOff* vom Kontroller beim Event *onUp* des Buttons.
28+
- Mit *LedGlows* des Models die LED steuern.
2129

22-
*Kontroller*
23-
- Auf das Drücken des Buttons: die LED soll leuchten nach update des Models.
24-
- Auf das Beenden des Drückens des Buttons: die LED soll nicht mehr leuchten nach update des Models.
30+
<div class="hint">
31+
Für die Registration von <i>ledOff</i> und die Ansteuerung der LED kann fast der gleiche Syntax wie für <i>pressButton</i>
32+
oder die Ausgabe des Counters verwendet werden.
33+
</div>
2534

26-
---
27-
> **_Hinweise:_**
28-
> Die Aufgabe erfordert Änderungen in der View und dem Kontroller.
29-
>
30-
> Es existiert im Model neu die Variable *LedGlows*. Sobald diese auf *True* gesetzt ist, soll auch die LED leuchten.
31-
> Wenn der Button nicht mehr gedrückt wird, soll die LED nicht mehr leuchten.
32-
---
3335

3436
### Check Programmierung
3537
Die Programmierung lässt sich mit dem Button *Check* überprüfen. Nach erfolgreich abgeschlossenem Test kann mit der

Tutorial/ModelViewController/Model/src/controller/Controller.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ public Controller(Model model) {
1212
}
1313

1414
public void pressButton(){
15-
//whenever the button is pressed, the model is updated
15+
//whenever the function pressbutton is called, the model will be updated
16+
//set new message
1617
setValue(model.message, "You pressed the Button "+(get(model.counter)+1)+" Times");
17-
18+
//increment counter from model by one
1819
increase(model.counter);
1920

20-
//what happens when we press the button?
21+
//set value ledGlows to true
2122
setValue(model.ledGlows, true);
2223

2324
//using 'runLater' assures that new value is set on model

Tutorial/ModelViewController/Model/src/model/Model.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import util.mvcbase.ObservableValue;
44

55
public class Model {
6+
//Variables that are to be available in the view must be recorded as observables.
7+
//This allows project-specific functions to be called which automatically update the view
8+
//in the event of value changes.
69
public final ObservableValue<Integer> counter = new ObservableValue<>(0);
710
public final ObservableValue<Boolean> ledGlows = new ObservableValue<>(false);
811
public final ObservableValue<String> message = new ObservableValue<>("You pressed the Button 0 Times");

Tutorial/ModelViewController/Model/src/view/View.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import view.components.helpers.PIN;
1111

1212
public class View extends PuiBase<Model, Controller> {
13-
//declare all hardware components attached to RaspPi
13+
//declare all hardware components attached to Pi
1414
//these are protected to give unit tests access to them
1515
protected LedButton ledButton;
1616

@@ -20,27 +20,28 @@ public View(Controller controller, Context pi4J) {
2020

2121
@Override
2222
public void initializeParts() {
23-
//Which components are we using?
23+
//Which components are we using? use the PINs D26 and PWM19
2424
ledButton = new LedButton(pi4J, PIN.D26, false, PIN.PWM19);
2525
}
2626

2727
@Override
2828
public void shutdown() {
2929
//what is there to do when we shut down the app?
3030
ledButton.btnDeRegisterAll();
31+
ledButton.ledOff();
3132
super.shutdown();
3233
}
3334

3435
@Override
3536
public void setupUiToActionBindings(Controller controller) {
36-
//what happens, when we interact with the hardware?
37+
//which methods of the controller must be called on a hardware event
3738
ledButton.btnOnDown(controller::pressButton);
3839
ledButton.btnOnUp(controller::ledOff);
3940
}
4041

4142
@Override
4243
public void setupModelToUiBindings(Model model) {
43-
//what happens, when the model registered that we interacted with the components?
44+
//which event should be triggered when the model changes
4445
onChangeOf(model.message).execute((oldValue, newValue) -> System.out.println(newValue));
4546

4647
onChangeOf(model.ledGlows).execute(((oldValue, newValue) -> ledButton.ledSetState(newValue)));

Tutorial/ModelViewController/Model/task-info.yaml

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,27 @@ files:
1515
- name: src/view/View.java
1616
visible: true
1717
placeholders:
18-
- offset: 445
18+
- offset: 441
1919
length: 30
2020
placeholder_text: /* TODO declare the LedButton */
21-
- offset: 671
21+
- offset: 694
2222
length: 59
2323
placeholder_text: /* TODO initialize the LedButton */
24-
- offset: 846
25-
length: 29
24+
- offset: 869
25+
length: 57
2626
placeholder_text: /* TODO implement the Shutdown */
27-
- offset: 1056
27+
- offset: 1124
2828
length: 92
29-
placeholder_text: /* TODO connect the interactions from the button with the function of the controller
30-
*/
31-
- offset: 1322
29+
placeholder_text: /* TODO connect the interactions from the button with the function
30+
of the controller */
31+
- offset: 1365
3232
length: 88
33-
placeholder_text: /* TODO give out the message stored in the model to the console, when the message changes */
34-
- offset: 1420
33+
placeholder_text: "/* TODO give out the message stored in the model to the console,\
34+
\ when the message changes */"
35+
- offset: 1463
3536
length: 94
36-
placeholder_text: /* TODO create a listener on the "ledGlows" of the model, and toggle the led */
37+
placeholder_text: "/* TODO create a listener on the \"ledGlows\" of the model,\
38+
\ and toggle the led */"
3739
- name: src/view/components/helpers/PIN.java
3840
visible: false
3941
- name: src/view/components/Component.java
@@ -47,13 +49,14 @@ files:
4749
- name: src/model/Model.java
4850
visible: true
4951
placeholders:
50-
- offset: 241
52+
- offset: 458
5153
length: 105
52-
placeholder_text: /* TODO create an Observable of type String with the name "message" */
54+
placeholder_text: /* TODO create an Observable of type String with the name "message"
55+
*/
5356
- name: src/controller/Controller.java
5457
visible: true
5558
placeholders:
56-
- offset: 414
59+
- offset: 458
5760
length: 83
5861
placeholder_text: /* TODO refresh the model with the new message */
5962
- name: src/ModelViewController-Model.run.xml

0 commit comments

Comments
 (0)