vervolg C 1 Onderwerpen voor vandaag Gelinkte lijsten

  • Slides: 25
Download presentation
vervolg C 1 Onderwerpen voor vandaag • Gelinkte lijsten • Finite State Machine (Eindige

vervolg C 1 Onderwerpen voor vandaag • Gelinkte lijsten • Finite State Machine (Eindige Toestands Machine) Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 2 Lijst operaties 1. Voeg 2. Haal 3. Voeg 4. Haal 5.

vervolg C 2 Lijst operaties 1. Voeg 2. Haal 3. Voeg 4. Haal 5. Voeg 6. Haal toe weg achteraan (enqueue) vooraan (dequeue, pop) vooraan (push) achteraan (? ) in ‘t midden Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 3 Singly linked, double linked Hogeschool van Utrecht / Institute for Computer,

vervolg C 3 Singly linked, double linked Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 4 Ending, circular Hogeschool van Utrecht / Institute for Computer, Communication and

vervolg C 4 Ending, circular Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 5 Insert, delete (singly linked) Hogeschool van Utrecht / Institute for Computer,

vervolg C 5 Insert, delete (singly linked) Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 6 Insert, delete (double linked) Hogeschool van Utrecht / Institute for Computer,

vervolg C 6 Insert, delete (double linked) Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 7 Sentinels Sentinel ( ~ wachter ): dummy queue element dat er

vervolg C 7 Sentinels Sentinel ( ~ wachter ): dummy queue element dat er altijd is. 1 bij circulaire lijsten, 2 bij ‘eindigende’ lijsten. Voordeel: geen apart geval meer in de code voor begin of einde van de lijst. Nadeel: extra geheugen nodig voor de sentinel. Sentinel == zelfde type als lijst elementen, dus als een lijstelement ‘groot’ is kan dit kostbaar zijn. Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 8 lijsten Link Vorm Sentinels Enkel Eindig Nee Eenvoudig concept Enkel Eindig

vervolg C 8 lijsten Link Vorm Sentinels Enkel Eindig Nee Eenvoudig concept Enkel Eindig Ja Code voor lege lijst en laatste element eenvoudiger Enkel Circulair Nee Geen aparte ‘first’ en ‘last’ pointers nodig Enkel Circulair ja Dubbel Eindig Nee Dubbel Eindig Ja Dubbel Circulair Nee Dubbel Circulair ja Delete kan met alleen pointer naar het element Ingewikkeld concept, maar de eenvoudigste code, zonder if’s Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 9 Finite State Machine (= Eindige Toestands Machine) • states (toestanden) •

vervolg C 9 Finite State Machine (= Eindige Toestands Machine) • states (toestanden) • events (gebeurtenissen, boodschappen, triggers) • actions (wat je programma doet) Diverse notaties in gebruik, schrik niet als je een iets andere notatie tegenkomt. Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 10 FSM voorbeeld : knipper LED op 1/2 Hz States : –

vervolg C 10 FSM voorbeeld : knipper LED op 1/2 Hz States : – LED is aan – LED is uit Events : – 1 seconde timer tick Actions : – LED aanzetten – LED uitzetten Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 11 knipper LED op 1/2 Hz : FSM diagram init LED uitzetten

vervolg C 11 knipper LED op 1/2 Hz : FSM diagram init LED uitzetten LED is uit 1 s timer tick LED aanzetten LED is aan 1 s timer tick LED uitzetten Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 12 knipper LED op 1/2 Hz : State-Transistion diagram Event 1 s

vervolg C 12 knipper LED op 1/2 Hz : State-Transistion diagram Event 1 s timer tick State LED is uit LED aanzetten LED is aan LED uitzetten LED is uit Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 13 FSM voorbeeld : knipper LED op 1/2 Hz // states #define

vervolg C 13 FSM voorbeeld : knipper LED op 1/2 Hz // states #define knipper_state_init 0 #define knipper_state_uit 1 #define knipper_state_aan 2 void fsm_knipper( int & state, int led, int event ){ if( *state == knipper_state_init ){ Zet_LED( led, 0 ); *state = knipper_state_uit; } // events #define knipper_event_tick 100 if( *state == knipper_state_uit ){ if( event == knipper_event_tick ){ Zet_LED( led, 1 ); *state = knipper_state_aan; return; } } // current state int knipper_state = knipper_state_init; // acties void Zet_LED( int led, int x ){. . . } if( *state == knipper_state_aan ){ if( event == knipper_event_tick ){ Zet_LED( led, 0 ); *state = knipper_state_uit; return; } } } Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 14 FSM voorbeeld : Toegangspoort 1 Hogeschool van Utrecht / Institute for

vervolg C 14 FSM voorbeeld : Toegangspoort 1 Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 15 FSM voorbeeld : Toegangspoort 2 Hogeschool van Utrecht / Institute for

vervolg C 15 FSM voorbeeld : Toegangspoort 2 Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 16 FSM voorbeeld : Toegangspoort 3 Hogeschool van Utrecht / Institute for

vervolg C 16 FSM voorbeeld : Toegangspoort 3 Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 17 FSM voorbeeld : Toegangspoort 3 Event Coin Pass Reset Ready Locked

vervolg C 17 FSM voorbeeld : Toegangspoort 3 Event Coin Pass Reset Ready Locked Unlocked Reset. Alarm() Lock() Reset. Alarm() State Locked Unlock() Alarm() Unlocked Violation Thankyou() Lock() Unlocked Locked Violation Locked Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C // states #define poort_state_init 0 #define poort_state_locked 1 #define poort_state_unlocked 2 #define

vervolg C // states #define poort_state_init 0 #define poort_state_locked 1 #define poort_state_unlocked 2 #define poort_state_violation 3 // events #define poort_event_coin 100 #define poort_event_pass 100 #define poort_event_reset 100 18 void fms_poort( int event ){ if( state == poort_state_init ){ poort_lock(); state = poort_state_locked; } if( state == poort_state_locked ){ if( event == poort_event_coin ){ poort_unlock(); state = poort_state_unlocked(); } if( event == poort_event_pass ){ poort_alarm(); state = poort_state_violation(); } return; } // current state int poort_state = poort_state_init; // actions void poort_thankyou( void ); void poort_lock( void ); void poort_unlock( void ); void poort_start_alarm( void ); void poort_stop_alarm( void ); . . . } Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 19 knipper LED op X Hz : State-Transistion diagram Event 1 ms

vervolg C 19 knipper LED op X Hz : State-Transistion diagram Event 1 ms timer tick State LED is uit n++; if( n == 500/x ){ LED aanzetten n = 0; state = LED is aan } LED is aan n++; if( n == 500/x ){ LED uitzetten n = 0; state = LED is uit } Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 20 FSM voorbeeld : knipper LED op X Hz void fsm_knipper( int

vervolg C 20 FSM voorbeeld : knipper LED op X Hz void fsm_knipper( int & state, int led, int X, int *n, int event ){ if( *state == knipper_state_init ){ Zet_LED( led, 0 ); *state = knipper_state_uit; *n == 0; return; } if( *state == knipper_state_uit ){ if( event == knipper_event_tick ){ *n++; if( *n == 500/x ){ Zet_LED( led, 1 ); *n = 0; *state = knipper_state_aan; } return; } } if( *state == knipper_state_aan ){ if( event == knipper_event_tick ){ *n++; if( *n == 500/x ){ Zet_LED( led, 0 ); *n = 0; *state = knipper_state_uit; } return; } Hogeschool van Utrecht / Institute for Computer, } } Communication and Media Technology

vervolg C 21 FSM voorbeeld : knipper diverse LEDs while( 1 ){ wait_1 ms();

vervolg C 21 FSM voorbeeld : knipper diverse LEDs while( 1 ){ wait_1 ms(); fsm_knipper(. . . &state 1, &state 2, &state 3, &state 4, l, 2, 3, 4, 10, 12, 300, 5, &n 1, &n 2, &n 3, &n 4, knipper_event_tick ); ); } Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C Rotary encoder (quadrature encoder) Sensor voor het bijhouden van de draaiing van

vervolg C Rotary encoder (quadrature encoder) Sensor voor het bijhouden van de draaiing van een as. Toegepassingen: – user interface (afstemknop) – computermuis – motorsturing Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 22

vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 23

vervolg C Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology 23

vervolg C 24 Opdracht ”rotary encoder” Een rotary encoder FSM moet in de stand

vervolg C 24 Opdracht ”rotary encoder” Een rotary encoder FSM moet in de stand van een rotary encoder bijhouden. Bij een click naar rechts +1, bij een klick naar links -1. Er moet rekening gehouden worden met denderen en ‘halve bewegingen’ (= effect is hetzelfde als denderen). Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

vervolg C 25 Opdracht ”rotary encoder” Stel de lijst van states (4), events (4)

vervolg C 25 Opdracht ”rotary encoder” Stel de lijst van states (4), events (4) en acties (2), op voor een rotary encoder FSM - Maak het State Transition Diagram (er zijn ‘fysiek-logisch onmogelijke’ events, die hoef je niet te laten zien) - Vertaal je STD naar C code Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology